upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/acphal.cpp
diff options
context:
space:
mode:
authorandretuta <andreviniciusmerlia@gmail.com>2026-11-24 04:03:38 -0300
committerandretuta <andreviniciusmerlia@gmail.com>2026-11-24 04:03:38 -0300
commita99a3fb6ad419bf7f0a255d8b31fbd8112da1b39 (patch)
treebd38ee3601631b9bb8d1b8b8391fd82233cdb29c /runtime/shims/acphal.cpp
parentcfcedb58e2802c5cd38afd1cbde877ae9d71c2a5 (diff)
acphal: preserve XMA packet chains and context lifetime
Diffstat (limited to 'runtime/shims/acphal.cpp')
-rw-r--r--runtime/shims/acphal.cpp113
1 files changed, 56 insertions, 57 deletions
diff --git a/runtime/shims/acphal.cpp b/runtime/shims/acphal.cpp
index 7f4de8fd8..2688f96e5 100644
--- a/runtime/shims/acphal.cpp
+++ b/runtime/shims/acphal.cpp
@@ -303,6 +303,7 @@ ApuRegion g_cached_heap;
303ApuRegion g_noncached_heap; 303ApuRegion g_noncached_heap;
304 304
305// Tracked SHAPE context arrays 305// Tracked SHAPE context arrays
306std::mutex g_shape_mutex;
306std::vector<void*> g_active_shape_arrays; 307std::vector<void*> g_active_shape_arrays;
307 308
308// Resolves a contiguous [apu, apu + len) byte range inside the APU heap. 309// Resolves a contiguous [apu, apu + len) byte range inside the APU heap.
@@ -419,7 +420,7 @@ public:
419 ~XmaEngine() { unbind(); } 420 ~XmaEngine() { unbind(); }
420 421
421 void bind(SHAPE_XMA_CONTEXT* array, uint32_t count) { 422 void bind(SHAPE_XMA_CONTEXT* array, uint32_t count) {
422 stop_worker(); 423 unbind();
423 std::unique_lock lock(table_mutex_); 424 std::unique_lock lock(table_mutex_);
424 array_ = reinterpret_cast<volatile uint32_t*>(array); 425 array_ = reinterpret_cast<volatile uint32_t*>(array);
425 voices_.clear(); 426 voices_.clear();
@@ -427,15 +428,17 @@ public:
427 for (uint32_t i = 0; i < count; ++i) { 428 for (uint32_t i = 0; i < count; ++i) {
428 voices_.push_back(std::make_unique<XmaVoiceSlot>()); 429 voices_.push_back(std::make_unique<XmaVoiceSlot>());
429 } 430 }
430 active_count_.store(0, std::memory_order_relaxed);
431 } 431 }
432 432
433 void unbind() { 433 void unbind() {
434 {
435 std::unique_lock lock(table_mutex_);
436 array_ = nullptr;
437 voices_.clear();
438 active_count_.store(0, std::memory_order_relaxed);
439 }
440 // Once detached, commands cannot start a worker for this table.
434 stop_worker(); 441 stop_worker();
435 std::unique_lock lock(table_mutex_);
436 array_ = nullptr;
437 voices_.clear();
438 active_count_.store(0, std::memory_order_relaxed);
439 } 442 }
440 443
441 bool handle_command(uint32_t command, const void* config) { 444 bool handle_command(uint32_t command, const void* config) {
@@ -481,11 +484,11 @@ public:
481 } 484 }
482 485
483 void enable(uint32_t index) { 486 void enable(uint32_t index) {
484 { 487 std::shared_lock table_lock(table_mutex_);
485 std::shared_lock table_lock(table_mutex_); 488 XmaContextView ctx = context_locked(index);
486 XmaContextView ctx = context_locked(index); 489 if (!ctx.words) return;
487 if (!ctx.words) return;
488 490
491 {
489 std::lock_guard slot_lock(voices_[index]->mutex); 492 std::lock_guard slot_lock(voices_[index]->mutex);
490 const bool was_active = voices_[index]->voice.has_value(); 493 const bool was_active = voices_[index]->voice.has_value();
491 // Clear error_status (26..30) and error flag (31). 494 // Clear error_status (26..30) and error flag (31).
@@ -512,16 +515,16 @@ public:
512 } 515 }
513 516
514 void advance_read(uint32_t index, uint32_t blocks) { 517 void advance_read(uint32_t index, uint32_t blocks) {
518 std::shared_lock table_lock(table_mutex_);
519 XmaContextView ctx = context_locked(index);
520 if (!ctx.words) return;
521
515 bool wake = false; 522 bool wake = false;
516 { 523 {
517 std::shared_lock table_lock(table_mutex_);
518 XmaContextView ctx = context_locked(index);
519 if (!ctx.words) return;
520
521 std::lock_guard slot_lock(voices_[index]->mutex); 524 std::lock_guard slot_lock(voices_[index]->mutex);
522 const uint32_t raw_size = ctx.field(0, 22, 5); 525 const uint32_t raw_size = ctx.field(0, 22, 5);
523 const uint32_t size = raw_size ? raw_size : 1u; 526 const uint32_t size = raw_size ? raw_size : 1u;
524 const uint32_t read = (ctx.field(9, 0, 5) + blocks) % size; 527 const uint32_t read = (ctx.field(9, 0, 5) + blocks % size) % size;
525 ctx.set_field(9, 0, 5, read); 528 ctx.set_field(9, 0, 5, read);
526 if (blocks > 0) { 529 if (blocks > 0) {
527 ctx.set_field(1, 31, 1, 1); 530 ctx.set_field(1, 31, 1, 1);
@@ -559,6 +562,7 @@ private:
559 pcm.assign(samples, samples + count); 562 pcm.assign(samples, samples + count);
560 return; 563 return;
561 } 564 }
565 voice.decoder.reset(); // Resume with a clean state after replacing a packet with silence.
562 } 566 }
563#else 567#else
564 (void)voice; 568 (void)voice;
@@ -599,13 +603,16 @@ private:
599 return true; 603 return true;
600 } 604 }
601 605
606 const uint8_t* packet_data = data + packet * kXmaPacketBytes;
607 const uint8_t packet_skip = packet_data[3];
602 std::vector<int16_t> pcm; 608 std::vector<int16_t> pcm;
603 decode_packet(ctx, voice, data + packet * kXmaPacketBytes, pcm); 609 decode_packet(ctx, voice, packet_data, pcm);
604 const std::size_t skip_samples = std::min(voice.skip * channels, pcm.size()); 610 const std::size_t skip_samples = std::min(voice.skip * channels, pcm.size());
605 voice.skip -= skip_samples / channels; 611 voice.skip -= skip_samples / channels;
606 voice.pcm.insert(voice.pcm.end(), pcm.begin() + static_cast<std::ptrdiff_t>(skip_samples), pcm.end()); 612 voice.pcm.insert(voice.pcm.end(), pcm.begin() + static_cast<std::ptrdiff_t>(skip_samples), pcm.end());
607 613
608 std::size_t next = packet + 1; 614 // XMA2 packet headers link packets from the same stream; 0xff ends the chain.
615 std::size_t next = packet_skip == 0xFFu ? packets : packet + 1 + packet_skip;
609 const uint32_t loops = ctx.field(0, 12, 8); 616 const uint32_t loops = ctx.field(0, 12, 8);
610 const std::size_t loop_end = ctx.field(4, 0, 26); 617 const std::size_t loop_end = ctx.field(4, 0, 26);
611 if (loops > 0 && loop_end != 0 && packet == loop_end / kXmaPacketBits) { 618 if (loops > 0 && loop_end != 0 && packet == loop_end / kXmaPacketBits) {
@@ -921,45 +928,38 @@ HRESULT __stdcall AcpHalCreate(void** ppAcpHal) {
921HRESULT __stdcall AcpHalAllocateShapeContexts(SHAPE_CONTEXTS* ctx) { 928HRESULT __stdcall AcpHalAllocateShapeContexts(SHAPE_CONTEXTS* ctx) {
922 if (!ctx) return E_POINTER; 929 if (!ctx) return E_POINTER;
923 930
924 SHAPE_XMA_CONTEXT* xma_array = nullptr; 931 std::lock_guard lock(g_shape_mutex);
925 uint32_t xma_count = 0;
926 {
927 std::lock_guard lock(g_apu_mutex);
928 932
929 auto alloc_array = [](uint32_t count, size_t elem_size) -> void* { 933 auto alloc_array = [](uint32_t count, size_t elem_size) -> void* {
930 if (count == 0) return nullptr; 934 if (count == 0) return nullptr;
931 void* ptr = std::calloc(count, elem_size); 935 void* ptr = std::calloc(count, elem_size);
932 if (ptr) g_active_shape_arrays.push_back(ptr); 936 if (ptr) g_active_shape_arrays.push_back(ptr);
933 return ptr; 937 return ptr;
934 }; 938 };
935 939
936 ctx->srcContextArray = static_cast<SHAPE_SRC_CONTEXT*>( 940 ctx->srcContextArray = static_cast<SHAPE_SRC_CONTEXT*>(
937 alloc_array(ctx->numSrcContexts, sizeof(SHAPE_SRC_CONTEXT))); 941 alloc_array(ctx->numSrcContexts, sizeof(SHAPE_SRC_CONTEXT)));
938 ctx->eqCompContextArray = static_cast<SHAPE_EQCOMP_CONTEXT*>( 942 ctx->eqCompContextArray = static_cast<SHAPE_EQCOMP_CONTEXT*>(
939 alloc_array(ctx->numEqCompContexts, sizeof(SHAPE_EQCOMP_CONTEXT))); 943 alloc_array(ctx->numEqCompContexts, sizeof(SHAPE_EQCOMP_CONTEXT)));
940 ctx->filtVolContextArray = static_cast<SHAPE_FILTVOL_CONTEXT*>( 944 ctx->filtVolContextArray = static_cast<SHAPE_FILTVOL_CONTEXT*>(
941 alloc_array(ctx->numFiltVolContexts, sizeof(SHAPE_FILTVOL_CONTEXT))); 945 alloc_array(ctx->numFiltVolContexts, sizeof(SHAPE_FILTVOL_CONTEXT)));
942 ctx->dmaContextArray = static_cast<SHAPE_DMA_CONTEXT*>( 946 ctx->dmaContextArray = static_cast<SHAPE_DMA_CONTEXT*>(
943 alloc_array(ctx->numDmaContexts, sizeof(SHAPE_DMA_CONTEXT))); 947 alloc_array(ctx->numDmaContexts, sizeof(SHAPE_DMA_CONTEXT)));
944 ctx->xmaContextArray = static_cast<SHAPE_XMA_CONTEXT*>( 948 ctx->xmaContextArray = static_cast<SHAPE_XMA_CONTEXT*>(
945 alloc_array(ctx->numXmaContexts, sizeof(SHAPE_XMA_CONTEXT))); 949 alloc_array(ctx->numXmaContexts, sizeof(SHAPE_XMA_CONTEXT)));
946 ctx->pcmContextArray = static_cast<SHAPE_PCM_CONTEXT*>( 950 ctx->pcmContextArray = static_cast<SHAPE_PCM_CONTEXT*>(
947 alloc_array(ctx->numPcmContexts, sizeof(SHAPE_PCM_CONTEXT))); 951 alloc_array(ctx->numPcmContexts, sizeof(SHAPE_PCM_CONTEXT)));
948 952
949 // On PC there is no physical APU address translation for host context arrays; 953 // On PC there is no physical APU address translation for host context arrays;
950 // assign unique synthetic addresses from the APU base. 954 // assign unique synthetic addresses from the APU base.
951 ctx->apuSrcContextArray = {ctx->srcContextArray ? kApuBaseAddress : 0}; 955 ctx->apuSrcContextArray = {ctx->srcContextArray ? kApuBaseAddress : 0};
952 ctx->apuEqCompContextArray = {ctx->eqCompContextArray ? kApuBaseAddress + 0x1000 : 0}; 956 ctx->apuEqCompContextArray = {ctx->eqCompContextArray ? kApuBaseAddress + 0x1000 : 0};
953 ctx->apuFiltVolContextArray = {ctx->filtVolContextArray ? kApuBaseAddress + 0x2000 : 0}; 957 ctx->apuFiltVolContextArray = {ctx->filtVolContextArray ? kApuBaseAddress + 0x2000 : 0};
954 ctx->apuDmaContextArray = {ctx->dmaContextArray ? kApuBaseAddress + 0x3000 : 0}; 958 ctx->apuDmaContextArray = {ctx->dmaContextArray ? kApuBaseAddress + 0x3000 : 0};
955 ctx->apuXmaContextArray = {ctx->xmaContextArray ? kApuBaseAddress + 0x4000 : 0}; 959 ctx->apuXmaContextArray = {ctx->xmaContextArray ? kApuBaseAddress + 0x4000 : 0};
956 ctx->apuPcmContextArray = {ctx->pcmContextArray ? kApuBaseAddress + 0x5000 : 0}; 960 ctx->apuPcmContextArray = {ctx->pcmContextArray ? kApuBaseAddress + 0x5000 : 0};
957 961
958 xma_array = ctx->xmaContextArray; 962 g_xma_engine.bind(ctx->xmaContextArray, ctx->numXmaContexts);
959 xma_count = ctx->numXmaContexts;
960 }
961
962 g_xma_engine.bind(xma_array, xma_count);
963 963
964 trace_call("AcpHalAllocateShapeContexts", 964 trace_call("AcpHalAllocateShapeContexts",
965 ",\"src\":" + std::to_string(ctx->numSrcContexts) + 965 ",\"src\":" + std::to_string(ctx->numSrcContexts) +
@@ -971,11 +971,10 @@ HRESULT __stdcall AcpHalAllocateShapeContexts(SHAPE_CONTEXTS* ctx) {
971// ---- AcpHalReleaseShapeContexts ---- 971// ---- AcpHalReleaseShapeContexts ----
972 972
973HRESULT __stdcall AcpHalReleaseShapeContexts(SHAPE_CONTEXTS* ctx = nullptr) { 973HRESULT __stdcall AcpHalReleaseShapeContexts(SHAPE_CONTEXTS* ctx = nullptr) {
974 std::lock_guard lock(g_shape_mutex);
974 // Stop and detach the XMA worker before freeing backing arrays. 975 // Stop and detach the XMA worker before freeing backing arrays.
975 g_xma_engine.unbind(); 976 g_xma_engine.unbind();
976 977
977 std::lock_guard lock(g_apu_mutex);
978
979 // Free all recorded context allocations 978 // Free all recorded context allocations
980 for (void* p : g_active_shape_arrays) { 979 for (void* p : g_active_shape_arrays) {
981 if (p) std::free(p); 980 if (p) std::free(p);