-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmmx_msu1_music.asm
316 lines (259 loc) · 4.94 KB
/
mmx_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
lorom
; MSU memory map I/O
MSU_STATUS = $2000
MSU_ID = $2002
MSU_AUDIO_TRACK_LO = $2004
!MSU_AUDIO_TRACK_HI = $2005
!MSU_AUDIO_VOLUME = $2006
!MSU_AUDIO_CONTROL = $2007
; SPC communication ports
SPC_PORT_0 = $2140
; MSU_STATUS possible values
MSU_STATUS_TRACK_MISSING = $8
MSU_STATUS_AUDIO_PLAYING = %00010000
MSU_STATUS_AUDIO_REPEAT = %00100000
MSU_STATUS_AUDIO_BUSY = $40
MSU_STATUS_DATA_BUSY = %10000000
; Constants
FULL_VOLUME = $FF
DUCKED_VOLUME = $60
FADE_DELTA = ($FF/45)
; Variables
fadeState = $180
fadeVolume = $181
; FADE_STATE possibles values
FADE_STATE_IDLE = $00
FADE_STATE_FADEOUT = $01
FADE_STATE_FADEIN = $02
; Fade-in/Fade-out hijack in NMI routine
org $80817A
jsr MSU_FadeUpdate
; Add a hook to where the sound effects/special commands are played
org $80885B
jsr MSU_SoundEffectsAndCommand
; At Capcom logo, init the required variables
org $808613
jsr MSU_Init
; Play music from Options Screen, All music after level music, Password Screen, Stage Select
org $8087AA
jsr MSU_Main
; Play Title Screen
org $808D8F
jsr MSU_Main
; Play music at level load
org $809A2D
jmp MSU_Main
; Play stage selected jingle
org $809709
jsr MSU_Main
; Ending ??
org $809CFA
jsr MSU_Main
; Got a weapon
org $80ABD6
jsr MSU_Main
; A = Music to play + $10
org $80FBCE
MSU_Main:
php
; Backup A and Y in 16bit mode
rep #$30
pha
phy
sep #$30 ; Set all registers to 8 bit mode
tay
; Check if MSU-1 is present
lda MSU_ID
cmp #'S'
bne .MSUNotFound
.MSUFound:
; Set track
tya
clc
sbc #$F
tay
sta MSU_AUDIO_TRACK_LO
stz !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 .MSUNotFound
; Play the song and add repeat if needed
jsr TrackNeedLooping
sta !MSU_AUDIO_CONTROL
; Set volume
lda.b #FULL_VOLUME
sta.w !MSU_AUDIO_VOLUME
; Reset the fade state machine
lda #$00
sta fadeState
rep #$30
ply
pla
plp
rts
; Call original routine if MSU-1 is not found
.MSUNotFound:
rep #$30
ply
pla
plp
jsr $87B0
rts
MSU_Init:
php
sep #$30
pha
; Check if MSU-1 is present
lda MSU_ID
cmp #'S'
bne .MSUNotFound
; Set volume
lda.b #FULL_VOLUME
sta.w !MSU_AUDIO_VOLUME
; Reset the fade state machine
lda.b #$00
sta.l fadeState
.MSUNotFound
pla
plp
jsr $87B0
rts
TrackNeedLooping:
; Capcom Jingle
cpy #$00
beq .noLooping
; Title Screen
cpy #$0F
beq .noLooping
; Victory Jingle
cpy #$11
beq .noLooping
; Stage Selected Jingle
cpy #$12
beq .noLooping
; Got a Weapon
cpy #$17
beq .noLooping
; Boss Tension 1
cpy #$1E
beq .noLooping
lda #$03
rts
.noLooping:
lda #$01
rts
MSU_SoundEffectsAndCommand:
php
sep #$30
pha
lda MSU_ID
cmp #'S'
bne .MSUNotFound_SE
pla
; $F5 is a command to resume music
cmp #$F5
beq .ResumeMusic
; $F6 is a command to fade-out music
cmp #$F6
beq .StopMusic
; $FE is a command to raise volume back to full volume coming from pause menu
cmp #$FE
beq .RaiseVolume
; $FF is a command to drop volume when going to pause menu
cmp #$FF
beq .DropVolume
; If not, play the sound as the game excepts to
bra .PlaySound
.ResumeMusic:
; Stop the SPC music if any
lda #$F6
sta SPC_PORT_0
; Resume music then fade-in to full volume
lda #$03
sta !MSU_AUDIO_CONTROL
lda.b #FADE_STATE_FADEIN
sta fadeState
lda #$00
sta fadeVolume
bra .CleanupAndReturn
.StopMusic:
sta SPC_PORT_0
lda MSU_STATUS
and.b #MSU_STATUS_AUDIO_PLAYING
beq .CleanupAndReturn
; Fade-out current music then stop it
lda.b #FADE_STATE_FADEOUT
sta fadeState
lda.b #FULL_VOLUME
sta fadeVolume
bra .CleanupAndReturn
.RaiseVolume:
sta.w SPC_PORT_0
lda.b #FULL_VOLUME
sta.w !MSU_AUDIO_VOLUME
bra .CleanupAndReturn
.DropVolume:
sta.w SPC_PORT_0
lda.b #DUCKED_VOLUME
sta.w !MSU_AUDIO_VOLUME
bra .CleanupAndReturn
.MSUNotFound_SE:
pla
.PlaySound:
sta SPC_PORT_0
.CleanupAndReturn:
plp
rts
MSU_FadeUpdate:
; Original code I hijacked that increase the real frame counter
inc.w $0B9E
php
sep #$30
pha
lda MSU_ID
cmp #'S'
bne .MSUNotFound
; Switch on fade state
lda fadeState
cmp.b #FADE_STATE_IDLE
beq .MSUNotFound
cmp.b #FADE_STATE_FADEOUT
beq .FadeOutUpdate
cmp.b #FADE_STATE_FADEIN
beq .FadeInUpdate
bra .MSUNotFound
.FadeOutUpdate:
lda fadeVolume
sec
sbc.b #FADE_DELTA
bcs +
lda #$0
+
sta fadeVolume
sta.w !MSU_AUDIO_VOLUME
beq .SetToIdle
bra .MSUNotFound
.FadeInUpdate:
lda fadeVolume
clc
adc.b #FADE_DELTA
bcc +
lda.b #FULL_VOLUME
+
sta fadeVolume
sta.w !MSU_AUDIO_VOLUME
cmp.b #FULL_VOLUME
beq .SetToIdle
bra .MSUNotFound
.SetToIdle:
lda.b #FADE_STATE_IDLE
sta fadeState
.MSUNotFound
pla
plp
rts