-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathplayer.s
439 lines (381 loc) · 7.28 KB
/
player.s
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
; lsdpack - standalone LSDj (Little Sound Dj) recorder + player {{{
; Copyright (C) 2018 Johan Kotlinski
; https://www.littlesounddj.com
;
; 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. }}}
if __RGBDS_MAJOR__ != 0 || __RGBDS_MINOR__ != 5 || __RGBDS_PATCH__ != 1
warn "Unknown RGBDS version {__RGBDS_VERSION__}! lsdpack was tested with RGBDS 0.5.1"
endc
SECTION "player",ROM0[$3d00]
; .gbs player entry points
ret
jp LsdjPlaySong
jp LsdjTick
; Starts playing a song. If a song is already playing,
; make sure interrupts are disabled when calling this.
;
; IN: a = song number
; OUT: -
; SIDE EFFECTS: trashes af
;
LsdjPlaySong::
push de
push hl
ld [Song],a
ld hl,SongLocations
add a,a
add a,a
add a,l
ld l,a
ld de,CurrentBank
ld a,[hl+]
ld [de],a ; CurrentBank
inc de
ld a,[hl+]
ld [de],a ; CurrentBank + 1
inc de
ld a,[hl+]
ld [de],a ; CurrentPtr
inc de
ld a,[hl+]
ld [de],a ; CurrentPtr + 1
xor a
ldh [$26],a ; stop sound
ld [RepeatCmdCounter],a
pop hl
pop de
ret
CMD_END_TICK = 0
CMD_SAMPLE_START = 1
CMD_SONG_STOP = 2
CMD_NEXT_BANK = 3
CMD_AMP_DEC_PU0 = 4
CMD_AMP_DEC_PU1 = 5
CMD_AMP_DEC_NOI = 6
CMD_PITCH_PU0 = 7
CMD_PITCH_PU1 = 8
CMD_PITCH_WAV = 9
CMD_SAMPLE_NEXT = 10
; Call this six times per screen update,
; evenly spread out over the screen.
;
; IN: -
; OUT: -
; SIDE EFFECTS: changes ROM bank, trashes af
;
LsdjTick::
push hl
push bc
.tick
ld a,[CurrentBank+1]
ld [$3000],a
ld a,[CurrentBank]
ld [$2000],a
ld a,[CurrentPtr+1] ; hl = ptr
ld h,a
ld a,[CurrentPtr]
ld l,a
xor a
ld b,a ; b = last read command byte
.loop
bit 7,b
jr nz,.lyc_done
; load repeated cmd
ld a,[RepeatCmdCounter]
or a
jr z,.load_new_cmd_from_stream
dec a
ld [RepeatCmdCounter],a
ld a,[RepeatCmd]
jr .apply_cmd
.load_new_cmd_from_stream
ld a,[hl+]
bit 6,a
jr z,.apply_cmd
; save repeated cmd
and ~$40
ld [RepeatCmd],a
ld b,a
ld a,[hl+]
ld [RepeatCmdCounter],a
ld a,b
.apply_cmd
ld b,a
and $3f
jr z,.lyc_done
cp 10
jp z,.sample_next
jr c,.is_command
; write sound register
ld c,a
ld a,[hl+]
ldh [c],a
jr .loop
.is_command
cp CMD_SAMPLE_START
jp z,.sample_start
cp CMD_AMP_DEC_PU0
jr z,.amp_dec_pu0
cp CMD_AMP_DEC_PU1
jr z,.amp_dec_pu1
cp CMD_AMP_DEC_NOI
jr z,.amp_dec_noi
cp CMD_PITCH_PU0
jr z,.pitch_pu0
cp CMD_PITCH_PU1
jr z,.pitch_pu1
cp CMD_PITCH_WAV
jr z,.pitch_wav
cp CMD_NEXT_BANK
jr z,.next_bank
cp CMD_SONG_STOP
jr z,.handle_song_stop
.lyc_done
ld a,l
ld [CurrentPtr],a
ld a,h
ld [CurrentPtr+1],a
pop bc
pop hl
ret
.handle_song_stop:
ld a,[Song]
call LsdjPlaySong
jp .tick
.amp_dec_pu0:
ld a,9
ldh [$12],a
ld a,$11
ldh [$12],a
ld a,$18
ldh [$12],a
jp .loop
.amp_dec_pu1:
ld a,9
ldh [$17],a
ld a,$11
ldh [$17],a
ld a,$18
ldh [$17],a
jp .loop
.amp_dec_noi:
ld a,9
ldh [$21],a
ld a,$11
ldh [$21],a
ld a,$18
ldh [$21],a
jp .loop
.pitch_pu0:
ld a,[hl+]
ldh [$13],a
ld a,[hl+]
ldh [$14],a
jp .loop
.pitch_pu1:
ld a,[hl+]
ldh [$18],a
ld a,[hl+]
ldh [$19],a
jp .loop
.pitch_wav:
ld a,[hl+]
ldh [$1d],a
ld a,[hl+]
ldh [$1e],a
jp .loop
.next_bank:
ld a,$40
ld h,a
xor a
ld l,a
ld a,[CurrentBank]
inc a
ld [CurrentBank],a
ld [$2000],a
jp nz,.loop
ld a,[CurrentBank+1]
inc a
ld [CurrentBank+1],a
ld [$3000],a
; Reapply LSB in case of non-MBC5 cartridge.
ld [CurrentBank],a
ld [$2000],a
jp .loop
.sample_start:
push de
ld a,[hl+]
ld c,a ; c = sample bank
ld [SampleBank],a
ld a,[hl+]
ld e,a
ld a,[hl+]
ld d,a ; de = sample ptr
push hl
; switch bank
xor a
ld [$3000],a
ld a,c
ld [$2000],a
ld h,d
ld l,e
; disable wave channel output
ldh a,[$25]
ld e,a
and a,~$44
ldh [$25],a
xor a
ldh [$1a],a ; mute channel
ld a,[hl+]
ldh [$30],a
ld a,[hl+]
ldh [$31],a
ld a,[hl+]
ldh [$32],a
ld a,[hl+]
ldh [$33],a
ld a,[hl+]
ldh [$34],a
ld a,[hl+]
ldh [$35],a
ld a,[hl+]
ldh [$36],a
ld a,[hl+]
ldh [$37],a
ld a,[hl+]
ldh [$38],a
ld a,[hl+]
ldh [$39],a
ld a,[hl+]
ldh [$3a],a
ld a,[hl+]
ldh [$3b],a
ld a,[hl+]
ldh [$3c],a
ld a,[hl+]
ldh [$3d],a
ld a,[hl+]
ldh [$3e],a
ld a,[hl+]
ldh [$3f],a
ld a,l
ld [SampleAddress],a
ld a,h
ld [SampleAddress+1],a
ld a,$80 ; unmute
ldh [$1a],a
pop hl
ld a,[CurrentBank+1]
ld [$3000],a
ld a,[CurrentBank]
ld [$2000],a
ld a,[hl+]
ldh [$1e],a
ld [SamplePitchMsb],a
ld a,[hl+]
ldh [$1d],a
ld a,e
ldh [$25],a
pop de
jp .loop
.sample_next:
push de
ld a,[SampleBank]
ld c,a ; c = sample bank
ld a,[SampleAddress]
ld e,a
ld a,[SampleAddress+1]
ld d,a ; de = sample ptr
push hl
; switch bank
xor a
ld [$3000],a
ld a,c
ld [$2000],a
ld h,d
ld l,e
; disable wave channel output
ldh a,[$25]
ld e,a
and a,~$44
ldh [$25],a
xor a
ldh [$1a],a ; mute channel
ld a,[hl+]
ldh [$30],a
ld a,[hl+]
ldh [$31],a
ld a,[hl+]
ldh [$32],a
ld a,[hl+]
ldh [$33],a
ld a,[hl+]
ldh [$34],a
ld a,[hl+]
ldh [$35],a
ld a,[hl+]
ldh [$36],a
ld a,[hl+]
ldh [$37],a
ld a,[hl+]
ldh [$38],a
ld a,[hl+]
ldh [$39],a
ld a,[hl+]
ldh [$3a],a
ld a,[hl+]
ldh [$3b],a
ld a,[hl+]
ldh [$3c],a
ld a,[hl+]
ldh [$3d],a
ld a,[hl+]
ldh [$3e],a
ld a,[hl+]
ldh [$3f],a
ld a,$80 ; unmute
ldh [$1a],a
ld a,[SamplePitchMsb]
ldh [$1e],a
ld a,e
ldh [$25],a
ld a,[CurrentBank+1]
ld [$3000],a
ld a,[CurrentBank]
ld [$2000],a
ld a,l
ld [SampleAddress],a
ld a,h
ld [SampleAddress+1],a
pop hl
pop de
jp .loop
SECTION "player_ram",WRAM0
Song:
ds 1
CurrentBank:
ds 2
CurrentPtr:
ds 2
SampleBank:
ds 1
SampleAddress:
ds 2
SamplePitchMsb:
ds 1
RepeatCmd:
ds 1
RepeatCmdCounter:
ds 1