/
cyd.s
426 lines (356 loc) · 6.55 KB
/
cyd.s
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
; Cân y Ddraig
; ... or "Dragon's Song"
; ... or "There's Good CyD"
; Copyright 2013-2015 Ciaran Anscomb
; -----------------------------------------------------------------------
include "dragonhw.s"
if !VSYNC
frag_dur equ 247 ; just under 50 fragments per second
endif
; -----------------------------------------------------------------------
; Waves are specifically aligned to a page boundary, so only the page
; number is necessary to reference them.
; -----------------------------------------------------------------------
; The playback core (and most of the tune processing) fits within one page
; of memory. Keep DP pointed at this page, and everything should stay
; fast.
player_dp equ *>>8
setdp player_dp
; Many of the per-channel variables are (self-)modified directly in the
; code. Here are the ones that aren't:
chan_vars macro
c\1ctimer fcb 1
c\1etimer fcb 1
c\1arptimer fcb 1
c\1loop fcb 0
endm
chan_vars 1
chan_vars 2
chan_vars 3
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
play_frag
; Envelope processing. Once the envelope counter (cXetimer) decrements to
; zero, start again from env_r. Note that if this loops round (256
; fragments) before a new note is played, env_r will restart.
chan_env macro
c\1env_ptr equ *+1
ldx #$0000
dec c\1etimer
bne 1F
c\1env_r equ *+1
ldx #$0000
1 lda ,x+
beq 2F
sta c\1wave
stx c\1env_ptr
2
endm
chan_env 1
chan_env 2
chan_env 3
c3wave equ *+1
ldx #silent
ldu #reg_pia1_pdra
if VSYNC
leay -29,u ; y=reg_pia0_crb
lda -1,y ; clear outstanding IRQ
else
ldy #frag_dur
endif
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Core mixer loop. A sound fragment plays until IRQ is detected, giving
; 50 fragments per second. For this to be portable to NTSC, a switch to
; counter-based timing is required.
mixer_loop
c1off equ *+1
ldd #$0000 ; 3
c1freq equ *+1
addd #$0100 ; 4
std c1off ; 5
sta c1val ; 4
; == 16
c2off equ *+1
ldd #$5555 ; 3
c2freq equ *+1
addd #$0100 ; 4
std c2off ; 5
sta c2val ; 4
; == 16
c3off equ *+1
ldd #$aaaa ; 3
c3freq equ *+1
addd #$0100 ; 4
std c3off ; 5
ldb a,x ; 5
; == 17
c1wave equ *+1
c1val equ *+2
addb >silent ; 5
c2wave equ *+1
c2val equ *+2
addb >silent ; 5
stb ,u ; 4
; == 16
if VSYNC
lda ,y ; 4
bpl mixer_loop ; 3
; == 7
; == 70 (mixer loop)
else
leay -1,y ; 5
bne mixer_loop ; 3
; == 8
; == 71 (mixer loop)
endif
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if VSYNC
sta reg_sam_r1s ; FAST CPU rate
endif
; Add portamento
chan_port macro
ldx c\1freq
c\1port equ *+2
leax <0,x
stx c\1freq
endm
chan_port 1
chan_port 2
chan_port 3
; Tune processing. Decrement the command timer and when it reaches zero,
; fetch & process the next command.
process_tune
chan_handle macro
; arpeggio
dec c\1arptimer
bne 20F
inc c\1wantnote ; any non-zero
c\1arpptr equ *+1
ldx #null_arp
lda ,x+
bne 10F
ldx c\1arpbase
10 stx c\1arpptr
sta c\1arp
20
dec c\1ctimer
bne c\1checknote
c\1tuneptr equ *+1
ldu #$0000
c\1nextbyte lda ,u+
bmi 30F
; jump to command handler
c\1cmd ldx #jumptable_c\1
jmp [a,x]
; a=note (0-127)
30
c\1ads_time equ *+1
ldb #$00
stb c\1etimer
c\1env_ads equ *+1
ldx #$0000
stx c\1env_ptr
pulu b ; b=time
c\1setnote stb c\1ctimer
sta c\1note
c\1done stu c\1tuneptr
c\1arpbase equ *+1
ldx #null_arp
stx c\1arpptr
bra c\1donote
c\1checknote
c\1wantnote equ *+1
lda #$00
beq c\1nonote
c\1donote
clr c\1wantnote
c\1note equ *+1
lda #$00
c\1tp equ *+1
adda #$00
c\1arp equ *+1
adda #$00
c\1arptime equ *+1
ldb #$00
stb c\1arptimer
lsla
ldx #ftable+128
ldd a,x
std c\1freq
c\1nonote
endm
chan_handle 1
chan_handle 2
chan_handle 3
if VSYNC
sta reg_sam_r1c ; AD CPU rate
endif
rts
; -----------------------------------------------------------------------
; Command handlers
rest_c macro
silence_c\1 ldd #envelope_0
std c\1env_ptr
xrest_c\1 clr c\1etimer
rest_c\1 pulu a ; a=time
sta c\1ctimer
jmp c\1done
endm
setnote_c macro
setnote_c\1 pulu a,b ; a=note, b=time
jmp c\1setnote
endm
setpatch_c macro
setpatch_c\1 ldx #patch_table
pulu b
lda #5
mul
leax d,x
lda ,x
sta c\1ads_time
ldd 1,x
std c\1env_ads
ldd 3,x
std c\1env_r
jmp c\1nextbyte
endm
setport_c macro
setport_c\1 pulu a ; a=port
sta c\1port
jmp c\1nextbyte
endm
settp_c macro
settp_c\1 pulu a ; a=tp
sta c\1tp
jmp c\1nextbyte
endm
loop_c macro
loop_c\1 pulu a
sta c\1loop
stu c\1next
jmp c\1nextbyte
endm
next_c macro
next_c\1 dec c\1loop
beq 1F
c\1next equ *+1
ldu #$0000
1 jmp c\1nextbyte
endm
jump_c macro
jump_c\1 ldu ,u
jmp c\1nextbyte
endm
call_c macro
calltp_c\1 pulu a
sta c\1tp
call_c\1 pulu x
stu c\1_ret_addr
leau ,x
jmp c\1nextbyte
endm
return_c macro
c\1_ret_addr equ *+1
return_c\1 ldu #$0000
jmp c\1nextbyte
endm
setarp_c macro
clrarp_c\1 ldx #0
clra
bra 10F
setarp_c\1 pulu a,x
10 sta c\1arptime
sta c\1arptimer
stx c\1arpbase
stx c\1arpptr
jmp c\1nextbyte
endm
rest_c 1
rest_c 2
rest_c 3
setnote_c 1
setnote_c 2
setnote_c 3
setpatch_c 1
setpatch_c 2
setpatch_c 3
setport_c 1
setport_c 2
setport_c 3
settp_c 1
settp_c 2
settp_c 3
loop_c 1
loop_c 2
loop_c 3
next_c 1
next_c 2
next_c 3
jump_c 1
jump_c 2
jump_c 3
call_c 1
call_c 2
call_c 3
return_c 1
return_c 2
return_c 3
setarp_c 1
setarp_c 2
setarp_c 3
silence equ $00
rest equ $02
xrest equ $04
setnote equ $06
setpatch equ $08
setport equ $0a
settp equ $0c
loop equ $0e
next equ $10
jump equ $12
call equ $14
calltp equ $16
return equ $18
setarp equ $1a
clrarp equ $1c
jumptable_c macro
jumptable_c\1
fdb silence_c\1
fdb rest_c\1
fdb xrest_c\1
fdb setnote_c\1
fdb setpatch_c\1
fdb setport_c\1
fdb settp_c\1
fdb loop_c\1
fdb next_c\1
fdb jump_c\1
fdb call_c\1
fdb calltp_c\1
fdb return_c\1
fdb setarp_c\1
fdb clrarp_c\1
endm
jumptable_c 1
jumptable_c 2
jumptable_c 3
; -----------------------------------------------------------------------
select_tune
ldx #tune_table
ldb #6
mul
leax d,x
ldd ,x++
std c1tuneptr
ldd ,x++
std c2tuneptr
ldd ,x++
std c3tuneptr
lda #1
sta c1ctimer
sta c2ctimer
sta c3ctimer
jmp process_tune
; -----------------------------------------------------------------------
null_arp fcb 0
ftable include "ftable.s"
tune_table fdb tune0_c1,tune0_c2,tune0_c3 ; tune 0