File: sb.c

package info (click to toggle)
allegro4.2 2%3A4.2.2-2.2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 25,220 kB
  • ctags: 14,905
  • sloc: ansic: 128,127; asm: 17,020; cpp: 3,856; sh: 2,239; objc: 1,168; makefile: 713; python: 296; pascal: 179; perl: 73
file content (1338 lines) | stat: -rw-r--r-- 29,574 bytes parent folder | download | duplicates (10)
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
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
/*         ______   ___    ___ 
 *        /\  _  \ /\_ \  /\_ \ 
 *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___ 
 *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
 *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
 *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
 *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
 *                                           /\____/
 *                                           \_/__/
 *
 *      Soundblaster driver. Supports DMA driven sample playback (mixing 
 *      up to eight samples at a time) and raw note output to the SB MIDI 
 *      port. The Adlib (FM synth) MIDI playing code is in adlib.c.
 *
 *      By Shawn Hargreaves.
 *
 *      Input routines added by Ove Kaaven.
 *
 *      See readme.txt for copyright information.
 */


#include <string.h>

#include "allegro.h"
#include "allegro/internal/aintern.h"
#include "allegro/platform/aintdos.h"

#ifndef ALLEGRO_DOS
   #error something is wrong with the makefile
#endif



/* external interface to the digital SB driver */
static int sb_detect(int input);
static int sb_init(int input, int voices);
static void sb_exit(int input);
static int sb_set_mixer_volume(int volume);
static int sb_buffer_size(void);
static int sb_rec_cap_rate(int bits, int stereo);
static int sb_rec_cap_parm(int rate, int bits, int stereo);
static int sb_rec_source(int source);
static int sb_rec_start(int rate, int bits, int stereo);
static void sb_rec_stop(void);
static int sb_rec_read(void *buf);

static char sb_desc[256] = EMPTY_STRING;



#define SB_DRIVER_CONTENTS             \
   0, 0,                               \
   MIXER_MAX_SFX, MIXER_DEF_SFX,       \
   sb_detect,                          \
   sb_init,                            \
   sb_exit,                            \
   sb_set_mixer_volume,                \
   NULL,                               \
   NULL,                               \
   NULL,                               \
   sb_buffer_size,                     \
   _mixer_init_voice,                  \
   _mixer_release_voice,               \
   _mixer_start_voice,                 \
   _mixer_stop_voice,                  \
   _mixer_loop_voice,                  \
   _mixer_get_position,                \
   _mixer_set_position,                \
   _mixer_get_volume,                  \
   _mixer_set_volume,                  \
   _mixer_ramp_volume,                 \
   _mixer_stop_volume_ramp,            \
   _mixer_get_frequency,               \
   _mixer_set_frequency,               \
   _mixer_sweep_frequency,             \
   _mixer_stop_frequency_sweep,        \
   _mixer_get_pan,                     \
   _mixer_set_pan,                     \
   _mixer_sweep_pan,                   \
   _mixer_stop_pan_sweep,              \
   _mixer_set_echo,                    \
   _mixer_set_tremolo,                 \
   _mixer_set_vibrato,                 \
   0, 0,                               \
   sb_rec_cap_rate,                    \
   sb_rec_cap_parm,                    \
   sb_rec_source,                      \
   sb_rec_start,                       \
   sb_rec_stop,                        \
   sb_rec_read



DIGI_DRIVER digi_sb10 =
{
   DIGI_SB10,
   empty_string,
   empty_string,
   "Sound Blaster 1.0", 
   SB_DRIVER_CONTENTS
};



DIGI_DRIVER digi_sb15 =
{
   DIGI_SB15,
   empty_string,
   empty_string,
   "Sound Blaster 1.5", 
   SB_DRIVER_CONTENTS
};



DIGI_DRIVER digi_sb20 =
{
   DIGI_SB20,
   empty_string,
   empty_string,
   "Sound Blaster 2.0", 
   SB_DRIVER_CONTENTS
};



DIGI_DRIVER digi_sbpro =
{
   DIGI_SBPRO,
   empty_string,
   empty_string,
   "Sound Blaster Pro", 
   SB_DRIVER_CONTENTS
};



DIGI_DRIVER digi_sb16=
{
   DIGI_SB16,
   empty_string,
   empty_string,
   "Sound Blaster 16", 
   SB_DRIVER_CONTENTS
};



/* external interface to the SB midi output driver */
static int sb_midi_detect(int input);
static int sb_midi_init(int input, int voices);
static void sb_midi_exit(int input);
static void sb_midi_output(int data);

static char sb_midi_desc[256] = EMPTY_STRING;



