Files
mandy/runtime/test/chad_dsp_test.cpp
NotRexed 908fb65d8a runtime: port VIctory's audio HLE (xapu, chad, acphal coprocessor, XMA)
Ported from VIctory (victory/crates/victory-hle/src/modules/apu.rs, dsp.rs,
audio.rs; victory-formats/src/xma.rs), GPL-3.0-or-later, Marvisak and
contributors (DexNom fork), keeping its measured ABI: XApu records
(CommandId 0x18, Activate 0x38, Process 0x40, ApuResult 0x50), command
kinds, options and HRESULTs; XDsp StreamDesc 0x1c, Status 16, Command
0x20; IAcpHal slot order, 28-byte messages, command ids 1-3, 5-8, 18;
SHAPE_CONTEXTS 0x60.

- shims/xapu.cpp (xapu.dll): queues and results are real. PLACEHOLDER
  decoding: no Opus decoder is in the repository (VIctory uses the
  opus-pure crate), so a process command parses its packet
  (src/opus_packet.h, RFC 6716 framing as libopus checks it) for its
  frame count, output length and errors, and writes silence. Malformed
  packets fail with BAD_PACKET; rate conversion stays E_NOTIMPL.
- shims/chad_dsp.cpp (chad.dll): completion (status words, tickets) is
  real; convolutions and transforms output silence, as in VIctory.
