-
Notifications
You must be signed in to change notification settings - Fork 43
/
SOUNDLIB.ASM
345 lines (260 loc) · 7.29 KB
/
SOUNDLIB.ASM
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
; Catacomb Source Code
; Copyright (C) 1993-2014 Flat Rock Software
;
; 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.
.MODEL TPASCAL
; this is just a hacked up version of the C library...
;
;offsets into .SPK file at segment SPKRfile, offset 0
;
;sound records each take up 16 bytes, and start at $10, and continue to $3F0
snd_start equ 0
snd_priority equ 2
snd_samples equ 3
snd_name equ 4
.DATA
extrn SoundOn:BYTE, SoundData:DWORD
SoundMode dw ? ;0=SPKR, 1= adlib...
OldInt8 dd ? ;StartupSPK saves here, Shutdown restores
ExtraInts db ? ;number of PlaySPKR's to a regular int 8
Intcount db ? ;counter for extraints, call OldInt8 at 0
SndPtr dw ? ;Pointer to frequency of current sound
SndPriority db ? ;current sound's priority
.CODE
SPKactive dw 0 ;set non zero when started
;=======================================================================
;========
;
; StartupSound
;
; Sets up the new INT 8 ISR and various internal pointers.
; Assumes that the calling program has pointer SOUNDDATA to something
; meaningful...
;
;========
StartupSound PROC FAR
PUBLIC StartupSound
test cs:[SPKactive],0FFFFh ;see if library is active
jne @@started ;library was allready started
@@start:
call FAR PTR StopSound ;make sure nothing is playing
mov ax,0
mov es,ax
mov ax,es:[32] ;get the old intterupt 8 vector
mov word ptr [oldint8],ax
mov ax,es:[34]
mov word ptr [oldint8+2],ax
mov ax,1
mov [extraints],al ;the timer is still going at the
mov [intcount],al ;normal rate now
mov WORD PTR es:[32],OFFSET UpdateSPKR ;point int 8 to UpdateSPKR
mov WORD PTR es:[34],SEG UpdateSPKR
mov ax,WORD PTR [SoundData+2]
inc [SPKactive] ;sound routines are now active
@@started:
mov al,1
mov [soundon],al
ret
StartupSound ENDP
;=======================================================================
;========
;
; ShutdownSound
;
;========
ShutdownSound PROC FAR
PUBLIC ShutdownSound
mov ax,[SPKactive]
cmp ax,0
je @@done ;sound library wasn't started...
mov ax,0
mov es,ax
mov ax,word ptr [Oldint8] ;get the old intterupt 8 vector
mov es:[32],ax ;point int 8 back to BIOS
mov ax,word ptr [Oldint8+2]
mov es:[34],ax
mov al,36h ;tell the timer chip we are going to
out 43h,al ;change the speed of timer 0
mov al,0 ;system expects 0000 for rate
out 40h,al ;low
out 40h,al ;high
mov [SPKactive],0 ;sound routines are now inactive
in al,61h ;get peripheral (speaker) port value
and al,11111101b ;turn speaker off
out 61h,al
@@done:
ret
ShutdownSound ENDP
;=========================================================================
;===========
;
; PlaySound (soundnum)
;
; If the sound's priority is >= the current priority, SoundPtr, SndPriority,
; and the timer speed are changed
;
;===========
PlaySound1 PROC FAR playnum:WORD
PUBLIC PlaySound1
mov ax,WORD PTR [SoundData+2]
mov es,ax ;point es: to the spkr file
mov si,playnum ;index into the sound headers
shl si,1
shl si,1
shl si,1
shl si,1
mov al,es:[si+snd_Priority] ;priority table (one byte each)
cmp al,[SndPriority]
jb @@playdone ;current sound has higher priority
mov [SndPriority],al
mov ax,es:[si+snd_Start] ;offset in .SPK file
mov [SndPtr],ax ;store it in the sound playing table
mov bl,es:[si+snd_samples] ;samples / regular timer tick (1-255)
mov [extraints],bl ;an OldInt8 will be called after this
mov [intcount],bl ;many UpdateSPKR times have been called
cmp bl,1 ;sample rate of 0 or 1 = 0000 for timer
ja @@oktodiv
xor bx,bx
jmp @@settimer
@@oktodiv:
xor bh,bh
xor ax,ax
mov dx,1
div bx ;$10000 / samples = timer rate
mov bx,ax
@@settimer:
mov al,36h ;tell the timer chip we are going to
out 43h,al ;change the speed of timer 0
mov al,bl ;low byte of sample rate
out 40h,al
mov al,bh ;high byte of sample rate
out 40h,al
@@playdone:
ret
PlaySound1 ENDP
;======================================================================
;===========
;
; StopSound
;
;===========
StopSound PROC FAR
PUBLIC StopSound
xor ax,ax ;set to 0
mov [SndPtr],ax
mov [SndPriority],al
in al,61h ;get peripheral (speaker) port value
and al,11111101b ;turn speaker off
out 61h,al
ret
StopSound ENDP
;======================================================================
;========
;
; WaitendSound
; Just waits around until the current sound stops playing
;
;========
WaitendSound PROC FAR
PUBLIC WaitendSound
pushf
call FAR PTR UpdateSPKR ;in case a sound was just started and hasn't
;been hit by an INT yet
@@wait:
mov ax,[sndptr]
cmp ax,0 ;when the ptr is 0, nothing is on
jne @@wait
ret
WaitendSound ENDP
;=========================================================================
;========
;
; UpdateSPKR
; only called by interrupt $8!
;
;=========
UpdateSPKR PROC FAR
push ax
push bx
push cx
push si
push ds
push es
mov ax,SEG @DATA
mov ds,ax ;ds to this data segment
mov ax,WORD PTR [SoundData+2]
mov es,ax ;es to sound file
mov al,20h
out 20h,al ;we got the interrupt here
dec [intcount] ;see if it is time for a BIOS int 8...
jne @@dosounds
mov al,[extraints]
mov [intcount],al ;reset interrupt counter
pushf ;so the IRET from bios returns right
call [OldInt8] ;call the old BIOS timer routine
@@dosounds:
;
; play the speaker
;
mov si,[SndPtr]
cmp si,0
je @@nosound ;nothing playing
mov bx,es:[si]
inc [SndPtr]
inc [SndPtr]
cmp bx,0
je @@nosound ;a zero frequency is no sound, but don't stop
cmp bx,-1 ;a -1 frequency is end of sound
jne @@playfreq
xor ax,ax
mov [SndPtr],ax
mov [SndPriority],al
mov al,36h ;tell the timer chip we are going to
out 43h,al ;change the speed of timer 0
mov al,0 ;back to normal clock speed
out 40h,al
out 40h,al
inc al
mov [extraints],al ;one bios int / int8
mov [intcount],al
@@nosound:
in al,61h ;get peripheral (speaker) port value
and al,11111100b ;turn speaker off
out 61h,al
jmp @@doneplay
@@playfreq:
test [soundon],0FFh ;if soundon=0, don't play anything
je @@nosound
mov al,10110110b ;write to channel 2 (speaker) timer
out 43h,al
mov al,bl
out 42h,al ;low byte
mov al,bh
out 42h,al ;high byte
in al,61h ;get peripheral (speaker) port value
or al,00000011b ;turn speaker on to timer
out 61h,al
@@doneplay:
pop es
pop ds
pop si
pop cx
pop bx
pop ax
iret
UpdateSPKR ENDP
;==========================================================================
END