Optimize subroutine tail calls

refactor
Christophe Parent 2024-02-07 23:12:51 -08:00
parent acc90317a8
commit f7793b12e4
1 changed files with 42 additions and 52 deletions

View File

@ -1288,7 +1288,7 @@ ExitMsgs:
PlayerEndWorld:
lda WorldEndTimer ; check to see if world end timer expired
bne EndExitOne ; branch to leave if not
bne EndExit ; branch to leave if not
ldy WorldNumber ; check world number
cpy #World8 ; if on world 8, player is done with game,
bcs EndChkBButton ; thus branch to read controller
@ -1300,20 +1300,18 @@ PlayerEndWorld:
inc FetchNewGameTimerFlag ; set flag to load game timer from header
lda #GameModeValue
sta OperMode ; set mode of operation to game mode
EndExitOne:
EndExit:
rts ; and leave
EndChkBButton:
lda SavedJoypad1Bits
ora SavedJoypad2Bits ; check to see if B button was pressed on
and #B_Button ; either controller
beq EndExitTwo ; branch to leave if not
beq EndExit ; branch to leave if not
lda #$01 ; otherwise set world selection flag
sta WorldSelectEnableFlag
lda #$ff ; remove onscreen player's lives
sta NumberofLives
jsr TerminateGame ; do sub to continue other player or end game
EndExitTwo:
rts ; leave
jmp TerminateGame ; continue other player or end game
;-------------------------------------------------------------------------------------
@ -1342,7 +1340,7 @@ ScoreUpdateData:
FloateyNumbersRoutine:
lda FloateyNum_Control,x ; load control for floatey number
beq EndExitOne ; if zero, branch to leave
beq EndExit ; if zero, branch to leave
cmp #$0b ; if less than $0b, branch
bcc ChkNumTimer
lda #$0b ; otherwise set to $0b, thus keeping
@ -2133,8 +2131,7 @@ WriteBlankMT:
rts
ReplaceBlockMetatile:
jsr WriteBlockMetatile ; write metatile to vram buffer to replace block object
rts ; leave
jmp WriteBlockMetatile ; write metatile to vram buffer to replace block object
DestroyBlockMetatile:
lda #$00 ; force blank metatile if branched/jumped to this point
@ -3193,7 +3190,7 @@ DoAPTasks:
jsr AreaParserTasks
dec AreaParserTaskNum ; if all tasks not complete do not
bne SkipATRender ; render attribute table yet
jsr RenderAttributeTables
jmp RenderAttributeTables
SkipATRender:
rts
@ -5628,7 +5625,7 @@ UpdScrollVar:
lda #$00 ; reset vram buffer offset used in conjunction with
sta VRAM_Buffer2_Offset ; level graphics buffer at $0341-$035f
RunParser:
jsr AreaParserTaskHandler ; update the name table with more level graphics
jmp AreaParserTaskHandler ; update the name table with more level graphics
ExitEng:
rts ; and after all that, we're finally done!
@ -6010,8 +6007,7 @@ EnterSidePipe:
tay ; and nullify controller bit override here
RightPipe:
tya ; use contents of Y to
jsr AutoControlPlayer ; execute player control routine with ctrl bits nulled
rts
jmp AutoControlPlayer ; execute player control routine with ctrl bits nulled
;-------------------------------------------------------------------------------------
@ -6023,9 +6019,9 @@ PlayerChangeSize:
EndChgSize:
cmp #$c4 ; check again for another specific moment
bne ExitChgSize ; and branch to leave if before or after that point
jsr DonePlayerTask ; otherwise do sub to init timer control and set routine
jmp DonePlayerTask ; otherwise do sub to init timer control and set routine
ExitChgSize:
rts ; and then leave
rts ; leave
;-------------------------------------------------------------------------------------
@ -6975,7 +6971,7 @@ GiveFPScr:
FPGfx:
jsr GetEnemyOffscreenBits ; get offscreen information
jsr RelativeEnemyPosition ; get relative coordinates
jsr FlagpoleGfxHandler ; draw flagpole flag and floatey number
jmp FlagpoleGfxHandler ; draw flagpole flag and floatey number
ExitFlagP:
rts
@ -7237,8 +7233,7 @@ RunBBSubs:
jsr PlayerEnemyCollision ; handle player to enemy collisions
jmp EnemyGfxHandler ; draw the bullet bill and leave
KillBB:
jsr EraseEnemyObject ; kill bullet bill and leave
rts
jmp EraseEnemyObject ; kill bullet bill
;-------------------------------------------------------------------------------------
@ -7335,8 +7330,7 @@ RunHSubs:
jsr GetMiscOffscreenBits ; get offscreen information
jsr RelativeMiscPosition ; get relative coordinates
jsr GetMiscBoundBox ; get bounding box coordinates
jsr DrawHammer ; draw the hammer
rts ; and we are done here
jmp DrawHammer ; draw the hammer
;-------------------------------------------------------------------------------------
; $02 - used to store vertical high nybble offset from block buffer routine
@ -7592,9 +7586,9 @@ RunPUSubs:
jsr GetEnemyBoundBox ; get bounding box coordinates
jsr DrawPowerUp ; draw the power-up object
jsr PlayerEnemyCollision ; check for collision with player
jsr OffscreenBoundsCheck ; check to see if it went offscreen
jmp OffscreenBoundsCheck ; check to see if it went offscreen
ExitPUp:
rts ; and we're done
rts
;-------------------------------------------------------------------------------------
; These apply to all routines in this section unless otherwise noted:
@ -7752,7 +7746,7 @@ ExtraLifeMushBlock:
VineBlock:
ldx #$05 ; load last slot for enemy object buffer
ldy SprDataOffset_Ctrl ; get control bit
jsr Setup_Vine ; set up vine object
jmp Setup_Vine ; set up vine object
ExitBlockChk:
rts ; leave
@ -7811,7 +7805,7 @@ CheckTopOfBlock:
sta ($06),y ; otherwise put blank metatile where coin was
jsr RemoveCoin_Axe ; write blank metatile to vram buffer
ldx SprDataOffset_Ctrl ; get control bit
jsr SetupJumpCoin ; create jumping coin object and update coin variables
jmp SetupJumpCoin ; create jumping coin object and update coin variables
TopEx:
rts ; leave!
@ -8430,9 +8424,9 @@ StrFre:
InitEnemyObject:
lda #$00 ; initialize enemy state
sta Enemy_State,x
jsr CheckpointEnemyID ; jump ahead to run jump engine and subroutines
jmp CheckpointEnemyID ; jump ahead to run jump engine and subroutines
ExEPar:
rts ; then leave
rts ; leave
DoGroup:
jmp HandleGroupEnemies ; handle enemy group objects
@ -9854,7 +9848,7 @@ ChkKillGoomba:
lda Enemy_ID,x
cmp #Goomba ; check for goomba object
bne NKGmba ; branch if not found
jsr EraseEnemyObject ; otherwise, kill this goomba object
jmp EraseEnemyObject ; otherwise, kill this goomba object
NKGmba:
rts ; leave!
@ -10991,8 +10985,7 @@ SetupExpl:
sta Fireball_Rel_XPos
ldy Enemy_SprDataOffset,x ; get OAM data offset
lda ExplosionGfxCounter,x ; get explosion graphics counter
jsr DrawExplosion_Fireworks ; do a sub to draw the explosion then leave
rts
jmp DrawExplosion_Fireworks ; do a sub to draw the explosion
FireworksSoundScore:
lda #$00 ; disable enemy buffer flag
@ -11501,7 +11494,7 @@ YMDown:
ChkYPCollision:
lda PlatformCollisionFlag,x ; if collision flag not set here, branch
bmi ExYPl ; to leave
jsr PositionPlayerOnVPlat ; otherwise position player appropriately
jmp PositionPlayerOnVPlat ; otherwise position player appropriately
ExYPl:
rts ; leave
@ -11530,9 +11523,9 @@ PPHSubt:
SetPVar:
sta Player_PageLoc ; save result to player's page location
sty Platform_X_Scroll ; put saved value from second sub here to be used later
jsr PositionPlayerOnVPlat ; position player vertically and appropriately
jmp PositionPlayerOnVPlat ; position player vertically and appropriately
ExXMP:
rts ; and we are done here
rts ; we are done here
;--------------------------------
@ -11540,7 +11533,7 @@ DropPlatform:
lda PlatformCollisionFlag,x ; if no collision between platform and player
bmi ExDPl ; occurred, just leave without moving anything
jsr MoveDropPlatform ; otherwise do a sub to move platform down very quickly
jsr PositionPlayerOnVPlat ; do a sub to position player appropriately
jmp PositionPlayerOnVPlat ; do a sub to position player appropriately
ExDPl:
rts ; leave
@ -11552,9 +11545,9 @@ RightPlatform:
bmi ExRPl ; and platform, branch ahead, leave speed unaltered
lda #$10
sta Enemy_X_Speed,x ; otherwise set new speed (gets moving if motionless)
jsr PositionPlayerOnHPlat ; use saved value from earlier sub to position player
jmp PositionPlayerOnHPlat ; use saved value from earlier sub to position player
ExRPl:
rts ; then leave
rts ; leave
;--------------------------------
@ -11581,9 +11574,9 @@ MoveLiftPlatforms:
ChkSmallPlatCollision:
lda PlatformCollisionFlag,x ; get bounding box counter saved in collision flag
beq ExLiftP ; if none found, leave player position alone
jsr PositionPlayerOnS_Plat ; use to position player correctly
jmp PositionPlayerOnS_Plat ; use to position player correctly
ExLiftP:
rts ; then leave
rts ; leave
;-------------------------------------------------------------------------------------
; $00 - page location of extended left boundary
@ -11637,7 +11630,7 @@ ExtendLB:
cpy #JumpspringObject ; if jumpspring, do not erase
beq ExScrnBd ; erase all others too far to the right
TooFar:
jsr EraseEnemyObject ; erase object if necessary
jmp EraseEnemyObject ; erase object if necessary
ExScrnBd:
rts ; leave
@ -11871,7 +11864,7 @@ UpToSuper:
UpToFiery:
ldy #$00 ; set value to be used as new player state
jsr SetPRout ; set values to stop certain things in motion
jmp SetPRout ; set values to stop certain things in motion
NoPUp:
rts
@ -11966,7 +11959,7 @@ HandlePECollisions:
bcs KSPts ; data obtained from the stomp counter + 3
lda KickedShellPtsData,y ; otherwise, set points based on proximity to timer expiration
KSPts:
jsr SetupFloateyNumber ; set values for floatey number now
jmp SetupFloateyNumber ; set values for floatey number now
ExPEC:
rts ; leave!!!
@ -12746,7 +12739,7 @@ ChkGERtn:
; $06-$07 - block buffer address
StopPlayerMove:
jsr ImpedePlayerMove ; stop player's movement
jmp ImpedePlayerMove ; stop player's movement
ExCSM:
rts ; leave
@ -13190,9 +13183,9 @@ SetForStn:
sta EnemyIntervalTimer,x ; set timer here
lda #$03 ; set state here, apparently used to render
sta Enemy_State,x ; upside-down koopas and buzzy beetles
jsr EnemyLanding ; then land it properly
jmp EnemyLanding ; then land it properly
ExSteChk:
rts ; then leave
rts ; leave
ProcEnemyDirection:
lda Enemy_ID,x ; check enemy identifier for goomba
@ -13918,7 +13911,7 @@ RenderH:
lda #$00
sta Misc_State,x ; otherwise nullify misc object state
lda #$f8
jsr DumpTwoSpr ; do sub to move hammer sprites offscreen
jmp DumpTwoSpr ; do sub to move hammer sprites offscreen
NoHOffscr:
rts ; leave
@ -14092,7 +14085,7 @@ SLChk:
lda Enemy_OffscreenBits ; check d7 of offscreen bits
asl ; and if d7 is not set, skip sub
bcc ExDLPl
jsr MoveSixSpritesOffscreen ; otherwise branch to move all sprites offscreen
jmp MoveSixSpritesOffscreen ; otherwise branch to move all sprites offscreen
ExDLPl:
rts
@ -14760,7 +14753,7 @@ AllRowC:
lda Enemy_Y_HighPos,x ; check high byte of vertical position
cmp #$02 ; if not yet past the bottom of the screen, branch
bne ExEGHandler
jsr EraseEnemyObject ; what it says
jmp EraseEnemyObject ; what it says
ExEGHandler:
rts
@ -16305,8 +16298,7 @@ ContinueGrowItems:
lda #$9d ; load contents of other reg directly
sta SND_SQUARE2_REG
lda PUp_VGrow_FreqData,y ; use secondary counter / 2 as offset for frequency regs
jsr SetFreq_Squ2
rts
jmp SetFreq_Squ2
StopGrowItems:
jmp EmptySfx2Buffer ; branch to stop playing sounds
@ -17235,8 +17227,7 @@ LoopCopyChars:
dex
bne LoopCopyChars ; repeat until we've copied enough pages
ldx #$00
jsr BankSwitch
rts
jmp BankSwitch
; The sound engine is located in Bank 2; we switch back to Bank 0 when we are done
SoundEngineBankSwitch:
@ -17244,8 +17235,7 @@ SoundEngineBankSwitch:
jsr BankSwitch
jsr SoundEngine
ldx #$00
jsr BankSwitch
rts
jmp BankSwitch
BankSwitch:
lda BankTable,x ; remap from the convenient banks numbered 0 through 2 to the value needed by the hardware