forked from keendreams/keen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
id_sd.c
1742 lines (1532 loc) · 39.2 KB
/
id_sd.c
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
/* Keen Dreams Source Code
* Copyright (C) 2014 Javier M. Chavez
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//
// ID Engine
// ID_SD.c - Sound Manager
// v1.0d1
// By Jason Blochowiak
//
//
// This module handles dealing with generating sound on the appropriate
// hardware
//
// Depends on: User Mgr (for parm checking)
//
// Globals:
// For User Mgr:
// SoundSourcePresent - Sound Source thingie present?
// SoundBlasterPresent - SoundBlaster card present?
// AdLibPresent - AdLib card present?
// SoundMode - What device is used for sound effects
// (Use SM_SetSoundMode() to set)
// MusicMode - What device is used for music
// (Use SM_SetMusicMode() to set)
// For Cache Mgr:
// NeedsDigitized - load digitized sounds?
// NeedsMusic - load music?
//
#pragma hdrstop // Wierdo thing with MUSE
#include <dos.h>
#ifdef _MUSE_ // Will be defined in ID_Types.h
#include "ID_SD.h"
#else
#include "ID_HEADS.H"
#endif
#pragma hdrstop
#pragma warn -pia
#define SDL_SoundFinished() {SoundNumber = SoundPriority = 0;}
// Macros for SoundBlaster stuff
#define sbOut(n,b) outportb((n) + sbLocation,b)
#define sbIn(n) inportb((n) + sbLocation)
#define sbWriteDelay() while (sbIn(sbWriteStat) & 0x80);
#define sbReadDelay() while (sbIn(sbDataAvail) & 0x80);
// Macros for AdLib stuff
#define selreg(n) outportb(0x388,n)
#define writereg(n) outportb(0x389,n)
#define readstat() inportb(0x388)
//
// Stuff I need
//
// This table maps channel numbers to carrier and modulator op cells
static byte carriers[9] = { 3, 4, 5,11,12,13,19,20,21},
modifiers[9] = { 0, 1, 2, 8, 9,10,16,17,18};
static ActiveTrack *tracks[sqMaxTracks];
static word sqMode,sqFadeStep;
// Global variables
boolean LeaveDriveOn,
SoundSourcePresent,SoundBlasterPresent,AdLibPresent,
NeedsDigitized,NeedsMusic;
SDMode SoundMode;
SMMode MusicMode;
longword TimeCount;
word *SoundTable; // Really * _seg *SoundTable, but that don't work
boolean ssIsTandy;
word ssPort = 2;
// Internal variables
static boolean SD_Started;
static boolean TimerDone;
static word TimerVal,TimerDelay10,TimerDelay25,TimerDelay100;
static char *ParmStrings[] =
{
"noal",
"nosb",
"nodr",
"noss",
"sst",
"ss1",
"ss2",
"ss3",
nil
};
static void (*SoundUserHook)(void);
static word SoundNumber,SoundPriority;
static void interrupt (*t0OldService)(void);
static word t0CountTable[] = {2,2,2,2,10,10};
static long LocalTime;
// PC Sound variables
static byte pcLastSample,far *pcSound;
static longword pcLengthLeft;
static word pcSoundLookup[255];
// SoundBlaster variables
static boolean sbNoCheck,
sbSamplePlaying,
sbIsCompressed,sbCompFirst;
static byte sbOldIntMask = -1;
static byte huge *sbNextSegPtr;
static int sbLocation = -1,sbInterrupt = 7,sbIntVec = 0xf,
sbIntVectors[] = {-1,-1,0xa,0xb,-1,0xd,-1,0xf};
static longword sbNextSegLen;
static SampledSound huge *sbSamples;
static void interrupt (*sbOldIntHand)(void);
// SoundSource variables
static boolean ssNoCheck,
ssIsCompressed,ssCompFirst,
ssIsSlow;
static word ssControl,ssStatus,ssData,
ssHoldOver;
static byte ssOn,ssOff,
far *ssSample;
static longword ssLengthLeft;
// AdLib variables
static boolean alNoCheck;
static byte far *alSound;
static word alBlock;
static longword alLengthLeft;
// Sequencer variables
static boolean sqActive;
static word *sqTracks[sqMaxTracks];
static word alFXReg;
// Internal routines
///////////////////////////////////////////////////////////////////////////
//
// SDL_SetTimer0() - Sets system timer 0 to the specified speed
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_SetTimer0(word speed)
{
#ifndef TPROF // If using Borland's profiling, don't screw with the timer
outportb(0x43,0x36); // Change timer 0
outportb(0x40,speed);
outportb(0x40,speed >> 8);
#else
speed++; // Shut the compiler up
#endif
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SetIntsPerSec() - Uses SDL_SetTimer0() to set the number of
// interrupts generated by system timer 0 per second
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_SetIntsPerSec(word ints)
{
SDL_SetTimer0(1192755 / ints);
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_TimingService() - Used by SDL_InitDelay() to determine a timing
// value for the current system that we're running on
//
///////////////////////////////////////////////////////////////////////////
static void interrupt
SDL_TimingService(void)
{
TimerVal = _CX;
TimerDone++;
outportb(0x20,0x20); // Ack interrupt
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_InitDelay() - Sets up TimerDelay's for SDL_Delay()
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_InitDelay(void)
{
int i;
word timer,speed;
setvect(8,SDL_TimingService); // Set to my timer 0 ISR
SDL_SetIntsPerSec(1000); // Time 1ms
for (i = 0,timer = 0;i < 10;i++) // Do timing test 10 times
{
asm xor dx,dx // Zero DX
asm mov cx,0xffff // Put starting value in CX
asm mov [TimerDone],cx // TimerDone = false - 1
startloop:
asm or [TimerDone],0
asm jnz startloop // Make sure we're at the start
loop:
asm test [TimerDone],1 // See if TimerDone flag got hit
asm jnz done // Yep - drop out of the loop
asm loop loop
done:
if (0xffff - TimerVal > timer)
timer = 0xffff - TimerVal;
}
timer += timer / 2; // Use some slop
TimerDelay10 = timer / (1000 / 10);
TimerDelay25 = timer / (1000 / 25);
TimerDelay100 = timer / (1000 / 100);
SDL_SetTimer0(0); // Reset timer 0
setvect(8,t0OldService); // Set back to old ISR
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_Delay() - Delays the specified amount of time
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_Delay(word delay)
{
if (!delay)
return;
asm mov cx,[delay]
loop:
asm test [TimerDone],0 // Useless code - just for timing equivilency
asm jnz done
asm loop loop
done:;
}
//
// PC Sound code
//
///////////////////////////////////////////////////////////////////////////
//
// SDL_PCPlaySound() - Plays the specified sound on the PC speaker
//
///////////////////////////////////////////////////////////////////////////
#ifdef _MUSE_
void
#else
static void
#endif
SDL_PCPlaySound(PCSound far *sound)
{
asm pushf
asm cli
pcLastSample = -1;
pcLengthLeft = sound->common.length;
pcSound = sound->data;
asm popf
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_PCStopSound() - Stops the current sound playing on the PC Speaker
//
///////////////////////////////////////////////////////////////////////////
#ifdef _MUSE_
void
#else
static void
#endif
SDL_PCStopSound(void)
{
asm pushf
asm cli
(long)pcSound = 0;
asm in al,0x61 // Turn the speaker off
asm and al,0xfd // ~2
asm out 0x61,al
asm popf
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_PCService() - Handles playing the next sample in a PC sound
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_PCService(void)
{
byte s;
word t;
if (pcSound)
{
s = *pcSound++;
if (s != pcLastSample)
{
asm pushf
asm cli
pcLastSample = s;
if (s) // We have a frequency!
{
t = pcSoundLookup[s];
asm mov bx,[t]
asm mov al,0xb6 // Write to channel 2 (speaker) timer
asm out 43h,al
asm mov al,bl
asm out 42h,al // Low byte
asm mov al,bh
asm out 42h,al // High byte
asm in al,0x61 // Turn the speaker & gate on
asm or al,3
asm out 0x61,al
}
else // Time for some silence
{
asm in al,0x61 // Turn the speaker & gate off
asm and al,0xfc // ~3
asm out 0x61,al
}
asm popf
}
if (!(--pcLengthLeft))
{
SDL_PCStopSound();
SDL_SoundFinished();
}
}
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_ShutPC() - Turns off the pc speaker
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_ShutPC(void)
{
asm pushf
asm cli
asm in al,0x61 // Turn the speaker & gate off
asm and al,0xfc // ~3
asm out 0x61,al
asm popf
}
//
// SoundBlaster code
//
///////////////////////////////////////////////////////////////////////////
//
// SDL_SBStopSample() - Stops any active sampled sound and causes DMA
// requests from the SoundBlaster to cease
//
///////////////////////////////////////////////////////////////////////////
#ifdef _MUSE_
void
#else
static void
#endif
SDL_SBStopSample(void)
{
byte is;
if (sbSamplePlaying)
{
sbSamplePlaying = false;
sbWriteDelay();
sbOut(sbWriteCmd,0xd0); // Turn off DSP DMA
is = inportb(0x21); // Restore interrupt mask bit
if (sbOldIntMask & (1 << sbInterrupt))
is |= (1 << sbInterrupt);
else
is &= ~(1 << sbInterrupt);
outportb(0x21,is);
}
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SBPlaySeg() - Plays a chunk of sampled sound on the SoundBlaster
// Insures that the chunk doesn't cross a bank boundary, programs the DMA
// controller, and tells the SB to start doing DMA requests for DAC
//
///////////////////////////////////////////////////////////////////////////
static longword
SDL_SBPlaySeg(byte huge *data,longword length)
{
unsigned datapage;
longword dataofs,uselen;
uselen = length;
datapage = FP_SEG(data) >> 12;
dataofs = ( (FP_SEG(data)&0xfff)<<4 ) + FP_OFF(data);
if (dataofs>=0x10000)
{
datapage++;
dataofs-=0x10000;
}
if (dataofs + uselen > 0x10000)
uselen = 0x10000 - dataofs;
uselen--;
// Program the DMA controller
outportb(0x0a,5); // Mask off channel 1 DMA
outportb(0x0c,0); // Clear byte ptr F/F to lower byte
outportb(0x0b,0x49); // Set transfer mode for D/A conv
outportb(0x02,(byte)dataofs); // Give LSB of address
outportb(0x02,(byte)(dataofs >> 8)); // Give MSB of address
outportb(0x83,(byte)datapage); // Give page of address
outportb(0x03,(byte)uselen); // Give LSB of length
outportb(0x03,(byte)(uselen >> 8)); // Give MSB of length
outportb(0x0a,1); // Turn on channel 1 DMA
// Start playing the thing
sbWriteDelay();
sbOut(sbWriteCmd,0x14);
sbWriteDelay();
sbOut(sbWriteData,(byte)uselen);
sbWriteDelay();
sbOut(sbWriteData,(byte)(uselen >> 8));
return(uselen + 1);
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SBService() - Services the SoundBlaster DMA interrupt
//
///////////////////////////////////////////////////////////////////////////
static void interrupt
SDL_SBService(void)
{
longword used;
sbIn(sbDataAvail); // Ack interrupt to SB
if (sbNextSegPtr)
{
used = SDL_SBPlaySeg(sbNextSegPtr,sbNextSegLen);
if (sbNextSegLen <= used)
sbNextSegPtr = nil;
else
{
sbNextSegPtr += used;
sbNextSegLen -= used;
}
}
else
{
SDL_SBStopSample();
SDL_SoundFinished();
}
outportb(0x20,0x20); // Ack interrupt
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SBPlaySample() - Plays a sampled sound on the SoundBlaster. Sets up
// DMA to play the sound
//
///////////////////////////////////////////////////////////////////////////
#ifdef _MUSE_
void
#else
static void
#endif
SDL_SBPlaySample(SampledSound far *sample)
{
byte huge *data,
timevalue;
longword used;
SDL_SBStopSample();
data = (byte huge *)(sample->data);
timevalue = 256 - (1000000 / sample->hertz);
// DEBUG - deal with compressed sounds
// Set the SoundBlaster DAC time constant
sbWriteDelay();
sbOut(sbWriteCmd,0x40);
sbWriteDelay();
sbOut(sbWriteData,timevalue);
used = SDL_SBPlaySeg(data,sample->common.length);
if (sample->common.length <= used)
sbNextSegPtr = nil;
else
{
sbNextSegPtr = data + used;
sbNextSegLen = sample->common.length - used;
}
// Save old interrupt status and unmask ours
sbOldIntMask = inportb(0x21);
outportb(0x21,sbOldIntMask & ~(1 << sbInterrupt));
sbWriteDelay();
sbOut(sbWriteCmd,0xd4); // Make sure DSP DMA is enabled
sbSamplePlaying = true;
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_CheckSB() - Checks to see if a SoundBlaster resides at a
// particular I/O location
//
///////////////////////////////////////////////////////////////////////////
static boolean
SDL_CheckSB(int port)
{
int i;
sbLocation = port << 4; // Initialize stuff for later use
sbOut(sbReset,true); // Reset the SoundBlaster DSP
SDL_Delay(TimerDelay10); // Wait 4usec
sbOut(sbReset,false); // Turn off sb DSP reset
SDL_Delay(TimerDelay100); // Wait 100usec
for (i = 0;i < 100;i++)
{
if (sbIn(sbDataAvail) & 0x80) // If data is available...
{
if (sbIn(sbReadData) == 0xaa) // If it matches correct value
return(true);
else
{
sbLocation = -1; // Otherwise not a SoundBlaster
return(false);
}
}
}
sbLocation = -1; // Retry count exceeded - fail
return(false);
}
///////////////////////////////////////////////////////////////////////////
//
// Checks to see if a SoundBlaster is in the system. If the port passed is
// -1, then it scans through all possible I/O locations. If the port
// passed is 0, then it uses the default (2). If the port is >0, then
// it just passes it directly to SDL_CheckSB()
//
///////////////////////////////////////////////////////////////////////////
static boolean
SDL_DetectSoundBlaster(int port)
{
int i;
if (port == 0) // If user specifies default, use 2
port = 2;
if (port == -1)
{
if (SDL_CheckSB(2)) // Check default before scanning
return(true);
for (i = 1;i <= 6;i++) // Scan through possible SB locations
if (SDL_CheckSB(i)) // If found at this address,
return(true); // return success
return(false); // All addresses failed, return failure
}
else
return(SDL_CheckSB(port)); // User specified address or default
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_StartSB() - Turns on the SoundBlaster
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_StartSB(void)
{
sbOldIntHand = getvect(sbIntVec); // Get old interrupt handler
setvect(sbIntVec,SDL_SBService); // Set mine
sbWriteDelay();
sbOut(sbWriteCmd,0xd1); // Turn on DSP speaker
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_ShutSB() - Turns off the SoundBlaster
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_ShutSB(void)
{
SDL_SBStopSample();
setvect(sbIntVec,sbOldIntHand); // Set vector back
}
// Sound Source Code
///////////////////////////////////////////////////////////////////////////
//
// SDL_SSStopSample() - Stops a sample playing on the Sound Source
//
///////////////////////////////////////////////////////////////////////////
#ifdef _MUSE_
void
#else
static void
#endif
SDL_SSStopSample(void)
{
(long)ssSample = 0;
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SSService() - Handles playing the next sample on the Sound Source
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_SSService(void)
{
boolean gotit;
byte v;
while (ssSample)
{
asm mov dx,[ssStatus] // Check to see if FIFO is currently empty
asm in al,dx
asm test al,0x40
asm jnz done // Nope - don't push any more data out
gotit = false;
if (ssIsSlow && (ssHoldOver != (word)-1))
{
gotit = true;
v = ssHoldOver;
ssHoldOver = -1;
}
else
{
if (ssIsCompressed)
{
if (ssCompFirst)
{
// DEBUG - not written
}
// DEBUG - not written
}
else
{
v = *ssSample++;
gotit = true;
if (ssIsSlow)
ssHoldOver = v;
if (!(--ssLengthLeft))
{
(long)ssSample = 0;
SDL_SoundFinished();
}
}
}
if (gotit)
{
asm mov dx,[ssData] // Pump the value out
asm mov al,[v]
asm out dx,al
asm mov dx,[ssControl] // Pulse printer select
asm mov al,[ssOff]
asm out dx,al
asm push ax
asm pop ax
asm mov al,[ssOn]
asm out dx,al
asm push ax // Delay a short while
asm pop ax
asm push ax
asm pop ax
}
}
done:
; // Garbage for compiler
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SSPlaySample() - Plays the specified sample on the Sound Source
//
///////////////////////////////////////////////////////////////////////////
#ifdef _MUSE_
void
#else
static void
#endif
SDL_SSPlaySample(SampledSound far *sample)
{
asm pushf
asm cli
ssLengthLeft = sample->common.length;
ssSample = sample->data;
ssIsSlow = sample->hertz < 7000;
ssHoldOver = -1;
if (sample->bits < 8)
ssIsCompressed = ssCompFirst = true;
asm popf
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_StartSS() - Sets up for and turns on the Sound Source
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_StartSS(void)
{
if (ssPort == 3)
ssControl = 0x27a; // If using LPT3
else if (ssPort == 2)
ssControl = 0x37a; // If using LPT2
else
ssControl = 0x3be; // If using LPT1
ssStatus = ssControl - 1;
ssData = ssStatus - 1;
ssOn = 0x04;
if (ssIsTandy)
ssOff = 0x0e; // Tandy wierdness
else
ssOff = 0x0c; // For normal machines
outportb(ssControl,ssOn); // Enable SS
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_ShutSS() - Turns off the Sound Source
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_ShutSS(void)
{
outportb(ssControl,ssOff);
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_CheckSS() - Checks to see if a Sound Source is present at the
// location specified by the sound source variables
//
///////////////////////////////////////////////////////////////////////////
static boolean
SDL_CheckSS(void)
{
boolean present = false;
longword lasttime;
// Turn the Sound Source on and wait awhile (4 ticks)
SDL_StartSS();
lasttime = TimeCount;
while (TimeCount < lasttime + 4)
;
asm mov dx,[ssStatus] // Check to see if FIFO is currently empty
asm in al,dx
asm test al,0x40
asm jnz checkdone // Nope - Sound Source not here
asm mov cx,32 // Force FIFO overflow (FIFO is 16 bytes)
outloop:
asm mov dx,[ssData] // Pump a neutral value out
asm mov al,0x80
asm out dx,al
asm mov dx,[ssControl] // Pulse printer select
asm mov al,[ssOff]
asm out dx,al
asm push ax
asm pop ax
asm mov al,[ssOn]
asm out dx,al
asm push ax // Delay a short while before we do this again
asm pop ax
asm push ax
asm pop ax
asm loop outloop
asm mov dx,[ssStatus] // Is FIFO overflowed now?
asm in al,dx
asm test al,0x40
asm jz checkdone // Nope, still not - Sound Source not here
present = true; // Yes - it's here!
checkdone:
SDL_ShutSS();
return(present);
}
static boolean
SDL_DetectSoundSource(void)
{
for (ssPort = 1;ssPort <= 3;ssPort++)
if (SDL_CheckSS())
return(true);
return(false);
}
// AdLib Code
///////////////////////////////////////////////////////////////////////////
//
// alOut(n,b) - Puts b in AdLib card register n
//
///////////////////////////////////////////////////////////////////////////
void
alOut(byte n,byte b)
{
asm pushf
asm cli
asm mov dx,0x388
asm mov al,[n]
asm out dx,al
SDL_Delay(TimerDelay10);
asm mov dx,0x389
asm mov al,[b]
asm out dx,al
asm popf
SDL_Delay(TimerDelay25);
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SetInstrument() - Puts an instrument into a generator
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_SetInstrument(int which,Instrument *inst)
{
byte c,m;
// DEBUG - what about percussive instruments?
m = modifiers[which];
c = carriers[which];
tracks[which]->inst = *inst;
alOut(m + alChar,inst->mChar);
alOut(m + alScale,inst->mScale);
alOut(m + alAttack,inst->mAttack);
alOut(m + alSus,inst->mSus);
alOut(m + alWave,inst->mWave);
alOut(c + alChar,inst->cChar);
alOut(c + alScale,inst->cScale);
alOut(c + alAttack,inst->cAttack);
alOut(c + alSus,inst->cSus);
alOut(c + alWave,inst->cWave);
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_ALStopSound() - Turns off any sound effects playing through the
// AdLib card
//
///////////////////////////////////////////////////////////////////////////
#ifdef _MUSE_
void
#else
static void
#endif
SDL_ALStopSound(void)
{
asm pushf
asm cli
(long)alSound = 0;
alOut(alFreqH + 0,0);
asm popf
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_ALPlaySound() - Plays the specified sound on the AdLib card
//
///////////////////////////////////////////////////////////////////////////
#ifdef _MUSE_
void
#else
static void
#endif
SDL_ALPlaySound(AdLibSound far *sound)
{
byte c,m;
Instrument far *inst;
asm pushf
asm cli
SDL_ALStopSound();
alLengthLeft = sound->common.length;
alSound = sound->data;
alBlock = ((sound->block & 7) << 2) | 0x20;
inst = &sound->inst;
if (!(inst->mSus | inst->cSus))
Quit("SDL_ALPlaySound() - Seriously suspicious instrument");
m = modifiers[0];
c = carriers[0];
alOut(m + alChar,inst->mChar);
alOut(m + alScale,inst->mScale);
alOut(m + alAttack,inst->mAttack);
alOut(m + alSus,inst->mSus);
alOut(m + alWave,inst->mWave);
alOut(c + alChar,inst->cChar);
alOut(c + alScale,inst->cScale);
alOut(c + alAttack,inst->cAttack);
alOut(c + alSus,inst->cSus);
alOut(c + alWave,inst->cWave);
asm popf
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_ALSoundService() - Plays the next sample out through the AdLib card
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_ALSoundService(void)
{
byte s;
if (alSound)
{
s = *alSound++;
if (!s)
alOut(alFreqH + 0,0);
else
{
alOut(alFreqL + 0,s);
alOut(alFreqH + 0,alBlock);
}
if (!(--alLengthLeft))
{
(long)alSound = 0;