Debian Codegraph @

src/asm/6502/nes/test.s

raw
1 ; command stream player test - NES version
2 ; NES because it's what I am most familiar with, besides Atari 2600
3
4 .include "nes.i"
5
6 pendingTick=$400
7 curChan=$401
8 joyInput=$402
9 joyPrev=$403
10
11 tempFreq=$06
12 mulTemp0=$08
13 mulTemp1=$09
14 tempNote=$0a
15 tempOctave=$0b
16 temp16=$0c
17
18 ; program ROM
19 .BANK 1 SLOT 1
20 .ORGA $8000
21
22 main:
23 ; initialize processor
24 sei
25 cld
26 ldx #$ff
27 txs
28 ; clear all memory
29 ldx #$07
30 stx $01
31 lda #$00
32 sta $00
33 tay
34 - sta ($00),y
35 dey
36 bne -
37 dex
38 stx $01
39 bne -
40 ; clear zero page
41 ldx #$00
42 - sta $00,x
43 dex
44 bne -
45 initPlayer:
46 ; initialize command stream player
47 jsr fcsInit
48 ; enable audio (not DPCM though)
49 + lda #$0f
50 sta $4015
51 initPPU:
52 ; wait for PPU to be ready
53 bit PPUSTATUS
54 ; wait two frames
55 - bit PPUSTATUS
56 bpl -
57 - bit PPUSTATUS
58 bpl -
59 startPlayer:
60 ; draw some text
61 lda #$20
62 sta PPUADDR
63 lda #$40
64 sta PPUADDR
65
66 lda #<screenText
67 sta $00
68 lda #>screenText
69 sta $01
70 ldy #0
71 clc
72 - lda ($00),y
73 beq ++
74 sta PPUDATA
75 inc $00
76 bne -
77 inc $01
78 bcc -
79
80 ; set palette
81 ++ bit PPUSTATUS
82 lda #$3f
83 sta PPUADDR
84 lda #$00
85 sta PPUADDR
86
87 ldx #$00
88 - lda ppuPalette.w,x
89 sta PPUDATA
90 inx
91 cpx #$10
92 bne -
93
94 ; set up PPU
95 lda #$0e
96 sta PPUMASK
97 ; reset scroll position
98 bit PPUSTATUS
99 lda #$00
100 sta PPUSCROLL
101 sta PPUSCROLL
102 ; set up VBlank interrupt
103 lda #$80
104 sta PPUCTRL
105 loop:
106 lda pendingTick
107 beq loop
108 ; wait a bit so we can see the raster
109 ldy #$ff
110 ldx #$02
111
112 - dey
113 bne -
114 dex
115 bne -
116
117 ; raster time display
118 lda #$0f
119 sta PPUMASK
120
121 jsr fcsTick
122 jsr dispatchTick
123 lda #0
124 sta pendingTick
125
126 ; raster time display end
127 lda #$0e
128 sta PPUMASK
129
130 jmp loop
131
132 .MACRO ppuPos
133 bit PPUSTATUS
134 lda #>($2000+((\1))+((\2)<<5))
135 sta PPUADDR
136 lda #<($2000+((\1))+((\2)<<5))
137 sta PPUADDR
138 .ENDM
139
140 ; interrupt handlers
141 nmi:
142 php
143 pha
144
145 ; read controller
146 lda joyInput
147 sta joyPrev
148 lda #$01
149 sta JOY1
150 sta joyInput
151 lsr
152 sta JOY1
153
154 - lda JOY1
155 lsr
156 rol joyInput
157 bcc -
158
159 ; PPU update
160 ppuPos 10, 4
161
162 lda curChan
163 jsr printNum
164
165 lda curChan
166 rol
167 tax
168
169 ppuPos 12, 6
170
171 lda chanPC.w+1,x
172 jsr printNum
173 lda chanPC.w,x
174 jsr printNum
175
176 ppuPos 12, 7
177
178 lda chanTicks.w+1,x
179 jsr printNum
180 lda chanTicks.w,x
181 jsr printNum
182
183 ppuPos 14, 8
184 lda chanNote.w,x
185 jsr printNum
186
187 ppuPos 14, 9
188 lda chanPitch.w,x
189 jsr printNum
190
191 ppuPos 20, 9
192 lda chanVibratoDebug.w,x
193 jsr printNum
194
195 ppuPos 14, 10
196 lda chanVibrato.w,x
197 jsr printNum
198
199 ppuPos 14, 11
200 lda chanVibratoPos.w,x
201 jsr printNum
202
203 ppuPos 14, 12
204 lda chanArp.w,x
205 jsr printNum
206
207 ppuPos 20, 12
208 lda chanArpTicks.w,x
209 jsr printNum
210
211 ppuPos 23, 12
212 lda chanArpStage.w,x
213 jsr printNum
214
215 ppuPos 14, 13
216 lda chanPortaSpeed.w,x
217 jsr printNum
218
219 ppuPos 20, 13
220 lda chanPortaTarget.w,x
221 jsr printNum
222
223 ppuPos 12, 14
224 lda chanVol.w+1,x
225 jsr printNum
226 lda chanVol.w,x
227 jsr printNum
228
229 ; end of PPU update
230 bit PPUSTATUS
231 lda #0
232 sta PPUSCROLL
233 sta PPUSCROLL
234
235 ; process joy input
236 lda joyInput
237 eor joyPrev
238 and joyInput
239
240 ; left
241 pha
242 and #$02
243 beq +
244 dec curChan
245 bpl +
246 lda #0
247 sta curChan
248
249 ; right
250 + pla
251 and #$01
252 beq +
253 inc curChan
254
255 ; set pendingTick
256 + lda #1
257 sta pendingTick
258
259 ; end
260 pla
261 plp
262 rti
263
264 irq:
265 rti
266
267 printNum:
268 pha
269 lsr
270 lsr
271 lsr
272 lsr
273 tay
274 lda hexChar.w,y
275 sta PPUDATA
276 pla
277 and #$0f
278 tay
279 lda hexChar.w,y
280 sta PPUDATA
281 rts
282
283 ; a: factor 1
284 ; y: factor 2
285 ; ya: product
286 mul8:
287 lsr
288 sta mulTemp0
289 tya
290 beq mul8Zero
291
292 dey
293 sty mulTemp1
294 lda #0
295
296 ; iteration 0
297 bcc +
298 adc mulTemp1
299 + ror
300
301 ; iterations 1-7
302 .REPEAT 7
303 ror mulTemp0
304 bcc +
305 adc mulTemp1
306 + ror
307 .ENDR
308
309 ; end
310 tay
311 lda mulTemp0
312 ror
313 mul8Zero:
314 rts
315
316 ; command stream player definition
317 FCS_MAX_CHAN=8
318
319 fcsAddrBase=$30
320 fcsZeroPage=$0e
321 fcsGlobalStack=$200
322 fcsPtr=cmdStream
323 fcsVolMax=volMaxArray
324
325 ; dispatch state definition
326 dcStateBase=$410
327
328 dcBaseFreq=dcStateBase ; short
329 dcPitch=dcStateBase+(2*FCS_MAX_CHAN) ; short
330 dcFreqChanged=dcStateBase+(4*FCS_MAX_CHAN) ; char
331
332 exampleNoteTableLow:
333 .db <$d5c, <$c9c, <$be8, <$b3c, <$a9a, <$a02, <$972, <$8ea, <$86a, <$7f2, <$780, <$714, <$6ae
334
335 exampleNoteTableHigh:
336 .db >$d5c, >$c9c, >$be8, >$b3c, >$a9a, >$a02, >$972, >$8ea, >$86a, >$7f2, >$780, >$714, >$6ae
337
338 exampleNoteDelta:
339 .db 192, 180, 172, 162, 152, 144, 136, 128, 120, 114, 108, 102
340
341 ; >>2
342 noteSubTable:
343 .db 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 168, 180
344
345 ; >>2
346 octaveTable:
347 .db 0, 0, 0
348 .db 1, 1, 1
349 .db 2, 2, 2
350 .db 3, 3, 3
351 .db 4, 4, 4
352 .db 5, 5, 5
353 .db 6, 6, 6
354 .db 7, 7, 7
355 .db 8, 8, 8
356 .db 9, 9, 9
357 .db 10, 10, 10
358 .db 11, 11, 11
359 .db 12, 12, 12
360 .db 13, 13, 13
361 .db 14, 14, 14
362 .db 15, 15, 15
363
364 ; command call table
365 fcsCmdTableLow=cmdTableLow
366 fcsCmdTableHigh=cmdTableHigh
367
368 ; calculate note_and octave from single-byte note_
369 ; a: note_from 0-180
370 ; set a to note_, and y to octave
371 calcNoteOctave:
372 ; push a for later use
373 pha
374 ; divide by 4 for indexing in the octave table
375 lsr
376 lsr
377 tay
378 ; get the octave and put it into y
379 lda octaveTable,y
380 tay
381 ; pull a (contains note_from 0-180)
382 pla
383 ; subtract it with the note_sub table to clamp note_to 0-12
384 sec
385 sbc noteSubTable,y
386 ; end
387 rts
388
389 dispatchNoteOn:
390 lda fcsArg0
391 sta dcBaseFreq+1,x
392 lda #0
393 sta dcBaseFreq,x
394 lda #1
395 sta dcFreqChanged,x
396 rts
397
398 dispatchNoteOff:
399 dispatchPrePorta:
400 rts
401
402 dispatchPorta:
403 ; check whether we already hit target
404 lda dcBaseFreq+1,x
405 cmp fcsArg1
406 bne +
407 lda dcBaseFreq,x
408 bne +
409 rts
410 ; we didn't - perform porta
411 ; 1. calculate porta speed
412 + lda fcsArg0
413 sta temp16
414 lda #0
415 sta temp16+1
416
417 ; <<2 to get 4 (default in Furnace)
418 clc
419 rol temp16
420 rol temp16+1
421 clc
422 rol temp16
423 rol temp16+1
424
425 ; 2. get direction of porta (cs = down, cc = up)
426 lda dcBaseFreq+1,x
427 cmp fcsArg1
428 bcc portaUp
429 ; 3. perform porta depending on direction
430 portaDown:
431 lda dcBaseFreq,x
432 sec
433 sbc temp16
434 sta dcBaseFreq,x
435 lda dcBaseFreq+1,x
436 sbc temp16+1
437 sta dcBaseFreq+1,x
438
439 ; check whether we hit target
440 bcs + ; carry set - no underflow
441 cmp fcsArg1
442 bcs + ; greater or equal to target
443 ; we did - let's clamp
444 lda #0
445 sta dcBaseFreq,x
446 lda fcsArg1
447 sta dcBaseFreq+1,x
448 ; end
449 + lda #1
450 sta dcFreqChanged,x
451 rts
452 portaUp:
453 lda dcBaseFreq,x
454 clc
455 adc temp16
456 sta dcBaseFreq,x
457 lda dcBaseFreq+1,x
458 adc temp16+1
459 sta dcBaseFreq+1,x
460
461 ; check whether we hit target
462 bcc + ; carry clear - no overflow
463 cmp fcsArg1
464 bcc + ; less than target
465 ; we did - let's clamp
466 lda #0
467 sta dcBaseFreq,x
468 lda fcsArg1
469 sta dcBaseFreq+1,x
470 ; end
471 + lda #1
472 sta dcFreqChanged,x
473 rts
474
475 dispatchLegato:
476 lda fcsArg0
477 sta dcBaseFreq+1,x
478 lda #0
479 sta dcBaseFreq,x
480 lda #1
481 sta dcFreqChanged,x
482 rts
483
484 dispatchPitch:
485 lda fcsArg0
486 clc
487 rol
488 sta dcPitch,x
489 lda fcsArg0+1
490 rol
491 sta dcPitch+1,x
492 lda #1
493 sta dcFreqChanged,x
494 rts
495
496 cmdTableLow:
497 .db <dispatchNoteOn
498 .db <dispatchNoteOff
499 .db 0 ; note_release
500 .db 0 ; env release
501 .db 0 ; instrument
502 .db 0 ; volume
503 .db 0, 0 ; unused
504 .db <dispatchPorta
505 .db <dispatchPitch
506 .db 0 ; panning
507 .db <dispatchLegato
508 .db <dispatchPrePorta
509 .dsb 243, 0
510
511 cmdTableHigh:
512 .db >dispatchNoteOn
513 .db >dispatchNoteOff
514 .db 0 ; note_release
515 .db 0 ; env release
516 .db 0 ; instrument
517 .db 0 ; volume
518 .db 0, 0 ; unused
519 .db >dispatchPorta
520 .db >dispatchPitch
521 .db 0 ; panning
522 .db >dispatchLegato
523 .db >dispatchPrePorta
524 .dsb 243, 0
525
526 handleFreq:
527 lda #0
528 sta dcFreqChanged,x
529
530 ; frequency handler - LINEAR
531 ; calculate effective pitch
532 lda dcBaseFreq,x
533 clc
534 adc dcPitch,x
535 sta tempFreq
536 lda dcBaseFreq+1,x
537 adc dcPitch+1,x
538 sta tempFreq+1
539
540 ; a contains note_
541 ; check for out of range
542 sec
543 sbc #60
544 bmi bottomPitch
545 jsr calcNoteOctave
546 sta tempNote
547 sty tempOctave
548
549 ; load note_from note_table
550 tay
551 lda exampleNoteTableLow,y
552 sta temp16
553 lda exampleNoteTableHigh,y
554 sta temp16+1
555
556 ; calculate pitch offset
557 lda exampleNoteDelta,y
558 ldy tempFreq
559 jsr mul8
560 lda temp16
561 sty temp16
562 sec
563 sbc temp16
564 sta temp16
565 bcs +
566 dec temp16+1
567
568 ; shift by octave
569 + ldy tempOctave
570 beq +
571 - lsr temp16+1
572 ror temp16
573 dey
574 bne -
575
576 + lda temp16+1
577 and #$f8
578 beq +
579
580 bottomPitch:
581 lda #$ff
582 sta temp16
583 lda #$07
584 sta temp16+1
585
586 ; make a sound
587 + txa
588 clc
589 ror
590 cmp curChan
591 bne +
592 lda #$3f
593 sta $4000
594 lda #$08
595 sta $4001
596 lda temp16
597 sta $4002
598 lda temp16+1
599 ora #$08
600 sta $4003
601 + rts
602
603 dispatchTick:
604 ldx #0
605 - lda dcFreqChanged,x
606 beq +
607 jsr handleFreq
608 + inx
609 inx
610 cpx #(FCS_MAX_CHAN*2)
611 bne -
612 rts
613
614 .include "../stream.s"
615
616 ; data
617 screenText:
618 .db " Furnace Test Player "
619 .db " "
620 .db " channel 00 "
621 .db " "
622 .db " PC $0000 (00) "
623 .db " tick 0000 "
624 .db " note 00 "
625 .db " pitch 00 ~00 "
626 .db " vib 00 "
627 .db " vibPos 00 "
628 .db " arp 00 (00/00) "
629 .db " porta 00 -> 00 "
630 .db " vol 0000 "
631 .db " pan 0000 "
632 .db 0
633
634 hexChar:
635 .db "0123456789ABCDEF"
636
637 ppuPalette:
638 .db $0e, $00, $10, $30
639 .db $0e, $00, $10, $30
640 .db $0e, $00, $10, $30
641 .db $0e, $00, $10, $30
642
643 volMaxArray:
644 .db $7f, 00
645 .db $7f, 00
646 .db $7f, 00
647 .db $7f, 00
648 .db $7f, 00
649 .db $7f, 00
650 .db $7f, 00
651 .db $7f, 00
652
653 .ORGA $9000
654
655 cmdStream:
656 .incbin "../seq.bin"
657
658 ; vectors
659 .ORGA $FFFA
660
661 .dw nmi
662 .dw main
663 .dw irq
664
665 ; character ROM
666 .BANK 2 SLOT 2
667 .ORGA $10000
668
669 .incbin "chr.bin"