-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathaudio.asm
679 lines (552 loc) · 11.6 KB
/
audio.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
; This file is part of Pwn Adventure Z.
; Pwn Adventure Z 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 3 of the License, or
; (at your option) any later version.
; Pwn Adventure Z 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 Pwn Adventure Z. If not, see <http://www.gnu.org/licenses/>.
.include "defines.inc"
.segment "FIXED"
PROC play_music_ptr
sta temp
lda #0
sta music_playing
lda ptr
sta music_page_list_ptr
lda ptr + 1
sta music_page_list_ptr + 1
lda arg0
sta music_page_bank_ptr
lda arg1
sta music_page_bank_ptr + 1
lda arg2
sta music_loop_list_ptr
lda arg3
sta music_loop_list_ptr + 1
lda arg4
sta music_loop_bank_ptr
lda arg5
sta music_loop_bank_ptr + 1
lda temp
sta music_page_list_bank
lda #0
sta music_page_offset
; Switch to bank containing music pointers
lda current_bank
pha
lda music_page_list_bank
jsr bankswitch
; Load pointer to first page of music
ldy #0
lda (music_page_list_ptr), y
sta music_page_ptr
ldy #1
lda (music_page_list_ptr), y
sta music_page_ptr + 1
ldy #0
lda (music_page_bank_ptr), y
sta music_page_bank
; Update pointers to next page
lda music_page_list_ptr
clc
adc #2
sta music_page_list_ptr
lda music_page_list_ptr + 1
adc #0
sta music_page_list_ptr + 1
lda music_page_bank_ptr
clc
adc #1
sta music_page_bank_ptr
lda music_page_bank_ptr + 1
adc #0
sta music_page_bank_ptr + 1
; Switch back to entry bank and start music playback
pla
jsr bankswitch
lda #1
sta music_playing
rts
.endproc
PROC play_music
cmp #0
bne hasmusic
jsr stop_music
rts
hasmusic:
cmp music_index
bne newmusic
rts
newmusic:
sta music_index
sec
sbc #1
asl
tay
lda music_descriptors, y
sta temp
lda music_descriptors + 1, y
sta temp + 1
ldy #0
lda (temp), y
sta ptr
iny
lda (temp), y
sta ptr + 1
iny
lda (temp), y
sta arg0
iny
lda (temp), y
sta arg1
iny
lda (temp), y
sta arg2
iny
lda (temp), y
sta arg3
iny
lda (temp), y
sta arg4
iny
lda (temp), y
sta arg5
iny
lda (temp), y
jsr play_music_ptr
rts
.endproc
PROC stop_music
lda #0
sta music_playing
lda #MUSIC_NONE
sta music_index
; Silence any notes from the music
lda #$30
sta SQ1_VOL
sta SQ2_VOL
lda sound_effect_playing
bne soundeffect
lda #0
sta TRI_LINEAR
lda #0
sta TRI_HI
lda #$30
sta NOISE_VOL
soundeffect:
rts
.endproc
PROC play_sound_effect_no_override
pha
lda sound_effect_playing
bne dontplay
pla
jmp play_sound_effect
dontplay:
pla
rts
.endproc
PROC play_sound_effect
pha
lda #0
sta sound_effect_playing
lda ptr
sta sound_effect_ptr
lda ptr + 1
sta sound_effect_ptr + 1
pla
sta sound_effect_bank
lda #0
sta sound_effect_offset
; Silence any notes from the music on the triangle and noise channels
lda #0
sta TRI_LINEAR
lda #0
sta TRI_HI
lda #$30
sta NOISE_VOL
lda #1
sta sound_effect_playing
rts
.endproc
PROC update_audio
lda current_bank
pha
; Set pointers to registers
lda #0
sta audio_reg_ptr
lda #$40
sta audio_reg_ptr + 1
lda #<music_reg_state
sta music_reg_state_ptr
lda #>music_reg_state
sta music_reg_state_ptr + 1
lda music_playing
bne musicvalid
jmp processeffect
musicvalid:
; There is music playing, switch to bank containing music
lda music_page_bank
jsr bankswitch
; Read mask of registers to be updated
jsr read_music_byte
sta audio_reg_mask
jsr read_music_byte
sta audio_reg_mask + 1
; Process first 8 audio registers
lda #0
sta audio_reg_index
lda #1
sta audio_temp
firstloop:
lda audio_reg_mask
and audio_temp
beq firstnotset
jsr read_music_byte
ldy audio_reg_index
sta (audio_reg_ptr), y
firstnotset:
lda audio_temp
asl
beq firstdone
sta audio_temp
inc audio_reg_index
jmp firstloop
firstdone:
; Process second 8 audio registers
lda #8
sta audio_reg_index
lda #1
sta audio_temp
secondloop:
lda audio_reg_mask + 1
cmp #2
beq secondnotset
and audio_temp
beq secondnotset
; If sound effect is playing, don't write the register, but save
; what was going to be written to the current music state
lda sound_effect_playing
bne effectactive
jsr read_music_byte
ldy audio_reg_index
sta (audio_reg_ptr), y
sta (music_reg_state_ptr), y
jmp secondnotset
effectactive:
jsr read_music_byte
ldy audio_reg_index
sta (music_reg_state_ptr), y
secondnotset:
lda audio_temp
asl
beq seconddone
sta audio_temp
inc audio_reg_index
jmp secondloop
seconddone:
; Check for end of stream marker
lda audio_reg_mask + 1
and #2
beq processeffect
; At end of music, loop now
lda music_loop_list_ptr
sta music_page_list_ptr
lda music_loop_list_ptr + 1
sta music_page_list_ptr + 1
lda music_loop_bank_ptr
sta music_page_bank_ptr
lda music_loop_bank_ptr + 1
sta music_page_bank_ptr + 1
lda music_page_list_bank
jsr bankswitch
ldy #0
lda (music_page_list_ptr), y
sta music_page_ptr
ldy #1
lda (music_page_list_ptr), y
sta music_page_ptr + 1
ldy #0
lda (music_page_bank_ptr), y
sta music_page_bank
; Update pointers to next page
lda music_page_list_ptr
clc
adc #2
sta music_page_list_ptr
lda music_page_list_ptr + 1
adc #0
sta music_page_list_ptr + 1
lda music_page_bank_ptr
clc
adc #1
sta music_page_bank_ptr
lda music_page_bank_ptr + 1
adc #0
sta music_page_bank_ptr + 1
lda #0
sta music_page_offset
processeffect:
; Check for an active sound effect
lda sound_effect_playing
bne haseffect
jmp done
haseffect:
; There is a valid sound effect, switch to the bank containing the effect
lda sound_effect_bank
jsr bankswitch
; Read the mask of registers to be updated
jsr read_sound_effect_byte
sta audio_reg_mask
; Process effect register updates
lda #8
sta audio_reg_index
lda #1
sta audio_temp
effectloop:
lda audio_reg_mask
cmp #2
beq effectnotset
and audio_temp
beq effectnotset
jsr read_sound_effect_byte
ldy audio_reg_index
sta (audio_reg_ptr), y
effectnotset:
lda audio_temp
asl
beq effectdone
sta audio_temp
inc audio_reg_index
jmp effectloop
effectdone:
; Check for end of stream marker
lda audio_reg_mask
and #2
beq done
; Sound effect is done
lda music_playing
beq nomusicaftereffect
; Music is playing, restore channels to music state
lda music_reg_state + 8
sta $4008
lda music_reg_state + $a
sta $400a
lda music_reg_state + $b
sta $400b
lda music_reg_state + $c
sta $400c
lda music_reg_state + $e
sta $400e
lda music_reg_state + $f
sta $400f
lda #0
sta sound_effect_playing
jmp done
nomusicaftereffect:
; No music and an effect is done, silence effect channels
lda #0
sta TRI_LINEAR
lda #0
sta TRI_HI
lda #$30
sta NOISE_VOL
lda #0
sta sound_effect_playing
done:
pla
jsr bankswitch
rts
.endproc
PROC read_music_byte
ldy music_page_offset
lda (music_page_ptr), y
inc music_page_offset
bne notend
; Went past end of page, load next page
pha
lda music_page_list_bank
jsr bankswitch
ldy #0
lda (music_page_list_ptr), y
sta music_page_ptr
ldy #1
lda (music_page_list_ptr), y
sta music_page_ptr + 1
ldy #0
lda (music_page_bank_ptr), y
sta music_page_bank
; Update pointers to next page
lda music_page_list_ptr
clc
adc #2
sta music_page_list_ptr
lda music_page_list_ptr + 1
adc #0
sta music_page_list_ptr + 1
lda music_page_bank_ptr
clc
adc #1
sta music_page_bank_ptr
lda music_page_bank_ptr + 1
adc #0
sta music_page_bank_ptr + 1
lda music_page_bank
jsr bankswitch
pla
notend:
rts
.endproc
PROC read_sound_effect_byte
ldy sound_effect_offset
lda (sound_effect_ptr), y
inc sound_effect_offset
rts
.endproc
.zeropage
VAR music_page_list_ptr
.word 0
VAR music_page_bank_ptr
.word 0
VAR music_page_ptr
.word 0
VAR sound_effect_ptr
.word 0
VAR audio_reg_ptr
.word 0
VAR music_reg_state_ptr
.word 0
.segment "TEMP"
VAR music_playing
.byte 0
VAR music_index
.byte 0
VAR music_page_list_bank
.byte 0
VAR music_page_bank
.byte 0
VAR music_page_offset
.byte 0
VAR music_reg_state
.byte 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
VAR sound_effect_playing
.byte 0
VAR sound_effect_bank
.byte 0
VAR sound_effect_offset
.byte 0
VAR audio_temp
.byte 0
VAR audio_reg_mask
.word 0
VAR audio_reg_index
.byte 0
VAR music_loop_list_ptr
.word 0
VAR music_loop_bank_ptr
.word 0
.segment "AUDIO0"
.include "audio/craft.asm"
.include "audio/enemydie.asm"
.include "audio/enemyhit.asm"
.include "audio/equip.asm"
.include "audio/getitem.asm"
.include "audio/light.asm"
.include "audio/open.asm"
.include "audio/pistol.asm"
.include "audio/playerhit.asm"
.include "audio/select.asm"
.include "audio/uimove.asm"
.include "audio/buy.asm"
.include "audio/sell.asm"
.include "audio/laser.asm"
.include "audio/warp.asm"
.include "audio/boom.asm"
.include "audio/ak.asm"
.include "audio/handcannon.asm"
.include "audio/lmg.asm"
.include "audio/melee.asm"
.include "audio/rocket.asm"
.include "audio/shotgun.asm"
.include "audio/smg.asm"
.include "audio/sniper.asm"
.segment "AUDIO0"
.include "audio/cave.asm"
.include "audio/horde.asm"
.include "audio/title.asm"
.segment "AUDIO1"
.include "audio/forest.asm"
.segment "AUDIO2"
.include "audio/town.asm"
.include "audio/mine.asm"
.segment "AUDIO3"
.include "audio/boss.asm"
.segment "AUDIO4"
.include "audio/credits.asm"
.segment "AUDIO5"
.include "audio/forestfull.asm"
.segment "AUDIO6"
.include "audio/blocky.asm"
.segment "AUDIO7"
.include "audio/sewer.asm"
.data
VAR cave_music_desc
.word music_cave_ptr & $ffff, music_cave_bank & $ffff
.word music_cave_ptr & $ffff, music_cave_bank & $ffff
.byte ^music_cave_ptr
VAR horde_music_desc
.word music_horde_ptr & $ffff, music_horde_bank & $ffff
.word music_horde_ptr & $ffff, music_horde_bank & $ffff
.byte ^music_horde_ptr
VAR title_music_desc
.word music_title_ptr & $ffff, music_title_bank & $ffff
.word music_title_ptr & $ffff, music_title_bank & $ffff
.byte ^music_title_ptr
VAR forest_music_desc
.word music_forest_ptr & $ffff, music_forest_bank & $ffff
.word music_forest_ptr & $ffff, music_forest_bank & $ffff
.byte ^music_forest_ptr
VAR forest_full_music_desc
.word music_forestfull_ptr & $ffff, music_forestfull_bank & $ffff
.word music_forestfull_ptr & $ffff, music_forestfull_bank & $ffff
.byte ^music_forestfull_ptr
VAR town_music_desc
.word music_town_ptr & $ffff, music_town_bank & $ffff
.word music_town_ptr & $ffff, music_town_bank & $ffff
.byte ^music_town_ptr
VAR mine_music_desc
.word music_mine_ptr & $ffff, music_mine_bank & $ffff
.word music_mine_ptr & $ffff, music_mine_bank & $ffff
.byte ^music_mine_ptr
VAR boss_music_desc
.word music_boss_ptr & $ffff, music_boss_bank & $ffff
.word music_boss_ptr & $ffff, music_boss_bank & $ffff
.byte ^music_boss_ptr
VAR credits_music_desc
.word music_credits_ptr & $ffff, music_credits_bank & $ffff
.word music_credits_ptr & $ffff, music_credits_bank & $ffff
.byte ^music_credits_ptr
VAR blocky_music_desc
.word music_blocky_ptr & $ffff, music_blocky_bank & $ffff
.word music_blocky_ptr & $ffff, music_blocky_bank & $ffff
.byte ^music_blocky_ptr
VAR sewer_music_desc
.word music_sewer_ptr & $ffff, music_sewer_bank & $ffff
.word music_sewer_ptr & $ffff, music_sewer_bank & $ffff
.byte ^music_sewer_ptr
VAR music_descriptors
.word cave_music_desc
.word forest_music_desc
.word forest_full_music_desc
.word horde_music_desc
.word title_music_desc
.word town_music_desc
.word mine_music_desc
.word boss_music_desc
.word credits_music_desc
.word blocky_music_desc
.word sewer_music_desc