-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrono_msu1_music.asm
894 lines (754 loc) · 13.2 KB
/
chrono_msu1_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
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
arch snes.cpu
// MSU memory map I/O
constant MSU_STATUS($002000)
constant MSU_ID($002002)
constant MSU_AUDIO_TRACK_LO($002004)
constant MSU_AUDIO_TRACK_HI($002005)
constant MSU_AUDIO_VOLUME($002006)
constant MSU_AUDIO_CONTROL($002007)
// SPC communication ports
constant SPC_COMM_0($2140)
constant SPC_COMM_1($2141)
constant SPC_COMM_2($2142)
constant SPC_COMM_3($2143)
// MSU_STATUS possible values
constant MSU_STATUS_TRACK_MISSING($8)
constant MSU_STATUS_AUDIO_PLAYING(%00010000)
constant MSU_STATUS_AUDIO_REPEAT(%00100000)
constant MSU_STATUS_AUDIO_BUSY($40)
constant MSU_STATUS_DATA_BUSY(%10000000)
// SNES Multiply register
constant SNES_MUL_OPERAND_A($004202)
constant SNES_MUL_OPERAND_B($004203)
constant SNES_DIV_DIVIDEND_L($004204)
constant SNES_DIV_DIVIDEND_H($004205)
constant SNES_DIV_DIVISOR($004206)
constant SNES_DIV_QUOTIENT_L($004214)
constant SNES_DIV_QUOTIENT_H($004215)
constant SNES_MUL_DIV_RESULT_L($004216)
constant SNES_MUL_DIV_RESULT_H($004217)
// Constants
constant FULL_VOLUME($FF)
constant BATTLE1_MUSIC($45)
constant THEME_LOOP($18)
constant THEME_ATTRACT($54)
constant ENDING_MUSIC($3F)
constant EPOCH_1999AD_MUSIC($50)
// =============
// = Variables =
// =============
// Game Variables
variable musicCommand($1E00)
variable musicRequested($1E01)
variable targetVolume($1E02)
variable soundBankRequested($1E10)
// My own variables
variable currentSong($7E1EE0)
variable fadeCount($7E1EE1)
variable fadeVolume($7E1EE2)
variable fadeStep($7E1EE4)
variable counter($7E1EE6)
variable frameCounter($7E1EE8)
variable inCombatHack($7E1EE9)
// **********
// * Macros *
// **********
// seek converts SNES HiROM address to physical address
macro seek(variable offset) {
origin (offset & $3FFFFF)
base offset
}
macro CheckMSUPresence(labelToJump) {
lda MSU_ID
cmp.b #'S'
bne {labelToJump}
}
macro WaitMulResult() {
nop
nop
nop
nop
}
macro WaitDivResult() {
nop
nop
nop
nop
nop
nop
nop
nop
}
// ========================
// = Original code hijack =
// ========================
// NMI hijack
seek($00FF10)
jml MSU_UpdateLoop
// Wait for song to finish command
seek($C03CC6)
jsl MSU_WaitSongFinish
nop
nop
nop
nop
// Wait for song to start (found when switching characters on overworld)
seek($C2CBE0)
jsl MSU_WaitSongStart
nop
nop
nop
nop
// Bike Race ending camera animation fix
// This is a lazy destructive hijack to the compressed code
// Original code check if the song has started:
// 7e475c lda $2143
// 7e475f and #$0f
// 7e4761 beq $4771
seek($C3257B)
lda.b #$01
nop
// Wait for title screen fix
// This chunk of code is copied into RAM
// by the decompression routine, that's why
// it got a db $80 in the middle
seek($C335AF)
nop
db $80
jsl MSU_WaitSongStart
nop
nop
// Epoch 1999 AD and Ending timing fix
seek($C30C28)
SoundCheckSuccessful:
// Equivalent to bra $0bb5
db $80,$8B
jsl MSU_EpochAndEndingFix
bcs SoundCheckSuccessful
rts
// Epoch 1999 AD event modification
// This is a destructive modification
// Remove syncronisation with music in the event
seek($FA96B1)
// 10 10 is Event Command Jump Forward ten bytes
// This is compressed data so the modification is
// replicated 3 times
db $10, $10, 0, 0, 0
// Fix for in normal combat with custom music, wrong sfx
// Tell the SPC routine to always load the data
seek($C70A8A)
jml MSU_BattleSampleHack
// Relevant calls to $C70004
// Found via hex editor by searching for JSL $C70004
seek($C01B73)
jsl MSU_Main
// Entering area
seek($C01B8B)
jsl MSU_Main
// Entering battle
seek($C01BCE)
jsl MSU_Main
seek($C01C2A)
jsl MSU_Main
// Exiting battle
seek($C01C3A)
jsl MSU_Main
seek($C01C52)
jsl MSU_Main
seek($C01CA9)
jsl MSU_Main
seek($C01CB7)
jsl MSU_Main
seek($C01CCF)
jsl MSU_Main
// Called during attract
seek($C03C43)
jsl MSU_Main
seek($C03C69)
jsl MSU_Main
seek($C03CB4)
jsl MSU_Main
seek($C161DF)
jsl MSU_Main
seek($C20462)
jsl MSU_Main
seek($C223F9)
jsl MSU_Main
seek($C22F49)
jsl MSU_Main
seek($C2CBF3)
jsl MSU_Main
seek($C2CC09)
jsl MSU_Main
seek($C309D1)
jsl MSU_Main
seek($C30BE9)
jsl MSU_Main
// Title Screen
seek($C31647)
jsl MSU_Main
seek($CD03AB)
jsl MSU_Main
seek($CD0D70)
jsl MSU_Main
seek($CD0D81)
jsl MSU_Main
// Pause during battle
seek($CD3E54)
jsl MSU_Main
// Unpause during battle
seek($CD3E70)
jsl MSU_Main
// Hijack for music in attract mode
seek($DB6E03)
db THEME_ATTRACT
seek($FA24A4)
db THEME_ATTRACT
seek($FA28FA)
db THEME_ATTRACT
seek($FA4925)
db THEME_ATTRACT
seek($FA4962)
db THEME_ATTRACT
seek($FA659C)
db THEME_ATTRACT
// ============
// = MSU Code =
// ============
seek($C5F370)
scope MSU_Main: {
php
// Backup A and Y in 16bit mode
rep #$30
pha
phx
phy
phd
phb
sep #$30 // Set all registers to 8 bit mode
CheckMSUPresence(.CallOriginalRoutine)
lda.w musicCommand
// Play Music
cmp.b #$10
bne +
jsr MSU_PlayMusic
bcs .CallOriginalRoutine
bcc .DoNotCallSPCRoutine
+
// Resume
cmp.b #$11
bne +
if {defined RESUME_EXPERIMENT} {
jsr MSU_PlayMusic
} else {
jsr MSU_ResumeMusic
}
bcs .CallOriginalRoutine
bcc .DoNotCallSPCRoutine
+
// Interrupt
cmp.b #$14
bne +
jsr MSU_PauseMusic
bcs .CallOriginalRoutine
bcc .DoNotCallSPCRoutine
+
// Fade
cmp.b #$81
bne +
jsr MSU_PrepareFade
bcs .CallOriginalRoutine
bcc .DoNotCallSPCRoutine
+
// Pause/unpause during battle
cmp.b #$F5
bne +
jsr MSU_PauseUnpause
+
// Call original routine
.CallOriginalRoutine:
// Restore original theme when MSU-1 is not present
lda.b #$10
cmp.w musicCommand
bne Original
lda.b #THEME_ATTRACT
cmp.w musicRequested
bne Original
lda.b #THEME_LOOP
sta.w musicRequested
Original:
rep #$30
plb
pld
ply
plx
pla
plp
jsl $C70004
rtl
.DoNotCallSPCRoutine:
rep #$30
plb
pld
ply
plx
pla
plp
rtl
}
scope MSU_PlayMusic: {
lda.w musicRequested
beq .StopMSUMusic
cmp.b #$FF
beq .SongAlreadyPlaying
cmp currentSong
beq .SongAlreadyPlaying
sta MSU_AUDIO_TRACK_LO
lda.b #$00
sta MSU_AUDIO_TRACK_HI
.CheckAudioStatus:
lda MSU_STATUS
and.b #MSU_STATUS_AUDIO_BUSY
bne .CheckAudioStatus
// Check if track is missing
lda MSU_STATUS
and.b #MSU_STATUS_TRACK_MISSING
bne .StopMSUMusic
// Play the song
lda.w musicRequested
jsr TrackNeedLooping
if {defined RESUME_EXPERIMENT} {
ldx musicCommand
cpx #$11
bne .SetAudioControl
// Add resume flag
sta MSU_AUDIO_CONTROL
bra SkipSetVolume
.SetAudioControl:
}
sta MSU_AUDIO_CONTROL
// Set volume
lda.b #FULL_VOLUME
sta.l MSU_AUDIO_VOLUME
if {defined RESUME_EXPERIMENT} {
SkipSetVolume:
}
// Only store current song if we were able to play the song
lda.w musicRequested
sta currentSong
// Set SPC music to silence and disable any fade if any was active
lda #$00
sta.w musicRequested
// Reset counter for Epoch 1999AD and Ending
sta counter
sta counter+1
sec
bra .Exit
.SongAlreadyPlaying:
clc
.Exit:
rts
.StopMSUMusic:
lda.b #$00
sta MSU_AUDIO_CONTROL
sta.l MSU_AUDIO_VOLUME
sta currentSong
sec
bra .Exit
}
scope MSU_ResumeMusic: {
// Little hack to fix issues in Ozzie's Fort
// where the game calls $11 instead of $10 to play
// dungeon music and $10 to play normal combat music
lda inCombatHack
beq .PlayMusic
lda #$00
sta inCombatHack
lda MSU_STATUS
and.b #MSU_STATUS_TRACK_MISSING
bne .CallOriginalCode
.PlayMusic:
lda.w musicRequested
cmp currentSong
beq +
jmp MSU_PlayMusic
+
sta currentSong
lda.b #$03
sta MSU_AUDIO_CONTROL
// Play silence after resuming music to
// reload correct SFX samples
lda.b #$10
sta.w musicCommand
lda.b #$00
sta.w musicRequested
.CallOriginalCode:
sec
rts
}
scope MSU_PauseMusic: {
if {defined RESUME_EXPERIMENT} {
lda.b #$4
sta MSU_AUDIO_CONTROL
jml MSU_PlayMusic
} else {
lda.w musicRequested
cmp.b #BATTLE1_MUSIC
beq .PauseMSUMusic
jml MSU_PlayMusic
.PauseMSUMusic:
lda #$01
sta inCombatHack
lda MSU_STATUS
and.b #MSU_STATUS_TRACK_MISSING
bne +
lda.b #$00
sta MSU_AUDIO_CONTROL
+
sec
rts
}
}
// c5f478
scope MSU_PrepareFade: {
// musicRequested = timing, targetVolume
lda.w musicRequested
sta fadeCount
bne .ComputeFade
.SetVolumeImmediate:
sta fadeCount
sta fadeStep
sta fadeStep+1
sta fadeVolume
lda.w targetVolume
sta.l MSU_AUDIO_VOLUME
sta fadeVolume+1
bra .Exit
.ComputeFade:
lda.w targetVolume
sec
sbc fadeVolume+1
beq .SetVolumeImmediate
php
bcs .IsCarrySet
eor #$FF
inc
.IsCarrySet:
// targetVolume / timing
sta SNES_DIV_DIVIDEND_L
lda #$00
sta SNES_DIV_DIVIDEND_H
lda musicRequested
sta SNES_DIV_DIVISOR
WaitDivResult()
lda SNES_DIV_QUOTIENT_L
sta fadeStep+1
lda #$00
sta SNES_DIV_DIVIDEND_L
lda SNES_MUL_DIV_RESULT_L
sta SNES_DIV_DIVIDEND_H
lda musicRequested
sta SNES_DIV_DIVISOR
WaitDivResult()
lda SNES_DIV_QUOTIENT_L
sta fadeStep
plp
bcs .IsCarrySet2
lda fadeStep
eor #$FF
sta fadeStep
lda fadeStep+1
eor #$FF
sta fadeStep+1
rep #$20
lda fadeStep
inc
sta fadeStep
sep #$20
.IsCarrySet2:
stz fadeVolume
.Exit:
sec
rts
}
scope MSU_PauseUnpause: {
lda MSU_STATUS
and.b #MSU_STATUS_TRACK_MISSING
bne .Exit
if {defined RESUME_EXPERIMENT} {
} else {
lda inCombatHack
bne .Exit
}
lda.w musicRequested
cmp.b #$F5
bne .Unpause
.Pause:
lda.b #$00
sta MSU_AUDIO_CONTROL
bra .Exit
.Unpause:
lda.b #$03
sta MSU_AUDIO_CONTROL
.Exit:
rts
}
scope TrackNeedLooping: {
// 1.01 A Premonition
cmp.b #48
beq .noLooping
// 1.02 Theme of Chrono Trigger (Attract)
cmp.b #THEME_ATTRACT
beq .noLooping
// 1.03 Morning Glow
cmp.b #15
beq .noLooping
// 1.10 Good Night
cmp.b #43
beq .noLooping
// 1.14 Huh ?!
cmp.b #37
beq .noLooping
// 1.16 A Prayer for the Wayfarer
cmp.b #36
beq .noLooping
// 2.02 Mystery from the Past
cmp.b #46
beq .noLooping
// 2.12 Fanfare 2
cmp.b #28
beq .noLooping
// 2.15 Fanfare 3
cmp.b #61
beq .noLooping
// 2.22 Fiedlord's Keep
cmp.b #72
beq .noLooping
// 3.14 To Far Away Times (Ending)
cmp.b #63
beq .noLooping
lda.b #$03
rts
.noLooping:
lda.b #$01
rts
}
scope MSU_UpdateLoop: {
php
rep #$20
pha
sep #$20
CheckMSUPresence(.CallNMI)
lda frameCounter
inc
sta frameCounter
cmp.b #60
bmi .CheckFade
lda.b #$0
sta frameCounter
// Increment counter at each second (each 60 fps)
// Will be used to fake timing for Epoch 1999 scene and ending
rep #$20
lda counter
inc
sta counter
sep #$20
.CheckFade:
lda frameCounter
lsr
bcs .CallNMI
lda fadeCount
beq .CallNMI
dec
sta fadeCount
clc
rep #$20
lda fadeVolume
adc fadeStep
sta fadeVolume
sep #$20
lda fadeVolume+1
sta MSU_AUDIO_VOLUME
.CallNMI:
rep #$20
pla
plp
jml $000500
}
scope MSU_WaitSongStart: {
php
rep #$20
pha
sep #$20
CheckMSUPresence(.OriginalCode)
rep #$20
pla
plp
rtl
.OriginalCode:
rep #$20
pla
plp
// Original code
-
lda $2143
and.b #$0F
beq -
rtl
}
scope MSU_WaitSongFinish: {
php
rep #$20
pha
sep #$20
CheckMSUPresence(.OriginalCode)
lda MSU_STATUS
and.b #(MSU_STATUS_AUDIO_PLAYING|MSU_STATUS_AUDIO_REPEAT)
bne +
inx
+
rep #$20
pla
plp
rtl
.OriginalCode:
rep #$20
pla
plp
lda $2143
and.b #$0F
bne +
inx
+
rtl
}
scope MSU_EpochAndEndingFix: {
sep #$20
lda currentSong
cmp.b #EPOCH_1999AD_MUSIC
beq .Epoch
cmp.b #ENDING_MUSIC
beq .Ending
jmp .RoutineSuccessful
.Epoch:
rep #$20
lda counter
cmp #49
bmi +
sep #$20
lda.b #5
jmp .OriginalCheck
+
cmp #21
bmi +
sep #$20
lda.b #3
jmp .OriginalCheck
+
cmp #16
bmi +
sep #$20
lda.b #2
jmp .OriginalCheck
+
cmp #7
bmi +
sep #$20
lda.b #1
jmp .OriginalCheck
+
sep #$20
lda.b #0
jmp .OriginalCheck
.Ending:
rep #$20
lda counter
cmp #280 // $118 or $1801 (little endian)
bmi +
sep #$20
lda.b #$A
bra .OriginalCheck
+
cmp #253 // $FD
bmi +
sep #$20
lda.b #$9
bra .OriginalCheck
+
cmp #193 // $C1
bmi +
sep #$20
lda.b #$8
bra .OriginalCheck
+
cmp #159 // $9F
bmi +
sep #$20
lda.b #$7
bra .OriginalCheck
+
cmp #127 // $7F
bmi +
sep #$20
lda.b #$6
bra .OriginalCheck
+
cmp #104 // $68
bmi +
sep #$20
lda.b #$5
bra .OriginalCheck
+
cmp #77 // $4D
bmi +
sep #$20
lda.b #$4
bra .OriginalCheck
+
cmp #45 // $2D
bmi +
sep #$20
lda.b #$3
bra .OriginalCheck
+
cmp #24 // $18
bmi +
sep #$20
lda.b #$2
bra .OriginalCheck
+
cmp #17 // $11
bmi +
sep #$20
lda.b #$1
bra .OriginalCheck
+
sep #$20
lda.b #$0
bra .OriginalCheck
.OriginalCheck:
and.b #$0F
cmp ($20)
bpl .RoutineSuccessful
rep #$20
dec $20
clc
rtl
.RoutineSuccessful:
sec
rtl
}
scope MSU_BattleSampleHack: {
lda.w soundBankRequested
beq .NotInCombat
jml $C70A96
.NotInCombat:
dec
-
cmp $2140
beq -
pla
pla
pla
jml $C70192
}