aboutsummaryrefslogtreecommitdiff
path: root/src-z80/player/pcm (copia).z80
blob: ae7b34a7fd82de05b4c5c50dbdf5dd2912557ac3 (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
;****************************************************************************
; UpdatePCM
; Updates PCM output upon a timer event
;****************************************************************************

UpdatePCM:
    ret                         ; $C9 = RET    = no PCM playback
                                ; $D0 = RET NC = PCM playback
    
    exx                         ; Switch to PCM registers

.doagain:
    ld (ix+0), $27              ; Acknowledge timer
    ld (ix+1), $1F

    ld a, (hl)                  ; Fetch next sample
    inc a                       ; Check if it's the end of the waveform
    jr z, .stop
    ld (ix+0), $2A              ; Nope, send sample to YM2612
    ld (ix+1), a
    
    inc l                       ; Update buffer position
    jr z, .reload               ; Need to buffer more?

.nopcm:
    exx                         ; Switch to normal registers
    ret                         ; End of subroutine

.stop:
    ld a, $C9                   ; Stop playback
    ld (UpdatePCM), a
    ld (ix+0), $2A              ; Turn off DAC
    ld (ix+1), $80
    ld (ix+0), $2B
    ld (ix+1), $00
    exx                         ; Switch to normal registers
    ret                         ; End of subroutine

.reload:
    ld a, (RAM_LastBank)        ; Bank switch if needed
    cp c
    jp z, .noswitchu
    ld a, c
    ld (RAM_LastBank), a
    ld hl, $6000
    BankSwitch
.noswitchu:
    
    ld hl, RAM_PCMBuffer        ; Load samples into the buffer
    ld a, c
    ex de, hl
    
    ldi
    ldi
    ldi
    ldi
    
    ldi
    ldi
    ldi
    ldi
    
    ldi
    ldi
    ldi
    ldi
    
    ldi
    ldi
    ldi
    ldi
    
    ex de, hl
    ld c, a
    
    ld a, e
    or a
    jp nz, .nobankchg           ; Update high bytes of address if needed
    inc d
    jp nz, .nobankchg
    ld d, $80
    inc c
.nobankchg:
    
    ld l, RAM_PCMBuffer&$FF     ; Go back to the beginning of the buffer
    jp .doagain                 ; We took so long we should play the next
                                  ; sample already ._.'

;****************************************************************************
; PlayPCM* [event $0C]
; Plays a PCM sample
;----------------------------------------------------------------------------
; input c .... current bank
; input hl ... current address
;----------------------------------------------------------------------------
; breaks: af, b
;****************************************************************************

PlayPCMSFX:
    call PlayPCM                ; We're just a wrapper
    jp ProcessSFXRun            ; End of subroutine

PlayPCMBGM:
    PollPCM

    ld a, (RAM_Locked+6)        ; Check if channel is free
    or a
    jp nz, ProcessBGMSkip1      ; Don't play sample if locked

    call PlayPCM                ; We're just a wrapper
    jp ProcessBGMRun            ; End of subroutine

PlayPCM:
    ld a, (RAM_GlobalVol+$0C)   ; Are we allowed to play PCM?
    or a
    ret z
    
    call GetParam               ; Get sample ID
    
    ld a, b
    exx                         ; We'll modify PCM data now

    ld h, RAM_PointerList>>8    ; Get offset in pointer list
    ld l, a

    ld d, (hl)                  ; Get PCM address
    inc h
    ld e, (hl)
    inc h
    ld c, (hl)
    
    ld hl, $6000                ; Initial bank switch
    ld a, c
    ld (RAM_LastBank), a
    BankSwitch
    
    ld h, RAM_PCMBuffer>>8      ; Set buffer where the sample starts
    ld a, e
    or $F0
    ld l, a
    
    ld b, l
.load1st:                       ; Copy initial samples into the buffer
    ld a, (de)
    ld (hl), a
    inc e
    inc l
    jp nz, .load1st
    ld l, b
    
    ld a, e                     ; Check if the sample should skip ahead
    or a                          ; already
    jp nz, .noskip1st
    inc d
    jp nz, .noskip1st
    ld d, $80
    inc c
.noskip1st:

    exx                         ; Back to standard registers
    ld a, $D0                   ; Enable PCM playback
    ld (UpdatePCM), a
    
    ld (ix+0), $2B              ; Turn on DAC
    ld (ix+1), $80
    ld (ix+0), $2A
    ld (ix+1), $80

    ret                         ; End of subroutine

;****************************************************************************
; StopPCM*
; Stops a PCM sample
;****************************************************************************

StopPCMSFX:
    call StopPCM                ; We're just a wrapper
    jp ProcessSFXRun            ; End of subroutine

StopPCMBGM:
    PollPCM

    ld a, (RAM_Locked+6)        ; Check if channel is free
    or a
    jp nz, ProcessBGMRun        ; Don't stop sample if locked

    call StopPCM                ; We're just a wrapper
    jp ProcessBGMRun            ; End of subroutine

StopPCM:
    ld a, $C9                   ; Stop PCM playback
    ld (UpdatePCM), a

    ld (ix+0), $2B              ; Disable DAC
    ld (ix+1), $00

    ret                         ; End of subroutine

;****************************************************************************
; LockChannelPCM [event $EC]
; Locks the PCM channel
;****************************************************************************

LockChannelPCM:
    ld a, $01                   ; Lock PCM channel
    ld (RAM_Locked+6), a
    
    call StopPCM                ; Stop PCM playback
    jp ProcessSFXRun            ; End of subroutine

;****************************************************************************
; SetPCMRate [command $07]
; Changes the sample rate of PCM
;****************************************************************************

SetPCMRate:
    ld a, (RAM_ComBank)         ; Get new rate
    cpl
    ld b, a
    
    xor a                       ; Parsed command already
    ld (RAM_Command), a
    
    ld a, b                     ; Set high bits of timer
    ld (ix+0), $24
    rrca
    rrca
    or $C0
    ld (ix+1), a
    
    ld a, b                     ; Set low bits of timer
    ld (ix+0), $25
    and $03
    ld (ix+1), a
    
    jp IdleLoop                 ; End of subroutine