File: sbdrv.c

package info (click to toggle)
atari800 0.9.8a-2
  • links: PTS
  • area: contrib
  • in suites: potato, slink
  • size: 1,844 kB
  • ctags: 3,008
  • sloc: ansic: 29,881; asm: 2,142; makefile: 78; sh: 8
file content (1288 lines) | stat: -rw-r--r-- 31,390 bytes parent folder | download
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
/*****************************************************************************/
/*                                                                           */
/* Module:    SBDRV                                                          */
/* Purpose:   Sound Blaster DAC DMA Driver V1.2                              */
/* Author(s): Ron Fries, Neil Bradley and Bradford Mott                      */
/*                                                                           */
/* 02/20/97 - Initial Release                                                */
/*                                                                           */
/* 08/19/97 - V1.1 - Corrected problem with the auto-detect of older SB      */
/*            cards and problem with DSP shutdown which left the auto-init   */
/*            mode active.  Required creating a function to reset the DSP.   */
/*            Also, added checks on the BLASTER settings to verify they      */
/*            are possible values for either SB or SB compatibles.           */
/*            Added several helpful information/error messages.  These can   */
/*            be disabled by removing the SBDRV_SHOW_ERR definition.         */
/*                                                                           */
/* 12/24/97 - V1.2 - Added support for DJGPP (by Bradford Mott).             */
/*                                                                           */
/* 05/01/98 - V1.3 - Added timeout handling and ResetDSP (by Robert Golias)  */
/*                                                                           */
/*****************************************************************************/
/*                                                                           */
/*                 License Information and Copyright Notice                  */
/*                 ========================================                  */
/*                                                                           */
/* SBDrv is Copyright(c) 1997-1998 by Ron Fries, Neil Bradley and            */
/*       Bradford Mott                                                       */
/*                                                                           */
/* This library is free software; you can redistribute it and/or modify it   */
/* under the terms of version 2 of the GNU Library General Public License    */
/* as published by the Free Software Foundation.                             */
/*                                                                           */
/* This library 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 Library */
/* General Public License for more details.                                  */
/* To obtain a copy of the GNU Library General Public License, write to the  */
/* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   */
/*                                                                           */
/* Any permitted reproduction of these routines, in whole or in part, must   */
/* bear this legend.                                                         */
/*                                                                           */
/*****************************************************************************/

#ifdef DJGPP
#include <go32.h>
#include <dpmi.h>
#include <sys/movedata.h>
#endif							/*  */

#include <dos.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
#include "sbdrv.h"

#define DSP_RESET         0x06
#define DSP_READ          0x0a
#define DSP_WRITE         0x0c
#define DSP_ACK           0x0e

#define DMA_BASE          0x00
#define DMA_COUNT         0x01
#define DMA_MASK          0x0a
#define DMA_MODE          0x0b
#define DMA_FF            0x0c

#define MASTER_VOLUME     0x22
#define LINE_VOLUME       0x2e
#define FM_VOLUME         0x26

/* declare local global variables */
#ifdef DJGPP
static int theDOSBufferSegment;

static int theDOSBufferSelector;

#endif							/*  */
static uint8 *Sb_buffer;

static uint16 Sb_buf_size = 200;

static uint16 Sb_offset;

static uint16 Playback_freq;

static uint8 Sb_init = 0;

static uint8 Count_low;

static uint8 Count_high;


static uint16 IOaddr = 0x220;

static uint16 Irq = 7;

static uint16 Dma = 1;

static uint8 DMAmode = AUTO_DMA;

/*static */ uint8 DMAcount;

static void (*FillBuffer) (uint8 * buf, uint16 buf_size);


#ifdef DJGPP
static _go32_dpmi_seginfo OldIntVectInfo;

static _go32_dpmi_seginfo NewIntVectInfo;

#else							/*  */
static void (__interrupt * OldIntVect) (void);

#endif							/*  */

/* function prototypes */
#ifdef DJGPP
static void newIntVect(void);

#else							/*  */
static void interrupt newIntVect(void);

#endif							/*  */

static void setNewIntVect(uint16 irq);

static void setOldIntVect(uint16 irq);

static void dsp_out(uint16 port, uint8 val);

