-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspredit.acme
728 lines (662 loc) · 17.9 KB
/
spredit.acme
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
; vim: syntax=assembly
;Zero page pointers
zero_page_pointer_0 = $fb
*=$0801
;Loader program in basic
;2019 SYS4096
!byte $0B, $08 ;Pointer to the next basic line
!byte $13, $14 ;Line number 2019
!byte $9e ;SYS token
!byte $34, $30, $39, $36 ;"4096" ascii encoded
!byte $00 ;end of line
!byte $00, $00 ;End of program (null next line pointer)
;Actual program starts here
*=$1000
;Setup program
jsr sub_screen_clear
jsr sub_screen_set_color
jsr sub_canvas_set_color
jsr sub_screen_draw_vertical_border
jsr sub_screen_draw_logo
jsr sub_screen_draw_hex_all
jsr sub_screen_draw_color_info
jsr sub_screen_draw_signature
jsr sub_sprite_setup
;Setup irq
sei ;Disable interupts for now
lda #%01111111 ;Disable CIA-1 interupts
sta $dc0d
and $d011 ;Clear the most significant vic irq bit
sta $d011
lda #$00 ;Set at which line should the irq happen
sta $d012
lda #<irq ;Set the interupt vector
sta $0314
lda #>irq
sta $0315
lda #%00000001 ;Enable raster interupts at vic
sta $d01a
cli ;Reenable interupts
;Done with setup, wait for interupts
jmp * ;Do nothing
;IRQ
irq
asl $d019 ;Acknowledge the interupt
jsr sub_main_loop
jmp $ea31 ;Rune kernal interupt handler
;End irq
;Run every frame
sub_main_loop
jsr sub_cursor_save
jsr sub_cursor_joystick
jsr sub_keyboard_read
jsr sub_canvas_restore
jsr sub_canvas_cursor
rts
;END main_loop
;Reads keyboard
sub_keyboard_read
lda $dc02 ;Save cia directions, probably not necessary
pha ;but just in case we want to reverty to kernal
lda $dc03 ;input later...
pha
lda #$ff ;Set the port directions
sta $dc02
lda #$00
sta $dc03
lda #%11111011 ;Query for F and D
sta $dc00
;F
lda #%00100000
bit $dc01
bne +
lda var_keyboard_debounce_f
cmp #0
bne ++
inc var_color_fg
jsr sub_keyboard_change_color
lda #1
sta var_keyboard_debounce_f
jmp ++
+ lda #0
sta var_keyboard_debounce_f
;D
++ lda #%00000100
bit $dc01
bne +
lda var_keyboard_debounce_d
cmp #0
bne ++
inc var_color_bd
jsr sub_keyboard_change_color
lda #1
sta var_keyboard_debounce_d
jmp ++
+ lda #0
sta var_keyboard_debounce_d
++ lda #%11110111 ;Query for B
sta $dc00
;B
lda #%00010000
bit $dc01
bne +
lda var_keyboard_debounce_b
cmp #0
bne ++
inc var_color_bg
jsr sub_keyboard_change_color
lda #1
sta var_keyboard_debounce_b
jmp ++
+ lda #0
sta var_keyboard_debounce_b
++ pla ;Restore cia directions
sta $dc03
pla
sta $dc02
rts
;END keyboard_read
;Done after every color change key
sub_keyboard_change_color
lda #$0f
and var_color_bg
sta var_color_bg
lda #$0f
and var_color_bd
sta var_color_bd
lda #$0f
and var_color_fg
sta var_color_fg
jsr sub_screen_set_color
jsr sub_canvas_set_color
jsr sub_sprite_setup
jsr sub_screen_draw_color_info
rts
;END keyb0ard_change_color
;Clear screen and set BG to black
sub_screen_clear
lda #$60 ;Blank screen code
ldx #$00
- sta $0400, x ;Clear screen
sta $0500, x
sta $0600, x
sta $0700, x
dex ;Decrament x
bne - ;Keep clearing until reached $00 again
rts
;END clear_screen
;Draw the vertical border on the screen
sub_screen_draw_vertical_border
lda #3 ;The starting line - 1
- clc
adc #1
pha
asl ;Multiply by two
tax
lda lut_screen_line, x ;Fetch the line location from lut
sta zero_page_pointer_0
inx
lda lut_screen_line, x
sta zero_page_pointer_0 + 1
ldy #24 ;Column index to draw at
lda #0
lda #$e0 ;Character to draw
sta (zero_page_pointer_0), y
pla
cmp #24 ;Done on 25th line
bne -
rts
;END screen_draw_vertical_border
;Draw the top logo on the screen
sub_screen_draw_logo
lda #160 ;Screen code count
- tax
dex
lda const_screen_logo, x
sta $0400, x
txa
cmp #$00
bne - ;until zero reached
rts
;END screen_draW_logo
;Draws color info on the screen
sub_screen_draw_color_info
;FG
lda #$f2 ;Not a lot of text so doing it
sta zero_page_pointer_0 ;completely manually :)
lda #$06
sta zero_page_pointer_0+1
ldy #$00 ;First the label
lda #$06
sta (zero_page_pointer_0),y
iny
lda #$07
sta (zero_page_pointer_0),y
iny
iny
lda var_color_fg ;Then most significant bit of the color
lsr ;Should always be 0, but in case of future
lsr ;changes doing it properly
lsr
lsr
tax
lda lut_hex_nibble_screen_codes,x
sta (zero_page_pointer_0),y
iny
lda #$0f ;Then the least significant bit gets shown
lda var_color_fg ;Kinda funny how here it is more important
tax
lda lut_hex_nibble_screen_codes,x
sta (zero_page_pointer_0),y
;BG
lda #$1a ;Not a lot of text so doing it
sta zero_page_pointer_0 ;completely manually :)
lda #$07
sta zero_page_pointer_0+1
ldy #$00 ;First the label
lda #$02
sta (zero_page_pointer_0),y
iny
lda #$07
sta (zero_page_pointer_0),y
iny
iny
lda var_color_bg ;Then most significant bit of the color
lsr ;Should always be 0, but in case of future
lsr ;changes doing it properly
lsr
lsr
tax
lda lut_hex_nibble_screen_codes,x
sta (zero_page_pointer_0),y
iny
lda #$0f ;Then the least significant bit gets shown
lda var_color_bg ;Kinda funny how here it is more important
tax
lda lut_hex_nibble_screen_codes,x
sta (zero_page_pointer_0),y
;BD
lda #$42 ;Not a lot of text so doing it
sta zero_page_pointer_0 ;completely manually :)
lda #$07
sta zero_page_pointer_0+1
ldy #$00 ;First the label
lda #$02
sta (zero_page_pointer_0),y
iny
lda #$04
sta (zero_page_pointer_0),y
iny
iny
lda var_color_bd ;Then most significant bit of the color
lsr ;Should always be 0, but in case of future
lsr ;changes doing it properly
lsr
lsr
tax
lda lut_hex_nibble_screen_codes,x
sta (zero_page_pointer_0),y
iny
lda #$0f ;Then the least significant bit gets shown
lda var_color_bd ;Kinda funny how here it is more important
tax
lda lut_hex_nibble_screen_codes,x
sta (zero_page_pointer_0),y
rts
;END screen_draw_color_info
;Draw signature
sub_screen_draw_signature
lda #$b9 ;Location on screen
sta zero_page_pointer_0
lda #$07
sta zero_page_pointer_0+1
ldy #$6
- lda const_signature, y
sta (zero_page_pointer_0),y
dey
bpl -
lda #$e4 ;Location on screen
sta zero_page_pointer_0
ldy #$3
- lda const_signature+7, y
sta (zero_page_pointer_0),y
dey
bpl -
rts
;END screen_draw_signature
;Set the color of the screen
sub_screen_set_color
ldx #$00
lda var_color_bg ;Index/BG Color
sta $d021 ;VIC REG - BG Color 0
lda var_color_bd ;Screen color
sta $d020 ;VIC REG - Border color
- sta $d800, x ;Set fg color
sta $d900, x
sta $da00, x
sta $db00, x
dex ;Decrament x
bne - ;Keep clearing until reached $00 again
rts
;END screen_set_color
;Draws all of the hex bytes on screen
sub_screen_draw_hex_all
ldy #$00 ;Starting row
-- tya
pha
ldx #$00
- pla
tay
pha
txa
pha
jsr sub_screen_draw_hex_byte
pla
clc
adc #1
tax
cmp #3
bne -
pla
clc
adc #1
tay
cmp #21 ;Lines
bne --
rts
;END screen_draw_hex_all
;Draws sprite content byte as hex on screen for bytes at x and y
sub_screen_draw_hex_byte
txa ;Be sure not to lose the coordinates
pha
tya
pha
clc
adc #4 ;Offset line to match with the canvas
asl ;Multiply by two to look up in lut
tax ;Continue with regular screen location lookup
lda lut_screen_line, x
sta zero_page_pointer_0
inx
lda lut_screen_line, x
sta zero_page_pointer_0+1
pla ;Restore locations
tay
pla
tax
sta var_tmp
lda lut_sprite_line_offset, y ;Look up sprite byte
clc
adc var_tmp
tay
lda const_sprites_mem, y
pha ;Store for later
txa ;multiply 2x + x = 3x
asl
sta var_tmp
txa
clc
adc var_tmp
adc #25 ;Rendering offset
tay
pla ;Get it and save it again
pha
lsr ;Get the most significan nibble
lsr
lsr
lsr
tax ;Get the screen code
lda lut_hex_nibble_screen_codes, x
sta (zero_page_pointer_0), y ;Print
iny
pla ;Get the most significant nibble
and #%00001111
tax
lda lut_hex_nibble_screen_codes, x
sta (zero_page_pointer_0), y
rts
;END screen_draw_hex
;Save cursors previous location
sub_cursor_save
lda var_cursor_x
sta var_cursor_prev_x
lda var_cursor_y
sta var_cursor_prev_y
rts
;END cursor_save
;Handle joystick input on port 2
sub_cursor_joystick
lda #$00
cmp var_cursor_cooldown
beq + ;If no cooldown process input
dec var_cursor_cooldown
jmp ++
+ ;UP
lda #%00000001 ;Load direction mask
bit $dc00 ;Test if it's pressed down
bne + ;If not skip instructions
dec var_cursor_y
jsr sub_cursor_moved
+ ;DOWN
lda #%00000010
bit $dc00
bne +
inc var_cursor_y
jsr sub_cursor_moved
+ ;LEFT
lda #%00000100
bit $dc00
bne +
dec var_cursor_x
jsr sub_cursor_moved
+ ;RIGHT
lda #%00001000
bit $dc00
bne +
inc var_cursor_x
jsr sub_cursor_moved
++
+ ;FIRE
jsr sub_cursor_limit
+ lda #%00010000
bit $dc00
bne +
lda #$01 ;Check if debounce is active
cmp var_plotting_debounce
beq ++
jsr sub_cursor_plot ;Otherwise plot
lda #$01 ;And enable debounce
sta var_plotting_debounce
jmp ++
+ lda #$00 ;If button wasn't pressed disable debounce
sta var_plotting_debounce
++ rts
;END handle_joystick
;Does everything that has to be done when plotting to screen
sub_cursor_plot
ldx var_cursor_x
ldy var_cursor_y
jsr sub_sprite_toggle_pixel
lda var_cursor_x ;calculate which byte will be changed aka div by 3
ldx #0
ldy var_cursor_y
cmp #8
bmi ++
cmp #16
bmi +
inx
+ inx
++ jsr sub_screen_draw_hex_byte
rts
;END cursor_plot
;Utilities ran after cursor moved involving debounce and cooldown
sub_cursor_moved
lda #const_cursor_cooldown_time ;Set up cursor cooldown
sta var_cursor_cooldown
lda #$00 ;Disable plotting debounce
sta var_plotting_debounce
rts
;end cursor_moved
;Make sure cursor doesn't go out of bounds
sub_cursor_limit
lda #24 ;24 columns
cmp var_cursor_x
bne +
lda #0 ;Warp
sta var_cursor_x
+ lda #$ff ;Opposite direction
cmp var_cursor_x
bne +
lda #23 ;Warp
sta var_cursor_x
+ lda #21 ;21 rows
cmp var_cursor_y
bne +
lda #0 ;Warp
sta var_cursor_y
+ lda #$ff ;Opposite direction
cmp var_cursor_y
bne +
lda #20 ;Warp
sta var_cursor_y
+ rts
;END limit_cursor
;Restores previous cursor location
sub_canvas_restore
ldx var_cursor_prev_x ;Look up the pixel on the sprite itself
ldy var_cursor_prev_y
jsr sub_sprite_get_pixel
cmp #$01
beq +
lda #$60 ;Empty
jmp ++
+ lda #$e0 ;Filled
++ ldx var_cursor_prev_x ;Plot the respective character
ldy var_cursor_prev_y
jsr sub_canvas_plot
rts
;END canvas_restore
;Draws cursor on canvas
sub_canvas_cursor
ldx var_cursor_x ;Look up the pixel on the sprite itself
ldy var_cursor_y
jsr sub_sprite_get_pixel
cmp #$01
beq +
lda #$57 ;Empty
jmp ++
+ lda #$d7 ;Filled
++ ldx var_cursor_x
ldy var_cursor_y
jsr sub_canvas_plot
rts
;END canvas_cursor
;Sets the color of canvas
sub_canvas_set_color
lda #4 ;First line
-- pha ;Save for later
asl ;Multiply by two
tax
lda lut_color_line, x
sta zero_page_pointer_0
inx
lda lut_color_line, x
sta zero_page_pointer_0+1
ldy #$ff ;First column - 1
- iny
lda var_color_fg ;Color
sta (zero_page_pointer_0),y
tya
cmp #23 ;Last column
bne -
pla
clc
adc #1
cmp #25 ;Last line
bne --
rts
;END canvas_set_color
;Plots a screen code in register A on canvas at X, Y register locations
sub_canvas_plot
pha ;Save A for later move y to a and x to y
iny ;Shift canvas down a bit
iny
iny
iny
tya ;Confusing, but required for addressing modes to work
pha
txa
tay
pla
asl ;Multiply y by 2
tax
lda lut_screen_line, x ;Move line location to a pointer
sta zero_page_pointer_0
inx
lda lut_screen_line, x
sta zero_page_pointer_0+1
pla ;Screen code to draw
sta (zero_page_pointer_0), y
rts
;END canvas_plot
;Sets up sprite preview
sub_sprite_setup
lda #const_sprites_vic ;Get the sprite location in VIC friendly way
sta $07f8 ;Tell VIC where the sprite is
lda #$01 ;Sprite enable flags
sta $d015 ;Tell VIC to enable sprite
lda var_color_fg
sta $d027 ;Set sprite color
lda #$30 ;Position sprite
sta $d000
lda #$60
sta $d001
lda #$01
sta $d010
rts
;END setup_sprite
;Find pixel data at x any y registers on sprite
sub_sprite_get_pixel
lda lut_sprite_line_offset, y ;Find line offset in the lookup table
tay
txa ;Find total byte offset, store it in y
- cmp #$08 ;and find bit offset and store it in x
bmi +
sbc #$08
iny
jmp -
+ tax
lda const_sprites_mem, y ;Fetch the byte this pixel is in from sprite
inx ;Find the pixel bit in the byte
inx
- rol
dex
bne -
and #%00000001 ;Clear the other bits
rts
;END sprite_get_pixel
;Toggle a pixel on sprite at X and Y
sub_sprite_toggle_pixel
lda lut_sprite_line_offset, y ;Find line offset in the lookup table
tay
txa ;Find total byte offset, store it in y
- cmp #$08 ;and find bit offset and store it in x
bmi +
sbc #$08
iny
jmp -
+ tax
lda #$01
inx ;Find the pixel bit in the byte
inx
- ror
dex
bne -
eor const_sprites_mem, y ;Toggle the pixel
sta const_sprites_mem, y
rts
;END sprite_set_pixel
;Variables
var_tmp !byte 0
var_plotting_debounce !byte 0
var_cursor_cooldown !byte 0
var_cursor_prev_x !byte 0
var_cursor_prev_y !byte 0
var_cursor_x !byte 0
var_cursor_y !byte 0
var_color_bg !byte 0
var_color_bd !byte $0b
var_color_fg !byte 1
var_keyboard_debounce_f !byte 0
var_keyboard_debounce_b !byte 0
var_keyboard_debounce_d !byte 0
;Screen line LUT
lut_screen_line !for I, 0, 24 {
!word $0400 + 40*I
}
;Screen color line LUT
lut_color_line !for I, 0, 24 {
!word $d800 + 40*I
}
;Sprite line offset LUT
lut_sprite_line_offset !for I, 0, 20 {
!byte I*3
}
lut_hex_nibble_screen_codes
!byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$01,$02,$03,$04,$05,$06
; 0 1 2 3 4 5 6 7 8 9 a b c d e f
;Constants
const_sprites_mem = $2000
const_sprites_vic = $2000/64
const_cursor_cooldown_time = 3
const_signature
!byte $16,$0d,$05,$04,$0e,$09,$13
!byte $32,$30,$31,$39
; V M E D N I S
; 2 0 1 9
const_screen_logo ;Logo in form of direct screen codes to allow me to do some pretty art
!byte $a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$ec,$e2,$fb,$e2,$e2,$ec,$e2,$fb,$e2,$e2,$ec,$e2,$a0,$fb,$e2,$e2,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0
!byte $a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$61,$e2,$fb,$7c,$7e,$61,$e2,$e1,$7c,$e2,$61,$a0,$e1,$e1,$e7,$f4,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0
!byte $a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$e1,$e1,$a0,$61,$fc,$fb,$e1,$a0,$61,$a0,$e1,$e1,$e7,$f4,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0
!byte $a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$fc,$62,$fe,$fe,$a0,$fc,$a0,$fe,$62,$62,$fc,$62,$fe,$fe,$fc,$fe,$96,$b1,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0,$a0; S P R E D I T !
*=$2000 ;To get warnings if we are encroaching on sprite space