aboutsummaryrefslogtreecommitdiff
path: root/src-z80/core/bgm.z80
blob: c1650f13335ec32483674a2eb0a3a8f5831f4d7b (plain)
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
;****************************************************************************
; PlayBGM [command $04]
; Plays a BGM
;****************************************************************************

PlayBGM:
    PollPCM
    call ClearBGM               ; Clear BGM resources
    PollPCM

    ld a, (RAM_Status)          ; Show BGM playback in Echo's status
    or $02
    ld (RAM_Status), a

    PollPCM

    ld hl, RAM_ComBank          ; Get command parameters
    ld c, (hl)
    inc l
    ld e, (hl)
    inc l
    ld d, (hl)

    PollPCM

    xor a                       ; Command parsed
    ld (RAM_Command), a

    ld hl, RAM_BGMData          ; Set BGM as playing
    ld (hl), $01
    inc l                       ; No delays!
    ld (hl), $01
    inc l                       ; Store BGM start bank
    ld (hl), c
    inc l                       ; Store BGM start address (low)
    ld (hl), e
    inc l                       ; Store BGM start address (high)
    ld (hl), d

    PollPCM

    ld hl, ProcessBGM           ; Tell Echo to process BGM
    ld (DoTick_BGM+1), hl

    PollPCM
    jp IdleLoop                 ; End of subroutine

;****************************************************************************
; ProcessBGM
; Processes a tick for a BGM
;****************************************************************************

ProcessBGM:
    PollPCM

    ld hl, RAM_BGMData+1        ; BGM data address

    ld a, (hl)                  ; Delaying?
    dec a
    jp z, .nodelay
    ld (hl), a

    jp DoTick_BGMSkip           ; End of subroutine

.nodelay:
    PollPCM

    inc l
    ld c, (hl)                  ; Get current address
    inc l
    ld e, (hl)
    inc l
    ld d, (hl)
    ex de, hl