static uint8 hextodec(char c);

static void logErr(char *st);

static uint8 getBlasterEnv(void);

static uint8 dsp_in(uint16 port);


/*****************************************************************************/
/*                                                                           */
/* Module:  setNewIntVect                                                    */
/* Purpose: To set the specified interrupt vector to the sound output        */
/*          processing interrupt.                                            */
/* Author:  Ron Fries                                                        */
/* Date:    January 1, 1997                                                  */
/*                                                                           */
/*****************************************************************************/

static void setNewIntVect(uint16 irq)
{

	if (irq > 7) {

#ifdef DJGPP
		_go32_dpmi_get_protected_mode_interrupt_vector(irq + 0x68,
													   &OldIntVectInfo);

		NewIntVectInfo.pm_selector = _my_cs();

		NewIntVectInfo.pm_offset = (int) newIntVect;

		_go32_dpmi_allocate_iret_wrapper(&NewIntVectInfo);

		_go32_dpmi_set_protected_mode_interrupt_vector(irq + 0x68,
													   &NewIntVectInfo);

#else							/*  */
		OldIntVect = _dos_getvect(irq + 0x68);

		_dos_setvect(irq + 0x68, newIntVect);

#endif							/*  */
	}

	else {

#ifdef DJGPP
		_go32_dpmi_get_protected_mode_interrupt_vector(irq + 0x08,
													   &OldIntVectInfo);

		NewIntVectInfo.pm_selector = _my_cs();

		NewIntVectInfo.pm_offset = (int) newIntVect;

		_go32_dpmi_allocate_iret_wrapper(&NewIntVectInfo);

		_go32_dpmi_set_protected_mode_interrupt_vector(irq + 0x08,
													   &NewIntVectInfo);

#else							/*  */
		OldIntVect = _dos_getvect(irq + 0x08);

		_dos_setvect(irq + 0x08, newIntVect);

#endif							/*  */
	}

}



/*****************************************************************************/
/*                                                                           */
/* Module:  setOldIntVect                                                    */
/* Purpose: To restore the original vector                                   */
/* Author:  Ron Fries                                                        */
/* Date:    January 1, 1997                                                  */
/*                                                                           */
/*****************************************************************************/

static void setOldIntVect(uint16 irq)
{

	if (irq > 7) {

#ifdef DJGPP
		_go32_dpmi_set_protected_mode_interrupt_vector(irq + 0x68,
													   &OldIntVectInfo);

		_go32_dpmi_free_iret_wrapper(&NewIntVectInfo);

#else							/*  */
		_dos_setvect(irq + 0x68, OldIntVect);

#endif							/*  */
	}

	else {

#ifdef DJGPP
		_go32_dpmi_set_protected_mode_interrupt_vector(irq + 0x08,
													   &OldIntVectInfo);

		_go32_dpmi_free_iret_wrapper(&NewIntVectInfo);

#else							/*  */
		_dos_setvect(irq + 0x08, OldIntVect);

#endif							/*  */
	}

}



/*****************************************************************************/
/*                                                                           */
/* Module:  dsp_out                                                          */
/* Purpose: To send a byte to the SB's DSP                                   */
/* Author:  Ron Fries                                                        */
/* Date:    September 10, 1996                                               */
/*                                                                           */
/*****************************************************************************/

static void dsp_out(uint16 port, uint8 val)
{
	uint32 timeout=60000;
	/* wait for buffer to be free */
	while ((timeout--) && (inp(IOaddr + DSP_WRITE) & 0x80)) {

		/* do nothing */
	}


	/* transmit the next byte */
	outp(port, val);

}



/*****************************************************************************/
/*                                                                           */
/* Module:  dsp_in                                                           */
/* Purpose: To read a byte from the SB's DSP                                 */
/* Author:  Ron Fries                                                        */
/* Date:    January 26, 1997                                                 */
/*                                                                           */
/*****************************************************************************/

static uint8 dsp_in(uint16 port)
{

	uint16 x = 10000;			/* set timeout */


	/* wait for buffer to be free */
	while (((inp(IOaddr + 0x0E) & 0x80) == 0) && (x > 0)) {

		/* decrement the timeout */
		x--;

	}


	if (x > 0) {

		/* read the data byte */
		return (inp(port));

	}

	else {

		return (0);

	}

}



