-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
sblaster.cpp
3418 lines (2866 loc) · 88.5 KB
/
sblaster.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 (C) 2019-2024 The DOSBox Staging Team
* Copyright (C) 2002-2021 The DOSBox Team
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <array>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <map>
#include <optional>
#include <string>
#include <tuple>
#include "autoexec.h"
#include "bios.h"
#include "bit_view.h"
#include "channel_names.h"
#include "control.h"
#include "dma.h"
#include "hardware.h"
#include "inout.h"
#include "math_utils.h"
#include "midi.h"
#include "mixer.h"
#include "pic.h"
#include "setup.h"
#include "shell.h"
#include "string_utils.h"
#include "support.h"
constexpr uint8_t MixerIndex = 0x04;
constexpr uint8_t MixerData = 0x05;
constexpr uint8_t DspReset = 0x06;
constexpr uint8_t DspReadData = 0x0A;
constexpr uint8_t DspWriteData = 0x0C;
constexpr uint8_t DspWriteStatus = 0x0C;
constexpr uint8_t DspReadStatus = 0x0E;
constexpr uint8_t DspAck16Bit = 0x0f;
constexpr uint8_t DspNoCommand = 0;
constexpr uint16_t DmaBufSize = 1024;
constexpr uint8_t DspBufSize = 64;
constexpr uint16_t DspDacSize = 512;
constexpr uint8_t SbShift = 14;
constexpr uint16_t SbShiftMask = ((1 << SbShift) - 1);
constexpr uint8_t MinAdaptiveStepSize = 0; // max is 32767
// It was common for games perform some initial checks
// and resets on startup, resulting a rapid susccession of resets.
constexpr uint8_t DspInitialResetLimit = 4;
constexpr auto NativeDacRateHz = 45454;
constexpr uint16_t DefaultPlaybackRateHz = 22050;
enum class DspState {
Reset, ResetWait, Normal, HighSpeed
};
enum class SbType {
None = 0,
SB1 = 1,
SBPro1 = 2,
SB2 = 3,
SBPro2 = 4,
SB16 = 6,
GameBlaster = 7
};
enum class FilterType { None, SB1, SB2, SBPro1, SBPro2, SB16, Modern };
enum class SbIrq { Irq8, Irq16, IrqMpu };
enum class DspMode { None, Dac, Dma, DmaPause, DmaMasked };
enum class DmaMode {
None,
Adpcm2Bit,
Adpcm3Bit,
Adpcm4Bit,
Pcm8Bit,
Pcm16Bit,
Pcm16BitAliased
};
enum class EssType { None, Es1688 };
struct SbInfo {
uint32_t freq_hz = 0;
struct {
bool stereo = false;
bool sign = false;
bool autoinit = false;
DmaMode mode = {};
uint32_t rate = 0; // sample rate
uint32_t mul = 0; // samples-per-millisecond multipler
uint32_t singlesize = 0; // size for single cycle transfers
uint32_t autosize = 0; // size for auto init transfers
uint32_t left = 0; // Left in active cycle
uint32_t min = 0;
union {
uint8_t b8[DmaBufSize];
int16_t b16[DmaBufSize];
} buf = {};
uint32_t bits = 0;
DmaChannel* chan = nullptr;
uint32_t remain_size = 0;
} dma = {};
bool speaker_enabled = false;
bool midi_enabled = false;
uint8_t time_constant = 0;
DspMode mode = DspMode::None;
SbType type = SbType::None;
// ESS chipset emulation, to be set only for SbType::SBPro2
EssType ess_type = EssType::None;
FilterType sb_filter_type = FilterType::None;
FilterType opl_filter_type = FilterType::None;
FilterState sb_filter_state = FilterState::Off;
struct {
bool pending_8bit;
bool pending_16bit;
} irq = {};
struct {
DspState state = {};
uint8_t cmd = 0;
uint8_t cmd_len = 0;
uint8_t cmd_in_pos = 0;
uint8_t cmd_in[DspBufSize] = {};
struct {
// Last values added to the fifo
uint8_t lastval = 0;
uint8_t data[DspBufSize] = {};
// Index of current entry
uint8_t pos = 0;
// Number of entries in the fifo
uint8_t used = 0;
} in = {}, out = {};
uint8_t test_register = 0;
uint8_t write_status_counter = 0;
uint32_t reset_tally = 0;
int cold_warmup_ms = 0;
int hot_warmup_ms = 0;
int warmup_remaining_ms = 0;
} dsp = {};
struct {
int16_t data[DspDacSize + 1] = {};
// Number of entries in the DAC
uint16_t used = 0;
// Index of current entry
int16_t last = 0;
} dac = {};
struct {
uint8_t index = 0;
uint8_t dac[2] = {};
uint8_t fm[2] = {};
uint8_t cda[2] = {};
uint8_t master[2] = {};
uint8_t lin[2] = {};
uint8_t mic = 0;
bool stereo = false;
bool enabled = false;
bool filtered = false;
uint8_t unhandled[0x48] = {};
uint8_t ess_id_str[4] = {};
uint8_t ess_id_str_pos = {};
} mixer = {};
struct {
uint8_t reference = 0;
uint16_t stepsize = 0;
bool haveref = false;
} adpcm = {};
struct {
uint16_t base = 0;
uint8_t irq = 0;
uint8_t dma8 = 0;
uint8_t dma16 = 0;
} hw = {};
struct {
int value = 0;
uint32_t count = 0;
} e2 = {};
MixerChannelPtr chan = nullptr;
};
static SbInfo sb = {};
// clang-format off
// Number of bytes in input for commands (sb/sbpro)
static uint8_t dsp_cmd_len_sb[256] = {
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x00
// 1,0,0,0, 2,0,2,2, 0,0,0,0, 0,0,0,0, // 0x10
1,0,0,0, 2,2,2,2, 0,0,0,0, 0,0,0,0, // 0x10 Wari hack
0,0,0,0, 2,0,0,0, 0,0,0,0, 0,0,0,0, // 0x20
0,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0, // 0x30
1,2,2,0, 0,0,0,0, 2,0,0,0, 0,0,0,0, // 0x40
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x50
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x60
0,0,0,0, 2,2,2,2, 0,0,0,0, 0,0,0,0, // 0x70
2,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x80
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x90
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xa0
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xb0
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xc0
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xd0
1,0,1,0, 1,0,0,0, 0,0,0,0, 0,0,0,0, // 0xe0
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 // 0xf0
};
// Number of bytes in input for commands (sb16)
static uint8_t dsp_cmd_len_sb16[256] = {
0,0,0,0, 1,2,0,0, 1,0,0,0, 0,0,2,1, // 0x00
// 1,0,0,0, 2,0,2,2, 0,0,0,0, 0,0,0,0, // 0x10
1,0,0,0, 2,2,2,2, 0,0,0,0, 0,0,0,0, // 0x10 Wari hack
0,0,0,0, 2,0,0,0, 0,0,0,0, 0,0,0,0, // 0x20
0,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0, // 0x30
1,2,2,0, 0,0,0,0, 2,0,0,0, 0,0,0,0, // 0x40
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x50
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x60
0,0,0,0, 2,2,2,2, 0,0,0,0, 0,0,0,0, // 0x70
2,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x80
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x90
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xa0
3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0xb0
3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0xc0
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xd0
1,0,1,0, 1,0,0,0, 0,0,0,0, 0,0,0,0, // 0xe0
0,0,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0 // 0xf0
};
// clang-format on
static uint8_t asp_regs[256];
static bool asp_init_in_progress = false;
static int e2_incr_table[4][9] = {
{ 0x01, -0x02, -0x04, 0x08, -0x10, 0x20, 0x40, -0x80, -106},
{-0x01, 0x02, -0x04, 0x08, 0x10, -0x20, 0x40, -0x80, 165},
{-0x01, 0x02, 0x04, -0x08, 0x10, -0x20, -0x40, 0x80, -151},
{ 0x01, -0x02, 0x04, -0x08, -0x10, 0x20, -0x40, 0x80, 90}
};
static const char* sb_log_prefix()
{
switch (sb.type) {
case SbType::SB1: return "SB1";
case SbType::SB2: return "SB2";
case SbType::SBPro1: return "SBPRO1";
case SbType::SBPro2: return "SBPRO2";
case SbType::SB16: return "SB16";
case SbType::GameBlaster: return "GB";
case SbType::None:
assertm(false, "Should not use SbType::None as a log prefix");
return "SB";
default:
assertm(false,
format_str("Invalid SbType value: %d",
static_cast<int>(sb.type)));
return "";
}
}
static void dsp_change_mode(const DspMode mode);
static void flush_remainig_dma_transfer();
static void suppress_dma_transfer(const uint32_t size);
static void play_dma_transfer(const uint32_t size);
typedef void (*process_dma_f)(uint32_t);
static process_dma_f ProcessDMATransfer;
static void dsp_enable_speaker(const bool enabled)
{
// Speaker output is always enabled on the SB16 and ESS cards; speaker
// enable/disable commands are simply ignored. Only the SB Pro and
// earlier models can toggle the speaker-output via speaker
// enable/disable commands.
if (sb.type == SbType::SB16 || sb.ess_type != EssType::None) {
return;
}
// Speaker-output is already in the requested state
if (sb.speaker_enabled == enabled) {
return;
}
// If the speaker's being turned on, then flush old
// content before releasing the channel for playback.
if (enabled) {
PIC_RemoveEvents(suppress_dma_transfer);
flush_remainig_dma_transfer();
// Speaker powered-on after cold-state, give it warmup time
sb.dsp.warmup_remaining_ms = sb.dsp.cold_warmup_ms;
}
sb.chan->Enable(enabled);
sb.speaker_enabled = enabled;
#if 0
// This can be very noisy as some games toggle the speaker for every effect
LOG_MSG("%s: Speaker-output has been toggled %s",
sb_log_prefix(),
(enabled ? "on" : "off"));
#endif
}
static void init_speaker_state()
{
if (sb.type == SbType::SB16 || sb.ess_type != EssType::None) {
// Speaker output (DAC output) is always enabled on the SB16 and ESS
// cards. Because the channel is active, we treat this as a startup
// event.
const bool is_cold_start = sb.dsp.reset_tally <=
DspInitialResetLimit;
sb.dsp.warmup_remaining_ms = is_cold_start ? sb.dsp.cold_warmup_ms
: sb.dsp.hot_warmup_ms;
sb.speaker_enabled = true;
} else {
// SB Pro and earlier models have the speaker-output disabled by
// default.
sb.speaker_enabled = false;
}
sb.chan->Enable(sb.speaker_enabled);
}
static void log_filter_config(const char* channel_name, const char* output_type,
const FilterType filter)
{
// clang-format off
static const std::map<FilterType, std::string> filter_name_map = {
{FilterType::SB1, "Sound Blaster 1.0"},
{FilterType::SB2, "Sound Blaster 2.0"},
{FilterType::SBPro1, "Sound Blaster Pro 1"},
{FilterType::SBPro2, "Sound Blaster Pro 2"},
{FilterType::SB16, "Sound Blaster 16"},
{FilterType::Modern, "Modern"},
};
// clang-format on
if (filter == FilterType::None) {
LOG_MSG("%s: %s filter disabled", channel_name, output_type);
} else {
auto it = filter_name_map.find(filter);
if (it != filter_name_map.end()) {
auto filter_type = it->second;
LOG_MSG("%s: %s %s output filter enabled",
channel_name,
filter_type.c_str(),
output_type);
}
}
}
struct FilterConfig {
FilterState hpf_state = FilterState::Off;
uint8_t hpf_order = {};
uint16_t hpf_cutoff_freq_hz = {};
FilterState lpf_state = FilterState::Off;
uint8_t lpf_order = {};
uint16_t lpf_cutoff_freq_hz = {};
ResampleMethod resample_method = {};
uint16_t zoh_rate_hz = {};
};
static void set_filter(MixerChannelPtr channel, const FilterConfig& config)
{
if (config.hpf_state == FilterState::On ||
config.hpf_state == FilterState::ForcedOn) {
channel->ConfigureHighPassFilter(config.hpf_order,
config.hpf_cutoff_freq_hz);
}
channel->SetHighPassFilter(config.hpf_state);
if (config.lpf_state == FilterState::On ||
config.lpf_state == FilterState::ForcedOn) {
channel->ConfigureLowPassFilter(config.lpf_order,
config.lpf_cutoff_freq_hz);
}
channel->SetLowPassFilter(config.lpf_state);
if (config.resample_method == ResampleMethod::ZeroOrderHoldAndResample) {
channel->SetZeroOrderHoldUpsamplerTargetRate(config.zoh_rate_hz);
}
channel->SetResampleMethod(config.resample_method);
}
static std::optional<FilterType> determine_filter_type(const std::string& filter_choice,
const SbType sb_type)
{
if (filter_choice == "auto") {
// clang-format off
switch (sb_type) {
case SbType::None: return FilterType::None; break;
case SbType::SB1: return FilterType::SB1; break;
case SbType::SB2: return FilterType::SB2; break;
case SbType::SBPro1: return FilterType::SBPro1; break;
case SbType::SBPro2: return FilterType::SBPro2; break;
case SbType::SB16: return FilterType::SB16; break;
case SbType::GameBlaster: return FilterType::None; break;
}
// clang-format on
} else if (filter_choice == "off") {
return FilterType::None;
} else if (filter_choice == "sb1") {
return FilterType::SB1;
} else if (filter_choice == "sb2") {
return FilterType::SB2;
} else if (filter_choice == "sbpro1") {
return FilterType::SBPro1;
} else if (filter_choice == "sbpro2") {
return FilterType::SBPro2;
} else if (filter_choice == "sb16") {
return FilterType::SB16;
} else if (filter_choice == "modern") {
return FilterType::Modern;
}
return {};
}
static void configure_sb_filter_for_model(MixerChannelPtr channel,
const std::string& filter_prefs,
const bool filter_always_on,
const SbType sb_type)
{
const auto filter_prefs_parts = split(filter_prefs);
const auto filter_choice = filter_prefs_parts.empty()
? "auto"
: filter_prefs_parts[0];
FilterConfig config = {};
auto enable_lpf = [&](const uint8_t order, const uint16_t cutoff_freq_hz) {
config.lpf_state = filter_always_on ? FilterState::ForcedOn
: FilterState::On;
config.lpf_order = order;
config.lpf_cutoff_freq_hz = cutoff_freq_hz;
};
auto enable_zoh_upsampler = [&] {
config.resample_method = ResampleMethod::ZeroOrderHoldAndResample;
config.zoh_rate_hz = NativeDacRateHz;
};
const auto filter_type = [&]() {
if (const auto maybe_filter_type = determine_filter_type(filter_choice,
sb_type)) {
return *maybe_filter_type;
} else {
LOG_WARNING("%s: Invalid 'sb_filter' setting: '%s', using 'modern'",
sb_log_prefix(),
filter_choice.c_str());
set_section_property_value("sblaster", "sb_filter", "modern");
return FilterType::Modern;
}
}();
switch (filter_type) {
case FilterType::None: enable_zoh_upsampler(); break;
case FilterType::SB1:
enable_lpf(2, 3800);
enable_zoh_upsampler();
break;
case FilterType::SB2:
enable_lpf(2, 4800);
enable_zoh_upsampler();
break;
case FilterType::SBPro1:
case FilterType::SBPro2:
enable_lpf(2, 3200);
enable_zoh_upsampler();
break;
case FilterType::SB16:
// With the zero-order-hold upsampler disabled, we're
// just relying on Speex to resample to the host rate.
// This applies brickwall filtering at half the SB
// channel rate, which perfectly emulates the dynamic
// brickwall filter of the SB16.
config.resample_method = ResampleMethod::Resample;
break;
case FilterType::Modern:
// Linear interpolation upsampling is the legacy DOSBox behaviour
config.resample_method = ResampleMethod::LinearInterpolation;
break;
}
constexpr auto OutputType = "DAC";
log_filter_config(sb_log_prefix(), OutputType, filter_type);
set_filter(channel, config);
}
static void configure_sb_filter(MixerChannelPtr channel,
const std::string& filter_prefs,
const bool filter_always_on, const SbType sb_type)
{
// A bit unfortunate, but we need to enable the ZOH upsampler and the
// correct upsample rate first for the filter cutoff frequency
// validation to work correctly.
channel->SetZeroOrderHoldUpsamplerTargetRate(NativeDacRateHz);
channel->SetResampleMethod(ResampleMethod::ZeroOrderHoldAndResample);
if (!channel->TryParseAndSetCustomFilter(filter_prefs)) {
// Not a custom filter setting; try to parse it as a
// model-specific setting.
configure_sb_filter_for_model(channel,
filter_prefs,
filter_always_on,
sb_type);
}
}
static void configure_opl_filter_for_model(MixerChannelPtr opl_channel,
const std::string& filter_prefs,
const SbType sb_type)
{
const auto filter_prefs_parts = split(filter_prefs);
const auto filter_choice = filter_prefs_parts.empty()
? "auto"
: filter_prefs_parts[0];
FilterConfig config = {};
config.resample_method = ResampleMethod::Resample;
auto enable_lpf = [&](const uint8_t order, const uint16_t cutoff_freq_hz) {
config.lpf_state = FilterState::On;
config.lpf_order = order;
config.lpf_cutoff_freq_hz = cutoff_freq_hz;
};
const auto filter_type = [&]() {
if (const auto maybe_filter_type = determine_filter_type(filter_choice,
sb_type)) {
return *maybe_filter_type;
} else {
LOG_WARNING("%s: Invalid 'opl_filter' setting: '%s', using 'auto'",
sb_log_prefix(),
filter_choice.c_str());
set_section_property_value("sblaster", "opl_filter", "auto");
if (const auto filter_type = determine_filter_type("auto", sb_type);
filter_type) {
return *filter_type;
} else {
assert(false);
return FilterType::None;
}
}
}();
// The filter parameters have been tweaked by analysing real hardware
// recordings. The results are virtually indistinguishable from the real
// thing by ear only.
switch (filter_type)
{
case FilterType::None:
case FilterType::SB16:
case FilterType::Modern: break;
case FilterType::SB1:
case FilterType::SB2: enable_lpf(1, 12000); break;
case FilterType::SBPro1:
case FilterType::SBPro2: enable_lpf(1, 8000); break;
}
constexpr auto OutputType = "OPL";
log_filter_config(ChannelName::Opl, OutputType, filter_type);
set_filter(opl_channel, config);
}
static void configure_opl_filter(MixerChannelPtr opl_channel,
const std::string& filter_prefs, const SbType sb_type)
{
assert(opl_channel);
if (!opl_channel->TryParseAndSetCustomFilter(filter_prefs)) {
// Not a custom filter setting; try to parse it as a
// model-specific setting.
configure_opl_filter_for_model(opl_channel, filter_prefs, sb_type);
}
}
static void sb_raise_irq(const SbIrq irq_type)
{
LOG(LOG_SB, LOG_NORMAL)("Raising IRQ");
switch (irq_type) {
case SbIrq::Irq8:
if (sb.irq.pending_8bit) {
// LOG_MSG("SB: 8bit irq pending");
return;
}
sb.irq.pending_8bit = true;
PIC_ActivateIRQ(sb.hw.irq);
break;
case SbIrq::Irq16:
if (sb.irq.pending_16bit) {
// LOG_MSG("SB: 16bit irq pending");
return;
}
sb.irq.pending_16bit = true;
PIC_ActivateIRQ(sb.hw.irq);
break;
default: break;
}
}
static void dsp_flush_data()
{
sb.dsp.out.used = 0;
sb.dsp.out.pos = 0;
}
static double last_dma_callback = 0.0;
static void dsp_dma_callback(const DmaChannel* chan, const DMAEvent event)
{
if (chan != sb.dma.chan || event == DMA_REACHED_TC) {
return;
} else if (event == DMA_MASKED) {
if (sb.mode == DspMode::Dma) {
// Catch up to current time, but don't generate an IRQ!
// Fixes problems with later sci games.
const auto t = PIC_FullIndex() - last_dma_callback;
auto s = static_cast<uint32_t>(sb.dma.rate * t / 1000.0);
if (s > sb.dma.min) {
LOG(LOG_SB, LOG_NORMAL)
("limiting amount masked to sb.dma.min");
s = sb.dma.min;
}
auto min_size = sb.dma.mul >> SbShift;
if (!min_size) {
min_size = 1;
}
min_size *= 2;
if (sb.dma.left > min_size) {
if (s > (sb.dma.left - min_size)) {
s = sb.dma.left - min_size;
}
// This will trigger an irq, see
// play_dma_transfer, so lets not do that
if (!sb.dma.autoinit && sb.dma.left <= sb.dma.min) {
s = 0;
}
if (s) {
ProcessDMATransfer(s);
}
}
sb.mode = DspMode::DmaMasked;
// dsp_change_mode(DspMode::DmaMasked);
LOG(LOG_SB, LOG_NORMAL)
("DMA masked,stopping output, left %d", chan->curr_count);
}
} else if (event == DMA_UNMASKED) {
if (sb.mode == DspMode::DmaMasked && sb.dma.mode != DmaMode::None) {
dsp_change_mode(DspMode::Dma);
// sb.mode=DspMode::Dma;
flush_remainig_dma_transfer();
LOG(LOG_SB, LOG_NORMAL)
("DMA unmasked,starting output, auto %d block %d",
static_cast<int>(chan->is_autoiniting),
chan->base_count);
}
} else {
E_Exit("Unknown sblaster dma event");
}
}
static uint8_t decode_adpcm_portion(const int bit_portion,
const uint8_t adjust_map[],
const int8_t scale_map[], const int last_index)
{
auto& scale = sb.adpcm.stepsize;
auto& sample = sb.adpcm.reference;
const auto i = std::clamp(bit_portion + scale, 0, last_index);
scale = (scale + adjust_map[i]) & 0xff;
sample = static_cast<uint8_t>(clamp(sample + scale_map[i], 0, 255));
return sample;
}
static std::array<uint8_t, 4> decode_adpcm_2bit(const uint8_t data)
{
// clang-format off
constexpr int8_t ScaleMap[] = {
0, 1, 0, -1, 1, 3, -1, -3,
2, 6, -2, -6, 4, 12, -4, -12,
8, 24, -8, -24, 6, 48, -16, -48
};
constexpr uint8_t AdjustMap[] = {
0, 4, 0, 4,
252, 4, 252, 4, 252, 4, 252, 4,
252, 4, 252, 4, 252, 4, 252, 4,
252, 0, 252, 0
};
// clang-format on
static_assert(ARRAY_LEN(ScaleMap) == ARRAY_LEN(AdjustMap));
constexpr auto LastIndex = static_cast<uint8_t>(sizeof(ScaleMap) - 1);
return {decode_adpcm_portion((data >> 6) & 0x3, AdjustMap, ScaleMap, LastIndex),
decode_adpcm_portion((data >> 4) & 0x3, AdjustMap, ScaleMap, LastIndex),
decode_adpcm_portion((data >> 2) & 0x3, AdjustMap, ScaleMap, LastIndex),
decode_adpcm_portion((data >> 0) & 0x3, AdjustMap, ScaleMap, LastIndex)};
}
static std::array<uint8_t, 3> decode_adpcm_3bit(const uint8_t data)
{
// clang-format off
constexpr int8_t ScaleMap[40] = {
0, 1, 2, 3, 0, -1, -2, -3,
1, 3, 5, 7, -1, -3, -5, -7,
2, 6, 10, 14, -2, -6, -10, -14,
4, 12, 20, 28, -4, -12, -20, -28,
5, 15, 25, 35, -5, -15, -25, -35
};
constexpr uint8_t AdjustMap[40] = {
0, 0, 0, 8, 0, 0, 0, 8,
248, 0, 0, 8, 248, 0, 0, 8,
248, 0, 0, 8, 248, 0, 0, 8,
248, 0, 0, 8, 248, 0, 0, 8,
248, 0, 0, 0, 248, 0, 0, 0
};
// clang-format on
static_assert(ARRAY_LEN(ScaleMap) == ARRAY_LEN(AdjustMap));
constexpr auto LastIndex = static_cast<uint8_t>(sizeof(ScaleMap) - 1);
return {decode_adpcm_portion((data >> 5) & 0x7, AdjustMap, ScaleMap, LastIndex),
decode_adpcm_portion((data >> 2) & 0x7, AdjustMap, ScaleMap, LastIndex),
decode_adpcm_portion((data & 0x3) << 1, AdjustMap, ScaleMap, LastIndex)};
}
static std::array<uint8_t, 2> decode_adpcm_4bit(const uint8_t data)
{
// clang-format off
constexpr int8_t ScaleMap[64] = {
0, 1, 2, 3, 4, 5, 6, 7, 0, -1, -2, -3, -4, -5, -6, -7,
1, 3, 5, 7, 9, 11, 13, 15, -1, -3, -5, -7, -9, -11, -13, -15,
2, 6, 10, 14, 18, 22, 26, 30, -2, -6, -10, -14, -18, -22, -26, -30,
4, 12, 20, 28, 36, 44, 52, 60, -4, -12, -20, -28, -36, -44, -52, -60
};
constexpr uint8_t AdjustMap[64] = {
0, 0, 0, 0, 0, 16, 16, 16,
0, 0, 0, 0, 0, 16, 16, 16,
240, 0, 0, 0, 0, 16, 16, 16,
240, 0, 0, 0, 0, 16, 16, 16,
240, 0, 0, 0, 0, 16, 16, 16,
240, 0, 0, 0, 0, 16, 16, 16,
240, 0, 0, 0, 0, 0, 0, 0,
240, 0, 0, 0, 0, 0, 0, 0
};
// clang-format on
static_assert(ARRAY_LEN(ScaleMap) == ARRAY_LEN(AdjustMap));
constexpr auto LastIndex = static_cast<uint8_t>(sizeof(ScaleMap) - 1);
return {decode_adpcm_portion(data >> 4, AdjustMap, ScaleMap, LastIndex),
decode_adpcm_portion(data & 0xf, AdjustMap, ScaleMap, LastIndex)};
}
template <typename T>
static const T* maybe_silence(const uint32_t num_samples, const T* buffer)
{
if (sb.dsp.warmup_remaining_ms <= 0) {
return buffer;
}
static std::vector<T> quiet_buffer = {};
constexpr auto Silent = Mixer_GetSilentDOSSample<T>();
if (quiet_buffer.size() < num_samples) {
quiet_buffer.resize(num_samples, Silent);
}
--sb.dsp.warmup_remaining_ms;
return quiet_buffer.data();
}
static uint32_t read_dma_8bit(const uint32_t bytes_to_read, const uint32_t i = 0)
{
const auto bytes_read = sb.dma.chan->Read(bytes_to_read, sb.dma.buf.b8 + i);
assert(bytes_read <= DmaBufSize * sizeof(sb.dma.buf.b8[0]));
return check_cast<uint32_t>(bytes_read);
}
static uint32_t read_dma_16bit(const uint32_t bytes_to_read, const uint32_t i = 0)
{
const auto unsigned_buf = reinterpret_cast<uint8_t*>(sb.dma.buf.b16 + i);
const auto bytes_read = sb.dma.chan->Read(bytes_to_read, unsigned_buf);
assert(bytes_read <= DmaBufSize * sizeof(sb.dma.buf.b16[0]));
return check_cast<uint32_t>(bytes_read);
}
static void play_dma_transfer(const uint32_t bytes_requested)
{
// How many bytes should we read from DMA?
const auto lower_bound = sb.dma.autoinit ? bytes_requested : sb.dma.min;
const auto bytes_to_read = sb.dma.left <= lower_bound ? sb.dma.left
: bytes_requested;
// All three of these must be populated during the DMA sequence to
// ensure the proper quantities and unit are being accounted for.
// For example: use the channel count to convert from samples to frames.
uint32_t bytes_read = 0;
uint32_t samples = 0;
uint16_t frames = 0;
// In DmaMode::Pcm16BitAliased mode temporarily divide by 2 to get
// number of 16-bit samples, because 8-bit DMA Read returns byte size,
// while in DmaMode::Pcm16Bit mode 16-bit DMA Read returns word size.
const uint8_t dma16_to_sample_divisor = sb.dma.mode == DmaMode::Pcm16BitAliased
? 2
: 1;
// Used to convert from samples to frames (which is what AddSamples
// unintuitively uses.. )
const uint8_t channels = sb.dma.stereo ? 2 : 1;
last_dma_callback = PIC_FullIndex();
// Temporary counter for ADPCM modes
auto decode_adpcm_dma =
[&](auto decode_adpcm_fn) -> std::tuple<uint32_t, uint32_t, uint16_t> {
const uint32_t num_bytes = read_dma_8bit(bytes_to_read);
uint32_t num_samples = 0;
uint16_t num_frames = 0;
// Parse the reference ADPCM byte, if provided
uint32_t i = 0;
if (num_bytes > 0 && sb.adpcm.haveref) {
sb.adpcm.haveref = false;
sb.adpcm.reference = sb.dma.buf.b8[0];
sb.adpcm.stepsize = MinAdaptiveStepSize;
++i;
}
// Decode the remaining DMA buffer into samples using the
// provided function
while (i < num_bytes) {
const auto decoded = decode_adpcm_fn(sb.dma.buf.b8[i]);
constexpr auto NumDecoded = check_cast<uint8_t>(
decoded.size());
sb.chan->AddSamples_m8(NumDecoded,
maybe_silence(NumDecoded,
decoded.data()));
num_samples += NumDecoded;
i++;
}
// ADPCM is mono
num_frames = check_cast<uint16_t>(num_samples);
return {num_bytes, num_samples, num_frames};
};
// Read the actual data, process it and send it off to the mixer
switch (sb.dma.mode) {
case DmaMode::Adpcm2Bit:
std::tie(bytes_read, samples, frames) = decode_adpcm_dma(
decode_adpcm_2bit);
break;
case DmaMode::Adpcm3Bit:
std::tie(bytes_read, samples, frames) = decode_adpcm_dma(
decode_adpcm_3bit);
break;
case DmaMode::Adpcm4Bit:
std::tie(bytes_read, samples, frames) = decode_adpcm_dma(
decode_adpcm_4bit);
break;
case DmaMode::Pcm8Bit:
if (sb.dma.stereo) {
bytes_read = read_dma_8bit(bytes_to_read, sb.dma.remain_size);
samples = bytes_read + sb.dma.remain_size;
frames = check_cast<uint16_t>(samples / channels);
// Only add whole frames when in stereo DMA mode. The
// number of frames comes from the DMA request, and
// therefore user-space data.
if (frames) {
if (sb.dma.sign) {
const auto signed_buf = reinterpret_cast<int8_t*>(
sb.dma.buf.b8);
sb.chan->AddSamples_s8s(
frames,
maybe_silence(samples, signed_buf));
} else {
sb.chan->AddSamples_s8(
frames,
maybe_silence(samples,
sb.dma.buf.b8));
}
}
// Otherwise there's an unhandled dangling sample from
// the last round
if (samples & 1) {
sb.dma.remain_size = 1;
sb.dma.buf.b8[0] = sb.dma.buf.b8[samples - 1];
} else {
sb.dma.remain_size = 0;
}
} else { // Mono