-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.asm
711 lines (651 loc) · 11.1 KB
/
snake.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
// Assembler: KickAssembler v4.19
BasicUpstart2(main)
.var brkFile = createFile("breakpoints.txt")
.macro break() {
.eval brkFile.writeln("break " + toHexString(*))
}
//.var music = LoadSid("/home/methos/Music/C64Music/MUSICIANS/0-9/20CC/van_Santen_Edwin/13_Seconds_of_Massacre.sid")
.var music = LoadSid("/home/methos/Music/HVSC69/MUSICIANS/0-9/20CC/van_Santen_Edwin/Blackmail_Tune_1.sid")
//.var music = LoadSid("/home/methos/Music/C64Music/MUSICIANS/T/Tel_Jeroen/Fun_Fun.sid")
//.var music = LoadSid("fallen_down_b.sid")
#import "pseudo.lib"
.var irq_line_top = $20
.var vic = $0000
.var screen = vic + $0400
.var spr_data = vic + $2400
.var col_delay = 4
.var tmp = $ff
// snake vars
.var grow = 4
.var max_size = 32
// characters:
//
// $40 ---
//
// |
// $42 |
// |
//
// $55 /-
// |
//
// $49 -\
// |
// |
// $4a \-
//
// |
// $4b -/
//
// *
// $51 ***
// *
main:
jsr clear
jsr snake_init
lda #0
jsr music.init
jsr spr_init
// inline: setup irq
sei
mov #$35 : $01
mov16 #irq_top : $fffe
mov #$1b : $d011
mov #irq_line_top : $d012
mov #$81 : $d01a
mov #$7f : $dc0d
mov #$7f : $dd0d
lda $dc0d
lda $dd0d
mov #$ff : $d019
cli
jmp *
spr_init:
// sprite logic
lda #(spr_data - vic + 64 * 0) / 64
sta screen + $03f8
lda #(spr_data - vic + 64 * 1) / 64
sta screen + $03f9
lda #(spr_data - vic + 64 * 2) / 64
sta screen + $03fa
lda #(spr_data - vic + 64 * 3) / 64
sta screen + $03fb
lda #(spr_data - vic + 64 * 4) / 64
sta screen + $03fc
lda #(spr_data - vic + 64 * 5) / 64
sta screen + $03fd
lda #(spr_data - vic + 64 * 6) / 64
sta screen + $03fe
// copy sprites
ldx #0
!l:
lda m0spr, x
sta spr_data + 64 * 0, x
inx
cpx #64
bne !l-
ldx #0
!l:
lda m1spr, x
sta spr_data + 64 * 1, x
inx
cpx #64
bne !l-
!l:
lda m2spr, x
sta spr_data + 64 * 2, x
inx
cpx #64
bne !l-
!l:
lda m3spr, x
sta spr_data + 64 * 3, x
inx
cpx #64
bne !l-
!l:
lda m4spr, x
sta spr_data + 64 * 4, x
inx
cpx #64
bne !l-
!l:
lda m5spr, x
sta spr_data + 64 * 5, x
inx
cpx #64
bne !l-
!l:
lda m6spr, x
sta spr_data + 64 * 6, x
inx
cpx #64
bne !l-
// show sprites
lda #$7f
sta $d015
lda #$01
sta $d027
lda #$03
sta $d028
lda #$0f
sta $d029
lda #$09
sta $d02a
lda #$0b
sta $d02b
lda #$05
sta $d02c
lda #$0d
sta $d02e
lda #$88 + -1 * 20
sta $d000
lda #$88 + 0 * 20
sta $d002
lda #$88 + 1 * 20
sta $d004
lda #$88 + 2 * 20
sta $d006
lda #$88 + 3 * 20
sta $d008
lda #$88 + 4 * 20
sta $d00a
lda #$88 + 5 * 20
sta $d00c
lda #$80
sta $d001
sta $d003
sta $d005
sta $d007
sta $d009
sta $d00b
sta $d00d
rts
spr_step:
lda spr_count0
and #$3f
tax
lda sinus, x
sta $d001
inc spr_count0
lda spr_count1
and #$3f
tax
lda sinus, x
sta $d003
inc spr_count1
lda spr_count2
and #$3f
tax
lda sinus, x
sta $d005
inc spr_count2
lda spr_count3
and #$3f
tax
lda sinus, x
sta $d007
inc spr_count3
lda spr_count4
and #$3f
tax
lda sinus, x
sta $d009
inc spr_count4
lda spr_count5
and #$3f
tax
lda sinus, x
sta $d00b
inc spr_count5
lda spr_count6
and #$3f
tax
lda sinus, x
sta $d00d
inc spr_count6
lda spr_delay
beq !change+
dec spr_delay
rts
!change:
mov #col_delay : spr_delay
inc $d027
inc $d027
inc $d028
inc $d028
inc $d029
inc $d029
inc $d02a
inc $d02a
inc $d02b
inc $d02b
inc $d02c
inc $d02c
inc $d02d
inc $d02d
rts
spr_delay:
.byte 4
spr_count0:
.byte 0
spr_count1:
.byte 2
spr_count2:
.byte 4
spr_count3:
.byte 6
spr_count4:
.byte 8
spr_count5:
.byte 10
spr_count6:
.byte 12
// clear screen
clear:
lda #' '
ldx #0
!l:
sta $0400, x
sta $0500, x
sta $0600, x
sta $06e8, x
inx
bne !l-
lda #0
sta $d020
// green: 5
// light green: 13
sta $d021
lda #5
ldx #0
!l:
sta $d800, x
sta $d900, x
sta $da00, x
sta $dae8, x
inx
bne !l-
rts
snake_init:
lda #' '
jsr next_dot
// remember our position
mov dot_y : snake_y
mov dot_x : snake_x
mov16 dot_pos : snake_pos
// setup other snake stuff
mov #grow : snake_grow
lda #0
sta snake_head
sta snake_tail
sta snake_size
// setup target
lda #$51
jsr next_dot
rts
next_dot:
tay
ldx dot_index
!l:
// setup store address and save it elsewhere
lda dots_low, x
sta !put+ + 1
sta dot_pos
lda dots_high, x
sta !put+ + 2
sta dot_pos + 1
// save y and x
lda dots_y, x
sta dot_y
lda dots_x, x
sta dot_x
tya
!put:
sta $0400
// dot_index = (dot_index + 1) % 32
inx
txa
and #31
sta dot_index
rts
delay:
.byte 0
step:
jsr spr_step
// XXX uncomment this to slow down the game
// ldx delay
// beq snake_step
// dec delay
// rts
snake_step:
mov #4 : delay
// setup snake movement code
ldx snake_dir
lda fptr_snake, x
sta !fptr+ + 1
lda fptr_snake + 1, x
sta !fptr+ + 2
!fptr:
jsr !hang+
// compute new head screen location
lda snake_dp
clc
adc snake_pos
sta snake_pos
lda snake_dp + 1
adc snake_pos + 1
sta snake_pos + 1
// grow code
lda snake_size
cmp #max_size
beq !no_grow+
ldx snake_grow
beq !no_grow+
dex
//dec $d020
stx snake_grow
inc snake_size
jmp !hold_tail+
!no_grow:
// erase old tail
ldx snake_tail
lda snakes_tbl, x
sta !erase+ + 1
lda snakes_tbl + 1, x
sta !erase+ + 2
lda #' '
!erase:
sta $0400
// tail = (tail + 1) % 32
lda snake_tail
clc
adc #2
and #63
sta snake_tail
!hold_tail:
// NOTE tail should not overlap with target! (target becomes invisible)
// update head and store head in table
ldx snake_head
lda snake_pos
sta !fetch_head+ + 1
sta !put_head+ + 1
sta snakes_tbl, x
lda snake_pos + 1
sta !fetch_head+ + 2
sta !put_head+ + 2
sta snakes_tbl + 1, x
// head = (head + 1) % 32
lda snake_head
clc
adc #2
and #63
sta snake_head
!fetch_head:
lda $0400
cmp #$51
beq !next+
cmp #' '
bne !die+
jmp !move+
!next:
inc snake_grow
lda #$51
jsr next_dot
!move:
lda #$e0
!put_head:
sta $0400
!hang:
rts
!die:
// reset screen state
// remove snake
ldx #0
ldy #' '
!l:
lda snakes_tbl, x
sta !erase+ + 1
lda snakes_tbl + 1, x
sta !erase+ + 2
!erase:
sty $0400
inx
inx
cpx #$40
bne !l-
// remove target
lda dot_pos
sta !erase+ + 1
lda dot_pos + 1
sta !erase+ + 2
lda #' '
!erase:
sta $0400
// reset snake state
jsr snake_init
rts
dot_index:
.byte 0
dot_pos:
.word 0
dot_y:
.byte 0
dot_x:
.byte 0
snake_pos:
.word 0
snake_y:
.byte 0
snake_x:
.byte 0
snake_dir:
.byte 0
snake_dp:
.word 0
irq_top:
irq
//inc $d020
jsr step
jsr music.play
//dec $d020
qri
step_snake_right:
mov #0 : snake_dir
lda snake_x
cmp dot_x
bne !move+
lda snake_y
cmp dot_y
bmi !down+
beq !next+
jmp step_snake_up
!next:
!move:
lda snake_x
cmp #39
beq !wrap+
mov16 #1 : snake_dp
inc snake_x
rts
!wrap:
mov16 #-39 : snake_dp
mov #0 : snake_x
rts
!down:
jmp step_snake_down
step_snake_up:
mov #2 : snake_dir
lda snake_y
cmp dot_y
bne !move+
lda snake_x
cmp dot_x
bmi !right+
beq !next+
jmp step_snake_left
!next:
!move:
lda snake_y
beq !wrap+
mov16 #-40 : snake_dp
dec snake_y
rts
!wrap:
mov16 #24 * 40 : snake_dp
mov #24 : snake_y
rts
!right:
jmp step_snake_right
step_snake_left:
mov #4 : snake_dir
lda snake_x
cmp dot_x
bne !move+
lda snake_y
cmp dot_y
bmi !down+
beq !next+
jmp step_snake_up
!next:
!move:
lda snake_x
beq !wrap+
mov16 #-1 : snake_dp
dec snake_x
rts
!wrap:
mov16 #39 : snake_dp
mov #39 : snake_x
rts
!down:
jmp step_snake_down
step_snake_down:
mov #6 : snake_dir
lda snake_y
cmp dot_y
bne !move+
lda snake_x
cmp dot_x
bmi !right+
beq !next+
jmp step_snake_left
!next:
!move:
lda snake_y
cmp #24
beq !wrap+
mov16 #40 : snake_dp
inc snake_y
rts
!wrap:
mov16 #-24 * 40 : snake_dp
mov #0 : snake_y
rts
!right:
jmp step_snake_right
snake_head:
.byte 0
snake_tail:
.byte 0
snake_grow:
.byte 0
snake_size:
.byte 0
.align $80
// precalculated random locations for dots
dots_low:
.byte $34, $9A, $4D, $26, $13, $89, $44, $A2, $D1, $68, $34, $1A, $8D, $46, $23, $91, $C8, $E4, $72, $39, $9C, $CE, $E7, $F3, $F9, $FC, $7E, $BF, $5F, $AF, $57, $AB
dots_high:
.byte $05, $04, $04, $06, $05, $06, $07, $05, $04, $04, $06, $05, $04, $06, $07, $07, $05, $04, $06, $07, $07, $07, $07, $04, $05, $06, $05, $06, $05, $06, $07, $07
dots_y:
.byte $07, $03, $01, $0D, $06, $10, $14, $0A, $05, $02, $0E, $07, $03, $0E, $14, $16, $0B, $05, $0F, $14, $17, $18, $18, $07, $0C, $13, $09, $11, $08, $11, $15, $17
dots_x:
.byte $1C, $22, $25, $1E, $23, $09, $24, $12, $09, $18, $04, $02, $15, $16, $03, $21, $10, $1C, $1A, $19, $04, $0E, $27, $03, $19, $04, $16, $17, $1F, $07, $0F, $13
// bytes used: 128
fptr_snake:
.word step_snake_right, step_snake_up, step_snake_left, step_snake_down
.align $40
snakes_tbl:
.word 0, 0, 0, 0, 0, 0, 0, 0
.word 0, 0, 0, 0, 0, 0, 0, 0
.word 0, 0, 0, 0, 0, 0, 0, 0
.word 0, 0, 0, 0, 0, 0, 0, 0
.align $40
m0spr:
.byte $00,$00,$00,$0f,$00,$00,$0f,$00
.byte $00,$0f,$00,$00,$0f,$00,$00,$0f
.byte $00,$00,$0f,$00,$00,$0f,$00,$00
.byte $0f,$00,$00,$0f,$00,$00,$0f,$00
.byte $00,$0f,$00,$00,$0f,$00,$00,$0f
.byte $00,$00,$0f,$00,$00,$0f,$00,$00
.byte $0f,$00,$00,$0f,$ff,$f8,$0f,$ff
.byte $f8,$0f,$ff,$f8,$00,$00,$00,$00
.align $40
m1spr:
.byte $00,$00,$00,$00,$7e,$00,$01,$ff
.byte $80,$03,$ff,$c0,$07,$ff,$e0,$07
.byte $c3,$e0,$07,$81,$e0,$0f,$81,$f0
.byte $0f,$81,$f0,$0f,$00,$f0,$0f,$00
.byte $f0,$0f,$00,$f0,$0f,$81,$f0,$0f
.byte $81,$f0,$07,$81,$e0,$07,$c3,$e0
.byte $07,$ff,$e0,$03,$ff,$c0,$01,$ff
.byte $80,$00,$7e,$00,$00,$00,$00,$00
.align $40
m2spr:
.byte $00,$00,$00,$00,$7e,$00,$00,$ff
.byte $00,$01,$ff,$80,$03,$e7,$c0,$07
.byte $81,$e0,$07,$81,$e0,$0f,$81,$f0
.byte $0f,$00,$f0,$0f,$00,$f0,$0f,$ff
.byte $f0,$0f,$ff,$f0,$0f,$ff,$f0,$0f
.byte $00,$f0,$0f,$00,$f0,$0f,$00,$f0
.byte $0f,$00,$f0,$0f,$00,$f0,$0f,$00
.byte $f0,$0f,$00,$f0,$00,$00,$00,$00
.align $40
m3spr:
.byte $00,$00,$00,$0f,$fc,$00,$0f,$ff
.byte $00,$0f,$ff,$80,$0f,$07,$c0,$0f
.byte $01,$e0,$0f,$01,$e0,$0f,$01,$f0
.byte $0f,$00,$f0,$0f,$00,$f0,$0f,$00
.byte $f0,$0f,$00,$f0,$0f,$00,$f0,$0f
.byte $01,$f0,$0f,$01,$e0,$0f,$01,$e0
.byte $0f,$07,$c0,$0f,$ff,$80,$0f,$ff
.byte $00,$0f,$fc,$00,$00,$00,$00,$00
.align $40
m4spr:
.byte $00,$00,$00,$0f,$ff,$e0,$0f,$ff
.byte $e0,$0f,$ff,$e0,$00,$7c,$00,$00
.byte $7c,$00,$00,$7c,$00,$00,$7c,$00
.byte $00,$7c,$00,$00,$7c,$00,$00,$7c
.byte $00,$00,$7c,$00,$00,$7c,$00,$00
.byte $7c,$00,$00,$7c,$00,$00,$7c,$00
.byte $00,$7c,$00,$0f,$ff,$e0,$0f,$ff
.byte $e0,$0f,$ff,$e0,$00,$00,$00,$00
.align $40
m5spr:
.byte $00,$00,$00,$0f,$80,$f0,$0f,$80
.byte $f0,$0f,$c0,$f0,$0f,$c0,$f0,$0f
.byte $e0,$f0,$0f,$e0,$f0,$0f,$70,$f0
.byte $0f,$70,$f0,$0f,$38,$f0,$0f,$38
.byte $f0,$0f,$1c,$f0,$0f,$1c,$f0,$0f
.byte $0e,$f0,$0f,$0e,$f0,$0f,$07,$f0
.byte $0f,$07,$f0,$0f,$03,$f0,$0f,$03
.byte $f0,$0f,$01,$f0,$00,$00,$00,$00
.align $40
m6spr:
.byte $00,$00,$00,$00,$7f,$80,$01,$ff
.byte $80,$03,$ff,$80,$07,$ff,$80,$07
.byte $c0,$00,$07,$80,$00,$0f,$80,$00
.byte $0f,$80,$00,$0f,$1f,$c0,$0f,$1f
.byte $e0,$0f,$1f,$e0,$0f,$81,$e0,$0f
.byte $81,$e0,$07,$81,$e0,$07,$c3,$e0
.byte $07,$ff,$e0,$03,$ff,$c0,$01,$ff
.byte $80,$00,$7e,$00,$00,$00,$00,$00
.align $40
sinus:
.fill $40, round($80 + $08 * sin(toRadians(i * 360 / $40)))
// MUSIC
* = music.location "music"
.fill music.size, music.getData(i)
.print "music_init = $" + toHexString(music.init)
.print "music_play = $" + toHexString(music.play)