/*****************************************************************************/
/*                                                                           */
/* Module:  hextodec                                                         */
/* Purpose: Convert the input character to hex                               */
/* Author:  Ron Fries                                                        */
/* Date:    September 10, 1996                                               */
/*                                                                           */
/*****************************************************************************/

uint8 hextodec(char c)
{

	uint8 retval = 0;


	c = toupper(c);


	if ((c >= '0') && (c <= '9')) {

		retval = c - '0';

	}

	else if ((c >= 'A') && (c <= 'F')) {

		retval = c - 'A' + 10;

	}


	return (retval);

}



/*****************************************************************************/
/*                                                                           */
/* Module:  logErr                                                           */
/* Purpose: Displays an error message.                                       */
/* Author:  Ron Fries                                                        */
/* Date:    September 24, 1996                                               */
/*                                                                           */
/*****************************************************************************/

static void logErr(char *st)
{

#ifdef SBDRV_SHOW_ERR
	printf("%s", st);

#endif							/*  */
}


/*****************************************************************************/
/*                                                                           */
/* Module:  getBlasterEnv                                                    */
/* Purpose: Read the BLASTER environment variable and set the local globals  */
/* Author:  Ron Fries                                                        */
/* Date:    September 10, 1996                                               */
/*                                                                           */
/*****************************************************************************/

static uint8 getBlasterEnv(void)
{

	char *env;

	char *ptr;

	uint16 count = 0;


	env = strupr(getenv("BLASTER"));


	/* if the environment variable exists */
	if (env) {

		/* search for the address setting */
		ptr = strchr(env, 'A');

		if (ptr) {

			/* if valid, read and convert the IO address */
			IOaddr = (hextodec(ptr[1]) << 8) +
				(hextodec(ptr[2]) << 4) +
				(hextodec(ptr[3]));


			/* verify the IO address is one of the possible SB settings */
			switch (IOaddr) {

			case 0x210:

			case 0x220:

			case 0x230:

			case 0x240:

			case 0x250:

			case 0x260:

			case 0x280:

			case 0x2A0:

			case 0x2C0:

			case 0x2E0:

				/* IO address OK so indicate one more valid item found */
				count++;

				break;


			default:

				logErr("Invalid Sound Blaster I/O address specified.\n");

				logErr("Possible values are:  ");

				logErr("210, 220, 230, 240, 250, 260, 280, 2A0, 2C0, 2E0.\n");

			}

		}

		else {

			logErr("Unable to read Sound Blaster I/O address.\n");

		}


		/* search for the IRQ setting */
		ptr = strchr(env, 'I');

		if (ptr) {

			/* if valid, read and convert the IRQ */
			/* if the IRQ has two digits */
			if ((ptr[1] == '1') && ((ptr[2] >= '0') && (ptr[2] <= '5'))) {

				/* then convert accordingly (using decimal) */
				Irq = hextodec(ptr[1]) * 10 + hextodec(ptr[2]);

			}

			else {

				/* else convert as a single hex digit */
				Irq = hextodec(ptr[1]);

			}


			/* verify the IRQ setting is one of the possible SB settings */
			switch (Irq) {

			case 2:			/* two is actually the interrupt cascade for IRQs > 7 */

				/* IRQ nine is the cascase for 2 */
				Irq = 9;


				/* IRQ OK so indicate one more valid item found */
				count++;

				break;


			case 3:

			case 4:

			case 5:

			case 7:

			case 9:

			case 10:

			case 11:

			case 12:

			case 15:


				/* IRQ OK so indicate one more valid item found */
				count++;

				break;


			default:

				logErr("Invalid Sound Blaster IRQ specified.\n");

				logErr("Possible values are:  ");

				logErr("2, 3, 4, 5, 7, 9, 10, 11, 12, 15.\n");

			}

		}

		else {

			logErr("Unable to read Sound Blaster IRQ.\n");

		}


		/* search for the DMA setting */
		ptr = strchr(env, 'D');

		if (ptr) {

			/* if valid, read and convert the DMA */
			Dma = hextodec(ptr[1]);


			/* verify the DMA setting is one of the possible 8-bit SB settings */
			switch (Dma) {

			case 0:

			case 1:

			case 3:

				/* DMA OK so indicate one more valid item found */
				count++;

				break;


			default:

				logErr("Invalid Sound Blaster 8-bit DMA specified.\n");

				logErr("Possible values are:  ");

				logErr("0, 1, 3.\n");

			}

		}

		else {

			logErr("Unable to read Sound Blaster DMA setting.\n");

		}

	}

	else {

		logErr("BLASTER enviroment variable not configured.");

	}


	return (count != 3);

}