MIDI_DRIVER midi_sb_out =
{
   MIDI_SB_OUT,
   empty_string,
   empty_string,
   "SB MIDI interface", 
   0, 0, 0xFFFF, 0, -1, -1,
   sb_midi_detect,
   sb_midi_init,
   sb_midi_exit,
   NULL,
   NULL,
   sb_midi_output,
   _dummy_load_patches,
   _dummy_adjust_patches,
   _dummy_key_on,
   _dummy_noop1,
   _dummy_noop2,
   _dummy_noop3,
   _dummy_noop2,
   _dummy_noop2
};



static int sb_in_use = FALSE;             /* is SB being used? */
static int sb_stereo = FALSE;             /* in stereo mode? */
static int sb_recording = FALSE;          /* in input mode? */
static int sb_16bit = FALSE;              /* in 16 bit mode? */
static int sb_midi_out_mode = FALSE;      /* active for MIDI output? */
static int sb_midi_in_mode = FALSE;       /* active for MIDI input? */
static int sb_int = -1;                   /* interrupt vector */
static int sb_dsp_ver = -1;               /* SB DSP version */
static int sb_dma8 = -1;                  /* 8-bit DMA channel (SB16) */
static int sb_dma16 = -1;                 /* 16-bit DMA channel (SB16) */
static int sb_hw_dsp_ver = -1;            /* as reported by autodetect */
static int sb_dma_size = -1;              /* size of dma transfer in bytes */
static int sb_dma_mix_size = -1;          /* number of samples to mix */
static int sb_dma_count = 0;              /* need to resync with dma? */
static volatile int sb_semaphore = FALSE; /* reentrant interrupt? */

static int sb_sel[2];                     /* selectors for the buffers */
static unsigned long sb_buf[2];           /* pointers to the two buffers */
static int sb_bufnum = 0;                 /* the one currently in use */

static int sb_recbufnum = 0;              /* the one to be returned */

static int sb_master_vol = -1;            /* stored mixer settings */
static int sb_digi_vol = -1;
static int sb_fm_vol = -1;

static int sb_detecting_midi = FALSE;

static void sb_lock_mem(void);



/* sb_read_dsp:
 *  Reads a byte from the SB DSP chip. Returns -1 if it times out.
 */
static INLINE RET_VOLATILE int sb_read_dsp(void)
{
   int x;

   for (x=0; x<0xFFFF; x++)
      if (inportb(0x0E + _sound_port) & 0x80)
	 return inportb(0x0A+_sound_port);

   return -1; 
}



/* sb_write_dsp:
 *  Writes a byte to the SB DSP chip. Returns -1 if it times out.
 */
static INLINE RET_VOLATILE int sb_write_dsp(unsigned char byte)
{
   int x;

   for (x=0; x<0xFFFF; x++) {
      if (!(inportb(0x0C+_sound_port) & 0x80)) {
	 outportb(0x0C+_sound_port, byte);
	 return 0;
      }
   }
   return -1; 
}



/* _sb_voice:
 *  Turns the SB speaker on or off.
 */
void _sb_voice(int state)
{
   if (state) {
      sb_write_dsp(0xD1);

      if (sb_hw_dsp_ver >= 0x300) {          /* set up the mixer */

	 if (sb_master_vol < 0) {
	    outportb(_sound_port+4, 0x22);   /* store master volume */
	    sb_master_vol = inportb(_sound_port+5);
	 }

	 if (sb_digi_vol < 0) {
	    outportb(_sound_port+4, 4);      /* store DAC level */
	    sb_digi_vol = inportb(_sound_port+5);
	 }

	 if (sb_fm_vol < 0) {
	    outportb(_sound_port+4, 0x26);   /* store FM level */
	    sb_fm_vol = inportb(_sound_port+5);
	 }
      }
   }
   else {
      sb_write_dsp(0xD3);

      if (sb_hw_dsp_ver >= 0x300) {          /* reset previous mixer settings */

	 outportb(_sound_port+4, 0x22);      /* restore master volume */
	 outportb(_sound_port+5, sb_master_vol);

	 outportb(_sound_port+4, 4);         /* restore DAC level */
	 outportb(_sound_port+5, sb_digi_vol);

	 outportb(_sound_port+4, 0x26);      /* restore FM level */
	 outportb(_sound_port+5, sb_fm_vol);
      }
   }
}



/* _sb_set_mixer:
 *  Alters the SB-Pro hardware mixer.
 */
int _sb_set_mixer(int digi_volume, int midi_volume)
{
   if (sb_hw_dsp_ver < 0x300)
      return -1;

   if (digi_volume >= 0) {                   /* set DAC level */
      outportb(_sound_port+4, 4);
      outportb(_sound_port+5, (digi_volume & 0xF0) | (digi_volume >> 4));
   }

   if (midi_volume >= 0) {                   /* set FM level */
      outportb(_sound_port+4, 0x26);
      outportb(_sound_port+5, (midi_volume & 0xF0) | (midi_volume >> 4));
   }

   return 0;
}



