-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsound.s
609 lines (555 loc) · 17 KB
/
sound.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
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
; Ninjatracker V2.03 gamemusic playroutine
; Relocation defines
NT_FIRSTNOTE = $18
NT_DUR = $c0
NT_HEADERLENGTH = 6
NT_NUMFIXUPS = 21
NT_ADDZERO = $80
NT_ADDWAVE = $00
NT_ADDPULSE = $04
NT_ADDFILT = $08
NT_ADDCMD = $0c
NT_ADDLEGATOCMD = $10
NT_ADDPATT = $14
NT_HRPARAM = $00
NT_FIRSTWAVE = $09
MUSIC_SILENCE = $00
MUSIC_GAMEOVER = $01
MUSIC_TITLE = $04
MUSIC_ENDING1 = $08
MUSIC_ENDING2 = $0c
MUSIC_MYSTERY = $10
MUSIC_OUTSIDE = $14
MUSIC_OFFICES = $18
MUSIC_MAINTENANCE = $1c
MUSIC_CAVES = $20
MUSIC_THRONE = $24
MUSIC_HIDEOUT = $28
MUSIC_NETHER = $2c
MUSIC_ASSAULT = $30
FIRST_INGAME_SONG = MUSIC_MYSTERY
; Play a song. Load if necessary. Do not reinit if already playing
;
; Parameters: A song number, $00-$03 in first file, $04-$07 in second etc.
; Returns: -
; Modifies: A,X,Y,loader temp vars
PlaySong: cmp #FIRST_INGAME_SONG ;If title/intro/outro music, always play even if music off
bcc PS_MusicOn
ldx musicMode ;If music off, always select silence tune
bne PS_MusicOn
txa
PS_MusicOn:
PS_CurrentSong: cmp #$ff
beq PS_Done
sta PS_CurrentSong+1
pha
lsr
lsr
PS_LoadedMusic: cmp #$ff ;Check if music already loaded
beq PS_SameMusicFile
sta PS_LoadedMusic+1
ldx #F_MUSIC
jsr MakeFileName
lda #$7f
sta ntInitSong ;Silence during loading
lda #<musicData
ldx #>musicData
jsr LoadFileRetry
; Initialize new music data
;
; Parameters: -
; Returns: -
; Modifies: A,X,Y,loader temp vars
InitMusicData: lda #<(musicData+NT_HEADERLENGTH-1)
sta zpSrcLo
lda #>(musicData+NT_HEADERLENGTH-1)
sta zpSrcHi
ldx #NT_NUMFIXUPS-1
IMD_Loop: lda ntFixupTblLo,x
sta zpDestLo
lda ntFixupTblHi,x
sta zpDestHi
lda ntFixupTblAdd,x
pha
bmi IMD_AddDone
lsr
lsr
IMD_AddSize: tay
IMD_GetSize: lda musicData,y
clc
adc zpSrcLo
sta zpSrcLo
bcc IMD_AddDone
inc zpSrcHi
IMD_AddDone: pla
and #$03
clc
adc zpSrcLo
ldy #$01
sta (zpDestLo),y
lda #$00
adc zpSrcHi
iny
sta (zpDestLo),y
dex
bpl IMD_Loop
PS_SameMusicFile:
pla
and #$03
sta ntInitSong
FS_Done:
PS_Done: rts
; Fade music. A new song must be initialized after this to be able to play sound effects.
;
; Parameters: -
; Returns: -
; Modifies: A,X
FadeSong: lda PS_CurrentSong+1
beq FS_Done ;No fade if game music off
FS_Loop: lda ntMasterVol
beq FS_Done
dec ntMasterVol
ldx #$06
FS_Delay: jsr WaitBottom
dex
bne FS_Delay
beq FS_Loop
; Play a sound effect, with priority (higher memory address has precedence)
; Parameters: A sound effect number
; Returns: -
; Modifies: A,loader temp vars
PlaySfx: stx zpSrcLo
sty zpSrcHi
ldx soundMode ;Check for sounds disabled
beq PSfx_Done
tay
PSfx_MusicCheck:lda PS_CurrentSong+1
bne PSfx_HasMusic
PSfx_NextChn: lda #0
clc
adc #7
cmp #21
bcc PSfx_ChnNotOver
PSfx_HasMusic: lda #0
PSfx_ChnNotOver:sta PSfx_NextChn+1
tax
lda ntChnSfx,x ;If channel not empty, check priority
beq PSfx_Ok
lda sfxTblLo,y
cmp ntChnSfxLo,x
lda sfxTblHi,y
sbc ntChnSfxHi,x
bmi PSfx_Done
PSfx_Ok:
PSfx_LastSfx: cpy #$ff ;If same sound played already this frame, do not multi-play
beq PSfx_Done
sty PSfx_LastSfx+1
lda #$01
sta ntChnSfx,x
lda sfxTblLo,y
sta ntChnSfxLo,x
lda sfxTblHi,y
sta ntChnSfxHi,x
PSfx_Done: ldx zpSrcLo
ldy zpSrcHi
rts
;New song initialization
Play_DoInit: asl
bpl Play_NoSilence
jmp SilenceSID
Play_NoSilence: asl
adc ntInitSong
tay
Play_SongTblP0: lda $1000,y
sta ntTrackLo
Play_SongTblP1: lda $1000,y
sta ntTrackHi
txa
sta ntFiltPos
sta $d417
ldx #21
Play_InitLoop: sta ntChnPattPos-1,x
dex
bne Play_InitLoop
jsr Play_InitChn
ldx #$07
jsr Play_InitChn
ldx #$0f
stx ntMasterVol
dex
Play_InitChn:
Play_SongTblP2: lda $1000,y
sta ntChnSongPos,x
iny
lda #$ff
sta ntChnNewNote,x
sta ntChnDuration,x
sta ntChnTrans,x
sta ntInitSong
rts
; Play one frame of music & sound effects
;
; Parameters: -
; Returns: -
; Modifies: A,X,Y,player vars
PlayRoutine: ldx #$00
lda ntInitSong
bpl Play_DoInit
;Filter execution
ldy ntFiltPos
beq Play_FiltDone
Play_FiltTimeM1:lda $1000,y
bpl Play_FiltMod
cmp #$ff
bcs Play_FiltJump
Play_SetFilt: sta $d417
and #$70
sta Play_FiltType+1
Play_FiltJump:
Play_FiltSpdM1a:lda $1000,y
bcs Play_FiltJump2
Play_NextFilt: inc ntFiltPos
bcc Play_StoreCutoff
Play_FiltJump2: sta ntFiltPos
bcs Play_FiltDone
Play_FiltMod: clc
dec ntFiltTime
bmi Play_NewFiltMod
bne Play_FiltCutoff
inc ntFiltPos
bcc Play_FiltDone
Play_NewFiltMod:sta ntFiltTime
Play_FiltCutoff:lda ntFiltCutoff
Play_FiltSpdM1b:adc $1000,y
Play_StoreCutoff:
sta ntFiltCutoff
sta $d416
Play_FiltDone:
Play_FiltType: lda #$00
ora ntMasterVol
sta $d418
;Channel execution
jsr Play_ChnExec
ldx #$07
jsr Play_ChnExec
ldx #$0e
;Update duration counter
Play_ChnExec: inc ntChnCounter,x
bne Play_NoPattern
;Get data from pattern
Play_Pattern: ldy ntChnPattNum,x
Play_PattTblLoM1:
lda $1000,y
sta ntTemp1
Play_PattTblHiM1:
lda $1000,y
sta ntTemp2
ldy ntChnPattPos,x
lda (ntTemp1),y
lsr
sta ntChnNewNote,x
bcc Play_NoNewCmd
Play_NewCmd: iny
lda (ntTemp1),y
sta ntChnCmd,x
bcc Play_Rest
Play_CheckHr: bmi Play_Rest
lda ntChnSfx,x
bne Play_Rest
lda #$fe
sta ntChnGate,x
sta $d405,x
lda #NT_HRPARAM
sta $d406,x
Play_Rest: iny
lda (ntTemp1),y
cmp #$c0
bcc Play_NoNewDur
iny
sta ntChnDuration,x
Play_NoNewDur: lda (ntTemp1),y
beq Play_EndPatt
tya
Play_EndPatt: sta ntChnPattPos,x
Play_JumpToWave:ldy ntChnSfx,x
bne Play_JumpToSfx
jmp Play_WaveExec
Play_JumpToSfx: jmp Play_SfxExec
;No new command, or gate control
Play_NoNewCmd: cmp #NT_FIRSTNOTE/2
bcc Play_GateCtrl
lda ntChnCmd,x
bcs Play_CheckHr
Play_GateCtrl: lsr
ora #$fe
sta ntChnGate,x
bcc Play_NewCmd
sta ntChnNewNote,x
bcs Play_Rest
;No new pattern data
Play_LegatoCmd: tya
and #$7f
tay
bpl Play_SkipAdsr
Play_JumpToPulse:
ldy ntChnSfx,x
bne Play_JumpToSfx
jmp Play_PulseExec
Play_NoPattern: lda ntChnCounter,x
cmp #$02
bne Play_JumpToPulse
;Reload counter and check for new note / command exec / track access
Play_Reload: lda ntChnDuration,x
sta ntChnCounter,x
lda ntChnNewNote,x
bpl Play_NewNoteInit
lda ntChnPattPos,x
bne Play_JumpToPulse
;Get data from track
Play_Track: ldy ntChnSongPos,x
lda (ntTrackLo),y
bne Play_NoSongJump
iny
lda (ntTrackLo),y
tay
lda (ntTrackLo),y
Play_NoSongJump:bpl Play_NoSongTrans
sta ntChnTrans,x
iny
lda (ntTrackLo),y
Play_NoSongTrans:
sta ntChnPattNum,x
iny
tya
sta ntChnSongPos,x
bcs Play_JumpToWave
bcc Play_CmdExecuted
;New note init / command exec
Play_NewNoteInit:
cmp #NT_FIRSTNOTE/2
bcc Play_SkipNote
adc ntChnTrans,x
asl
sta ntChnNote,x
sec
Play_SkipNote: ldy ntChnCmd,x
bmi Play_LegatoCmd
Play_CmdADM1: lda $1000,y
sta $d405,x
Play_CmdSRM1: lda $1000,y
sta $d406,x
bcc Play_SkipGate
lda #$ff
sta ntChnGate,x
lda #NT_FIRSTWAVE
sta $d404,x
Play_SkipGate:
Play_SkipAdsr:
Play_CmdWaveM1: lda $1000,y
beq Play_SkipWave
sta ntChnWavePos,x
lda #$00
sta ntChnWaveTime,x
Play_SkipWave:
Play_CmdPulseM1:lda $1000,y
beq Play_SkipPulse
sta ntChnPulsePos,x
lda #$00
sta ntChnPulseTime,x
Play_SkipPulse:
Play_CmdFiltM1: lda $1000,y
beq Play_SkipFilt
sta ntFiltPos
lda #$00
sta ntFiltTime
Play_SkipFilt: clc
lda ntChnPattPos,x
beq Play_Track
Play_CmdExecuted:
Play_NoTrack: rts
;Pulse execution
Play_NoPulseMod:cmp #$ff
Play_PulseSpdM1a:
lda $1000,y
bcs Play_PulseJump
inc ntChnPulsePos,x
bcc Play_StorePulse
Play_PulseJump: sta ntChnPulsePos,x
bcs Play_PulseDone
Play_PulseExec: ldy ntChnPulsePos,x
beq Play_PulseDone
Play_PulseTimeM1:
lda $1000,y
bmi Play_NoPulseMod
Play_PulseMod: clc
dec ntChnPulseTime,x
bmi Play_NewPulseMod
bne Play_NoNewPulseMod
inc ntChnPulsePos,x
bcc Play_PulseDone
Play_NewPulseMod:
sta ntChnPulseTime,x
Play_NoNewPulseMod:
lda ntChnPulse,x
Play_PulseSpdM1b:
adc $1000,y
adc #$00
Play_StorePulse:sta ntChnPulse,x
sta $d402,x
sta $d403,x
Play_PulseDone:
;Wavetable execution
Play_WaveExec: ldy ntChnWavePos,x
beq Play_WaveDone
Play_WaveM1: lda $1000,y
cmp #$c0
bcs Play_SlideOrVib
cmp #$90
bcc Play_WaveChange
;Delayed wavetable
Play_WaveDelay: beq Play_NoWaveChange
dec ntChnWaveTime,x
beq Play_NoWaveChange
bpl Play_WaveDone
sbc #$90
sta ntChnWaveTime,x
bcs Play_WaveDone
;Wave change + arpeggio
Play_WaveChange:sta ntChnWave,x
tya
sta ntChnWaveOld,x
Play_NoWaveChange:
Play_WaveP0: lda $1000,y
cmp #$ff
bcs Play_WaveJump
Play_NoWaveJump:inc ntChnWavePos,x
bcc Play_WaveJumpDone
Play_WaveJump:
Play_NoteP0: lda $1000,y
sta ntChnWavePos,x
Play_WaveJumpDone:
Play_NoteM1a: lda $1000,y
asl
bcs Play_AbsFreq
adc ntChnNote,x
Play_AbsFreq: tay
bne Play_NoteNum
Play_SlideDone: ldy ntChnNote,x
lda ntChnWaveOld,x
sta ntChnWavePos,x
Play_NoteNum: lda ntFreqTbl-24,y
sta ntChnFreqLo,x
sta $d400,x
lda ntFreqTbl-23,y
Play_StoreFreqHi:
sta $d401,x
sta ntChnFreqHi,x
Play_WaveDone: lda ntChnWave,x
and ntChnGate,x
sta $d404,x
rts
;Slide or vibrato
Play_SlideOrVib:sbc #$e0
sta ntTemp1
lda ntChnCounter,x
beq Play_WaveDone
Play_NoteM1b: lda $1000,y
sta ntTemp2
bcc Play_Vibrato
;Slide (toneportamento)
Play_Slide: ldy ntChnNote,x
lda ntChnFreqLo,x
sbc ntFreqTbl-24,y
pha
lda ntChnFreqHi,x
sbc ntFreqTbl-23,y
tay
pla
bcs Play_SlideDown
Play_Slideup: adc ntTemp2
tya
adc ntTemp1
bcs Play_SlideDone
Play_FreqAdd: lda ntChnFreqLo,x
adc ntTemp2
sta ntChnFreqLo,x
sta $d400,x
lda ntChnFreqHi,x
adc ntTemp1
jmp Play_StoreFreqHi
Play_SlideDown: sbc ntTemp2
tya
sbc ntTemp1
bcc Play_SlideDone
Play_FreqSub: lda ntChnFreqLo,x
sbc ntTemp2
sta ntChnFreqLo,x
sta $d400,x
lda ntChnFreqHi,x
sbc ntTemp1
jmp Play_StoreFreqHi
;Sound effect hard restart
Play_SfxHRFirstWave:
lda #NT_FIRSTWAVE
sta ntChnWave,x
Play_SfxHR: lda #$00
sta $d405,x
sta $d406,x
beq Play_WaveDone
;Vibrato
Play_Vibrato: lda ntChnWaveTime,x
bpl Play_VibNoDir
cmp ntTemp1
bcs Play_VibNoDir2
eor #$ff
Play_VibNoDir: sec
Play_VibNoDir2: sbc #$02
sta ntChnWaveTime,x
lsr
lda #$00
sta ntTemp1
bcc Play_FreqAdd
bcs Play_FreqSub
;Sound effect
Play_SfxExec: lda ntChnSfxLo,x
sta ntTemp1
lda ntChnSfxHi,x
sta ntTemp2
lda #$fe
sta ntChnNewNote,x
sta ntChnGate,x
inc ntChnSfx,x
cpy #$02
beq Play_SfxHRFirstWave
bcc Play_SfxHR
Play_SfxMain: lda (ntTemp1),y
beq Play_SfxEnd
Play_SfxNoEnd: asl
tay
lda ntFreqTbl-24,y
sta $d400,x
lda ntFreqTbl-23,y
sta $d401,x
ldy ntChnSfx,x
lda (ntTemp1),y
beq Play_SfxDone
cmp #$82
bcs Play_SfxDone
inc ntChnSfx,x
Play_SfxWaveChg:sta ntChnWave,x
sta $d404,x
ldy #$02
lda (ntTemp1),y
sta $d402,x
sta $d403,x
dey
lda (ntTemp1),y
sta $d405,x
dey
lda (ntTemp1),y
sta $d406,x
Play_SfxDone: rts
Play_SfxEnd: sta ntChnSfx,x
sta ntChnWavePos,x
sta ntChnWaveOld,x
beq Play_SfxWaveChg