/*****************************************************************************/
/*                                                                           */
/* Module:  low_malloc                                                       */
/* Purpose: To allocate memory in the first 640K of memory                   */
/* Author:  Neil Bradley                                                     */
/* Date:    December 16, 1996                                                */
/*                                                                           */
/*****************************************************************************/

#ifdef __WATCOMC__
void dos_memalloc(unsigned short int para, unsigned short int *seg, unsigned short int *sel);

#pragma  aux dos_memalloc = \
"push  ecx" \
"push  edx" \
"mov   ax, 0100h" \
"int   31h" \
"pop   ebx" \
"mov   [ebx], dx" \
"pop   ebx" \
"mov   [ebx], ax" \
parm[bx][ecx][edx] \
modify[ax ebx ecx edx];


void dos_memfree(short int sel);

#pragma  aux dos_memfree =  \
"mov   ax, 0101h" \
"int   31h" \
parm[dx] \
modify[ax dx];


void *low_malloc(int size)
{

	unsigned short int seg;

	unsigned short int i = 0;


	dos_memalloc((size >> 4) + 1, &seg, &i);

	return ((char *) (seg << 4));

}

#endif							/*  */


/*****************************************************************************/
/*                                                                           */
/* Module:  Set_master_volume                                                */
/* Purpose: To set the Sound Blaster's master volume                         */
/* Author:  Neil Bradley                                                     */
/* Date:    January 1, 1997                                                  */
/*                                                                           */
/*****************************************************************************/

void Set_master_volume(uint8 left, uint8 right)
{

	/* if the SB was initialized */
	if (Sb_init) {

		outp(IOaddr + 0x04, MASTER_VOLUME);

		outp(IOaddr + 0x05, ((left & 0xf) << 4) + (right & 0x0f));

	}

}



/*****************************************************************************/
/*                                                                           */
/* Module:  Set_line_volume                                                  */
/* Purpose: To set the Sound Blaster's line level volume                     */
/* Author:  Neil Bradley                                                     */
/* Date:    January 1, 1997                                                  */
/*                                                                           */
/*****************************************************************************/

void Set_line_volume(uint8 left, uint8 right)
{

	/* if the SB was initialized */
	if (Sb_init) {

		outp(IOaddr + 0x04, LINE_VOLUME);

		outp(IOaddr + 0x05, ((left & 0xf) << 4) + (right & 0x0f));

	}

}



/*****************************************************************************/
/*                                                                           */
/* Module:  Set_FM_volume                                                    */
/* Purpose: To set the Sound Blaster's FM volume                             */
/* Author:  Neil Bradley                                                     */
/* Date:    January 1, 1997                                                  */
/*                                                                           */
/*****************************************************************************/

void Set_FM_volume(uint8 left, uint8 right)
{

	/* if the SB was initialized */
	if (Sb_init) {

		outp(IOaddr + 0x04, FM_VOLUME);

		outp(IOaddr + 0x05, ((left & 0xf) << 4) + (right & 0x0f));

	}

}



/*****************************************************************************/
/*                                                                           */
/* Module:  ResetDSP                                                         */
/* Purpose: To reset the SB DSP.  Returns a value of zero if unsuccessful.   */
/*          This function requires as input the SB base port address.        */
/* Author:  Ron Fries                                                        */
/* Date:    August 5, 1997                                                   */
/*                                                                           */
/*****************************************************************************/

