-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.asm
375 lines (320 loc) · 4.88 KB
/
music.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
363
364
365
366
367
368
369
370
371
372
373
374
375
SECTION "MUSIC MEMORY", WRAM0
wMusicStatus: ds 1
wMusicSpeedDivider: ds 1
;current song settings
wMusicSongSpeed: ds 1
wMusicSongLow : ds 1
wMusicSongHigh: ds 1
;music engine data
wMusicCurrentLow: ds 1
wMusicCurrentHigh: ds 1
NOTE_SONG_LOOP EQU $FF
NOTE_SONG_END EQU $FE
NOTE_SONG_EMPTY_ROW EQU $FD
SECTION "FREQUENCYTABLE", ROM0
FrequencyTable:
dw 0
C3:
dw 44
dw 156
dw 262
dw 363
dw 457
dw 547
dw 631
dw 710
dw 786
dw 854
dw 923
dw 986
dw 1046
dw 1102
dw 1155
dw 1205
dw 1253
dw 1297
dw 1339
dw 1379
dw 1417
dw 1452
dw 1486
dw 1517
dw 1546
dw 1575
dw 1602
dw 1627
dw 1650
dw 1673
dw 1694
dw 1714
dw 1732
dw 1750
dw 1767
dw 1783
dw 1798
dw 1812
dw 1825
dw 1837
dw 1849
dw 1860
dw 1871
dw 1881
dw 1890
dw 1899
dw 1907
dw 1915
dw 1923
dw 1930
dw 1936
dw 1943
dw 1949
dw 1954
dw 1959
dw 1964
dw 1969
dw 1974
dw 1978
dw 1982
dw 1985
dw 1988
dw 1992
dw 1995
dw 1998
dw 2001
dw 2004
dw 2006
dw 2009
dw 2011
dw 2013
B8: dw 2015
; 72 notes
; C3 = 1, B8 = 72
;SECTION "SONG DATA", ROMX
;DemoSongData:
;db $36
;db $00
;db $00
;db $00
;db $00
;db $00
;db $36
;db $00
;db $FF
;DEMOSONG_SPEED EQU 20
SECTION "MUSIC CODE", ROM0
MusicInit:
xor a
ld [wMusicStatus], a
ld [wMusicSpeedDivider], a
ldh [rAUDVOL], a
ldh [rAUDTERM], a
ldh [rAUDENA], a
ldh [$FF30], a
ldh [$FF31], a
ldh [$FF32], a
ldh [$FF33], a
ldh [$FF34], a
ldh [$FF35], a
ldh [$FF36], a
ldh [$FF37], a
xor $ff
ldh [$FF38], a
ldh [$FF39], a
ldh [$FF3A], a
ldh [$FF3B], a
ldh [$FF3C], a
ldh [$FF3D], a
ldh [$FF3E], a
ldh [$FF3F], a
ld a, $8f
ldh [rAUDENA], a
ld a, $ff
ldh [rAUDTERM], a
ld a, $77
ldh [rAUDVOL], a
;ld hl, DemoSongData
;ld a, DEMOSONG_SPEED
;call MusicStartSong
ret
; param hl startaddress of the song
; param a speed
MusicStartSong:
ld [wMusicSongSpeed], a
ld a, h
ld [wMusicSongHigh], a
ld [wMusicCurrentHigh], a
ld a, l
ld [wMusicSongLow], a
ld [wMusicCurrentLow], a
ld a, 1
ld [wMusicStatus], a
xor a
ld [wMusicSpeedDivider], a
ret
MusicUpdate:
ld a, [wMusicStatus]
and a
ret z
ld a, [wMusicSongSpeed]
ld b, a
ld a, [wMusicSpeedDivider]
inc a
ld [wMusicSpeedDivider], a
cp a, b
ret nz
xor a
ld [wMusicSpeedDivider] ,a
; we got an update, it was a tick AND we passed the divider, make some noise
ld a, [wMusicCurrentLow]
ld l, a
ld a, [wMusicCurrentHigh]
ld h, a
ld d, 3
.processData:
ld a, [hl]
;check for end
cp NOTE_SONG_END
jr nz, .noEnd
; turn off song
xor a
ld [wMusicStatus], a
ret
.noEnd:
cp NOTE_SONG_EMPTY_ROW
jr nz, .notEmpty
inc hl
ld a, h
ld [wMusicCurrentHigh], a
ld a, l
ld [wMusicCurrentLow], a
ret
.notEmpty:
;check for loop
.checkLoop:
cp NOTE_SONG_LOOP
jr nz, .noLoop
;loop
ld a, [wMusicSongLow]
ld l, a
ld a, [wMusicSongHigh]
ld h, a
ld a, [hl]
; process and inc hl
.noLoop:
;check highest bit
bit 7, a
jr z, .processFrequencyIndex
call PlaySample
jr .advanceStep
.processFrequencyIndex:
;if the index is 0, the step is empty
and a
jr z, .advanceStep
;if d = 2, 1 use according channel
ld b, a
ld a, d
cp 2
jr nz, .otherChannel
ld a, b
call PlayNoteCh2
jr .advanceStep
.otherChannel:
ld a, b
call PlayNoteCh3
.advanceStep:
inc hl
dec d
jr nz, .processData
ld a, h
ld [wMusicCurrentHigh], a
ld a, l
ld [wMusicCurrentLow], a
ret
PlayNoteCh2:
push hl
sla a
ld hl, FrequencyTable
ld b, 0
ld c, a
add hl, bc
ld a, $80
ldh [rNR21], a
ld a, $54
ldh [rNR22], a
ld a, [hli]
ldh [rNR23], a
ld a, [hl]
or a, $C0
ldh [rNR24], a
pop hl
ret
PlayNoteCh3:
push hl
sla a
ld hl, FrequencyTable
ld b, 0
ld c, a
add hl, bc
;channel 3
ld a, $80
ldh [rNR30], a
ld a, $df
ldh [rNR31], a
ld a, $40 ;20 for full volume
ldh [rNR32], a
ld a, [hli]
ldh [rNR33], a
ld a, [hl]
or a, $c0
ldh [rNR34], a
pop hl
ret
PlaySample:
and $0f
cp a, 1
jr nz, .noBD
ld a, $4a
ldh [rNR10], a
ld a, $bf
ldh [rNR11], a
ld a, $f1
ldh [rNR12], a
ld a, $ff
ldh [rNR13], a
ld a, $83
ldh [rNR14], a
.noBD:
cp a, 2
jr nz, .noSnare
xor a
ldh [rNR41], a
ld a, $73
ldh [rNR42], a
ld a, $51
ldh [rNR43], a
ld a, $80
ldh [rNR44], a
.noSnare:
cp a, 3
jr nz, .noHH
ld a, $1f
ldh [rNR41], a
ld a, $71
ldh [rNR42], a
ld a, $10
ldh [rNR43], a
ld a, $c0
ldh [rNR44], a
.noHH:
cp a, 4
jr nz, .noOHH
xor a
ldh [rNR41], a
ld a, $f1
ldh [rNR42], a
ld a, $10
ldh [rNR43], a
ld a, $80
ldh [rNR44], a
.noOHH:
ret