-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.asm
More file actions
815 lines (726 loc) · 21.9 KB
/
game.asm
File metadata and controls
815 lines (726 loc) · 21.9 KB
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
defm copyBytes
lda #</1
sta $fb
lda #>/1
sta $fc
lda #</2
sta $fd
lda #>/2
sta $fe
lda #</3
sta $03
lda #>/3
sta $04
ldx #0
ldy #0
jsr copy
endm
defm debug
ldx /1
txa
sta $02
lda #$80
and $02
lsr
lsr
lsr
lsr
lsr
lsr
lsr
sta 1024
txa
sta $02
lda #$40
and $02
lsr
lsr
lsr
lsr
lsr
lsr
sta 1025
txa
sta $02
lda #$20
and $02
lsr
lsr
lsr
lsr
lsr
sta 1026
txa
sta $02
lda #$10
and $02
lsr
lsr
lsr
lsr
sta 1027
txa
sta $02
lda #$08
and $02
lsr
lsr
lsr
sta 1028
txa
sta $02
lda #$04
and $02
lsr
lsr
sta 1029
txa
sta $02
lda #$02
and $02
lsr
sta 1030
txa
sta $02
lda #$01
and $02
sta 1031
endm
;setsprite nr,x,y,c,spr
defm setsprite
ldx #/1
lda #/4
sta $d027,X
lda #/5
sta $07f8,X
txa
rol A
tax
lda #/2
sta $d000,X
lda #/3
sta $d001,X
endm
screenChars = $0400
colorRam = $d800
joystick = $dc00
spriteEnable = $d015
sprite0P = $07f8
sprite0X = $d000
sprite0Y = $d001
sprite0C = $d027
sprite1P = $07f9
sprite1X = $d002
sprite1Y = $d003
sprite1C = $d028
logicMapAddr = $6000
randomByte = $D41B
directionUp = 1
directionDown = 2
directionLeft = 4
directionRight = 8
dinoMoveSpeed = 16
spriteDinoRight = $80
spriteDinoLeft = $88
fruitBerry = $A0
dinoMapMove = $80 ;7bit walkout of screen
; 10 SYS (4096):REM @c64cosmin 2022
*=$0801
BYTE $20, $08, $0A, $00, $9E, $20, $28, $34, $30, $39, $36, $29, $3a, $8f, $20, $40, $43, $36, $34, $43, $4F, $53, $4D, $49, $4E, $20, $32, $30, $32, $32, $00, $00, $00
*=$2000
incbin "sprite.spt" , 1, 40, true
*=$3000
incbin "tiles.cst", 0, 255
*=$4000
incbin "levels.sdd", 1, 2
*=$5000
dinoX byte 96
dinoXHi byte 0
dinoY byte 64
dinoMapAddr byte 0
dinoState byte 0
dinoMoveIncrement byte 0
dinoAnim byte 0
dinoSprite byte spriteDinoRight
dinoColor byte 13
qdinoX byte 112
qdinoXHi byte 0
qdinoY byte 64
qdinoMapAddr byte 0
qdinoState byte 0
qdinoMoveIncrement byte 0
qdinoAnim byte 0
qdinoSprite byte spriteDinoRight
qdinoSpriteColor byte 10
qqdinoX byte 128
qqdinoXHi byte 0
qqdinoY byte 64
qqdinoMapAddr byte 0
qqdinoState byte 0
qqdinoMoveIncrement byte 0
qqdinoAnim byte 0
qqdinoSprite byte spriteDinoRight
qqdinoSpriteColor byte 7
qqqdinoX byte 144
qqqdinoXHi byte 0
qqqdinoY byte 64
qqqdinoMapAddr byte 0
qqqdinoState byte 0
qqqdinoMoveIncrement byte 0
qqqdinoAnim byte 0
qqqdinoSprite byte spriteDinoRight
qqqdinoSpriteColor byte 14
*=$1000
init ldx #$ff
stx spriteEnable
ldx #$17
stx $d011
ldx #24
stx $d016
ldx #$1c
stx $d018
ldx #0
stx $d020
ldx #5
stx $d021
ldx #0
stx $d022
ldx #9
stx $d023
;random generator
LDA #$6F
LDY #$81
LDX #$FF
STA $D413
STY $D412
STX $D40E
STX $D40F
STX $D414
lda #0
sta $06
ldx #0
jsr load_map
loop lda #$fb
raster cmp $d012
bne raster ;wait for raster
inc $d020
;logic
ldx #0 ;joystick 2
jsr joy_moved ;load joystate in $02
ldx #0 ;pointer to player
ldy #0
sty $04 ;player draw to sprite 0,1
jsr dino_update ;dino update
;logic
lda #0
sta $d020
;jsr dbg_map
jmp loop
;ldx #1
;jsr joy_moved
jsr random_control
ldx #9
ldy #2
sty $04
jsr dino_update
jsr random_control
ldx #18
ldy #4
sty $04
jsr dino_update
jsr random_control
ldx #27
ldy #6
sty $04
jsr dino_update
;logic
lda #0
sta $d020
;jsr dbg_map
jmp loop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;load map
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;register X to map index
load_map txa
cmp #0
bne load_map_1
copyBytes $4000, $d800, $03e8 ;map 0
copyBytes $47d0, $0400, $03e8
jmp logical_map
load_map_1 cmp #1
bne load_map_2
copyBytes $43e8, $d800, $03e8 ;map 1
copyBytes $4bb8, $0400, $03e8
load_map_2
logical_map lda #0
sta $02 ;i = 0; i < 240
sta $03 ;j = 0
lda #<screenChars
sta $05
lda #>screenChars
sta $06 ;pointer to screen chars
loop_logic_map lda #0
ldx $02
sta logicMapAddr,X
ldx #0
lda ($05,X)
and #$60 ;solid tile %x11xxxxx
cmp #$60
bne skip_set_solid
lda #1
ldx $02
sta logicMapAddr,X
skip_set_solid lda #2 ;pointer += 2
clc
adc $05
sta $05
lda #0
adc $06
sta $06 ;increment with carry pointer $05,$06
inc $03 ;j+=2
inc $03
lda $03
cmp #40
bne skip_j40 ;if j==40; pointer+=40
lda #40
clc
adc $05
sta $05
lda #0
adc $06
sta $06
lda #0
sta $03 ;j=0
skip_j40
inc $02 ;i++
lda $02
cmp #240
bne loop_logic_map ;reached end of logical map i < 240
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;debug map
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
dbg_map lda #0
sta $02 ;i = 0; i < 240
sta $03 ;j = 0
lda #<screenChars
sta $05
lda #>screenChars
sta $06 ;pointer to screen chars
lda #<colorRam
sta $fb
lda #>colorRam
sta $fc ;pointer to screen chars
loop_dbg_map ldx $02
lda logicMapAddr,X
ldx #0
sta ($fb,X)
lda #1
sta ($05,X)
lda #2 ;pointer += 2
clc
adc $05
sta $05
lda #0
adc $06
sta $06 ;increment with carry pointer $05,$06
lda #2
clc
adc $fb
sta $fb
lda #0
adc $fc
sta $fc ;increment with carry pointer $05,$06
inc $03 ;j+=2
inc $03
lda $03
cmp #40
bne skip_dbg_j40 ;if j==40; pointer+=40
lda #40
clc
adc $05
sta $05
lda #0
adc $06
sta $06
lda #40
clc
adc $fb
sta $fb
lda #0
adc $fc
sta $fc
lda #0
sta $03 ;j=0
skip_dbg_j40
inc $02 ;i++
lda $02
cmp #240
bne loop_dbg_map ;reached end of logical map i < 240
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;dino instance update
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;register X to pointer
;;$04 is sprite ID
;;$02 controls movement of dino
dino_update lda dinoMoveIncrement,X ;check if dino is not moving
cmp #0
beq dino_no_move ;if dino is not moving then not move
jmp dino_move ;if no movement just slide
dino_no_move lda $02 ;else check control
and #$f ;for movement
cmp #0 ;is dino moved
bne dino_ctrl ;if there is no control skip
;else look for collision
jmp dino_draw
dino_ctrl lda dinoState,X ;load state
and #$f0 ;drop movement
ora $02 ;add control
sta dinoState,X ;store state
jsr dino_addr
jsr dino_map_move
lda dinoState,X
and #dinoMapMove
cmp #dinoMapMove
beq dino_move
ldy $03 ;$03 next position map addr
lda logicMapAddr,Y ;load map property
cmp #0 ;is tile free?
bne dino_draw ;if not cannot move
lda #dinoMoveSpeed ;else start animation
sta dinoMoveIncrement,X
ldy $03 ;load front position
lda #1
sta logicMapAddr,Y ;mark place as occupied
dino_move jsr dino_mv_rtn
dino_move_dec dec dinoMoveIncrement,X
lda dinoMoveIncrement,X
cmp #0
bne dino_draw
;clean up positions on map
lda dinoState,X
and #dinoMapMove
cmp #dinoMapMove
bne dino_nrml_clean ;if not map move
lda dinoState,X
and #$7f
sta dinoState,X
dino_nrml_clean lda #0
ldy dinoMapAddr,X
sta logicMapAddr,Y ;clean current position
dino_draw ldy $04
lda dinoAnim,X ;animate walk
lsr A ; divide by 2
and #$7
clc
adc dinoSprite,X
sta sprite0P,Y
clc
adc #$10
sta sprite1P,Y
lda #0 ;black outline
sta sprite0C,Y
lda dinoColor,X
sta sprite1C,Y ;color outline
tya
asl A
tay ;multiply Y register with 2
lda dinoY,X
clc
adc #$35 ; offsetY
sta sprite0Y,Y
sta sprite1Y,Y
lda dinoX,X
sta $03
lda dinoXHi,X
sta $04
lda #$14 ; offsetX
clc
adc $03
sta $03
lda #0
adc $04
sta $04
lda $03
sta sprite0X,Y
sta sprite1X,Y
;high part of X position
lda $04
asl $04
ora $04
sta $04 ;double the bits in hi part
lda #3
sta $06 ;mask
sty $05
lsr $05 ;each dino has two sprites
lsr $05 ;divide back Yreg with 2
lda $05 ;sprite Xhi
cmp #0
beq dino_skip_xhi
dino_xhi asl $04
asl $04
asl $06
asl $06
dec $05
lda $05
cmp #0
bne dino_xhi
dino_skip_xhi lda $06 ;mask
eor #$ff ;invert
sta $06
lda $d010
and $06 ;remove bit
sta $d010
lda $04
ora $d010 ;set bit
sta $d010
rts ;return
dino_addr lda dinoY,X ;adr = ypos
and #$f0
lsr A
lsr A ;divide by 4 (ypos/16 * 4)
sta dinoMapAddr,X
clc
adc dinoMapAddr,X
adc dinoMapAddr,X
adc dinoMapAddr,X
adc dinoMapAddr,X
sta dinoMapAddr,X ;adr = ypos*4*5 == (y*20)
lda dinoX,X ;xpos
lsr A
lsr A
lsr A
lsr A ;divide by 16
clc
adc dinoMapAddr,X
sta dinoMapAddr,X
lda dinoXHi,X
cmp #0
beq dino_addr_nohi
lda #$10 ;0x100 shifted by 4 to right = 0x10
clc
adc dinoMapAddr,X
sta dinoMapAddr,X
dino_addr_nohi lda dinoMapAddr,X ;adr = xpos + ypos*20
sta $03 ;also hold this for frontAddr
lda dinoState,X
and #$0f
dino_f_up cmp #directionUp
bne dino_f_dw
lda $03
sec
sbc #20 ;map width = 20
sta $03
dino_f_dw lda dinoState,X
and #$0f
cmp #directionDown
bne dino_f_lf
lda $03
clc
adc #20 ;map width = 20
sta $03
dino_f_lf lda dinoState,X
and #$0f
cmp #directionLeft
bne dino_f_rg
dec $03
dino_f_rg lda dinoState,X
and #$0f
cmp #directionRight
bne dino_f_end
inc $03
dino_f_end rts
dino_mv_rtn inc dinoAnim,X
lda dinoState,X
and #$f
dino_move_up cmp #directionUp
bne dino_move_dw
dec dinoY,X
dino_move_dw cmp #directionDown
bne dino_move_lf
inc dinoY,X
dino_move_lf cmp #directionLeft
bne dino_move_rg
lda dinoX,X
sec
sbc #1
sta dinoX,X
lda dinoXHi,X
sbc #0
sta dinoXHi,X
lda #spriteDinoLeft
sta dinoSprite,X
jmp dino_move_skip
dino_move_rg cmp #directionRight
bne dino_move_skip
lda #1
clc
adc dinoX,X
sta dinoX,X
lda #0
adc dinoXHi,X
sta dinoXHi,X
lda #spriteDinoRight
sta dinoSprite,X
dino_move_skip rts
dino_map_move lda dinoState,X
and #$0f
cmp #directionLeft
bne dino_map_rg
lda dinoXHi,X
cmp #0
bne dino_map_rg
lda dinoX,X
cmp #0
bne dino_map_rg
lda #$01
sta dinoXHi,X
lda #$40
sta dinoX,X ;set dino to the right
lda #dinoMoveSpeed ;start animation
sta dinoMoveIncrement,X
lda dinoState,X
ora #dinoMapMove
sta dinoState,X ;mark dino as transition
rts
dino_map_rg lda dinoState,X
and #$0f
cmp #directionRight
bne dino_map_up
lda dinoXHi,X
cmp #$01
bne dino_map_up
lda dinoX,X
cmp #$30
bne dino_map_up
lda #$ff
sta dinoXHi,X
lda #$f0
sta dinoX,X ;set dino to the left
lda #dinoMoveSpeed ;start animation
sta dinoMoveIncrement,X
lda dinoState,X
ora #dinoMapMove
sta dinoState,X ;mark dino as transition
dino_map_up lda dinoState,X
and #$0f
cmp #directionUp
bne dino_map_dw
lda dinoY,X
cmp #$00
bne dino_map_dw
lda #$c0
sta dinoY,X ;set dino to the bottom
lda #dinoMoveSpeed ;start animation
sta dinoMoveIncrement,X
lda dinoState,X
ora #dinoMapMove
sta dinoState,X ;mark dino as transition
dino_map_dw lda dinoState,X
and #$0f
cmp #directionDown
bne dino_map_done
lda dinoY,X
cmp #$b0
bne dino_map_done
lda #$f0
sta dinoY,X ;set dino to the bottom
lda #dinoMoveSpeed ;start animation
sta dinoMoveIncrement,X
lda dinoState,X
ora #dinoMapMove
sta dinoState,X ;mark dino as transition
rts
dino_map_done rts
;end compute front position
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;random control routine
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;will return random direction control to $02
random_control lda #1
sta $02
lda randomByte
and #3
cmp #0
beq skip_random
shift_random asl $02
tax
dex
txa
cmp #0
bne shift_random
skip_random rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;joystick routine
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;copies only one direction from the joystick to $02
;;this can be used as direction control for one dino
;logic for joystick control
joy_moved lda #0
sta $02 ;reset state
lda joystick,X
eor #$f ;xor joy state
and #$f
cmp #0
bne joy_was_moved ;joy is moved
rts ;else return
joy_was_moved lda joystick,X
and #directionUp
cmp #$0
bne joy_dw
lda #directionUp
ora $02
sta $02
rts
joy_dw lda joystick,X
and #directionDown
cmp #$0
bne joy_lf
lda #directionDown
ora $02
sta $02
rts
joy_lf lda joystick,X
and #directionLeft
cmp #$0
bne joy_rg
lda #directionLeft
ora $02
sta $02
rts
joy_rg lda joystick,X
and #directionRight
cmp #$0
bne joy_return
lda #directionRight
ora $02
sta $02
joy_return rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;copy routine
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;copies bytes from $fb$fc to $fd$fe
;;number of bytes stored in $02$03
copy lda ($fb),Y
sta ($fd),Y
lda $03 ;is low counter 0?
cmp #0
bne copy_dec_ctr
lda $04 ;is high counter 0?
cmp #0
beq copy_end
dec $04
copy_dec_ctr dec $03 ;decrease counter
iny ;increase pointer
tya
cmp #0
bne copy
inc $fc
inc $fe
dex
txa
cmp #0
bne copy
copy_end rts