uint8 ResetDSP(uint16 ioaddr)
{

	uint8 x;

	uint16 y;


	/* assume the init was not successful */
	Sb_init = 0;


	/* send a DSP reset to the SB */
	outp(ioaddr + DSP_RESET, 1);


	/* wait a few microsec */
	x = inp(ioaddr + DSP_RESET);

	x = inp(ioaddr + DSP_RESET);

	x = inp(ioaddr + DSP_RESET);

	x = inp(ioaddr + DSP_RESET);


	/* clear the DSP reset */
	outp(ioaddr + DSP_RESET, 0);


	/* wait a bit until the SB indicates good status */
	y = 0;


	do {

		x = inp(ioaddr + DSP_READ);

		y++;

	} while ((y < 1000) && (x != 0xaa));


	/* if we were able to successfully reset the SB */
	if (x == 0xaa) {

		/* turn on speaker */
		dsp_out(ioaddr + DSP_WRITE, 0xd1);


		/* read to make sure DSP register is clear */
		dsp_in(ioaddr + DSP_READ);


		/* set time constant */
		dsp_out(ioaddr + DSP_WRITE, 0x40);

		dsp_out(ioaddr + DSP_WRITE,
				(unsigned char) (256 - 1000000L / Playback_freq));


		/* indicate successful initialization */
		Sb_init = 1;

	}


	return (Sb_init);

}



/*****************************************************************************/
/*                                                                           */
/* Module:  OpenSB                                                           */
/* Purpose: To reset the SB and prepare all buffers and other global         */
/*          global variables for sound output.  Allows the user to select    */
/*          the playback frequency, number of buffers, and size of each      */
/*          buffer.  Returns a value of zero if unsuccessful.                */
/* Author:  Ron Fries                                                        */
/* Date:    January 1, 1997                                                  */
/*                                                                           */
/*****************************************************************************/

uint8 OpenSB(uint16 playback_freq, uint16 buffer_size)
{

	/* initialize local globals */
	if (buffer_size > 0) {

		Sb_buf_size = buffer_size;

	}


	Playback_freq = playback_freq;


	/* assume the init was not successful */
	Sb_init = 0;


	/* attempt to read the Blaster Environment Variable */
	if (getBlasterEnv() == 0) {

		/* if the DSP could be successfully reset */
		if (ResetDSP(IOaddr) != 0) {

			/* setup the DSP interrupt service routine */
			setNewIntVect(Irq);


			/* Enable the interrupt used */
			if (Irq > 7) {

				outp(0xa1, inp(0xa1) & (~(1 << (Irq - 8))));

			}

			else {

				outp(0x21, inp(0x21) & (~(1 << Irq)));

			}


			/* make sure interrupts are enabled */
			_enable();


			/* create a buffer to hold the data */
#ifdef __WATCOMC__
			Sb_buffer = low_malloc(Sb_buf_size * 2);

#elif defined(DJGPP)
			Sb_buffer = (uint8 *) malloc(Sb_buf_size * 2);

			theDOSBufferSegment = __dpmi_allocate_dos_memory((Sb_buf_size * 2 + 15) >> 4,
												  &theDOSBufferSelector);

#else							/*  */
			Sb_buffer = malloc(Sb_buf_size * 2);

#endif							/*  */

			/* if we were unable to successfully allocate the buffer */
#ifdef DJGPP
			if ((Sb_buffer == 0) || (theDOSBufferSegment == -1))
#else							/*  */
			if (Sb_buffer == 0)
#endif							/*  */
			{

				logErr("Unable to allocate buffer for audio output.\n");


				/* close the SB */
				CloseSB();

			}

		}

		else {

			logErr("Unable to initialize the Sound Card.\n");

		}


	}


	return (Sb_init);

}



/*****************************************************************************/
/*                                                                           */
/* Module:  CloseSB                                                          */
/* Purpose: Closes the SB and disables the interrupts.                       */
/* Author:  Ron Fries                                                        */
/* Date:    January 1, 1997                                                  */
/*                                                                           */
/*****************************************************************************/

