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:
Diffstat (limited to 'runtime/shims/acphal.cpp')
-rw-r--r--runtime/shims/acphal.cpp64
1 files changed, 26 insertions, 38 deletions
diff --git a/runtime/shims/acphal.cpp b/runtime/shims/acphal.cpp
index 2688f96e5..05ceedd17 100644
--- a/runtime/shims/acphal.cpp
+++ b/runtime/shims/acphal.cpp
@@ -139,24 +139,7 @@ struct SHAPE_DMA_CONTEXT {
139static_assert(sizeof(SHAPE_DMA_CONTEXT) == 16, "SHAPE_DMA_CONTEXT is 16 bytes per GDK"); 139static_assert(sizeof(SHAPE_DMA_CONTEXT) == 16, "SHAPE_DMA_CONTEXT is 16 bytes per GDK");
140 140
141struct SHAPE_XMA_CONTEXT { 141struct SHAPE_XMA_CONTEXT {
142 uint32_t sizeRead0 : 12; 142 uint32_t words[16]; // See the shared word layout above the XMA engine.
143 uint32_t numLoops : 8;
144 uint32_t validBuffer : 2;
145 uint32_t sizeWrite : 5;
146 uint32_t offsetWrite : 5;
147 uint32_t sizeRead1 : 12;
148 uint32_t sequenceNumber : 12;
149 uint32_t stopSampleAddress : 8;
150 uint32_t subframeOffset : 4;
151 uint32_t reserved0 : 1;
152 uint32_t isLooping : 1;
153 uint32_t subframeSkip : 2;
154 uint32_t packetMetadata : 16;
155 uint32_t currentByteOffset : 26;
156 uint32_t reserved1 : 6;
157 uint32_t loopStartOffset : 26;
158 uint32_t loopEndOffset : 6;
159 uint32_t reserved[11]; // the GDK's 64-byte context: the published field list leaves one trailing word
160}; 143};
161static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64, "SHAPE_XMA_CONTEXT is 64 bytes per GDK"); 144static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64, "SHAPE_XMA_CONTEXT is 64 bytes per GDK");
162 145
@@ -382,10 +365,9 @@ struct Xma2PcmFree {
382#endif 365#endif
383 366
384struct XmaContextView { 367struct XmaContextView {
385 volatile uint32_t* words = nullptr; 368 uint32_t* words = nullptr;
386 369
387 uint32_t get(std::size_t index) const { return words[index]; } 370 uint32_t get(std::size_t index) const { return std::atomic_ref(words[index]).load(); }
388 void set(std::size_t index, uint32_t value) const { words[index] = value; }
389 371
390 uint32_t field(std::size_t index, uint32_t shift, uint32_t bits) const { 372 uint32_t field(std::size_t index, uint32_t shift, uint32_t bits) const {
391 const uint32_t mask = (bits >= 32) ? 0xFFFFFFFFu : ((1u << bits) - 1u); 373 const uint32_t mask = (bits >= 32) ? 0xFFFFFFFFu : ((1u << bits) - 1u);
@@ -395,7 +377,10 @@ struct XmaContextView {
395 void set_field(std::size_t index, uint32_t shift, uint32_t bits, uint32_t value) const { 377 void set_field(std::size_t index, uint32_t shift, uint32_t bits, uint32_t value) const {
396 const uint32_t raw_mask = (bits >= 32) ? 0xFFFFFFFFu : ((1u << bits) - 1u); 378 const uint32_t raw_mask = (bits >= 32) ? 0xFFFFFFFFu : ((1u << bits) - 1u);
397 const uint32_t mask = raw_mask << shift; 379 const uint32_t mask = raw_mask << shift;
398 set(index, (get(index) & ~mask) | ((value << shift) & mask)); 380 const uint32_t field_value = (value << shift) & mask;
381 std::atomic_ref word(words[index]);
382 uint32_t previous = word.load();
383 while (!word.compare_exchange_weak(previous, (previous & ~mask) | field_value)) {}
399 } 384 }
400 385
401 std::size_t channels() const { return 1u + static_cast<std::size_t>(field(1, 29, 1)); } 386 std::size_t channels() const { return 1u + static_cast<std::size_t>(field(1, 29, 1)); }
@@ -407,6 +392,7 @@ struct XmaVoice {
407#endif 392#endif
408 std::deque<int16_t> pcm; 393 std::deque<int16_t> pcm;
409 std::size_t skip = 0; // samples per channel to drop (start/loop subframe skip) 394 std::size_t skip = 0; // samples per channel to drop (start/loop subframe skip)
395 std::optional<std::size_t> next_input_packet;
410 bool ended = false; 396 bool ended = false;
411}; 397};
412 398
@@ -422,7 +408,7 @@ public:
422 void bind(SHAPE_XMA_CONTEXT* array, uint32_t count) { 408 void bind(SHAPE_XMA_CONTEXT* array, uint32_t count) {
423 unbind(); 409 unbind();
424 std::unique_lock lock(table_mutex_); 410 std::unique_lock lock(table_mutex_);
425 array_ = reinterpret_cast<volatile uint32_t*>(array); 411 array_ = array;
426 voices_.clear(); 412 voices_.clear();
427 voices_.reserve(count); 413 voices_.reserve(count);
428 for (uint32_t i = 0; i < count; ++i) { 414 for (uint32_t i = 0; i < count; ++i) {
@@ -522,8 +508,8 @@ public:
522 bool wake = false; 508 bool wake = false;
523 { 509 {
524 std::lock_guard slot_lock(voices_[index]->mutex); 510 std::lock_guard slot_lock(voices_[index]->mutex);
525 const uint32_t raw_size = ctx.field(0, 22, 5); 511 const uint32_t size = ctx.field(0, 22, 5);
526 const uint32_t size = raw_size ? raw_size : 1u; 512 if (size == 0) return;
527 const uint32_t read = (ctx.field(9, 0, 5) + blocks % size) % size; 513 const uint32_t read = (ctx.field(9, 0, 5) + blocks % size) % size;
528 ctx.set_field(9, 0, 5, read); 514 ctx.set_field(9, 0, 5, read);
529 if (blocks > 0) { 515 if (blocks > 0) {
@@ -537,7 +523,7 @@ public:
537private: 523private:
538 XmaContextView context_locked(uint32_t index) const { 524 XmaContextView context_locked(uint32_t index) const {
539 if (!array_ || index >= voices_.size()) return {}; 525 if (!array_ || index >= voices_.size()) return {};
540 return XmaContextView{array_ + static_cast<std::size_t>(index) * 16}; 526 return XmaContextView{array_[index].words};
541 } 527 }
542 528
543 // Fills `pcm` (interleaved s16) for one 2048-byte packet. Without the Rust decoder, or when it 529 // Fills `pcm` (interleaved s16) for one 2048-byte packet. Without the Rust decoder, or when it
@@ -559,7 +545,7 @@ private:
559 const std::unique_ptr<LeonidaXma2PcmBlock, Xma2PcmFree> block(raw); 545 const std::unique_ptr<LeonidaXma2PcmBlock, Xma2PcmFree> block(raw);
560 const int16_t* samples = leonida_xma2_pcm_data(block.get()); 546 const int16_t* samples = leonida_xma2_pcm_data(block.get());
561 const std::size_t count = leonida_xma2_pcm_sample_count(block.get()); 547 const std::size_t count = leonida_xma2_pcm_sample_count(block.get());
562 pcm.assign(samples, samples + count); 548 if (count != 0) pcm.assign(samples, samples + count);
563 return; 549 return;
564 } 550 }
565 voice.decoder.reset(); // Resume with a clean state after replacing a packet with silence. 551 voice.decoder.reset(); // Resume with a clean state after replacing a packet with silence.
@@ -580,7 +566,7 @@ private:
580 uint32_t current = ctx.field(4, 31, 1); 566 uint32_t current = ctx.field(4, 31, 1);
581 const uint32_t valid = ctx.field(0, 20, 2); 567 const uint32_t valid = ctx.field(0, 20, 2);
582 568
583 if ((valid & (1u << current)) == 0) { 569 if (voice.next_input_packet || (valid & (1u << current)) == 0) {
584 const uint32_t other = 1u - current; 570 const uint32_t other = 1u - current;
585 if ((valid & (1u << other)) == 0) { 571 if ((valid & (1u << other)) == 0) {
586 voice.ended = true; 572 voice.ended = true;
@@ -590,7 +576,9 @@ private:
590 } 576 }
591 current = other; 577 current = other;
592 ctx.set_field(4, 31, 1, current); 578 ctx.set_field(4, 31, 1, current);
593 ctx.set_field(2, 0, 26, 0); 579 const auto next_bit = voice.next_input_packet.value_or(0) * kXmaPacketBits;
580 ctx.set_field(2, 0, 26, static_cast<uint32_t>(next_bit));
581 voice.next_input_packet.reset();
594 } 582 }
595 583
596 const uint32_t address = (current == 0) ? ctx.get(5) : ctx.get(6); 584 const uint32_t address = (current == 0) ? ctx.get(5) : ctx.get(6);
@@ -599,6 +587,7 @@ private:
599 const uint8_t* data = apu_span(address & ~0x7FFu, packets * kXmaPacketBytes); 587 const uint8_t* data = apu_span(address & ~0x7FFu, packets * kXmaPacketBytes);
600 if (!data || packet >= packets) { 588 if (!data || packet >= packets) {
601 ctx.set_field(0, 20 + current, 1, 0); 589 ctx.set_field(0, 20 + current, 1, 0);
590 voice.next_input_packet = 0;
602 ctx.set_field(2, 0, 26, 0); 591 ctx.set_field(2, 0, 26, 0);
603 return true; 592 return true;
604 } 593 }
@@ -627,6 +616,7 @@ private:
627 } 616 }
628 if (next >= packets) { 617 if (next >= packets) {
629 ctx.set_field(0, 20 + current, 1, 0); 618 ctx.set_field(0, 20 + current, 1, 0);
619 voice.next_input_packet = next - packets;
630 next = 0; 620 next = 0;
631 } 621 }
632 ctx.set_field(2, 0, 26, static_cast<uint32_t>(next * kXmaPacketBits)); 622 ctx.set_field(2, 0, 26, static_cast<uint32_t>(next * kXmaPacketBits));
@@ -635,20 +625,18 @@ private:
635 625
636 static void pump_voice(XmaContextView ctx, XmaVoice& voice) { 626 static void pump_voice(XmaContextView ctx, XmaVoice& voice) {
637 const uint32_t size = ctx.field(0, 22, 5); 627 const uint32_t size = ctx.field(0, 22, 5);
638 if (size == 0) return; 628 auto* ring = apu_span(ctx.get(7), size * kXmaBlockBytes);
629 if (!ring) return;
639 630
640 while (ctx.field(1, 31, 1) == 1) { 631 for (uint32_t blocks = 0; blocks < size && ctx.field(1, 31, 1) == 1; ++blocks) {
641 int decode_guard = 0; 632 int decode_guard = 0;
642 while (voice.pcm.size() < kXmaSamplesPerBlock && decode_guard++ < 16) { 633 while (voice.pcm.size() < kXmaSamplesPerBlock && decode_guard++ < 16) {
643 if (!decode_one_packet(ctx, voice)) break; 634 if (!decode_one_packet(ctx, voice)) break;
644 } 635 }
645 if (voice.pcm.empty()) return; 636 if (voice.pcm.empty()) return;
646 637
647 const uint32_t write = ctx.field(0, 27, 5); 638 const uint32_t write = ctx.field(0, 27, 5) % size;
648 const uint32_t out_addr = 639 uint8_t* out = ring + write * kXmaBlockBytes;
649 ctx.get(7) + write * static_cast<uint32_t>(kXmaBlockBytes);
650 uint8_t* out = apu_span(out_addr, kXmaBlockBytes);
651 if (!out) return;
652 640
653 for (std::size_t i = 0; i < kXmaSamplesPerBlock; ++i) { 641 for (std::size_t i = 0; i < kXmaSamplesPerBlock; ++i) {
654 int16_t sample = 0; 642 int16_t sample = 0;
@@ -663,7 +651,7 @@ private:
663 651
664 const uint32_t next_write = (write + 1u) % size; 652 const uint32_t next_write = (write + 1u) % size;
665 ctx.set_field(0, 27, 5, next_write); 653 ctx.set_field(0, 27, 5, next_write);
666 if (next_write == ctx.field(9, 0, 5)) { 654 if (next_write == ctx.field(9, 0, 5) % size) {
667 ctx.set_field(1, 31, 1, 0); 655 ctx.set_field(1, 31, 1, 0);
668 } 656 }
669 } 657 }
@@ -728,7 +716,7 @@ private:
728 } 716 }
729 717
730 mutable std::shared_mutex table_mutex_; 718 mutable std::shared_mutex table_mutex_;
731 volatile uint32_t* array_ = nullptr; 719 SHAPE_XMA_CONTEXT* array_ = nullptr;
732 std::vector<std::unique_ptr<XmaVoiceSlot>> voices_; 720 std::vector<std::unique_ptr<XmaVoiceSlot>> voices_;
733 std::atomic<uint32_t> active_count_{0}; 721 std::atomic<uint32_t> active_count_{0};
734 722