Optimize ADC and SBC instructions

develop
Christophe Parent 2024-06-01 08:50:42 -07:00
parent 4b63ee3b1b
commit 749556d1b9
1 changed files with 47 additions and 89 deletions

View File

@ -1802,8 +1802,7 @@ EndGameText:
dex ; are we printing the world/lives display?
bne CheckPlayerName ; if not, branch to check player's name
lda NumberofLives ; otherwise, check number of lives
clc ; and increment by one for display
adc #$01
adc #$01 ; and increment by one for display (clc optimization)
cmp #10 ; more than 9 lives?
bcc PutLives
sbc #10 ; if so, subtract 10 and put a crown tile
@ -2175,8 +2174,7 @@ SaveHAdder:
lda #$00
sta $05 ; initialize temp high byte
lda $02 ; get vertical high nybble offset used in block buffer routine
clc
adc #$20 ; add 32 pixels for the status bar
adc #$20 ; add 32 pixels for the status bar (clc optimization)
asl
rol $05 ; shift and rotate d7 onto d0 and d6 into carry
asl
@ -4602,9 +4600,8 @@ GetAreaDataAddrs:
lda AreaPointer ; mask out all but 5 LSB
and #%00011111
sta AreaAddrsLOffset ; save as low offset
lda EnemyAddrHOffsets,y ; load base value with 2 altered MSB,
clc ; then add base value to 5 LSB, result
adc AreaAddrsLOffset ; becomes offset for level data
lda EnemyAddrHOffsets,y ; load base value with 2 altered MSB, then add base value to 5 LSB,
adc AreaAddrsLOffset ; result becomes offset for level data (clc optimization)
tay
lda EnemyDataAddrLow,y ; use offset to load pointer
sta EnemyDataLow
@ -6576,9 +6573,8 @@ RghtFrict:
XSpdSign:
cmp #$00 ; if player not moving or moving to the right,
bpl SetAbsSpd ; branch and leave horizontal speed value unmodified
eor #$ff
clc ; otherwise get two's compliment to get absolute
adc #$01 ; unsigned walking/running speed
eor #$ff ; otherwise get two's compliment to get absolute
adc #$01-1 ; unsigned walking/running speed (clc optimization)
SetAbsSpd:
sta Player_XSpeedAbsolute ; store walking/running speed here and leave
rts
@ -6869,8 +6865,7 @@ WhirlpoolActivate:
lsr ; shift d0 into carry (to run on every other frame)
bcc WhPull ; if d0 not set, branch to last part of code
lda $01 ; get center
sec
sbc Player_X_Position ; subtract player's horizontal coordinate
sbc Player_X_Position ; subtract player's horizontal coordinate (sec optimization)
lda $00 ; get page location of center
sbc Player_PageLoc ; subtract borrow
bpl LeftWh ; if player to the left of center, branch
@ -6886,8 +6881,7 @@ LeftWh:
lsr ; shift d0 into carry
bcc WhPull ; if d0 not set, branch
lda Player_X_Position ; otherwise slowly pull player right, towards the center
clc
adc #$01 ; add one pixel
adc #$01-1 ; add one pixel (clc optimization)
sta Player_X_Position ; set player's new horizontal coordinate
lda Player_PageLoc
adc #$00 ; add carry
@ -7142,8 +7136,7 @@ FireCannon:
lda Cannon_X_Position,y ; get horizontal coordinate of cannon
sta Enemy_X_Position,x ; save as horizontal coordinate of bullet bill
lda Cannon_Y_Position,y ; get vertical coordinate of cannon
sec
sbc #$08 ; subtract eight pixels (because enemies are 24 pixels tall)
sbc #$08-1 ; subtract eight pixels (because enemies are 24 pixels tall) (sec optimization)
sta Enemy_Y_Position,x ; save as vertical coordinate of bullet bill
lda #$01
sta Enemy_Y_HighPos,x ; set vertical high byte of bullet bill
@ -7269,8 +7262,7 @@ ProcHammerObj:
beq SetHSpd ; if currently at 2, branch
bcs SetHPos ; if greater than 2, branch elsewhere
txa
clc ; add 13 bytes to use
adc #$0d ; proper misc object
adc #$0d ; add 13 bytes to use proper misc object (clc optimization)
tax ; return offset to X
lda #$10
sta $00 ; set downward movement force
@ -7295,8 +7287,7 @@ SetHSpd:
SetHPos:
dec Misc_State,x ; decrement hammer's state
lda Enemy_X_Position,y ; get enemy's horizontal position
clc
adc #$02 ; set position 2 pixels to the right
adc #$02-1 ; set position 2 pixels to the right (clc optimization)
sta Misc_X_Position,x ; store as hammer's horizontal position
lda Enemy_PageLoc,y ; get enemy's page location
adc #$00 ; add carry
@ -8723,8 +8714,7 @@ CreateSpiny:
lda #$01 ; put spiny within vertical screen unit
sta Enemy_Y_HighPos,x
lda Enemy_Y_Position,y ; put spiny eight pixels above where lakitu is
sec
sbc #$08
sbc #$08 ; (sec optimization)
sta Enemy_Y_Position,x
lda PseudoRandomBitReg,x ; get 2 LSB of LSFR and save to Y
and #%00000011
@ -8856,8 +8846,7 @@ MaxCC:
asl ; otherwise, multiply A by 2
GSeed:
pha ; save to stack
clc
adc $00 ; add to last two bits of LSFR we saved earlier
adc $00 ; add to last two bits of LSFR we saved earlier (clc optimization)
sta $00 ; save it there
lda PseudoRandomBitReg+1,x
and #%00000011 ; if neither of the last two bits of second LSFR set,
@ -8867,8 +8856,7 @@ GSeed:
sta $00 ; third LSFR part
RSeed:
pla ; get value from stack we saved earlier
clc
adc $01 ; add to last two bits of LSFR we saved in other place
adc $01 ; add to last two bits of LSFR we saved in other place (clc optimization)
tay ; use as pseudorandom offset here
lda FlyCCXSpeedData,y ; get horizontal speed using pseudorandom offset
sta Enemy_X_Speed,x
@ -8881,9 +8869,8 @@ RSeed:
and #%00000010
beq D2XPos1 ; if d1 not set, branch
lda Enemy_X_Speed,x
eor #$ff ; if d1 set, change horizontal speed
clc ; into two's compliment, thus moving in the opposite
adc #$01 ; direction
eor #$ff ; if d1 set, change horizontal speed into two's compliment,
adc #$01 ; thus moving in the opposite direction (clc optimization)
sta Enemy_X_Speed,x
inc Enemy_MovingDir,x ; increment to move towards the left
D2XPos1:
@ -9006,8 +8993,7 @@ PutAtRightExtent:
SpawnFromMouth:
lda Enemy_X_Position,y ; get bowser's horizontal position
sec
sbc #$0e ; subtract 14 pixels
sbc #$0e ; subtract 14 pixels (sec optimization)
sta Enemy_X_Position,x ; save as flame's horizontal position
lda Enemy_PageLoc,y
sta Enemy_PageLoc,x ; copy page location from bowser to flame
@ -9061,9 +9047,8 @@ StarFChk:
lda Enemy_ID,y ; check for presence of star flag object
cmp #StarFlagObject ; if there isn't a star flag object,
bne StarFChk ; routine goes into infinite loop = crash
lda Enemy_X_Position,y
sec ; get horizontal coordinate of star flag object, then
sbc #$30 ; subtract 48 pixels from it and save to
lda Enemy_X_Position,y ; get horizontal coordinate of star flag object, then
sbc #$30 ; subtract 48 pixels from it and save to (sec optimization)
pha ; the stack
lda Enemy_PageLoc,y
sbc #$00 ; subtract the carry from the page location
@ -9182,8 +9167,7 @@ FireBulletBill:
HandleGroupEnemies:
ldy #$00 ; load value for green koopa troopa
sec
sbc #$37 ; subtract $37 from second byte read
sbc #$37-1 ; subtract $37 from second byte read (sec optimization)
pha ; save result in stack for now
cmp #$04 ; was byte in $3b-$3e range?
bcs SnglID ; if so, branch
@ -9227,8 +9211,7 @@ GSltLp:
sta Enemy_PageLoc,x ; store page location for enemy object
lda $03
sta Enemy_X_Position,x ; store x coordinate for enemy object
clc
adc #$18 ; add 24 pixels for next enemy
adc #$18 ; add 24 pixels for next enemy (clc optimization)
sta $03
lda $02 ; add carry to page location for
adc #$00 ; next enemy
@ -9254,8 +9237,7 @@ InitPiranhaPlant:
sta PiranhaPlant_MoveFlag,x ; be used as vertical speed, but not in this case
lda Enemy_Y_Position,x
sta PiranhaPlantDownYPos,x ; save original vertical coordinate here
sec
sbc #$18
sbc #$18 ; (sec optimization)
sta PiranhaPlantUpYPos,x ; save original vertical coordinate - 24 pixels here
lda #$09
jmp SetBBox2 ; set specific value for bounding box control
@ -10000,8 +9982,7 @@ ProcSwimmingB:
pla ; pull 3 LSB of frame counter from the stack
bne BSwimE ; branch to leave, execute code only every eighth frame
lda Enemy_Y_MoveForce,x
clc ; add to movement force to speed up swim
adc #$01
adc #$01 ; add to movement force to speed up swim (clc optimization)
sta Enemy_Y_MoveForce,x ; set movement force
sta BlooperMoveSpeed,x ; set as movement speed
cmp #$02
@ -10014,8 +9995,7 @@ SlowSwim:
pla ; pull 3 LSB of frame counter from the stack
bne NoSSw ; branch to leave, execute code only every eighth frame
lda Enemy_Y_MoveForce,x
sec ; subtract from movement force to slow swim
sbc #$01
sbc #$01 ; subtract from movement force to slow swim (sec optimization)
sta Enemy_Y_MoveForce,x ; set movement force
sta BlooperMoveSpeed,x ; set as movement speed
bne NoSSw ; if any speed, branch to leave
@ -10195,8 +10175,7 @@ SusFbar:
cmp #$18
bne SetupGFB ; if not at twenty-four branch to not change
SkpFSte:
clc
adc #$01 ; add one to spinning thing to avoid horizontal state
adc #$01-1 ; add one to spinning thing to avoid horizontal state (clc optimization)
sta FirebarSpinState_High,x
SetupGFB:
sta $ef ; save high byte of spinning thing, modified or otherwise
@ -10259,8 +10238,7 @@ AddHA:
sbc $06 ; original one and skip this part
jmp ChkFOfs
SubtR1:
sec ; subtract original X from the
sbc Enemy_Rel_XPos ; current sprite X
sbc Enemy_Rel_XPos ; subtract original X from the current sprite X (sec optimization)
ChkFOfs:
cmp #$59 ; if difference of coordinates within a certain range,
bcc VAHandl ; continue by handling vertical adder
@ -10321,8 +10299,7 @@ ChkVFBD:
cmp #$f0 ; because, really, what's the point?
bcs Chk2Ofs
lda Sprite_X_Position+4 ; get OAM X coordinate for sprite #1
clc
adc #$04 ; add four pixels
adc #$04 ; add four pixels (clc optimization)
sta $04 ; store here
sec ; subtract horizontal coordinate of firebar
sbc $06 ; from the X coordinate of player's sprite 1
@ -10371,33 +10348,28 @@ GetFirebarPosition:
cmp #$09
bcc GetHAdder ; if lower than $09, branch ahead
eor #%00001111 ; otherwise get two's compliment to oscillate
clc
adc #$01
adc #$01-1 ; (clc optimization)
GetHAdder:
sta $01 ; store result, modified or not, here
ldy $00 ; load number of firebar ball where we're at
lda FirebarTblOffsets,y ; load offset to firebar position data
clc
adc $01 ; add oscillated high byte of spinstate
adc $01 ; add oscillated high byte of spinstate (clc optimization)
tay ; to offset here and use as new offset
lda FirebarPosLookupTbl,y ; get data here and store as horizontal adder
sta $01
pla ; pull whatever was in A from the stack
pha ; save it again because we still need it
clc
adc #$08 ; add eight this time, to get vertical adder
adc #$08 ; add eight this time, to get vertical adder (clc optimization)
and #%00001111 ; mask out high nybble
cmp #$09 ; if lower than $09, branch ahead
bcc GetVAdder
eor #%00001111 ; otherwise get two's compliment
clc
adc #$01
adc #$01-1 ; (clc optimization)
GetVAdder:
sta $02 ; store result here
ldy $00
lda FirebarTblOffsets,y ; load offset to firebar position data again
clc
adc $02 ; this time add value in $02 to offset here and use as offset
adc $02 ; this time add value in $02 to offset here and use as offset (clc optimization)
tay
lda FirebarPosLookupTbl,y ; get data here and store as vertica adder
sta $02
@ -10444,8 +10416,7 @@ AddCCF:
cmp #$08 ; if result or two's compliment greater than eight,
bcs BPGet ; skip to the end without changing movement force
lda Enemy_Y_MoveForce,x
clc
adc #$10 ; otherwise add to it
adc #$10 ; otherwise add to it (clc optimization)
sta Enemy_Y_MoveForce,x
lsr ; move high nybble to low again
lsr
@ -11246,8 +11217,7 @@ ChkToMoveBalPlat:
lda PlatformCollisionFlag,x ; get collision flag
bpl ColFlg ; branch if collision
lda Enemy_Y_MoveForce,x
clc ; add $05 to contents of moveforce, whatever they be
adc #$05
adc #$05 ; add $05 to contents of moveforce, whatever they be (clc optimization)
sta $00 ; store here
lda Enemy_Y_Speed,x
adc #$00 ; add carry to vertical speed
@ -11370,8 +11340,7 @@ GetLRp:
pla ; get second/third copy of vertical speed from stack
bpl GetHRp ; skip this part if moving downwards or not at all
txa
clc
adc #$08 ; add eight to vertical coordinate and
adc #$08 ; add eight to vertical coordinate (clc optimization) and
tax ; save as X
GetHRp:
txa ; move vertical coordinate to A
@ -11780,8 +11749,7 @@ PlayerHammerCollision:
sta Misc_Collision_Flag,x ; otherwise set collision flag now
lda Misc_X_Speed,x
eor #$ff ; get two's compliment of
clc ; hammer's horizontal speed
adc #$01
adc #$01-1 ; hammer's horizontal speed (clc optimization)
sta Misc_X_Speed,x ; set to send hammer flying the opposite direction
lda StarInvincibleTimer ; if star mario invincibility timer set,
bne ExPHC ; branch to leave
@ -11942,8 +11910,7 @@ ChkInj:
cmp #Bloober
bcc ChkETmrs
lda Player_Y_Position ; add 12 pixels to player's vertical position
clc
adc #$0c
adc #$0c-1 ; (clc optimization)
cmp Enemy_Y_Position,x ; compare modified player's position to enemy's position
bcc EnemyStomped ; branch if this player's position above (less than) enemy's
ChkETmrs:
@ -12066,8 +12033,7 @@ HandleStompedShellE:
sta Enemy_State,x
inc StompChainCounter ; increment the stomp counter
lda StompChainCounter ; add whatever is in the stomp counter
clc ; to whatever is in the stomp timer
adc StompTimer
adc StompTimer ; to whatever is in the stomp timer (clc optimization)
jsr SetupFloateyNumber ; award points accordingly
inc StompTimer ; increment stomp timer of some sort
ldy PrimaryHardMode ; check primary hard mode flag
@ -12334,8 +12300,7 @@ ChkSmallPlatLoop:
MoveBoundBox:
lda BoundingBox_UL_YPos,y ; move bounding box vertical coordinates
clc ; 128 pixels downwards
adc #$80
adc #$80 ; 128 pixels downwards (clc optimization)
sta BoundingBox_UL_YPos,y
lda BoundingBox_DR_YPos,y
clc
@ -12353,9 +12318,8 @@ ProcSPlatCollisions:
ldx ObjectOffset ; return enemy object buffer offset to X, then continue
ProcLPlatCollisions:
lda BoundingBox_DR_YPos,y ; get difference by subtracting the top
sec ; of the player's bounding box from the bottom
sbc BoundingBox_UL_YPos ; of the platform's bounding box
lda BoundingBox_DR_YPos,y ; get difference by subtracting the top of the player's bounding box
sbc BoundingBox_UL_YPos ; from the bottom of the platform's bounding box (sec optimization)
cmp #$04 ; if difference too large or negative,
bcs ChkForTopCollision ; branch, do not alter vertical speed of player
lda Player_Y_Speed ; check to see if player's vertical speed is moving down
@ -12426,8 +12390,7 @@ PositionPlayerOnVPlat:
ldy Enemy_Y_HighPos,x
cpy #$01 ; if vertical high byte offscreen, skip this
bne ExPlPos
sec ; subtract 32 pixels from vertical coordinate
sbc #$20 ; for the player object's height
sbc #$20 ; subtract 32 pixels from vertical coordinate for the player object's height (sec optimization)
sta Player_Y_Position ; save as player's new vertical coordinate
tya
sbc #$00 ; subtract borrow and store as player's
@ -13292,8 +13255,7 @@ EnemyJump:
jsr SubtEnemyYPos ; do a sub here
bcc DoSide ; if enemy vertical coord + 62 < 68, branch to leave
lda Enemy_Y_Speed,x
clc ; add two to vertical speed
adc #$02
adc #$02-1 ; add two to vertical speed (clc optimization)
cmp #$03 ; if green paratroopa not falling, branch ahead
bcc DoSide
jsr ChkUnderEnemy ; otherwise, check to see if green paratroopa is
@ -13645,8 +13607,7 @@ BlockBufferChk_Enemy:
BlockBufferChk_FBall:
ldy #$1a ; set offset for block buffer adder data
txa
clc
adc #$07 ; add seven bytes to use
adc #$07-1 ; add seven bytes to use (clc optimization)
tax
lda #$00 ; set A to return vertical coordinate
BBChk_E:
@ -14085,8 +14046,7 @@ JCoinGfxHandler:
bcs DrawFloateyNumber_Coin ; branch to draw floatey number
lda Misc_Y_Position,x ; store vertical coordinate as
sta Sprite_Y_Position,y ; Y coordinate for first sprite
clc
adc #$08 ; add eight pixels
adc #$08 ; add eight pixels (clc optimization)
sta Sprite_Y_Position+4,y ; store as Y coordinate for second sprite
lda Misc_Rel_XPos ; get relative horizontal coordinate
sta Sprite_X_Position,y
@ -14386,8 +14346,7 @@ ChkRearSte:
and #%00100000 ; if bowser not defeated, do not set flag
beq DrawBowser
lda $02 ; subtract 16 pixels from
sec ; saved vertical coordinate
sbc #$10
sbc #$10 ; saved vertical coordinate (sec optimization)
sta $02
jmp FlipBowserOver ; jump to set vertical flip flag
@ -14561,9 +14520,8 @@ CheckForVerticalFlip:
cmp #$15
bcs FlipEnemyVertically ; also branch if enemy object => $15
txa
clc
adc #$08 ; if not selected objects or => $15, set
tax ; offset in X for next row
tax ; offset in X for next row (clc optimization)
FlipEnemyVertically:
lda Sprite_Tilenumber,x ; load first or second row tiles