Skip to content

Commit 54559f9

Browse files
committed
Merge branch 'Crash-Detection' of github.com:HydrantDude/TetrisGYM into Crash-Detection
2 parents 820ac29 + 68a7f59 commit 54559f9

File tree

1 file changed

+111
-59
lines changed

1 file changed

+111
-59
lines changed

src/playstate/updatestats.asm

Lines changed: 111 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,12 @@ calcBCDLinesAndTileQueue:
463463
@ret:
464464
rts
465465
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.
467467
sta cycleCount
468468
lda #$6F
469469
sta cycleCount+1 ;low byte at +1
470470

471-
lda completedLines
471+
lda completedLines ; checking if lines cleared
472472
beq @linesNotCleared
473473
ldx #$04 ;setting loop to run 4x
474474
@clearedLine:
@@ -490,7 +490,7 @@ testCrash:
490490
bne @clearedLine
491491

492492
@linesNotCleared:
493-
lda displayNextPiece
493+
lda displayNextPiece ;00 is nextbox enabled
494494
bne @nextOff
495495
lda #$8A ; add 394 cycles for nextbox
496496
adc cycleCount+1
@@ -504,7 +504,7 @@ testCrash:
504504
bne @allegro
505505
lda #$95 ; 149 in decimal.
506506
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
508508
beq @addMusicCycles
509509
adc #$26 ;add 38 cycles for disabling allegro
510510
@addMusicCycles:
@@ -520,7 +520,7 @@ testCrash:
520520
asl
521521
asl
522522
asl
523-
asl ;multiply by 16
523+
asl ;multiply by 16 cycles per cell checked
524524
tax ; save low byte result
525525
lda cycleCount
526526
adc #$00 ; add high byte carry
@@ -555,14 +555,14 @@ testCrash:
555555
lda crashFlag
556556
and #$01
557557
beq @digit2
558-
lda #$4F ; add 79 cycles for 10s place
558+
lda #$4F ; add 79 cycles for lines 10s place
559559
adc allegroIndex
560560
sta allegroIndex
561561
@digit2:
562562
lda crashFlag
563563
and #$02
564564
beq @clearStats
565-
lda #$0C ; add 12 cycles for 100s place
565+
lda #$0C ; add 12 cycles for lines 100s place
566566
adc allegroIndex
567567
sta allegroIndex
568568
@clearStats:
@@ -585,7 +585,7 @@ testCrash:
585585
bcc @single
586586
cmp #$08
587587
bcs @over7
588-
lda #$09 ; 1-6 costs 9
588+
lda #$09 ; 1-6 pushdown costs 9 add'l cycles
589589
adc allegroIndex
590590
sta allegroIndex
591591
@over7:
@@ -627,7 +627,7 @@ testCrash:
627627
sta factorA24+2
628628
sta factorB24+1
629629
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.
631631
jsr unsigned_mul24 ; result in product24
632632
clc
633633
lda product24
@@ -650,10 +650,10 @@ testCrash:
650650
cmp #$08
651651
bne @not8
652652
clc
653-
adc allegroIndex
653+
adc allegroIndex ; adds 8 cycles for current piece = 8 (horizontal Z)
654654
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
657657
adc allegroIndex
658658
sta allegroIndex
659659
@randomFactors:
@@ -672,13 +672,13 @@ testCrash:
672672
@newBit0:
673673
lda nmiReturnAddr
674674
cmp #<updateAudioWaitForNmiAndResetOamStaging+10
675-
beq @returnLate ; checking which instruction returned to
675+
beq @returnLate ; checking which instruction returned to. if so, add 3 cycles
676676
lda #$03
677677
clc
678678
adc allegroIndex
679679
sta allegroIndex
680680
@returnLate:
681-
lda rng_seed+1 ; RNG for OAMDMA
681+
lda rng_seed+1 ; RNG for OAMDMA, add 1 cycle for syncing
682682
lsr
683683
bcc @noDMA
684684
inc allegroIndex
@@ -691,7 +691,9 @@ testCrash:
691691
lda cycleCount
692692
adc #$00
693693
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.
695697
;confettiA should occur on cycle count when switch1 = 29768+75 = 29843-30192
696698
;level lag is +195 from line lag
697699
;level lag to RTS is +70 rts +6 jsr +6 +41 to beginning of confettiA = 123 = 30315+ line lag
@@ -705,129 +707,141 @@ testCrash:
705707
cmp #$31 ; gap
706708
bcs @continue
707709
lda #$F0
708-
sta crashFlag
709-
jmp @crashGraphics
710+
sta crashFlag ; F0 means standard crash.
711+
jmp @crashGraphics ;too far to branch
710712
@continue:
711713
cmp #$36
712714
bcc @nextSwitch
713715
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:
715729
lda #$F0
716730
sta crashFlag
717-
jmp @crashGraphics
731+
jmp @crashGraphics ;triggering crash in all other cases
718732

