-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathichwav.asm
363 lines (296 loc) · 9.58 KB
/
ichwav.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
;
; DOS based .WAV player using AC'97 and codec interface.
;
;
.DOSSEG
.MODEL small, c, os_dos
.386
.CODE
INCLUDE constant.inc
INCLUDE ich2ac97.inc
extern filehandle:WORD
extern BDL_BUFFER:WORD
extern WAV_BUFFER1:WORD
extern WAV_BUFFER2:WORD
extern NAMBAR:WORD
extern NABMBAR:WORD
; player internal variables and other equates.
FILESIZE equ 64 * 1024 ; 64k file buffer size.
ENDOFFILE equ BIT0 ; flag for knowing end of file
;===========================================================================
; entry: none. File is already open and [filehandle] filled.
; exit: not until the song is finished or the user aborts.
;
playWav proc public
; load 64k into buffer 1
mov ax, ds:[WAV_BUFFER1]
call loadFromFile
; and 64k into buffer 2
mov ax, ds:[WAV_BUFFER2]
call loadFromFile
;
; register reset the DMA engine. This may cause a pop noise on the output
; lines when the device is reset. Prolly a better idea to mute output, then
; reset.
;
mov dx, ds:[NABMBAR]
add dx, PO_CR_REG ; set pointer to Cntl reg
mov al, RR ; set reset
out dx, al ; self clearing bit
;
;
; write last valid index to 31 to start with.
; The Last Valid Index register tells the DMA engine when to stop playing.
;
; As we progress through the song we change the last valid index to always be
; something other than the index we're currently playing.
;
mov al, 1
call setLastValidIndex
; create Buffer Descriptor List
;
; A buffer descriptor list is a list of pointers and control bits that the
; DMA engine uses to know where to get the .wav data and how to play it.
;
; I set it up to use only 2 buffers of .wav data, and whenever 1 buffer is
; playing, I refresh the other one with good data.
;
;
; For the control bits, you can specify that the DMA engine fire an interrupt
; after a buffer has been processed, but I poll the current index register
; to know when it's safe to update the other buffer.
;
; I set the BUP bit, which tells the DMA engine to just play 0's (silence)
; if it ever runs out of data to play. Good for safety.
;
push es
mov ax, ds:[BDL_BUFFER] ; get segment # for BDL
mov es, ax
mov cx, 32 / 2 ; make 32 entries in BDL
xor di, di
@@:
; set buffer descriptor 0 to start of data file in memory
movzx eax, ds:[WAV_BUFFER1]
shl eax, 4 ; convert seg:off ->0:offset
stosd ; store pointer to wavbuffer1
;
; set length to 32k samples. 1 sample is 16bits or 2bytes.
; Set control (bits 31:16) to BUP, bits 15:0=number of samples.
;
xor eax, eax ;
or eax, BUP ;
mov ax, FILESIZE / 2 ; 64k of data at 16bit each
stosd ; is 32k samples.
; 2nd buffer:
movzx eax, ds:[WAV_BUFFER2]
shl eax, 4 ; convert seg:off ->0:offset
stosd ; store pointer to wavbuffer2
; set length to 64k (32k of two 16 bit samples)
; Set control (bits 31:16) to BUP, bits 15:0=number of samples
;
xor eax, eax
or eax, BUP
mov ax, FILESIZE / 2 ; 64k / 2 samples
stosd ; make size 64k, no IRQs
loop @b
pop es
;
; tell the DMA engine where to find our list of Buffer Descriptors.
; this 32bit value is a flat mode memory offset (ie no segment:offset)
;
; write NABMBAR+10h with offset of buffer descriptor list
;
movzx eax, ds:[BDL_BUFFER]
mov dx, ds:[NABMBAR]
add dx, PO_BDBAR_REG ; set pointer to BDL
shl eax, 4 ; convert seg:off to 0:off
out dx, eax ; write to AC97 controller
;
;
; All set. Let's play some music.
;
;
mov dx, ds:[NABMBAR]
add dx, PO_CR_REG ; DCM out control register
mov al, RPBM
out dx, al ; set start!
;
; while DMA engine is running, examine current index and wait until it hits 1
; as soon as it's 1, we need to refresh the data in wavbuffer1 with another
; 64k. Likewise when it's playing buffer 2, refresh buffer 1 and repeat.
;
tuneLoop:
mov dx, ds:[NABMBAR]
add dx, PO_CIV_REG
@@:
call updateLVI ; set LVI != CIV
call check4keyboardstop ; keyboard halt?
jc exit ;
call getCurrentIndex ; read CIV to see if we can refresh.
test al, BIT0
jz @b ; loop if not ready yet.
; refresh buffer 0
mov ax, ds:[WAV_BUFFER1]
call loadFromFile ; refresh buffer 1 with new data
jc exit ; exit if we ran out of data to play
@@:
call updateLVI ; same loop as above
call check4keyboardstop
jc exit
call getCurrentIndex
test al, BIT0
jnz @b
; refresh buffer 1
mov ax, ds:[WAV_BUFFER2]
call loadFromFile
jnc tuneloop
exit:
mov dx, ds:[NABMBAR] ; finshed with song, stop everything
add dx, PO_CR_REG ; DCM out control register
mov al, 0
out dx, al ; stop player
ret
playWav endp
; load data from file in 32k chunks. Would be nice to load 1 64k chunk,
; but in DOS you can only load FFFF bytes at a time.
;
; entry: ax = segment to load data to
; exit: CY set if end of file reached.
; note: last file buffer is padded with 0's to avoid pops at the end of song.
; assumes file is already open. uses [filehandle]
;
loadFromFile proc public
push ax
push cx
push dx
push es
push ds
push ds ; copy es->ds since we
pop es ; mess with DS
test es:[flags], ENDOFFILE ; have we already read the
stc ; last of the file?
jnz endLFF
mov ds, ax
xor dx, dx ; load file into memory
mov cx, (FILESIZE / 2) ; 32k chunk
mov ah, 3fh
mov bx, cs:[filehandle]
int 21h
clc
cmp ax, cx
jz @f
or es:[flags], ENDOFFILE ; flag end of file
call padfill ; blank pad the remainder
clc ; don't exit with CY yet.
jmp endLFF
@@:
add dx, ax
mov cx, (FILESIZE / 2) ; 32k chunk
mov ah, 3fh
mov bx, cs:[filehandle]
int 21h
clc
cmp ax, cx
jz endLFF
or es:[flags], ENDOFFILE ; flag end of file
call padfill ; blank pad the remainder
clc ; don't exit with CY yet.
endLFF:
pop ds
pop es
pop dx
pop cx
pop ax
ret
loadFromFile endp
; entry ds:ax points to last byte in file
; cx=target size
; note: must do byte size fill
; destroys bx, cx
;
padfill proc
push bx
sub cx, ax
mov bx, ax
xor al, al
@@:
mov byte ptr ds:[bx], al
inc bx
loop @b
pop bx
ret
padfill endp
; examines the CIV and the LVI. When they're the same, we set LVI <> CIV
; that way, we never run out of buffers to play.
;
updateLVI proc
push ax
push dx
mov dx, ds:[NABMBAR]
add dx, PO_CIV_REG ; read Current Index Value
in ax, dx ; and Last Valid Index value together.
cmp al, ah ; are we playing the last index?
jnz @f ; no, never mind
call setNewIndex ; set it to something else.
@@:
pop dx
pop ax
ret
updateLVI endp
; set the last valid index to something other than what we're currently
; playing so that we never end.
;
; this routine just sets the last valid index to 1 less than the index
; that we're currently playing, thus keeping it in and endless loop
; input: none
; outpt: none
;
setNewIndex proc
push ax
call getCurrentIndex ; get CIV
test ds:[flags], ENDOFFILE
jnz @f
; not at the end of the file yet.
dec al ; make new index <> current
and al, INDEX_MASK ; make sure new value is 0-31
@@:
call setLastValidIndex ; write new value
clc
pop ax
ret
setNewIndex endp
; returns AL = current index value
getCurrentIndex proc
push dx
mov dx, ds:[NABMBAR]
add dx, PO_CIV_REG
in al, dx
pop dx
ret
getCurrentIndex endp
; input AL = index # to stop on
setLastValidIndex proc
push dx
mov dx, ds:[NABMBAR]
add dx, PO_LVI_REG
out dx, al
pop dx
ret
setLastValidIndex endp
; checks if either shift key has been pressed. Exits with CY if so.
;
check4keyboardstop proc
push ds
push 0
pop ds ; examine BDA for keyboard flags
test byte ptr ds:[417h], (BIT0 OR BIT1)
pop ds
stc
jnz @f
clc
@@:
ret
check4keyboardstop endp
.DATA
flags dd 0
End