-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.asm
1150 lines (950 loc) · 21.9 KB
/
main.asm
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*=$0801
sysline:
!byte $0b,$08,$01,$00,$9e,$32,$30,$36,$31,$00,$00,$00 ;= SYS 2061
* = $080d ;=2061
!macro SetBorderColor .color {
lda #.color
sta $d020
}
start
+SetBorderColor 0
sta$d021 ; infill color to black as well
lda#$02
sta$0400 ; draw 'B'
; setup sound
jsr init_sid
sei ; disable interrupts
lda #<vic_rst_irq
sta $0314 ;
lda #>vic_rst_irq ;
sta $0315 ; fill interrupt table entry for VIC-II RST interrupt
; VIC-II can generate interrupts, these have to be enabled
; and, once on occurs, a the bit in the interrupt latch
; register ($d019) needs to be cleared.
;
; $d01a is the interrupt enable register - a bit in the
; first 4 bits will enable one of the 4 interrupts.
;
; here we will enable the 'reached certain raster line' (RST)
; interrupt. The raster line is stored in $d012 and $d011.
asl $d019
lda #$7b
sta $dc0d
lda #$81
sta $d01a ; write to VIC-II interrupt register
lda #$1b
sta $d011
lda #$80
sta $d012
cli ; enable interrupts
; enable 'high-res' bitmap mode; this gives us 320x200 pixel (=64000)
; in graphics memory but only 40x25 (=1000) bytes for color.
lda$d011 ; set BMM=1
ora#0b00100000
sta$d011
lda$d016 ; unset MCM
and#0b11101111
sta$d016
lda$d018
ora#0b00001000
sta$d018 ; move graphics to $2000 instead of $1000
; colors are defined for 8x8 pixels at once, upper nibble for 'on' pixels.
; for simplicity we'll fill all 40x25 byte with white for on-pixels and
; black for off-pixels.
ldx#$00
lda#0b01010000
colorfill_loop
sta$0400,x
sta$0500,x
sta$0600,x
sta$0700,x
dex
bne colorfill_loop
; overwrite all pixels with 0 to blank the screen
;
; there are 64000 pixels, 1 bit for each -> 8000 byte for the whole screen.
; therefore we have 8000/256 = 31.25 pages to fill, starting at $2000.
; $2000, $2100, ...
;
; since we only have 16 bit registers we can use zero-page adressing:
; we write $2000 (high byte $20 and low byte $00 respectively) to
; memory location $00fc and $00fb. Then we can do something like
; sta ($fb), y to set ($2000 + y) to the content of register A.
ldx#32
ldy#$00
lda#$00
sta$fb
lda#$20
sta$fc
lda#0
clearscr_loop
sta($fb),y
dey
bne clearscr_loop
inc$fc
ldy#$00
dex
bne clearscr_loop
; 320x200 resolution, 40x25 bytes, therefore 256/40=6.4 rows per page
;
; 0b0 0b1 0b2 0b3 ... 0b7 8b0 8b1 8b2 ... 8b7
; 1b0 1b1 1b2 ... 9b0 9b1 ...
; 2b0 ... ... Ab0 ...
; 3b0 Bb0
; 4b0 Cb0
; 5b0 Db0
; 6b0 Eb0
; 7b0 Fb0
;
; when base addr. = $2000, then 0b0 is bit 0 at $2000, 0b1 is bit 1 at $2000.
;
!addr FP_A = $C400
!addr FP_B = $C430
!addr FP_C = $C460
!addr FP_XCUR = $C4C0 ; 6 byte per float
!addr FP_YCUR = $C4F0
!addr FP_ZCUR = $C520
!addr FP_TEMP = $C550
!addr FP_SCALE_Y = $C560
!addr FP_OFFSET_X = $C570
!addr INT_X = $C600
!addr INT_Y = $C602
!addr INT_M = $C604
!addr INT_X_DT = $C606
!addr INT_Y_DT = $C608
!addr INT_Z_DT = $C60A
!addr SCREEN_ADDR = $C630
; LUT for ORing patterns
!addr SCREEN_MASK_0 = $C640
!addr SCREEN_MASK_1 = $C641
!addr SCREEN_MASK_2 = $C642
!addr SCREEN_MASK_3 = $C643
!addr SCREEN_MASK_4 = $C644
!addr SCREEN_MASK_5 = $C645
!addr SCREEN_MASK_6 = $C646
!addr SCREEN_MASK_7 = $C647
lda#0b00000001
sta SCREEN_MASK_0
lda#0b00000010
sta SCREEN_MASK_1
lda#0b00000100
sta SCREEN_MASK_2
lda#0b00001000
sta SCREEN_MASK_3
lda#0b00010000
sta SCREEN_MASK_4
lda#0b00100000
sta SCREEN_MASK_5
lda#0b01000000
sta SCREEN_MASK_6
lda#0b10000000
sta SCREEN_MASK_7
!macro lshift_16bit .hb, .lb {
asl .lb
rol .hb
}
!macro rshift_16bit .hb, .lb {
lsr .hb
ror .lb
}
; X is 16 bit (FC FB)
; Y is 8 bit (Y)
;lda#00
;sta$FC
;lda#$02
;sta$FB
;ldy#$0
;jsr blit_xy
;lda#00
;sta$FC
;lda#$04
;sta$FB
;ldy #0
;jsr blit_xy
; store FAC to RAM (X=Addr.LB, Y=Addr.HB)
!addr MOVMF = $BBD4
; load FAC from RAM (A=Addr.LB, Y=Addr.HB)
!addr MOVFM = $BBA2
; FAC to 16-bit signed int (Y=Addr.LB, A=Addr.HB)
!addr FACINX = $B1AA
; Add FAC + number in RAM (A=Addr.LB, Y=Addr.HB)
!addr FADD = $B867
; Subtract FAC - number in RAM (A=Addr.LB, Y=Addr.HB)
; FAC = Mem - FAC
!addr FSUB = $B850
; Divide number in RAM by FAC (A=Addr.LB, Y=Addr.HB)
!addr FDIV = $BB0F
; Multiply number from RAM * FAC (clobbers ARG, A=Addr.LB, Y=Addr.HB)
!addr FMULT = $BA28
; Convert 16-bit signed to float in FAC (Y=LB, A=HB)
!addr GIVAYF = $B391
; Copy ARG to FAC
!addr MOVEF = $BBFC
; Copy FAC to ARG
!addr MOVFA = $BC0F
; Subtract ARG from FAC1
; FAC = ARG - FAC
!addr FSUBT = $B853
; Convert FAC1 to 32 bit integer
!addr QINT = $BC9B
!macro set_int_param .name, .value {
ldy#.value
lda#0
jsr GIVAYF
ldx #< .name
ldy #> .name
jsr MOVMF
}
!macro float_to_fac1 .name {
lda#< .name
ldy#> .name
jsr MOVFM
}
!macro fdiv .other {
lda#< .other
ldy#> .other
jsr FDIV
}
!macro fsub .other {
lda#< .other
ldy#> .other
jsr FSUB
}
!macro fadd .other {
lda#< .other
ldy#> .other
jsr FADD
}
!macro fmult .other {
lda#< .other
ldy#> .other
jsr FMULT
}
!macro movmf .other {
ldx#< .other
ldy#> .other
jsr MOVMF
}
!macro fac1_to_int16 .location {
jsr FACINX
sty .location
sta .location + 1
}
+set_int_param FP_XCUR, 2
+set_int_param FP_YCUR, 1
+set_int_param FP_ZCUR, 1
+set_int_param FP_A, 10
+set_int_param FP_B, 28
+set_int_param FP_C, 8
+set_int_param FP_SCALE_Y, 25
+set_int_param FP_OFFSET_X, 160
; initialize FP_C = 8/3
+set_int_param FP_TEMP, 3
lda#< FP_TEMP
ldy#> FP_TEMP
jsr MOVFM
lda#< FP_C
ldy#> FP_C
jsr FDIV
ldx#< FP_C
ldy#> FP_C
jsr MOVMF
; normally we would multiply delta-time (dt, e.g. 0.01)
; to our differential equation results to get the next
; X/Y/Z values. We can save a multiplication here if we
; assume that we approximate 0.01 by shifting the float
; exponent to the right.
;
; Some approximate values:
; - 1/(2**8) = 0.0039..
; - 1/(2**7) = 0.0078..
; - 1/(2**6) = 0.0156..
;
; So we just define the shift amount to set our dt.
!set dt_shift = 6
!macro multiply_dt_to_fac1 {
clc
lda $61
sbc #dt_shift
sta $61
}
!macro save_int_gradient_to .target {
; save int(grad/8) to .target
jsr MOVFA
clc
lda $61
sbc #3
sta $61
+fac1_to_int16 .target
jsr MOVEF
}
; testing operation order of FSUB
;
;+set_int_param FP_TEMP, 100
;+float_to_fac1 FP_A
;lda #< FP_TEMP
;ldy #> FP_TEMP
;jsr FSUB
;+movmf FP_TEMP ; expect 90 in FP_TEMP
main
lda #$20 - 5
sta $FA
draw_loop
jsr xyz_step
lda INT_X
sta $FB
lda INT_X + 1
sta $FC
ldy INT_Y
jsr blit_xy
dec $FA
;bne draw_loop
jmp draw_loop
hang
jmp hang
!zone xyz_step {
xyz_step
; X_new = a * (Y_cur - X_cur)
; X_cur = X_cur + X_new * dt
;
; 1. Y_cur - X_cur
+float_to_fac1 FP_XCUR
lda#< FP_YCUR
ldy#> FP_YCUR
jsr FSUB
; 2. a * FAC1
lda#< FP_A
ldy#> FP_A
jsr FMULT
+save_int_gradient_to INT_X_DT
; 3. FAC1 * dt
+multiply_dt_to_fac1
; 4. FAC1 + X_cur
lda#< FP_XCUR
ldy#> FP_XCUR
jsr FADD
; 4. X_cur = FAC1
ldx#< FP_XCUR
ldy#> FP_XCUR
jsr MOVMF
; store int(fp_x) as X coordinate
;
; we're multiplying X_CUR by 8 but
; we don't really multiply, we just
; add 3 to the float's exponent.
;
; original code:
;!+set_int_param FP_SCALE_X, 8
;[...]
;lda #< FP_SCALE_X
;ldy #> FP_SCALE_X
;jsr FMULT
clc
lda #3
adc $61
sta $61
lda #< FP_OFFSET_X
ldy #> FP_OFFSET_X
jsr FADD
+fac1_to_int16 INT_X
; Y_new = X_cur * (b - Z_cur) - Y_cur
; Y_cur = Y_cur + Y_new * dt
; (b - Z_cur)
+float_to_fac1 FP_ZCUR
+fsub FP_B
; FAC1 * X_cur
+fmult FP_XCUR
; FAC1 = ARG - FAC1
; ARG = FAC1 (= X_cur * (b - Z_cur))
; FAC1 = Y_cur
jsr MOVFA ; ARG = FAC1
+float_to_fac1 FP_YCUR
jsr FSUBT
+save_int_gradient_to INT_Y_DT
; FAC1 * dt
+multiply_dt_to_fac1
; FAC1 + Y_cur
+fadd FP_YCUR
; Y_cur = FAC1
+movmf FP_YCUR
; Z_new = X_cur * Y_cur - c * Z_cur
; Z_cur = Z_cur + Z_new * dt
;
; 1. temp=(c * Z_cur)
+float_to_fac1 FP_C
+fmult FP_ZCUR
+movmf FP_TEMP
; 2. (X_cur * Y_cur)
+float_to_fac1 FP_XCUR
+fmult FP_YCUR
; 3. FAC1 - temp
jsr MOVFA
+float_to_fac1 FP_TEMP
jsr FSUBT
+save_int_gradient_to INT_Z_DT
; 4. FAC1 * dt
+multiply_dt_to_fac1
; 5. FAC1 + Z_cur
+fadd FP_ZCUR
; 6. Z_cur = FAC1
+movmf FP_ZCUR
; compute Y addr.
; u = (y + z * 10)
; y_px = int((u * 25)) >> 6
;
; store int(y + z*10) as Y coordinate
; note that FAC1 contains Z_CUR at this point in time.
+fmult FP_A ; XXX abuses the fact that A=10
+fadd FP_YCUR
+fmult FP_SCALE_Y
; normally we'd use +fac1_to_int16 but it assumes signed
; integer (which we don't expect) and therefore values >32k
; will break. instead we'll use QINT and take the lowest
; two bytes.
jsr QINT
lda $64 ; HB of 16 bit int
ldy $65 ; LB of 16 bit int
sta INT_Y+1
sty INT_Y
+rshift_16bit INT_Y+1, INT_Y
+rshift_16bit INT_Y+1, INT_Y
+rshift_16bit INT_Y+1, INT_Y
+rshift_16bit INT_Y+1, INT_Y
+rshift_16bit INT_Y+1, INT_Y
+rshift_16bit INT_Y+1, INT_Y
; from here on we will only use INT_Y since the possible Y
; range is from 0 to 200 anyway.
; we need to invert it so the image is not flipped, though.
clc
lda #200
sbc INT_Y
sta INT_Y
; compute l2 norm L2(int(dx), int(dy)+2int(dz)) to get the current
; curve's magnitude. note that we only scale dz by 2 as simulations
; deemed this sufficient and safes us some computation.
;
; * we use an L2 approximation (max(x,y) + 1/2*min(x,y)) to avoid
; sqrt and squaring :)
; * we scale both dx and dy+2dz by 1/4 to get a good value range
; for our purporses (math. correctness to hell)
;
;
lda INT_Y_DT
clc
adc INT_Z_DT
adc INT_Z_DT
sta INT_M ; INT_M = A = dy + 2dz
cmp INT_X_DT
bcs .x_is_max_y_is_min
jmp .y_is_max_x_is_min
.x_is_max_y_is_min
lda INT_M
lsr ;
lsr ; scale dy+2dz value by 4 (we do x/4 later)
lsr ; scale INT_M by 2 since it contains the minimum
sta INT_M
lda INT_X_DT
lsr
lsr
clc
adc INT_M ; INT_M = dx/4 (max) + ((dy + 2dz)/4)/2
sta INT_M
jmp .l2_ready
.y_is_max_x_is_min
lda INT_M
lsr
lsr ; INT_M = (dy+2dz)/4 (max)
sta INT_M
lda INT_X_DT
lsr
lsr ; scale dx by 4
lsr ; scale dx by 2 because its the minimum
clc
adc INT_M
sta INT_M
.l2_ready
rts
}
blit_xy
; parameters: x (16 bit), y (8 bit)
; 0 <= x < 320, 0 <= y < 200
;
; assume x is in $FC $FB
; assume y is in Y
;
; clobbers SCREEN_ADDR global, FC/FB and FD/FE.
; this is a quest to resolve x/y coordinates into an
; screen buffer address. we're assuming 0x2000 as base
; address.
;
; since we have 40x25 byte (8 pixel each, giving 320x200 pixel)
; we have a global adressing (byte-level) and a local adressing
; (bit-level).
; assume x is in $FC $FB
; assume y is in Y
; intitialize addr. variable to 0x2000
lda#$00
sta SCREEN_ADDR
lda#$20
sta SCREEN_ADDR + 1
; compute pixel mask to OR on the region; this will set the pixel bit
; in the byte for which we're currently computing the address of.
_screen_mask
lda $FB
and #7
eor #7
tax
; we round the X offset to a power of 8 since we have
; 8 pixel for each adressable byte (pixels are bits, remember).
;
; addr = addr + (x & 0xF8)
; ^^^^^^^^ -> x.LB = (x.LB & 8)
_x_shift
clc
lda $FB
and #$F8
adc SCREEN_ADDR
sta SCREEN_ADDR
lda $FC
adc SCREEN_ADDR + 1
sta SCREEN_ADDR + 1
_y_shift_global
; yoff_row = (y >> 3) * 40 * 8
; yoff_row = (y & 0xF8) * 40
; u = y & 0xF8
; y_off_row = u * 40
; y_off_row = u * ((1 << 5) + (1 << 3))
; y_off_row = (u << 5) + (u << 3)
;
; clear high byte of (FC,FB) and (FE,FD)
lda#$0
sta$fc
sta$fe
; init low bytes to y * 0xF8 (u = y & 0xF8)
tya
and #$f8
sta $fb
sta $fd
; y1 = u << 5
+lshift_16bit $FC, $FB
+lshift_16bit $FC, $FB
+lshift_16bit $FC, $FB
+lshift_16bit $FC, $FB
+lshift_16bit $FC, $FB
; y2 = u << 3
+lshift_16bit $FE, $FD
+lshift_16bit $FE, $FD
+lshift_16bit $FE, $FD
; y_off_row = y1 + y2
clc
lda $fd
adc $fb
sta $fb
lda $fe
adc $fc
sta $fc
; add y_off_row (FE/FD) to screen addr.
clc
lda SCREEN_ADDR
adc $fb
sta SCREEN_ADDR
lda SCREEN_ADDR + 1
adc $fc
sta SCREEN_ADDR + 1
; add yoff_local to screen_addr
_y_shift_local
clc
tya
and #7
adc SCREEN_ADDR
sta SCREEN_ADDR
lda#0
adc SCREEN_ADDR+1
sta SCREEN_ADDR+1
; load addr., mask pattern, store again
lda SCREEN_ADDR
sta $FB
lda SCREEN_ADDR+1
sta $FC
ldy #0
lda ($FB), Y
ora SCREEN_MASK_0, X
sta ($FB), Y
rts
!addr SID_MEMORY_START = $d400
!addr FREQ_LO_VOICE1 = $d400
!addr FREQ_HI_VOICE1 = $d401
!addr CONTROL_VOICE1 = $d404
!addr ATTACK_DUR_VOICE1 = $d405
!addr SUSTAIN_REL_VOICE1 = $d406
!addr WAV_DUTY_LO_VOICE1 = $d402
!addr WAV_DUTY_HI_VOICE1 = $d403
!addr FREQ_LO_VOICE2 = $d407
!addr FREQ_HI_VOICE2 = $d408
!addr CONTROL_VOICE2 = $d40b
!addr ATTACK_DUR_VOICE2 = $d40c
!addr SUSTAIN_REL_VOICE2 = $d40d
!addr FREQ_LO_VOICE3 = $d40e
!addr FREQ_HI_VOICE3 = $d40f
!addr CONTROL_VOICE3 = $d412
!addr ATTACK_DUR_VOICE3 = $d413
!addr SUSTAIN_REL_VOICE3 = $d414
!addr FILTER_CUTOFF_HI = $d416
!addr FILTER_CUTOFF_LO = $d415
voiceinit
!word voice1
!word voice2
!word voice3
voiceloop
!word voice1loop
!word voice2loop
!word voice3loop
; initial pulse wave duty cycles for each voice
;
init_values_pulse
!byte $08,$03,$03
; initial wave form for each voice
;
; bit desc.
; 7 noise
; 6 pulse
; 5 sawtooth
; 4 triangle
; 3 test
; 2 ring modulation with voice N (1:3, 2:1, 3:1)
; 1 sync with voice N (1:3, 2:1, 3:1)
; 0 gate
;
init_values_wave
!byte $08,$08,$08
play_durations
play_duration_voice1
!byte $01
play_duration_voice2
!byte $01
play_duration_voice3
!byte $01
off_duration
!byte $00
vibrato_state
!byte $00
; zero page variables for pointers to sounds
!addr voice1_ptr = $30
!addr voice2_ptr = $32
!addr voice3_ptr = $34
!addr sound_ptr = $36
!addr sound_idx = $38
; global filter and main volume config for
; register $d416, $d417 and $d418
;
; $d416: filter cutoff freq high byte (bits $d415:{3..0} are the low byte)
; $d417: filter resonance and routing config
; 7..4: filter resonance
; 3: external input into filter
; 2: voice 3 into filter?
; 1: voice 2 into filter?
; 0: voice 1 into filter?
; $d418: filter mode and main volume control
; 7: mute voice 3
; 6: high pass
; 5: band pass
; 4: low pass
; 3..0: main volume
init_values_sid
!byte $00,$f4,$1f
; this code initializes the SID memory starting at $d400.
;
; SID has 3 configurable voices which are initialized here.
;
!zone init_sid {
init_sid
ldy #$18
lda #$00
.loop1
sta SID_MEMORY_START,y
dey
bpl .loop1 ; clear the SID memory with zeroes
; populate voice settings with sensible values
;
ldy #$0e ; y = voice offset in SID memory
ldx #$02 ; x = voice index
.loop2
lda init_values_pulse,x
sta WAV_DUTY_HI_VOICE1,y
lda init_values_wave,x
sta CONTROL_VOICE1,y
lda #$00
sta ATTACK_DUR_VOICE1,y
lda #$01
sta play_durations,x ; reset play durations as well in case of program
; restarts without rebooting the machine
lda voiceinit,x
sta voice1_ptr,x
lda voiceinit+3,x
sta voice1_ptr+3,x
lda init_values_sid,x
sta FILTER_CUTOFF_HI,x ; set filter cutoff, resonance and mode / main volume
; abuses x for writing several bytes but does not
; depend on a voice (is a global setting)
tya
sec
sbc #$07
tay
dex
bpl .loop2
rts
}
;
; CIA 1 Port B ($DC01) Joy 2
; PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0
; CIA1
;
; Port A
; ($DC00)
; PA7 STOP Q C= SPACE 2 CTRL <- 1
; PA6 / ^ = RSHIFT HOME ; * £
; PA5 , @ : . - L P +
; PA4 N O K M 0 J I 9 Fire
; PA3 V U H B 8 G Y 7 Right
; PA2 X T F C 6 D R 5 Left
; PA1 LSHIFT E S Z 4 A W 3 Down
; PA0 CRSRDN F5 F3 F1 F7 CRSRRT RETURN DELETE Up
; Joy 1 Fire Right Left Down Up
;
; https://www.c64-wiki.com/wiki/Keyboard
; sub-routine to clear the screen and reset the state of the drawing
!zone reset_screen {
reset_screen
+set_int_param FP_XCUR, 2
+set_int_param FP_YCUR, 1
+set_int_param FP_ZCUR, 1
ldx#32
ldy#$00
lda#$00
sta$fb
lda#$20
sta$fc
lda#0
.clearscr_loop
sta($fb),y
dey
bne .clearscr_loop
inc$fc
ldy#$00
dex
bne .clearscr_loop
}
!zone play_sounds {
play_sounds
; we hijack this interrupt for checking if the user pressed R
; to reset the program. lazyness :)
lda #0b11111011
sta $DC00
lda $DC01
and #0b00000010
bne .r_not_pressed
jsr reset_screen
.r_not_pressed
lda play_duration_voice1
clc
cmp #2
bcs .play_sound ; if play duration is >1 play that sound
lda off_duration
bne .off
lda #0b11101111
sta $DC00
; check if M key is pressed
lda $DC01
and #0b00010000
bne .addr_no_m
; M key handler
; init playing of sound
clc
lda INT_M
lsr
lsr
lsr
adc #1
sta play_duration_voice1
sta off_duration
jsr .play_sound_voice1
.play_sound
dec play_duration_voice1
.addr_no_m
; check if N key is pressed
lda $DC01
and #0b10000000
bne .no_key
; N key handler
lda #1
sta play_duration_voice2
jsr .play_sound_voice2
rts
.off
lda #00
sta CONTROL_VOICE1
dec off_duration
rts
.no_key
lda #00
sta CONTROL_VOICE2
rts
.play_sound_voice1
lda #$84
sta SUSTAIN_REL_VOICE1
lda #$0a
sta FREQ_HI_VOICE1
lda #$11
sta CONTROL_VOICE1
lda #0
sta ATTACK_DUR_VOICE1
rts
.play_sound_voice2
!set freq = $10
lda #$84
sta SUSTAIN_REL_VOICE2
clc
lda vibrato_state
beq .sub
lda #0
sta vibrato_state
lda #freq
adc INT_M
jmp .freq_modified
.sub
lda #freq
sbc INT_M
lda #1
sta vibrato_state
.freq_modified
sta FREQ_HI_VOICE2
lda #$11
sta CONTROL_VOICE2
lda #0
sta ATTACK_DUR_VOICE2
rts
}