Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/audio_hal_exe_calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ frame count of the Opus packet's table-of-contents byte (RFC 6716, section 3.1)
`xapu_chad_test` covers the layouts and the silent path; `xapu_opus_test` covers the decoded path with a generated
packet.

The `chad.dll` signatures, structure layouts, limits and error codes come from the same project
(`crates/victory-hle/src/modules/dsp.rs` at commit `ffe310e`), facts only; the top of `xapu_chad.cpp` lists them. They
fit the shapes above: the title's `0x80` is the stream limit, `XDspActivate`'s two outputs are a 16-byte status block
and the stream, and the dwords the title waits on are the status block's last completed ticket (+4) and its result
(+0xc). Commands complete when submitted and their output is zeros: no convolution is computed.

## `xg_xs.dll` and `XCRDAPI.dll`

| Import | Sites | What the sites show |
Expand Down
7 changes: 4 additions & 3 deletions docs/runtime_imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ input. Activate, process, deactivate and reset commands are copied from the titl
result in order. A process command decodes its Opus packet through `runtime/opus_decoder` (Rust over the BSD-3-Clause
`opus-pure`) into the slot's output, honouring first frame, frame count, output length and the deinterleaved option;
without the crate the result carries the table-of-contents frame count and silence. The layouts follow Marvisak/VIctory
`crates/victory-hle/src/modules/apu.rs` (GPL-3.0-or-later). `XDspActivate` writes its two outputs as pointers to zeroed
memory, which the title reads as "command done" after each `XDspSubmitCommand`; the DSP stays silent. `xapu_chad_test`
covers the layouts, the queue and the silent path; `xapu_opus_test` the decoded path. With `--trace`, the first call of
`crates/victory-hle/src/modules/apu.rs` (GPL-3.0-or-later). The `chad.dll` layouts, limits and error codes follow
`crates/victory-hle/src/modules/dsp.rs` (same licence and commit): `XDspActivate` hands out a stream and its 16-byte
status block, and each `XDspSubmitCommand` completes at once, advancing the ticket the title waits on and writing
zeros as its output, so the DSP's reverb stays dry. `xapu_chad_test` covers the layouts, the queues and the silent path; `xapu_opus_test` the decoded path. With `--trace`, the first call of
each, and the first command of each type, is an `audio_queue` event.
The table above was measured before this shim and still lists these ten as unimplemented; they are served now. The call shapes agree with `docs/audio_hal_exe_calls.md`, which was measured with `tools/import_call_sites.py`.

Expand Down
193 changes: 164 additions & 29 deletions runtime/shims/xapu_chad.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// chad.dll: the console's audio DSP command queue. No PC has one, so this gives the title a connection whose calls
// succeed and zeroed DSP status: nothing the title submits is processed. xapu.dll, the APU command queue, is in
// xapu_queue.cpp.
// chad.dll: the console's audio DSP command queue. No PC has one, so this gives the title connections and streams
// whose commands complete at once with silent output: nothing the title submits is processed. xapu.dll, the APU
// command queue, is in xapu_queue.cpp.
//
// Read from the executable's call sites (counts in docs/runtime_imports.md), statically, never run:
// XDspConnectWithMaximumStreamLimit(buffer, u32, u32, 0x80, 1, handle*) 1 site; success is a return of 0. The
Expand All @@ -14,11 +14,36 @@
// at +4 equals *id.
// XDspDeactivate, XDspDisconnect 1 site each, tail calls: the caller returns what they return.
//
// HYPOTHESIS: XDspActivate's outputs point at zeroed memory owned by the connection. XDspSubmitCommand leaves *id at
// the title's 0, so the zeroed status reads as "command 0 done, no error" and the title does not wait.
// chad.dll's signatures, layouts, limits and error codes are facts recorded by Marvisak/VIctory (GPL-3.0-or-later,
// crates/victory-hle/src/modules/dsp.rs at commit ffe310e), which runs the retail executable through these calls; see
// docs/related_projects.md. Nothing of its code is copied. They fit every chad shape above:
// HRESULT XDspConnectWithMaximumStreamLimit(void* base, uint32_t base_bytes, uint32_t ir_seconds,
// uint32_t max_streams, uint32_t max_channels, DspConnection** out)
// E_INVALIDARG for a null base, base_bytes outside 0x10000..0x1F400000, ir_seconds above 0x80,
// max_streams above 0x100 or max_channels above 2; then E_POINTER for a null out.
// HRESULT XDspActivate(DspConnection*, const DspStreamDesc*, const DspStatus** status, DspStream** stream)
// E_POINTER for a null status or stream out; E_INVALIDARG for a null connection or description
// or a description that breaks the rules below; E_OUTOFMEMORY once max_streams are active.
// The status block starts zeroed with result S_OK. A description is valid when:
// kind 1 (convolution): 1 or 2 channels, 0x200 or 0x400 frames, option 0x4 clear;
// any other kind: 1 channel, 0x200, 0x400 or 0x800 frames;
// and options 0x2 and 0x8 need 2 channels.
// HRESULT XDspSubmitCommand(DspStream*, const DspCommand*, uint32_t* ticket)
// E_INVALIDARG for a null stream or command. The command completes at once: frames * channels
// floats of output (when not null) are written, the status block's submitted and completed
// words advance to the next ticket and its result is S_OK, and *ticket (when not null) gets
// that ticket. This is the wait above: completed (+4) equals *id and result (+0xc) is 0.
// HRESULT XDspDeactivate(DspStream*), HRESULT XDspDisconnect(DspConnection*) E_INVALIDARG for null.
// No convolution or other block transform is computed: the hardware's impulse response layout is not known, so the
// output of every command is zeros and reverb returns are dry.
//
// HYPOTHESIS: a connection or stream handle that is unknown or already released is refused with E_INVALIDARG, and
// XDspDisconnect with streams still active releases them, so their handles are then refused too. The title releases
// its streams before it disconnects; what the console does otherwise is not recorded.
#include <windows.h>
#include <intrin.h>

