-
Notifications
You must be signed in to change notification settings - Fork 7
/
falling.asm
2990 lines (2510 loc) · 89.8 KB
/
falling.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
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
.inesprg 2 ; 2x 16KB PRG code banks (banks 0, 1, 2, 3)
.ineschr 1 ; 1x 8KB CHR data banks (bank 4)
.inesmap 0 ; mapper (0 = NROM), no bank swapping
.inesmir 0 ; 0 = horizontal background mirroring (for vertical scrolling)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Game state flowchart:
;
; v---------\
; TITLE -> PLAYING -> PAUSED
; -> GAME OVER
; ^------------------/
;; RAM Layout:
; Page 0 ($0000-$00FF) - Main game variables
; Page 1 ($0100-$01FF) - Stack
; Page 2 ($0200-$02FF) - Sprite data
; Page 3 ($0300-$03FF) - Sound engine variables and data
;; PRG-ROM Layout:
; $8000-$9FFF (bank 0): Sound engine
; $A000-$BFFF (bank 1): Unused
; $C000-$DFFF (bank 2): Main program code
; $E000-$FFFA (bank 3): Palettes, sprite data, background name/attr tables
; $FFFA-$FFFF (bank 3): Interrupt vectors
;; TODO:
; - Add distinct features to Day/Sunset/Night modes: Day=normal, Sunset=slow and chill, Night=hardcore(?)
; - Test on standard emulators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DECLARE VARIABLES
; Start variables at ram location $0000.
; This is within zero-page ($0000-$00FF) and so should be used only for heavy-use variables.
.rsset $0000
; .rs 1 means reserve one byte of space
gamestate .rs 1 ; gamestate (0=title, 1=playing, 2=pause, 3=gameover)
rng .rs 2 ; 16-bit seed for 8-bit random number (in rng+0)
clock100 .rs 1 ; frame counter: rolls over every 100 frames
clock256 .rs 1 ; frame counter: rolls over every 256 frames
clock60 .rs 1 ; frame counter: rolls over every 60 frames (one second)
clockSecs .rs 4 ; second counter: increments every 60 frames to track seconds (overflows after 136.2 years)
buttons .rs 1 ; player 1 gamepad buttons, one bit per button
gamemode .rs 1 ; game mode (0=Day, 1=Sunset, 2=Night)
difficulty .rs 1 ; difficulty (0=Easy, 1=Medium, 2=Hard, 3=Insane)
difficultyflag .rs 1 ; flag to indicate that difficulty should no longer change (1=max difficulty reached)
ppu_cr1 .rs 1 ; state of PPU Control Register 1 ($2000, PPUCTRL)
ppu_cr2 .rs 1 ; state of PPU Control Register 2 ($2001, PPUMASK)
ppu_scroll .rs 1 ; current background scroll position (should only go to 240)
ppu_nametable .rs 1 ; current nametable priority (0 or 2) for swapping while scrolling
playerx .rs 1 ; player char, horizontal position of midpoint between feet
playery .rs 1 ; player char, vertical position of midpoint between feet
playerfacing .rs 1 ; direction player is facing (0=left, 1=right)
playerarm_animframe .rs 1 ; track arm animation frame (0-5)
playerbobstate .rs 1 ; tracks state of bobbing animation
playerlives .rs 1 ; remaining number of player lives
playerinvnc .rs 1 ; invincibility counter. on collision, this is set and must count down to 0 before another collision.
playerscore .rs 2 ; player score (2 bytes to store up to a score of 65535)
highscore .rs 2 ; high score (updated on game over)
obst1_update .rs 1 ; flags for obstacle updates
obst2_update .rs 1
obst3_update .rs 1
obst1_release .rs 1 ; timers controlling release of obstacles
obst2_release .rs 1
obst3_release .rs 1
obst1_x .rs 1 ; x-pos of obstacles. obstacle sprites will be placed relative to this.
obst2_x .rs 1
obst3_x .rs 1
obst1_y .rs 1 ; y-pos of obstacles. tracked for collision purposes.
obst2_y .rs 1
obst3_y .rs 1
pkupcollisions .rs 1 ; pickup collision flags (one per bit)
pkuplife_flag .rs 1 ; flag to indicate whether a life pickup has been released
pkuplife_x .rs 1 ; x-pos of life pickup
pkuplife_y .rs 1 ; y-pos of life pickup
pkupcoin_flag .rs 1 ; flag to indicate whether a coin pickup has been released
pkupcoin_x .rs 1 ; x-pos of coin pickup
pkupcoin_y .rs 1 ; y-pos of coin pickup
startlatch .rs 1 ; used to check if start button has been unpressed
buttonlatch .rs 1 ; used to check if other buttons have been unpressed
cursorpos .rs 1 ; position of the title screen cursor (0=day mode, 1=sunset mode, 2=night mode)
; High and low byte for indirect addressing. Used for long loops, i.e. loading >256 bytes.
; Note: Indirect addresses are read backwards (second addr holds high byte, first addr holds low byte).
indr_low .rs 1
indr_high .rs 1
y_mod .rs 1 ; for use in mod subroutine
tempvar1 .rs 1 ; temporary, short use variable
temp_bin .rs 2 ; temp variables for binary to decimal conversion
temp_dec .rs 5
; Sound related variables
sound_ptr .rs 2 ; address pointer used by sound engine for indirect addressing (must be on zero-page)
sound_cur_song .rs 1 ; index of currently playing song
; Masks for BIT instructions in button scripts (BIT masks use variables instead of constants due to BIT opcode issue)
MASK_A .rs 1
MASK_B .rs 1
MASK_SELECT .rs 1
MASK_START .rs 1
MASK_UP .rs 1
MASK_DOWN .rs 1
MASK_LEFT .rs 1
MASK_RIGHT .rs 1
PKUP_COLL_LIFE .rs 1 ; masks for pkupcollisions
PKUP_COLL_COIN .rs 1 ; *BIT masks use variables instead of constants due to BIT opcode issue
;; DECLARE CONSTANTS
STATETITLE = $00 ; displaying title screen
STATEPLAYING = $01 ; playing game
STATEPAUSE = $02 ; displaying pause screen
STATEGAMEOVER = $03 ; displaying game over screen
WALL_LEFT = $0D ; player (coordinate point) boundaries
WALL_RIGHT = $F6
WALL_TOP = $28
WALL_BOTTOM = $A0
PLAYERX_INIT = $80
PLAYERY_INIT = $58
PLAYERBASESPEED = $02 ; initial horiz speed per frame
PLAYERINVNC = 80 ; number of invincibility frames after collision
OBSTBASESPEED = $01 ; initial upward speed of obstacles
OBST1_X_INIT = $30
OBST2_X_INIT = $60
OBST3_X_INIT = $90
OBST_REL_INIT = $50 ; initial release timer value. evenly spaces obstacles (80px apart)
OBST_RESETY = $FF ; reset y-pos for obstacles after wrap, and before initial release
PKUPBASESPEED = $01 ; initial upward speed of pickups
;PKUPLIFECHANCE = 32 ; set to N for a 1/N chance to spawn on each frame (should ideally be a power of 2)
PKUPLIFEX_INIT = $20
PKUPLIFEY_INIT = $F2
;PKUPCOINCHANCE = 3 ; set to N for a 1/N chance to spawn on each frame (should ideally be a power of 2)
PKUPCOINX_INIT = $A0
PKUPCOINY_INIT = $FF
UI_CUTOFF = $14 ; bottom pixel of UI elements. no game objects should go above this y-pos.
UI_LIVESX = $F0 ; lives counter position
UI_LIVESY = $0C
UI_SCOREX = $08 ; score counter position
UI_SCOREY = $0C
UI_HISCOREX = $74 ; high-score display position
UI_HISCOREY = $0C
CURSOR_DAY = $8F ; cursor y-positions for each mode selection
CURSOR_SUNSET = $A7
CURSOR_NIGHT = $BF
SPRITE_RAM_UI = $0200 ; starting location of sprite RAM for UI elements (also starting point for all sprites)
SPRITE_RAM_OBST = $0260 ; starting location of sprite RAM for obstacles
SPRITE_RAM_CHAR = $0290 ; starting location of sprite RAM for character
SPRITE_RAM_PKUP = $02C0 ; starting location of sprite RAM for pickups
SPRITE_RAM_TITL = $02C8 ; starting location of sprite RAM for title screen elements
NUMSPRITES = (8+10+6+12+12+2+1) ; number of active sprites. used when initially loading sprites.
PAUSED_TEXTX = $68
PAUSED_TEXTY = $60
;PAUSED_PPCR2 = %10000001 ; XORed with state of PPUMASK to create a pause gray-out. (grayscale + intensify reds)
GAMEOVER_TEXTX = $5C
GAMEOVER_TEXTY = $60
;;;;;;;;;;;;;;;;;;
.bank 2
.org $C000
;; Reset script ;;
vblankwait:
BIT $2002
BPL vblankwait
RTS
RESET:
SEI ; disable IRQs
CLD ; disable decimal mode
LDX #$40
STX $4017 ; disable APU frame IRQ
LDX #$FF
TXS ; Set up stack
INX ; now X = 0
STX $2000 ; disable NMI
STX $2001 ; disable rendering
STX $4010 ; disable DMC IRQs
JSR vblankwait ; First wait for vblank to make sure PPU is ready
clrmem:
LDA #$00
STA $0000, x ; initializes all variables to 0
STA $0100, x
STA $0300, x
STA $0400, x
STA $0500, x
STA $0600, x
STA $0700, x
LDA #$FE
STA $0200, x
INX
BNE clrmem
JSR vblankwait ; Second wait for vblank, PPU is ready after this
;; Set initial values (on RESET) ;;
; set default 16-bit rng seed
LDA #$12
STA rng ; store high 8 bits in rng address
LDA #$34
STA rng+1 ; store low 8 bits in rng address
LDA #%10010000 ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
STA ppu_cr1 ; port $2000
LDA #%00011110 ; enable sprites, enable background, no clipping on left side
STA ppu_cr2 ; port $2001
LDA #PLAYERX_INIT
STA playerx
LDA #PLAYERY_INIT
STA playery
LDA #$01
STA playerfacing
LDA #$03
STA playerlives
JSR Obst_Init ; initialize obstacle positions, variables, and timers
LDA #PKUPLIFEX_INIT
STA pkuplife_x
LDA #PKUPLIFEY_INIT
STA pkuplife_y
LDA #PKUPCOINX_INIT
STA pkupcoin_x
LDA #PKUPCOINY_INIT
STA pkupcoin_y
; Set mask "constants"
LDA #%10000000
STA PKUP_COLL_LIFE
LDA #%01000000
STA PKUP_COLL_COIN
LDA #%10000000
STA MASK_A
LDA #%01000000
STA MASK_B
LDA #%00100000
STA MASK_SELECT
LDA #%00010000
STA MASK_START
LDA #%00001000
STA MASK_UP
LDA #%00000100
STA MASK_DOWN
LDA #%00000010
STA MASK_LEFT
LDA #%00000001
STA MASK_RIGHT
;; Sprite data only needs to be fully loaded once, here.
LoadSprites:
; start DMA transfer of sprite data from $0200+ to SPR-RAM
LDA #$00
STA $2003 ; set the low byte (00) of the RAM address
LDA #$02
STA $4014 ; set the high byte (02) of the RAM address, start the transfer
LDX #$00
LoadSpritesLoop:
LDA sprites, x
STA $0200, x
INX
CPX #(NUMSPRITES*4) ; load all sprites (4 data bytes each)
BNE LoadSpritesLoop
;LDA ( sprites+1 ), x ; 64*4 = 256 bytes, but loop can only run 255 times (max of X). if needed, add last data byte
;STA ( $0201+$01 ), x
;; Write second Playing nametable to $2800 (mirrored on $2C00)
; this must be done here because loading two nametables at once during the game will
; result in an NMI being called halfway through the second load.
; since we don't scroll on the menu, we won't see this second nametable until the game starts
LDA $2002 ; read PPU status to reset the high/low latch
LDA #$28
STA $2006 ; write the high byte of $2800 address (nametable 1)
LDA #$00
STA $2006 ; write the low byte of $2800 address (nametable 1)
LDX #$00
LDY #$00
LDA #LOW(background1) ; load 2-byte address of background1 into indr_low and indr_high
STA indr_low ; for indirect addressing
LDA #HIGH(background1)
STA indr_high
JSR LoadNametable
; Set starting game state
LDA #STATETITLE
STA gamestate
; Load initial Title screen graphics and logic
JSR EngineTitleInit
LDA ppu_cr1
STA $2000
LDA ppu_cr2
STA $2001
; Initialize sound engine
JSR sound_init
Forever:
JMP Forever ; main game loop, waiting for NMI
;;; Start of VBlank ;;;
; VBlank is the only safe time to write to PPU.
; Thus, all PPU-writing code should be near the start of NMI.
; Code towards the end of NMI can potentially run past VBlank into next screen drawing cycle.
; This is fine for all code that does not write to PPU (unless it runs long enough to get to next NMI).
NMI:
;; Swap Nametables if needed, for smooth scrolling
NTSwapCheck:
LDA ppu_scroll ; check if the scroll just wrapped from 240 to 0
CMP #240
BNE NTSwapCheckDone
LDA #0
STA ppu_scroll ; reset ppu_scroll
NTSwap:
LDA ppu_nametable ; load current Nametable number (%00 or %10)
EOR #%00000010 ; exclusive OR of bit 0 will flip that bit
STA ppu_nametable
NTSwapCheckDone:
LDA #$00
STA $2005 ; first write: no horizontal scrolling
LDA ppu_scroll
STA $2005 ; second write: advance vertical scroll to ppu_scroll
; ppu_scroll will only increment if gamestate is STATEPLAYING, if ppu_scroll is fixed at 0, scrolling will not happen.
;; PPU clean up section, so rendering the next frame starts properly.
LDA ppu_cr1 ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
ORA ppu_nametable ; swap Nametables 0 and 2
STA $2000
LDA ppu_cr2 ; enable sprites, enable background, no clipping on left side
STA $2001
;; Initiate sprite update
LDA #$00
STA $2003 ; set the low byte (00) of the RAM address
LDA #$02
STA $4014 ; set the high byte (02) of the RAM address, start the transfer
;; Advance random number (gameplay seed will be determined by length of time spend on title screen)
JSR prng
;; Get controller inputs and start game engine
JSR ReadController ; get the current button data for player 1
;; Check for Start button
LDA buttons
BIT MASK_START ; get Start button, bit is 1 (Z=0) if start is pressed
BNE StartCheck_StartPressed ; branch if Z=0
LDA #$00
CMP startlatch ; check state of startlatch
BEQ GameEngine ; if start is not pressed and startlatch is clear, skip all checks
LDA #$00
STA startlatch ; if start is not pressed and startlatch is set, clear startlatch
JMP GameEngine ; since start is not pressed, skip all checks
StartCheck_StartPressed:
LDA #$00
CMP startlatch ; check state of startlatch
BEQ StartCheck_Playing ; if start is pressed and startlatch is clear, change state
JMP GameEngine ; if start is pressed and startlatch is set, skip all checks
StartCheck_Playing:
LDA gamestate
CMP #STATEPLAYING ; check if we're Playing
BNE StartCheck_Title ; if not, see if we're on Title screen
LDA #STATEPAUSE ; if so, set Pause state
STA gamestate
JSR EnginePausedInit ; run initial Pause script, loading sprites
LDA #$01
STA startlatch ; engage startlatch, indicating start is being held down
JMP GameEngine ; skip remaining checks
StartCheck_Title:
LDA gamestate
CMP #STATETITLE ; check if we're on Title screen
BNE StartCheck_Paused ; if not, see if we're Paused
LDA #STATEPLAYING ; if so, set Playing state
STA gamestate
JSR EnginePlayingInit_T ; run initial start new game script
LDA #$01
STA startlatch ; engage startlatch, indicating start is being held down
JMP GameEngine ; skip remaining checks
StartCheck_Paused:
LDA gamestate
CMP #STATEPAUSE ; check if we're on Pause screen
BNE StartCheck_GameOver ; if not, check if we're on Game Over screen
LDA #STATEPLAYING ; if so, set Playing state
STA gamestate
JSR EnginePlayingInit_P ; run initial unpause script
LDA #$01
STA startlatch ; engage startlatch, indicating start is being held down
JMP GameEngine ; skip remaining checks
StartCheck_GameOver:
LDA gamestate
CMP #STATEGAMEOVER ; check if we're on Game Over screen
BNE GameEngine ; if not, continue to GameEngine
LDA #STATETITLE ; if so, set Title state
STA gamestate
JSR EngineTitleInit ; run initial new game script
LDA #$01
STA startlatch ; engage startlatch, indicating start is being held down
JMP GameEngine ; skip remaining checks
GameEngine:
LDA gamestate
CMP #STATETITLE
BNE GameEngine_NotTitle ; need to use JMP here because relative addressing used by BEQ goes out of range
JMP EngineTitle ; game is displaying title screen
GameEngine_NotTitle:
LDA gamestate
CMP #STATEPLAYING
BNE GameEngine_NotPlaying
JMP EnginePlaying ; game is playing
GameEngine_NotPlaying:
LDA gamestate
CMP #STATEPAUSE
BNE GameEngine_NotPaused
JMP EnginePause ; game is paused
GameEngine_NotPaused:
LDA gamestate
CMP #STATEGAMEOVER
BNE GameEngine_NotOver
JMP EngineGameOver ; game is displaying ending screen
GameEngine_NotOver:
GameEngineDone:
SoundEngine:
JSR sound_play_frame
SoundEngineDone:
RTI ; return from NMI
;;;;;;;;;;;;;;;;;;
EngineTitleInit:
; Initial script to run on game start or when gamestate switches from Game Over to Title screen
LDA #0
STA ppu_scroll ; reset and disable background scrolling
LDA #$00
STA ppu_nametable ; reset nametable priority to default (display nametable 0)
LDA #$03
STA playerlives ; reset lives
LDA #$00
STA difficulty ; reset difficulty to Easy ($00)
LDA #$00
STA difficultyflag ; reset difficulty flag
;; Load palettes ;;
EngineTitleInit_LoadPlts:
LDA $2002 ; read PPU status to reset the high/low latch
LDA #$3F
STA $2006 ; write the high byte of $3F00 address (palette area)
LDA #$00
STA $2006 ; write the low byte of $3F00 address (palette area)
LDX #$00 ; start out at 0
EngineTitleInit_LoadPltsLoop:
LDA titlepalette, x
STA $2007 ; write to PPU
INX
CPX #32 ; run 32 times for 16 bg colors and 16 sprite colors
BNE EngineTitleInit_LoadPltsLoop
;; Load background ;;
; disable sprite and background visibility to properly unlock PPU RAM
LDA ppu_cr2
EOR #%00011000
STA $2001
EngineTitleInit_LoadBG:
;; write Title screen nametable to $2000 (mirrored on $2400)
LDA $2002 ; read PPU status to reset the high/low latch
LDA #$20
STA $2006 ; write the high byte of $2000 address (nametable 0)
LDA #$00
STA $2006 ; write the low byte of $2000 address (nametable 0)
LDX #$00
LDY #$00
LDA #LOW(background_title) ; load 2-byte address of background_title into indr_low and indr_high
STA indr_low ; for indirect addressing
LDA #HIGH(background_title)
STA indr_high
JSR LoadNametable
;; Hide game sprites ;;
LDX #$00
EngineTitleInit_HideSpritesLoop:
LDA ($0200+2), x ; load attributes byte of sprite data
EOR #%00100000 ; XOR to set Priority bit to 1 (display behind BG)
STA ($0200+2), x
INX ; advance to next sprite
INX
INX
INX
CPX #(SPRITE_RAM_TITL - $0200) ; only hide sprites up until Title sprites are reached
BNE EngineTitleInit_HideSpritesLoop
; Move player and all obstacles off-screen.
; Without this, sprites may show up through "black" (transparent) pixels on title background.
LDA #$F0
STA playerx
STA playery
JSR CharMoveSpritesUpdate ; shift character sprites to match (playerx, playery) position
LDA #$FF
STA obst1_y
STA obst2_y
STA obst3_y
JSR Obst_MoveY
; clear Game Over letters
LDX #S_LETTER1
JSR ClearUIElement
LDX #S_LETTER2
JSR ClearUIElement
LDX #S_LETTER3
JSR ClearUIElement
LDX #S_LETTER4
JSR ClearUIElement
LDX #S_LETTER5
JSR ClearUIElement
LDX #S_LETTER6
JSR ClearUIElement
LDX #S_LETTER7
JSR ClearUIElement
LDX #S_LETTER8
JSR ClearUIElement
; reset cursor position to Day mode
LDA #$00
STA cursorpos ; update variable
LDA #CURSOR_DAY
STA (SPRITE_RAM_TITL+S_CURSOR+0) ; change cursor y-pos
LDA (SPRITE_RAM_TITL+S_CURSOR+2)
AND #%11111100 ; clear last two bits
ORA #%00000000 ; set last two bits to %00
STA (SPRITE_RAM_TITL+S_CURSOR+2) ; change cursor palette number
LDA #$00
STA gamemode ; reset gamemode to Day mode
JSR sound_unpause ; reset sound engine pause state
LDA #sng_Title ; load and play Title theme
STA sound_cur_song
JSR sound_load
RTS ; end of EngineTitle_Init
EngineTitle:
;; Advance clocks
JSR ClockStep
; Reset clockSecs and fix to 0 while on Title screen
LDA #$00
STA clockSecs+3
STA clockSecs+2
STA clockSecs+1
STA clockSecs+0
EngineTitle_ButtonUp:
LDA buttons
BIT MASK_UP ; get Up button
BEQ EngineTitle_ButtonDown ; skip if bit is 0 (Z=1). if we branch here, Up was not pressed
LDA #$00
CMP buttonlatch ; check if a button is still being held down
BEQ EngineTitle_ButtonUpCont
JMP EngineTitle_SkipUpdates ; if so, skip all updates
EngineTitle_ButtonUpCont:
LDA cursorpos
SEC
SBC #$01
STA cursorpos ; if not, update cursor position
LDA #$01
STA buttonlatch ; engage buttonlatch, indicating a button is being held down
JMP EngineTitle_ButtonDone ; if Up is pressed, don't check Down
EngineTitle_ButtonDown:
LDA buttons
BIT MASK_DOWN ; get Down button
BEQ EngineTitle_NoUpDown ; skip if bit is 0 (Z=1). if we branch here, Up/Down were not pressed
LDA #$00
CMP buttonlatch ; check if a button is still being held down
BEQ EngineTitle_ButtonDownCont
JMP EngineTitle_SkipUpdates ; if so, skip all updates
EngineTitle_ButtonDownCont:
LDA cursorpos
CLC
ADC #$01
STA cursorpos ; if not, update cursor position
LDA #$01
STA buttonlatch ; engage buttonlatch, indicating a button is being held down
JMP EngineTitle_ButtonDone ; if Down is pressed, don't run NoUpDown script
EngineTitle_NoUpDown:
LDA #$00
STA buttonlatch ; since Up/Down were not pressed, clear button latch
JMP EngineTitle_SkipUpdates ; since Up/Down were not pressed, skip all cursor and background updates
EngineTitle_ButtonDone:
; If cursorpos went below $00 (up from top option), set to $00
LDA cursorpos
CMP #$FF
BNE EngineTitle_NoUnderflow
LDA #$00
STA cursorpos
JMP EngineTitle_SkipUpdates
EngineTitle_NoUnderflow:
; If cursorpos went above $02 (down from bottom option), set to $02
LDA cursorpos
CMP #$03
BNE EngineTitle_NoOverflow
LDA #$02
STA cursorpos
JMP EngineTitle_SkipUpdates
EngineTitle_NoOverflow:
;; Change cursor y-position ($8F for Day mode, $A7 for Sunset mode, $BF for Night mode)
;; Change cursor palette number (%00000000 for Day mode, %00000001 for Sunset mode, %00000010 for Night mode)
;; Swap background attribute table to switch background palette (BG0 for Day mode, BG1 for Sunset mode, BG2 for Night mode).
LDA cursorpos
CMP #$00
BNE EngineTitle_CursorMove1
LDA #CURSOR_DAY ; change cursor y-pos
STA (SPRITE_RAM_TITL+S_CURSOR+0)
LDA (SPRITE_RAM_TITL+S_CURSOR+2)
AND #%11111100 ; clear last two bits
ORA #%00000000 ; set last two bits to %00
STA (SPRITE_RAM_TITL+S_CURSOR+2) ; change cursor palette number
JSR ReplaceAttTables_DayMode ; swap background attribute table
LDA #$00
STA gamemode ; update gamemode for Day
JMP EngineTitle_CursorMoveDone
EngineTitle_CursorMove1:
CMP #$01
BNE EngineTitle_CursorMove2
LDA #CURSOR_SUNSET ; change cursor pos
STA (SPRITE_RAM_TITL+S_CURSOR+0)
LDA (SPRITE_RAM_TITL+S_CURSOR+2) ; change cursor palette number
AND #%11111100 ; clear last two bits
ORA #%00000001 ; set last two bits to %01
STA (SPRITE_RAM_TITL+S_CURSOR+2)
JSR ReplaceAttTables_SunsetMode ; swap background attribute table
LDA #$01
STA gamemode ; update gamemode for Sunset
JMP EngineTitle_CursorMoveDone
EngineTitle_CursorMove2: ; else
LDA #CURSOR_NIGHT ; change cursor pos
STA (SPRITE_RAM_TITL+S_CURSOR+0)
LDA (SPRITE_RAM_TITL+S_CURSOR+2) ; change cursor palette number
AND #%11111100 ; clear last two bits
ORA #%00000010 ; set last two bits to %10
STA (SPRITE_RAM_TITL+S_CURSOR+2)
JSR ReplaceAttTables_NightMode ; swap background attribute table
LDA #$02
STA gamemode ; update gamemode for Night
JMP EngineTitle_CursorMoveDone
EngineTitle_CursorMoveDone:
; reset clock256 and hide cursor to force cursor blink on next frame (hides laggy cursor palette switching)
LDA (SPRITE_RAM_TITL+S_CURSOR+2)
ORA #%00100000 ; set cursor sprite priority to 1 (hide)
STA (SPRITE_RAM_TITL+S_CURSOR+2)
LDA #$FF ; cursor will reappear next frame when clock hits $00
STA clock256
; initiate sprite update here to force cursor to update before background (preventing palette overlap)
LDA #$00
STA $2003 ; set the low byte (00) of the RAM address
LDA #$02
STA $4014 ; set the high byte (02) of the RAM address, start the transfer
EngineTitle_SkipUpdates:
;; Blink cursor
LDA clock256 ; load A and Y for mod
LDY #16 ; blink every 16 frames
JSR mod
CMP #0
BNE EngineTitle_CursorBlinkDone
LDA (SPRITE_RAM_TITL+S_CURSOR+2)
EOR #%00100000 ; flip cursor sprite priority
STA (SPRITE_RAM_TITL+S_CURSOR+2)
EngineTitle_CursorBlinkDone:
JMP GameEngineDone
;;;;;;;;;;;;;;;;;;
EnginePausedInit:
; Initial script to run when gamestate switches from Playing to Pause
EnginePausedInit_LoadPalettes:
LDA $2002 ; read PPU status to reset the high/low latch
LDA #$3F
STA $2006 ; write the high byte of $3F00 address (palette area)
LDA #$00
STA $2006 ; write the low byte of $3F00 address (palette area)
LDX #$00 ; start out at 0
EnginePausedInit_LoadPalettesLoop:
LDA pausepalette, x
STA $2007 ; write to PPU
INX
CPX #32 ; run 32 times for 16 bg colors and 16 sprite colors
BNE EnginePausedInit_LoadPalettesLoop
;; PPU clean up (must be called, or background will skip for a frame on pause)
LDA #$00
STA $2005 ; first write: no horizontal scrolling
LDA ppu_scroll
STA $2005 ; second write: advance vertical scroll
LDA ppu_cr1 ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
ORA ppu_nametable ; swap Nametables 0 and 2
STA $2000
LDA ppu_cr2 ; enable sprites, enable background, no clipping on left side
STA $2001
;; Print "Paused" on screen
; load letters into sprite slots
LDY #0 ; get Y ready to offset letter x-positions
LDX #S_LETTER1 ; load ram offset to X (which sprite to load)
LDA #T_P ; load tile number for letter
JSR PausedLetterLoop
LDX #S_LETTER2
LDA #T_A
JSR PausedLetterLoop
LDX #S_LETTER3
LDA #T_U
JSR PausedLetterLoop
LDX #S_LETTER4
LDA #T_S
JSR PausedLetterLoop
LDX #S_LETTER5
LDA #T_E
JSR PausedLetterLoop
LDX #S_LETTER6
LDA #T_D
JSR PausedLetterLoop
JMP PausedLetterDone
PausedLetterLoop:
STA (SPRITE_RAM_UI+1), x ; set tile number
TYA
CLC
ADC #PAUSED_TEXTX ; get x-pos, offset by Y
STA (SPRITE_RAM_UI+3), x ; set x-pos
LDA #PAUSED_TEXTY ; get y-pos
STA (SPRITE_RAM_UI+0), x ; set y-pos
INY ; increment Y by 8 pixels
INY
INY
INY
INY
INY
INY
INY
RTS
PausedLetterDone:
LDA #sfx_Pause ; play Pause SFX
JSR sound_load
JSR sound_pause ; run sound engine pause script
RTS ; end of EnginePausedInit
EnginePause:
;; Main pause loop
JMP GameEngineDone
;;;;;;;;;;;;;;;;;;
EngineGameOverInit:
; Initial script to run when gamestate switches from Playing to Game Over
; disable sprite and background visibility to properly unlock PPU RAM
LDA ppu_cr2
EOR #%00011000
STA $2001
EngineGameOverInit_LoadPlts:
LDA $2002 ; read PPU status to reset the high/low latch
LDA #$3F
STA $2006 ; write the high byte of $3F00 address (palette area)
LDA #$00
STA $2006 ; write the low byte of $3F00 address (palette area)
LDX #$00 ; start out at 0
EngineGameOverInit_LoadPltsLoop:
LDA gameoverpalette, x
STA $2007 ; write to PPU
INX
CPX #32 ; run 32 times for 16 bg colors and 16 sprite colors
BNE EngineGameOverInit_LoadPltsLoop
;; PPU clean up (must be called, or background will skip for a frame on pause)
LDA #$00
STA $2005 ; first write: no horizontal scrolling
LDA ppu_scroll
STA $2005 ; second write: advance vertical scroll
LDA ppu_cr1 ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
ORA ppu_nametable ; swap Nametables 0 and 2
STA $2000
LDA ppu_cr2 ; enable sprites, enable background, no clipping on left side
STA $2001
;; Print "Game Over" on screen
; load letters into sprite slots
LDY #0 ; get Y ready to offset letter x-positions
LDX #S_LETTER1 ; load ram offset to X (which sprite to load)
LDA #T_G ; load tile number for letter
JSR GameOverLetterLoop
LDX #S_LETTER2
LDA #T_A
JSR GameOverLetterLoop
LDX #S_LETTER3
LDA #T_M
JSR GameOverLetterLoop
LDX #S_LETTER4
LDA #T_E
JSR GameOverLetterLoop
INY ; space - increment Y by 8 pixels
INY
INY
INY
INY
INY
INY
INY
LDX #S_LETTER5
LDA #T_O
JSR GameOverLetterLoop
LDX #S_LETTER6
LDA #T_V
JSR GameOverLetterLoop
LDX #S_LETTER7
LDA #T_E
JSR GameOverLetterLoop
LDX #S_LETTER8
LDA #T_R
JSR GameOverLetterLoop
JMP GameOverLetterDone
GameOverLetterLoop:
STA (SPRITE_RAM_UI+1), x ; set tile number
TYA
CLC
ADC #GAMEOVER_TEXTX ; get x-pos, offset by Y
STA (SPRITE_RAM_UI+3), x ; set x-pos
LDA #GAMEOVER_TEXTY ; get y-pos
STA (SPRITE_RAM_UI+0), x ; set y-pos
INY ; increment Y by 8 pixels
INY
INY
INY
INY
INY
INY
INY
RTS
GameOverLetterDone:
; Check and update highscore
LDA highscore+0
CMP playerscore+0
BCC HighScoreUpdate ; if highscore MSB < playerscore MSB, update highscore
BNE HighScoreEnd ; but, if highscore MSB > playerscore MSB, skip update
; (past this point, we know highscore MSB <= playerscore MSB)
LDA highscore+1
CMP playerscore+1
BCC HighScoreUpdate ; else if highscore LSB < playerscore LSB, update highscore
JMP HighScoreEnd ; else, skip update
HighScoreUpdate:
LDA playerscore+0
STA highscore+0
LDA playerscore+1
STA highscore+1
HighScoreEnd:
; Update high score display
JSR UpdateHighScoreDisplay
LDA #sfx_Gameover ; play Game Over SFX
JSR sound_load
JSR sound_pause ; run sound engine pause script
RTS ; end of EngineGameOverInit
EngineGameOver:
;; Main game over loop
JMP GameEngineDone
;;;;;;;;;;;;;;;;;;
EnginePlayingInit_P:
; Initial script to run when gamestate switches from Pause to Playing