-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspu.cpp
1060 lines (911 loc) · 35.8 KB
/
spu.cpp
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
/*
Copyright 2020 Hydr8gon
This file is part of NooDS.
NooDS is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NooDS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with NooDS. If not, see <https://www.gnu.org/licenses/>.
*/
#include <chrono>
#include <cstring>
#include "spu.h"
#include "core.h"
#include "settings.h"
const int Spu::indexTable[] =
{
-1, -1, -1, -1, 2, 4, 6, 8
};
const int16_t Spu::adpcmTable[] =
{
0x0007, 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x0010, 0x0011, 0x0013, 0x0015,
0x0017, 0x0019, 0x001C, 0x001F, 0x0022, 0x0025, 0x0029, 0x002D, 0x0032, 0x0037, 0x003C, 0x0042,
0x0049, 0x0050, 0x0058, 0x0061, 0x006B, 0x0076, 0x0082, 0x008F, 0x009D, 0x00AD, 0x00BE, 0x00D1,
0x00E6, 0x00FD, 0x0117, 0x0133, 0x0151, 0x0173, 0x0198, 0x01C1, 0x01EE, 0x0220, 0x0256, 0x0292,
0x02D4, 0x031C, 0x036C, 0x03C3, 0x0424, 0x048E, 0x0502, 0x0583, 0x0610, 0x06AB, 0x0756, 0x0812,
0x08E0, 0x09C3, 0x0ABD, 0x0BD0, 0x0CFF, 0x0E4C, 0x0FBA, 0x114C, 0x1307, 0x14EE, 0x1706, 0x1954,
0x1BDC, 0x1EA5, 0x21B6, 0x2515, 0x28CA, 0x2CDF, 0x315B, 0x364B, 0x3BB9, 0x41B2, 0x4844, 0x4F7E,
0x5771, 0x602F, 0x69CE, 0x7462, 0x7FFF
};
Spu::Spu(Core *core): core(core)
{
// Mark the buffer as not ready
ready = false;
}
Spu::~Spu()
{
// Free the buffers
//delete[] bufferIn;
//delete[] bufferOut;
}
uint32_t *Spu::getSamples(int count)
{
uint32_t *out = nullptr;
/*// Initialize the buffers
if (bufferSize != count)
{
delete[] bufferIn;
delete[] bufferOut;
bufferIn = new uint32_t[count];
bufferOut = new uint32_t[count];
bufferSize = count;
bufferPointer = 0;
}
bool wait;
// If FPS limit is enabled, try to wait until the buffer is filled
// If the emulation isn't full speed, waiting would starve the audio buffer
// So if it's taking too long, just let it play an empty buffer
if (Settings::getFpsLimiter() == 2) // Accurate
{
std::chrono::steady_clock::time_point waitTime = std::chrono::steady_clock::now();
wait = false;
// Use a while loop to constantly check if the wait condition has been satisfied
// This is wasteful, but ensures a swift break from the wait state
while (!ready.load())
{
if (std::chrono::steady_clock::now() - waitTime > std::chrono::microseconds(1000000 / 60))
{
wait = true;
break;
}
}
}
else // Disabled/Light
{
// Use a condition variable to save CPU cycles
// This might take longer than expected due to the OS scheduler and other factors
std::unique_lock<std::mutex> lock(mutex2);
wait = !cond2.wait_for(lock, std::chrono::microseconds(1000000 / 60), [&]{ return ready.load(); });
}
uint32_t *out = new uint32_t[count];
if (wait)
{
// Fill the output buffer with the last played sample to prevent crackles when running slow
for (int i = 0; i < count; i++)
out[i] = bufferOut[count - 1];
}
else
{
// Fill the output buffer with new data
memcpy(out, bufferOut, count * sizeof(uint32_t));
}
// Signal that the buffer was played
{
std::lock_guard<std::mutex> guard(mutex1);
ready.store(false);
cond1.notify_one();
}
*/
return out;
}
void Spu::runGbaSample()
{
/*int64_t sampleLeft = 0;
int64_t sampleRight = 0;
// Generate an audio sample
if (gbaMainSoundCntX & BIT(7))
{
int32_t data[4] = {};
// Run the tone channels
for (int i = 0; i < 2; i++)
{
if (!(gbaMainSoundCntX & BIT(i)))
continue;
// Run the frequency sweeper at 128Hz when enabled (first channel only)
if (i == 0 && gbaFrameSequencer % 256 == 128 && (gbaSoundCntL[i] & 0x70) && --gbaSweepTimer <= 0)
{
// Calculate the frequency change
uint16_t frequency = (gbaSoundCntX[i] & 0x07FF);
int sweep = frequency >> (gbaSoundCntL[i] & 0x07);
if (gbaSoundCntL[i] & BIT(3)) sweep = -sweep;
// Sweep the frequency
frequency += sweep;
if (frequency < 0x800)
{
// Set the new frequency and reload the sweep timer
gbaSoundCntX[i] = (gbaSoundCntX[i] & ~0x07FF) | frequency;
gbaSweepTimer = (gbaSoundCntL[i] & 0x70) >> 4;
}
else
{
// Disable the channel if the frequency is too high
gbaMainSoundCntX &= ~BIT(i);
continue;
}
}
// Decrement and reload the sound timer
gbaSoundTimers[i] -= 4;
while ((gbaSoundTimers[i]) <= 0)
gbaSoundTimers[i] += 2048 - (gbaSoundCntX[i] & 0x07FF);
// Determine the point in the duty cycle where the sample switches from low to high
int duty;
switch ((gbaSoundCntH[i] & 0x00C0) >> 6)
{
case 0: duty = (2048 - (gbaSoundCntX[i] & 0x07FF)) * 7 / 8; break;
case 1: duty = (2048 - (gbaSoundCntX[i] & 0x07FF)) * 6 / 8; break;
case 2: duty = (2048 - (gbaSoundCntX[i] & 0x07FF)) * 4 / 8; break;
case 3: duty = (2048 - (gbaSoundCntX[i] & 0x07FF)) * 2 / 8; break;
}
// Set the sample to low or high based on the position in the duty cycle
data[i] = (gbaSoundTimers[i] < duty) ? -0x80 : 0x80;
// Run the length counter at 256Hz when enabled
if (gbaFrameSequencer % 128 == 0 && (gbaSoundCntX[i] & BIT(14)) && (gbaSoundCntH[i] & 0x003F))
{
// Decrement the length counter
gbaSoundCntH[i] = (gbaSoundCntH[i] & ~0x003F) | ((gbaSoundCntH[i] & 0x003F) - 1);
// Disable the channel when the counter hits zero
if ((gbaSoundCntH[i] & 0x003F) == 0)
gbaMainSoundCntX &= ~BIT(i);
}
// Run the envelope timer at 64Hz
if (gbaFrameSequencer == 448 && --gbaEnvTimers[i] <= 0)
{
if (gbaEnvTimers[i] == 0)
{
// Adjust the envelope volume if the timer period was non-zero
if ((gbaSoundCntH[i] & BIT(11)) && gbaEnvelopes[i] < 15)
gbaEnvelopes[i]++;
else if (!(gbaSoundCntH[i] & BIT(11)) && gbaEnvelopes[i] > 0)
gbaEnvelopes[i]--;
}
else
{
// The envelope seems to reset with a period of zero?
gbaEnvelopes[i] = (gbaSoundCntH[i] & 0xF000) >> 12;
}
// Reload the envelope timer
gbaEnvTimers[i] = (gbaSoundCntH[i] & 0x0700) >> 8;
}
// Apply the envelope volume
data[i] = data[i] * gbaEnvelopes[i] / 15;
}
// Run the wave channel
if ((gbaMainSoundCntX & BIT(2)) && (gbaSoundCntL[1] & BIT(7)))
{
// Decrement and reload the sound timer
// Each reload increases the current wave digit
gbaSoundTimers[2] -= 64;
while ((gbaSoundTimers[2]) <= 0)
{
gbaSoundTimers[2] += (2048 - (gbaSoundCntX[2] & 0x07FF));
gbaWaveDigit = (gbaWaveDigit + 1) % 64;
}
// Determine which wave RAM bank to read from
// If the dimension is set to 2, samples from the other bank will play after the first 32 samples
int bank = (gbaSoundCntL[1] & BIT(6)) >> 6;
if ((gbaSoundCntL[1] & BIT(5)) && gbaWaveDigit >= 32)
bank = !bank;
// Read the current 4-bit sample from the wave RAM
data[2] = gbaWaveRam[bank][(gbaWaveDigit % 32) / 2];
if (gbaWaveDigit & 1)
data[2] &= 0x0F;
else
data[2] >>= 4;
// Run the length counter at 256Hz when enabled
if (gbaFrameSequencer % 128 == 0 && (gbaSoundCntX[2] & BIT(14)) && (gbaSoundCntH[2] & 0x00FF))
{
// Decrement the length counter
gbaSoundCntH[2] = (gbaSoundCntH[2] & ~0x00FF) | ((gbaSoundCntH[2] & 0x00FF) - 1);
// Disable the channel when the counter hits zero
if ((gbaSoundCntH[2] & 0x00FF) == 0)
gbaMainSoundCntX &= ~BIT(2);
}
// Apply volume
// If bit 15 is set, the volume shift is overridden and 75% is forced
switch ((gbaSoundCntH[2] & 0xE000) >> 13)
{
case 0: data[2] >>= 4; break;
case 1: data[2] >>= 0; break;
case 2: data[2] >>= 1; break;
case 3: data[2] >>= 2; break;
default: data[2] = data[2] * 3 / 4; break;
}
// Convert the sample to an 8-bit value
data[2] = (data[2] * 0x100 / 0xF);
}
// Run the noise channel
if (gbaMainSoundCntX & BIT(3))
{
// Decrement and reload the sound timer
// Each reload advances the random generator
gbaSoundTimers[3] -= 16;
while ((gbaSoundTimers[3]) <= 0)
{
int divisor = (gbaSoundCntX[3] & 0x0007) * 16;
if (divisor == 0) divisor = 8;
gbaSoundTimers[3] += (divisor << ((gbaSoundCntX[3] & 0x00F0) >> 4));
// Advance the random generator and save the carry bit to bit 15
gbaNoiseValue &= ~BIT(15);
if (gbaNoiseValue & BIT(0))
gbaNoiseValue = BIT(15) | ((gbaNoiseValue >> 1) ^ ((gbaSoundCntH[3] & BIT(3)) ? 0x60 : 0x6000));
else
gbaNoiseValue >>= 1;
}
// Set the sample to low or high based on the last carry bits
data[3] = (gbaNoiseValue & BIT(15)) ? 0x80 : -0x80;
// Run the length counter at 256Hz when enabled
if (gbaFrameSequencer % 128 == 0 && (gbaSoundCntX[3] & BIT(14)) && (gbaSoundCntH[3] & 0x003F))
{
// Decrement the length counter
gbaSoundCntH[3] = (gbaSoundCntH[3] & ~0x003F) | ((gbaSoundCntH[3] & 0x003F) - 1);
// Disable the channel when the counter hits zero
if ((gbaSoundCntH[3] & 0x003F) == 0)
gbaMainSoundCntX &= ~BIT(3);
}
// Run the envelope timer at 64Hz
if (gbaFrameSequencer == 448 && --gbaEnvTimers[2] <= 0)
{
if (gbaEnvTimers[2] == 0)
{
// Adjust the envelope volume if the timer period was non-zero
if ((gbaSoundCntH[3] & BIT(11)) && gbaEnvelopes[2] < 15)
gbaEnvelopes[2]++;
else if (!(gbaSoundCntH[3] & BIT(11)) && gbaEnvelopes[2] > 0)
gbaEnvelopes[2]--;
}
else
{
// The envelope seems to reset with a period of zero?
gbaEnvelopes[2] = (gbaSoundCntH[3] & 0xF000) >> 12;
}
// Reload the envelope timer
gbaEnvTimers[2] = (gbaSoundCntH[3] & 0x0700) >> 8;
}
// Apply the envelope volume
data[3] = data[3] * gbaEnvelopes[2] / 15;
}
// Mix the PSG channels
// The maximum volume is +/-0x80 per channel
for (int i = 0; i < 4; i++)
{
// Apply the DMA mixing volume
switch (gbaMainSoundCntH & 0x0003)
{
case 0: data[i] >>= 2; break;
case 1: data[i] >>= 1; break;
case 2: data[i] >>= 0; break;
}
// Add the data to the samples
if (gbaMainSoundCntL & BIT(12 + i))
sampleLeft += data[i] * ((gbaMainSoundCntL & 0x0070) >> 4) / 7;
if (gbaMainSoundCntL & BIT(8 + i))
sampleRight += data[i] * (gbaMainSoundCntL & 0x0007) / 7;
}
// Mix FIFO channel A
// The maximum volume is +/-0x200, achieved by shifting the data left by 2
if (gbaMainSoundCntH & BIT(9))
sampleLeft += gbaSampleA << ((gbaMainSoundCntH & BIT(2)) ? 2 : 1);
if (gbaMainSoundCntH & BIT(8))
sampleRight += gbaSampleA << ((gbaMainSoundCntH & BIT(2)) ? 2 : 1);
// Mix FIFO channel B
// The maximum volume is +/-0x200, achieved by shifting the data left by 2
if (gbaMainSoundCntH & BIT(13))
sampleLeft += gbaSampleB << ((gbaMainSoundCntH & BIT(3)) ? 2 : 1);
if (gbaMainSoundCntH & BIT(12))
sampleRight += gbaSampleB << ((gbaMainSoundCntH & BIT(3)) ? 2 : 1);
// Increment the frame sequencer
// The frame sequencer runs at 512Hz, and has 8 steps before repeating
// Audio is generated at 32768Hz, so every multiple of 64 is a new step
gbaFrameSequencer = (gbaFrameSequencer + 1) % 512;
}
// Apply the sound bias
sampleLeft += (gbaSoundBias & 0x03FF);
sampleRight += (gbaSoundBias & 0x03FF);
// Apply clipping
if (sampleLeft < 0x000) sampleLeft = 0x000;
if (sampleLeft > 0x3FF) sampleLeft = 0x3FF;
if (sampleRight < 0x000) sampleRight = 0x000;
if (sampleRight > 0x3FF) sampleRight = 0x3FF;
// Expand the samples to signed 16-bit values and return them
sampleLeft = (sampleLeft - 0x200) << 5;
sampleRight = (sampleRight - 0x200) << 5;
if (bufferSize == 0) return;
// Write the samples to the buffer
bufferIn[bufferPointer++] = (sampleRight << 16) | (sampleLeft & 0xFFFF);
// Handle a full buffer
if (bufferPointer == bufferSize)
swapBuffers();*/
}
void Spu::runSample()
{
/* int64_t mixerLeft = 0, mixerRight = 0;
int64_t channelsLeft[2] = {}, channelsRight[2] = {};
// Mix the sound channels
for (int i = 0; i < 16; i++)
{
// Skip disabled channels
if (!(enabled & BIT(i)))
continue;
int format = (soundCnt[i] & 0x60000000) >> 29;
int64_t data = 0;
// Read the sample data
switch (format)
{
case 0: // PCM8
{
data = core->memory.read<int8_t>(1, soundCurrent[i]) << 8;
break;
}
case 1: // PCM16
{
data = core->memory.read<int16_t>(1, soundCurrent[i]);
break;
}
case 2: // ADPCM
{
data = adpcmValue[i];
break;
}
case 3: // Pulse/Noise
{
if (i >= 8 && i <= 13) // Pulse waves
{
// Set the sample to low or high depending on the position in the duty cycle
int duty = 7 - ((soundCnt[i] & 0x07000000) >> 24);
data = (dutyCycles[i - 8] < duty) ? -0x7FFF : 0x7FFF;
}
else if (i >= 14) // Noise
{
// Set the sample to low or high depending on the carry bit (saved as bit 15)
data = (noiseValues[i - 14] & BIT(15)) ? -0x7FFF : 0x7FFF;
}
break;
}
}
// Increment the timer for the length of a sample
// The SPU runs at 16756991Hz with a sample rate of 32768Hz
// 16756991 / 32768 = ~512 cycles per sample
soundTimers[i] += 512;
bool overflow = (soundTimers[i] < 512);
// Handle timer overflow
while (overflow)
{
// Reload the timer
soundTimers[i] += soundTmr[i];
overflow = (soundTimers[i] < soundTmr[i]);
switch (format)
{
case 0: case 1: // PCM8/PCM16
{
// Increment the data pointer by the size of one sample
soundCurrent[i] += 1 + format;
break;
}
case 2: // ADPCM
{
// Get the 4-bit ADPCM data
uint8_t adpcmData = core->memory.read<uint8_t>(1, soundCurrent[i]);
adpcmData = adpcmToggle[i] ? ((adpcmData & 0xF0) >> 4) : (adpcmData & 0x0F);
// Calculate the sample difference
int32_t diff = adpcmTable[adpcmIndex[i]] / 8;
if (adpcmData & BIT(0)) diff += adpcmTable[adpcmIndex[i]] / 4;
if (adpcmData & BIT(1)) diff += adpcmTable[adpcmIndex[i]] / 2;
if (adpcmData & BIT(2)) diff += adpcmTable[adpcmIndex[i]] / 1;
// Apply the sample difference to the sample
if (adpcmData & BIT(3))
{
adpcmValue[i] += diff;
if (adpcmValue[i] > 0x7FFF) adpcmValue[i] = 0x7FFF;
}
else
{
adpcmValue[i] -= diff;
if (adpcmValue[i] < -0x7FFF) adpcmValue[i] = -0x7FFF;
}
// Calculate the next index
adpcmIndex[i] += indexTable[adpcmData & 0x7];
if (adpcmIndex[i] < 0) adpcmIndex[i] = 0;
if (adpcmIndex[i] > 88) adpcmIndex[i] = 88;
// Move to the next 4-bit ADPCM data
adpcmToggle[i] = !adpcmToggle[i];
if (!adpcmToggle[i]) soundCurrent[i]++;
// Save the ADPCM values at the loop position
if (soundCurrent[i] == soundSad[i] + soundPnt[i] * 4 && !adpcmToggle[i])
{
adpcmLoopValue[i] = adpcmValue[i];
adpcmLoopIndex[i] = adpcmIndex[i];
}
break;
}
case 3: // Pulse/Noise
{
if (i >= 8 && i <= 13) // Pulse waves
{
// Increment the duty cycle counter
dutyCycles[i - 8] = (dutyCycles[i - 8] + 1) % 8;
}
else if (i >= 14) // Noise
{
// Clear the previous saved carry bit
noiseValues[i - 14] &= ~BIT(15);
// Advance the random generator and save the carry bit to bit 15
if (noiseValues[i - 14] & BIT(0))
noiseValues[i - 14] = BIT(15) | ((noiseValues[i - 14] >> 1) ^ 0x6000);
else
noiseValues[i - 14] >>= 1;
}
break;
}
}
// Repeat or end the sound if the end of the data is reached
if (format != 3 && soundCurrent[i] >= soundSad[i] + (soundPnt[i] + soundLen[i]) * 4)
{
if ((soundCnt[i] & 0x18000000) >> 27 == 1) // Loop infinite
{
soundCurrent[i] = soundSad[i] + soundPnt[i] * 4;
// Restore the ADPCM values from the loop position
if (format == 2)
{
adpcmValue[i] = adpcmLoopValue[i];
adpcmIndex[i] = adpcmLoopIndex[i];
adpcmToggle[i] = false;
}
}
else // One-shot
{
soundCnt[i] &= ~BIT(31);
enabled &= ~BIT(i);
break;
}
}
}
// Apply the volume divider
// The sample now has 4 fractional bits
int divShift = (soundCnt[i] & 0x00000300) >> 8;
if (divShift == 3) divShift++;
data <<= 4 - divShift;
// Apply the volume factor
// The sample now has 11 fractional bits
int mulFactor = (soundCnt[i] & 0x0000007F);
if (mulFactor == 127) mulFactor++;
data = (data << 7) * mulFactor / 128;
// Apply panning
// The samples are now rounded to 8 fractional bits
int panValue = (soundCnt[i] & 0x007F0000) >> 16;
if (panValue == 127) panValue++;
int64_t dataLeft = (data * (128 - panValue) / 128) >> 3;
int64_t dataRight = (data * panValue / 128) >> 3;
// Redirect channels 1 and 3 if enabled
if (i == 1 || i == 3)
{
channelsLeft[i >> 1] = dataLeft;
channelsRight[i >> 1] = dataRight;
if (mainSoundCnt & BIT(12 + (i >> 1)))
continue;
}
// Add the channel to the mixer
mixerLeft += dataLeft;
mixerRight += dataRight;
}
// Capture sound
for (int i = 0; i < 2; i++)
{
// Skip disabled capture channels
if (!(sndCapCnt[i] & BIT(7)))
continue;
// Increment the timer for the length of a sample
sndCapTimers[i] += 512;
bool overflow = (sndCapTimers[i] < 512);
// Handle timer overflow
while (overflow)
{
// Reload the timer
sndCapTimers[i] += soundTmr[1 + (i << 1)];
overflow = (sndCapTimers[i] < soundTmr[1 + (i << 1)]);
// Write a sample to the buffer
int64_t sample = ((i == 0) ? mixerLeft : mixerRight);
if (sndCapCnt[i] & BIT(3)) // PCM8
{
core->memory.write<uint8_t>(1, sndCapCurrent[i], sample >> 16);
sndCapCurrent[i]++;
}
else // PCM16
{
core->memory.write<uint16_t>(1, sndCapCurrent[i], sample >> 8);
sndCapCurrent[i] += 2;
}
// Repeat or end the capture if the end of the buffer is reached
if (sndCapCurrent[i] >= sndCapDad[i] + sndCapLen[i] * 4)
{
if (sndCapCnt[i] & BIT(2)) // One-shot
{
sndCapCnt[i] &= ~BIT(7);
continue;
}
else // Loop
{
sndCapCurrent[i] = sndCapDad[i];
}
}
}
}
// Get the left output sample
int64_t sampleLeft;
switch ((mainSoundCnt & 0x0300) >> 8) // Left output selection
{
case 0: sampleLeft = mixerLeft; break; // Mixer
case 1: sampleLeft = channelsLeft[0]; break; // Channel 1
case 2: sampleLeft = channelsLeft[1]; break; // Channel 3
case 3: sampleLeft = channelsLeft[0] + channelsLeft[1]; break; // Channel 1 + 3
}
// Get the right output sample
int64_t sampleRight;
switch ((mainSoundCnt & 0x0C00) >> 10) // Right output selection
{
case 0: sampleRight = mixerRight; break; // Mixer
case 1: sampleRight = channelsRight[0]; break; // Channel 1
case 2: sampleRight = channelsRight[1]; break; // Channel 3
case 3: sampleRight = channelsRight[0] + channelsRight[1]; break; // Channel 1 + 3
}
// Apply the master volume
// The samples are now rounded to no fractional bits
int masterVol = (mainSoundCnt & 0x007F);
if (masterVol == 127) masterVol++;
sampleLeft = (sampleLeft * masterVol / 128) >> 8;
sampleRight = (sampleRight * masterVol / 128) >> 8;
// Convert to 10-bit and apply the sound bias
sampleLeft = (sampleLeft >> 6) + soundBias;
sampleRight = (sampleRight >> 6) + soundBias;
// Apply clipping
if (sampleLeft < 0x000) sampleLeft = 0x000;
if (sampleLeft > 0x3FF) sampleLeft = 0x3FF;
if (sampleRight < 0x000) sampleRight = 0x000;
if (sampleRight > 0x3FF) sampleRight = 0x3FF;
// Expand the samples to signed 16-bit values and return them
sampleLeft = (sampleLeft - 0x200) << 5;
sampleRight = (sampleRight - 0x200) << 5;
if (bufferSize == 0) return;
// Write the samples to the buffer
bufferIn[bufferPointer++] = (sampleRight << 16) | (sampleLeft & 0xFFFF);
// Handle a full buffer
if (bufferPointer == bufferSize)
swapBuffers();*/
}
void Spu::swapBuffers()
{
// Wait until the buffer has been played, keeping the emulator throttled to 60 FPS
// Synchronizing to the audio eliminites the potential for nasty audio crackles
/*if (Settings::getFpsLimiter() == 2) // Accurate
{
std::chrono::steady_clock::time_point waitTime = std::chrono::steady_clock::now();
while (ready.load() && std::chrono::steady_clock::now() - waitTime <= std::chrono::microseconds(1000000));
}
else if (Settings::getFpsLimiter() == 1) // Light
{
std::unique_lock<std::mutex> lock(mutex1);
cond1.wait_for(lock, std::chrono::microseconds(1000000), [&]{ return !ready.load(); });
}
// Swap the buffers
SWAP(bufferOut, bufferIn);
// Signal that the buffer is ready to play
{
std::lock_guard<std::mutex> guard(mutex2);
ready.store(true);
cond2.notify_one();
}
// Reset the buffer pointer
bufferPointer = 0;*/
}
void Spu::startChannel(int channel)
{
// Reload the channel's internal registers
/*soundCurrent[channel] = soundSad[channel];
soundTimers[channel] = soundTmr[channel];
switch ((soundCnt[channel] & 0x60000000) >> 29) // Format
{
case 2: // ADPCM
{
// Read the ADPCM header
uint32_t header = core->memory.read<uint32_t>(1, soundSad[channel]);
adpcmValue[channel] = (int16_t)header;
adpcmIndex[channel] = (header & 0x007F0000) >> 16;
if (adpcmIndex[channel] > 88) adpcmIndex[channel] = 88;
adpcmToggle[channel] = false;
soundCurrent[channel] += 4;
break;
}
case 3: // Pulse/Noise
{
// Reset the pulse or noise values
if (channel >= 8 && channel <= 13) // Pulse waves
dutyCycles[channel - 8] = 0;
else if (channel >= 14) // Noise
noiseValues[channel - 14] = 0x7FFF;
break;
}
}
// Enable the channel
enabled |= BIT(channel);*/
}
void Spu::gbaFifoTimer(int timer)
{
/*if (((gbaMainSoundCntH & BIT(10)) >> 10) == timer) // FIFO A
{
// Get a new sample
if (!gbaFifoA.empty())
{
gbaSampleA = gbaFifoA.front();
gbaFifoA.pop();
}
// Request more data from the DMA if half empty
if (gbaFifoA.size() <= 16)
core->dma[1].trigger(3, 0x02);
}
if (((gbaMainSoundCntH & BIT(14)) >> 14) == timer) // FIFO B
{
// Get a new sample
if (!gbaFifoB.empty())
{
gbaSampleB = gbaFifoB.front();
gbaFifoB.pop();
}
// Request more data from the DMA if half empty
if (gbaFifoB.size() <= 16)
core->dma[1].trigger(3, 0x04);
}*/
}
void Spu::writeGbaSoundCntL(int channel, uint8_t value)
{
/*if (!(gbaMainSoundCntX & BIT(7))) return;
// Write to one of the GBA SOUNDCNT_L registers
uint8_t mask = (channel == 0) ? 0x7F : 0xE0;
gbaSoundCntL[channel / 2] = (gbaSoundCntL[channel / 2] & ~mask) | (value & mask);*/
}
void Spu::writeGbaSoundCntH(int channel, uint16_t mask, uint16_t value)
{
/*if (!(gbaMainSoundCntX & BIT(7))) return;
// Write to one of the GBA SOUNDCNT_H registers
switch (channel)
{
case 2: mask &= 0xE0FF; break;
case 3: mask &= 0xFF3F; break;
}
gbaSoundCntH[channel] = (gbaSoundCntH[channel] & ~mask) | (value & mask);*/
}
void Spu::writeGbaSoundCntX(int channel, uint16_t mask, uint16_t value)
{
/*if (!(gbaMainSoundCntX & BIT(7))) return;
// Write to one of the GBA SOUNDCNT_X registers
mask &= (channel == 3) ? 0x40FF : 0x47FF;
gbaSoundCntX[channel] = (gbaSoundCntX[channel] & ~mask) | (value & mask);
// Restart the channel
if ((value & BIT(15)))
{
if (channel < 2) // Tone
{
if (channel == 0) gbaSweepTimer = (gbaSoundCntL[0] & 0x70) >> 4;
gbaEnvelopes[channel] = (gbaSoundCntH[channel] & 0xF000) >> 12;
gbaEnvTimers[channel] = (gbaSoundCntH[channel] & 0x0700) >> 8;
gbaSoundTimers[channel] = 2048 - (gbaSoundCntX[channel] & 0x07FF);
}
else if (channel == 2) // Wave
{
gbaWaveDigit = 0;
gbaSoundTimers[2] = 2048 - (gbaSoundCntX[2] & 0x07FF);
}
else // Noise
{
gbaNoiseValue = (gbaSoundCntH[3] & BIT(3)) ? 0x40 : 0x4000;
gbaEnvelopes[2] = (gbaSoundCntH[3] & 0xF000) >> 12;
gbaEnvTimers[2] = (gbaSoundCntH[3] & 0x0700) >> 8;
int divisor = (gbaSoundCntX[3] & 0x0007) * 16;
if (divisor == 0) divisor = 8;
gbaSoundTimers[3] = (divisor << ((gbaSoundCntX[3] & 0x00F0) >> 4));
}
gbaMainSoundCntX |= BIT(channel);
}*/
}
void Spu::writeGbaMainSoundCntL(uint16_t mask, uint16_t value)
{
/*if (!(gbaMainSoundCntX & BIT(7))) return;
// Write to the main GBA SOUNDCNT_L register
mask &= 0xFF77;
gbaMainSoundCntL = (gbaMainSoundCntL & ~mask) | (value & mask);*/
}
void Spu::writeGbaMainSoundCntH(uint16_t mask, uint16_t value)
{
// Write to the main GBA SOUNDCNT_H register
/*mask &= 0x770F;
gbaMainSoundCntH = (gbaMainSoundCntH & ~mask) | (value & mask);
// Empty FIFO A if requested
if (value & BIT(11))
{
while (!gbaFifoA.empty())
gbaFifoA.pop();
}
// Empty FIFO B if requested
if (value & BIT(15))
{
while (!gbaFifoB.empty())
gbaFifoB.pop();
}*/
}
void Spu::writeGbaMainSoundCntX(uint8_t value)
{
// Write to the main GBA SOUNDCNT_X register
/*gbaMainSoundCntX = (gbaMainSoundCntX & ~0x80) | (value & 0x80);
// Reset the PSG channels when disabled
if (!(gbaMainSoundCntX & BIT(7)))
{
for (int i = 0; i < 4; i++)
{
if (i < 2) gbaSoundCntL[i] = 0;
gbaSoundCntH[i] = 0;
gbaSoundCntX[i] = 0;
}
gbaMainSoundCntL = 0;
gbaMainSoundCntX &= ~0x0F;
gbaFrameSequencer = 0;
}*/
}
void Spu::writeGbaSoundBias(uint16_t mask, uint16_t value)
{
// Write to the GBA SOUNDBIAS register
//mask &= 0xC3FE;
//gbaSoundBias = (gbaSoundBias & ~mask) | (value & mask);
}
void Spu::writeGbaWaveRam(int index, uint8_t value)
{
// Write to the currently inactive GBA wave RAM bank
//gbaWaveRam[!(gbaSoundCntL[1] & BIT(6))][index] = value;
}
void Spu::writeGbaFifoA(uint32_t mask, uint32_t value)
{
// Push PCM8 data to the GBA sound FIFO A
/*for (int i = 0; i < 32; i += 8)
{
if (gbaFifoA.size() < 32 && (mask & (0xFF << i)))
gbaFifoA.push(value >> i);
}*/
}
void Spu::writeGbaFifoB(uint32_t mask, uint32_t value)
{
// Push PCM8 data to the GBA sound FIFO B
/*for (int i = 0; i < 32; i += 8)
{
if (gbaFifoB.size() < 32 && (mask & (0xFF << i)))
gbaFifoB.push(value >> i);
}*/
}
void Spu::writeSoundCnt(int channel, uint32_t mask, uint32_t value)
{
/*bool enable = (!(soundCnt[channel] & BIT(31)) && (value & BIT(31)));
// Write to one of the SOUNDCNT registers
mask &= 0xFF7F837F;
soundCnt[channel] = (soundCnt[channel] & ~mask) | (value & mask);
// Start the channel if the enable bit changes from 0 to 1 and the other conditions are met
if (enable && (mainSoundCnt & BIT(15)) && (soundSad[channel] != 0 || ((soundCnt[channel] & 0x60000000) >> 29) == 3))
startChannel(channel);
else if (!(soundCnt[channel] & BIT(31)))
enabled &= ~BIT(channel);*/
}
void Spu::writeSoundSad(int channel, uint32_t mask, uint32_t value)
{
// Write to one of the SOUNDSAD registers
/*mask &= 0x07FFFFFC;
soundSad[channel] = (soundSad[channel] & ~mask) | (value & mask);
// Restart the channel if the source address is valid and the other conditions are met
if (((soundCnt[channel] & 0x60000000) >> 29) != 3) // Not pulse/noise
{
if (soundSad[channel] != 0 && (mainSoundCnt & BIT(15)) && (soundCnt[channel] & BIT(31)))
startChannel(channel);
else
enabled &= ~BIT(channel);
}*/
}
void Spu::writeSoundTmr(int channel, uint16_t mask, uint16_t value)
{
// Write to one of the SOUNDTMR registers
//soundTmr[channel] = (soundTmr[channel] & ~mask) | (value & mask);
}
void Spu::writeSoundPnt(int channel, uint16_t mask, uint16_t value)
{
// Write to one of the SOUNDPNT registers
//soundPnt[channel] = (soundPnt[channel] & ~mask) | (value & mask);
}
void Spu::writeSoundLen(int channel, uint32_t mask, uint32_t value)
{
// Write to one of the SOUNDLEN registers
/*mask &= 0x003FFFFF;
soundLen[channel] = (soundLen[channel] & ~mask) | (value & mask);*/
}
void Spu::writeMainSoundCnt(uint16_t mask, uint16_t value)
{
/*bool enable = (!(mainSoundCnt & BIT(15)) && (value & BIT(15)));
// Write to the main SOUNDCNT register
mask &= 0xBF7F;
mainSoundCnt = (mainSoundCnt & ~mask) | (value & mask);
if (enable)
{
// Start the channels if the enable bit changes from 0 to 1 and the other conditions are met
for (int i = 0; i < 16; i++)
{
if ((soundCnt[i] & BIT(31)) && (soundSad[i] != 0 || ((soundCnt[i] & 0x60000000) >> 29) == 3))
startChannel(i);
}
}
else if (!(mainSoundCnt & BIT(15)))
{
// Disable all channels if the master enable is turned off
enabled = 0;
}*/
}
void Spu::writeSoundBias(uint16_t mask, uint16_t value)
{
// Write to the SOUNDBIAS register