#include <cstddef>
#include <cstdint>
#include <cstring>
#include <mutex>
Expand All @@ -31,11 +56,80 @@

namespace {

#pragma pack(push, 1)
struct DspStreamDesc {
uint32_t kind; // 1: convolution
uint32_t options;
uint32_t frames; // per block
uint32_t channels;
uint32_t ir_floats;
const float* ir; // impulse response, in the hardware's layout
};
#pragma pack(pop)
struct DspStatus { // the block the title polls
uint32_t submitted;
uint32_t completed; // ticket of the last completed command
uint32_t reserved;
HRESULT result; // of that command
};
struct DspCommand { // one block
float gains[2];
const float* input;
float* output;
uint64_t reserved;
};
static_assert(sizeof(DspStreamDesc) == 0x1C && offsetof(DspStreamDesc, ir) == 0x14, "packed description");
static_assert(sizeof(DspStatus) == 16 && offsetof(DspStatus, completed) == 4 && offsetof(DspStatus, result) == 0xC,
"the dwords the title's wait reads");
static_assert(sizeof(DspCommand) == 32 && offsetof(DspCommand, output) == 0x10, "gains, input, output, reserved");

constexpr uint32_t kDspConvolution = 1;
constexpr uint32_t kDspStereoOptions = 0x2 | 0x8; // need two channels
constexpr uint32_t kDspNotForConvolution = 0x4;
constexpr uint32_t kDspMinBaseBytes = 0x10000;
constexpr uint32_t kDspMaxBaseBytes = 0x1F400000;
constexpr uint32_t kDspMaxIrSeconds = 0x80;
constexpr uint32_t kDspMaxStreams = 0x100;
constexpr uint32_t kDspMaxChannels = 2;

struct DspStream;
struct DspConnection {
uint8_t status[256];
uint8_t stream[256];
uint32_t max_streams;
std::set<DspStream*> streams;
};
struct DspStream {
DspConnection* owner;
uint32_t frames;
uint32_t channels;
DspStatus status; // handed to the title by XDspActivate
};

// Every live handle, so a stale or doubled release is refused instead of freeing twice. One lock for all: the title
// submits one command per block, so there is nothing to contend.
std::mutex g_dsp_mutex;
std::set<DspConnection*> g_dsp_connections;
std::set<DspStream*> g_dsp_streams;

bool dsp_description_valid(const DspStreamDesc& d) {
if ((d.options & kDspStereoOptions) && d.channels != 2) return false;
if (d.kind == kDspConvolution) {
if (d.options & kDspNotForConvolution) return false;
return (d.channels == 1 || d.channels == 2) && (d.frames == 0x200 || d.frames == 0x400);
}
return d.channels == 1 && (d.frames == 0x200 || d.frames == 0x400 || d.frames == 0x800);
}

// The title polls the status block from other threads: each word is one interlocked store, result before completed.
void dsp_store(uint32_t& word, uint32_t value) {
InterlockedExchange(reinterpret_cast<volatile LONG*>(&word), static_cast<LONG>(value));
}

void dsp_release_stream(DspStream* s) { // under g_dsp_mutex
s->owner->streams.erase(s);
g_dsp_streams.erase(s);
delete s;
}

// gta6rt --trace FILE: the first call of each function, with the caller, so the first boot shows what the title sends
// without a line per call.
void note(const char* api, uint64_t value, const void* caller) {
Expand All @@ -52,43 +146,84 @@ void note(const char* api, uint64_t value, const void* caller) {
rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller))));
}

