-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpace Invaders.asm
More file actions
2011 lines (1790 loc) · 46.6 KB
/
Space Invaders.asm
File metadata and controls
2011 lines (1790 loc) · 46.6 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
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
; Used DASM to compile
; see https://github.com/Julio-Rats/dasm
;
; Command to compile:
; dasm SpaceInvaders.asm -oSpaceInvaders.bin -f3
;
PROCESSOR 6502
INCLUDE "vcs.h"
;===================================================================
; TV SYSTEM DEFINITION
;===================================================================
SYSTEM_TV = "NTSC" ; (ONLY NTSC FOR NOW)
;===================================================================
; NTSC 60 FPS
IF SYSTEM_TV == "NTSC"
; Number of Hot Scanlines
KERNEL_SCANLINE = 192
; Timers
VBLANK_TIMER = 51
FRAME_TIMER = 249
OVERSCAN_TIMER = 54
; Color Hex NTSC
BACK_COLOR = $00
PF_COLOR = $06
FLOOR_COLOR = $E2
PLAYER_COLOR = $C4
LEFT_SCORE_COLOR = $C4
RIGHT_SCORE_COLOR = $F6
LEFT_LIMIT_COLOR = $F6
RIGHT_LIMIT_COLOR = $C4
ENEMY_COLOR = $16
DEFENSE_COLOR = $34
;===================================================================
; OTHERS
ELSE
ECHO "TV SYSTEM NOT SUPPORTED!"
ERR
ENDIF
;===================================================================
;===================================================================
; Global Constants
;===================================================================
GROUND_SCAN = KERNEL_SCANLINE-8
; Size and Lengths
PLAYER_LEN = 10
ALIEN_LEN = 10
DEFENSE_LEN = 9
ALIENS_LINES = 6
ALIENS_INTER = 8
SPEED_TB_LEN = Dstrc_tb-Speed_tb
; Start Positions
PLAYER_START = 75
ALIENS_START = 22
DEFENSE_START = 42
; Scanline Position
PLAYER_SCAN = [GROUND_SCAN-PLAYER_LEN-1]
ENEMY_DIST = [PLAYER_SCAN-152]
DEF_SCAN = [PLAYER_SCAN-27]
; Speeds
PLAYER_SPEED = 1
ALIENS_X_SPEED = 1
ALIENS_Y_SPEED = ALIEN_LEN
ALS_FRAME_MOVE = %00100000 ; Min -> %00100000
MSL_SPEED = 4
; Limits
PLAYER_L_LIMIT = 33
PLAYER_R_LIMIT = 123
ALIENS_L_LIMIT = 22
ALIENS_DR_LIMIT = 49
ALIENS_DY_LIMIT = [PLAYER_SCAN-(ALIEN_LEN+ALIENS_INTER)*ALIENS_LINES+ALIENS_INTER-4]
; Random Number Utils
TAPS = $B8
SEED = 13
; Probabilistics Used
PROB_DEFENSE_DMG = [255 * 60/100]
;===================================================================
;===================================================================
; VARIABLES RAM ($0080-$00FF)(128B RAM)
SEG.U VARIABLES
ORG $0080
RANDOM_NUMBER ds 1 ; Random Number :)
FRAME_COUNT ds 1 ; Frame Count
SCANLINE_COUNT ds 1 ; Temp for Ajust Number of Scanline Passed
PLAYER_POS ds 1 ; Player X-pos
SCORE_VALUE ds 4 ; Left and Rigth Score Value (1 Byte for 2 Digits, 2 Bytes for each side)
SCORE_INDEX ds 8 ; Index to Indirect PTR, Relative to Number0 + Score_value*5 (one for each digit)
MSL_POS ds 3 ; Player, Alien1, Alien2 X-pos
MSL_SCAN ds 3 ; Player, Alien1, Alien2 Y-pos
MSL_STAT ds 1 ; Missile Status (See Below)
; MSL_STAT DECODER
; Bit Status
;
; 7 Player Missile
; 6 Alien 1º Missile
; 5 Alien 2º Missile
;
; 0-1 Current Missile Draw bit 0: Player MSL -- bit 1: Aliens MSL
MSL_CURRSCAN ds 1 ; Missile Current Scanline Delay
MSL_RLTVSCAN ds 1 ; Relative Distance to Next Missile - Only used in Alien Missile Frame'
MSL_NEXTPOS ds 1 ; Next Missile Pos - Only used in Alien Missile Frame
ALIENS_POS ds 1 ; Alien X-pos (First Line and Collumn)
ALIENS_SCAN ds 1 ; Alien Y-pos
ALIENS_SPEED ds 1 ; Frames Need to Move Aliens
ALIENS_MASK ds 1 ; Control of direction and Orientation (See Below)
; ALIENS_MASK DECODER
; Bit Status
;
; 7 Direction 0: Right 1: left
; 6 Orientation 0: Normal 1: Mirrored
;
ALIENS_STAT ds 6 ; Alien Status (bit 0 to 5, Alive Alien := 1)
COLLUMNS_ALIVE ds 1 ; Collumns of Living Aliens in the Previous Frame
ALIENS_NUM ds 1 ; Used to count how many lines are left to draw
ALIENS_COUNT ds 1 ; Number of Current Lines of Aliens
ALIENS_CURR ds 2 ; Get Current PTR GRP Alien
ALIENS_DELAY ds 2 ; PTR for Time ajust for X-pos Aliens
ALIENS_R_LIMIT ds 1 ; Max Right Position for Aliens Sprite
ALIENS_Y_LIMIT ds 1 ; Max vertical Position for Aliens Sprite
DEFENSE_POS ds 1 ; Base Defense X-Pos
DEFENSE_SHAPE ds 27 ; Base Defense Shape
POINTER_GRP ds 12 ; Dedicated memory for indirect pointers for building graphs (shapes)
; --- Variable no longer used ---
; PLAYER_GRP ds 2 ; Player PTR GRP
; SCORE_GRP ds 2 ; PTR to $FF04 --> decremented for each line of the score (counter and pointer :p)
; ALIENS_GRP ds 12 ; Aliens PTR GRP
; --- End ---
TEMP_SCORE_ATT ds 1 ; Used to Rotations ATT Aliens and Socre, Generic Temporary
;=============================================================================================
;=============================================================================================
; CODE SEGMENT
SEG CODE
ORG $F000 ; Start of "Cart Area" (See Atari Memory Map)
;=============================================================================================
; CPU ENTRYPOINT
;=============================================================================================
BootGame:
SEI
CLD
; Clear Memory
LDX #0
TXA
TXS
ClearMemory:
DEX
PHA
BNE ClearMemory
PHA
; Get Seed for random numbers generation
LDA INTIM
BNE NoNULLSeed ; Check Null Seed (zero)
LDA #SEED ; Default Seed with INTIM returns NULL (zero)
NoNULLSeed:
STA RANDOM_NUMBER
JSR NewRound
; Start VBlank period
LDA #%10000010 ; Starting VBlank
STA VBLANK
;=============================================================================================
; NEW FRAME CYCLE
;=============================================================================================
StartFrame:
; Sync with TV
LDA #%00001110 ; Vertical sync is signaled by VSYNC's bit 1...
VsyncWait:
STA WSYNC ; (WSYNC write => wait for end of scanline)
STA VSYNC
LSR
BNE VsyncWait
; Set time for vertical blank period
LDA #VBLANK_TIMER ; Timing VBlank Scanlines
STA TIM64T
; Decrement Counter Frame
DEC FRAME_COUNT
;=============================================================================================
;=============================================================================================
; VBLANK CODE AREA
;=============================================================================================
;=============================================================================================
; Reset Back Color
LDA #BACK_COLOR
STA COLUBK
LDA #PF_COLOR
STA COLUPF
; Prepare Color for Score
LDA #LEFT_SCORE_COLOR
STA COLUP0
LDA #RIGHT_SCORE_COLOR
STA COLUP1
; Playfield Score Mode
LDA #$02
STA CTRLPF
; Vertical Delay for Aliens
LDA #1
STA VDELP0
STA VDELP1
; Tree Copies - Medium Dist (Aliens Used)
LDA #$06
STA NUSIZ0
STA NUSIZ1
; Preparing pointer and counter for Score
LDA #$04
STA POINTER_GRP
; Set high address Score GRP PTR
LDA #>Number0
STA POINTER_GRP+1
; Calculates relative indexes for Scores
JSR SetIndexScore
; Speed Aliens
JSR ChangeAliensSpeed
; Move Aliens
JSR LogicAliens
JSR MoveSpritesAliens
; Set X-Pos of Aliens
LDA ALIENS_POS
LDX #0
JSR SetHorizPos
LDA ALIENS_POS
CLC
ADC #16
INX
JSR SetHorizPos
; Move Missel
JSR LogicMissile
;=============================================================================================
; INPUT CONTROL PROCESSING AREA
;=============================================================================================
; Input Code
; Check End Game
LDA ALIENS_SPEED
BEQ NoMove
; Check Shot in Progress
LDA MSL_STAT
BMI GetMove
; Buttom Press
BIT INPT4
BMI GetMove
LDA PLAYER_POS
CLC
ADC #$04
STA MSL_POS
LDX #$04
JSR SetHorizPos
LDA #[PLAYER_SCAN-20]
STA MSL_SCAN
LDA #%10000000
ORA MSL_STAT
STA MSL_STAT
GetMove:
; Get Directional Controls
BIT SWCHA
BPL RightMove
BVC LeftMove
JMP NoMove
RightMove:
; Check Right Move Available
LDA PLAYER_POS
CMP #[PLAYER_R_LIMIT-5]
BCS NoMove
ADC #PLAYER_SPEED
STA PLAYER_POS
JMP NoMove
LeftMove:
; Check Left Move Available
LDA PLAYER_POS
CMP #[PLAYER_L_LIMIT+1]
BCC NoMove
SBC #PLAYER_SPEED
STA PLAYER_POS
NoMove:
; Hard Reset
LDA SWCHB
AND #1
BNE NoPressReset
JSR ResetGame
NoPressReset:
;=============================================================================================
; WAIT FOR VBLANK FINISH
;=============================================================================================
; Wait Rest of Existing VBlank (Async Clock)
WaitVblankEnd:
LDA INTIM
BNE WaitVblankEnd
; Register Y for Count Hot Scanlines
TAY ; A:=0 -> Y
STA SCANLINE_COUNT
STA WSYNC
; Apply Moves in Buffers
STA HMOVE
; Clear Collisions (New Frame)
STA CXCLR
; Start Timer Counter
LDA #FRAME_TIMER
STA TIM64T
; VBlank and Disable Latches Inputs
LDA #%10000000
; Out VBlank (Magic Starts Here !!)
STA VBLANK ; Stop VBlank
;=============================================================================================
;=============================================================================================
;=============================================================================================
; KERNEL
;=============================================================================================
;=============================================================================================
;=============================================================================================
;
; PRINT SCREEN MOMENT (HOT SCANLINES)
;
; Start Visible Scanlines
; Kernel Code
; --- Wait and Drawn Score Points ---
LDX #8
LoopSyncScore:
DEX
BNE LoopSyncScore
NOP
NOP
LoopDrawScore:
; Draw Score
; PF0 | PF1 | PF2 PF0 | PF1 | PF2
; X | _ _ | Y W | _ _ | Z
;-------------------------------
LDY SCORE_INDEX+4
LDA (POINTER_GRP),Y ; X
STA PF0
LDA #0
STA PF1
LDY SCORE_INDEX+1
LDA (POINTER_GRP),Y ; Y
LSR
LSR
LSR
LSR
STA PF2
; --> Half Screen Input Data
LDY SCORE_INDEX+6
LDA (POINTER_GRP),Y ; W
STA PF0
LDY SCORE_INDEX+3
LDA (POINTER_GRP),Y ; Z
LSR
LSR
LSR
LSR
STA PF2
; Draw Score
; PF0 | PF1 | PF2 PF0 | PF1 | PF2
; | X Y | | W Z |
;-------------------------------
LDY SCORE_INDEX
LDA (POINTER_GRP),Y ; Y
AND #$0F
STA TEMP_SCORE_ATT
LDY SCORE_INDEX+5
LDA (POINTER_GRP),Y ; X
ASL
ASL
ASL
ASL
ORA TEMP_SCORE_ATT
STA PF1
LDA #0
STA PF0
STA PF2
; --> Half Screen Input Data
LDY SCORE_INDEX+2
LDA (POINTER_GRP),Y ; Z
AND #$0F
STA TEMP_SCORE_ATT
LDY SCORE_INDEX+7
LDA (POINTER_GRP),Y ; W
ASL
ASL
ASL
ASL
; Forced absolute mode to spend one more cycle, ensuring loop synchronization with the scanline
; ORA.W TEMP_SCORE_ATT
ORA TEMP_SCORE_ATT
STA PF1
DEC POINTER_GRP
; If the BPL changes pages, the forced absolute mode in ORA must be removed.
BPL LoopDrawScore
; All code inside the loop above,
; plus the branch cycles must have exactly 76 machine cycles (228 color clocks)
;-------------------------------
; Disable PF and Remove Score Mode
LDA #0
STA PF1
STA CTRLPF
; Clear Buffers of Moves, made in VBlank
STA HMCLR
; Set Color to Aliens
LDA #ENEMY_COLOR
STA COLUP0
STA COLUP1
DEC MSL_CURRSCAN
DEC MSL_CURRSCAN
; Adjusts pointers used by score
; Set Based Aliens GRP
BIT ALIENS_MASK
BVS InverseSprite
LDA #>SpriteEnemieLine1
LDY #<SpriteEnemieLine1
JMP LoadTempSprite
InverseSprite:
LDA #>SpriteEnemieLine1_I
LDY #<SpriteEnemieLine1_I
LoadTempSprite:
STA ALIENS_CURR+1
STY ALIENS_CURR
; Set First Line of Aliens
LDA ALIENS_STAT+5
STA TEMP_SCORE_ATT
LDX #11
LoopSetAliensL1Grp:
ROR TEMP_SCORE_ATT
BCS AlienL1Alive
LDA #>SpritEmpty
LDY #<SpritEmpty
JMP SetAlienL1
AlienL1Alive:
LDA ALIENS_CURR+1
LDY ALIENS_CURR
SetAlienL1:
STA POINTER_GRP,X
DEX
STY POINTER_GRP,X
DEX
BPL LoopSetAliensL1Grp
; Scanlines used in Score
LDY #14
; --- Wait for Draw Aliens ---
WaitEnemies:
INY
JSR TryDrawMSL
STA WSYNC
CPY ALIENS_SCAN
BCC WaitEnemies
;------------------- Main loop for drawing Aliens -------------------
LDX #11
LoopRelativeDelayAliens:
DEX
BNE LoopRelativeDelayAliens
STY SCANLINE_COUNT
LDY #[ALIEN_LEN-1]
JMP (ALIENS_DELAY) ; Indirect Jump
JmpDelay: ; Aliens Position / Relative Byte Code Jump
CMP $80 ; 121-129 -22
CMP $80 ; 112-120 -20
CMP $80 ; 103-111 -18
CMP $80 ; 94-102 -16
CMP $80 ; 85-93 -14
CMP $80 ; 76-84 -12
CMP $80 ; 67-75 -10
CMP $80 ; 58-66 -8
CMP $80 ; 49-57 -6
CMP $80 ; 40-48 -4
CMP $80 ; 31-39 -2
DrawEnemies: ; 22-30 0
CMP $80
; Odd Scanline
LDA (POINTER_GRP+10),Y ; Alien 6
TAX
LDA (POINTER_GRP),Y ; Alien 1
STA GRP0 ; GRP0A == Alien 1
LDA (POINTER_GRP+2),Y ; Alien 2
STA GRP1 ; GRP0A/GRP0B == Alien 1 ; GRP1A == Alien 2
LDA (POINTER_GRP+4),Y ; Alien 3
STA GRP0 ; GRP0A == Alien 3/GRP0B == Alien 1 ; GRP1A/GPR1B == Alien 2
LDA (POINTER_GRP+6),Y ; Alien 4
STA GRP1 ; GRP0A/GRP0B == Alien 3 ; GRP1A == Alien 4/GPR1B == Alien 2
LDA (POINTER_GRP+8),Y ; Alien 5
STA GRP0 ; GRP0A == Alien 5/GRP0B == Alien 3 ; GRP1A/GPR1B == Alien 4
STX GRP1 ; GRP0A/GRP0B == Alien 5 ; GRP1A == Alien 6/GPR1B == Alien 4
STX GRP0 ; GRP0A == Alien 6/GRP0B == Alien 5 ; GRP1A/GPR1B == Alien 6
; Delay 11 Cycles with no cross page, else 10 cycles
PHA
PLA
NOP
NOP
DEC MSL_CURRSCAN
DEC MSL_CURRSCAN
; Even Scanline
DEY
LDA (POINTER_GRP+10),Y ; Alien 6
TAX
LDA (POINTER_GRP),Y ; Alien 1
STA GRP0 ; GRP0A == Alien 1
LDA (POINTER_GRP+2),Y ; Alien 2
STA GRP1 ; GRP0A/GRP0B == Alien 1 ; GRP1A == Alien 2
LDA (POINTER_GRP+4),Y ; Alien 3
STA GRP0 ; GRP0A == Alien 3/GRP0B == Alien 1 ; GRP1A/GPR1B == Alien 2
LDA (POINTER_GRP+6),Y ; Alien 4
STA GRP1 ; GRP0A/GRP0B == Alien 3 ; GRP1A == Alien 4/GPR1B == Alien 2
LDA (POINTER_GRP+8),Y ; Alien 5
STA GRP0 ; GRP0A == Alien 5/GRP0B == Alien 3 ; GRP1A/GPR1B == Alien 4
STX GRP1 ; GRP0A/GRP0B == Alien 5 ; GRP1A == Alien 6/GPR1B == Alien 4
STX GRP0 ; GRP0A == Alien 6/GRP0B == Alien 5 ; GRP1A/GPR1B == Alien 6
; Check the Missile draw
LDA MSL_CURRSCAN
CMP #8
BCC TryDrawMSL1
LDA #0
BCS TryDrawMSL2
TryDrawMSL1:
NOP
LDA #2
TryDrawMSL2:
STA ENABL
DEY
BPL DrawEnemies
; Stop Drawn Aliens
LDA #0
STA GRP0
STA GRP1
STA GRP0
; Check End of Aliens Lines
DEC ALIENS_NUM
BEQ ExitAliens
DEC ALIENS_COUNT
LDX ALIENS_COUNT
LDA ALIENS_STAT-1,X
STA TEMP_SCORE_ATT
; Set Next Line Sprites (Pointer Arithmetic)
LDA ALIENS_CURR
CLC
ADC #ALIEN_LEN
STA ALIENS_CURR
DEC MSL_CURRSCAN
; Set Aliens Sprite
LDX #11
LoopSetAliensGrp:
ROR TEMP_SCORE_ATT
BCS AlienAlive ; 2/3
LDA #>SpritEmpty ; 2 (2)
LDY #<SpritEmpty ; 2 (4)
JMP SetAlien ; 3 (6)
AlienAlive: ; 3
LDA ALIENS_CURR+1 ; 3 (3)
LDY ALIENS_CURR ; 3 (6)
SetAlien: ; 9
STA POINTER_GRP,X
DEX
STY POINTER_GRP,X
DEX
BPL LoopSetAliensGrp
; Restore Y-Count Scanline
LDA #[ALIEN_LEN+4]
CLC
ADC SCANLINE_COUNT
TAY
DEC MSL_CURRSCAN
DEC MSL_CURRSCAN
LDX #[ALIENS_INTER-4]
; Adjustment in Scanline Count (too much delay consumes a Scanline naturally)
LDA ALIENS_POS
CMP #$55
BCC LoopInterAliens
DEX ; Spend one scanline less
INY ; Increments the Scanline counter
LoopInterAliens: ; Consume Unused Scanlines Between Aliens Lines
JSR TryDrawMSL
INY
DEX
STA WSYNC
BNE LoopInterAliens
DEC MSL_CURRSCAN
; Prepare for Another Loop
LDX #10 ; Delay in LoopRelativeDelayAliens
JMP LoopRelativeDelayAliens
;----------------------------- Exit Aliens -----------------------------
ExitAliens:
LDA SCANLINE_COUNT
CLC
ADC #ALIEN_LEN
STA SCANLINE_COUNT
TAY
; Jump if Aliens Land (No Print Player)
CPY #[PLAYER_SCAN-4]
BCC PlayerAlive
; Player Is Dead
LDA #$10
STA NUSIZ0
STA NUSIZ1
STA WSYNC
; Delay to print the shot limits in the right and left position
LDX #2
PlayerDeadAjustScan:
DEX
BNE PlayerDeadAjustScan
NOP
JMP PlayerDeadJmp
; Play Alive
PlayerAlive:
LDA #0
STA VDELP0
STA VDELP1
; Jump if Aliens "Destroy" Base Defense
CPY #[DEF_SCAN-3]
; Defense Is Destroyed
BMI DefenseNotIsGone
; JSR TryDrawMSL
; 8 cycles Delay / 24 Color Clocks
LDA ($80,X) ; Delay
NOP ; Delay
INY
NoScanConsume:
JMP DefenseIsGone
DefenseNotIsGone:
; 8 cycles Delay / 24 Color Clocks
LDA ($80,X) ; Delay
NOP ; Delay
; Prepare GRP0 for 3 Copies Medium for Base Defense Drawn
LDA #6
STA NUSIZ0
; Set Color of Base Defense
LDA #DEFENSE_COLOR
STA COLUP0
; Set Position of Base Defense
LDA DEFENSE_POS
LDX #0
INY
JSR SetHorizPos
INY
STA WSYNC
STA HMOVE
; Wait for time to draw the 3 defensive barriers
WaitDefense:
INY
JSR TryDrawMSL
STA WSYNC
STA HMCLR
CPY #[DEF_SCAN-1]
BCS WaitDefense2
INY
JSR TryDrawOrChangeMSL
STA WSYNC
STA HMOVE
CPY #[DEF_SCAN-1]
BCC WaitDefense
WaitDefense2:
; Saving the current Scanline count
INY
STY SCANLINE_COUNT
; Delay for GRP exchange at correct time
LDY #13
LoopRelativeDelayDefense:
DEY
BNE LoopRelativeDelayDefense
;------------------- Main loop for drawing Defense -------------------
; Time-synchronized loop to draw defenses
LDX #0
LoopDrawDefense:
DEC MSL_CURRSCAN
LDA MSL_CURRSCAN
CMP #8
BCC TryDrawMSL3
LDA #0
BCS TryDrawMSL4
TryDrawMSL3:
NOP
LDA #2
TryDrawMSL4:
STA ENABL
; Draw Defenses
LDA DEFENSE_SHAPE,X
STA GRP0
; Delay 4 Cycles
NOP
NOP
LDA [DEFENSE_SHAPE+DEFENSE_LEN],X
STA GRP0
; Delay 4 Cycles
NOP
NOP
LDA [DEFENSE_SHAPE+2*DEFENSE_LEN],X
STA GRP0
LDY #5
LoopDelayDefense:
DEY
BNE LoopDelayDefense
DEC MSL_CURRSCAN
LDA MSL_CURRSCAN
CMP #8
BCC TryDrawMSL5
LDA #0
BCS TryDrawMSL6
TryDrawMSL5:
NOP
LDA #2
TryDrawMSL6:
STA ENABL
LDA DEFENSE_SHAPE,X
STA GRP0
; Delay 4 Cycles
NOP
NOP
LDA DEFENSE_SHAPE+DEFENSE_LEN,X
STA GRP0
; Delay 4 Cycles
NOP
NOP
LDA [DEFENSE_SHAPE+2*DEFENSE_LEN],X
STA GRP0
; Delay 10 Cycles
ROL $80
ROR $80
INC SCANLINE_COUNT
INC SCANLINE_COUNT
INX
CPX #9
BNE LoopDrawDefense
;------------------- Exit Defense -------------------
LDA #0
STA GRP0
LDY SCANLINE_COUNT
INY
; After Defense or Aliens (def destroyed)
DefenseIsGone:
; Player GRP set
LDA #<SpritePlayer
STA POINTER_GRP
LDA #>SpritePlayer
STA POINTER_GRP+1
; Set Player Pos
LDA PLAYER_POS
LDX #0
JSR SetHorizPos
WaitPlayer:
JSR TryDrawOrChangeMSL
INY
STA WSYNC
STA HMOVE
CPY #[PLAYER_SCAN-3]
BCS WaitPlayer2
JSR TryDrawMSL
INY
STA WSYNC
STA HMCLR
CPY #[PLAYER_SCAN-3]
BCC WaitPlayer
WaitPlayer2:
INY
STA WSYNC
LDA #0
STA ENABL
STA HMOVE
INY
STA WSYNC
STA HMCLR
; Start Draw Player
; Missele/Player Sets
LDA #$10
STA NUSIZ0
STA NUSIZ1
; Set Player Color
LDA #PLAYER_COLOR
STA COLUP0
; Save in Y Value in X, Y need for Indirect Address
TYA
TAX
;------------------- Main loop for drawing Player -------------------
LDY #[PLAYER_LEN-1]
DrawPlayer:
INX
STA WSYNC ; --> Sync Wsync
LDA (POINTER_GRP),Y
STA GRP0
DEY
BPL DrawPlayer
;------------------- Exit Player -------------------
TXA
TAY
PlayerDeadJmp:
; Missele 0 Color
LDA #RIGHT_LIMIT_COLOR
STA COLUP0
; Missele 1 Color
LDA #LEFT_LIMIT_COLOR
STA COLUP1
; Delay M0 Limits Position
LDX #3 ; Delay M1 Limits Position
STA RESM0 ; --> Need Sync Pos 29
; Fine Limits Position
LDA #$C0
STA HMM0 ; Pos 33
LDA #$90
STA HMM1 ; Pos 123
DelayM1:
DEX
BNE DelayM1
INY
STA RESM1 ; --> Need Sync Pos 116
; Apply Moves
STA WSYNC
STA HMOVE
; Ground Color
LDA #FLOOR_COLOR
STA COLUBK
; End Player
LDA #0
STA GRP0
STA GRP1
INY
STA WSYNC
; Enable Missiles
LDA #2
STA ENAM0
STA ENAM1
; Limits Height
LDX #5
LimLen:
INY
STA WSYNC
DEX
BNE LimLen
; End Limits
LDA #0
STA ENABL
STA ENAM0
STA ENAM1
STA HMCLR
;
;=============================================================================================
;=============================================================================================
; OVERSCAN CODE AREA
;=============================================================================================
;=============================================================================================
; Set Direction and Position of Aliens Sprites
JSR LogicAliens
; Overscan Code
JSR RandNumber ; Temp, only tests
;=============================================================================================
; COLLISION PROCESSING AREA
;=============================================================================================
; Collision Code
; P0 and Ball Collided. There are 3 cases, Defense, Alien, Player
BIT CXP0FB
BVS Collision
; P1 and Ball Collided, only aliens use P1
BIT CXP1FB
BVS Collision
JMP NoCollision
Collision:
JSR BallCollision
NoCollision:
; Wait hot Scanlines Over
ScanlineEnd:
LDA INTIM
BNE ScanlineEnd
;=============================================================================================
;=============================================================================================
;=============================================================================================
; END OF KERNEL
;=============================================================================================
;=============================================================================================
;=============================================================================================
Overscan:
STA WSYNC
LDA #%11000010 ; "Turn Off Cathodic Ray"
; Starts VBlank and Latches Inputs
STA VBLANK
LDA #OVERSCAN_TIMER ; Timing OverScanlines
STA TIM8T
;=============================================================================================
; END OVERSCAN
;=============================================================================================
; Wait Rest of Existing OverScan (Async Clock)
WaitOverscanEnd: ; Timing OverScanlines
LDA INTIM
BNE WaitOverscanEnd
JMP StartFrame ; Back to Start
;=============================================================================================
;=============================================================================================
;=============================================================================================
; FUNCTION DECLARATION
;=============================================================================================
;=============================================================================================
;=============================================================================================
; Functions Codes