/* sb_set_mixer_volume:
 *  Sets the SB mixer volume for playing digital samples.
 */
static int sb_set_mixer_volume(int volume)
{
   return _sb_set_mixer(volume, -1);
}



/* sb_stereo_mode:
 *  Enables or disables stereo output for SB-Pro.
 */
static void sb_stereo_mode(int enable)
{
   outportb(_sound_port+0x04, 0x0E); 
   outportb(_sound_port+0x05, (enable ? 2 : 0));
}



/* sb_input_stereo_mode:
 *  Enables or disables stereo input for SB-Pro.
 */
static void sb_input_stereo_mode(int enable)
{
   sb_write_dsp(enable ? 0xA8 : 0xA0);
}



/* sb_set_sample_rate:
 *  The parameter is the rate to set in Hz (samples per second).
 */
static void sb_set_sample_rate(unsigned int rate)
{
   if (sb_16bit) {
      sb_write_dsp(0x41);
      sb_write_dsp(rate >> 8);
      sb_write_dsp(rate & 0xff);
   }
   else {
      if (sb_stereo)
	 rate *= 2;

      sb_write_dsp(0x40);
      sb_write_dsp((unsigned char)(256-1000000/rate));
   }
}



/* sb_set_input_sample_rate:
 *  The parameter is the rate to set in Hz (samples per second).
 */
static void sb_set_input_sample_rate(unsigned int rate, int stereo)
{
   if (sb_16bit) {
      sb_write_dsp(0x42);
      sb_write_dsp(rate >> 8);
      sb_write_dsp(rate & 0xff);
   }
   else {
      if (stereo)
	 rate *= 2;

      sb_write_dsp(0x40);
      sb_write_dsp((unsigned char)(256-1000000/rate));
   }
}



/* _sb_reset_dsp:
 *  Resets the SB DSP chip, returning -1 on error.
 */
int _sb_reset_dsp(int data)
{
   int x;

   outportb(0x06+_sound_port, data);

   for (x=0; x<8; x++)
      inportb(0x06+_sound_port);

   outportb(0x06+_sound_port, 0);

   if (sb_read_dsp() != 0xAA)
      return -1;

   return 0;
}



/* _sb_read_dsp_version:
 *  Reads the version number of the SB DSP chip, returning -1 on error.
 */
int _sb_read_dsp_version(void)
{
   int x, y;

   if (sb_hw_dsp_ver > 0)
      return sb_hw_dsp_ver;

   if (_sound_port <= 0)
      _sound_port = 0x220;

   if (_sb_reset_dsp(1) != 0) {
      sb_hw_dsp_ver = -1;
   }
   else {
      sb_write_dsp(0xE1);
      x = sb_read_dsp();
      y = sb_read_dsp();
      sb_hw_dsp_ver = ((x << 8) | y);
   }

   return sb_hw_dsp_ver;
}



/* sb_buffer_size:
 *  Returns the current DMA buffer size, for use by the audiostream code.
 */
static int sb_buffer_size(void)
{
   return (sb_stereo ? sb_dma_mix_size/2 : sb_dma_mix_size);
}



/* sb_play_buffer:
 *  Starts a dma transfer of size bytes. On cards capable of it, the
 *  transfer will use auto-initialised dma, so there is no need to call
 *  this routine more than once. On older cards it must be called from
 *  the end-of-buffer handler to switch to the new buffer.
 */
static void sb_play_buffer(int size)
{
   if (sb_dsp_ver <= 0x200) {                /* 8 bit single-shot */
      sb_write_dsp(0x14);
      sb_write_dsp((size-1) & 0xFF);
      sb_write_dsp((size-1) >> 8);
   }
   else if (sb_dsp_ver < 0x400) {            /* 8 bit auto-initialised */
      sb_write_dsp(0x48);
      sb_write_dsp((size-1) & 0xFF);
      sb_write_dsp((size-1) >> 8);
      sb_write_dsp(0x90);
   }
   else {                                    /* 16 bit */
      size /= 2;
      sb_write_dsp(0xB6);
      sb_write_dsp(0x30);
      sb_write_dsp((size-1) & 0xFF);
      sb_write_dsp((size-1) >> 8);
   }
}

END_OF_STATIC_FUNCTION(sb_play_buffer);



/* sb_record_buffer:
 *  Starts a dma transfer of size bytes. On cards capable of it, the
 *  transfer will use auto-initialised dma, so there is no need to call
 *  this routine more than once. On older cards it must be called from
 *  the end-of-buffer handler to switch to the new buffer.
 */