719733
@nextSwitch:
720734
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
722736
dex
723737
bne @loop
724738
;562 has been added to the cycle count
725739
;confettiA at 30405-30754 76C5-7822
726740
lda displayNextPiece
727741
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.
729743
adc #$8A
730744
sta cycleCount+1
731745
lda cycleCount
732746
adc #$01 ; high byte of 18A
733747
sta cycleCount
734748
bne @nextCheck
735749
@nextOn:
736-
lda cycleCount
737-
cmp #$76
750+
lda cycleCount ;testing for limited confetti
751+
cmp #$76 ;high byte min
738752
bcc @allegroClear
739753
bne @not76
740754
lda cycleCount+1
741-
cmp #$C5
755+
cmp #$C5 ;low byte min
742756
bcc @allegroClear
743757
bcs @confettiA
744-
@not76: cmp #$78
758+
@not76: cmp #$78 ;high byte max
745759
bcc @confettiA
746760
bne @nextCheck
747761
lda cycleCount+1
748-
cmp #$23
762+
cmp #$23 ;low byte max
749763
bcs @nextCheck
750764
@confettiA:
751-
lda #$E0
765+
lda #$E0 ;E0 = limited confetti
752766
sta crashFlag
753767
jmp confettiHandler
754768
@nextCheck:
755-
;levellag at 30877 789D
769+
;levellag at 30877 = 0x789D
756770
lda cycleCount
757-
cmp #$78
771+
cmp #$78 ;high byte min
758772
bcc @allegroClear
759773
bne @levelLag
760774
lda cycleCount+1
761-
cmp #$9D
775+
cmp #$9D;low byte min
762776
bcc @allegroClear
763777
@levelLag:
764778
lda #$01
765779
sta lagFlag
766-
;linelag at 31072 7960
780+
;linelag at 31072 = 0x7960
767781
lda cycleCount
768-
cmp #$79
782+
cmp #$79;high byte min
769783
bcc @allegroClear
770784
bne @lineLag
771785
lda cycleCount+1
772-
cmp #$60
786+
cmp #$60;low byte min
773787
bcc @allegroClear
774788
@lineLag:
775789
lda #$03
776790
sta lagFlag
777791
;confettiB at 31327-31755 7A5F-7C0B
778792
lda cycleCount
779-
cmp #$7A
793+
cmp #$7A ;high byte min
780794
bcc @allegroClear
781795
bne @not7A
782796
lda cycleCount+1
783-
cmp #$5F
797+
cmp #$5F ;low byte min
784798
bcc @allegroClear
785799
bcs @confettiB
786-
@not7A: cmp #$7C
800+
@not7A: cmp #$7C ;high byte max
787801
bcc @confettiB
788802
bne @allegroClear
789803
lda cycleCount+1
790-
cmp #$0C
804+
cmp #$0C ;low byte max
791805
bcs @allegroClear
792806
@confettiB:
793-
lda #$D0
807+
lda #$D0 ;D0 = infinite confetti
794808
sta crashFlag
795809
jmp confettiHandler
796810
@allegroClear:
797-
lda #$00
811+
lda #$00 ;reset allegro flag and return to program execution, no crash
798812
sta allegroIndex
799813
lda lagFlag
800-
beq @noLag
814+
beq @noLag ;if lag should happen, wait a frame here so that sprite staging doesn't happen.
801815
lda #$00
802816
sta verticalBlankingInterval
803817
@checkForNmi:
804-
lda verticalBlankingInterval
818+
lda verticalBlankingInterval ;busyloop
805819
beq @checkForNmi
806820
@noLag: rts
807821
@crashGraphics:
808822
lda #$00
809-
sta allegroIndex
823+
sta allegroIndex ; resetting variable
810824
lda crashMode
811825
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
813827
ora #$04
814828
sta outOfDateRenderFlags
815829
lda #$02
816-
sta soundEffectSlot0Init
830+
sta soundEffectSlot0Init ; play topout sfx
817831
rts
818832
@otherMode:
819-
cmp #CRASH_CRASH
833+
cmp #CRASH_CRASH ;if crash mode, crash
820834
bcc @topout
821835
bne @allegroClear
822-
.byte 02
836+
.byte 02 ; stp
823837
@topout:
824-
lda #LINECAP_HALT
838+
lda #LINECAP_HALT ;if topout, activate linecap
825839
sta linecapState
826840
rts
827841
factorTable:
828-
.byte $53, $88, $7D, $7D, $7D
842+
.byte $53, $88, $7D, $7D, $7D ;0 single double triple tetris
829843
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
831845
switchTable:
832846
.byte $3C, $77, $3C, $65, $3C, $66, $3C;60 119 60 101 60 102 60 gets read in reverse
833847
confettiHandler:
@@ -837,31 +851,69 @@ confettiHandler:
837851
lda heldButtons_player1
838852
and #$A0 ; A, Select
839853
bne @endConfetti
840-
lda frameCounter
854+
lda frameCounter ;use framecounter for Y coordinate of text, like original confetti but without the offset
841855
cmp #$FF
842856
bne @drawConfetti
843857
lda heldButtons_player1
844858
and #$47 ; B, Down, Left, Right
845859
beq @endConfetti
846860
@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
849863
sta spriteXOffset
850-
lda #$19
864+
lda #$19 ;ID for "confetti" text
851865
sta spriteIndexInOamContentLookup
852-
jsr stringSpriteAlignRight
866+
jsr stringSpriteAlignRight ;draw to screen
853867
lda #$00
854-
sta verticalBlankingInterval
868+
sta verticalBlankingInterval ;wait until next frame
855869
@checkForNmi:
856-
lda verticalBlankingInterval
870+
lda verticalBlankingInterval ;busyloop
857871
beq @checkForNmi
858872
jmp confettiHandler
859873
@infiniteConfetti:
860874
lda heldButtons_player1
861-
adc #$80
875+
adc #$80 ; loading 80 as Y coordinate of confetti text if nothing is held.
862876
cmp #$80
863-
beq @drawConfetti
877+
beq @drawConfetti ; if any button is pressed, exit confetti
864878
@endConfetti:
865879
lda #$00
866880
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

Comments
 (0)