-
Notifications
You must be signed in to change notification settings - Fork 5
/
tanks.a
732 lines (588 loc) · 13 KB
/
tanks.a
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
;; -*- asm -*-
;; Border Control Memory 53280
;; Background Control Memory 53281
!source "macros.a"
!source "sprites.a"
!zone BASIC {
* = $1c01
!wo .EndOfText ; Pointer to end of text
!wo $000a ; Line number 10
!pet $9e, "4864", $00 ; SYS4864\0
!by $00, $00 ; End of Text Token
.EndOfText:
}
!addr {
!zone ZeroPage {
}
!zone SystemVarsAndRegisters {
IRQHandlerPtr = $0314
IRQHandlerPtrHi = $0315
CustomChars = $2000
Character_ROM = $d000
SpritePtrStart = $07f8
SpriteColorStart = $d027
SpritePosStart = $d000
SpriteXMSB = $d010
Joystick2 = $dc00
Joystick1 = $dc01
TextStart = $0400
SpriteEn = $d015
BgColor = $D021
BorderColor = $D020
MMU_Control = $ff00
VIC_Raster = $d012
VIC_Interrupt = $d019
VIC_SpSpCollide = $d01e
VIC_Keyboard = $d02f
}
}
!zone Init {
* = $1300
!macro .InitSprite .spriteno, .shapePtr, .facing, .color, .x, .y {
;; Set sprite shape
lda #.shapePtr / 64
sta SpritePtrStart + .spriteno
;; Set sprite color
lda #.color
sta SpriteColorStart + .spriteno
;; Set sprite position -- stride is 2, hence the multiply
ldx #.x
stx ShadowSpritePos + (.spriteno * 2)
ldy #.y
sty ShadowSpritePos + (.spriteno * 2) + 1
;; Set facing direction
lda #.facing
sta SpriteFacing + .spriteno
}
Init:
cld
;; Disable BASIC IRQs
lda #$ff
sta $d8
sta $12fd
;; Disable BASIC, leave I/O, Kernal, and Character ROMs
lda #$00
sta MMU_Control
jsr ClearTextScreen
jsr FadeToBlack
jsr WedgeInterruptRoutine
+.InitSprite 0, TankDown, DirDown, Color_Red, 0, 0
+.InitSprite 1, TankUp, DirUp, Color_LtBlue, 148, 179
!for i, 2, 7 {
+.InitSprite i, Bullet, 0, Color_Black, 0, (i - 2) * 10
}
;; Enable all sprites by default
lda #$ff
sta SpriteEn
jmp GameLoop
}
!zone GameLoop {
GameLoop:
;; Ensure we only process input update during vblank, once per frame
bit FrameStartFlag
bpl GameLoop
;; Clear the frame start flag
lda #$00
sta FrameStartFlag
FrameStart:
jsr HandleCollision
;; Handle input for both 1st and 2nd player
ldx #$00
jsr HandleInput
inx
jsr HandleInput
jsr UpdateBullets
jsr FixupPositions
jsr HandleTimers
jsr MarkDeadSprites
jmp GameLoop
}
!zone MarkDeadSprites {
MarkDeadSprites:
lda #0
ldx #2 ; skip tanks
.loop:
lda SpriteFacing, x
bne .next
;; Value is zero, mark as dead
lda #Blank / 64
sta SpritePtrStart, x
.next:
inx
cpx #$08
bne .loop
rts
}
!zone HandleTimers {
HandleTimers:
lda ShootDelayCounter
beq .nextPlayer
dec ShootDelayCounter
.nextPlayer:
lda ShootDelayCounter + 1
beq .return
dec ShootDelayCounter + 1
.return:
rts
}
!zone HandleCollision {
HandleCollision:
;; Set the border color based upon collision
lda CollisionFlag
tax
bne .borderRed
.borderBlack:
lda #Color_Black
jmp .next
.borderRed:
lda #Color_Red
.next:
sta BorderColor
;; Clear the collision flag
lda #$00
sta CollisionFlag
rts
}
!zone FixupPositions {
FixupPositions:
ldx #$00
ldy #$00
.checkX:
lda ShadowSpritePos, x
cmp #MaxXCoord
bcc .checkY ; If X > MaxXCoord, we're out of bounds
;; Set direction to zero, since we've been stopped
pha
lda #$00
sta SpriteFacing, y
pla
;; Compare to the midpoint of OOB space
cmp #((256 - MaxXCoord) >> 1) + MaxXCoord
bcc .setMaxX ; Set to max X or min X, depending on which is closer
bcs .setMinX
.setMaxX:
lda #MaxXCoord
jmp .storeX
.setMinX:
lda #0
.storeX:
sta ShadowSpritePos, x
.checkY:
lda ShadowSpritePos + 1, x
cmp #MaxYCoord
bcc .next ; If Y > MaxYCoord, we're out of bounds
;; Set direction to zero, since we've been stopped
pha
lda #$00
sta SpriteFacing, y
pla
;; Compare to the midpoint of OOB space
cmp #((256 - MaxYCoord) >> 1) + MaxXCoord
bcc .setMaxY ; Set to max Y or min Y, depending on which is closer
bcs .setMinY
.setMaxY:
lda #MaxYCoord
jmp .storeY
.setMinY:
lda #0
.storeY:
sta ShadowSpritePos + 1, x
.next:
inx
inx
iny
cpx #$0f
bmi .checkX
rts
}
!zone UpdateBullets {
UpdateBullets:
;; Start from sprite 2 -- we increment at the top of the loop
ldx #$01
txa
;; Multiply by 2 for stride
asl
tay
;; X contains the facing offset
;; Y contains the position offset
.next:
inx
iny
iny
cpx #$08
beq .return ; Stop at 8
lda SpriteFacing, x
beq .next ; Skip dead sprites
lsr
bcs .moveUp
lsr
bcs .moveDown
lsr
bcs .moveLeft
lsr
bcs .moveRight
.return:
rts
.moveUp:
lda ShadowSpritePos + 1, y
clc
adc #-BulletSpeed
sta ShadowSpritePos + 1, y
jmp .next
.moveDown:
lda ShadowSpritePos + 1, y
clc
adc #BulletSpeed
sta ShadowSpritePos + 1, y
jmp .next
.moveLeft:
lda ShadowSpritePos, y
clc
adc #-BulletSpeed
sta ShadowSpritePos, y
jmp .next
.moveRight:
lda ShadowSpritePos, y
clc
adc #BulletSpeed
sta ShadowSpritePos, y
jmp .next
}
!zone HandleInput {
HandleInput:
;; ShadowSpritePos is coordinate pairs, so we have to have a stride of two
;; to index into it. Store in X so we can use both player number (Y) and
;; ShadowSpritePos offsets.
txa
tay
asl
tax
;; X contains the coordinate pair offset (stride 2)
;; Y contains the player number offset (stride 1)
;; Update tank position based upon Joystick
sei
lda #$ff
sta Joystick2 ; Force joystick outputs on (shared with keyboard)
sta VIC_Keyboard ; Force VIC-II keyboard outputs on (shared with joystick)
lda Joystick2, y ; Safe to read joystick now (keyboard won't interfere)
cli
;; Shift left so we can skip past bits 5-7 and check fire first
asl
asl
asl
asl
bcc .ckfire
asl
bcc .ckright
asl
bcc .ckleft
asl
bcc .ckdown
asl
bcc .ckup
rts
.ckleft:
!zn {
dec ShadowSpritePos, x ; stride 2
lda #TankLeft / 64
sta SpritePtrStart, y
lda #DirLeft
sta SpriteFacing, y
rts
}
.ckup:
!zn {
dec ShadowSpritePos + 1, x ; stride 2
lda #TankUp / 64
sta SpritePtrStart, y
lda #DirUp
sta SpriteFacing, y
rts
}
.ckright:
!zn {
inc ShadowSpritePos, x ; stride 2
lda #TankRight / 64
sta SpritePtrStart, y
lda #DirRight
sta SpriteFacing, y
rts
}
.ckdown:
!zn {
inc ShadowSpritePos + 1, x ; stride 2
lda #TankDown / 64
sta SpritePtrStart, y
lda #DirDown
sta SpriteFacing, y
rts
}
.ckfire:
!zn {
lda ShootDelayCounter, y
bne + ; If our counter isn't zero, don't spawn
;; Setup the shooting delay
lda #ShootDelay
sta ShootDelayCounter, y
jsr MaybeSpawnBullet
+ rts
}
}
!zone MaybeSpawnBullet {
MaybeSpawnBullet:
;; X and Y are configured thusly:
;; Y: player number
;; Find a free slot in the facing array
;; If zero:
;; Set sprite shape pointer
;; Copy facing from player facing direction
;; Copy sprite pos from player position
;; Else:
;; Return
;; Find a free slot in the facing array
ldx #$02
.loop:
lda SpriteFacing, x
beq .foundFreeSlot
inx
cpx #$08
bne .loop
rts ; Didn't find a free slot
.foundFreeSlot:
;; X contains the free slot number
;; Set the sprite shape
lda #Bullet / 64
sta SpritePtrStart, x
;; Copy facing direction
lda SpriteFacing, y ; from player
sta SpriteFacing, x ; to bullet
;; Multiply slot number and player number by 2 to handle the striding of the
;; position array.
tya
asl
tay
txa
asl
tax
;; Copy the position from the player
lda ShadowSpritePos, y
sta ShadowSpritePos, x
lda ShadowSpritePos + 1, y
sta ShadowSpritePos + 1, x
rts
}
!zone Globals {
;;; System IRQ pointer
SysISRPtr: !by $00
SysISRPtrHi: !by $00
;;; Tracks the direction the sprite last moved in. Zero means it is dead.
SpriteFacing: !fill 8, $00
SpriteFacingEnd:
;;; Shadow X/Y positions for sprites -- 255 max positions horizontally.
ShadowSpritePos: !fill 16, $00
ShadowSpritePosEnd:
FrameStartFlag: !by $00
FrameStartRasterLine = 250
CollisionFlag: !by $00
ShootDelayCounter: !by $00, $00
ShootDelay = 15 ; 30 frames of delay between firing
;;; Direction flags
DirUp = %.......1
DirDown = %......1.
DirLeft = %.....1..
DirRight = %....1...
;;; Constant; max bullet speed is 4px / frame
BulletSpeed = 4
;;; Max visible coordinates
MaxXCoord = 148
MaxYCoord = 179
VIC_Int_Scanline = %.......#
VIC_Int_SpFgCollide = %......#.
VIC_Int_SpSpCollide = %.....#..
}
!zone InterruptCallback {
InterruptCallback:
;; Ack the interrupts from the VIC
lda VIC_Interrupt
jsr MaybeTriggerFrame
jsr CheckCollision
;; Handle other functions not tightly coupled with the IRQs
jsr UpdateSpritePositions
jsr CycleBulletColors
;; Give control back to the system ISR
jmp (SysISRPtr)
}
!zone CheckCollision {
CheckCollision:
;; Save A, since we'll use it in other triggers
pha
;; Check if we were triggered because of a sprite collision
and #VIC_Int_SpSpCollide
beq .out
;; Clear out the collision register
lda VIC_SpSpCollide
;; Set the collision flag
sta CollisionFlag
.out:
pla
rts
}
!zone MaybeTriggerFrame {
MaybeTriggerFrame:
;; Save A, since we'll use it in other triggers
pha
;; Check if we were triggered by the raster trigger
and #VIC_Int_Scanline
beq .out
;; Set the frame start flag
lda #$ff
sta FrameStartFlag
;; Re-set the raster trigger
lda #FrameStartRasterLine
sta VIC_Raster
.out:
pla
rts
}
!zone CycleBulletColors {
CycleBulletColors:
;; Eight sprites
ldx #$08
.loop:
inc SpriteColorStart, x
dex
;; Skip the first two sprites
cpx #$01
bne .loop
rts
}
;;; Updates the positions of the sprites using the shadow sprite data.
;;;
;;; Uses Shallan's technique of using the MSB of the X coordinate byte as the
;;; MSB for the VIC's Sprite MSB register. The visible playfield ends up being
;;; 148x179. 0,0 is correctly placed in the upper right of the playfield, and
;;; 148x179 is the last coordinate where all pixels of a sprite are visible.
;;;
;;; max-x: 148, max-y=179
;;;
!zone UpdateSpritePositions {
UpdateSpritePositions:
;; ;; Prep stack for MSB bits
lda #$00
pha
;; Start from 255 (-1) since we increment first in the loop
ldx #$ff
.updateNextSprite:
;; Update X first
inx
lda ShadowSpritePos, x
clc
adc #24 / 2 ; playfield starts at X=24
asl ; shift MSB to carry
sta SpritePosStart, x
;; Store MSB in the stack for later
pla
ror
pha
;; Update Y next
inx
lda ShadowSpritePos, x
clc
adc #50 ; playfield starts at Y=50
sta SpritePosStart, x
;; On to the next pair
cpx #$0f ; 16 bytes to update
bne .updateNextSprite
;; Update the MSB
pla
sta SpriteXMSB
rts
}
!zone WedgeInterruptRoutine {
WedgeInterruptRoutine:
sei
;; Save system ISR
lda IRQHandlerPtr
sta SysISRPtr
lda IRQHandlerPtrHi
sta SysISRPtrHi
;; Install our ISR
lda #<InterruptCallback
sta IRQHandlerPtr
lda #>InterruptCallback
sta IRQHandlerPtrHi
;; Set the raster trigger for triggering each frame
lda #FrameStartRasterLine
sta VIC_Raster
cli
rts
}
!zone FadeToBlack {
.border: !by 15, 12, 11, 0
.backgd: !by 13, 15, 11, 0
FadeToBlack:
ldx #$00
.loop:
lda .border, x
sta BgColor
lda .backgd, x
sta BorderColor
beq +
ldy #100
jsr BusyDelay
inx
jmp .loop
+
rts
}
!zone ClearTextScreen {
ClearTextScreen:
ldx #$00
!macro .SetToMaxChar .address {
lda .address, x
cmp #$20
beq +
lda #127
sta .address, x
+
}
.loop:
+.SetToMaxChar TextStart
+.SetToMaxChar TextStart + $100
+.SetToMaxChar TextStart + $200
+.SetToMaxChar TextStart + $300
inx
bne .loop
!macro .DecToZero .address {
lda .address, x
cmp #$20
beq +
dec .address, x
+
}
ldy #128
.dec_loop:
+.DecToZero TextStart
+.DecToZero TextStart + $100
+.DecToZero TextStart + $200
+.DecToZero TextStart + $300
inx
bne .dec_loop
dey
bne .dec_loop
rts
}
;;; BusyDelay
;;; Y - number of times to wait
!zone BusyDelay {
BusyDelay:
+SaveRegisters
ldx #$00
.loop:
inx
bne .loop
dey
bne .loop
+RestoreRegisters
rts
}