@@ -463,12 +463,12 @@ calcBCDLinesAndTileQueue:
463
463
@ret:
464
464
rts
465
465
testCrash:
466
- lda #$1C ; setting all cycles which always happen
466
+ lda #$1C ; setting all cycles which always happen. for optimizing, this can be removed if all compared numbers are reduced by $6F1C.
467
467
sta cycleCount
468
468
lda #$6F
469
469
sta cycleCount+1 ;low byte at +1
470
470
471
- lda completedLines
471
+ lda completedLines ; checking if lines cleared
472
472
beq @linesNotCleared
473
473
ldx #$04 ;setting loop to run 4x
474
474
@clearedLine:
@@ -490,7 +490,7 @@ testCrash:
490
490
bne @clearedLine
491
491
492
492
@linesNotCleared:
493
- lda displayNextPiece
493
+ lda displayNextPiece ;00 is nextbox enabled
494
494
bne @nextOff
495
495
lda #$8A ; add 394 cycles for nextbox
496
496
adc cycleCount+1
@@ -504,7 +504,7 @@ testCrash:
504
504
bne @allegro
505
505
lda #$95 ; 149 in decimal.
506
506
clc
507
- ldx wasAllegro ; FF is allegro. 00 is no allegro.
507
+ ldx wasAllegro ; FF is allegro. 00 is no allegro. wasAllegro contains allegro status prior to this frame
508
508
beq @addMusicCycles
509
509
adc #$26 ;add 38 cycles for disabling allegro
510
510
@addMusicCycles:
@@ -520,7 +520,7 @@ testCrash:
520
520
asl
521
521
asl
522
522
asl
523
- asl ;multiply by 16
523
+ asl ;multiply by 16 cycles per cell checked
524
524
tax ; save low byte result
525
525
lda cycleCount
526
526
adc #$00 ; add high byte carry
@@ -555,14 +555,14 @@ testCrash:
555
555
lda crashFlag
556
556
and #$01
557
557
beq @digit2
558
- lda #$4F ; add 79 cycles for 10s place
558
+ lda #$4F ; add 79 cycles for lines 10s place
559
559
adc allegroIndex
560
560
sta allegroIndex
561
561
@digit2:
562
562
lda crashFlag
563
563
and #$02
564
564
beq @clearStats
565
- lda #$0C ; add 12 cycles for 100s place
565
+ lda #$0C ; add 12 cycles for lines 100s place
566
566
adc allegroIndex
567
567
sta allegroIndex
568
568
@clearStats:
@@ -585,7 +585,7 @@ testCrash:
585
585
bcc @single
586
586
cmp #$08
587
587
bcs @over7
588
- lda #$09 ; 1-6 costs 9
588
+ lda #$09 ; 1-6 pushdown costs 9 add'l cycles
589
589
adc allegroIndex
590
590
sta allegroIndex
591
591
@over7:
@@ -627,7 +627,7 @@ testCrash:
627
627
sta factorA24+2
628
628
sta factorB24+1
629
629
sta factorB24+2
630
- sta crashFlag ; done with flags and can now reuse variable
630
+ sta crashFlag ; unrelated to current routine, just needed to clear the flag and $00 was loaded.
631
631
jsr unsigned_mul24 ; result in product24
632
632
clc
633
633
lda product24
@@ -650,10 +650,10 @@ testCrash:
650
650
cmp #$08
651
651
bne @not8
652
652
clc
653
- adc allegroIndex
653
+ adc allegroIndex ; adds 8 cycles for current piece = 8 (horizontal Z)
654
654
sta allegroIndex
655
- @not8: bcc @randomFactors
656
- lda #$0B ; would be 12 but carry is set
655
+ @not8: bcc @randomFactors ; adc above means branch always when entered after it. piece < 8 adds 0 cycles.
656
+ lda #$0B ; would be 12 but carry is set. for piece > 8
657
657
adc allegroIndex
658
658
sta allegroIndex
659
659
@randomFactors:
@@ -672,13 +672,13 @@ testCrash:
672
672
@newBit0:
673
673
lda nmiReturnAddr
674
674
cmp #<updateAudioWaitForNmiAndResetOamStaging+10
675
- beq @returnLate ; checking which instruction returned to
675
+ beq @returnLate ; checking which instruction returned to. if so, add 3 cycles
676
676
lda #$03
677
677
clc
678
678
adc allegroIndex
679
679
sta allegroIndex
680
680
@returnLate:
681
- lda rng_seed+1 ; RNG for OAMDMA
681
+ lda rng_seed+1 ; RNG for OAMDMA, add 1 cycle for syncing
682
682
lsr
683
683
bcc @noDMA
684
684
inc allegroIndex
@@ -691,7 +691,9 @@ testCrash:
691
691
lda cycleCount
692
692
adc #$00
693
693
sta cycleCount
694
- ;crash should occur on cycle count results 29739-29744, 29750-29768 = $742B-7430, $7436-7448
694
+ ;crash should occur on cycle count results 29739-29744, 29750-29768 = $742B-7430, $7436-7448 | red crash on 7448 7447 7446 7445 7444 7443
695
+ ;x8 sw1 | x7 sw2 | x6 sw3 | x5 sw4 | x4 sw5 | x3 sw6 | x2 sw7 | x1 sw8
696
+ ;switch routine is reversed.
695
697
;confettiA should occur on cycle count when switch1 = 29768+75 = 29843-30192
696
698
;level lag is +195 from line lag
697
699
;level lag to RTS is +70 rts +6 jsr +6 +41 to beginning of confettiA = 123 = 30315+ line lag
@@ -705,129 +707,141 @@ testCrash:
705
707
cmp #$31 ; gap
706
708
bcs @continue
707
709
lda #$F0
708
- sta crashFlag
709
- jmp @crashGraphics
710
+ sta crashFlag ; F0 means standard crash.
711
+ jmp @crashGraphics ;too far to branch
710
712
@continue:
711
713
cmp #$36
712
714
bcc @nextSwitch
713
715
cmp #$49
714
- bcs @nextSwitch
716
+ bcs @nextSwitch ;between 7436 & 7448
717
+ cmp #$43
718
+ bcc @notRed ;checking if crash is during first crashable instruction
719
+ cpx #$07 ; checking which switch routine is active.
720
+ beq @nextSwitch ;continues crashless if sw2
721
+ cpx #$03
722
+ bne @notRed ; runs graphics corruption if sw6
723
+ ldx #$FF ; these are normally set by the code it would jump to after NMI.
724
+ ldy #$00
725
+ lda #$81 ; value normally held at this point in sw6
726
+ jsr satanSpawn
727
+ jmp @allegroClear ;allegroClear is basically return, just clears the variable first.
728
+ @notRed:
715
729
lda #$F0
716
730
sta crashFlag
717
- jmp @crashGraphics
731
+ jmp @crashGraphics ;triggering crash in all other cases
718
732
719
733
@nextSwitch:
720
734
lda switchTable-2 ,x ; adding cycles to advance to next switch routine
721
- sta allegroIndex
735
+ sta allegroIndex ; reusing code at the beginning of the loop that added the accumulated allegroIndex to the main cycle count
722
736
dex
723
737
bne @loop
724
738
;562 has been added to the cycle count
725
739
;confettiA at 30405-30754 76C5-7822
726
740
lda displayNextPiece
727
741
beq @nextOn
728
- lda cycleCount+1 ; add 394 cycles for nextbox if not added earlier
742
+ lda cycleCount+1 ; add 394 cycles for nextbox if not added earlier. Necessary because we're checking for pre-nextbox NMI now.
729
743
adc #$8A
730
744
sta cycleCount+1
731
745
lda cycleCount
732
746
adc #$01 ; high byte of 18A
733
747
sta cycleCount
734
748
bne @nextCheck
735
749
@nextOn:
736
- lda cycleCount
737
- cmp #$76
750
+ lda cycleCount ;testing for limited confetti
751
+ cmp #$76 ;high byte min
738
752
bcc @allegroClear
739
753
bne @not76
740
754
lda cycleCount+1
741
- cmp #$C5
755
+ cmp #$C5 ;low byte min
742
756
bcc @allegroClear
743
757
bcs @confettiA
744
- @not76: cmp #$78
758
+ @not76: cmp #$78 ;high byte max
745
759
bcc @confettiA
746
760
bne @nextCheck
747
761
lda cycleCount+1
748
- cmp #$23
762
+ cmp #$23 ;low byte max
749
763
bcs @nextCheck
750
764
@confettiA:
751
- lda #$E0
765
+ lda #$E0 ;E0 = limited confetti
752
766
sta crashFlag
753
767
jmp confettiHandler
754
768
@nextCheck:
755
- ;levellag at 30877 789D
769
+ ;levellag at 30877 = 0x789D
756
770
lda cycleCount
757
- cmp #$78
771
+ cmp #$78 ;high byte min
758
772
bcc @allegroClear
759
773
bne @levelLag
760
774
lda cycleCount+1
761
- cmp #$9D
775
+ cmp #$9D ;low byte min
762
776
bcc @allegroClear
763
777
@levelLag:
764
778
lda #$01
765
779
sta lagFlag
766
- ;linelag at 31072 7960
780
+ ;linelag at 31072 = 0x7960
767
781
lda cycleCount
768
- cmp #$79
782
+ cmp #$79 ;high byte min
769
783
bcc @allegroClear
770
784
bne @lineLag
771
785
lda cycleCount+1
772
- cmp #$60
786
+ cmp #$60 ;low byte min
773
787
bcc @allegroClear
774
788
@lineLag:
775
789
lda #$03
776
790
sta lagFlag
777
791
;confettiB at 31327-31755 7A5F-7C0B
778
792
lda cycleCount
779
- cmp #$7A
793
+ cmp #$7A ;high byte min
780
794
bcc @allegroClear
781
795
bne @not7A
782
796
lda cycleCount+1
783
- cmp #$5F
797
+ cmp #$5F ;low byte min
784
798
bcc @allegroClear
785
799
bcs @confettiB
786
- @not7A: cmp #$7C
800
+ @not7A: cmp #$7C ;high byte max
787
801
bcc @confettiB
788
802
bne @allegroClear
789
803
lda cycleCount+1
790
- cmp #$0C
804
+ cmp #$0C ;low byte max
791
805
bcs @allegroClear
792
806
@confettiB:
793
- lda #$D0
807
+ lda #$D0 ;D0 = infinite confetti
794
808
sta crashFlag
795
809
jmp confettiHandler
796
810
@allegroClear:
797
- lda #$00
811
+ lda #$00 ;reset allegro flag and return to program execution, no crash
798
812
sta allegroIndex
799
813
lda lagFlag
800
- beq @noLag
814
+ beq @noLag ;if lag should happen, wait a frame here so that sprite staging doesn't happen.
801
815
lda #$00
802
816
sta verticalBlankingInterval
803
817
@checkForNmi:
804
- lda verticalBlankingInterval
818
+ lda verticalBlankingInterval ;busyloop
805
819
beq @checkForNmi
806
820
@noLag: rts
807
821
@crashGraphics:
808
822
lda #$00
809
- sta allegroIndex
823
+ sta allegroIndex ; resetting variable
810
824
lda crashMode
811
825
bne @otherMode
812
- lda outOfDateRenderFlags
826
+ lda outOfDateRenderFlags ; if mode = 0, tell score to update (might not be necessary?) so that crash info is printed
813
827
ora #$04
814
828
sta outOfDateRenderFlags
815
829
lda #$02
816
- sta soundEffectSlot0Init
830
+ sta soundEffectSlot0Init ; play topout sfx
817
831
rts
818
832
@otherMode:
819
- cmp #CRASH_CRASH
833
+ cmp #CRASH_CRASH ;if crash mode, crash
820
834
bcc @topout
821
835
bne @allegroClear
822
- .byte 02
836
+ .byte 02 ; stp
823
837
@topout:
824
- lda #LINECAP_HALT
838
+ lda #LINECAP_HALT ;if topout, activate linecap
825
839
sta linecapState
826
840
rts
827
841
factorTable:
828
- .byte $53 , $88 , $7D , $7D , $7D
842
+ .byte $53 , $88 , $7D , $7D , $7D ;0 single double triple tetris
829
843
sumTable:
830
- .byte $E1 , $1C , $38 , $54 , $80 ; tetris is 4*28+16 = 128
844
+ .byte $E1 , $1C , $38 , $54 , $80 ; 0 single double triple tetris. tetris is 4*28+16 = 128
831
845
switchTable:
832
846
.byte $3C , $77 , $3C , $65 , $3C , $66 , $3C ;60 119 60 101 60 102 60 gets read in reverse
833
847
confettiHandler:
@@ -837,31 +851,69 @@ confettiHandler:
837
851
lda heldButtons_player1
838
852
and #$A0 ; A, Select
839
853
bne @endConfetti
840
- lda frameCounter
854
+ lda frameCounter ;use framecounter for Y coordinate of text, like original confetti but without the offset
841
855
cmp #$FF
842
856
bne @drawConfetti
843
857
lda heldButtons_player1
844
858
and #$47 ; B, Down, Left, Right
845
859
beq @endConfetti
846
860
@drawConfetti:
847
- sta spriteYOffset
848
- lda #$A8
861
+ sta spriteYOffset ;either frameCounter or 80 loaded to A depending on confetti type
862
+ lda #$A8 ;center of playfield
849
863
sta spriteXOffset
850
- lda #$19
864
+ lda #$19 ;ID for "confetti" text
851
865
sta spriteIndexInOamContentLookup
852
- jsr stringSpriteAlignRight
866
+ jsr stringSpriteAlignRight ;draw to screen
853
867
lda #$00
854
- sta verticalBlankingInterval
868
+ sta verticalBlankingInterval ;wait until next frame
855
869
@checkForNmi:
856
- lda verticalBlankingInterval
870
+ lda verticalBlankingInterval ;busyloop
857
871
beq @checkForNmi
858
872
jmp confettiHandler
859
873
@infiniteConfetti:
860
874
lda heldButtons_player1
861
- adc #$80
875
+ adc #$80 ; loading 80 as Y coordinate of confetti text if nothing is held.
862
876
cmp #$80
863
- beq @drawConfetti
877
+ beq @drawConfetti ; if any button is pressed, exit confetti
864
878
@endConfetti:
865
879
lda #$00
866
880
sta allegroIndex
867
- rts
881
+ rts
882
+ satanSpawn: ; copied from routine vanilla game's memset_ppu_page_and_more which is no longer present in gym
883
+ sta tmp1
884
+ stx tmp2
885
+ sty tmp3
886
+ lda PPUSTATUS
887
+ lda currentPpuCtrl
888
+ and #$FB
889
+ sta PPUCTRL
890
+ sta currentPpuCtrl
891
+ lda tmp1
892
+ sta PPUADDR
893
+ ldy #$00
894
+ sty PPUADDR
895
+ ldx #$04
896
+ cmp #$20
897
+ bcs LAC40
898
+ ldx tmp3
899
+ LAC40: ldy #$00
900
+ lda tmp2
901
+ LAC44: sta PPUDATA
902
+ dey
903
+ bne LAC44
904
+ dex
905
+ bne LAC44
906
+ ldy tmp3
907
+ lda tmp1
908
+ cmp #$20
909
+ bcc LAC67
910
+ adc #$02
911
+ sta PPUADDR
912
+ lda #$C0
913
+ sta PPUADDR
914
+ ldx #$40
915
+ LAC61: sty PPUDATA
916
+ dex
917
+ bne LAC61
918
+ LAC67: ldx tmp2
919
+ rts
0 commit comments