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