ProcessBGMRun:
    PollPCM                     ; Fetch next event
    call GetParam
    PollPCM

    ld a, b                     ; Parse byte

    cp $08
    jp c, NoteOnFMBGM           ; Events $00-$07: note on FM
    cp $0B
    jp c, NoteOnPSGBGM          ; Events $08-$0A: note on PSG (square)
    jp z, NoteOnNoiseBGM        ; Event $0B: note on PSG (noise)
    cp $0C
    jp z, PlayPCMBGM            ; Event $0C: play PCM

    PollPCM
    ld a, b

    cp $18
    jp c, NoteOffFMBGM          ; Events $10-$17: note off FM
    cp $1C
    jp c, NoteOffPSGBGM         ; Events $18-$1B: note off PSG
    jp z, StopPCMBGM            ; Event $1C: note off PCM

    PollPCM
    ld a, b

    cp $FE
    jp z, SetDelayBGM           ; Event $FE: set delay
    cp $FF
    jp z, StopBGMEvent          ; Event $FF: stop BGM
    cp $FC
    jp z, LoopBGM               ; Event $FC: loop BGM
    cp $FD
    jp z, SetLoopBGM            ; Event $FD: set loop point

    PollPCM
    ld a, b

    cp $28
    jp c, SetFMVolBGM           ; Events $28-$2B: set FM volume
    cp $2C
    jp c, SetPSGVolBGM          ; Events $28-$2B: set PSG volume

    PollPCM
    ld a, b

    cp $38
    jp c, SetNoteFMBGM          ; Events $30-$37: set FM note
    cp $3B
    jp c, SetNotePSGBGM         ; Events $38-$3A: set PSG note (square)
    jp z, SetNoteNoiseBGM       ; Event $3B: set PSG note (noise)

    PollPCM
    ld a, b

    cp $48
    jp c, LoadFMBGM             ; Events $40-$47: load FM instrument
    cp $4C
    jp c, LoadPSGBGM            ; Events $48-$4B: load PSG instrument

    PollPCM
    ld a, b

    cp $F8                      ; Events $F0-$F7: set FM parameters
    jp c, SetFMParamBGM

    PollPCM                     ; FFFFFFFFF bad event >:(
    jp StopBGMEvent             ; End of subroutine

ProcessBGMSkip2:                ; This is where we land after a locked event
    PollPCM                       ; that had two bytes for the parameter
    inc l                         ; Skip first byte
    jp nz, .nobankskip2
    inc h
    jp nz, .nobankskip2
    ld h, $80
    inc c
.nobankskip2:

ProcessBGMSkip1:                ; This is where we land after a locked event
    PollPCM                       ; that had one byte for the parameter
    inc l                         ; Skip byte
    jp nz, .nobankskip1
    inc h
    jp nz, .nobankskip1
    ld h, $80
    inc c
.nobankskip1:

ProcessBGMSkip:                 ; This is where we land after a locked event
    PollPCM                       ; without parameters

    jp ProcessBGMRun            ; Keep processing

;****************************************************************************
; StopBGM* [command $05, event $FF]
; Stops BGM playback
;****************************************************************************

StopBGMEvent:
    call StopBGM                ; We're just a wrapper
    jp DoTick_BGMSkip           ; End of subroutine

StopBGMCmd:
    xor a                       ; Command parsed
    ld (RAM_Command), a
    call StopBGM                ; We're just a wrapper
    jp IdleLoop                 ; End of subroutine

StopBGM:
    ld a, (RAM_Status)          ; Hide BGM playback in Echo's status
    and $FD
    ld (RAM_Status), a

    PollPCM

    call ClearBGM               ; Clear BGM resources
    PollPCM

    xor a                       ; Stop playback
    ld (RAM_BGMPlaying), a
    ld hl, DoTick_BGMSkip
    ld (DoTick_BGM+1), hl

    ret                         ; End of subroutine

;****************************************************************************
; ClearBGM
; Clears BGM resources
;****************************************************************************

ClearBGM:
    ld a, (RAM_Locked+6)        ; Stop PCM playback if needed
    or a
    call z, StopPCM

;----------------------------------------------------------------------------

    ld b, 4                     ; Reset all PSG channels
    ld de, RAM_PSGData+48+15
    ld hl, RAM_Locked+11
.mutepsg:
    PollPCM
    
    ld (hl), $00                ; Reset BGM volume
    
    ld a, e
    sub 15
    ld e, a
    
    ld a, (hl)                  ; Mute PSG channel if it isn't locked
    or a
    jr nz, .nopsgmute
    xor a
    ld (de), a
    
.nopsgmute:
    PollPCM
    
    dec e
    dec l
    djnz .mutepsg

;----------------------------------------------------------------------------

    ld b, 8                     ; Reset all FM channels
    ld de, RAM_BGMFMVol+7
.resetfm:
    PollPCM
    
    xor a                       ; Reset BGM volume
    ld (de), a
    dec e
    
    ld a, (hl)                  ; Kill FM channel if not locked
    or a
    jp nz, .nofmkill
    
    PollPCM
    
    ld a, b
    dec a
    call KillFM
    
.nofmkill:
    dec l
    djnz .resetfm

;----------------------------------------------------------------------------

    ret                         ; End of subroutine

;****************************************************************************
; LoopBGM [event $FC]
; Makes a BGM loop
;****************************************************************************

LoopBGM:
    PollPCM

    ex de, hl                   ; Get looping address
    inc l
    ld c, (hl)
    inc l
    ld e, (hl)
    inc l
    ld d, (hl)
    dec l
    dec l
    dec l
    ex de, hl

    jp ProcessBGMRun            ; End of subroutine

;****************************************************************************
; SetLoopBGM [event $FD]
; Sets the BGM loop point
;****************************************************************************

SetLoopBGM:
    PollPCM

    ex de, hl
    inc l                       ; Store loop point address
    ld (hl), c
    inc l
    ld (hl), e
    inc l
    ld (hl), d
    dec l
    dec l
    dec l
    ex de, hl

    jp ProcessBGMRun            ; End of subroutine