- shims/acphal.cpp: the coprocessor acknowledges commands with VIctory's
  messages (fmod waits for the first in System_Create; the old stub never
  answered), sets notification words, and decodes enabled XMA contexts on
  a thread into their rings. Context arrays get device addresses that map
  back. Fixes: SHAPE_CONTEXTS is 0x60 (APU addresses are 32-bit; 120
  bytes wrote 24 past the title's structure); AcpHalReleaseShapeContexts
  writes nothing through its argument (the title passes a handle); the
  non-cached heap's device addresses start on a 64 KiB boundary; state
  the decoding thread reads is immortal.
- src/xma_decode.*, src/xma_tables.h: real XMA2 decoding (VIctory's port
  of FFmpeg's WMA Pro decoder) with four fixes from VIctory's history
  that the DexNom snapshot lacks (ba42db643, d4d182204, 03a6f2290,
  4eb0898b3); src/xma_context.*: the context machine, with three more
  (7a5d85e6a, 1054fe87d, 985d8d4b0).

Host output is unchanged: the title's middleware opens WASAPI through
COM (com_shim.cpp forwards CoCreateInstance to the host), as in VIctory.

Tests (CPU only, no game data, in CTest): xma (decoder against a
direct-sum IMDCT, VIctory's frame walk, noise and ring tests), xapu,
chad_dsp, and acphal extended (messages, notification words, the
decoding thread filling a ring). gta6rt --dry-run on 1.0.2.1 binds all
ten xapu/chad imports (unimplemented 11 -> 1: DStorageGetFactory).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 17:25:05 -05:00

151 lines
7.9 KiB
C++

// chad.dll through the registry: connect limits, stream shapes, the stream limit, and the status block a submitted
// command completes. The flow follows VIctory's test (crates/victory-hle/src/modules/dsp.rs,
// convolution_stream_as_the_title_drives_it), GPL-3.0-or-later, by Marvisak and contributors (DexNom fork); Leonida is
// GPL-3.0-or-later. No game data.
#include <windows.h>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <vector>
#include "shims.h"
namespace {
int g_checks = 0;
int g_failures = 0;
void check(bool condition, const char* what) {
++g_checks;
std::printf("%s %s\n", condition ? "PASS" : "FAIL", what);
if (!condition) ++g_failures;
}
constexpr HRESULT kPointer = static_cast<HRESULT>(0x80004003u);
constexpr HRESULT kOutOfMemory = static_cast<HRESULT>(0x8007000Eu);
constexpr HRESULT kInvalidArg = static_cast<HRESULT>(0x80070057u);
// Test mirrors of the measured layouts (VIctory dsp.rs).
#pragma pack(push, 1)
struct StreamDesc {
uint32_t kind, options, frames, channels, ir_floats;
const float* ir;
};
#pragma pack(pop)
struct Status {
uint32_t submitted, completed, reserved;
HRESULT result;
};
struct Command {
float gains[2];
const float* input;
float* output;
uint64_t reserved;
};
static_assert(sizeof(StreamDesc) == 0x1c && sizeof(Status) == 16 && sizeof(Command) == 0x20);
using ConnectFn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t, uint32_t, uint32_t, void**);
using DisconnectFn = HRESULT(__stdcall*)(void*);
using ActivateFn = HRESULT(__stdcall*)(void*, const StreamDesc*, const Status**, void**);
using DeactivateFn = HRESULT(__stdcall*)(void*);
using SubmitFn = HRESULT(__stdcall*)(void*, const Command*, uint32_t*);
} // namespace
int main() {
std::printf("chad_dsp_test: registered chad.dll shim\n");
const auto connect = reinterpret_cast<ConnectFn>(rt::shims::find("chad.dll", "XDspConnectWithMaximumStreamLimit"));
const auto disconnect = reinterpret_cast<DisconnectFn>(rt::shims::find("chad.dll", "XDspDisconnect"));
const auto activate = reinterpret_cast<ActivateFn>(rt::shims::find("chad.dll", "XDspActivate"));
const auto deactivate = reinterpret_cast<DeactivateFn>(rt::shims::find("chad.dll", "XDspDeactivate"));
const auto submit = reinterpret_cast<SubmitFn>(rt::shims::find("chad.dll", "XDspSubmitCommand"));
check(connect && disconnect && activate && deactivate && submit, "all five exports are registered");
check(rt::shims::owns_dll("chad.dll"), "the registry owns chad.dll");
if (!connect || !disconnect || !activate || !deactivate || !submit) return 1;
std::vector<uint8_t> base(0x100000);
void* dsp = nullptr;
check(connect(nullptr, 0x100000, 24, 0x80, 1, &dsp) == kInvalidArg, "connect: no memory");
check(connect(base.data(), 0xffff, 24, 0x80, 1, &dsp) == kInvalidArg, "connect: under 64 KiB");
check(connect(base.data(), 0x1F400001, 24, 0x80, 1, &dsp) == kInvalidArg, "connect: over 500 MiB");
check(connect(base.data(), 0x100000, 0x81, 0x80, 1, &dsp) == kInvalidArg, "connect: over 128 s of response");
check(connect(base.data(), 0x100000, 24, 0x101, 1, &dsp) == kInvalidArg, "connect: over 256 streams");
check(connect(base.data(), 0x100000, 24, 0x80, 3, &dsp) == kInvalidArg, "connect: over two channels");
check(connect(base.data(), 0x100000, 24, 0x80, 1, nullptr) == kPointer, "connect: no out pointer");
check(connect(base.data(), 0x100000, 24, 0x80, 1, &dsp) == S_OK && dsp, "connect as VIctory's test does");
// Stereo convolution with option 2, as the title asks for.
std::vector<float> ir(0x800);
const StreamDesc desc{1, 2, 0x400, 2, 0x800, ir.data()};
const Status* status = nullptr;
void* stream = nullptr;
check(activate(dsp, &desc, nullptr, &stream) == kPointer, "activate: no status pointer");
check(activate(dsp, &desc, &status, nullptr) == kPointer, "activate: no stream pointer");
check(activate(nullptr, &desc, &status, &stream) == kInvalidArg, "activate: no connection");
check(activate(dsp, nullptr, &status, &stream) == kInvalidArg, "activate: no description");
check(activate(dsp, &desc, &status, &stream) == S_OK && status && stream, "activate a stereo convolution");
check(status->submitted == 0 && status->completed == 0 && status->result == S_OK, "a fresh status block");
std::vector<float> output(0x800 + 16, 1.0f);
const std::vector<float> input(0x800, 0.5f);
const Command command{{1.0f, 1.0f}, input.data(), output.data(), 0};
uint32_t ticket = 0;
check(submit(stream, &command, &ticket) == S_OK && ticket == 1, "submit: ticket 1");
check(status->completed == ticket && status->submitted == ticket && status->result == S_OK,
"completed at once: the status names the ticket");
bool silent = true, guarded = true;
for (size_t i = 0; i < 0x800; ++i) silent &= output[i] == 0.0f;
for (size_t i = 0x800; i < output.size(); ++i) guarded &= output[i] == 1.0f;
check(silent, "the output block is silence (placeholder transform)");
check(guarded, "only one block of the stream written");
check(submit(stream, &command, nullptr) == S_OK && status->completed == 2, "a second command, no ticket asked");
const Command no_output{{1.0f, 1.0f}, input.data(), nullptr, 0};
check(submit(stream, &no_output, &ticket) == S_OK && ticket == 3 && status->completed == 3, "a command without output");
check(submit(nullptr, &command, &ticket) == kInvalidArg && submit(stream, nullptr, &ticket) == kInvalidArg,
"submit: null stream or command");
check(deactivate(stream) == S_OK, "deactivate");
check(deactivate(nullptr) == kInvalidArg, "deactivate: null");
// Mono streams refuse stereo options; convolutions take 512 or 1024 frames and not option 4; other transforms are
// mono only, of 512 to 2048 frames.
const auto shape = [&](uint32_t kind, uint32_t options, uint32_t frames, uint32_t channels) {
const StreamDesc d{kind, options, frames, channels, 0, nullptr};
void* s = nullptr;
const HRESULT hr = activate(dsp, &d, &status, &s);
if (hr == S_OK) deactivate(s);
return hr;
};
check(shape(1, 2, 0x400, 1) == kInvalidArg, "a mono convolution with option 2");
check(shape(1, 8, 0x400, 1) == kInvalidArg, "a mono convolution with option 8");
check(shape(1, 0, 0x400, 1) == S_OK, "a mono convolution");
check(shape(1, 0, 0x200, 2) == S_OK, "a stereo convolution of 512 frames");
check(shape(1, 0, 0x800, 2) == kInvalidArg, "a convolution of 2048 frames");
check(shape(1, 4, 0x400, 2) == kInvalidArg, "a convolution with option 4");
check(shape(1, 0, 0x400, 3) == kInvalidArg, "three channels");
check(shape(2, 0, 0x400, 1) == S_OK, "a mono transform");
check(shape(2, 4, 0x800, 1) == S_OK, "a transform of 2048 frames with option 4");
check(shape(2, 0, 0x400, 2) == kInvalidArg, "a stereo transform");
check(shape(2, 0, 0x100, 1) == kInvalidArg, "a transform of 256 frames");
// The stream limit, and a stream freed for another.
void* limited = nullptr;
check(connect(base.data(), 0x10000, 0, 2, 2, &limited) == S_OK, "connect for two streams");
const StreamDesc mono{2, 0, 0x200, 1, 0, nullptr};
void* a = nullptr;
void* b = nullptr;
void* c = nullptr;
check(activate(limited, &mono, &status, &a) == S_OK && activate(limited, &mono, &status, &b) == S_OK,
"two streams");
check(activate(limited, &mono, &status, &c) == kOutOfMemory, "a third: out of memory");
check(deactivate(a) == S_OK && activate(limited, &mono, &status, &c) == S_OK, "one freed, another activated");
deactivate(b);
deactivate(c);
check(disconnect(limited) == S_OK, "disconnect the limited connection");
check(disconnect(dsp) == S_OK, "disconnect");
check(disconnect(nullptr) == kInvalidArg, "disconnect: null");
std::printf("chad_dsp_test complete: %d checks, %d failures\n", g_checks, g_failures);
return g_failures ? 1 : 0;
}