static void sb_record_buffer(int size, int stereo, int bits)
{
   if (sb_dsp_ver <= 0x200) {                /* 8 bit single-shot */
      sb_write_dsp(0x24);
      sb_write_dsp((size-1) & 0xFF);
      sb_write_dsp((size-1) >> 8);
   }
   else if (sb_dsp_ver < 0x400) {            /* 8 bit auto-initialised */
      sb_write_dsp(0x48);
      sb_write_dsp((size-1) & 0xFF);
      sb_write_dsp((size-1) >> 8);
      sb_write_dsp(0x98);
   }
   else if (bits <= 8) {                     /* 8 bit */
      sb_write_dsp(0xCE);
      sb_write_dsp((stereo) ? 0x20 : 0x00);
      sb_write_dsp((size-1) & 0xFF);
      sb_write_dsp((size-1) >> 8);
   }
   else {                                    /* 16 bit */
      size /= 2;
      sb_write_dsp(0xBE);
      sb_write_dsp((stereo) ? 0x20 : 0x00);
      sb_write_dsp((size-1) & 0xFF);
      sb_write_dsp((size-1) >> 8);
   }
}

END_OF_STATIC_FUNCTION(sb_record_buffer);



/* sb_interrupt:
 *  The SB end-of-buffer interrupt handler. Swaps to the other buffer 
 *  if the card doesn't have auto-initialised dma, and then refills the
 *  buffer that just finished playing.
 */
static int sb_interrupt(void)
{
   unsigned char isr;

   if (sb_dsp_ver >= 0x400) {
      /* read SB16 ISR mask */
      outportb(_sound_port+4, 0x82);
      isr = inportb(_sound_port+5) & 7;

      if (isr & 4) {
	 /* MPU-401 interrupt */
	 _mpu_poll();
	 _eoi(_sound_irq);
	 return 0;
      }

      if (!(isr & 3)) {
	 /* unknown interrupt */
	 _eoi(_sound_irq);
	 return 0;
      }
   }

   if (sb_dsp_ver <= 0x200) {                /* not auto-initialised */
      _dma_start(_sound_dma, sb_buf[1-sb_bufnum], sb_dma_size, FALSE, FALSE);
      if (sb_recording)
	 sb_record_buffer(sb_dma_size, FALSE, 8);
      else
	 sb_play_buffer(sb_dma_size);
   }
   else {                                    /* poll dma position */
      sb_dma_count++;
      if (sb_dma_count > 16) {
	 sb_bufnum = (_dma_todo(_sound_dma) > (unsigned)sb_dma_size) ? 1 : 0;
	 sb_dma_count = 0;
      }
   }

   if ((!sb_semaphore) && (!sb_recording)) {
      sb_semaphore = TRUE;

      ENABLE();                              /* mix some more samples */
      _mix_some_samples(sb_buf[sb_bufnum], _dos_ds, TRUE);
      DISABLE();

      sb_semaphore = FALSE;
   } 

   sb_bufnum = 1 - sb_bufnum; 

   if ((sb_recording) && (digi_recorder)) {
      sb_semaphore = TRUE;

      ENABLE();                              /* sample input callback */
      digi_recorder();
      DISABLE();

      sb_semaphore = FALSE;
   }

   if (sb_16bit)                             /* acknowledge SB */
      inportb(_sound_port+0x0F);
   else
      inportb(_sound_port+0x0E);

   _eoi(_sound_irq);                         /* acknowledge interrupt */
   return 0;
}

END_OF_STATIC_FUNCTION(sb_interrupt);



/* sb_start:
 *  Starts up the sound output.
 */
static void sb_start(void)
{
   sb_bufnum = 0;

   _sb_voice(1);
   sb_set_sample_rate(_sound_freq);

   if ((sb_hw_dsp_ver >= 0x300) && (sb_dsp_ver < 0x400))
      sb_stereo_mode(sb_stereo);

   if (sb_dsp_ver <= 0x200)
      _dma_start(_sound_dma, sb_buf[0], sb_dma_size, FALSE, FALSE);
   else
      _dma_start(_sound_dma, sb_buf[0], sb_dma_size*2, TRUE, FALSE);

   sb_play_buffer(sb_dma_size);
}



/* sb_stop:
 *  Stops the sound output.
 */
static void sb_stop(void)
{
   /* halt sound output */
   _sb_voice(0);

   /* stop dma transfer */
   _dma_stop(_sound_dma);

   if (sb_dsp_ver <= 0x0200)
      sb_write_dsp(0xD0); 

   _sb_reset_dsp(1);
}



/* sb_detect:
 *  SB detection routine. Uses the BLASTER environment variable,
 *  or 'sensible' guesses if that doesn't exist.
 */
