-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevsb.cpp
1469 lines (1180 loc) · 42.9 KB
/
devsb.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
#include "snddefs.h"
#if (defined(SNDLIB_DEVICE_ENABLE_SB) || \
defined(SNDLIB_DEVICE_ENABLE_SB16) || \
defined(SNDLIB_DEVICE_ENABLE_ESS))
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "devsb.h"
#include "convert.h"
#include "sndmisc.h"
#include "snderror.h"
// define to enable logging
//#define DEBUG_LOG
#define arrayof(x) (sizeof(x) / sizeof(x[0]))
const uint32_t probeDataLength = 64;
const uint32_t sbIoBase[] = { 0x220, 0x240, 0x260, 0x280 };
const uint32_t sbIrq[] = { 3, 4, 5, 7, 9, 10, 11, 12 };
const uint32_t sbDma[] = { 0, 1, 3 };
const uint32_t sbDmaHigh[] = { 0, 1, 3, 5, 6, 7 }; // SB16 can transfer 16bit audio over low DMA channels
// SB 2.0/Pro and ESS AudioDrive resources
const soundResourceInfo sbRes[] = {
{
SND_RES_IOBASE,
arrayof(sbIoBase),
sbIoBase
},
{
SND_RES_IRQ,
arrayof(sbIrq),
sbIrq
},
{
SND_RES_DMA,
arrayof(sbDma),
sbDma
},
};
// SB16 resources
const soundResourceInfo sb16Res[] = {
{
SND_RES_IOBASE,
arrayof(sbIoBase),
sbIoBase
},
{
SND_RES_IRQ,
arrayof(sbIrq),
sbIrq
},
{
SND_RES_DMA,
arrayof(sbDma),
sbDma
},
{
SND_RES_DMA2,
arrayof(sbDmaHigh),
sbDmaHigh
}
};
const uint32_t sb2OldRates[] = {4000, 22222};
const uint32_t sb2Rates[] = {4000, 45454};
const uint32_t sb16Rates[] = {5000, 45454}; // yes, SB16 lowest limit is at ~5khz
const uint32_t sb16extRates[] = {5000, 48000};
const uint32_t essRates[] = {4000, 48000}; // officially up to 44100, but 48khz is fine (for 1868/1869 at least)
// SB 2.0 (DSP 2.0 w/o highspeed) sound caps
const soundFormatCapability sb2OldCaps[] = {
{
(SND_FMT_INT8 | SND_FMT_MONO | SND_FMT_UNSIGNED),
-2, // variable range
sb2OldRates,
}
};
// SB 2.0 (DSP >= 2.1) sound caps
const soundFormatCapability sb2Caps[] = {
{
(SND_FMT_INT8 | SND_FMT_MONO | SND_FMT_UNSIGNED),
-2, // variable range
sb2Rates,
}
};
// SB Pro sound caps
const soundFormatCapability sbProCaps[] = {
{
(SND_FMT_INT8 | SND_FMT_MONO | SND_FMT_UNSIGNED),
-2, // variable range
sb2Rates,
},
{
(SND_FMT_INT8 | SND_FMT_STEREO | SND_FMT_UNSIGNED),
-2, // variable range
sb2OldRates,
}
};
// SB16 sound caps
const soundFormatCapability sb16Caps[] = {
{
(SND_FMT_INT8 | SND_FMT_INT16 | SND_FMT_MONO | SND_FMT_STEREO | SND_FMT_SIGNED | SND_FMT_UNSIGNED),
-2, // variable range
sb16Rates,
}
};
// SB16 48khz sound caps (geninue Creative cards can go only up to 45454 Hz, some sources claim that SBLive! SB16 emulator can work up to 48kHz?)
const soundFormatCapability sb16extCaps[] = {
{
(SND_FMT_INT8 | SND_FMT_INT16 | SND_FMT_MONO | SND_FMT_STEREO | SND_FMT_SIGNED | SND_FMT_UNSIGNED),
-2, // variable range
sb16extRates,
}
};
// ESS AudioDrive sound caps
const soundFormatCapability essCaps[] = {
{
(SND_FMT_INT8 | SND_FMT_INT16 | SND_FMT_MONO | SND_FMT_STEREO | SND_FMT_SIGNED | SND_FMT_UNSIGNED),
-2, // variable range
essRates,
}
};
// ---------------------------------------------
// SB16 helper routines
void sbDspWrite(uint32_t base, uint8_t data) {
int timeout = (1ULL << 10); while (((inp(base + 0xC) & 0x80) == 0x80) && (--timeout != 0));
//while (inp(base + 0xC) & 0x80);
outp(base + 0xC, data);
#ifdef DEBUG_LOG
printf("dsp write = %02X%s\n", data, (timeout == 0 ? " (timeout!)" : ""));
#endif
}
#pragma aux sbDspWrite parm routine []
uint32_t sbDspRead(uint32_t base) {
int timeout = (1ULL << 10); while (((inp(base + 0xE) & 0x80) == 0) && (--timeout != 0));
//while (inp(base + 0xE) & 0x80);
uint8_t data = inp(base + 0xA);
#ifdef DEBUG_LOG
printf("dsp read = %02X%s\n", data, (timeout == 0 ? " (timeout!)" : ""));
#endif
return data;
}
#pragma aux sbDspRead parm routine [] value [eax]
#if defined(SNDLIB_DEVICE_ENABLE_ESS)
void essRegWrite(uint32_t base, uint8_t reg, uint8_t data) {
sbDspWrite(base, reg); sbDspWrite(base, data);
#ifdef DEBUG_LOG
printf("ess write Rx%02X = %02X\n", reg, data);
#endif
}
#pragma aux essRegWrite parm routine []
uint32_t essRegRead(uint32_t base, uint8_t reg) {
sbDspWrite(base, 0xC0); sbDspWrite(base, reg); uint32_t data = sbDspRead(base);
#ifdef DEBUG_LOG
printf("ess read Rx%02X = %02X\n", reg, data);
#endif
return data;
}
#pragma aux essRegRead parm routine [] value [eax]
#endif
void sbMixerWrite(uint32_t base, uint8_t index, uint8_t data) {
outp(base + 4, index); outp(base + 5, data);
#ifdef DEBUG_LOG
printf("mixer write Rx%02X = %02X\n", index, data);
#endif
}
uint8_t sbMixerRead(uint32_t base, uint8_t index) {
outp(base + 4, index); uint8_t data = inp(base + 5);
#ifdef DEBUG_LOG
printf("mixer read Rx%02X = %02X\n", index, data);
#endif
return data;
}
inline uint8_t sbGetInterruptMask(uint32_t base) {
//return sbMixerRead(base, 0x82);
outp(base + 4, 0x82); return inp(base + 5); // isr+debug printout fix
}
inline void sbAck8Bit(uint32_t base) {
inp(base + 0xE);
}
inline void sbAck16Bit(uint32_t base) {
inp(base + 0xF);
}
uint32_t sbTimeConstant(int32_t rate) {
if (rate < 4000) return 0; else return 256 - (1000000 / rate);
}
// round(a / b)
uint32_t udivRound(uint32_t a, uint32_t b);
#pragma aux udivRound = \
"xor edx, edx" "div ebx" "shr ebx, 1" \
"cmp edx, ebx" "jb _skip_inc" "inc eax" \
"_skip_inc:" parm [eax] [ebx] value [eax] modify [eax ebx edx]
uint32_t sbTimeConstantAccurate(uint32_t rate, bool stereo) {
if (rate < 4000) return 0;
uint32_t tc = udivRound(1000000, rate << (stereo ? 1 : 0));
return 256 - tc;
}
uint32_t sbGetActualSampleRate(uint32_t tc, bool stereo) {
return (1000000 / ((256 - tc) << (stereo ? 1 : 0)));
}
#if defined(SNDLIB_DEVICE_ENABLE_ESS)
uint32_t essGetDivisor(uint32_t rate) {
if (rate < 4000) return 0;
return (rate > 22222) ? 256 - udivRound(795500, rate) : 128 - udivRound(397700, rate);
}
uint32_t essGetActualSampleRate(uint32_t tc) {
return (tc >= 128 ? (795500 / (256 - tc)) : (397700 / (128 - tc)));
}
#ifdef SNDLIB_DEVICE_ESS_ENABLE_ES1869_FEATURES
// ES1869 only: supports accurate clock rate for 48 kHz
uint32_t ess1869GetDivisor(uint32_t rate) {
if (rate < 4000) return 0;
return ((rate % 8000) == 0) ? 256 - udivRound(768000, rate) : 128 - udivRound(793800, rate);
}
uint32_t ess1869GetActualSampleRate(uint32_t tc) {
return (tc >= 128 ? (768000 / (256 - tc)) : (793800 / (128 - tc)));
}
#endif
#endif
bool sbDspReset(uint32_t base) {
uint32_t timeout;
// trigger reset
outp(base + 0x6, 1);
// wait a bit (i.e. read from unused port)
timeout = 400; while (--timeout) inp(0x80); // should not screw everything up
// remove reset and wait for 0xAA in read buffer
outp(base + 0x6, 0);
timeout = 20; bool detected = false;
while((--timeout) && (!detected)) {
if (sbDspRead(base) == 0xAA) detected = true;
}
return detected;
}
// -------------- devSBBase common stuff -----------------
sndSBBase::sndSBBase(const char *name) : IsaDmaDevice(name) {
// fill with defaults
isDetected = isInitialised = isPlaying = isPaused = false;
currentPos = irqs = 0;
oldTotalPos = 0;
devinfo.name = getName();
devinfo.version = NULL;
devinfo.maxBufferSize = 32768; // BYTES
devinfo.caps = NULL;
devinfo.capsLen = 0;
devinfo.flags = 0;
}
uint32_t sndSBBase::sbDetect(SoundDevice::deviceInfo *info, bool manualDetect) {
uint32_t sbType, sbDspVersion;
#ifdef DEBUG_LOG
fprintf(stderr, __func__": start detect...\n");
#endif
char envstr[64] = { 0 };
if (info == NULL) return 0;
// query BLASTER variable
char* blasterEnv = getenv("BLASTER");
if (blasterEnv != NULL) {
#ifdef DEBUG_LOG
printf("BLASTER variable = %s\n", blasterEnv);
#endif
// copy variable to temporary buffer
strncpy(envstr, blasterEnv, sizeof(envstr));
// tokenize
char* p = strtok(envstr, " ");
while (p != NULL) {
switch (toupper(*p)) {
case 'A': info->iobase = strtol(p + 1, NULL, 16); break;
case 'I': info->irq = atoi(p + 1); break;
case 'D': info->dma = atoi(p + 1); break;
case 'H': info->dma2 = atoi(p + 1); break;
case 'T': sbType = atoi(p + 1); break;
/*
case 'P' : res.midiPort = atoi(p+1); break;
case 'E' : res.emuPort = atoi(p+1); break;
*/
default: break;
}
p = strtok(NULL, " ");
}
}
else if (manualDetect == false) return 0; else {
info->iobase = info->dma = info->irq = info->dma2 = -1;
}
// check for port available
if (info->iobase == -1)
if (manualDetect == false) return 0; else {
// probe each port
bool isFound = false;
for (const uint32_t* port = sbIoBase; port < (sbIoBase + arrayof(sbIoBase)); port++) {
if (sbDspReset(*port) == true) {
// found!
isFound = true;
info->iobase = *port;
break;
}
}
if (isFound == false) return 0;
}
#ifdef DEBUG_LOG
printf("io = 0x%X, irq = %d, dma = %d, highdma = %d\n", info->iobase, info->irq, info->dma, info->dma2);
#endif
// test for SB presence by resetting DSP
if (sbDspReset(info->iobase) == false) return 0;
#ifdef DEBUG_LOG
printf("dsp reset ok\n");
#endif
// get DSP version
sbDspWrite(info->iobase, 0xE1);
sbDspVersion = (sbDspRead(info->iobase) << 8); sbDspVersion |= sbDspRead(info->iobase);
#ifdef DEBUG_LOG
printf("dsp version %d.%d\n", sbDspVersion >> 8, sbDspVersion & 0xff);
#endif
// get extended detect info
if (detectExt(info, sbDspVersion) != SND_ERR_OK) return 0;
// fill IRQ/DMA info
if (fillIrqDma(info, sbDspVersion) != SND_ERR_OK) return 0;
// test for IRQ/DMA avail info
if ((info->irq == -1) || (info->dma == -1))
if (manualDetect == false) return 0; else {
irqEntry irqstuff = { 0 };
irqstuff.handler = &sbDetectIrqProc;
snd_IrqDetectInfo.iobase = info->iobase;
snd_IrqDetectInfo.irq = &irqstuff;
if (info->irq == -1) {
// probe IRQ manually
for (const uint32_t* irq = sbIrq; irq < (sbIrq + arrayof(sbIrq)); irq++) {
#ifdef DEBUG_LOG
printf("\nprobe irq %d...", *irq);
#endif
// hook
if (irqHook(*irq, &irqstuff, true)) continue;
// reset irq found flag
snd_IrqDetectInfo.found = false;
// trigger IRQ
sbDspWrite(info->iobase, 0xF2);
volatile uint32_t timeout = 16384;
do {
inp(0x80);
if (snd_IrqDetectInfo.found) {
#ifdef DEBUG_LOG
printf("found\n");
#endif
info->irq = *irq;
break;
}
} while (--timeout);
irqUnhook(&irqstuff, true);
// acknowledge interrupt if not found
if (snd_IrqDetectInfo.found) break; else sbAck8Bit(snd_IrqDetectInfo.iobase);
}
if (info->irq == -1) return 0;
}
// probe dma
if (info->dma == -1) {
//info->dma = 1; // SB 2.0/Pro default
::dmaBlock testblk;
if (dmaAlloc(probeDataLength, &testblk) == false) return 0;
memset(testblk.ptr, 0x80, probeDataLength);
for (const uint32_t* dma = sbDma; dma < (sbDma + arrayof(sbDma)); dma++) {
#ifdef DEBUG_LOG
printf("\nprobe dma %d...", *dma);
#endif
dmaSetup(*dma, &testblk, probeDataLength, dmaModeSingle | dmaModeRead | dmaModeNoAutoInit);
// do single-cycle transfer and track dma position change
sbDspReset(info->iobase);
sbDspWrite(info->iobase, 0xD1);
sbDspWrite(info->iobase, 0x40); sbDspWrite(info->iobase, sbTimeConstant(8000));
sbDspWrite(info->iobase, 0x14); sbDspWrite(info->iobase, probeDataLength & 0xFF); sbDspWrite(info->iobase, probeDataLength >> 8);
volatile uint32_t timeout = 16384;
volatile uint32_t dmapos = dmaGetCurrentCount(*dma);
do {
if (dmapos != dmaGetCurrentCount(*dma)) {
info->dma = *dma;
#ifdef DEBUG_LOG
printf("found\n");
#endif
break;
};
} while (--timeout);
dmaStop(*dma);
if (info->dma != -1) break;
}
dmaFree(&testblk);
sbDspWrite(info->iobase, 0xD3);
sbDspReset(info->iobase);
if (info->dma == -1) return 0;
}
}
#ifdef DEBUG_LOG
printf("io = 0x%X, irq = %d, dma = %d, highdma = %d\n", info->iobase, info->irq, info->dma, info->dma2);
#endif
// fill caps
if (fillDspInfo(info, sbDspVersion) == 0) return 0;
// detected! :)
isDetected = true;
return sbDspVersion;
}
uint32_t sndSBBase::detectExt(SoundDevice::deviceInfo* info, uint32_t sbDspVersion)
{
return SND_ERR_OK;
}
uint32_t sndSBBase::fillIrqDma(SoundDevice::deviceInfo* info, uint32_t sbDspVersion)
{
return SND_ERR_OK;
}
uint32_t sndSBBase::fillDspInfo(SoundDevice::deviceInfo * info, uint32_t sbDspVersion)
{
return 0;
}
const char* sndSBBase::devinfoToString(SoundDevice::deviceInfo * info, uint32_t sbDspVersion) {
snprintf(
info->privateBuf,
info->privateBufSize,
"A%03X/I%d/D%d, DSP v.%d.%02d\0",
info->iobase, info->irq, info->dma,
(sbDspVersion >> 8) & 0xFF, sbDspVersion & 0xFF
);
info->version = info->privateBuf;
return info->version;
}
bool sndSBBase::irqProc()
{
return true;
}
void __far __interrupt sndSBBase::sbDetectIrqProc()
{
// signal IRQ is found
snd_IrqDetectInfo.found = true;
sbAck8Bit(snd_IrqDetectInfo.iobase);
outp(snd_IrqDetectInfo.irq->info->picbase, 0x20); if (snd_IrqDetectInfo.irq->info->flags & IRQ_SECONDARYPIC) outp(0x20, 0x20);
}
uint32_t sndSBBase::init(SoundDevice::deviceInfo* info)
{
// deinit
if (isInitialised) done();
// validate fields
SoundDevice::deviceInfo *p = (info != NULL ? info : &this->devinfo);
// validate resources
if ((p->iobase == -1) || (p->iobase == 0) || (p->irq == -1) || (p->dma == -1)) return SND_ERR_INVALIDCONFIG;
// copy resource fields
if (info != NULL) {
this->devinfo.iobase = p->iobase;
this->devinfo.irq = p->irq;
this->devinfo.dma = p->dma;
this->devinfo.dma2 = p->dma2;
}
// TODO: redetect
// reset DSP
if (sbDspReset(this->devinfo.iobase) == false) return SND_ERR_NOTFOUND;
// disable speaker
sbDspWrite(this->devinfo.iobase, 0xD3);
isInitialised = true;
return SND_ERR_OK;
}
uint32_t sndSBBase::close() {
// stop playback
if (isPlaying) stop();
// stop DMA
if (dmaChannel != -1) dmaStop(dmaChannel);
// deallocate DMA block
dmaBufferFree();
// unhook irq if hooked
if (irq.hooked) irqUnhook(&irq, false);
// reset DSP (optional but just in cause)
if (sbDspReset(devinfo.iobase) == false) return SND_ERR_NOTFOUND;
// fill with defaults
isOpened = isPlaying = false;
currentPos = renderPos = irqs = 0;
dmaChannel = dmaBlockSize = dmaBufferCount = dmaBufferSize = dmaBufferSamples = dmaCurrentPtr = dmaRenderPtr = 0;
return SND_ERR_OK;
}
uint32_t sndSBBase::done() {
if (isOpened) close();
isInitialised = false;
return SND_ERR_OK;
}
const char* sndSBBase::getName()
{
return devinfo.name;
}
uint32_t sndSBBase::openCommon(uint32_t sampleRate, soundFormat fmt, soundFormat newFormat, uint32_t bufferSize, soundDeviceCallback callback, void* userdata, soundFormatConverterInfo* conv) {
uint32_t result = SND_ERR_OK;
// pass converter info
#ifdef DEBUG_LOG
printf("src = 0x%x, dst = 0x%x\n", fmt, newFormat);
#endif
if (getConverter(fmt, newFormat, conv) != SND_ERR_OK) return SND_ERR_UNKNOWN_FORMAT;
conv->bytesPerSample = getBytesPerSample(newFormat);
// we have all relevant info for opening sound device, do it now
// allocate DMA buffer
if ((result = dmaBufferInit(bufferSize, conv)) != SND_ERR_OK) return result;
// allocate DMA buffer
if ((result = installIrq()) != SND_ERR_OK) return result;
// save callback info
this->callback = callback;
this->userdata = userdata;
// pass coverter info
memcpy(&convinfo, conv, sizeof(convinfo));
// debug output
#ifdef DEBUG_LOG
fprintf(stderr, __func__": requested format 0x%X, opened format 0x%X, rate %d hz, buffer %d bytes\n", fmt, newFormat, sampleRate, bufferSize);
#endif
isOpened = true;
return SND_ERR_OK;
}
uint32_t sndSBBase::ioctl(uint32_t function, void* data, uint32_t len)
{
// stub
return SND_ERR_UNSUPPORTED;
}
#ifdef SNDLIB_DEVICE_ENABLE_SB
// ----------- SB 1.x/2.x/Pro common stuff -------------------------
sndSoundBlaster::sndSoundBlaster() : sndSBBase("Sound Blaster 1.x/2.x/Pro") {
timeConstant = 0;
}
uint32_t sndSoundBlaster::fillDspInfo(SoundDevice::deviceInfo *info, uint32_t sbDspVersion) {
// fill info
info->maxBufferSize = 32768; // BYTES
info->flags = SND_DEVICE_CLOCKDRIFT;
switch (sbDspVersion >> 8) {
case 1:
// SB 1.x w/o autoinit
info->caps = sb2OldCaps;
info->capsLen = arrayof(sb2OldCaps);
info->name = "Sound Blaster 1.0";
break;
case 2:
case 4: // SB16 doesn't support SBPro stereo mode!
if (sbDspVersion == 0x200) {
// old SB 2.0 w/o highspeed mode
info->caps = sb2OldCaps;
info->capsLen = arrayof(sb2OldCaps);
info->name = "Sound Blaster 2.0 (non-highspeed)";
} else {
// new SB 2.0 w/ highspeed mode
info->caps = sb2Caps;
info->capsLen = arrayof(sb2Caps);
info->name = "Sound Blaster 2.0";
}
break;
case 3:
info->caps = sbProCaps;
info->capsLen = arrayof(sbProCaps);
info->name = "Sound Blaster Pro";
break;
/*
case 4:
info->caps = sb16Caps;
info->capsLen = arrayof(sb16Caps);
info->name = "Sound Blaster 16";
info->flags = 0;
break;
*/
default:
return 0;
}
return sbDspVersion;
}
uint32_t sndSoundBlaster::detect(SoundDevice::deviceInfo *info) {
// clear and fill device info
this->devinfo.clear();
this->dspVersion = sbDetect(&this->devinfo, true);
// if dspVersion == 0 - SB not found!
if (this->dspVersion == 0) return SND_ERR_NOTFOUND;
// fill DSP version
devinfoToString(&this->devinfo, this->dspVersion);
// copy info if not NULL
if (info != NULL) *info = devinfo;
return SND_ERR_OK;
}
uint32_t sndSoundBlaster::open(uint32_t sampleRate, soundFormat fmt, uint32_t bufferSize, uint32_t flags, soundDeviceCallback callback, void *userdata, soundFormatConverterInfo *conv) {
uint32_t result = SND_ERR_OK;
if ((conv == NULL) || (callback == NULL)) return SND_ERR_NULLPTR;
// stooop!
if (isOpened) close();
// clear converter info
memset(conv, 0, sizeof(soundFormatConverterInfo));
soundFormat newFormat = fmt;
// check if format is supported
if (flags & SND_OPEN_NOCONVERT) {
// no conversion if performed
if (isFormatSupported(sampleRate, fmt, conv) != SND_ERR_OK) return SND_ERR_UNKNOWN_FORMAT;
} else {
// conversion is allowed
switch (dspVersion >> 8) {
case 3:
// convert 16bit to 8bit unsigned, stereo->mono if (rate > 22222) else stereo
if (((fmt & SND_FMT_CHANNELS_MASK) == SND_FMT_STEREO) && (sampleRate <= 22222))
newFormat = SND_FMT_UNSIGNED | SND_FMT_INT8 | SND_FMT_STEREO;
else
// samplerate > 22222hz, fallthrough here
case 1:
case 2:
case 4: // SB16 doesn't support SBPro stereo mode!
// convert 16bit to 8bit unsigned, stereo to mono, check for rate limit
newFormat = SND_FMT_UNSIGNED | SND_FMT_INT8 | SND_FMT_MONO;
// check if format supported
if (isFormatSupported(sampleRate, newFormat, conv) != SND_ERR_OK) return SND_ERR_UNKNOWN_FORMAT;
break;
default:
return SND_ERR_UNSUPPORTED;
}
}
// calculate new sample rate
timeConstant = sbTimeConstantAccurate(sampleRate, newFormat & SND_FMT_STEREO);
conv->sampleRate = sbGetActualSampleRate(timeConstant, newFormat & SND_FMT_STEREO);
// pass the rest to common open function
return openCommon(sampleRate, fmt, newFormat, bufferSize, callback, userdata, conv);
}
uint32_t sndSoundBlaster::resume() {
// resume playback
// enable speaker
sbDspWrite(devinfo.iobase, 0xD1);
// resume DMA
switch (playbackType) {
case SingleCycle:
case AutoInit:
sbDspWrite(devinfo.iobase, 0xD4);
break;
case HighSpeed:
// idk
sbDspWrite(devinfo.iobase, 0x90); // UNRELIABLE
break;
}
isPaused = false;
return SND_ERR_RESUMED;
}
uint32_t sndSoundBlaster::start() {
uint32_t rtn = SND_ERR_OK;
if (rtn = prefill() != SND_ERR_OK) return rtn;
// ------------------------------------
// device-specific code
// acknowledge stuck IRQs
sbAck8Bit(devinfo.iobase);
// enable speaker
sbDspWrite(devinfo.iobase, 0xD1);
// set time constant
sbDspWrite(devinfo.iobase, 0x40);
sbDspWrite(devinfo.iobase, timeConstant);
// program DMA controller for transfer
if (dmaSetup(dmaChannel, &dmaBlock, dmaBlockSize, dmaModeSingle | dmaModeAutoInit | dmaModeRead) == false)
return SND_ERR_DMA;
#ifdef DEBUG_LOG
printf("dma ready\n");
#endif
// init mixer stereo mode (sbpro only)
// warning - creative official doc asks you to do 1-byte single cycle transfer (to avoid reversed stereo?) then do actual transfers, we'll omit it now
if ((dspVersion >> 8) == 0x3) {
// disable lowpass filter + optionally do stereo
sbMixerWrite(devinfo.iobase, 0xE,
(sbMixerRead(devinfo.iobase, 0xE) & ~0x22) | ((convinfo.format & SND_FMT_STEREO) ? 0x22 : 0x02)
);
}
// select mode and enable DMA playback
if (dspVersion >= 0x200) {
// set block size
sbDspWrite(devinfo.iobase, 0x48);
sbDspWrite(devinfo.iobase, (dmaBufferSize - 1) & 0xFF);
sbDspWrite(devinfo.iobase, (dmaBufferSize - 1) >> 8);
if ((convinfo.sampleRate << (convinfo.format & SND_FMT_STEREO ? 1 : 0)) > 22222) {
playbackType = HighSpeed;
sbDspWrite(devinfo.iobase, 0x90);
#ifdef DEBUG_LOG
printf("highspeed mode\n");
#endif
} else {
playbackType = AutoInit;
sbDspWrite(devinfo.iobase, 0x1C);
#ifdef DEBUG_LOG
printf("normal mode\n");
#endif
}
} else {
playbackType = SingleCycle;
sbDspWrite(devinfo.iobase, 0x14);
sbDspWrite(devinfo.iobase, (dmaBufferSize - 1) & 0xFF);
sbDspWrite(devinfo.iobase, (dmaBufferSize - 1) >> 8);
#ifdef DEBUG_LOG
printf("SB 1.x single cycle mode\n");
#endif
}
#ifdef DEBUG_LOG
printf("playback started\n");
#endif
// ------------------------------------
// done! we're playing sound :)
isPaused = false; isPlaying = true;
return SND_ERR_OK;
}
// pause
uint32_t sndSoundBlaster::pause() {
switch (playbackType) {
case SingleCycle:
case AutoInit:
sbDspWrite(devinfo.iobase, 0xD0);
break;
case HighSpeed:
// totally unreliable, at least on dosbox-x
sbDspReset(devinfo.iobase);
break;
}
// disable speaker
sbDspWrite(devinfo.iobase, 0xD3);
isPaused = true;
return SND_ERR_OK;
}
uint32_t sndSoundBlaster::ioctl(uint32_t function, void * data, uint32_t len)
{
return sndSBBase::ioctl(function, data, len);
}
uint32_t sndSoundBlaster::stop() {
if (isPlaying) {
// stop DMA
switch (playbackType) {
case SingleCycle:
sbDspWrite(devinfo.iobase, 0xD0);
break;
case AutoInit:
sbDspWrite(devinfo.iobase, 0xDA);
break;
case HighSpeed:
sbDspReset(devinfo.iobase);
break;
}
dmaStop(dmaChannel);
}
// reset mixer to mono (sbpro only)
if ((dspVersion >> 8) == 0x3) {
sbMixerWrite(devinfo.iobase, 0xE, sbMixerRead(devinfo.iobase, 0xE) & ~0x02);
}
isPlaying = false;
// clear playing position
currentPos = renderPos = irqs = 0;
dmaCurrentPtr = dmaRenderPtr = 0;
return SND_ERR_OK;
}
// irq procedure
bool sndSoundBlaster::irqProc() {
// restart block if single cycle
if ((playbackType == SingleCycle) && (!isPaused)) {
sbDspWrite(devinfo.iobase, 0x14);
sbDspWrite(devinfo.iobase, (dmaBufferSize - 1) & 0xFF);
sbDspWrite(devinfo.iobase, (dmaBufferSize - 1) >> 8);
}
// advance play pointers
irqAdvancePos();
// acknowledge dma interrupt
sbAck8Bit(devinfo.iobase);
// acknowledge interrupt
outp(irq.info->picbase, 0x20); if (irq.info->flags & IRQ_SECONDARYPIC) outp(0x20, 0x20);
// advance play pointers, call callback
irqCallbackCaller();
return false; // we're handling EOI by itself
}
#endif
#ifdef SNDLIB_DEVICE_ENABLE_SB16
// ----------- SB16 common stuff -------------------------
sndSoundBlaster16::sndSoundBlaster16() : sndSBBase("Sound Blaster 16") {
// fill with defaults
is16Bit = false;
};
uint32_t sndSoundBlaster16::fillDspInfo(SoundDevice::deviceInfo *info, uint32_t sbDspVersion) {
// fill info
info->maxBufferSize = 32768; // BYTES
if (sbDspVersion >= 0x400) {
info->caps = sb16Caps;
info->capsLen = arrayof(sb16Caps);
info->name = "Sound Blaster 16";
info->flags = 0;
} else
return 0; // not a SB16!
return sbDspVersion;
}
uint32_t sndSoundBlaster16::detect(SoundDevice::deviceInfo *info) {
// clear and fill device info
this->devinfo.clear();
this->dspVersion = sbDetect(&this->devinfo, true);
// filter out no SB + SB 1.0 (w/o autoinit)
if (this->dspVersion < 0x400) return SND_ERR_NOTFOUND;
// fill DSP version
devinfoToString(&this->devinfo, this->dspVersion);
// copy info if not NULL
if (info != NULL) *info = devinfo;
return SND_ERR_OK;
}
uint32_t sndSoundBlaster16::open(uint32_t sampleRate, soundFormat fmt, uint32_t bufferSize, uint32_t flags, soundDeviceCallback callback, void *userdata, soundFormatConverterInfo *conv) {
uint32_t result = SND_ERR_OK;
if ((conv == NULL) || (callback == NULL)) return SND_ERR_NULLPTR;
// stooop!
if (isOpened) close();
// clear converter info
memset(conv, 0, sizeof(soundFormatConverterInfo));
soundFormat newFormat = fmt;
// check if format is supported
if ((flags & SND_OPEN_NOCONVERT) == 0) {
// conversion is allowed
// suggest 16bit mono/stereo, leave orig format for 8/16bit
if ((fmt & SND_FMT_DEPTH_MASK) > SND_FMT_INT16) {
newFormat = (fmt & (SND_FMT_CHANNELS_MASK | SND_FMT_SIGN_MASK)) | SND_FMT_INT16;
}
}
if (isFormatSupported(sampleRate, newFormat, conv) != SND_ERR_OK) return SND_ERR_UNKNOWN_FORMAT;
// pass the rest to common open function
return openCommon(sampleRate, fmt, newFormat, bufferSize, callback, userdata, conv);
}
uint32_t sndSoundBlaster16::resume() {
// resume playback
sbDspWrite(devinfo.iobase, (is16Bit ? 0xD6 : 0xD4));
isPaused = false;
return SND_ERR_RESUMED;
}
uint32_t sndSoundBlaster16::start() {
uint32_t rtn = SND_ERR_OK;
if ((rtn = prefill()) != SND_ERR_OK) return rtn;
// ------------------------------------
// device-specific code
// reset DSP
if (sbDspReset(devinfo.iobase) == false) return SND_ERR_INVALIDCONFIG;
// check if 16 bit transfer
is16Bit = (convinfo.format & SND_FMT_INT16) == SND_FMT_INT16;
dmaChannel = (is16Bit ? devinfo.dma2 : devinfo.dma);