void CloseSB(void)
{

#ifdef __WATCOMC__
	uint32 addr;

#endif							/*  */

	/* if the SB was initialized */
	if (Sb_init) {

		/* stop all DMA transfer */
		Stop_audio_output();
		ResetDSP(IOaddr); /*GOLDA CHANGED*/


		/* turn the speaker off */
		dsp_out(IOaddr + DSP_WRITE, 0xd3);


		/* indicate SB no longer active */
		Sb_init = 0;


		/* Disable the interrupt used */
		if (Irq > 7) {

			outp(0xa1, inp(0xa1) | (1 << (Irq - 8)));

		}

		else {

			outp(0x21, inp(0x21) | (1 << Irq));

		}


		/* restore the original interrupt routine */
		setOldIntVect(Irq);


		/* free any memory that had been allocated */
		if (Sb_buffer != 0) {

#ifdef __WATCOMC__
			addr = (uint32) Sb_buffer;

			dos_memfree((uint16) (addr >> 4));

#elif defined(DJGPP)
			free(Sb_buffer);

			__dpmi_free_dos_memory(theDOSBufferSelector);

#else							/*  */
			free(Sb_buffer);

#endif							/*  */
		}

	}
}



/*****************************************************************************/
/*                                                                           */
/* Module:  Stop_audio_output                                                */
/* Purpose: Stops the SB's DMA transfer.                                     */
/* Author:  Ron Fries                                                        */
/* Date:    January 17, 1997                                                 */
/*                                                                           */
/*****************************************************************************/

void Stop_audio_output(void)
{

	/* stop any transfer that may be in progress */

	/* if the SB was initialized */
	if (Sb_init) {

		/* halt DMA */
		dsp_out(IOaddr + DSP_WRITE, 0xd0);


		/* exit DMA operation */
		dsp_out(IOaddr + DSP_WRITE, 0xda);


		/* halt DMA */
		dsp_out(IOaddr + DSP_WRITE, 0xd0);

	}

}



/*****************************************************************************/
/*                                                                           */
/* Module:  Start_audio_output                                               */
/* Purpose: Fills all configured buffers and outputs the first.              */
/* Author:  Ron Fries                                                        */
/* Date:    February 20, 1997                                                */
/*                                                                           */
/*****************************************************************************/

uint8 Start_audio_output(uint8 dma_mode,
						 void (*fillBuffer) (uint8 * buf, uint16 n))
{

	uint8 ret_val = 1;

	static uint8 pagePort[8] =
	{0x87, 0x83, 0x81, 0x82};

	uint8 offset_low;

	uint8 offset_high;

	uint8 page_no;

	uint8 count_low;

	uint8 count_high;

	uint32 addr;

	clock_t start_time;


	/* if the SB initialized properly */
	if (Sb_init) {

		/* set the fill buffer routine */
		FillBuffer = fillBuffer;


		/* keep track of the DMA selection */
		DMAmode = dma_mode;


		/* stop any transfer that may be in progress */
		Stop_audio_output();


		/* fill the buffer */
		FillBuffer(Sb_buffer, Sb_buf_size * 2);


#ifdef DJGPP
		/* Copy data to DOS memory buffer */
		dosmemput(Sb_buffer, Sb_buf_size * 2, theDOSBufferSegment << 4);

#endif							/*  */

		/* calculate high, low and page addresses of buffer */
#ifdef __WATCOMC__
		addr = (uint32) Sb_buffer;

#elif defined(DJGPP)
		addr = ((uint32) theDOSBufferSegment) << 4;

#else							/*  */
		addr = ((uint32) FP_SEG(Sb_buffer) << 4) +
			(uint32) FP_OFF(Sb_buffer);

#endif							/*  */
		Sb_offset = (uint16) (addr & 0x0ffff);

		offset_low = (uint8) (addr & 0x0ff);

		offset_high = (uint8) ((addr >> 8) & 0x0ff);

		page_no = (uint8) (addr >> 16);


		count_low = (uint8) ((Sb_buf_size * 2) - 1) & 0x0ff;

		count_high = (uint8) (((Sb_buf_size * 2) - 1) >> 8) & 0x0ff;


		/* program the DMAC for output transfer */
		outp(DMA_MASK, 0x04 | Dma);

		outp(DMA_FF, 0);


		/* select auto-initialize DMA mode */
		outp(DMA_MODE, 0x58 | Dma);

		outp(DMA_BASE + (Dma << 1), offset_low);

		outp(DMA_BASE + (Dma << 1), offset_high);

		outp(pagePort[Dma], page_no);

		outp(DMA_COUNT + (Dma << 1), count_low);

		outp(DMA_COUNT + (Dma << 1), count_high);

		outp(DMA_MASK, Dma);


		/* calculate the high/low buffer size counts */
		Count_low = (uint8) (Sb_buf_size - 1) & 0x0ff;

		Count_high = (uint8) ((Sb_buf_size - 1) >> 8) & 0x0ff;


		if (DMAmode == STANDARD_DMA) {

			/* start the standard DMA transfer */
			dsp_out(IOaddr + DSP_WRITE, 0x14);

			dsp_out(IOaddr + DSP_WRITE, Count_low);

			dsp_out(IOaddr + DSP_WRITE, Count_high);

		}

		else {

			/* reset the DMA counter */
			DMAcount = 0;

			ResetDSP(IOaddr); /*GOLDA CHANGED*/

			/* set the auto-initialize buffer size */
			dsp_out(IOaddr + DSP_WRITE, 0x48);

			dsp_out(IOaddr + DSP_WRITE, Count_low);

			dsp_out(IOaddr + DSP_WRITE, Count_high);


			/* and start the auto-initialize DMA transfer */
			dsp_out(IOaddr + DSP_WRITE, 0x1c);


			start_time = clock();


			/* Delay for a bit and wait for DMAcount to change. */
			/* Wait for the DMA to be called twice to make sure */
			/* auto-init mode is working properly. */
			while ((clock() - start_time < (int) (CLK_TCK / 2)) && (DMAcount < 2)) {

				/* This routine will wait for up to 1/2 second for DMAcount */
				/* to change.  The value in CLK_TCK is the number of times */
				/* the clock will tick in one second. */
			}

			/* if the auto-init DMA is not active */
			if (DMAcount < 2) {

				/* Reset the SB DSP */
				ResetDSP(IOaddr);


				/* then try again with STANDARD_DMA */
				Start_audio_output(STANDARD_DMA, fillBuffer);

			}

		}


		ret_val = 0;

	}


	return (ret_val);

}



