upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/acphal_test.cpp
diff options
context:
space:
mode:
authorandretuta <andreviniciusmerlia@gmail.com>2026-11-24 01:44:00 -0300
committerandretuta <andreviniciusmerlia@gmail.com>2026-11-24 01:44:00 -0300
commit4750df6a06e68c05343c5895f1d6dcddf61e38c3 (patch)
tree7546846eaf91a7057fe22dde56c9f1fe4b0b85bb /runtime/test/acphal_test.cpp
parentece4ba5b31ebc6f345c8e29eae1261f2f780057a (diff)
acphal: add portable XMA2 decoder, ACP message queue, and SHAPE XMA worker
- Add portable C++20 XMA2 packet parser and WMA Pro frame decoder (runtime/src/xma2_decoder.{h,cpp}, following FFmpeg wmaprodec.c / wmaprodata.h LGPL-2.1-or-later under Leonida's GPL-3.0-or-later) with cross-platform xma2_decoder_test. - Implement IAcpHal command/message queue protocol (REGISTER_MESSAGES, UNREGISTER_MESSAGES, LOAD_FLOWGRAPH, COMMAND_COMPLETED, DISCONNECTED, queue-full dropped counters, and APU notification word writes) in runtime/shims/acphal.cpp. - Wire a lazily-started, cleanly-joined background XMA2 decoder thread pumping enabled SHAPE_XMA_CONTEXT output rings in APU memory.
Diffstat (limited to 'runtime/test/acphal_test.cpp')
-rw-r--r--runtime/test/acphal_test.cpp205
1 files changed, 205 insertions, 0 deletions
diff --git a/runtime/test/acphal_test.cpp b/runtime/test/acphal_test.cpp
index bd93f7040..ca3167e64 100644
--- a/runtime/test/acphal_test.cpp
+++ b/runtime/test/acphal_test.cpp
@@ -10,6 +10,7 @@
10#include <cstdint> 10#include <cstdint>
11#include <cstdio> 11#include <cstdio>
12#include <cstring> 12#include <cstring>
13#include <vector>
13 14
14#include "shims.h" 15#include "shims.h"
15 16
@@ -257,6 +258,210 @@ int main() {
257 check(ctx_all.xmaContextArray == nullptr, "xmaContextArray is reset to null"); 258 check(ctx_all.xmaContextArray == nullptr, "xmaContextArray is reset to null");
258 check(release_ctx(nullptr) == S_OK, "AcpHalReleaseShapeContexts(nullptr) succeeds"); 259 check(release_ctx(nullptr) == S_OK, "AcpHalReleaseShapeContexts(nullptr) succeeds");
259 260
261 // 7. ACP command/message queue protocol (register, complete, overflow drops, disconnect, notify word)
262 {
263 #pragma pack(push, 1)
264 struct TestAcpMessage {
265 uint32_t kind;
266 uint32_t dropped;
267 uint32_t usec;
268 uint8_t data[16];
269 };
270 #pragma pack(pop)
271 static_assert(sizeof(TestAcpMessage) == 28, "ACP message is 28 bytes");
272
273 using ConnectFn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t);
274 using DisconnectFn = HRESULT(__stdcall*)(void*);
275 using SubmitFn = HRESULT(__stdcall*)(void*, uint32_t, uint64_t, uint32_t, const void*, uint32_t);
276 using PopFn = uint32_t(__stdcall*)(void*, TestAcpMessage*);
277 using CountFn = uint32_t(__stdcall*)(void*);
278 using ReleaseFn = HRESULT(__stdcall*)(void*);
279
280 void* q_acp = nullptr;
281 check(create(&q_acp) == S_OK && q_acp != nullptr, "AcpHalCreate for queue test");
282 auto vt = *static_cast<void* const* const*>(q_acp);
283 const auto fn_connect = reinterpret_cast<ConnectFn>(vt[0]);
284 const auto fn_disconnect = reinterpret_cast<DisconnectFn>(vt[1]);
285 const auto fn_submit = reinterpret_cast<SubmitFn>(vt[2]);
286 const auto fn_pop = reinterpret_cast<PopFn>(vt[3]);
287 const auto fn_count = reinterpret_cast<CountFn>(vt[4]);
288 const auto fn_release = reinterpret_cast<ReleaseFn>(vt[5]);
289
290 check(fn_connect(q_acp, 0, 16) == E_INVALIDARG, "Connect(commands=0) returns E_INVALIDARG");
291 check(fn_disconnect(q_acp) == E_FAIL, "Disconnect before Connect returns E_FAIL");
292 check(fn_connect(q_acp, 16, 2) == S_OK, "Connect(16, 2) succeeds");
293
294 // Allocate a 4-byte notification word in APU memory.
295 void* notify_va = nullptr;
296 uint32_t notify_apu = 0;
297 check(alloc(&notify_va, &notify_apu, 16, 16, 1) == S_OK && notify_va != nullptr,
298 "ApuAlloc notification word succeeds");
299 *static_cast<uint32_t*>(notify_va) = 0;
300
301 const uint32_t reg_mask = 0x92u; // FLOWGRAPH_COMPLETED(0x02) | COMMAND_COMPLETED(0x10) | DISCONNECTED(0x80)
302 check(fn_submit(q_acp, 2, 0x1122334455667788ull, 0xffffffffu, &reg_mask, notify_apu) == S_OK,
303 "REGISTER_MESSAGES with id and notify word succeeds");
304 check(*static_cast<volatile uint32_t*>(notify_va) == 1u,
305 "SubmitCommand writes 1 to APU notification word");
306 check(fn_count(q_acp) == 1u, "COMMAND_COMPLETED queued after REGISTER_MESSAGES");
307
308 TestAcpMessage msg{};
309 check(fn_pop(q_acp, &msg) == 1u, "PopMessage retrieves COMMAND_COMPLETED");
310 uint32_t ack_cmd = 0;
311 uint64_t ack_id = 0;
312 std::memcpy(&ack_cmd, msg.data, 4);
313 std::memcpy(&ack_id, msg.data + 4, 8);
314 check(msg.kind == 0x10u && msg.dropped == 0u && ack_cmd == 2u && ack_id == 0x1122334455667788ull,
315 "COMMAND_COMPLETED payload carries command id and 64-bit token");
316
317 // Overflow queue of capacity 2 with 4 commands -> 2 dropped, reported on the 5th.
318 for (uint64_t tok = 1; tok <= 4; ++tok) {
319 check(fn_submit(q_acp, 12, tok, 0xffffffffu, nullptr, 0) == S_OK,
320 "SubmitCommand during overflow succeeds");
321 }
322 check(fn_count(q_acp) == 2u, "Message queue caps at capacity 2");
323 check(fn_pop(q_acp, &msg) == 1u && fn_pop(q_acp, &msg) == 1u,
324 "Drained the 2 queued messages");
325 check(fn_submit(q_acp, 12, 5, 0xffffffffu, nullptr, 0) == S_OK,
326 "SubmitCommand 5 after drain succeeds");
327 check(fn_pop(q_acp, &msg) == 1u && msg.dropped == 2u,
328 "Next queued message reports dropped == 2");
329
330 // LOAD_FLOWGRAPH posts FLOWGRAPH_COMPLETED carrying word(1).
331 const uint32_t fg_cfg[2] = {0, 0x42u};
332 check(fn_submit(q_acp, 1, 0, 0xffffffffu, fg_cfg, 0) == S_OK, "LOAD_FLOWGRAPH succeeds");
333 check(fn_pop(q_acp, &msg) == 1u && msg.kind == 0x02u, "FLOWGRAPH_COMPLETED queued");
334 uint32_t fg_id = 0;
335 std::memcpy(&fg_id, msg.data, 4);
336 check(fg_id == 0x42u, "FLOWGRAPH_COMPLETED carries flowgraph word");
337
338 // Disconnect posts DISCONNECTED.
339 check(fn_disconnect(q_acp) == S_OK, "Disconnect succeeds when connected");
340 check(fn_count(q_acp) == 1u, "Disconnect queued DISCONNECTED message");
341 check(fn_pop(q_acp, &msg) == 1u && msg.kind == 0x80u, "Popped DISCONNECTED message");
342
343 free_mem(notify_va);
344 check(fn_release(q_acp) == S_OK, "Release queue AcpHal succeeds");
345 }
346
347 // 8. End-to-end XMA2 context ring decoding via background decoder thread
348 {
349 // Build a 2-packet XMA2 buffer with 3 silent mono frames per packet (6 frames total).
350 std::vector<uint8_t> stream(2 * 2048, 0xFFu);
351 auto put_bits = [&](size_t at, uint32_t value, size_t width) {
352 for (size_t i = 0; i < width; ++i) {
353 const size_t p = at + i;
354 const uint32_t bit = (value >> (width - 1 - i)) & 1u;
355 stream[p / 8] = static_cast<uint8_t>(
356 (stream[p / 8] & ~(0x80u >> (p % 8))) |
357 (bit << (7 - (p % 8))));
358 }
359 };
360 for (size_t packet = 0; packet < 2; ++packet) {
361 const size_t base = packet * 2048 * 8;
362 put_bits(base, 3u << 26, 32); // 3 frames, first_frame_bit = 0, skip = 0
363 for (size_t f = 0; f < 3; ++f) {
364 const size_t at = base + 32 + f * 31;
365 put_bits(at, 31, 15); // frame length
366 put_bits(at + 15, 0b10u, 2); // fixed layout, one 512-sample subframe
367 put_bits(at + 17, 0, 14); // gain(8) + trim(1) + subframe_skip/empty(5)
368 }
369 }
370
371 TestShapeContexts xma_ctx{};
372 xma_ctx.numXmaContexts = 4;
373 check(alloc_ctx(&xma_ctx) == S_OK && xma_ctx.xmaContextArray != nullptr,
374 "Allocate 4 XMA SHAPE contexts");
375
376 void* in_va = nullptr;
377 uint32_t in_apu = 0;
378 check(alloc(&in_va, &in_apu, 0x1000, 0x800, 1) == S_OK && in_va != nullptr,
379 "ApuAlloc 2-packet XMA2 input buffer");
380 std::memcpy(in_va, stream.data(), stream.size());
381
382 void* out_va = nullptr;
383 uint32_t out_apu = 0;
384 check(alloc(&out_va, &out_apu, 0x400, 0x100, 2) == S_OK && out_va != nullptr,
385 "ApuAlloc 4-block XMA2 output ring");
386 std::memset(out_va, 0x55, 0x400);
387
388 // Configure voice index 1: 2 packets in input 0, input 0 valid, 4 output blocks,
389 // writable = 1, 48 kHz mono, read_bit = 32.
390 auto* words = static_cast<volatile uint32_t*>(xma_ctx.xmaContextArray) + 16;
391 words[0] = 2u | (1u << 20) | (4u << 22);
392 words[1] = (1u << 31) | (3u << 27);
393 words[2] = 32u;
394 words[5] = in_apu;
395 words[7] = out_apu;
396
397 using ConnectFn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t);
398 using SubmitFn = HRESULT(__stdcall*)(void*, uint32_t, uint64_t, uint32_t, const void*, uint32_t);
399 using ReleaseFn = HRESULT(__stdcall*)(void*);
400
401 void* x_acp = nullptr;
402 check(create(&x_acp) == S_OK && x_acp != nullptr, "AcpHalCreate for XMA decode test");
403 auto vt = *static_cast<void* const* const*>(x_acp);
404 const auto fn_connect = reinterpret_cast<ConnectFn>(vt[0]);
405 const auto fn_submit = reinterpret_cast<SubmitFn>(vt[2]);
406 const auto fn_release = reinterpret_cast<ReleaseFn>(vt[5]);
407
408 check(fn_connect(x_acp, 0x1000, 0x1000) == S_OK, "Connect for XMA decode test");
409
410 // ENABLE_XMA (cmd 5) on voice 1.
411 const uint32_t enable_arg = 1u;
412 check(fn_submit(x_acp, 5, 0xf1, 0xffffffffu, &enable_arg, 0) == S_OK,
413 "ENABLE_XMA voice 1 succeeds");
414
415 // Wait up to 500 ms for the background decoder thread to fill the 4-block ring.
416 for (int wait = 0; wait < 500 && ((words[1] >> 31) & 1u) == 1u; ++wait) {
417 Sleep(1);
418 }
419 check(((words[1] >> 31) & 1u) == 0u,
420 "Background XMA2 decoder fills ring and clears writable flag");
421 check(((words[0] >> 27) & 0x1Fu) == 0u,
422 "Write block wrapped back to 0 after filling 4 blocks");
423
424 bool ring_zeroed = true;
425 const auto* ring_bytes = static_cast<const uint8_t*>(out_va);
426 for (size_t i = 0; i < 0x400; ++i) {
427 if (ring_bytes[i] != 0) {
428 ring_zeroed = false;
429 break;
430 }
431 }
432 check(ring_zeroed, "Decoded silent PCM overwrote sentinel bytes across all 4 ring blocks");
433
434 // Advance read pointer 4 blocks at a time until input exhausts and INPUT_INVALID fires.
435 const uint32_t advance_args[2] = {1u, 4u};
436 for (int step = 0; step < 20; ++step) {
437 check(fn_submit(x_acp, 18, 0xf2 + static_cast<uint64_t>(step),
438 0xffffffffu, advance_args, 0) == S_OK,
439 "ADVANCE_XMA_READ succeeds");
440 // Wait until either ring fills again (writable == 0) or stream hits error bit.
441 for (int wait = 0; wait < 200; ++wait) {
442 const uint32_t w1 = words[1];
443 const uint32_t w2 = words[2];
444 if (((w1 >> 31) & 1u) == 0u || ((w2 >> 31) & 1u) == 1u) break;
445 Sleep(1);
446 }
447 if (((words[2] >> 31) & 1u) == 1u && ((words[1] >> 31) & 1u) == 1u) {
448 break;
449 }
450 }
451 check(((words[0] >> 20) & 3u) == 0u, "XMA input buffer valid bits cleared on exhaustion");
452 check(((words[2] >> 31) & 1u) == 1u, "XMA error flag set on exhaustion");
453 check(((words[2] >> 26) & 0x1Fu) == 4u, "XMA error status is INPUT_INVALID (4)");
454
455 // DISABLE_XMA (cmd 7) on voice 1.
456 const uint32_t disable_arg = 1u;
457 check(fn_submit(x_acp, 7, 0xf9, 0xffffffffu, &disable_arg, 0) == S_OK,
458 "DISABLE_XMA voice 1 succeeds");
459 check(fn_release(x_acp) == S_OK, "Release XMA AcpHal succeeds");
460 free_mem(in_va);
461 free_mem(out_va);
462 check(release_ctx(&xma_ctx) == S_OK, "Release XMA SHAPE contexts joins decoder cleanly");
463 }
464
260 std::printf("acphal_test complete: %d checks, %d failures\n", g_checks, g_failures); 465 std::printf("acphal_test complete: %d checks, %d failures\n", g_checks, g_failures);
261 return g_failures ? 1 : 0; 466 return g_failures ? 1 : 0;
262} 467}