-
Notifications
You must be signed in to change notification settings - Fork 0
/
succ.asm
294 lines (223 loc) · 7.65 KB
/
succ.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
PUBLIC SOUNDPLAY_success
.Model LARGE
.386
succ SEGMENT PARA 'DATA'
Filename DB 'succ.wav', 0
Filehandle DW ?
filesize equ 3268
sounddata DB filesize dup(0)
last_time dd 0
voc_index dw 0 ;TO KEEP TRACK OF THE PLAYED SAMPLES TO KNOW THE NEXT AUDIO SAMPLE TO PLAY
LOADED DB 0 ;FLAG TO NOT LOAD THE SOUND FILE MULTIPLE TIMES WHEN THE SOUND FUNCTION CALLED MULTIPLE TIMES
succ ENDS
.Code
;-------------------------------------------------------------------------------------------------------------------------
; THE CONCEPT OF PLAYING SOUND CODE IS ADAPTED FROM: https://github.com/leonardo-ono/Assembly8086SBHardwareLevelDspProgrammingTest
; HE USED NASM NOT MASM SO PUTTING THE CODE TOGETHER TO MAKE IT WORK WAS NOT AN EASY TASK
;--------------------------------------------------------------------------------------------------------------------------
SOUNDPLAY_success PROC FAR
ASSUME DS:succ
MOV AX , succ
MOV DS , AX
MOV WORD PTR DS:VOC_INDEX, 0
CALL FAR PTR SOUNDS
MOV DS:LOADED, 1
RETF
SOUNDPLAY_success ENDP
SOUNDS PROC FAR
PUSHA
CMP LOADED, 1
JE DONT_LOAD_SOUND
CALL FAR PTR OPENFILE
CALL FAR PTR READDATA
CALL FAR PTR CLOSEFILE
DONT_LOAD_SOUND:
CALL FAR PTR START_FAST_CLOCK
CALL FAR PTR SB_RESET
SB_TURN_ON_SPEAKER:
MOV BL, 0D1H
CALL FAR PTR SB_WRITE_DSP
MOV DWORD PTR DS:[last_time], 0
.NEXT_SAMPLE:
CALL FAR PTR CLRBUFFR ;CLEAR THE KEYBOARD BUFFER JUST IN CASE
;{DISABLE THE PREVIOUS SAMPLE FROM THE PORT
MOV BL, 10H
CALL FAR PTR SB_WRITE_DSP
;}
.WAIT:
;{TO SYNCHRONIZE THE SAMPLE RATE PLAYED WITH CLOCK CYCLES
CALL FAR PTR GET_CURRENT_TIME
CMP EAX, DS:[LAST_TIME]
JBE .WAIT
MOV DS:[LAST_TIME], EAX
;}
;{GET THE SAMPLE WHOSE TURN HAS COME TO BE PLAYED IN BL
MOV EBX, 0
MOV BX, WORD PTR DS:VOC_INDEX
MOV BL, BYTE PTR DS:[SOUNDDATA + EBX]
CALL FAR PTR SB_WRITE_DSP
;}
INC WORD PTR DS:VOC_INDEX ;INCREMENT THE INDEX OF THE SAMPLES
CMP WORD PTR DS:VOC_INDEX, FILESIZE ;CHECK IF WE REACHED THE END OF THE FILE
JB .NEXT_SAMPLE;
STOP_PLAYING:
SB_TURN_OFF_SPEAKER:
MOV BL, 0D3H
CALL FAR PTR SB_WRITE_DSP
;RETURN THE CLOCK CYCLES TO THE NORMAL SPEED FOR THE REST OF THE GAME
CALL FAR PTR START_NORMAL_CLOCK
POPA
RETF
SOUNDS ENDP
;========================================================================================
; ______ _____ _ ______ _ _ _ _ _____ _ _____ _ _ _____
; | ____|_ _| | | ____| | | | | /\ | \ | | __ \| | |_ _| \ | |/ ____|
; | |__ | | | | | |__ | |__| | / \ | \| | | | | | | | | \| | | __
; | __| | | | | | __| | __ | / /\ \ | . ` | | | | | | | | . ` | | |_ |
; | | _| |_| |____| |____ | | | |/ ____ \| |\ | |__| | |____ _| |_| |\ | |__| |
; |_| |_____|______|______| |_| |_/_/ \_\_| \_|_____/|______|_____|_| \_|\_____|
;
;=========================================================================================
;------------------------------------------------------------------------------------------------------------
; file handling procedures
; credit: https://gist.github.com/3omar-mostafa/5da7ac824fa84a40706a8aebdd6b22d2
;------------------------------------------------------------------------------------------------------------
OPENFILE PROC FAR
MOV AH, 3DH
MOV AL, 0 ; READ ONLY
LEA DX, FILENAME
INT 21H
MOV DS:[FILEHANDLE], AX
RETF
OPENFILE ENDP
;------------------------------------------------------------
; TO READ THE SOUND FILE DATA AND PUT IT IN THE DATA SEGMENT
;------------------------------------------------------------
READDATA PROC FAR
MOV AH,3FH
MOV BX, DS:[FILEHANDLE]
MOV CX, FILESIZE ; NUMBER OF BYTES TO READ
LEA DX, SOUNDDATA
INT 21H
RETF
READDATA ENDP
;--------------------------------------------------
; AS THE NAME IMPLIES IT CLOSES THE OPEN FILE :D
;--------------------------------------------------
CLOSEFILE PROC FAR
MOV AH, 3EH
MOV BX, DS:[FILEHANDLE]
INT 21H
RETF
CLOSEFILE ENDP
;=============================================================================================================================
; _____ ____ _ _ _ _ _____ __ __ _ _ _____ _____ _ _ _ _______ _____ ____ _ _ _____
; / ____|/ __ \| | | | \ | | __ \ | \/ | /\ | \ | |_ _| __ \| | | | | /\|__ __|_ _/ __ \| \ | |/ ____|
; | (___ | | | | | | | \| | | | | | \ / | / \ | \| | | | | |__) | | | | | / \ | | | || | | | \| | (___
; \___ \| | | | | | | . ` | | | | | |\/| | / /\ \ | . ` | | | | ___/| | | | | / /\ \ | | | || | | | . ` |\___ \
; ____) | |__| | |__| | |\ | |__| | | | | |/ ____ \| |\ |_| |_| | | |__| | |____ / ____ \| | _| || |__| | |\ |____) |
; |_____/ \____/ \____/|_| \_|_____/ |_| |_/_/ \_\_| \_|_____|_| \____/|______/_/ \_\_| |_____\____/|_| \_|_____/
;
;=============================================================================================================================
;-----------------------------------------------------
; I USE IT TO GET THE CLOCK CYCLES BACK TO THE NORMAL
; SPEED FOR THE REST OF THE GAME
;-----------------------------------------------------
START_NORMAL_CLOCK PROC FAR
CLI
MOV AL, 36H
OUT 43H, AL
MOV AL, 040H
OUT 40H, AL
MOV AL, 0FCH
OUT 40H, AL
STI
RETF
START_NORMAL_CLOCK ENDP
;----------------------------------------------------------------------------
; ADJUST THE CLOCK SPEED TO THE SAMPLE RATE OF THE SOUND FILE WHICH IS 8000HZ
;----------------------------------------------------------------------------
START_FAST_CLOCK PROC FAR
CLI
MOV AL, 36H
OUT 43H, AL
MOV AL, 095H
OUT 40H, AL
MOV AL, 00H
OUT 40H, AL
STI
RETF
START_FAST_CLOCK ENDP
SB_RESET PROC FAR
MOV DX, 226H
MOV AL, 1
OUT DX, AL
MOV CX, 50
.WAIT_A_LITTLE:
NOP
LOOP .WAIT_A_LITTLE
MOV DX, 226H
MOV AL, 0
OUT DX, AL
MOV CX, 0
.CHECK_READ_DATA_AVAILABLE:
MOV DX, 22EH
IN AL, DX
OR AL, AL
JNS .NEXT_TRY
.READ_DATA:
MOV DX, 22AH
IN AL, DX
CMP AL, 0AAH
JNZ .ERROR
.OK:
MOV AX,1
JMP .END
.NEXT_TRY:
LOOP .CHECK_READ_DATA_AVAILABLE
.ERROR:
MOV AX, 0
.END:
RETF
SB_RESET ENDP
;---------------------------------------------------------------
; WRITE A SOUND NOTE IN WHICH IS IN BL TO THE PORT TO BE PLAYED
;---------------------------------------------------------------
; BL = SEND BYTE DATA
SB_WRITE_DSP PROC FAR
MOV DX, 22CH
.BUSY:
IN AL, DX
TEST AL, 10000000B
JNZ .BUSY
MOV AL, BL
OUT DX, AL
RETF
SB_WRITE_DSP ENDP
;---------------------------------------------------------
; GET CURRENT SYSTEM TIME ADN PUT IT IN EAX
;---------------------------------------------------------
GET_CURRENT_TIME PROC FAR
PUSH ES
MOV AX, 0
MOV ES, AX
MOV EAX, ES:[46CH]
POP ES
RETF
GET_CURRENT_TIME ENDP
;---------------------------------------------------------------------------------------------------------------
; CLEARS KEYBOARD BUFFER
; CREDIT: https://www.daniweb.com/programming/software-development/threads/77762/how-to-clear-keyboard-buffer
;---------------------------------------------------------------------------------------------------------------
CLRBUFFR PROC FAR
PUSH AX
PUSH ES
MOV AX, 0000H
MOV ES, AX
MOV ES:[041AH], 041EH
MOV ES:[041CH], 041EH ; CLEARS KEYBOARD BUFFER
POP ES
POP AX
RETF
CLRBUFFR ENDP
end SOUNDPLAY_success