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 04:29:18 -0300
committerandretuta <andreviniciusmerlia@gmail.com>2026-11-24 04:29:18 -0300
commite02ec36bf315de960a9db244e971bf00cfd66406 (patch)
tree629cb4a3488a855fb51d943ce8420cb263553d9d /runtime/test/acphal_test.cpp
parenta0327ee1af5bb44abfd1327d00167da526d1ac70 (diff)
acphal: synchronize XMA context words and bound the output ring
Diffstat (limited to 'runtime/test/acphal_test.cpp')
-rw-r--r--runtime/test/acphal_test.cpp121
1 files changed, 86 insertions, 35 deletions
diff --git a/runtime/test/acphal_test.cpp b/runtime/test/acphal_test.cpp
index 58bb48227..9ee2ee7ff 100644
--- a/runtime/test/acphal_test.cpp
+++ b/runtime/test/acphal_test.cpp
@@ -6,6 +6,7 @@
6 6
7#include <windows.h> 7#include <windows.h>
8 8
9#include <algorithm>
9#include <atomic> 10#include <atomic>
10#include <cstddef> 11#include <cstddef>
11#include <cstdint> 12#include <cstdint>
@@ -69,6 +70,14 @@ static_assert(sizeof(TestShapeContexts) == 120, "TestShapeContexts is 120 bytes"
69static_assert(offsetof(TestShapeContexts, xmaContextArray) == 0x38, 70static_assert(offsetof(TestShapeContexts, xmaContextArray) == 0x38,
70 "xmaContextArray is at offset +0x38"); 71 "xmaContextArray is at offset +0x38");
71 72
73struct XmaWords {
74 uint32_t* data;
75
76 std::atomic_ref<uint32_t> operator[](std::size_t index) const {
77 return std::atomic_ref(data[index]);
78 }
79};
80
72} // namespace 81} // namespace
73 82
74int main() { 83int main() {
@@ -348,7 +357,8 @@ int main() {
348 357
349 // 8. End-to-end XMA2 context ring decoding via background decoder thread 358 // 8. End-to-end XMA2 context ring decoding via background decoder thread
350 { 359 {
351 // Build a 2-packet XMA2 buffer with 3 silent mono frames per packet (6 frames total). 360 // Two synthetic XMA2 packets with three mono frames each. Each frame
361 // transmits one positive spectral coefficient so decoded PCM differs from fallback silence.
352 std::vector<uint8_t> stream(2 * 2048, 0xFFu); 362 std::vector<uint8_t> stream(2 * 2048, 0xFFu);
353 auto put_bits = [&](size_t at, uint32_t value, size_t width) { 363 auto put_bits = [&](size_t at, uint32_t value, size_t width) {
354 for (size_t i = 0; i < width; ++i) { 364 for (size_t i = 0; i < width; ++i) {
@@ -361,12 +371,29 @@ int main() {
361 }; 371 };
362 for (size_t packet = 0; packet < 2; ++packet) { 372 for (size_t packet = 0; packet < 2; ++packet) {
363 const size_t base = packet * 2048 * 8; 373 const size_t base = packet * 2048 * 8;
364 put_bits(base, 3u << 26, 32); // 3 frames, first_frame_bit = 0, skip = 0 374 put_bits(base, (3u << 26) | (1u << 8), 32);
375 constexpr size_t frame_bits = 82;
365 for (size_t f = 0; f < 3; ++f) { 376 for (size_t f = 0; f < 3; ++f) {
366 const size_t at = base + 32 + f * 31; 377 size_t at = base + 32 + f * frame_bits;
367 put_bits(at, 31, 15); // frame length 378 auto put = [&](uint32_t value, size_t width) {
368 put_bits(at + 15, 0b10u, 2); // fixed layout, one 512-sample subframe 379 put_bits(at, value, width);
369 put_bits(at + 17, 0, 14); // gain(8) + trim(1) + subframe_skip/empty(5) 380 at += width;
381 };
382 put(frame_bits, 15);
383 put(0b10u, 2); // Fixed layout, one 512-sample subframe.
384 put(0, 11); // Gain, trim, fill and subframe error flag.
385 put(1, 1); // Coefficients are present.
386 put(1, 1); // Explicit vector coefficient count.
387 put(0, 8); // Use only run/level coding.
388 put(0, 6); // Quantizer adjustment.
389 put(0, 2); // Scale factor step.
390 put((1u << 23) - 1, 23); // One zero delta per 48 kHz scale band.
391 put(0, 1); // Run/level table 0.
392 put(0b00u, 2); // Run 0, level 1.
393 put(1, 1); // Positive sign.
394 put(0b0101011u, 7); // End of coefficients.
395 put(0, 1); // Frame padding bit.
396 put(f < 2 ? 1u : 0u, 1); // Another frame follows in this packet.
370 } 397 }
371 } 398 }
372 399
@@ -389,7 +416,7 @@ int main() {
389 416
390 // Configure voice index 1: 2 packets in input 0, input 0 valid, 4 output blocks, 417 // Configure voice index 1: 2 packets in input 0, input 0 valid, 4 output blocks,
391 // writable = 1, 48 kHz mono, read_bit = 32. 418 // writable = 1, 48 kHz mono, read_bit = 32.
392 auto* words = static_cast<volatile uint32_t*>(xma_ctx.xmaContextArray) + 16; 419 const XmaWords words{static_cast<uint32_t*>(xma_ctx.xmaContextArray) + 16};
393 words[0] = 2u | (1u << 20) | (4u << 22); 420 words[0] = 2u | (1u << 20) | (4u << 22);
394 words[1] = (1u << 31) | (3u << 27); 421 words[1] = (1u << 31) | (3u << 27);
395 words[2] = 32u; 422 words[2] = 32u;
@@ -418,27 +445,28 @@ int main() {
418 for (int wait = 0; wait < 500 && ((words[1] >> 31) & 1u) == 1u; ++wait) { 445 for (int wait = 0; wait < 500 && ((words[1] >> 31) & 1u) == 1u; ++wait) {
419 Sleep(1); 446 Sleep(1);
420 } 447 }
421 check(((words[1] >> 31) & 1u) == 0u, 448 const bool ring_full = (words[1] >> 31) == 0u;
422 "Background XMA2 decoder fills ring and clears writable flag"); 449 check(ring_full, "Background XMA2 decoder fills ring and clears writable flag");
423 check(((words[0] >> 27) & 0x1Fu) == 0u, 450 check(((words[0] >> 27) & 0x1Fu) == 0u,
424 "Write block wrapped back to 0 after filling 4 blocks"); 451 "Write block wrapped back to 0 after filling 4 blocks");
425 452
426 bool ring_zeroed = true;
427 const auto* ring_bytes = static_cast<const uint8_t*>(out_va); 453 const auto* ring_bytes = static_cast<const uint8_t*>(out_va);
428 for (size_t i = 0; i < 0x400; ++i) { 454#if defined(LEONIDA_HAVE_XMA2_DECODER)
429 if (ring_bytes[i] != 0) { 455 check(ring_full && std::any_of(ring_bytes, ring_bytes + 0x400,
430 ring_zeroed = false; 456 [](uint8_t b) { return b != 0 && b != 0x55; }),
431 break; 457 "The Rust decoder writes non-silent PCM");
432 } 458#else
433 } 459 check(ring_full && std::all_of(ring_bytes, ring_bytes + 0x400, [](uint8_t b) { return b == 0; }),
434 check(ring_zeroed, "Decoded silent PCM overwrote sentinel bytes across all 4 ring blocks"); 460 "The fallback writes silence");
461#endif
435 462
436 // Advance read pointer 4 blocks at a time until input exhausts and INPUT_INVALID fires. 463 // Advance read pointer 4 blocks at a time until input exhausts and INPUT_INVALID fires.
437 const uint32_t advance_args[2] = {1u, 4u}; 464 const uint32_t advance_args[2] = {1u, 4u};
438 for (int step = 0; step < 20; ++step) { 465 int written_blocks = ring_full ? 4 : 0;
439 check(fn_submit(x_acp, 18, 0xf2 + static_cast<uint64_t>(step), 466 bool advanced = true;
440 0xffffffffu, advance_args, 0) == S_OK, 467 for (int step = 0; step < 6; ++step) {
441 "ADVANCE_XMA_READ succeeds"); 468 advanced &= fn_submit(x_acp, 18, 0xf2 + static_cast<uint64_t>(step),
469 0xffffffffu, advance_args, 0) == S_OK;
442 // Wait until either ring fills again (writable == 0) or stream hits error bit. 470 // Wait until either ring fills again (writable == 0) or stream hits error bit.
443 for (int wait = 0; wait < 200; ++wait) { 471 for (int wait = 0; wait < 200; ++wait) {
444 const uint32_t w1 = words[1]; 472 const uint32_t w1 = words[1];
@@ -449,7 +477,11 @@ int main() {
449 if (((words[2] >> 31) & 1u) == 1u && ((words[1] >> 31) & 1u) == 1u) { 477 if (((words[2] >> 31) & 1u) == 1u && ((words[1] >> 31) & 1u) == 1u) {
450 break; 478 break;
451 } 479 }
480 if ((words[1] >> 31) != 0u) break;
481 written_blocks += 4;
452 } 482 }
483 check(advanced, "ADVANCE_XMA_READ succeeds");
484 check(written_blocks == 24, "Six frames write exactly 24 blocks before input exhaustion");
453 check(((words[0] >> 20) & 3u) == 0u, "XMA input buffer valid bits cleared on exhaustion"); 485 check(((words[0] >> 20) & 3u) == 0u, "XMA input buffer valid bits cleared on exhaustion");
454 check(((words[2] >> 31) & 1u) == 1u, "XMA error flag set on exhaustion"); 486 check(((words[2] >> 31) & 1u) == 1u, "XMA error flag set on exhaustion");
455 check(((words[2] >> 26) & 0x1Fu) == 4u, "XMA error status is INPUT_INVALID (4)"); 487 check(((words[2] >> 26) & 0x1Fu) == 4u, "XMA error status is INPUT_INVALID (4)");
@@ -505,7 +537,7 @@ int main() {
505 "ApuAlloc 16-block loop output ring"); 537 "ApuAlloc 16-block loop output ring");
506 538
507 // Voice index 2. 539 // Voice index 2.
508 auto* words = static_cast<volatile uint32_t*>(xma_ctx.xmaContextArray) + 32; 540 const XmaWords words{static_cast<uint32_t*>(xma_ctx.xmaContextArray) + 32};
509 541
510 using ConnectFn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t); 542 using ConnectFn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t);
511 using SubmitFn = HRESULT(__stdcall*)(void*, uint32_t, uint64_t, uint32_t, const void*, uint32_t); 543 using SubmitFn = HRESULT(__stdcall*)(void*, uint32_t, uint64_t, uint32_t, const void*, uint32_t);
@@ -519,23 +551,27 @@ int main() {
519 const auto fn_release = reinterpret_cast<ReleaseFn>(vt[5]); 551 const auto fn_release = reinterpret_cast<ReleaseFn>(vt[5]);
520 check(fn_connect(x_acp, 0x1000, 0x1000) == S_OK, "Connect for packet loop test"); 552 check(fn_connect(x_acp, 0x1000, 0x1000) == S_OK, "Connect for packet loop test");
521 553
522 // Decodes the stream once on voice 2 with the given loop count and returns the number of 554 auto configure_voice = [&](uint32_t loops, uint32_t input_0_packets, uint32_t ring_size,
523 // blocks written when the error flag rose, or -1 on timeout. 555 uint32_t write, uint32_t read) {
524 auto run_blocks = [&](uint32_t loops, bool split_inputs = false) -> int {
525 std::memset(out_va, 0x55, 0x1000); 556 std::memset(out_va, 0x55, 0x1000);
526 words[0] = (split_inputs ? 2u : 3u) | (loops << 12) | 557 const uint32_t input_1_packets = 3u - input_0_packets;
527 ((split_inputs ? 3u : 1u) << 20) | (16u << 22); 558 words[0] = input_0_packets | (loops << 12) |
528 words[1] = (1u << 31) | (3u << 27) | (split_inputs ? 1u : 0u); 559 ((input_1_packets != 0 ? 3u : 1u) << 20) | (ring_size << 22) | (write << 27);
560 words[1] = (1u << 31) | (3u << 27) | input_1_packets;
529 words[2] = 32u; // read at packet 0 561 words[2] = 32u; // read at packet 0
530 words[3] = 32u; // loop start: packet 0 562 words[3] = 32u; // loop start: packet 0
531 words[4] = 100u; // loop end: packet 0 563 words[4] = 100u; // loop end: packet 0
532 words[5] = in_apu; 564 words[5] = in_apu;
533 words[6] = in_apu + 0x1000u; 565 words[6] = in_apu + input_0_packets * 2048u;
534 words[7] = out_apu; 566 words[7] = out_apu;
535 words[9] = 0u; 567 words[9] = read;
568 };
536 569
537 const uint32_t enable_arg = 2u; 570 const uint32_t voice_index = 2u;
538 check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &enable_arg, 0) == S_OK, 571 // Returns the number of blocks written before input exhaustion, or -1 on timeout.
572 auto run_blocks = [&](uint32_t loops, uint32_t input_0_packets = 3) -> int {
573 configure_voice(loops, input_0_packets, 16, 0, 0);
574 check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &voice_index, 0) == S_OK,
539 "ENABLE_XMA voice 2 succeeds"); 575 "ENABLE_XMA voice 2 succeeds");
540 int wait = 0; 576 int wait = 0;
541 for (; wait < 1000 && ((words[2] >> 31) & 1u) == 0u; ++wait) { 577 for (; wait < 1000 && ((words[2] >> 31) & 1u) == 0u; ++wait) {
@@ -544,8 +580,7 @@ int main() {
544 const int blocks = ((words[2] >> 31) & 1u) != 0u 580 const int blocks = ((words[2] >> 31) & 1u) != 0u
545 ? static_cast<int>((words[0] >> 27) & 0x1Fu) 581 ? static_cast<int>((words[0] >> 27) & 0x1Fu)
546 : -1; 582 : -1;
547 const uint32_t disable_arg = 2u; 583 check(fn_submit(x_acp, 7, 0xf4, 0xffffffffu, &voice_index, 0) == S_OK,
548 check(fn_submit(x_acp, 7, 0xf4, 0xffffffffu, &disable_arg, 0) == S_OK,
549 "DISABLE_XMA voice 2 succeeds"); 584 "DISABLE_XMA voice 2 succeeds");
550 return blocks; 585 return blocks;
551 }; 586 };
@@ -553,8 +588,24 @@ int main() {
553 check(run_blocks(0) == 8, "Packets 0 and 2 write 8 blocks, skipping packet 1"); 588 check(run_blocks(0) == 8, "Packets 0 and 2 write 8 blocks, skipping packet 1");
554 check(run_blocks(1) == 12, "One loop of packet 0 writes 12 blocks (packets 0, 0, 2)"); 589 check(run_blocks(1) == 12, "One loop of packet 0 writes 12 blocks (packets 0, 0, 2)");
555 check(((words[0] >> 12) & 0xFFu) == 0u, "Loop count is consumed by the single loop"); 590 check(((words[0] >> 12) & 0xFFu) == 0u, "Loop count is consumed by the single loop");
556 check(run_blocks(0, true) == 8 && (words[4] >> 31) == 1u, 591 check(run_blocks(0, 2) == 8 && (words[4] >> 31) == 1u,
557 "A skip to the end of input 0 continues at input 1"); 592 "A skip to the end of input 0 continues at input 1");
593 check(run_blocks(0, 1) == 8 && (words[4] >> 31) == 1u,
594 "An overshooting skip starts at packet 1 of input 1");
595
596 configure_voice(255, 3, 4, 7, 9);
597 check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &voice_index, 0) == S_OK,
598 "Enable infinite loop with out-of-range ring indices");
599 for (int wait = 0; wait < 1000 && (words[1] >> 31) != 0u; ++wait) Sleep(1);
600 check((words[1] >> 31) == 0u && ((words[0] >> 27) & 0x1Fu) == 1u,
601 "Ring stops at the normalized read index");
602 check(fn_submit(x_acp, 7, 0xf4, 0xffffffffu, &voice_index, 0) == S_OK,
603 "Disable infinite loop after filling bounded ring");
604 check(((words[0] >> 12) & 0xFFu) == 255u, "Infinite loop does not consume its loop count");
605 const auto* ring_bytes = static_cast<const uint8_t*>(out_va);
606 check(std::all_of(ring_bytes + 0x400, ring_bytes + 0x1000,
607 [](uint8_t b) { return b == 0x55; }),
608 "Output never writes beyond the configured four-block ring");
558 609
559 words[0] = 3u << 22; // three ring blocks 610 words[0] = 3u << 22; // three ring blocks
560 words[9] = 2u; 611 words[9] = 2u;