static int sb_detect(int input)
{
   char *blaster = getenv("BLASTER");
   char tmp[64], *msg;
   int cmask;
   int max_freq;
   int default_freq;

   if (!sb_detecting_midi) {
      /* input mode only works on the top of an existing output driver */
      if (input) {
	 if (digi_driver != digi_input_driver) {
	    ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("SB output driver must be installed before input can be read"));
	    return FALSE;
	 }
	 return TRUE;
      }

      /* what breed of SB are we looking for? */
      switch (digi_card) {

	 case DIGI_SB10:
	    sb_dsp_ver = 0x100;
	    break;

	 case DIGI_SB15:
	    sb_dsp_ver = 0x200;
	    break;

	 case DIGI_SB20:
	    sb_dsp_ver = 0x201;
	    break;

	 case DIGI_SBPRO:
	    sb_dsp_ver = 0x300;
	    break;

	 case DIGI_SB16:
	    sb_dsp_ver = 0x400;
	    break;

	 default:
	    sb_dsp_ver = -1;
	    break;
      } 
   }
   else
      sb_dsp_ver = -1;

   /* parse BLASTER env */
   if (blaster) { 
      while (*blaster) {
	 while ((*blaster == ' ') || (*blaster == '\t'))
	    blaster++;

	 if (*blaster) {
	    switch (*blaster) {

	       case 'a': case 'A':
		  if (_sound_port < 0)
		     _sound_port = strtol(blaster+1, NULL, 16);
		  break;

	       case 'i': case 'I':
		  if (_sound_irq < 0)
		     _sound_irq = strtol(blaster+1, NULL, 10);
		  break;

	       case 'd': case 'D':
		  sb_dma8 = strtol(blaster+1, NULL, 10);
		  break;

	       case 'h': case 'H':
		  sb_dma16 = strtol(blaster+1, NULL, 10);
		  break;
	    }

	    while ((*blaster) && (*blaster != ' ') && (*blaster != '\t'))
	       blaster++;
	 }
      }
   }

   if (_sound_port < 0)
      _sound_port = 0x220;

   /* make sure we got a good port address */
   if (_sb_reset_dsp(1) != 0) { 
      static int bases[] = { 0x210, 0x220, 0x230, 0x240, 0x250, 0x260, 0 };
      int i;

      for (i=0; bases[i]; i++) {
	 _sound_port = bases[i];
	 if (_sb_reset_dsp(1) == 0)
	    break;
      }
   }

   /* check if the card really exists */
   _sb_read_dsp_version();
   if (sb_hw_dsp_ver < 0) {
      ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Sound Blaster not found"));
      return FALSE;
   }

   if (sb_dsp_ver < 0) {
      sb_dsp_ver = sb_hw_dsp_ver;
   }
   else {
      if (sb_dsp_ver > sb_hw_dsp_ver) {
	 sb_hw_dsp_ver = sb_dsp_ver = -1;
	 ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Older SB version detected"));
	 return FALSE;
      }
   }

   if (sb_dsp_ver >= 0x400) {
      /* read configuration from SB16 card */
      if (_sound_irq < 0) {
	 outportb(_sound_port+4, 0x80);
	 cmask = inportb(_sound_port+5);
	 if (cmask&1) _sound_irq = 2; /* or 9? */
	 if (cmask&2) _sound_irq = 5;
	 if (cmask&4) _sound_irq = 7;
	 if (cmask&8) _sound_irq = 10;
      }
      if ((sb_dma8 < 0) || (sb_dma16 < 0)) {
	 outportb(_sound_port+4, 0x81);
	 cmask = inportb(_sound_port+5);
	 if (sb_dma8 < 0) {
	    if (cmask&1) sb_dma8 = 0;
	    if (cmask&2) sb_dma8 = 1;
	    if (cmask&8) sb_dma8 = 3;
	 }
	 if (sb_dma16 < 0) {
	    sb_dma16 = sb_dma8;
	    if (cmask&0x20) sb_dma16 = 5;
	    if (cmask&0x40) sb_dma16 = 6;
	    if (cmask&0x80) sb_dma16 = 7;
	 }
      }
   }

   /* if nothing else works */
   if (_sound_irq < 0)
      _sound_irq = 7;

   if (sb_dma8 < 0)
      sb_dma8 = 1;

   if (sb_dma16 < 0)
      sb_dma16 = 5;

   /* figure out the hardware interrupt number */
   sb_int = _map_irq(_sound_irq);

   if (!sb_detecting_midi) {
      /* what breed of SB? */
      if (sb_dsp_ver >= 0x400) {
	 msg = "SB 16";
	 max_freq = 45454;
	 default_freq = 22727;
      }
      else if (sb_dsp_ver >= 0x300) {
	 msg = "SB Pro";
	 max_freq = 22727;
	 default_freq = 22727;
      }
      else if (sb_dsp_ver >= 0x201) {
	 msg = "SB 2.0";
	 max_freq = 45454;
	 default_freq = 22727;
      }
      else if (sb_dsp_ver >= 0x200) {
	 msg = "SB 1.5";
	 max_freq = 16129;
	 default_freq = 16129;
      }
      else {
	 msg = "SB 1.0";
	 max_freq = 16129;
	 default_freq = 16129;
      }

      /* set up the playback frequency */
      if (_sound_freq <= 0)
	 _sound_freq = default_freq;

      if (_sound_freq < 15000) {
	 _sound_freq = 11906;
	 sb_dma_size = 128;
      }
      else if (MIN(_sound_freq, max_freq) < 20000) {
	 _sound_freq = 16129;
	 sb_dma_size = 128;
      }
      else if (MIN(_sound_freq, max_freq) < 40000) {
	 _sound_freq = 22727;
	 sb_dma_size = 256;
      }
      else {
	 _sound_freq = 45454;
	 sb_dma_size = 512;
      }

      if (sb_dsp_ver <= 0x200)
	 sb_dma_size *= 4;

      sb_dma_mix_size = sb_dma_size;

      /* can we handle 16 bit sound? */
      if (sb_dsp_ver >= 0x400) { 
	 if (_sound_dma < 0)
	    _sound_dma = sb_dma16;
	 else
	    sb_dma16 = _sound_dma;

	 sb_16bit = TRUE;
	 digi_driver->rec_cap_bits = 24;
	 sb_dma_size <<= 1;
      }
      else { 
	 if (_sound_dma < 0)
	    _sound_dma = sb_dma8;
	 else
	    sb_dma8 = _sound_dma;

	 sb_16bit = FALSE;
	 digi_driver->rec_cap_bits = 8;
      }

      /* can we handle stereo? */
      if (sb_dsp_ver >= 0x300) {
	 sb_stereo = TRUE;
	 digi_driver->rec_cap_stereo = TRUE;
	 sb_dma_size <<= 1;
	 sb_dma_mix_size <<= 1;
      }
      else {
	 sb_stereo = FALSE;
	 digi_driver->rec_cap_stereo = FALSE;
      }

      /* set up the card description */
      uszprintf(sb_desc, sizeof(sb_desc), get_config_text("%s (%d hz) on port %X, using IRQ %d and DMA channel %d"),
		uconvert_ascii(msg, tmp), _sound_freq, _sound_port, _sound_irq, _sound_dma);

      digi_driver->desc = sb_desc;
   }

   return TRUE;
}



