-
Notifications
You must be signed in to change notification settings - Fork 1
/
mmx3_msu1.asm
293 lines (251 loc) · 4.95 KB
/
mmx3_msu1.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
arch snes.cpu
// MSU memory map I/O
constant MSU_STATUS($2000)
constant MSU_ID($2002)
constant MSU_AUDIO_TRACK_LO($2004)
constant MSU_AUDIO_TRACK_HI($2005)
constant MSU_AUDIO_VOLUME($2006)
constant MSU_AUDIO_CONTROL($2007)
// SPC communication ports
constant SPC_COMM_0($2140)
// 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)
// Constants
if {defined EMULATOR_VOLUME} {
constant FULL_VOLUME($60)
constant DUCKED_VOLUME($20)
constant FADE_DELTA(1)
} else {
constant FULL_VOLUME($FF)
constant DUCKED_VOLUME($60)
constant FADE_DELTA(2)
}
// Variables
variable fadeState($180)
variable fadeVolume($181)
// FADE_STATE possibles values
constant FADE_STATE_IDLE($00)
constant FADE_STATE_FADEOUT($01)
constant FADE_STATE_FADEIN($02)
// **********
// * Macros *
// **********
// seek converts SNES LoROM address to physical address
macro seek(variable offset) {
origin ((offset & $7F0000) >> 1) | (offset & $7FFF)
base offset
}
macro CheckMSUPresence(labelToJump) {
lda.w MSU_ID
cmp.b #'S'
bne {labelToJump}
}
// NMI/VBlank hijack
seek($068075)
jml MSU_VBlankUpdate
// Play Music Routine
seek($0084E4)
jsr MSU_Main
pla
rts
// Play SFX routine
seek($088442)
jsl MSU_SFXAndCommand
seek($00FD20)
scope MSU_Main: {
php
// Backup A and Y in 16 bit mode
rep #$30
pha
phy
sep #$30
tay
// Check if MSU-1 is present
CheckMSUPresence(OriginalCode)
// Set track
tya
sec
sbc.b #$11
tay
sta.w MSU_AUDIO_TRACK_LO
stz.w MSU_AUDIO_TRACK_HI
CheckMSUAudioStatus:
lda.w MSU_STATUS
and.b #MSU_STATUS_AUDIO_BUSY
bne CheckMSUAudioStatus
// Check if the track is missing
lda.w MSU_STATUS
and.b #MSU_STATUS_TRACK_MISSING
bne TrackIsMissing
// Play the song and add repeat if needed
jsr TrackNeedLooping
sta.w MSU_AUDIO_CONTROL
// Set volume
lda.b #FULL_VOLUME
sta.w MSU_AUDIO_VOLUME
sta.w fadeVolume
// Reset the fade state machine
lda.b #$00
sta.w fadeState
rep #$30
ply
pla
plp
rts
TrackIsMissing:
// Reset the fade state machine and drop the volume
// on track missing
lda.b #$00
sta.w fadeState
sta.w MSU_AUDIO_VOLUME
sta.w fadeVolume
OriginalCode:
rep #$30
ply
pla
plp
// Pop return address
rep #$20
pla
sep #$20
// Original Code
jsr $84FD
pla
sta $7EFFFD
ldy #$FA
jmp $83D5
}
scope TrackNeedLooping: {
// Capcom Jingle
cpy.b #$00
beq NoLooping
// Title Screen
cpy.b #$0B
beq NoLooping
// Boss Selected Jingle
cpy.b #$0F
beq NoLooping
// Victory Jingle
cpy.b #$13
beq NoLooping
// Got a Weapon Jingle
cpy.b #$1F
beq NoLooping
lda.b #$03
rts
NoLooping:
lda.b #$01
rts
}
scope MSU_SFXAndCommand: {
php
sep #$20
pha
CheckMSUPresence(MSUNotFound)
pla
// $F5 is a command to resume music
cmp.b #$F5
beq .ResumeMusic
// $F6 is a command to fade-out music
cmp.b #$F6
beq .StopMusic
// $FE is a command to raise volume back to full volume coming from pause menu
cmp.b #$FE
beq .RaiseVolume
// $FF is a command to drop volume when going to pause menu
cmp.b #$FF
beq .DropVolume
// If not, play the sound as the game excepts to
bra .PlaySound
.ResumeMusic:
// Stop the SPC music if any
lda.b #$F6
sta.w SPC_COMM_0
// Resume music then fade-in to full volume
lda.b #$03
sta.w MSU_AUDIO_CONTROL
lda.b #FADE_STATE_FADEIN
sta.w fadeState
lda.b #$00
sta.w fadeVolume
bra .CleanupAndReturn
.StopMusic:
sta.w SPC_COMM_0
lda.w MSU_STATUS
and.b #MSU_STATUS_AUDIO_PLAYING
beq .CleanupAndReturn
// Fade-out current music then stop it
lda.b #FADE_STATE_FADEOUT
sta.w fadeState
bra .CleanupAndReturn
.RaiseVolume:
sta.w SPC_COMM_0
lda.b #FULL_VOLUME
sta.w MSU_AUDIO_VOLUME
bra .CleanupAndReturn
.DropVolume:
sta.w SPC_COMM_0
lda.b #DUCKED_VOLUME
sta.w MSU_AUDIO_VOLUME
bra .CleanupAndReturn
MSUNotFound:
pla
.PlaySound:
sta.w SPC_COMM_0
.CleanupAndReturn:
plp
inx
rtl
}
scope MSU_VBlankUpdate: {
php
sep #$20
pha
CheckMSUPresence(OriginalCode)
// Switch on fade state
lda.w fadeState
cmp.b #FADE_STATE_IDLE
beq OriginalCode
cmp.b #FADE_STATE_FADEOUT
beq .FadeOutUpdate
cmp.b #FADE_STATE_FADEIN
beq .FadeInUpdate
bra OriginalCode
.FadeOutUpdate:
lda.w fadeVolume
sec
sbc.b #FADE_DELTA
bcs +
lda.b #$00
+;
sta.w fadeVolume
sta.w MSU_AUDIO_VOLUME
beq .FadeOutCompleted
bra OriginalCode
.FadeInUpdate:
lda.w fadeVolume
clc
adc.b #FADE_DELTA
bcc +
lda.b #FULL_VOLUME
+;
sta.w fadeVolume
sta.w MSU_AUDIO_VOLUME
cmp.b #FULL_VOLUME
beq .SetToIdle
bra OriginalCode
.FadeOutCompleted:
lda.b #$00
sta.w MSU_AUDIO_CONTROL
.SetToIdle:
lda.b #FADE_STATE_IDLE
sta.w fadeState
OriginalCode:
pla
plp
jml $7E2000
}