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:
Diffstat (limited to 'runtime/test/acphal_test.cpp')
-rw-r--r--runtime/test/acphal_test.cpp199
1 files changed, 134 insertions, 65 deletions
diff --git a/runtime/test/acphal_test.cpp b/runtime/test/acphal_test.cpp
index 9ee2ee7ff..540906ec8 100644
--- a/runtime/test/acphal_test.cpp
+++ b/runtime/test/acphal_test.cpp
@@ -11,12 +11,28 @@
11#include <cstddef> 11#include <cstddef>
12#include <cstdint> 12#include <cstdint>
13#include <cstdio> 13#include <cstdio>
14#include <cstdlib>
14#include <cstring> 15#include <cstring>
16#include <new>
15#include <thread> 17#include <thread>
16#include <vector> 18#include <vector>
17 19
18#include "shims.h" 20#include "shims.h"
19 21
22// Fail only the calling test thread's designated allocation.
23static thread_local int allocations_before_failure = -1;
24void* operator new(std::size_t size) {
25 if (allocations_before_failure == 0) {
26 allocations_before_failure = -1;
27 throw std::bad_alloc();
28 }
29 if (allocations_before_failure > 0) --allocations_before_failure;
30 if (void* memory = std::malloc(size ? size : 1)) return memory;
31 throw std::bad_alloc();
32}
33void operator delete(void* memory) noexcept { std::free(memory); }
34void operator delete(void* memory, std::size_t) noexcept { std::free(memory); }
35
20namespace { 36namespace {
21 37
22using AcpHalCreateFn = HRESULT(__stdcall*)(void**); 38using AcpHalCreateFn = HRESULT(__stdcall*)(void**);
@@ -78,9 +94,42 @@ struct XmaWords {
78 } 94 }
79}; 95};
80 96
97// Synthetic mono XMA2 with a single positive spectral coefficient in each frame.
98void write_xma_packet(uint8_t* packet, uint32_t frames, uint8_t packet_skip) {
99 constexpr uint32_t frame_bits = 82;
100 std::memset(packet, 0xFF, 2048);
101 std::size_t at = 0;
102 auto put = [&](uint32_t value, std::size_t width) {
103 for (std::size_t i = 0; i < width; ++i, ++at) {
104 const auto mask = static_cast<uint8_t>(0x80u >> (at % 8));
105 packet[at / 8] &= static_cast<uint8_t>(~mask);
106 if ((value >> (width - 1 - i)) & 1u) packet[at / 8] |= mask;
107 }
108 };
109 put((frames << 26) | (1u << 8) | packet_skip, 32);
110 for (uint32_t frame = 0; frame < frames; ++frame) {
111 put(frame_bits, 15);
112 put(0b10u, 2); // Fixed layout, one 512-sample subframe.
113 put(0, 11); // Gain, trim, fill and subframe error flag.
114 put(1, 1); // Coefficients are present.
115 put(1, 1); // Explicit vector coefficient count.
116 put(0, 8); // Use only run/level coding.
117 put(0, 6); // Quantizer adjustment.
118 put(0, 2); // Scale factor step.
119 put((1u << 23) - 1, 23); // One zero delta per 48 kHz scale band.
120 put(0, 1); // Run/level table 0.
121 put(0b00u, 2); // Run 0, level 1.
122 put(1, 1); // Positive sign.
123 put(0b0101011u, 7); // End of coefficients.
124 put(0, 1); // Frame padding bit.
125 put(frame + 1 < frames ? 1 : 0, 1);
126 }
127}
128
81} // namespace 129} // namespace
82 130
83int main() { 131int main() {
132 std::setvbuf(stdout, nullptr, _IONBF, 0);
84 std::printf("acphal_test: registered acphal.dll shim verification\n"); 133 std::printf("acphal_test: registered acphal.dll shim verification\n");
85 134
86 const auto create = reinterpret_cast<AcpHalCreateFn>( 135 const auto create = reinterpret_cast<AcpHalCreateFn>(
@@ -264,6 +313,21 @@ int main() {
264 check(ctx_all.xmaContextArray != nullptr, "xmaContextArray is allocated"); 313 check(ctx_all.xmaContextArray != nullptr, "xmaContextArray is allocated");
265 check(ctx_all.pcmContextArray != nullptr, "pcmContextArray is allocated"); 314 check(ctx_all.pcmContextArray != nullptr, "pcmContextArray is allocated");
266 315
316 TestShapeContexts retry_ctx{};
317 retry_ctx.numXmaContexts = 2;
318 allocations_before_failure = 1; // The tracked allocation can grow, then voice slot allocation fails.
319 HRESULT allocation_result = E_UNEXPECTED;
320 try {
321 allocation_result = alloc_ctx(&retry_ctx);
322 } catch (const std::bad_alloc&) {
323 // An exception across the AcpHal boundary is a regression.
324 }
325 allocations_before_failure = -1;
326 check(allocation_result == E_OUTOFMEMORY && retry_ctx.xmaContextArray == nullptr,
327 "A failed XMA slot allocation returns E_OUTOFMEMORY without publishing a context");
328 check(alloc_ctx(&retry_ctx) == S_OK && retry_ctx.xmaContextArray != nullptr,
329 "SHAPE allocation can be retried after memory pressure");
330
267 // Release shape contexts 331 // Release shape contexts
268 check(release_ctx(&ctx_all) == S_OK, "AcpHalReleaseShapeContexts succeeds"); 332 check(release_ctx(&ctx_all) == S_OK, "AcpHalReleaseShapeContexts succeeds");
269 check(ctx_all.xmaContextArray == nullptr, "xmaContextArray is reset to null"); 333 check(ctx_all.xmaContextArray == nullptr, "xmaContextArray is reset to null");
@@ -359,44 +423,6 @@ int main() {
359 { 423 {
360 // Two synthetic XMA2 packets with three mono frames each. Each frame 424 // Two synthetic XMA2 packets with three mono frames each. Each frame
361 // transmits one positive spectral coefficient so decoded PCM differs from fallback silence. 425 // transmits one positive spectral coefficient so decoded PCM differs from fallback silence.
362 std::vector<uint8_t> stream(2 * 2048, 0xFFu);
363 auto put_bits = [&](size_t at, uint32_t value, size_t width) {
364 for (size_t i = 0; i < width; ++i) {
365 const size_t p = at + i;
366 const uint32_t bit = (value >> (width - 1 - i)) & 1u;
367 stream[p / 8] = static_cast<uint8_t>(
368 (stream[p / 8] & ~(0x80u >> (p % 8))) |
369 (bit << (7 - (p % 8))));
370 }
371 };
372 for (size_t packet = 0; packet < 2; ++packet) {
373 const size_t base = packet * 2048 * 8;
374 put_bits(base, (3u << 26) | (1u << 8), 32);
375 constexpr size_t frame_bits = 82;
376 for (size_t f = 0; f < 3; ++f) {
377 size_t at = base + 32 + f * frame_bits;
378 auto put = [&](uint32_t value, size_t width) {
379 put_bits(at, value, width);
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.
397 }
398 }
399
400 TestShapeContexts xma_ctx{}; 426 TestShapeContexts xma_ctx{};
401 xma_ctx.numXmaContexts = 4; 427 xma_ctx.numXmaContexts = 4;
402 check(alloc_ctx(&xma_ctx) == S_OK && xma_ctx.xmaContextArray != nullptr, 428 check(alloc_ctx(&xma_ctx) == S_OK && xma_ctx.xmaContextArray != nullptr,
@@ -406,7 +432,9 @@ int main() {
406 uint32_t in_apu = 0; 432 uint32_t in_apu = 0;
407 check(alloc(&in_va, &in_apu, 0x1000, 0x800, 1) == S_OK && in_va != nullptr, 433 check(alloc(&in_va, &in_apu, 0x1000, 0x800, 1) == S_OK && in_va != nullptr,
408 "ApuAlloc 2-packet XMA2 input buffer"); 434 "ApuAlloc 2-packet XMA2 input buffer");
409 std::memcpy(in_va, stream.data(), stream.size()); 435 auto* packets = static_cast<uint8_t*>(in_va);
436 write_xma_packet(packets, 3, 0);
437 write_xma_packet(packets + 2048, 3, 0);
410 438
411 void* out_va = nullptr; 439 void* out_va = nullptr;
412 uint32_t out_apu = 0; 440 uint32_t out_apu = 0;
@@ -499,27 +527,6 @@ int main() {
499 // 8b. Loops are rounded to whole packets: the loop end packet plays in full and the read 527 // 8b. Loops are rounded to whole packets: the loop end packet plays in full and the read
500 // restarts at the packet holding the loop start. One 1-frame packet = 512 samples = 4 blocks. 528 // restarts at the packet holding the loop start. One 1-frame packet = 512 samples = 4 blocks.
501 { 529 {
502 // Three packets with one silent mono frame each. Packet 0 skips packet 1,
503 // which belongs to another stream.
504 std::vector<uint8_t> stream(3 * 2048, 0xFFu);
505 auto put_bits = [&](size_t at, uint32_t value, size_t width) {
506 for (size_t i = 0; i < width; ++i) {
507 const size_t p = at + i;
508 const uint32_t bit = (value >> (width - 1 - i)) & 1u;
509 stream[p / 8] = static_cast<uint8_t>(
510 (stream[p / 8] & ~(0x80u >> (p % 8))) |
511 (bit << (7 - (p % 8))));
512 }
513 };
514 for (size_t packet = 0; packet < 3; ++packet) {
515 const size_t base = packet * 2048 * 8;
516 put_bits(base, (1u << 26) | (packet == 0 ? 1u : 0u), 32);
517 const size_t at = base + 32;
518 put_bits(at, 31, 15); // frame length
519 put_bits(at + 15, 0b10u, 2); // fixed layout, one 512-sample subframe
520 put_bits(at + 17, 0, 14); // gain(8) + trim(1) + subframe_skip/empty(5)
521 }
522
523 TestShapeContexts xma_ctx{}; 530 TestShapeContexts xma_ctx{};
524 xma_ctx.numXmaContexts = 4; 531 xma_ctx.numXmaContexts = 4;
525 check(alloc_ctx(&xma_ctx) == S_OK && xma_ctx.xmaContextArray != nullptr, 532 check(alloc_ctx(&xma_ctx) == S_OK && xma_ctx.xmaContextArray != nullptr,
@@ -527,9 +534,12 @@ int main() {
527 534
528 void* in_va = nullptr; 535 void* in_va = nullptr;
529 uint32_t in_apu = 0; 536 uint32_t in_apu = 0;
530 check(alloc(&in_va, &in_apu, 0x1800, 0x800, 1) == S_OK && in_va != nullptr, 537 check(alloc(&in_va, &in_apu, 0x2000, 0x800, 1) == S_OK && in_va != nullptr,
531 "ApuAlloc 3-packet loop input buffer"); 538 "ApuAlloc 4-packet loop and refill input buffer");
532 std::memcpy(in_va, stream.data(), stream.size()); 539 auto* packets = static_cast<uint8_t*>(in_va);
540 write_xma_packet(packets, 1, 1); // Packet 1 belongs to another stream.
541 write_xma_packet(packets + 0x800, 2, 0);
542 write_xma_packet(packets + 0x1000, 1, 0);
533 543
534 void* out_va = nullptr; 544 void* out_va = nullptr;
535 uint32_t out_apu = 0; 545 uint32_t out_apu = 0;
@@ -569,8 +579,10 @@ int main() {
569 579
570 const uint32_t voice_index = 2u; 580 const uint32_t voice_index = 2u;
571 // Returns the number of blocks written before input exhaustion, or -1 on timeout. 581 // 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 { 582 auto run_blocks = [&](uint32_t loops, uint32_t input_0_packets = 3,
583 uint32_t initial_skip = 0, uint32_t loop_skip = 0) -> int {
573 configure_voice(loops, input_0_packets, 16, 0, 0); 584 configure_voice(loops, input_0_packets, 16, 0, 0);
585 words[1].fetch_or((initial_skip << 24) | (loop_skip << 17));
574 check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &voice_index, 0) == S_OK, 586 check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &voice_index, 0) == S_OK,
575 "ENABLE_XMA voice 2 succeeds"); 587 "ENABLE_XMA voice 2 succeeds");
576 int wait = 0; 588 int wait = 0;
@@ -588,10 +600,26 @@ int main() {
588 check(run_blocks(0) == 8, "Packets 0 and 2 write 8 blocks, skipping packet 1"); 600 check(run_blocks(0) == 8, "Packets 0 and 2 write 8 blocks, skipping packet 1");
589 check(run_blocks(1) == 12, "One loop of packet 0 writes 12 blocks (packets 0, 0, 2)"); 601 check(run_blocks(1) == 12, "One loop of packet 0 writes 12 blocks (packets 0, 0, 2)");
590 check(((words[0] >> 12) & 0xFFu) == 0u, "Loop count is consumed by the single loop"); 602 check(((words[0] >> 12) & 0xFFu) == 0u, "Loop count is consumed by the single loop");
603 const auto* ring_bytes = static_cast<const uint8_t*>(out_va);
604#if defined(LEONIDA_HAVE_XMA2_DECODER)
605 auto has_signal = [](const uint8_t* start) {
606 return std::any_of(start, start + 0x400, [](uint8_t b) { return b != 0 && b != 0x55; });
607 };
608 check(has_signal(ring_bytes) && has_signal(ring_bytes + 0x400) && has_signal(ring_bytes + 0x800),
609 "Rust produces signal before and after rewinding the loop");
610 check(std::memcmp(ring_bytes, ring_bytes + 0x400, 0x400) == 0,
611 "Rewind decodes the first packet from fresh decoder state");
612#endif
613 check(run_blocks(1, 3, 2, 1) == 9 && ((words[1] >> 24) & 7u) == 0,
614 "Initial and loop subframe skips consume 3 blocks and clear the initial skip");
591 check(run_blocks(0, 2) == 8 && (words[4] >> 31) == 1u, 615 check(run_blocks(0, 2) == 8 && (words[4] >> 31) == 1u,
592 "A skip to the end of input 0 continues at input 1"); 616 "A skip to the end of input 0 continues at input 1");
593 check(run_blocks(0, 1) == 8 && (words[4] >> 31) == 1u, 617 check(run_blocks(0, 1) == 8 && (words[4] >> 31) == 1u,
594 "An overshooting skip starts at packet 1 of input 1"); 618 "An overshooting skip starts at packet 1 of input 1");
619#if defined(LEONIDA_HAVE_XMA2_DECODER)
620 check(has_signal(ring_bytes) && has_signal(ring_bytes + 0x400),
621 "Rust produces signal on both sides of the buffer switch");
622#endif
595 623
596 configure_voice(255, 3, 4, 7, 9); 624 configure_voice(255, 3, 4, 7, 9);
597 check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &voice_index, 0) == S_OK, 625 check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &voice_index, 0) == S_OK,
@@ -602,7 +630,6 @@ int main() {
602 check(fn_submit(x_acp, 7, 0xf4, 0xffffffffu, &voice_index, 0) == S_OK, 630 check(fn_submit(x_acp, 7, 0xf4, 0xffffffffu, &voice_index, 0) == S_OK,
603 "Disable infinite loop after filling bounded ring"); 631 "Disable infinite loop after filling bounded ring");
604 check(((words[0] >> 12) & 0xFFu) == 255u, "Infinite loop does not consume its loop count"); 632 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, 633 check(std::all_of(ring_bytes + 0x400, ring_bytes + 0x1000,
607 [](uint8_t b) { return b == 0x55; }), 634 [](uint8_t b) { return b == 0x55; }),
608 "Output never writes beyond the configured four-block ring"); 635 "Output never writes beyond the configured four-block ring");
@@ -613,6 +640,48 @@ int main() {
613 check(fn_submit(x_acp, 18, 0xf5, 0xffffffffu, advance_args, 0) == S_OK && words[9] == 2u, 640 check(fn_submit(x_acp, 18, 0xf5, 0xffffffffu, advance_args, 0) == S_OK && words[9] == 2u,
614 "ADVANCE_XMA_READ wraps at the ring size without overflowing"); 641 "ADVANCE_XMA_READ wraps at the ring size without overflowing");
615 642
643 auto wait_for_ring = [&]() {
644 for (int wait = 0; wait < 1000 && (words[1] >> 31) != 0u; ++wait) Sleep(1);
645 return (words[1] >> 31) == 0u;
646 };
647 const uint32_t refill_read_args[2] = {voice_index, 4};
648
649 configure_voice(0, 3, 4, 0, 0);
650 words[0] = 1u | (1u << 20) | (4u << 22);
651 write_xma_packet(packets, 3, 0);
652 check(fn_submit(x_acp, 5, 0xf6, 0xffffffffu, &voice_index, 0) == S_OK && wait_for_ring(),
653 "First packet fills the ring and leaves queued PCM");
654 words[0].fetch_or(1u << 20);
655 check(fn_submit(x_acp, 5, 0xf7, 0xffffffffu, &voice_index, 0) == S_OK,
656 "Re-enable while refilling the current input");
657 int refills = 0;
658 for (int i = 0; i < 5; ++i) {
659 if (fn_submit(x_acp, 18, 0xf8, 0xffffffffu, refill_read_args, 0) != S_OK || !wait_for_ring()) break;
660 ++refills;
661 }
662 check(refills == 5 && (words[2] >> 31) == 0u && ((words[0] >> 20) & 3u) == 0u,
663 "Refill and repeated enable preserve all six frames and consume the reused input");
664 check(fn_submit(x_acp, 7, 0xf9, 0xffffffffu, &voice_index, 0) == S_OK,
665 "Disable the single-buffer refill voice");
666
667 // The next same-stream packet is in the first input's refill, after a short second input.
668 write_xma_packet(packets, 1, 2);
669 write_xma_packet(packets + 0x1000, 2, 0);
670 write_xma_packet(packets + 0x1800, 1, 0);
671 configure_voice(0, 1, 4, 0, 0);
672 words[1] = (words[1] & ~0xFFFu) | 1u;
673 check(fn_submit(x_acp, 5, 0xfa, 0xffffffffu, &voice_index, 0) == S_OK && wait_for_ring(),
674 "Voice fills the ring before the skip crosses two buffers");
675 words[5] = in_apu + 0x1000u;
676 words[0] = (words[0] & ~0xFFFu) | 2u | (1u << 20);
677 check(fn_submit(x_acp, 18, 0xfb, 0xffffffffu, refill_read_args, 0) == S_OK && wait_for_ring(),
678 "The same-stream packet fills the ring after two buffer crossings");
679 check(((words[0] >> 20) & 3u) == 0u && (words[4] >> 31) == 0u,
680 "A skip across both buffers lands on the last packet of the reloaded input");
681 check(fn_submit(x_acp, 7, 0xfc, 0xffffffffu, &voice_index, 0) == S_OK,
682 "Disable the two-buffer refill voice");
683
684 check(release_ctx(x_acp) == S_OK, "Release SHAPE contexts through the opaque AcpHal handle");
616 check(fn_release(x_acp) == S_OK, "Release packet loop AcpHal succeeds"); 685 check(fn_release(x_acp) == S_OK, "Release packet loop AcpHal succeeds");
617 free_mem(in_va); 686 free_mem(in_va);
618 free_mem(out_va); 687 free_mem(out_va);