/* sb_init:
 *  SB init routine: returns zero on success, -1 on failure.
 */
static int sb_init(int input, int voices)
{
   if (input)
      return 0;

   if (sb_in_use) {
      ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Can't use SB MIDI interface and DSP at the same time"));
      return -1;
   }

   if (sb_dsp_ver <= 0x200) {       /* two conventional mem buffers */
      if ((_dma_allocate_mem(sb_dma_size, &sb_sel[0], &sb_buf[0]) != 0) ||
	  (_dma_allocate_mem(sb_dma_size, &sb_sel[1], &sb_buf[1]) != 0))
	 return -1;
   }
   else {                           /* auto-init dma, one big buffer */
      if (_dma_allocate_mem(sb_dma_size*2, &sb_sel[0], &sb_buf[0]) != 0)
	 return -1;

      sb_sel[1] = sb_sel[0];
      sb_buf[1] = sb_buf[0] + sb_dma_size;
   }

   sb_lock_mem();

   digi_driver->voices = voices;

   if (_mixer_init(sb_dma_mix_size, _sound_freq, sb_stereo, sb_16bit, &digi_driver->voices) != 0)
      return -1;

   _mix_some_samples(sb_buf[0], _dos_ds, TRUE);
   _mix_some_samples(sb_buf[1], _dos_ds, TRUE);

   _enable_irq(_sound_irq);
   _install_irq(sb_int, sb_interrupt);

   sb_start();

   sb_in_use = TRUE;
   return 0;
}



/* sb_exit:
 *  SB driver cleanup routine, removes ints, stops dma, frees buffers, etc.
 */
static void sb_exit(int input)
{
   if (input)
      return;

   sb_stop();
   _remove_irq(sb_int);
   _restore_irq(_sound_irq);

   __dpmi_free_dos_memory(sb_sel[0]);

   if (sb_sel[1] != sb_sel[0])
      __dpmi_free_dos_memory(sb_sel[1]);

   _mixer_exit();

   sb_hw_dsp_ver = sb_dsp_ver = -1;
   sb_in_use = FALSE;
}



/* sb_rec_cap_rate:
 *  Returns maximum input sampling rate.
 */