/*****************************************************************************/
/*                                                                           */
/* Module:  newIntVect                                                       */
/* Purpose: The interrupt vector to handle the DAC DMAC completed interrupt  */
/*          Sends the next buffer to the SB and re-fills the current buffer. */
/* Author:  Ron Fries                                                        */
/* Date:    January 1, 1997                                                  */
/*                                                                           */
/*****************************************************************************/
#ifdef DJGPP
static void newIntVect(void)
#else							/*  */
static void interrupt newIntVect(void)
#endif							/*  */
{

	uint16 addr;


	if (DMAmode == STANDARD_DMA) {

		/* restart standard DMA transfer */
		dsp_out(IOaddr + DSP_WRITE, 0x14);

		dsp_out(IOaddr + DSP_WRITE, Count_low);

		dsp_out(IOaddr + DSP_WRITE, Count_high);

	}


	DMAcount++;


	/* acknowledge the DSP interrupt */
	inp(IOaddr + DSP_ACK);


	/* determine the current playback position */
	addr = inp(DMA_BASE + (Dma << 1));	/* get low byte ptr */

	addr |= inp(DMA_BASE + (Dma << 1)) << 8;	/* and high byte ptr */


	addr -= Sb_offset;			/* subtract the offset */


	/* if we're currently playing the first half of the buffer */
	if (addr < Sb_buf_size) {

		/* reload the second half of the buffer */
		FillBuffer(Sb_buffer + Sb_buf_size, Sb_buf_size);


#ifdef DJGPP
		/* Copy data to DOS memory buffer */
		dosmemput(Sb_buffer + Sb_buf_size, Sb_buf_size,
				  (theDOSBufferSegment << 4) + Sb_buf_size);

#endif							/*  */
	}

	else {

		/* else reload the first half of the buffer */
		FillBuffer(Sb_buffer, Sb_buf_size);


#ifdef DJGPP
		/* Copy data to DOS memory buffer */
		dosmemput(Sb_buffer, Sb_buf_size, theDOSBufferSegment << 4);

#endif							/*  */
	}


	/* indicate end of interrupt  */
	outp(0x20, 0x20);


	if (Irq > 7) {

		outp(0xa0, 0x20);

	}

}