-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhires20.asm
4515 lines (4091 loc) · 71.8 KB
/
hires20.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
; hires20.asm
; Commodore Vic-20 High Resolution Graphics BASIC Extension
; Copyright (c) 2022 Dave Van Wagner (davevw.com)
; MIT LICENSE - see file LICENSE
; Thanks to https://archive.org/details/COMPUTEs_Mapping_the_VIC_1984_COMPUTE_Publications
; memory map
; 0000-03FF Low RAM (1K)
; 0400-0FFF RAM Expansion (3K)
; 1000-1FFF Standard RAM (4K)
; 1000-11E1 TEXT Video Memory 8x8 characters 00-FF, 22*23=482
; 11E2-1FFF Available RAM when in TEXT mode
; 1000-10EF HIRES Video Memory 8x16 characters 10-FF
; 10F0-1FFF HIRES Video RAM Bitmap (of characters 0F-FF)
; 2000-7FFF Expansion RAM (24K) - recommend relocation of start of BASIC
; 8000-8FFF Character ROM (4K)
; 9000-9FFF I/O space and color RAM (nybbles)
; A000-BFFF Cartridge ROM and/or RAM (8K) - HIRES20.ML loaded A000-????
; C000-DFFF BASIC ROM
; E000-FFFF KERNAL ROM
; WORKING SYNTAX
; COLOR [fg[+8][,[bg][,[bd][,aux[,inverse]]]]]
; COLOR [fg[+8]] @ x1,y1 [TO x2,y2]
; TEXT
; HIRES xr, yr [,fillbyte]
; DELAY jiffies
; PLOT COLOR ON|OFF
; PLOT [0|1|2|3|NOT|CLR] (@ x1,y1)|(TO x2,y2)... (** first @ optional if not multicolor)
; PLOT "ABC" @ x,y [,addr [,width,height [,bytes]]]
; RECT [NOT|CLR] [@] x1,y1 TO x2,y2
; RECT 0|1|2|3 @ x1,y1 TO x2,y2
; SHAPE GET|PUT|OR|XOR|AND|NOT|CLR addr @ x1, y1 TO x2, y2
; PROPOSED SYNTAX REMAINING
; PLOT [0|1|2|3 ,] "ABC" @ x,y [,addr [,width,height [,bytes]]]
; SHAPE [0|1|2|3] GET|PUT|OR|XOR|AND|NOT|CLR addr @ x1,y1 TO x2,y2
; PATTERN addr @ x1,y1 TO x2,y2
; PROPOSED VARIABLES
; .HIRES
; .XRES
; .YRES
; .JOYS
; .FORE
; .BACK
; .BORD
; .AUXC
; .REVS
; .PAL
; .NTSC
ptrl=$fe
ptrh=$ff
chkcom=$cefd
getbytc=$d79b
frmnum=$cd9e
syntax_error=$cf08
error=$c437
frmevl=$cd9e
pulstr=$d6a3
makadr=$d7f7 ; convert fp to 2 byte integer
listchr=$cb47 ; output a character
chars=$1000
bitmap_chars_most=240
bitmap=chars+bitmap_chars_most
char_first=bitmap_chars_most/16 ; 15
chrout=$ffd2
*=$A000
start
jmp init_basic
copyright
!byte 18
!text "VIC-20 HIRES"
!byte 13
!text "COPYRIGHT (C) 2022"
!byte 13
!text "BY DAVID VAN WAGNER"
!byte 13
!text "DAVEVW.COM"
!byte 13
!text "MIT LICENSE"
!byte 13
!byte 0
init_basic ; setup vectors for adding HIRES commands, etc.
ldx #0
- lda copyright,x
beq +
jsr chrout
inx
bne -
+ lda #<basic_error
sta $300
lda #>basic_error
sta $301
lda #<hires_crunch
sta $304
lda #>hires_crunch
sta $305
lda #<list_tokens
sta $306
lda #>list_tokens
sta $307
lda #<execute
sta $308
lda #>execute
sta $309
jmp crunch_patch
exec_text
jsr switch_text
jsr $0073
jmp reloop
exec_hires
jsr reset_font_params
jsr next_two_bytes
jsr hires_init
lda is_pal
ldx resx
ldy resy
sta 780
stx 781
sty 782
ldy #0
lda ($7a),y
cmp #$2C ; comma
bne +
jsr getbytc
txa
jsr fill_graphics
+ jmp reloop
hires_init
jsr verifyres
jsr switch_graphics
jsr clear_graphics
jsr fill_video_chars
jsr fill_video_color
rts
plot_string
ldx strlen
beq ++ ; check if nothing to do
; mode=1 PUT
lda #1
sta param5
; y2=y1+height-1
clc
lda param2
adc font_height
sbc #0 ; subtract one with carry clear
sta param4
cmp resy
bcs ++
ldy #0
sty $60
sty font_inverse
-
; x2=x1+width-1
clc
lda param1
adc font_width
sbc #0 ; subtract one with carry clear
sta param3
cmp resx
bcs ++
ldy $60
lda ($5a),y
cmp #18
bne +
sta font_inverse
beq +++
+ jsr petscii_to_screencode
jsr get_char_addr
jsr get_put_shape
; x1+=width
clc
lda param1
adc font_width
sta param1
bcs ++
cmp resx
bcs ++
; ++y (character index)
+++ inc $60
dec strlen
bne -
++ rts
exec_plot
lda #128
sta plot_mode ; assume monochrome set pixel
jsr lookahead
cmp #$CD ; COLOR token
bne +++ ; branch not color
; PLOT COLOR ON|OFF
jsr $0073
jsr $0073
bne +
--- jmp syntax_error
+ cmp #$91 ; ON token
bne +
lda 646
jmp ++
+ cmp #$D7 ; OFF token
bne ---
lda #$FF
++ sta plot_color
jsr $0073
jmp reloop
; optional NOT/CLR for erasing monochrome pixel
+++ cmp #$A8 ; NOT token
beq +
cmp #$9C ; CLR token
bne ++
+ jsr $0073
lda #$FF
sta plot_mode
jsr lookahead
++ cmp #$A4 ; TO token
bne + ; branch not to
jsr $0073
bne ++ ; should always branch
+ cmp #$40 ; @ token
bne +
jsr $0073 ; gobble optional @
jmp +++
; no optional @ (yet)
+ lda plot_mode
cmp #$FF
beq +++ ; NOT/CLR seen, but not TO, so must be coordinate
; so look for coordinate or multicolor choice
jsr $0073 ; get next token
jsr string_or_byte
bcc + ; branch if byte
; string!
sta strlen
stx $5a
sty $5b
ldy #0
lda ($7a),y
cmp #$40 ; require @
bne ---
jsr next_two_bytes
jsr more_plot_text_params
jsr plot_string
jmp reloop
+ ldy #0
lda ($7a),y
cmp #$A4 ; TO token?
bne +
stx plot_mode
beq ++
+ cmp #$40 ; @ required after multicolor choice
bne + ; must be coordinate instead
stx plot_mode
jmp +++ ; go get coordinate after @
; we got first coordinate, now go for second
+ cmp #$2c
beq +
jmp syntax_error
+ stx param1
jsr getbytc
stx param2
jmp ++++
-- ; loop:
cmp #$A4 ; TO token
bne +++++
++ jsr next_two_bytes
php
pha
lda #3
sta param4
lda plot_mode
sta param3
cmp #$80
bne +
dec param4
+ jsr hires_draw_line
- pla
plp
bne --
jmp reloop
+++++
cmp #$40 ; @ token
beq +++
jmp ---
+++
jsr next_two_bytes
++++
php
pha
lda plot_mode
cmp #$ff
+ bne +
jsr hires_unplot_point
jmp -
+ cmp #$80
bne +
jsr hires_plot_point
jmp -
+ sta param3
jsr hires_mplot_point
jmp -
more_plot_text_params
beq ++
jsr chkcom
jsr frmevl
jsr makadr
sty font_address
sta font_address+1
ldy #0
lda ($7a),y
beq ++
cmp #$3a
beq ++
cmp #$2c
bne ++++
jsr getbytc
stx font_width
cmp #$2C
bne ++++
jsr getbytc
stx font_height
+ clc
lda font_width
adc #7
lsr
lsr
lsr
beq +++
ldx font_height
beq +++
jsr multax
cpx #0
bne +++
sta font_bytes
ldy #0
lda ($7a),y
beq ++
cmp #$3a
beq ++
jsr getbytc
cpx #0
beq +++
stx font_bytes
++ rts
+++ jmp illegal_quantity
++++ jmp syntax_error
reset_font_params
lda #$00
sta font_address
lda #$80
sta font_address+1
lda #8
sta font_width
sta font_height
sta font_bytes
rts
hires_plot_point
jsr plot_prep
ora ($fb),y
sta ($fb),y
lda plot_color
bmi +
ldx plot_color_offset
and #7
sta $9400,x
+ rts
hires_unplot_point
jsr plot_prep
eor #$ff
and ($fb),y
sta ($fb),y
lda plot_color
bmi +
ldx plot_color_offset
and #7
sta $9400,x
+ rts
hires_mplot_point
jsr mplot_prep
and ($fb),y
sta $57
txa
ora $57
sta ($fb),y
lda plot_color
bmi +
ldx plot_color_offset
ora #8
sta $9400,x
+ rts
; param1 = x coordinate
; param2 = y coordinate
; param3 (optional) = multicolor 0,1,2,3 choice, or 255 (unplot hires)
; param4 = number of parameters
hires_draw_line
; check if out of range
lda param1
cmp resx
bcc +
- jmp illegal_quantity
+ lda param2
cmp resy
bcs -
lda param4
cmp #4
bcs -
cmp #2
bcc -
bne +
ldx #<hires_plot_point
ldy #>hires_plot_point
bne +++
+ lda param3
bmi ++
ldx #<hires_mplot_point
ldy #>hires_mplot_point
bne +++
++ ldx #<hires_unplot_point
ldy #>hires_unplot_point
+++ stx plot_point_vector
sty plot_point_vector+1
lda #1
sta incx
sta incy
; get absolute diffx, diffy
lda param1
sec
sbc oldx
bcs +
ldx #$ff
stx incx
eor #$ff
adc #1
+ sta diffx
lda param2
sec
sbc oldy
bcs +
ldy #$ff
sty incy
eor #$ff
adc #1
+ sta diffy
; test for point only
lda diffx
ora diffy
bne +
jmp call_plot_point_indirect
; swap old/new
+ ldx param1
ldy param2
lda oldx
sta param1
lda oldy
sta param2
stx param4
sty param5
lda diffx
cmp diffy
bcs +++
; diffx is less than diffy, so draw each y
lda #0
sta $59
- jsr call_plot_point_indirect
clc
lda param2
adc incy
sta param2
clc
lda $59
adc diffx
sta $59
bcs +
cmp diffy
bcc ++
+ sbc diffy
sta $59
clc
lda param1
adc incx
sta param1
++ lda param2
cmp param5
bne -
jmp call_plot_point_indirect ; one last time
+++ ; diffx is greater than or equal to diffy, so draw each x
lda #0
sta $59
- jsr call_plot_point_indirect
clc
lda param1
adc incx
sta param1
clc
lda $59
adc diffy
sta $59
bcs + ; if 8-bit overflow, then definitely bigger than diffx
cmp diffx
bcc ++
+ sbc diffx ; reset remainder
sta $59
clc
lda param2
adc incy
sta param2
++ lda param1
cmp param4
bne -
jmp call_plot_point_indirect ; one last time
exec_rect
lda #128
sta param5 ; assume monochrome set
; optional NOT/CLR for erasing monochrome rect
jsr lookahead
cmp #$A8 ; NOT token
beq +
cmp #$9C ; CLR token
bne ++
+ lda #255
sta param5
jsr $0073 ; gobble up
jsr lookahead
++ cmp #$40 ; skip over optional @
bne +
jsr $0073
+
-- jsr getbytc ; not sure yet, so parse one byte
cmp #$2c ; is next token is comma?
bne +
; yes, these are coordinates
stx param1
jsr getbytc
stx param2
jmp ++
+ ldy param5
cpy #$ff
bne + ; branch if didn't see NOT/CLR
- jmp syntax_error
+ stx param5 ; this is multicolor choice (must be 0-3)
cmp #$40 ; expect @
bne - ; @ was expected
beq --
++ cmp #$A4 ; TO token
bne - ; TO was expected
; expect two more bytes for ending coordinates
jsr getbytc
cmp #$2c
stx param3
bne -
jsr getbytc
stx param4
lda #5 ; assume 5 parameters, multicolor or erase
ldy param5
cpy #$80
bne +
lda #4 ; just monochrome normal, so no extra param
+ jsr hires_rect ; draw rectangle
jmp reloop
; param1/param2 = first x/y coordinate
; param3/param4 = second x/y coordinate
; param5 = multicolor 0,1,2,3 choice, or 255 (unplot hires), or undefined if 4 parameters
; .A = number of parameters
; validate and store parameters
hires_rect
cmp #6
bcc +
- jmp illegal_quantity
+ cmp #4
bcc -
sbc #2 ; convert to number of parameters will be passing to draw
sta rectdrawparams
cmp #3
bcc ++
lda param5
cmp #255
beq +
cmp #4
bcs -
+ sta rectdrawcolor
++ lda param1
cmp resx
bcs -
sta rectx1
lda param2
cmp resy
bcs -
sta recty1
lda param3
cmp resx
bcs -
sta rectx2
lda param4
cmp resy
bcs -
sta recty2
lda rectx2
sta oldx
lda recty2
sta oldy
ldx rectx1
ldy recty2
jsr +
ldx rectx1
ldy recty1
jsr +
ldx rectx2
ldy recty1
jsr +
ldx rectx2
ldy recty2
+ stx param1
sty param2
lda rectdrawcolor
sta param3
lda rectdrawparams
sta param4
jmp hires_draw_line
hires_color_at
lda param1
cmp resx
bcc +
- jmp illegal_quantity
+ lsr
lsr
lsr
sta param1
lda param2
cmp resy
bcs -
lsr
lsr
lsr
lsr
sta param2
lda param3
cmp resx
bcs -
lsr
lsr
lsr
sta param3
lda param4
cmp resy
bcs -
lsr
lsr
lsr
lsr
sta param4
lda param5
cmp #16
bcs -
ldy cols
jmp rect_color
text_color_at
lda param1
cmp #22
bcc +
- jmp illegal_quantity
+ lda param2
cmp #23
bcs -
lda param3
cmp #22
bcs -
lda param4
cmp #23
bcs -
lda param5
cmp #16
bcs -
ldy #22
jmp rect_color
exec_delay
jsr $0073
jsr frmnum
jsr makadr ; convert to integer
; set alarm with interrupts on during critical part, avoiding rollover
clc
pha ; save bits 8..15
tya ; transfer bits 0..7
sei
adc $A2
sta alarm+2
pla ; restore bits 8..15
adc $A1
sta alarm+1
lda $A0
cli
adc #0
sta alarm
; check for alarm, busy wait, highest byte down, assume interrupts off is fine, will wait for any rollover
lda alarm
- cmp $A0
bne -
lda alarm+1
- cmp $A1
bne -
lda alarm+2
- cmp $A2
bne -
jmp reloop
parse_shape_op ; convert token to shape operation mode 0..5
jsr $0073 ; get next token
ldx #0
cmp #$A1 ; GET
beq +
inx
cmp #$D0 ; PUT
beq +
inx
cmp #$B0 ; OR
beq +
inx
cmp #$D1 ; XOR
beq +
inx
cmp #$AF ; AND
beq +
inx
cmp #$A8 ; NOT
beq +
cmp #$9C ; CLR synonym for NOT
beq +
jmp syntax_error
+ stx param5
rts
exec_shape
jsr parse_shape_op
jsr $0073 ; get next token
jsr frmnum
jsr makadr ; convert to integer
sty $fd
sta $fe
; @ param1,param2 = x1/y1 position on screen (left/top of shape)
ldy #0
sty font_inverse
lda ($7a),y
cmp #$40 ; @
bne ++
jsr getbytc
stx param1
cmp #$2c ; comma
bne ++
jsr getbytc
stx param2
; TO param3,param4 = x2/y2 position on screen (right/bottom of shape)
cmp #$a4 ; TO token
bne ++
jsr getbytc
stx param3
cmp #$2c ; comma
bne ++
jsr getbytc
stx param4
; validate coordinate parameters
; x1 <= x2 < resx
; y1 <= y2 < resy
lda param1
cmp param3
beq +
bcs +++
+ lda param2
cmp param4
beq +
bcs +++
+ lda param3
cmp resx
bcs +++
lda param4
cmp resy
bcs +++
jsr get_put_shape
jmp reloop
++ jmp syntax_error
+++ jmp illegal_quantity
; mode GET(0)|PUT(1)|OR(2)|XOR(3)|AND(4)|NOT(5)
get_put_shape ; addr=$fd/$fe (x1,y1)=(param1,param2) (x2,y2)=(param3,param4) mode=param5
; load function address into $5e/$5f
ldx param5
bne +
lda #<get_shape_fn
ldy #>get_shape_fn
bne ++
+ cpx #6
bcc +
jmp +++ ; mode out of range
+ dex
bne +
; mode PUT(1)
lda font_inverse
beq not_rvs
lda #<put_shape_inverse_fn
ldy #>put_shape_inverse_fn
bne ++
not_rvs
lda #<put_shape_fn
ldy #>put_shape_fn
bne ++
+ dex
bne +
; mode OR(2)
lda #<or_shape_fn
ldy #>or_shape_fn
bne ++
+ dex
bne +
; mode XOR(3)
lda #<xor_shape_fn
ldy #>xor_shape_fn
bne ++
+ dex
bne +
; mode AND(4)
lda #<and_shape_fn
ldy #>and_shape_fn
bne ++
+ ; mode NOT(5)
lda #<not_shape_fn
ldy #>not_shape_fn
++ sta $5e
sty $5f
; shift = (X1 AND 7)
lda param1
and #7
sta shift
sec
lda #8
sbc shift
sta shiftopposite
; shmask = 255 >> shift
ldx shift
lda ff_rshifted,X
sta shmask
; columns = int((x2+1-(x1 and 248)+7)/8); // screen columns
lda param1
and #248
eor #$ff ; start 1s complement
sec ; complete 1s complement
adc #(7+1)
clc
adc param3
lsr
lsr
lsr
sta shcolumns
; colnum = 0
lda #0
sta shcolnum
; ys = y2+1-y1
lda param4
clc
adc #1
sec
sbc param2
sta shys
ldx param1
ldy param2
jsr plot_prep ; $fb/$fc has address based on x, y is untouched
; $57/$58 = addr(src)-ys
sec
lda $fd
sbc shys
sta $57
lda $fe
sbc #0
sta $58
; do
; {
---
; i = 0
ldy param2
sty shbitmapy
; if (columns - colnum = 1) // last column
; mask &= 255 << (~X2 AND 7)
sec
lda shcolumns
sbc shcolnum
cmp #1
bne +
lda param3
and #7
tax
lda ff_lshiftedrev, x
and shmask
sta shmask
+
; do
; {
--
; data = (src[i-ys] << (8-shift)) | (src[i] >> shift)
ldx param5
beq ++
lda #0
tay
ldx shift ; should be 0..7
beq + ; optimize for speed (skips if shiftopposite is 8)
lda ($57),y
ldx shiftopposite ; due to optimization, should be 7..1
- asl
dex
bne -
+ sta $59
lda ($fd),y
ldx shift
beq +
- lsr
dex
bne -
+ ora $59
; data &= mask
and shmask
sta $59
; dst[i] = (dst[i] & ~mask) | data; // apply operator
++ ldy shbitmapy
jsr call_shape_fn
; ++dst
; destination handled by inc bitmapy
; ++src
inc $57
bne +