static int sb_rec_cap_rate(int bits, int stereo)
{
   if (sb_dsp_ver < 0)
      return 0;

   if (sb_dsp_ver >= 0x400)
      /* SB16 can handle 45kHz under all circumstances */
      return 45454;

   /* lesser SB cards can't handle 16-bit */
   if (bits != 8)
      return 0;

   if (sb_dsp_ver >= 0x300)
      /* SB Pro can handle 45kHz, but only half that in stereo */
      return (stereo) ? 22727 : 45454;

   /* lesser SB cards can't handle stereo */
   if (stereo)
      return 0;

   if (sb_dsp_ver >= 0x201)
      /* SB 2.0 supports 15kHz */
      return 15151;

   /* SB 1.x supports 13kHz */
   return 13157;
}



/* sb_rec_cap_parm:
 *  Returns whether the specified parameters can be set.
 */
static int sb_rec_cap_parm(int rate, int bits, int stereo)
{
   int c, r;

   if ((r = sb_rec_cap_rate(bits, stereo)) <= 0)
      return 0;

   if (r < rate)
      return -r;

   if (sb_dsp_ver >= 0x400) {
      /* if bits==8 and rate==_sound_freq, bidirectional is possible,
	 but that's not implemented yet */
      return 1;
   }

   if (stereo)
      rate *= 2;

   c = 1000000/rate;
   r = 1000000/c;
   if (r != rate)
      return -r;

   return 1;
}



/* sb_rec_source:
 *  Sets the sampling source for audio recording.
 */
static int sb_rec_source(int source)
{
   int v1, v2;

   if (sb_hw_dsp_ver >= 0x400) {
      /* SB16 */
      switch (source) {

	 case SOUND_INPUT_MIC:
	    v1 = 1;
	    v2 = 1;
	    break;

	 case SOUND_INPUT_LINE:
	    v1 = 16;
	    v2 = 8;
	    break;

	 case SOUND_INPUT_CD:
	    v1 = 4;
	    v2 = 2;
	    break;

	 default:
	    return -1;
      }

      outportb(_sound_port+4, 0x3D);
      outportb(_sound_port+5, v1);

      outportb(_sound_port+4, 0x3E);
      outportb(_sound_port+5, v2);

      return 0;
   }
   else if (sb_hw_dsp_ver >= 0x300) {
      /* SB Pro */
      outportb(_sound_port+4, 0xC);
      v1 = inportb(_sound_port+5);

      switch (source) {

	 case SOUND_INPUT_MIC:
	    v1 = (v1 & 0xF9);
	    break;

	 case SOUND_INPUT_LINE:
	    v1 = (v1 & 0xF9) | 6;
	    break;

	 case SOUND_INPUT_CD:
	    v1 = (v1 & 0xF9) | 2;
	    break;

	 default:
	    return -1;
      }

      outportb(_sound_port+4, 0xC);
      outportb(_sound_port+5, v1);

      return 0;
   }

   return -1;
}



/* sb_rec_start:
 *  Stops playback, switches the SB to A/D mode, and starts recording.
 *  Returns the DMA buffer size if successful.
 */
static int sb_rec_start(int rate, int bits, int stereo)
{
   if (sb_rec_cap_parm(rate, bits, stereo) <= 0)
      return 0;

   sb_stop();

   sb_16bit = (bits>8);
   _sound_dma = (sb_16bit) ? sb_dma16 : sb_dma8;
   sb_recording = TRUE;
   sb_recbufnum = sb_bufnum = 0;

   _sb_voice(1);
   sb_set_input_sample_rate(rate, stereo);

   if ((sb_hw_dsp_ver >= 0x300) && (sb_dsp_ver < 0x400))
      sb_input_stereo_mode(stereo);

   if (sb_dsp_ver <= 0x200)
      _dma_start(_sound_dma, sb_buf[0], sb_dma_size, FALSE, TRUE);
   else
      _dma_start(_sound_dma, sb_buf[0], sb_dma_size*2, TRUE, TRUE);

   sb_record_buffer(sb_dma_size, stereo, bits);

   return sb_dma_size;
}



/* sb_rec_stop:
 *  Stops recording, switches the SB back to D/A mode, and restarts playback.
 */
static void sb_rec_stop(void)
{
   if (!sb_recording)
      return;

   sb_stop();

   sb_recording = FALSE;
   sb_16bit = (sb_dsp_ver >= 0x400);
   _sound_dma = (sb_16bit) ? sb_dma16 : sb_dma8;

   _mix_some_samples(sb_buf[0], _dos_ds, TRUE);
   _mix_some_samples(sb_buf[1], _dos_ds, TRUE);

   sb_start();
}



/* sb_rec_read:
 *  Retrieves the just recorded DMA buffer, if there is one.
 */
