-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsmckit.inc
302 lines (294 loc) · 7.54 KB
/
zsmckit.inc
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
.import popa
.import pusha
.import popax
.importzp sreg
.include "zsmkit.inc"
.export _zsm_init_engine
.export _zsm_tick
.export _zsm_play
.export _zsm_stop
.export _zsm_rewind
.export _zsm_close
.export _zsm_fill_buffers
.export _zsm_setlfs
.export _zsm_setfile
.export _zsm_loadpcm
.export _zsm_setmem
.export _zsm_setatten
.export _zsm_setcb
.export _zsm_clearcb
.export _zsm_getstate
.export _zsm_setrate
.export _zsm_getrate
.export _zsm_setloop
.export _zsm_opmatten
.export _zsm_psgatten
.export _zsm_pcmatten
.export _zsm_set_int_rate
.export _zcm_setmem
.export _zcm_play
.export _zcm_stop
.export _zsmkit_setisr
.export _zsmkit_clearisr
RAM_BANK_SELECT = $00
.segment "CODE"
;zsm_init_engine(uint8_t bank);
_zsm_init_engine=zsm_init_engine
;zsm_tick(uint8_t what);
_zsm_tick=zsm_tick
;zsm_play(uint8_t priority);
_zsm_play:
tax ; Priority is in .A, move it to .X
jmp zsm_play
;zsm_stop(uint8_t priority);
_zsm_stop:
tax ; Priority is in .A, move it to .X
jmp zsm_stop
;zsm_rewind(uint8_t priority);
_zsm_rewind:
tax ; Priority is in .A, move it to .X
jmp zsm_rewind
;zsm_close(uint8_t priority);
_zsm_close:
tax ; Priority is in .A, move it to .X
jmp zsm_close
;zsm_fill_buffers();
_zsm_fill_buffers=zsm_fill_buffers
;zsm_setlfs(uint8_t lfnsa, uint8_t priority, uint8_t device);
_zsm_setlfs:
tay ; device - in .Y
jsr popa
tax ; priority - in .X
jsr popa ; lfn/sa - in .A
jmp zsm_setlfs
;zsm_setfile(uint8_t priority, char *filename);
_zsm_setfile:
pha ; low byte of filename address
txa
tay ; high byte of filename address - in .Y
jsr popa
tax ; priority - in .X
pla ; low byte of filename - in .A
jmp zsm_setfile
;uint16_t zsm_loadpcm(uint8_t priority, uint16_t addr, uint8_t bank);
_zsm_loadpcm:
sta RAM_BANK_SELECT ; set RAM bank
jsr popa
pha ; low address
jsr popa
pha ; high addres
jsr popa
tax ; priority - in .X
ply ; high address - in .Y
pla ; low address - in .A
jsr zsm_loadpcm
; low address - in .A
phy ; high address
plx ; high address - in .X
; .X .A = lo hi address
rts
;zsm_setmem(uint8_t priority, uint16_t addr, uint8_t bank);
_zsm_setmem:
sta RAM_BANK_SELECT ; Set RAM Bank
jsr popa ; low address
pha
jsr popa ; high address
pha
jsr popa ; priority
tax ; priority - in .X
ply ; high address - in .Y
pla ; low address - in .A
jmp zsm_setmem
;zsm_setatten(uint8_t priority, uint8_t attenuation);
_zsm_setatten:
pha ; attenuation
jsr popa ; priority
tax ; priority - in .X
pla ; attenuation - in .A
jmp zsm_setatten
;zsm_setcb(uint8_t priority, zsm_callback, uint8_t bank);
_zsm_setcb:
sta RAM_BANK_SELECT ; Set RAM Bank
jsr popax ; Address of callback function
pha ; low addr
jsr popa ; Priority
cmp #0
bne @is1
pla ; Here priority = 0
; Write addr of users CB function to internal CB function
sta @cb0j+1 ; low addr
stx @cb0j+2 ; high addr
ldx #0 ; priority - in .X
lda #<@cb0 ; Set own callback function that will call
ldy #>@cb0 ; users callback function
jmp zsm_setcb
@is1: cmp #1
bne @is2
pla ; Here priority = 1
; Write addr of users CB function to internal CB function
sta @cb1j+1 ; low addr
stx @cb1j+2 ; high addr
ldx #1 ; priority - in .X
lda #<@cb1 ; Set own callback function that will call
ldy #>@cb1 ; users callback function
jmp zsm_setcb
@is2: cmp #2
bne @is3
pla ; Here priority = 2
; Write addr of users CB function to internal CB function
sta @cb2j+1 ; low addr
stx @cb2j+2 ; high addr
ldx #2 ; priority
lda #<@cb2 ; Set own callback function that will call
ldy #>@cb2 ; users callback function
jmp zsm_setcb
@is3:
pla ; Here priority = 3
; Write addr of users CB function to internal CB function
sta @cb3j+1 ; low addr
stx @cb3j+2 ; high addr
ldx #3 ; priority
lda #<@cb3 ; Set own callback function that will call
ldy #>@cb3 ; users callback function
jmp zsm_setcb
; ****** Callback functions that will call users callback function *******
; ****** This is done to get the return values in the correct order ******
; ****** for the C function *******
;void callbackfunction(uint8_t eventtype, uint8_t priority, uint8_t paramval)
@cb0: pha ; Save .A as it must contain value for function
tya ; Push .Y to soft-stack
jsr pusha
txa ; Push .X to soft-stack
jsr pusha
pla ; Restore .A for function call
@cb0j: jmp $FFFF
;void callbackfunction(uint8_t eventtype, uint8_t priority, uint8_t paramval)
@cb1: pha
tya
jsr pusha
txa
jsr pusha
pla
@cb1j: jmp $FFFF
;void callbackfunction(uint8_t eventtype, uint8_t priority, uint8_t paramval)
@cb2: pha
tya
jsr pusha
txa
jsr pusha
pla
@cb2j: jmp $FFFF
;void callbackfunction(uint8_t eventtype, uint8_t priority, uint8_t paramval)
@cb3: pha
tya
jsr pusha
txa
jsr pusha
pla
@cb3j: jmp $FFFF
;zsm_clearcb(uint8_t priority);
_zsm_clearcb:
tax ; Priority is in .A, move it to .X
jmp zsm_clearcb
;struct zsm_state zsm_getstate(uint8_t priority);
_zsm_getstate:
tax ; Priority is in .A, move it to .X
jsr zsm_getstate
; ordering return values to the zsm_state structure
;typedef struct zsm_state {
; uint8_t playing;
; uint8_t not_playable;
; uint16_t loopcnt;
;};
sta sreg+0 ; Address returned in SREG
sty sreg+1
php ; Save flags, we want to use .Z and .C
pla
pha
lsr ; Get value of .Z into .A
and #$01
tax ; .X = value of .Z
pla ; Get value of .C into .A
and #$01 ; .A = value of .C
rts
;zsm_setrate(uint8_t priority, uint16_t tickrate);
_zsm_setrate:
pha ; Save low part of tickrate
txa ; Move .X to .Y, using .A
tay ; high part of tickrate - in .Y
jsr popa ; Get priority
tax ; priority - in .X
pla ; low part of tickrate - in .A
jmp zsm_setrate
;uint16_t zsm_getrate(uint8_t priority);
_zsm_getrate:
tax ; Priority is in .A, move it to .X
jsr zsm_getrate ; low part of rate - in .A
phy ; Move high part of rate from .Y to .X through .A
plx ; high part of rate - in .X
rts
;zsm_setloop(uint8_t priority, uint8_t loop);
_zsm_setloop:
pha ; Save "do loop" value
jsr popa ; Get priority
tax ; pririty - in .X
pla ; Restore "do loop" value
lsr ; "do loop" value - in .C
jmp zsm_setloop
;zsm_opmatten(uint8_t priority, uint8_t channel, uint8_t val);
_zsm_opmatten:
pha ; Save attenuation value
jsr popa ; Get channel
tay ; channel - in .Y
jsr popa ; Get priority
tax ; priority - in .X
pla ; attenuation value - in .A
jmp zsm_opmatten
;zsm_psgatten(uint8_t priority, uint8_t channel, uint8_t val);
_zsm_psgatten:
pha ; Save attenuation value
jsr popa ; Get channel
tay ; channel - in .Y
jsr popa ; Get priority
tax ; priority - in .X
pla ; attenuation value - in .A
jmp zsm_psgatten
;zsm_pcmatten(uint8_t priority, uint8_t channel, uint8_t val);
_zsm_pcmatten:
pha ; Save attenuation value
jsr popa ; Get channel
tay ; channel - in .Y
jsr popa ; Get priority
tax ; priority - in .X
pla ; attenuation value - in .A
jmp zsm_pcmatten
;zsm_set_int_rate(uint8_t herz, uint8_t in256th);
_zsm_set_int_rate:
tay ; fraction - in .Y
jsr popa ; frequency - in .A
jmp zsm_set_int_rate
;zcm_setmem(uint8_t priority, uint16_t addr, uint8_t bank);
_zcm_setmem:
sta RAM_BANK_SELECT ; Set RAM Bank
jsr popa ; low address
pha
jsr popa ; high address
pha
jsr popa ; priority
tax ; priority - in .X
ply ; high address - in .Y
pla ; low address - in .A
jmp zcm_setmem
;zcm_play(uint8_t slot, uint8_t volume);
_zcm_play:
pha ; Save volume
jsr popa ; Get slot
tax ; slot - in .X
pla ; volume - in .A
jmp zcm_play
;zcm_stop();
_zcm_stop=zcm_stop
;zsmkit_setisr();
_zsmkit_setisr=zsmkit_setisr
;zsmkit_clearisr();
_zsmkit_clearisr=zsmkit_clearisr