HRESULT __stdcall XDspConnectWithMaximumStreamLimit(void* buffer, uint32_t a, uint32_t b, uint32_t max_streams,
uint32_t flags, void** handle) {
HRESULT __stdcall XDspConnectWithMaximumStreamLimit(void* base, uint32_t base_bytes, uint32_t ir_seconds,
uint32_t max_streams, uint32_t max_channels,
DspConnection** handle) {
note("XDspConnectWithMaximumStreamLimit", max_streams, _ReturnAddress());
(void)buffer;
(void)a;
(void)b;
(void)flags;
if (!handle) return E_INVALIDARG;
*handle = new (std::nothrow) DspConnection{};
return *handle ? S_OK : E_OUTOFMEMORY;
if (!base || base_bytes < kDspMinBaseBytes || base_bytes > kDspMaxBaseBytes || ir_seconds > kDspMaxIrSeconds ||
max_streams > kDspMaxStreams || max_channels > kDspMaxChannels)
return E_INVALIDARG;
if (!handle) return E_POINTER;
auto* c = new (std::nothrow) DspConnection{max_streams, {}}; // a throwing new would unwind into the title
if (!c) return E_OUTOFMEMORY;
try {
const std::lock_guard lock(g_dsp_mutex);
g_dsp_connections.insert(c);
} catch (...) {
delete c;
return E_OUTOFMEMORY;
}
*handle = c;
return S_OK;
}

HRESULT __stdcall XDspDisconnect(void* handle) {
HRESULT __stdcall XDspDisconnect(DspConnection* c) {
note("XDspDisconnect", 0, _ReturnAddress());
delete static_cast<DspConnection*>(handle);
const std::lock_guard lock(g_dsp_mutex);
if (!c || !g_dsp_connections.erase(c)) return E_INVALIDARG;
while (!c->streams.empty()) dsp_release_stream(*c->streams.begin());
delete c;
return S_OK;
}

HRESULT __stdcall XDspActivate(void* handle, const void* description, void** status, void** stream) {
note("XDspActivate", 0, _ReturnAddress());
(void)description;
auto* c = static_cast<DspConnection*>(handle);
if (!c) return E_INVALIDARG;
if (status) *status = c->status;
if (stream) *stream = c->stream;
HRESULT __stdcall XDspActivate(DspConnection* c, const DspStreamDesc* description, const DspStatus** status,
DspStream** stream) {
note("XDspActivate", description ? description->kind : ~0ull, _ReturnAddress());
if (!status || !stream) return E_POINTER;
if (!c || !description) return E_INVALIDARG;
DspStreamDesc d;
std::memcpy(&d, description, sizeof d);
if (!dsp_description_valid(d)) return E_INVALIDARG;
const std::lock_guard lock(g_dsp_mutex);
if (!g_dsp_connections.count(c)) return E_INVALIDARG;
if (c->streams.size() >= c->max_streams) return E_OUTOFMEMORY;
auto* s = new (std::nothrow) DspStream{c, d.frames, d.channels, {0, 0, 0, S_OK}};
if (!s) return E_OUTOFMEMORY;
try {
g_dsp_streams.insert(s);
c->streams.insert(s);
} catch (...) {
g_dsp_streams.erase(s);
delete s;
return E_OUTOFMEMORY;
}
*status = &s->status;
*stream = s;
return S_OK;
}

HRESULT __stdcall XDspDeactivate(void* handle, uint64_t, uint64_t, uint64_t) {
HRESULT __stdcall XDspDeactivate(DspStream* s) {
note("XDspDeactivate", 0, _ReturnAddress());
(void)handle;
const std::lock_guard lock(g_dsp_mutex);
if (!s || !g_dsp_streams.count(s)) return E_INVALIDARG;
dsp_release_stream(s);
return S_OK;
}

// *id stays the 0 the title wrote, which the zeroed status block already reports as done.
HRESULT __stdcall XDspSubmitCommand(void*, void*, uint32_t*) {
// Completes at once with a block of silence: no convolution is computed.
HRESULT __stdcall XDspSubmitCommand(DspStream* s, const DspCommand* command, uint32_t* ticket) {
note("XDspSubmitCommand", 0, _ReturnAddress());
if (!s || !command) return E_INVALIDARG;
DspCommand cmd;
std::memcpy(&cmd, command, sizeof cmd);
const std::lock_guard lock(g_dsp_mutex);
if (!g_dsp_streams.count(s)) return E_INVALIDARG;
if (cmd.output) std::memset(cmd.output, 0, size_t{s->frames} * s->channels * sizeof(float));
const uint32_t next = s->status.submitted + 1; // wraps like the 32-bit word it is
dsp_store(s->status.submitted, next);
dsp_store(reinterpret_cast<uint32_t&>(s->status.result), static_cast<uint32_t>(S_OK));
dsp_store(s->status.completed, next);
if (ticket) *ticket = next;
return S_OK;
}

Expand Down
Loading
Loading