static int sb_rec_read(void *buf)
{
   if (!sb_recording)
      return 0;

   if (sb_bufnum == sb_recbufnum)
      return 0;

   dosmemget(sb_buf[sb_recbufnum], sb_dma_size, buf);
   sb_recbufnum = 1-sb_recbufnum;

   return 1;
}

END_OF_STATIC_FUNCTION(sb_rec_read);



/* sb_midi_interrupt:
 *  Interrupt handler for the SB MIDI input.
 */
static int sb_midi_interrupt(void)
{
   int c = sb_read_dsp();

   if ((c >= 0) && (midi_recorder))
      midi_recorder(c);

   _eoi(_sound_irq);
   return 0;
}

END_OF_STATIC_FUNCTION(sb_midi_interrupt);



/* sb_midi_output:
 *  Writes a byte to the SB midi interface.
 */
static void sb_midi_output(int data)
{
   sb_write_dsp(data);
}

END_OF_STATIC_FUNCTION(sb_midi_output);



/* sb_midi_detect:
 *  Detection routine for the SB MIDI interface.
 */
static int sb_midi_detect(int input)
{
   int ret;

   if ((input) && (sb_midi_out_mode))
      return TRUE;

   sb_detecting_midi = TRUE;

   ret = sb_detect(FALSE);

   sb_detecting_midi = FALSE;

   return ret;
}



/* sb_midi_init:
 *  Initialises the SB midi interface, returning zero on success.
 */
static int sb_midi_init(int input, int voices)
{
   if ((sb_in_use) && (!sb_midi_out_mode)) {
      ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Can't use SB MIDI interface and DSP at the same time"));
      return -1;
   }

   sb_dsp_ver = -1;
   sb_lock_mem();

   uszprintf(sb_midi_desc, sizeof(sb_midi_desc), get_config_text("Sound Blaster MIDI interface on port %X"), _sound_port);
   midi_sb_out.desc = sb_midi_desc;

   if (input) {
      _enable_irq(_sound_irq);
      _install_irq(sb_int, sb_midi_interrupt);
      sb_midi_in_mode = TRUE;
   }
   else
      sb_midi_out_mode = TRUE;

   sb_write_dsp(0x35);

   sb_in_use = TRUE;
   return 0;
}



/* sb_midi_exit:
 *  Resets the SB midi interface when we are finished.
 */
static void sb_midi_exit(int input)
{
   if (input) {
      _remove_irq(sb_int);
      _restore_irq(_sound_irq);
      sb_midi_in_mode = FALSE;
   }
   else
      sb_midi_out_mode = FALSE;

   if ((!sb_midi_in_mode) && (!sb_midi_out_mode)) {
      _sb_reset_dsp(1);
      sb_in_use = FALSE;
   }
}



/* sb_lock_mem:
 *  Locks all the memory touched by parts of the SB code that are executed
 *  in an interrupt context.
 */
static void sb_lock_mem(void)
{
   extern void _mpu_poll_end(void);

   LOCK_VARIABLE(digi_sb10);
   LOCK_VARIABLE(digi_sb15);
   LOCK_VARIABLE(digi_sb20);
   LOCK_VARIABLE(digi_sbpro);
   LOCK_VARIABLE(digi_sb16);
   LOCK_VARIABLE(midi_sb_out);
   LOCK_VARIABLE(_sound_freq);
   LOCK_VARIABLE(_sound_port);
   LOCK_VARIABLE(_sound_dma);
   LOCK_VARIABLE(_sound_irq);
   LOCK_VARIABLE(sb_int);
   LOCK_VARIABLE(sb_in_use);
   LOCK_VARIABLE(sb_recording);
   LOCK_VARIABLE(sb_16bit);
   LOCK_VARIABLE(sb_midi_out_mode);
   LOCK_VARIABLE(sb_midi_in_mode);
   LOCK_VARIABLE(sb_dsp_ver);
   LOCK_VARIABLE(sb_hw_dsp_ver);
   LOCK_VARIABLE(sb_dma_size);
   LOCK_VARIABLE(sb_dma_mix_size);
   LOCK_VARIABLE(sb_sel);
   LOCK_VARIABLE(sb_buf);
   LOCK_VARIABLE(sb_bufnum);
   LOCK_VARIABLE(sb_recbufnum);
   LOCK_VARIABLE(sb_dma_count);
   LOCK_VARIABLE(sb_semaphore);
   LOCK_VARIABLE(sb_recording);
   LOCK_FUNCTION(sb_play_buffer);
   LOCK_FUNCTION(sb_interrupt);
   LOCK_FUNCTION(sb_rec_read);
   LOCK_FUNCTION(sb_midi_interrupt);
   LOCK_FUNCTION(sb_midi_output);
   LOCK_FUNCTION(sb_record_buffer);
   LOCK_FUNCTION(_mpu_poll);

   _dma_lock_mem();
}