1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
|
// acphal.dll: Audio Control Processor HAL. Scalable Hardware Audio Processing Engine
// (SHAPE) and APU (Audio Processing Unit) physical memory management for Xbox Series.
//
// Provides:
// - A VirtualAlloc-backed APU heap (cached + non-cached) with 32-bit device addresses;
// - SHAPE context arrays sized to the documented GDK strides;
// - The IAcpHal command and message queue (registration, completion, disconnect,
// flowgraph acknowledgement, and APU notification word writes);
// - A background XMA2 decoder pumping enabled SHAPE_XMA_CONTEXT output rings.
//
// Signatures from the public GDK reference (devdocs.xbox.com):
// AcpHal, APU reference, SHAPE_CONTEXTS, APU_ADDRESS.
// Measured call-site shapes and findings from the retail executable (1.0.2.1):
// - docs/retail_boot_chain.md, docs/retail_boot_frame_loop.md (Stop 28)
// - PR #6885 (call sites and parameter analysis), PR #7009 (audio/HAL bring-up).
#include <windows.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <climits>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <optional>
#include <set>
#include <shared_mutex>
#include <stop_token>
#include <string>
#include <string_view>
#include <system_error>
#include <thread>
#include <vector>
#include "shims.h"
#include "startup_trace.h"
#if defined(LEONIDA_HAVE_XMA2_DECODER)
#include "leonida_xma2_decoder.h"
#endif
namespace {
// ---- APU_ADDRESS (from the GDK) ----
// The console APU uses a 40-bit physical address space.
// On PC we translate to a 32-bit APU address offset.
struct APU_ADDRESS {
uint64_t value; // 40-bit physical address
};
// ---- SHAPE context structures (from the public GDK documentation at devdocs.xbox.com) ----
//
// These structures are defined here with their documented GDK field layouts so that
// sizeof() returns the correct per-context byte count. Without the real layouts the
// empty structs would be 1 byte each, and the title's writes (e.g. memset with the
// real stride) would overflow the allocation.
// UINT32 bitfields packed into 32-bit words
struct SHAPE_SRC_CONTEXT {
uint32_t timestamp : 21;
uint32_t blocksToSkip : 2;
uint32_t internalSaturate : 1;
uint32_t mixBufPeakMag : 4;
uint32_t peakMag : 4;
uint32_t samplingIncrement : 21;
uint32_t reserved0 : 1;
uint32_t cmd : 2;
uint32_t mixBufPeakMag2 : 4;
uint32_t peakMag2 : 4;
uint32_t sampleCount;
uint32_t samplePointer : 25;
uint32_t reserved1 : 7;
uint32_t samplingIncrementTarget : 21;
uint32_t reserved2 : 11;
uint32_t reserved[3];
};
static_assert(sizeof(SHAPE_SRC_CONTEXT) == 32, "SHAPE_SRC_CONTEXT is 32 bytes per GDK");
// The EQ/Comp context: 88 UINT32s = 352 bytes per GDK definition.
#pragma pack(push, 1)
struct alignas(4) SHAPE_EQCOMP_CONTEXT {
uint8_t data[352];
};
#pragma pack(pop)
static_assert(sizeof(SHAPE_EQCOMP_CONTEXT) == 352, "SHAPE_EQCOMP_CONTEXT is 352 bytes per GDK");
struct SHAPE_FILTVOL_CONTEXT {
uint32_t timestamp : 21;
uint32_t reserved0 : 2;
uint32_t internalSaturate : 1;
uint32_t mixBufPeakMag : 4;
uint32_t peakMag : 4;
uint32_t delay1 : 30;
uint32_t headroom : 2;
uint32_t delay2 : 30;
uint32_t svfMode : 2;
uint32_t gainTarget : 16;
uint32_t gain : 16;
uint32_t qRecip : 24;
uint32_t reserved1 : 8;
uint32_t qRecipTarget : 24;
uint32_t reserved2 : 8;
uint32_t fc : 24;
uint32_t reserved3 : 8;
uint32_t fcTarget : 24;
uint32_t reserved4 : 8;
};
static_assert(sizeof(SHAPE_FILTVOL_CONTEXT) == 32, "SHAPE_FILTVOL_CONTEXT is 32 bytes per GDK");
struct SHAPE_DMA_CONTEXT {
uint32_t timestamp : 21;
uint32_t reserved0 : 3;
uint32_t mixBufPeakMag : 4;
uint32_t peakMag : 4;
uint32_t readPointer : 5;
uint32_t reserved1 : 3;
uint32_t writePointer : 5;
uint32_t reserved2 : 3;
uint32_t full : 1;
uint32_t reserved3 : 15;
uint32_t reserved4 : 9;
uint32_t address : 23;
uint32_t numFrames : 5;
uint32_t reserved5 : 3;
uint32_t floatConvert : 1;
uint32_t reserved6 : 7;
uint32_t numChannels : 3;
uint32_t reserved7 : 5;
uint32_t channel : 3;
uint32_t reserved8 : 5;
};
static_assert(sizeof(SHAPE_DMA_CONTEXT) == 16, "SHAPE_DMA_CONTEXT is 16 bytes per GDK");
struct SHAPE_XMA_CONTEXT {
uint32_t words[16]; // See the shared word layout above the XMA engine.
};
static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64, "SHAPE_XMA_CONTEXT is 64 bytes per GDK");
struct SHAPE_PCM_CONTEXT {
uint32_t timestamp : 21;
uint32_t reserved0 : 11;
uint32_t loopCount : 8;
uint32_t reserved1 : 8;
uint32_t full : 1;
uint32_t reserved2 : 15;
uint32_t readPointer : 21;
uint32_t reserved3 : 11;
uint32_t loopStartWritePointer : 21;
uint32_t reserved4 : 11;
uint32_t loopEnd : 21;
uint32_t reserved5 : 11;
uint32_t mode : 1;
uint32_t reserved6 : 15;
uint32_t format : 2;
uint32_t reserved7 : 14;
uint32_t reserved[2]; // the GDK's 32-byte context: the published field list leaves one trailing word
};
static_assert(sizeof(SHAPE_PCM_CONTEXT) == 32, "SHAPE_PCM_CONTEXT is 32 bytes per GDK");
// The SHAPE_CONTEXTS structure (from the GDK)
struct SHAPE_CONTEXTS {
uint32_t numSrcContexts;
uint32_t numEqCompContexts;
uint32_t numFiltVolContexts;
uint32_t numDmaContexts;
uint32_t numXmaContexts;
uint32_t numPcmContexts;
SHAPE_SRC_CONTEXT* srcContextArray;
SHAPE_EQCOMP_CONTEXT* eqCompContextArray;
SHAPE_FILTVOL_CONTEXT* filtVolContextArray;
SHAPE_DMA_CONTEXT* dmaContextArray;
SHAPE_XMA_CONTEXT* xmaContextArray;
SHAPE_PCM_CONTEXT* pcmContextArray;
APU_ADDRESS apuSrcContextArray;
APU_ADDRESS apuEqCompContextArray;
APU_ADDRESS apuFiltVolContextArray;
APU_ADDRESS apuDmaContextArray;
APU_ADDRESS apuXmaContextArray;
APU_ADDRESS apuPcmContextArray;
};
static_assert(sizeof(SHAPE_CONTEXTS) == 120, "SHAPE_CONTEXTS is 120 bytes per GDK");
static_assert(offsetof(SHAPE_CONTEXTS, xmaContextArray) == 0x38,
"xmaContextArray is at offset +0x38 per retail binary measurement");
// ---- Startup trace helper ----
void trace_call(const char* api, std::string_view detail = {}) noexcept try {
if (!rt::startup_trace::enabled()) return;
static std::mutex mutex;
static std::set<std::string> seen;
{
const std::lock_guard lock(mutex);
if (!seen.insert(std::string(api).append(detail)).second) return;
}
rt::startup_trace::event("acphal",
(std::string("\"api\":\"") + api + "\"").append(detail));
} catch (...) {
// Tracing cannot change the result of a guest-facing call.
}
// ---- Genuine APU Heap Implementation ----
constexpr uint32_t kApuBaseAddress = 0x40000000u;
constexpr uint64_t kApuRegionAlignment = 0x10000u;
uint64_t align_apu_region(uint64_t size) {
return (size + kApuRegionAlignment - 1) & ~(kApuRegionAlignment - 1);
}
struct ApuRegion {
void* base = nullptr;
uint64_t size = 0;
std::map<uint32_t, uint32_t> blocks; // offset -> size of active allocations
bool contains(const void* ptr) const {
if (!base || !ptr) return false;
const uintptr_t p = reinterpret_cast<uintptr_t>(ptr);
const uintptr_t b = reinterpret_cast<uintptr_t>(base);
return p >= b && p < b + size;
}
bool is_valid_address(const void* ptr, uint32_t alignment) const {
if (!base || !ptr) return false;
const uintptr_t p = reinterpret_cast<uintptr_t>(ptr);
const uintptr_t b = reinterpret_cast<uintptr_t>(base);
if (p < b || p >= b + size) return false;
if (alignment == 0) alignment = 1;
return ((p - b) & (static_cast<uintptr_t>(alignment) - 1)) == 0;
}
uint32_t to_offset(const void* ptr) const {
return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr) -
reinterpret_cast<uintptr_t>(base));
}
void* from_offset(uint32_t offset) const {
if (offset >= size) return nullptr;
return static_cast<uint8_t*>(base) + offset;
}
uint8_t* span_from_offset(uint32_t offset, std::size_t len) const {
if (!base || len == 0 || offset >= size) return nullptr;
const uint64_t remaining = size - offset;
if (len > remaining) return nullptr;
return static_cast<uint8_t*>(base) + offset;
}
void* allocate(uint32_t alloc_size, uint32_t alignment, uint32_t* out_offset) {
if (alloc_size == 0 || alignment == 0) return nullptr;
uint64_t at = 0;
for (auto it = blocks.begin();; ++it) {
at = (at + alignment - 1) & ~static_cast<uint64_t>(alignment - 1);
const uint64_t limit = (it == blocks.end()) ? size : it->first;
if (at + alloc_size <= limit) break;
if (it == blocks.end()) return nullptr; // out of memory
at = static_cast<uint64_t>(it->first) + it->second;
}
const uint32_t offset = static_cast<uint32_t>(at);
blocks.emplace(offset, alloc_size);
uint8_t* ptr = static_cast<uint8_t*>(base) + offset;
std::memset(ptr, 0, alloc_size);
if (out_offset) *out_offset = offset;
return ptr;
}
bool free_block(void* ptr) {
if (!contains(ptr)) return false;
const uint32_t offset = to_offset(ptr);
return blocks.erase(offset) > 0;
}
void release() {
if (base) {
VirtualFree(base, 0, MEM_RELEASE);
base = nullptr;
}
size = 0;
blocks.clear();
}
};
std::mutex g_apu_mutex;
bool g_apu_heap_created = false;
ApuRegion g_cached_heap;
ApuRegion g_noncached_heap;
// Tracked SHAPE context arrays
struct ShapeArrayFree {
void operator()(void* array) const noexcept { std::free(array); }
};
using ShapeAllocation = std::array<std::unique_ptr<void, ShapeArrayFree>, 6>;
std::mutex g_shape_mutex;
std::vector<ShapeAllocation> g_shape_allocations;
// Resolves a contiguous [apu, apu + len) byte range inside the APU heap.
uint8_t* apu_span(uint32_t apu, std::size_t len) {
if (apu < kApuBaseAddress || len == 0) return nullptr;
std::lock_guard lock(g_apu_mutex);
const uint32_t offset = apu - kApuBaseAddress;
if (g_cached_heap.base && offset < g_cached_heap.size) {
return g_cached_heap.span_from_offset(offset, len);
}
if (g_noncached_heap.base) {
const uint64_t cached_span = align_apu_region(g_cached_heap.size);
if (offset >= cached_span && offset - cached_span < g_noncached_heap.size) {
return g_noncached_heap.span_from_offset(
static_cast<uint32_t>(offset - cached_span), len);
}
}
return nullptr;
}
// ---- ACP Command / Message Protocol Constants ----
constexpr uint32_t kCmdLoadFlowgraph = 1;
constexpr uint32_t kCmdRegisterMessages = 2;
constexpr uint32_t kCmdUnregisterMessages = 3;
constexpr uint32_t kCmdEnableXma = 5;
constexpr uint32_t kCmdEnableXmas = 6;
constexpr uint32_t kCmdDisableXma = 7;
constexpr uint32_t kCmdDisableXmas = 8;
constexpr uint32_t kCmdAdvanceXmaRead = 18;
constexpr uint32_t kMsgFlowgraphCompleted = 0x02u;
constexpr uint32_t kMsgCommandCompleted = 0x10u;
constexpr uint32_t kMsgDisconnected = 0x80u;
// 128 samples per audio frame at 48 kHz = 375 audio frames/second.
constexpr uint64_t kAudioFramesPerSecond = 375;
// ---- XMA2 Hardware Context Decoder Engine ----
//
// Each 64-byte SHAPE_XMA_CONTEXT holds 16 32-bit words shared with the title:
// Word 0: input 0 packets (0..11), loop count (12..19, 255 = forever),
// valid input bits (20..21), output ring blocks (22..26), write block (27..31).
// Word 1: input 1 packets (0..11), loop end subframe (12..13), loop start skip subframes
// (17..19), initial skip subframes (24..26), rate index (27..28: 24/32/44.1/48 kHz),
// stereo (29), output writable (31).
// Word 2: read bit offset (0..25), error status (26..30), error flag (31).
// Word 3: loop start bit offset (0..25).
// Word 4: loop end bit offset (0..25), current input buffer index (31).
// Word 5: input 0 APU address.
// Word 6: input 1 APU address.
// Word 7: output ring APU address.
// Word 8: overlap buffer APU address.
// Word 9: output read block index (0..4).
//
// Packets are decoded by the Rust decoder in runtime/xma_decoder when CMake found cargo (LEONIDA_HAVE_XMA2_DECODER);
// otherwise each packet becomes silence for its frame count. The read offset (word 2) and the loop points (words 3
// and 4) are used per 2048-byte packet, so the loop-end subframe (word 1 bits 12..13) is not applied.
constexpr std::size_t kXmaBlockBytes = 256;
constexpr std::size_t kXmaSamplesPerBlock = kXmaBlockBytes / 2;
constexpr std::size_t kXmaPacketBytes = 2048;
constexpr std::size_t kXmaPacketBits = kXmaPacketBytes * 8;
constexpr std::size_t kXmaSubframeSamples = 128;
constexpr std::size_t kXmaFrameSamples = 512;
constexpr uint32_t kXmaErrorInputInvalid = 4;
#if defined(LEONIDA_HAVE_XMA2_DECODER)
constexpr uint32_t kXmaRates[4] = {24000, 32000, 44100, 48000};
struct Xma2DecoderFree {
void operator()(LeonidaXma2Decoder* d) const noexcept { leonida_xma2_decoder_destroy(d); }
};
struct Xma2PcmFree {
void operator()(LeonidaXma2PcmBlock* b) const noexcept { leonida_xma2_pcm_destroy(b); }
};
#endif
struct XmaContextView {
uint32_t* words = nullptr;
uint32_t get(std::size_t index) const { return std::atomic_ref(words[index]).load(); }
uint32_t field(std::size_t index, uint32_t shift, uint32_t bits) const {
const uint32_t mask = (bits >= 32) ? 0xFFFFFFFFu : ((1u << bits) - 1u);
return (get(index) >> shift) & mask;
}
uint32_t set_field(std::size_t index, uint32_t shift, uint32_t bits, uint32_t value) const {
const uint32_t raw_mask = (bits >= 32) ? 0xFFFFFFFFu : ((1u << bits) - 1u);
const uint32_t mask = raw_mask << shift;
const uint32_t field_value = (value << shift) & mask;
std::atomic_ref word(words[index]);
uint32_t previous = word.load();
while (!word.compare_exchange_weak(previous, (previous & ~mask) | field_value)) {}
return previous;
}
};
struct XmaVoice {
explicit XmaVoice(uint32_t format_bits) : pcm(0), format(format_bits) {}
std::size_t channels() const { return 1u + (format >> 2); }
std::size_t queued_samples() const { return pcm.size() - pcm_read; }
#if defined(LEONIDA_HAVE_XMA2_DECODER)
std::unique_ptr<LeonidaXma2Decoder, Xma2DecoderFree> decoder;
#endif
std::vector<int16_t> pcm;
std::size_t pcm_read = 0;
std::size_t loop_skip = 0; // samples per channel to drop after a rewind
uint32_t format; // SHAPE word 1 bits 27..29, rate and channel layout
bool ended = false;
};
struct XmaVoiceSlot {
std::mutex mutex;
std::optional<XmaVoice> voice;
};
class XmaEngine {
public:
~XmaEngine() { unbind(); }
void bind(SHAPE_XMA_CONTEXT* array, uint32_t count) {
// Construct with a size so MSVC's Debug iterator allocations can also fail
// through this call instead of the noexcept default constructor.
std::vector<std::unique_ptr<XmaVoiceSlot>> slots(count);
for (auto& slot : slots) slot = std::make_unique<XmaVoiceSlot>();
unbind();
std::unique_lock lock(table_mutex_);
array_ = array;
voices_.swap(slots);
}
void unbind() {
{
std::unique_lock lock(table_mutex_);
array_ = nullptr;
voices_.clear();
active_count_.store(0, std::memory_order_relaxed);
}
// Once detached, commands cannot start a worker for this table.
stop_worker();
}
bool handle_command(uint32_t command, const void* config) {
auto read_word = [config](std::size_t index) -> uint32_t {
if (!config) return 0u;
uint32_t value = 0;
std::memcpy(&value, static_cast<const uint8_t*>(config) + index * sizeof(uint32_t),
sizeof(uint32_t));
return value;
};
switch (command) {
case kCmdEnableXma:
if (config) enable(read_word(0));
return true;
case kCmdDisableXma:
if (config) disable(read_word(0));
return true;
case kCmdEnableXmas:
case kCmdDisableXmas: {
std::shared_lock lock(table_mutex_);
const std::size_t count = voices_.size();
lock.unlock();
if (!config) return true;
for (std::size_t i = 0; i < count; ++i) {
const uint32_t bits = read_word(i / 32);
if ((bits & (1u << (i % 32))) != 0) {
if (command == kCmdEnableXmas) {
enable(static_cast<uint32_t>(i));
} else {
disable(static_cast<uint32_t>(i));
}
}
}
return true;
}
case kCmdAdvanceXmaRead:
if (config) advance_read(read_word(0), read_word(1));
return true;
default:
return false;
}
}
void enable(uint32_t index) {
std::shared_lock table_lock(table_mutex_);
XmaContextView ctx = context_locked(index);
if (!ctx.words) return;
{
std::lock_guard slot_lock(voices_[index]->mutex);
auto& voice = voices_[index]->voice;
const uint32_t format = ctx.field(1, 27, 3);
// The worker cannot access the voice until the slot is unlocked.
ensure_worker_and_wake();
if (!voice) {
voice.emplace(format);
active_count_.fetch_add(1, std::memory_order_relaxed);
} else if (voice->format != format) {
voice->pcm.clear();
voice->pcm_read = 0;
voice->loop_skip = 0;
voice->format = format;
#if defined(LEONIDA_HAVE_XMA2_DECODER)
voice->decoder.reset();
#endif
}
voice->ended = false;
ctx.set_field(2, 26, 6, 0);
}
}
void disable(uint32_t index) {
std::shared_lock table_lock(table_mutex_);
if (index >= voices_.size()) return;
std::lock_guard slot_lock(voices_[index]->mutex);
if (voices_[index]->voice.has_value()) {
voices_[index]->voice.reset();
active_count_.fetch_sub(1, std::memory_order_relaxed);
}
}
void advance_read(uint32_t index, uint32_t blocks) {
std::shared_lock table_lock(table_mutex_);
XmaContextView ctx = context_locked(index);
if (!ctx.words) return;
bool wake = false;
{
std::lock_guard slot_lock(voices_[index]->mutex);
const uint32_t size = ctx.field(0, 22, 5);
if (size == 0) return;
const uint32_t read = (ctx.field(9, 0, 5) + blocks % size) % size;
ctx.set_field(9, 0, 5, read);
if (blocks > 0) {
ctx.set_field(1, 31, 1, 1);
}
wake = voices_[index]->voice.has_value();
}
if (wake) ensure_worker_and_wake();
}
private:
XmaContextView context_locked(uint32_t index) const {
if (!array_ || index >= voices_.size()) return {};
return XmaContextView{array_[index].words};
}
// Appends interleaved s16 for one 2048-byte packet. Without the Rust decoder, or when it
// fails, the packet becomes silence for its frame count (header bits 26..31) so timing holds.
static void decode_packet(XmaVoice& voice, const uint8_t* bytes) {
const std::size_t channels = voice.channels();
#if defined(LEONIDA_HAVE_XMA2_DECODER)
if (!voice.decoder) {
LeonidaXma2Decoder* raw = nullptr;
if (leonida_xma2_decoder_create_hardware(kXmaRates[voice.format & 3u],
static_cast<uint32_t>(channels), &raw) == LEONIDA_XMA2_OK) {
voice.decoder.reset(raw);
}
}
if (voice.decoder) {
LeonidaXma2PcmBlock* raw = nullptr;
if (leonida_xma2_decode_buffer(voice.decoder.get(), bytes, kXmaPacketBytes, &raw) == LEONIDA_XMA2_OK) {
const std::unique_ptr<LeonidaXma2PcmBlock, Xma2PcmFree> block(raw);
const int16_t* samples = leonida_xma2_pcm_data(block.get());
const std::size_t count = leonida_xma2_pcm_sample_count(block.get());
if (count != 0) voice.pcm.insert(voice.pcm.end(), samples, samples + count);
return;
}
voice.decoder.reset(); // Resume with a clean state after replacing a packet with silence.
}
#endif
voice.pcm.resize(voice.pcm.size() + static_cast<std::size_t>(bytes[0] >> 2) * kXmaFrameSamples * channels);
}
// Decodes the packet at the read offset of the current input buffer and queues its PCM.
// Offsets and loop points are used per packet: a loop plays its end packet in full and
// restarts at the start of the packet holding the loop start.
static bool decode_one_packet(XmaContextView ctx, XmaVoice& voice) {
if (voice.ended) return false;
const std::size_t channels = voice.channels();
uint32_t current = ctx.field(4, 31, 1);
const uint32_t valid = ctx.field(0, 20, 2);
if (valid == 0) {
voice.ended = true;
ctx.set_field(2, 26, 6, kXmaErrorInputInvalid | 32u);
return voice.queued_samples() != 0;
}
if ((valid & (1u << current)) == 0) {
current ^= 1u;
ctx.set_field(4, 31, 1, current);
}
const uint32_t address = (current == 0) ? ctx.get(5) : ctx.get(6);
const std::size_t packets = (current == 0) ? ctx.field(0, 0, 12) : ctx.field(1, 0, 12);
const std::size_t packet = ctx.field(2, 0, 26) / kXmaPacketBits;
const uint8_t* data = apu_span(address & ~0x7FFu, packets * kXmaPacketBytes);
if (!data || packet >= packets) {
finish_input(ctx, current, packet >= packets ? packet - packets : 0);
return true;
}
const uint8_t* packet_data = data + packet * kXmaPacketBytes;
const uint8_t packet_skip = packet_data[3];
// Decoding is only requested when less than a ring block remains. Keep that
// tail and reuse the packet storage as the voice consumes subsequent packets.
voice.pcm.erase(voice.pcm.begin(), voice.pcm.begin() + static_cast<std::ptrdiff_t>(voice.pcm_read));
voice.pcm_read = 0;
const std::size_t packet_start = voice.pcm.size();
decode_packet(voice, packet_data);
const std::size_t packet_samples = voice.pcm.size() - packet_start;
const std::size_t subframe_samples = kXmaSubframeSamples * channels;
const std::size_t initial_skip = ctx.field(1, 24, 3);
const std::size_t skipped_subframes = std::min(initial_skip, packet_samples / subframe_samples);
const std::size_t initial_samples = skipped_subframes * subframe_samples;
const std::size_t loop_samples = std::min(voice.loop_skip * channels, packet_samples - initial_samples);
const std::size_t skip_samples = initial_samples + loop_samples;
if (skip_samples != 0) {
const auto first = voice.pcm.begin() + static_cast<std::ptrdiff_t>(packet_start);
voice.pcm.erase(first, first + static_cast<std::ptrdiff_t>(skip_samples));
}
// Update the context only after the PCM append succeeds.
if (skipped_subframes != 0) {
ctx.set_field(1, 24, 3, static_cast<uint32_t>(initial_skip - skipped_subframes));
}
voice.loop_skip -= loop_samples / channels;
// XMA2 packet headers link packets from the same stream; 0xff ends the chain.
std::size_t next = packet_skip == 0xFFu ? packets : packet + 1 + packet_skip;
const uint32_t loops = ctx.field(0, 12, 8);
const std::size_t loop_end = ctx.field(4, 0, 26);
if (loops > 0 && loop_end != 0 && packet == loop_end / kXmaPacketBits) {
next = ctx.field(3, 0, 26) / kXmaPacketBits;
voice.loop_skip = static_cast<std::size_t>(ctx.field(1, 17, 3)) * kXmaSubframeSamples;
#if defined(LEONIDA_HAVE_XMA2_DECODER)
// A frame spilling out of the loop-end packet does not continue in the loop-start packet:
// start a fresh decoder there rather than decode the spliced bits.
voice.decoder.reset();
#endif
if (loops != 255u) ctx.set_field(0, 12, 8, loops - 1u);
}
if (next >= packets) {
finish_input(ctx, current, next - packets);
} else {
ctx.set_field(2, 0, 26, static_cast<uint32_t>(next * kXmaPacketBits));
}
return true;
}
static void finish_input(XmaContextView ctx, uint32_t current,
std::size_t skip_packets) {
// Publish the next offset before telling the title it can refill this input.
ctx.set_field(2, 0, 26, static_cast<uint32_t>(skip_packets * kXmaPacketBits));
const uint32_t other = 1u - current;
const uint32_t retired = ctx.set_field(0, 20 + current, 1, 0);
// Take the other input if it was queued at retirement. Otherwise a double
// refill of the empty pair starts at input 0. A lone input can still run.
ctx.set_field(4, 31, 1, (retired & (1u << (20 + other))) != 0 ? other : 0u);
}
static void pump_voice(XmaContextView ctx, XmaVoice& voice) {
const uint32_t size = ctx.field(0, 22, 5);
if (size == 0 || ctx.field(1, 31, 1) == 0) return;
auto* ring = apu_span(ctx.get(7), size * kXmaBlockBytes);
if (!ring) return;
for (uint32_t blocks = 0; blocks < size && ctx.field(1, 31, 1) == 1; ++blocks) {
int decode_guard = 0;
while (voice.queued_samples() < kXmaSamplesPerBlock && decode_guard++ < 16) {
if (!decode_one_packet(ctx, voice)) break;
}
if (voice.queued_samples() == 0) return;
const uint32_t write = ctx.field(0, 27, 5) % size;
uint8_t* out = ring + write * kXmaBlockBytes;
for (std::size_t i = 0; i < kXmaSamplesPerBlock; ++i) {
int16_t sample = 0;
if (voice.pcm_read != voice.pcm.size()) {
sample = voice.pcm[voice.pcm_read++];
}
const uint16_t raw = static_cast<uint16_t>(sample);
out[2 * i] = static_cast<uint8_t>(raw & 0xFFu);
out[2 * i + 1] = static_cast<uint8_t>((raw >> 8) & 0xFFu);
}
const uint32_t next_write = (write + 1u) % size;
ctx.set_field(0, 27, 5, next_write);
if (next_write == ctx.field(9, 0, 5) % size) {
ctx.set_field(1, 31, 1, 0);
}
}
}
void pump_all() {
std::shared_lock table_lock(table_mutex_);
if (!array_) return;
for (std::size_t i = 0; i < voices_.size(); ++i) {
std::lock_guard slot_lock(voices_[i]->mutex);
auto& voice = voices_[i]->voice;
if (!voice) continue;
auto ctx = context_locked(static_cast<uint32_t>(i));
try {
pump_voice(ctx, *voice);
} catch (const std::bad_alloc&) {
// Only this voice failed. Retire it and allow the title to enable it again.
voice.reset();
active_count_.fetch_sub(1, std::memory_order_relaxed);
ctx.set_field(2, 26, 6, kXmaErrorInputInvalid | 32u);
}
}
}
void ensure_worker_and_wake() {
std::lock_guard lock(worker_mutex_);
if (!worker_.joinable()) {
worker_ = std::jthread([this](std::stop_token stop) { worker_loop(stop); });
}
wake_requested_ = true;
worker_cv_.notify_one();
}
// Each worker owns its stop_token, so a stop only ever reaches the thread it was
// sent to; a worker started after this call is unaffected by it.
void stop_worker() {
std::jthread to_join;
{
std::lock_guard lock(worker_mutex_);
if (!worker_.joinable()) return;
worker_.request_stop();
worker_cv_.notify_all();
to_join = std::move(worker_);
}
if (to_join.joinable()) to_join.join();
}
void worker_loop(std::stop_token stop) {
for (;;) {
{
std::unique_lock lock(worker_mutex_);
if (stop.stop_requested()) return;
if (active_count_.load(std::memory_order_relaxed) == 0 && !wake_requested_) {
worker_cv_.wait(lock, stop, [this]() { return wake_requested_; });
if (stop.stop_requested()) return;
}
wake_requested_ = false;
}
pump_all();
std::unique_lock lock(worker_mutex_);
if (stop.stop_requested()) return;
if (!wake_requested_ && active_count_.load(std::memory_order_relaxed) > 0) {
// One 256-byte output block lasts ~2.67 ms at 48 kHz mono.
worker_cv_.wait_for(lock, stop, std::chrono::milliseconds(2),
[this]() { return wake_requested_; });
}
}
}
mutable std::shared_mutex table_mutex_;
SHAPE_XMA_CONTEXT* array_ = nullptr;
std::vector<std::unique_ptr<XmaVoiceSlot>> voices_;
std::atomic<uint32_t> active_count_{0};
std::mutex worker_mutex_;
std::condition_variable_any worker_cv_;
std::jthread worker_;
bool wake_requested_ = false;
};
XmaEngine g_xma_engine;
// ---- IAcpHal Interface Implementation ----
#pragma pack(push, 1)
struct AcpMessage {
uint32_t kind = 0;
uint32_t dropped = 0;
uint32_t usec = 0;
uint8_t data[16]{};
};
#pragma pack(pop)
static_assert(sizeof(AcpMessage) == 28, "AcpMessage is 28 bytes");
void write_u32_le(uint8_t* dst, uint32_t value) {
dst[0] = static_cast<uint8_t>(value & 0xFFu);
dst[1] = static_cast<uint8_t>((value >> 8) & 0xFFu);
dst[2] = static_cast<uint8_t>((value >> 16) & 0xFFu);
dst[3] = static_cast<uint8_t>((value >> 24) & 0xFFu);
}
void write_u64_le(uint8_t* dst, uint64_t value) {
write_u32_le(dst, static_cast<uint32_t>(value & 0xFFFFFFFFu));
write_u32_le(dst + 4, static_cast<uint32_t>(value >> 32));
}
struct IAcpHal {
virtual HRESULT __stdcall connect(uint32_t commands, uint32_t messages) = 0;
virtual HRESULT __stdcall disconnect() = 0;
virtual HRESULT __stdcall submit_command(uint32_t cmd, uint64_t id, uint32_t frame,
const void* config, uint32_t notification) = 0;
virtual uint32_t __stdcall pop_message(void* out) = 0;
virtual uint32_t __stdcall get_num_messages() = 0;
virtual HRESULT __stdcall release() = 0;
};
struct AcpHalImpl final : IAcpHal {
std::mutex mutex_;
bool connected_ = false;
uint32_t mask_ = 0;
std::deque<AcpMessage> messages_;
std::size_t capacity_ = 0;
uint32_t dropped_ = 0;
std::chrono::steady_clock::time_point since_ = std::chrono::steady_clock::now();
uint64_t elapsed_usec_locked() const {
const auto dt = std::chrono::steady_clock::now() - since_;
return static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::microseconds>(dt).count());
}
uint32_t audio_frame_locked() const {
return static_cast<uint32_t>((elapsed_usec_locked() * kAudioFramesPerSecond) / 1000000ull);
}
void post_locked(uint32_t kind, const uint8_t* payload, std::size_t len) {
if ((mask_ & kind) == 0) return;
if (messages_.size() >= capacity_) {
if (dropped_ < UINT32_MAX) ++dropped_;
return;
}
AcpMessage msg{};
msg.kind = kind;
msg.dropped = dropped_;
msg.usec = static_cast<uint32_t>(elapsed_usec_locked());
if (payload && len > 0) {
std::memcpy(msg.data, payload, std::min(len, sizeof(msg.data)));
}
try {
messages_.push_back(msg);
dropped_ = 0;
} catch (const std::bad_alloc&) {
if (dropped_ < UINT32_MAX) ++dropped_;
}
}
// Slot 0: Connect (called at boot with (0x1000, 0x1000), result checked)
HRESULT __stdcall connect(uint32_t commands, uint32_t messages) override {
trace_call("AcpHal::Connect", "");
if (commands == 0) return E_INVALIDARG;
std::lock_guard lock(mutex_);
connected_ = true;
capacity_ = messages;
return S_OK;
}
// Slot 1: Disconnect (called at shutdown)
HRESULT __stdcall disconnect() override {
trace_call("AcpHal::Disconnect", "");
std::lock_guard lock(mutex_);
if (!connected_) return E_FAIL;
post_locked(kMsgDisconnected, nullptr, 0);
connected_ = false;
return S_OK;
}
// Slot 2: SubmitCommand
// Acknowledges command completion, updates registered message masks, dispatches
// XMA voice commands, and writes any requested APU notification word.
HRESULT __stdcall submit_command(uint32_t cmd, uint64_t id, uint32_t frame,
const void* config, uint32_t notification) override try {
(void)frame;
auto read_word = [config](std::size_t index) -> uint32_t {
if (!config) return 0u;
uint32_t value = 0;
std::memcpy(&value, static_cast<const uint8_t*>(config) + index * sizeof(uint32_t),
sizeof(uint32_t));
return value;
};
const bool handled_xma = g_xma_engine.handle_command(cmd, config);
{
std::lock_guard lock(mutex_);
if (!handled_xma) {
switch (cmd) {
case kCmdRegisterMessages:
mask_ |= read_word(0);
break;
case kCmdUnregisterMessages:
mask_ &= ~read_word(0);
break;
case kCmdLoadFlowgraph: {
uint8_t payload[4];
write_u32_le(payload, read_word(1));
post_locked(kMsgFlowgraphCompleted, payload, sizeof(payload));
break;
}
default:
break;
}
}
if (id != 0) {
uint8_t done[16];
write_u32_le(done, cmd);
write_u64_le(done + 4, id);
write_u32_le(done + 12, audio_frame_locked());
post_locked(kMsgCommandCompleted, done, sizeof(done));
}
}
if (notification != 0) {
if (uint8_t* word_bytes = apu_span(notification, sizeof(uint32_t))) {
if ((reinterpret_cast<uintptr_t>(word_bytes) & 3u) == 0) {
*reinterpret_cast<volatile uint32_t*>(word_bytes) = 1u;
} else {
static constexpr uint8_t kOneLe[4] = {1, 0, 0, 0};
volatile uint8_t* dst = word_bytes;
for (std::size_t i = 0; i < 4; ++i) dst[i] = kOneLe[i];
}
}
}
trace_call("AcpHal::SubmitCommand", "");
return S_OK;
} catch (const std::bad_alloc&) {
return E_OUTOFMEMORY;
} catch (const std::system_error&) {
return E_FAIL;
}
// Slot 3: PopMessage — returns 0 without touching `out` when the queue is empty.
uint32_t __stdcall pop_message(void* out) override {
std::lock_guard lock(mutex_);
if (messages_.empty()) return 0u;
const AcpMessage msg = messages_.front();
messages_.pop_front();
if (out) std::memcpy(out, &msg, sizeof(msg));
return 1u;
}
// Slot 4: GetNumMessages
uint32_t __stdcall get_num_messages() override {
std::lock_guard lock(mutex_);
return static_cast<uint32_t>(messages_.size());
}
// Slot 5: Release
HRESULT __stdcall release() override {
trace_call("AcpHal::Release", "");
delete this;
return S_OK;
}
};
// ---- AcpHalCreate ----
HRESULT __stdcall AcpHalCreate(void** ppAcpHal) {
if (!ppAcpHal) return E_POINTER;
*ppAcpHal = nullptr;
try {
*ppAcpHal = new AcpHalImpl();
} catch (const std::bad_alloc&) {
return E_OUTOFMEMORY;
}
trace_call("AcpHalCreate", "");
return S_OK;
}
// ---- AcpHalAllocateShapeContexts ----
HRESULT __stdcall AcpHalAllocateShapeContexts(SHAPE_CONTEXTS* ctx) {
if (!ctx) return E_POINTER;
std::lock_guard lock(g_shape_mutex);
ShapeAllocation pending{};
std::size_t array_index = 0;
auto alloc_array = [&](uint32_t count, size_t elem_size) -> void* {
if (count == 0) return nullptr;
void* ptr = std::calloc(count, elem_size);
if (!ptr) throw std::bad_alloc();
pending[array_index++].reset(ptr);
return ptr;
};
SHAPE_CONTEXTS result{};
result.numSrcContexts = ctx->numSrcContexts;
result.numEqCompContexts = ctx->numEqCompContexts;
result.numFiltVolContexts = ctx->numFiltVolContexts;
result.numDmaContexts = ctx->numDmaContexts;
result.numXmaContexts = ctx->numXmaContexts;
result.numPcmContexts = ctx->numPcmContexts;
try {
result.srcContextArray = static_cast<SHAPE_SRC_CONTEXT*>(
alloc_array(result.numSrcContexts, sizeof(SHAPE_SRC_CONTEXT)));
result.eqCompContextArray = static_cast<SHAPE_EQCOMP_CONTEXT*>(
alloc_array(result.numEqCompContexts, sizeof(SHAPE_EQCOMP_CONTEXT)));
result.filtVolContextArray = static_cast<SHAPE_FILTVOL_CONTEXT*>(
alloc_array(result.numFiltVolContexts, sizeof(SHAPE_FILTVOL_CONTEXT)));
result.dmaContextArray = static_cast<SHAPE_DMA_CONTEXT*>(
alloc_array(result.numDmaContexts, sizeof(SHAPE_DMA_CONTEXT)));
result.xmaContextArray = static_cast<SHAPE_XMA_CONTEXT*>(
alloc_array(result.numXmaContexts, sizeof(SHAPE_XMA_CONTEXT)));
result.pcmContextArray = static_cast<SHAPE_PCM_CONTEXT*>(
alloc_array(result.numPcmContexts, sizeof(SHAPE_PCM_CONTEXT)));
// Assign synthetic APU addresses to the host-side context arrays.
result.apuSrcContextArray = {result.srcContextArray ? kApuBaseAddress : 0};
result.apuEqCompContextArray = {result.eqCompContextArray ? kApuBaseAddress + 0x1000 : 0};
result.apuFiltVolContextArray = {result.filtVolContextArray ? kApuBaseAddress + 0x2000 : 0};
result.apuDmaContextArray = {result.dmaContextArray ? kApuBaseAddress + 0x3000 : 0};
result.apuXmaContextArray = {result.xmaContextArray ? kApuBaseAddress + 0x4000 : 0};
result.apuPcmContextArray = {result.pcmContextArray ? kApuBaseAddress + 0x5000 : 0};
g_shape_allocations.reserve(g_shape_allocations.size() + 1);
if (result.xmaContextArray) g_xma_engine.bind(result.xmaContextArray, result.numXmaContexts);
} catch (const std::bad_alloc&) {
return E_OUTOFMEMORY;
}
// Both the backing arrays and the engine's slots are now fully allocated.
g_shape_allocations.push_back(std::move(pending));
*ctx = result;
if (rt::startup_trace::enabled()) {
try {
trace_call("AcpHalAllocateShapeContexts",
",\"src\":" + std::to_string(result.numSrcContexts) +
",\"xma\":" + std::to_string(result.numXmaContexts) +
",\"pcm\":" + std::to_string(result.numPcmContexts));
} catch (const std::bad_alloc&) {
// The allocation succeeded. A missing diagnostic does not invalidate it.
}
}
return S_OK;
}
// ---- AcpHalReleaseShapeContexts ----
HRESULT __stdcall AcpHalReleaseShapeContexts(void* /*hal*/) {
std::lock_guard lock(g_shape_mutex);
// Stop and detach the XMA worker before freeing backing arrays.
g_xma_engine.unbind();
// The export releases the process-wide SHAPE arrays. The argument is an
// opaque HAL handle, and allocation descriptors are owned by the caller.
g_shape_allocations.clear();
trace_call("AcpHalReleaseShapeContexts", "");
return S_OK;
}
// ---- ApuCreateHeap ----
HRESULT __stdcall ApuCreateHeap(uint32_t cachedSizeInBytes,
uint32_t nonCachedSizeInBytes) {
std::lock_guard lock(g_apu_mutex);
if (g_apu_heap_created) return E_FAIL;
const uint64_t address_bytes = (nonCachedSizeInBytes ? align_apu_region(cachedSizeInBytes)
: cachedSizeInBytes) + nonCachedSizeInBytes;
if (address_bytes > static_cast<uint64_t>(UINT32_MAX) + 1 - kApuBaseAddress) return E_INVALIDARG;
if (cachedSizeInBytes > 0) {
g_cached_heap.base = VirtualAlloc(
nullptr, cachedSizeInBytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (!g_cached_heap.base) return E_OUTOFMEMORY;
g_cached_heap.size = cachedSizeInBytes;
}
if (nonCachedSizeInBytes > 0) {
g_noncached_heap.base = VirtualAlloc(
nullptr, nonCachedSizeInBytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (!g_noncached_heap.base) {
g_cached_heap.release();
return E_OUTOFMEMORY;
}
g_noncached_heap.size = nonCachedSizeInBytes;
}
g_apu_heap_created = true;
if (rt::startup_trace::enabled()) {
try {
trace_call("ApuCreateHeap",
",\"cachedSize\":" + std::to_string(cachedSizeInBytes) +
",\"nonCachedSize\":" + std::to_string(nonCachedSizeInBytes));
} catch (const std::bad_alloc&) {
// The heap is ready even if its diagnostic cannot be built.
}
}
return S_OK;
}
// ---- ApuAlloc ----
HRESULT __stdcall ApuAlloc(void** virtualAddress, uint32_t* physicalAddress,
uint32_t sizeInBytes, uint32_t alignmentInBytes,
uint32_t flags) try {
if (!virtualAddress) return E_POINTER;
*virtualAddress = nullptr;
if (physicalAddress) *physicalAddress = 0;
if (sizeInBytes == 0) return E_INVALIDARG;
if (alignmentInBytes == 0) alignmentInBytes = 1;
if ((alignmentInBytes & (alignmentInBytes - 1)) != 0) return E_INVALIDARG;
std::lock_guard lock(g_apu_mutex);
// Routing: flags=2 routes to non-cached heap; flags=0 or 1 routes to cached heap.
// If non-cached heap wasn't created or has no room, fall back to cached heap
// because PC memory is unified and cache-coherent.
const bool prefer_noncached = (flags == 2);
ApuRegion* target = prefer_noncached ? &g_noncached_heap : &g_cached_heap;
if (!target->base) {
if (!prefer_noncached) {
if (g_apu_heap_created) return E_OUTOFMEMORY;
// Lazy default cached heap if ApuCreateHeap wasn't called yet
constexpr uint32_t kDefaultHeapSize = 64 * 1024 * 1024;
target->base = VirtualAlloc(nullptr, kDefaultHeapSize,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (!target->base) return E_OUTOFMEMORY;
target->size = kDefaultHeapSize;
g_apu_heap_created = true;
} else {
// Fall back to cached heap
target = &g_cached_heap;
if (!target->base) return E_OUTOFMEMORY;
}
}
uint32_t offset = 0;
void* ptr = target->allocate(sizeInBytes, alignmentInBytes, &offset);
if (!ptr && prefer_noncached && g_cached_heap.base) {
// Fall back to cached heap if non-cached heap is full
target = &g_cached_heap;
ptr = target->allocate(sizeInBytes, alignmentInBytes, &offset);
}
if (!ptr) return E_OUTOFMEMORY;
*virtualAddress = ptr;
if (physicalAddress) {
// Leave alignment padding between the two synthetic address ranges.
uint32_t apu = kApuBaseAddress + offset;
if (target == &g_noncached_heap) {
apu += static_cast<uint32_t>(align_apu_region(g_cached_heap.size));
}
*physicalAddress = apu;
}
return S_OK;
} catch (const std::bad_alloc&) {
return E_OUTOFMEMORY;
}
// ---- ApuFree ----
void __stdcall ApuFree(void* virtualAddress) {
if (!virtualAddress) return;
std::lock_guard lock(g_apu_mutex);
if (!g_cached_heap.free_block(virtualAddress)) {
g_noncached_heap.free_block(virtualAddress);
}
trace_call("ApuFree", "");
}
// ---- ApuIsVirtualAddressValid ----
BOOL __stdcall ApuIsVirtualAddressValid(void* virtualAddress, uint32_t alignment) {
if (!virtualAddress) return FALSE;
if (alignment == 0) alignment = 1;
std::lock_guard lock(g_apu_mutex);
if (g_cached_heap.is_valid_address(virtualAddress, alignment)) return TRUE;
if (g_noncached_heap.is_valid_address(virtualAddress, alignment)) return TRUE;
return FALSE;
}
// ---- ApuMapApuAddress ----
void* __stdcall ApuMapApuAddress(uint32_t apu) {
if (apu == 0 || apu < kApuBaseAddress) return nullptr;
std::lock_guard lock(g_apu_mutex);
const uint32_t offset = apu - kApuBaseAddress;
if (g_cached_heap.base && offset < g_cached_heap.size) {
return g_cached_heap.from_offset(offset);
}
if (g_noncached_heap.base) {
const uint32_t cached_span = static_cast<uint32_t>(align_apu_region(g_cached_heap.size));
if (offset >= cached_span && offset - cached_span < g_noncached_heap.size) {
return g_noncached_heap.from_offset(offset - cached_span);
}
}
return nullptr;
}
// ---- ApuMapVirtualAddress ----
uint32_t __stdcall ApuMapVirtualAddress(void* virtualAddress) {
if (!virtualAddress) return 0;
std::lock_guard lock(g_apu_mutex);
if (g_cached_heap.contains(virtualAddress)) {
return kApuBaseAddress + g_cached_heap.to_offset(virtualAddress);
}
if (g_noncached_heap.contains(virtualAddress)) {
return kApuBaseAddress + static_cast<uint32_t>(align_apu_region(g_cached_heap.size)) +
g_noncached_heap.to_offset(virtualAddress);
}
return 0;
}
} // namespace
SHIM_TABLE(acphal,
{"acphal.dll", "AcpHalAllocateShapeContexts", reinterpret_cast<void*>(&AcpHalAllocateShapeContexts)},
{"acphal.dll", "AcpHalCreate", reinterpret_cast<void*>(&AcpHalCreate)},
{"acphal.dll", "AcpHalReleaseShapeContexts", reinterpret_cast<void*>(&AcpHalReleaseShapeContexts)},
{"acphal.dll", "ApuAlloc", reinterpret_cast<void*>(&ApuAlloc)},
{"acphal.dll", "ApuCreateHeap", reinterpret_cast<void*>(&ApuCreateHeap)},
{"acphal.dll", "ApuFree", reinterpret_cast<void*>(&ApuFree)},
{"acphal.dll", "ApuIsVirtualAddressValid", reinterpret_cast<void*>(&ApuIsVirtualAddressValid)},
{"acphal.dll", "ApuMapApuAddress", reinterpret_cast<void*>(&ApuMapApuAddress)},
{"acphal.dll", "ApuMapVirtualAddress", reinterpret_cast<void*>(&ApuMapVirtualAddress)},
);
|