-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.s
874 lines (772 loc) · 19.5 KB
/
main.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
; vim: set et ts=8 sw=8 sts=8 syntax=64tass :
; D.Y.S.P. using pre-calculated cycle table
;
; 2016-04-01
music_sid = "Blitter.sid"
music_init = $1000
music_play = $1003
DYSP_HEIGHT = 128
JOY_UP = $01
JOY_DOWN = $02
JOY_LEFT = $04
JOY_RIGHT = $08
JOY_FIRE = $10
zp = $10
; BASIC SYS line
* = $0801
.word (+), 2016
.null $9e, format("%d", start)
+ .word 0
; Initialization code
;
; Set up sprites, initialize VIC, set up IRQ handlers
start
jsr $fda3
jsr $fd15
jsr $ff5b
sei
; set sprite colors
clc
ldx #0
- txa
adc #1
sta $d027,x
inx
cpx #8
bne -
; create an example sprite in the tape buffer
ldx #$3f
lda #$ff
- sta $0340,x
dex
bpl -
lda #($340 / 64)
ldx #7
- sta $07f8,x
dex
bpl -
; set up interface text
ldx #0
- lda iface_text,x
sta $0400,x
lda #$0b
sta $d800,x
inx
bne -
- lda iface_text + 256,x
sta $0500,x
lda #$0b
sta $d900,x
inx
cpx #$40
bne -
lda #0
jsr music_init
lda #$35
sta $01
lda #$7f
sta $dc0d
sta $dd0d
ldx #0
stx $dc0e
stx $dd0e
stx $3fff
lda #$01
sta $d01a
lda #$1b
sta $d011
lda #$29
ldx #<irq1
ldy #>irq1
sta $d012
stx $fffe
sty $ffff
ldx #<break
ldy #>break
stx $fffa
sty $fffb
stx $fffc
sty $fffd
bit $dc0d
bit $dd0d
inc $d019
cli
jmp *
;----------------------------------------------------------------------------;
; IRQ handlers: $d020 changes are used to show the raster time used by the ;
; various routines. ;
;----------------------------------------------------------------------------;
; avoid timing critical loops to cross page boundaries
.align 256
irq1
; 'Double IRQ' method to stabilize raster
pha
txa
pha
tya
pha
lda #$2a
ldx #<irq2
ldy #>irq2
sta $d012
stx $fffe
sty $ffff
lda #1
inc $d019
tsx
cli
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
irq2
txs
ldx #8
- dex
bne -
bit $ea
lda $d012
cmp $d012
beq +
+
ldx #$10
- lda sprite_positions,x
sta $d000,x
dex
bpl -
lda #$ff
sta $d015
ldx #$14
- dex
bne -
nop
jsr dysp ; the actual DYSP loop
lda #0
sta $d021
sta $d015
dec $d020
; responde to user input
jsr joystick2
jsr update_iface
dec $d020
jsr param_highlight
dec $d020
jsr music_play
lda #0
sta $d020
lda #$f9
ldx #<irq3
ldy #>irq3
sta $d012
stx $fffe
sty $ffff
lda #1
sta $d019
pla
tay
pla
tax
pla
break rti
irq3
pha
txa
pha
tya
pha
ldx #7
- dex
bne -
stx $d011
ldx #30
- dex
bne -
lda #$1b
sta $d011
dec $d020
; calculate X and Y movement of the DYSP
jsr dysp_x_sinus
jsr dysp_y_sinus
dec $d020
; clear the 'sprite enable' table
jsr dysp_clear_timing_fast
dec $d020
; calculate the 'sprite enable' values for raster line of the DYSP
jsr dysp_calc_timing_fast
lda #0
sta $d020
lda #$29
ldx #<irq1
ldy #>irq1
sta $d012
stx $fffe
sty $ffff
lda #1
sta $d019
pla
tay
pla
tax
pla
rti
; sprite positions: values for $d000-$d010
sprite_positions
.byte $00, $a0
.byte $18, $a0
.byte $30, $a0
.byte $48, $a0
.byte $60, $a0
.byte $78, $a0
.byte $90, $a0
.byte $a8, $a0
.byte $00
;----------------------------------------------------------------------------;
; User interface/DYSP control code ;
;----------------------------------------------------------------------------;
; DYSP sinus control parameters
dysp_x_idx1 .byte 0
dysp_x_idx2 .byte 0
dysp_y_idx1 .byte 0
dysp_y_idx2 .byte 0
params ; offset 0 of the parameters, used by the joystick routine
dysp_x_adc1 .byte 12
dysp_x_adc2 .byte 9
dysp_x_spd1 .byte 2
dysp_x_spd2 .byte 3
dysp_y_adc1 .byte $0f
dysp_y_adc2 .byte $f6
dysp_y_spd1 .byte $fe
dysp_y_spd2 .byte $3
; colorram locations of the parameter values
param_colram
.word $d807, $d811, $d81c, $d826
.word $d82f, $d839, $d844, $d84e
; index in the parameter list for the joystick routine
param_index .byte 0
; Text for the user interface
iface_text
.enc "screen"
; 0123456789abcdef0123456789abcdef01234567
.text "xadc1: 00 xadc2: 00 xspd1: 00 xspd2: 00"
.text "yadc1: 00 yadc2: 00 yspd1: 00 yspd2: 00"
.text " "
.text "joystick in port 2: "
.text " "
.text " left/right - select parameter "
.text " up/down - adjust parameter "
.text " fire button - set parameter to 0 "
.enc "none"
iface_text_end
; Translate A into hexadecimal digits in A (bit 7-4) and X (bit 3-0)
hex_digits
pha
and #$0f
cmp #$0a
bcs +
adc #$3a
+ sbc #$09
tax
pla
lsr
lsr
lsr
lsr
cmp #$0a
bcs +
adc #$3a
+ sbc #$09
rts
; Update the interface's parameter display
update_iface
lda dysp_x_adc1
jsr hex_digits
sta $0407
stx $0408
lda dysp_x_adc2
jsr hex_digits
sta $0411
stx $0412
lda dysp_x_spd1
jsr hex_digits
sta $041c
stx $041d
lda dysp_x_spd2
jsr hex_digits
sta $0426
stx $0427
lda dysp_y_adc1
jsr hex_digits
sta $042f
stx $0430
lda dysp_y_adc2
jsr hex_digits
sta $0439
stx $043a
lda dysp_y_spd1
jsr hex_digits
sta $0444
stx $0445
lda dysp_y_spd2
jsr hex_digits
sta $044e
stx $044f
rts
; highlight the currently adjustable parameter
param_highlight
; clear param highlighting
ldx #0
- lda param_colram,x
sta zp
lda param_colram + 1,x
sta zp + 1
ldy #0
lda #$0f
sta (zp),y
iny
sta (zp),y
inx
inx
cpx #16
bne -
; highlight current param
lda param_index
asl
tax
lda param_colram,x
sta zp
lda param_colram + 1,x
sta zp + 1
ldy #0
lda #$01
sta (zp),y
iny
sta (zp),y
rts
; Check user input from joystick #2
joystick2
lda #8
beq +
dec joystick2 + 1
rts
+ lda $dc00
sta zp
and #%00011111
eor #%00011111
bne +
rts
+ lda #8
sta joystick2 + 1
lda zp
and #JOY_UP
beq joy2_up
lda zp
and #JOY_DOWN
beq joy2_down
lda zp
and #JOY_LEFT
beq joy2_left
lda zp
and #JOY_RIGHT
beq joy2_right
lda zp
and #JOY_FIRE
beq joy2_fire
rts
joy2_up
ldx param_index
inc params,x
rts
joy2_down
ldx param_index
dec params,x
rts
joy2_left
lda param_index
sec
sbc #1
and #7
sta param_index
rts
joy2_right
lda param_index
clc
adc #1
and #7
sta param_index
rts
joy2_fire
ldx param_index
lda #0
sta params,x
rts
; Calculate DYSP Y-movement
dysp_y_sinus
ldx dysp_y_idx1
ldy dysp_y_idx2
; unroll loop for speed:
.for index = 0, index < 8, index = index + 1
lda ysinus,x
clc
adc ysinus,y
adc #$32
sta sprite_positions + 1 + (index * 2)
.if index < 7
; only needed 7 times
txa
clc
adc dysp_y_adc1
tax
tya
clc
adc dysp_y_adc2
tay
.endif
.next
lda dysp_y_idx1
clc
adc dysp_y_spd1
sta dysp_y_idx1
lda dysp_y_idx2
clc
adc dysp_y_spd2
sta dysp_y_idx2
rts
; temp storage for $d010 calculations
xmsb_tmp .byte 0
; Calculate DYSP X-movement using two sinus tables added together
dysp_x_sinus
lda #0
sta xmsb_tmp
ldx dysp_x_idx1
ldy dysp_x_idx2
; once again unroll loop for speed
.for index = 0, index < 8, index = index + 1
lda xsinus_256,x
clc
adc xsinus_96,y
sta sprite_positions + (index * 2)
bcc +
lda xmsb_tmp
ora #(1 << index)
sta xmsb_tmp
+
.if index < 7
; this section is only needed 7 times
txa
clc
adc dysp_x_adc1
tax
tya
clc
adc dysp_x_adc2
tay
.endif
.next
; store $d010 value in the IRQ handler
lda xmsb_tmp
sta sprite_positions + 16
lda dysp_x_idx1
clc
adc dysp_x_spd1
sta dysp_x_idx1
lda dysp_x_idx2
clc
adc dysp_x_spd2
sta dysp_x_idx2
rts
.align 256 ; avoid page boundary crossing in raster bars
; The actual DYSP routine:
;
; The access to the 'timing' table is what makes this possible. It contains,
; for each raster line, the number of cycles the sprites use. By storing that
; value in the BPL argument we can waste between 0 and 17 cycles inclusive.
;
; Unrolling this loop and altering the code which calculates the cycle waste
; values (storing them directly in the unrolled code, not in a table), we can
; easily add three raster splits.
dysp
ldy #8
ldx #0
- lda d021_table,x
dec $d016
sta $d021
sty $d016
lda d011_table,x
sta $d011
lda timing,x
sta _delay + 1
_delay bpl * + 2
cpx #$e0
cpx #$e0
cpx #$e0
cpx #$e0
cpx #$e0
cpx #$e0
cpx #$e0
cpx #$e0
bit $ea
inx
cpx #DYSP_HEIGHT
bne -
rts
.cerror * > $0fff, "code section too large!"
* = $2000
ysinus
.byte ((DYSP_HEIGHT - 24) / 4) + 0.5 + ((DYSP_HEIGHT - 24) / 4) * sin(range(256) * rad(360.0/256))
xsinus_256
.byte 128 + 127.5 * sin(range(256) * rad(360.0/256))
xsinus_96
.byte 48 + 47.5 * sin(range(256) * rad(360.0/256))
.align 256
; The 'sprite enable' table, this is where the number of active sprites per
; raster line is stored. The values in this table are used as index into the
; 'cycles' table to get the proper amount of cycles to skip in the DYSP loop
dysp_sprite_enable
.fill DYSP_HEIGHT, 0
.align 256
d011_table
.for row = 0, row < DYSP_HEIGHT, row = row + 1
.byte $18 + ((row + 3) & 7)
.next
.align 256
; Raster bar colors
d021_table
.byte $06, $00, $06, $04, $00, $06, $04, $0e
.byte $00, $06, $04, $0e, $0f, $00, $06, $04
.byte $0e, $0f, $07, $00 ,$06, $04, $0e, $0f
.byte $07, $01, $07, $0f, $0e, $04, $06, $00
.byte $07, $0f, $0e, $04, $06, $00, $0f, $0e
.byte $04, $06, $00, $0e, $04, $06, $00, $04
.byte $06, $00, $06, $00, $09, $08, $0a, $0f
.byte $07, $01, $07, $0f, $0a, $08, $09, $00
.byte $06, $00, $06, $04, $00, $06, $04, $0e
.byte $00, $06, $04, $0e, $0f, $00, $06, $04
.byte $0e, $0f, $07, $00 ,$06, $04, $0e, $0f
.byte $07, $01, $07, $0f, $0e, $04, $06, $00
.byte $07, $0f, $0e, $04, $06, $00, $0f, $0e
.byte $04, $06, $00, $0e, $04, $06, $00, $04
.byte $06, $00, $06, $00, $09, $08, $0a, $0f
.byte $07, $01, $07, $0f, $0a, $08, $09, $00
.align 256
; cycle delay table
timing
.fill 2, 0 ; don't touch this, raster code starts early
.fill DYSP_HEIGHT - 2, 0
.align 256
; number of cycles to skip in the branch
cycles
; skip cycles $d015 sprite(s) active
; $00-$07
.byte 0 ; $00 - %00000000 no sprites
.byte 3 ; $01 - %00000001 0
.byte 5 ; $02 - %00000010 1
.byte 5 ; $03 - %00000011 1 0
; $04-$07
.byte 5 ; $04 - %00000100 2
.byte 7 ; $05 - %00000101 2 0
.byte 7 ; $06 - %00000110 2 1
.byte 7 ; $07 - %00000111 2 1 0
; $08-$0b
.byte 5 ; $08 - %00001000 3
.byte 8 ; $09 - %00001001 3 0
.byte 9 ; $0a - %00001010 3 1
.byte 9 ; $0b - %00001011 3 1 0
; $0c-$0f
.byte 7 ; $0c - %00001100 3 2
.byte 9 ; $0d - %00001101 3 2 0
.byte 9 ; $0e - %00001110 3 2 1
.byte 9 ; $0f - %00001111 3 2 1 0
; $10-$13
.byte 5 ; $10 - %00010000 4
.byte 7 ; $11 - %00010001 4 0
.byte 10 ; $12 - %00010010 4 1
.byte 10 ; $13 - %00010011 4 1 0
; $14-$17
.byte 9 ; $14 - %00010100 4 2
.byte 11 ; $15 - %00010101 4 2 0
.byte 11 ; $16 - %00010110 4 2 1
.byte 11 ; $17 - %00010111 4 2 1 0
; $18-$1b
.byte 7 ; $18 - %00011000 4 3
.byte 10 ; $19 - %00011001 4 3 0
.byte 11 ; $1a - %00011010 4 3 1
.byte 11 ; $1b - %00011011 4 3 1 0
; $1c-$1f
.byte 9 ; $1c - %00011100 4 3 2
.byte 11 ; $1d - %00011101 4 3 2 0
.byte 11 ; $1e - %00011110 4 3 2 1
.byte 11 ; $1f - %00011111 4 3 2 1 0
; $20-$2f
.byte $05, $08, $09, $09
.byte $09, $0c, $0c, $0c
.byte $09, $0c, $0d, $0d
.byte $0b, $0d, $0d, $0d
; $30-$3f
.byte $07, $09, $0c, $0c
.byte $0b, $0d, $0d, $0d
.byte $09, $0c, $0d, $0d
.byte $0b, $0d, $0d, $0d
; $40-$4f
.byte $05, $07, $0a, $0a
.byte $0a, $0b, $0b, $0b
.byte $0a, $0d, $0e, $0e
.byte $0b, $0e, $0e, $0e
; $50-$5f
.byte $09, $0b, $0e, $0e
.byte $0d, $0f, $0f, $0f
.byte $0b, $0e, $0f, $0f
.byte $0d, $0f, $0f, $0f
; $60-$6f
.byte $07, $0a, $0b, $0b
.byte $0b, $0e, $0e, $0e
.byte $0b, $0e, $0f, $0f
.byte $0d, $0f, $0f, $0f
; $70-$7f
.byte $09, $0b, $0e, $0e
.byte $0d, $0f, $0f, $0f
.byte $0b, $0e, $0f, $0f
.byte $0d, $0f, $0f, $0f
; $80-$8f
.byte $05, $08, $09, $09
.byte $09, $0c, $0c, $0c
.byte $09, $0d, $0d, $0d
.byte $0c, $0d, $0d, $0d
; $90-$9f
.byte $09, $0c, $0f, $0f
.byte $0d, $10, $10, $10
.byte $0c, $0f, $10, $10
.byte $0d, $10, $10, $10
; $a0-$af
.byte $09, $0c, $0d, $0d
.byte $0d, $10, $10, $10
.byte $0d, $10, $11, $11
.byte $0f, $11, $11, $11
; $b0-$bf
.byte $0b, $0d, $10, $10
.byte $0f, $11, $11, $11
.byte $0d, $10, $11, $11
.byte $0f, $11, $11, $11
; $c0-$cf
.byte $07, $09, $0c, $0c
.byte $0c, $0d, $0d, $0d
.byte $0c, $0f, $10, $10
.byte $0d, $10, $10, $10
; $d0-$df
.byte $0b, $0d, $10, $10
.byte $0f, $11, $11, $11
.byte $0d, $10, $11, $11
.byte $0f, $11, $11, $11
; $e0-$ef
.byte $09, $0c, $0d, $0d
.byte $0d, $10, $10, $10
.byte $0d ,$10, $11, $11
.byte $0f, $11, $11, $11
; $f0-$ff
.byte $0b, $0d, $10, $10
.byte $0f, $11, $11, $11
.byte $0d, $10, $11, $11
.byte $0f, $11, $11, $11
; Clear the 'sprite enable' table, unrolled for speed
dysp_clear_timing_fast
lda #0
.for row = 0, row < DYSP_HEIGHT, row = row + 1
sta dysp_sprite_enable + row
.next
rts
; Calculate the 'sprite enable' values for each raster line of the DYSP
;
; For each sprite, we ORA 21 bytes of the table with the bitmask for that
; particular sprite. The result of these calculations is used to look up the
; number of cycles to waste in the DYSP raster code
;
; Again unrolled for speed, but still takes a lot of raster time
dysp_calc_timing_fast
lda sprite_positions + 1
sec
sbc #$32
tax
.for row = 0, row < 21, row = row + 1
lda dysp_sprite_enable + row,x
ora #1
sta dysp_sprite_enable + row,x
.next
lda sprite_positions + 3
sec
sbc #$32
tax
.for row = 0, row < 21, row = row + 1
lda dysp_sprite_enable + row,x
ora #2
sta dysp_sprite_enable + row,x
.next
lda sprite_positions + 5
sec
sbc #$32
tax
.for row = 0, row < 21, row = row + 1
lda dysp_sprite_enable + row,x
ora #4
sta dysp_sprite_enable + row,x
.next
lda sprite_positions + 7
sec
sbc #$32
tax
.for row = 0, row < 21, row = row + 1
lda dysp_sprite_enable + row,x
ora #8
sta dysp_sprite_enable + row,x
.next
lda sprite_positions + 9
sec
sbc #$32
tax
.for row = 0, row < 21, row = row + 1
lda dysp_sprite_enable + row,x
ora #16
sta dysp_sprite_enable + row,x
.next
lda sprite_positions + 11
sec
sbc #$32
tax
.for row = 0, row < 21, row = row + 1
lda dysp_sprite_enable + row,x
ora #32
sta dysp_sprite_enable + row,x
.next
lda sprite_positions + 13
sec
sbc #$32
tax
.for row = 0, row < 21, row = row + 1
lda dysp_sprite_enable + row,x
ora #64
sta dysp_sprite_enable + row,x
.next
lda sprite_positions + 15
sec
sbc #$32
tax
.for row = 0, row < 21, row = row + 1
lda dysp_sprite_enable + row,x
ora #128
sta dysp_sprite_enable + row,x
.next
; update actual cycle skip table
.for row = 0, row < DYSP_HEIGHT, row = row + 1
ldy dysp_sprite_enable + row
lda cycles,y
sta timing + 2 + row
.next
rts
; Link music
* = $1000
.binary music_sid, $7e