ref: 45ed05decf330faab4503fe8fecadc54698c9724
parent: bbc257476f9aac8d04d75a8036d412caa8f7d515
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Tue Jul 7 07:10:29 EDT 2020
Use HIGH() and LOW()
--- a/constants/audio_constants.asm
+++ b/constants/audio_constants.asm
@@ -25,10 +25,10 @@
const Ch8 ; 7
; HW sound channel register base addresses
-HW_CH1_BASE EQU (rNR10 % $100)
-HW_CH2_BASE EQU ((rNR21 % $100) - 1)
-HW_CH3_BASE EQU (rNR30 % $100)
-HW_CH4_BASE EQU ((rNR41 % $100) - 1)
+HW_CH1_BASE EQU LOW(rNR10)
+HW_CH2_BASE EQU LOW(rNR21) - 1
+HW_CH3_BASE EQU LOW(rNR30)
+HW_CH4_BASE EQU LOW(rNR41) - 1
; HW sound channel enable bit masks
HW_CH1_ENABLE_MASK EQU %00010001
--- a/constants/battle_constants.asm
+++ b/constants/battle_constants.asm
@@ -35,6 +35,8 @@
FRZ EQU 5
PAR EQU 6
+MAX_STAT_VALUE EQU 999
+
; volatile statuses 1
STORING_ENERGY EQU 0 ; Bide
THRASHING_ABOUT EQU 1 ; e.g. Thrash
--- a/engine/battle/core.asm
+++ b/engine/battle/core.asm
@@ -4182,8 +4182,8 @@
; if the enemy has used Light Screen, double the enemy's special
sla c
rl b
-; reflect and light screen boosts do not cap the stat at 999, so weird things will happen during stats scaling if
-; a Pokemon with 512 or more Defense has used Reflect, or if a Pokemon with 512 or more Special has used Light Screen
+; reflect and light screen boosts do not cap the stat at MAX_STAT_VALUE, so weird things will happen during stats scaling
+; if a Pokemon with 512 or more Defense has used Reflect, or if a Pokemon with 512 or more Special has used Light Screen
.specialAttackCritCheck
ld hl, wBattleMonSpecial
ld a, [wCriticalHitOrOHKO]
@@ -4295,8 +4295,8 @@
; if the player has used Light Screen, double the player's special
sla c
rl b
-; reflect and light screen boosts do not cap the stat at 999, so weird things will happen during stats scaling if
-; a Pokemon with 512 or more Defense has used Reflect, or if a Pokemon with 512 or more Special has used Light Screen
+; reflect and light screen boosts do not cap the stat at MAX_STAT_VALUE, so weird things will happen during stats scaling
+; if a Pokemon with 512 or more Defense has used Reflect, or if a Pokemon with 512 or more Special has used Light Screen
.specialAttackCritCheck
ld hl, wEnemyMonSpecial
ld a, [wCriticalHitOrOHKO]
@@ -4508,14 +4508,14 @@
jr nz, .cap
ldh a, [hQuotient + 2]
- cp (MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1) / $100
+ cp HIGH(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1)
jr c, .dont_cap_2
- cp (MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1) / $100 + 1
+ cp HIGH(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1) + 1
jr nc, .cap
ldh a, [hQuotient + 3]
- cp (MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1) % $100
+ cp LOW(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1)
jr nc, .cap
.dont_cap_2
@@ -4533,21 +4533,21 @@
jr c, .cap
ld a, [hl]
- cp (MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1) / $100
+ cp HIGH(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1)
jr c, .dont_cap_3
- cp (MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1) / $100 + 1
+ cp HIGH(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1) + 1
jr nc, .cap
inc hl
ld a, [hld]
- cp (MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1) % $100
+ cp LOW(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1)
jr c, .dont_cap_3
.cap
- ld a, (MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE) / $100
+ ld a, HIGH(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE)
ld [hli], a
- ld a, (MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE) % $100
+ ld a, LOW(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE)
ld [hld], a
.dont_cap_3
@@ -6526,14 +6526,14 @@
call Divide
pop hl
ldh a, [hDividend + 3]
- sub 999 % $100
+ sub LOW(MAX_STAT_VALUE)
ldh a, [hDividend + 2]
- sbc 999 / $100
+ sbc HIGH(MAX_STAT_VALUE)
jp c, .storeNewStatValue
-; cap the stat at 999
- ld a, 999 / $100
+; cap the stat at MAX_STAT_VALUE (999)
+ ld a, HIGH(MAX_STAT_VALUE)
ldh [hDividend + 2], a
- ld a, 999 % $100
+ ld a, LOW(MAX_STAT_VALUE)
ldh [hDividend + 3], a
.storeNewStatValue
ldh a, [hDividend + 2]
@@ -6573,7 +6573,7 @@
ret
; multiply stat at hl by 1.125
-; cap stat at 999
+; cap stat at MAX_STAT_VALUE
.applyBoostToStat
ld a, [hli]
ld d, a
@@ -6591,13 +6591,13 @@
adc d
ld [hli], a
ld a, [hld]
- sub 999 % $100
+ sub LOW(MAX_STAT_VALUE)
ld a, [hl]
- sbc 999 / $100
+ sbc HIGH(MAX_STAT_VALUE)
ret c
- ld a, 999 / $100
+ ld a, HIGH(MAX_STAT_VALUE)
ld [hli], a
- ld a, 999 % $100
+ ld a, LOW(MAX_STAT_VALUE)
ld [hld], a
ret
--- a/engine/battle/effects.asm
+++ b/engine/battle/effects.asm
@@ -399,11 +399,12 @@
inc d ; de = unmodified (original) stat
.checkIf999
pop bc
+ ; check if stat is already 999
ld a, [hld]
- sub 999 % $100 ; check if stat is already 999
+ sub LOW(MAX_STAT_VALUE)
jr nz, .recalculateStat
ld a, [hl]
- sbc 999 / $100
+ sbc HIGH(MAX_STAT_VALUE)
jp z, RestoreOriginalStatModifier
.recalculateStat ; recalculate affected stat
; paralysis and burn penalties, as well as badge boosts are ignored
@@ -431,15 +432,15 @@
ld b, $4
call Divide
pop hl
-; cap at 999
+; cap at MAX_STAT_VALUE (999)
ldh a, [hProduct + 3]
- sub 999 % $100
+ sub LOW(MAX_STAT_VALUE)
ldh a, [hProduct + 2]
- sbc 999 / $100
+ sbc HIGH(MAX_STAT_VALUE)
jp c, UpdateStat
- ld a, 999 / $100
+ ld a, HIGH(MAX_STAT_VALUE)
ldh [hMultiplicand + 1], a
- ld a, 999 % $100
+ ld a, LOW(MAX_STAT_VALUE)
ldh [hMultiplicand + 2], a
UpdateStat:
--- a/engine/gfx/mon_icons.asm
+++ b/engine/gfx/mon_icons.asm
@@ -236,7 +236,7 @@
; make a copy at wMonPartySpritesSavedOAM.
push af
ld c, $10
- ld h, wOAMBuffer / $100
+ ld h, HIGH(wOAMBuffer)
ldh a, [hPartyMonIndex]
swap a
ld l, a
--- a/engine/gfx/oam_dma.asm
+++ b/engine/gfx/oam_dma.asm
@@ -1,7 +1,7 @@
WriteDMACodeToHRAM::
; Since no other memory is available during OAM DMA,
; DMARoutine is copied to HRAM and executed there.
- ld c, hDMARoutine % $100
+ ld c, LOW(hDMARoutine)
ld b, DMARoutineEnd - DMARoutine
ld hl, DMARoutine
.copy
@@ -14,7 +14,7 @@
DMARoutine:
; initiate DMA
- ld a, wOAMBuffer / $100
+ ld a, HIGH(wOAMBuffer)
ldh [rDMA], a
; wait for DMA to finish
--- a/engine/gfx/sprite_oam.asm
+++ b/engine/gfx/sprite_oam.asm
@@ -18,7 +18,7 @@
.spriteLoop
ldh [hSpriteOffset2], a
- ld d, wSpriteStateData1 / $100
+ ld d, HIGH(wSpriteStateData1)
ldh a, [hSpriteOffset2]
ld e, a
ld a, [de] ; c1x0
@@ -79,7 +79,7 @@
ldh a, [hOAMBufferOffset]
ld e, a
- ld d, wOAMBuffer / $100
+ ld d, HIGH(wOAMBuffer)
.tileLoop
ldh a, [hSpriteScreenY] ; temp for sprite Y position
@@ -141,13 +141,13 @@
.nextSprite
ldh a, [hSpriteOffset2]
add $10
- cp $100 % $100
+ cp LOW($100)
jp nz, .spriteLoop
; Clear unused OAM.
ldh a, [hOAMBufferOffset]
ld l, a
- ld h, wOAMBuffer / $100
+ ld h, HIGH(wOAMBuffer)
ld de, $4
ld b, $a0
ld a, [wd736]
--- a/engine/items/inventory.asm
+++ b/engine/items/inventory.asm
@@ -12,10 +12,10 @@
push hl
push hl
ld d, PC_ITEM_CAPACITY ; how many items the PC can hold
- ld a, wNumBagItems & $FF
+ ld a, LOW(wNumBagItems)
cp l
jr nz, .checkIfInventoryFull
- ld a, wNumBagItems >> 8
+ ld a, HIGH(wNumBagItems)
cp h
jr nz, .checkIfInventoryFull
; if the destination is the bag
--- a/engine/link/cable_club.asm
+++ b/engine/link/cable_club.asm
@@ -255,9 +255,9 @@
ld hl, wEnemyMons + (SERIAL_PREAMBLE_BYTE - 1)
dec c
jr nz, .unpatchEnemyMonsLoop
- ld a, wEnemyMonOT % $100
+ ld a, LOW(wEnemyMonOT)
ld [wUnusedCF8D], a
- ld a, wEnemyMonOT / $100
+ ld a, HIGH(wEnemyMonOT)
ld [wUnusedCF8D + 1], a
xor a
ld [wTradeCenterPointerTableIndex], a
--- a/engine/movie/oak_speech/init_player_data.asm
+++ b/engine/movie/oak_speech/init_player_data.asm
@@ -23,9 +23,9 @@
START_MONEY EQU $3000
ld hl, wPlayerMoney + 1
- ld a, START_MONEY / $100
+ ld a, HIGH(START_MONEY)
ld [hld], a
- xor a
+ xor a ; LOW(START_MONEY)
ld [hli], a
inc hl
ld [hl], a
--- a/engine/movie/title.asm
+++ b/engine/movie/title.asm
@@ -126,13 +126,13 @@
ld [wTitleMonSpecies], a
call LoadTitleMonSprite
- ld a, (vBGMap0 + $300) / $100
+ ld a, HIGH(vBGMap0 + $300)
call TitleScreenCopyTileMapToVRAM
call SaveScreenTilesToBuffer1
ld a, $40
ldh [hWY], a
call LoadScreenTilesFromBuffer2
- ld a, vBGMap0 / $100
+ ld a, HIGH(vBGMap0)
call TitleScreenCopyTileMapToVRAM
ld b, SET_PAL_TITLE_SCREEN
call RunPaletteCommand
@@ -205,7 +205,7 @@
and a
jr nz, .scrollTitleScreenGameVersionLoop
- ld a, vBGMap1 / $100
+ ld a, HIGH(vBGMap1)
call TitleScreenCopyTileMapToVRAM
call LoadScreenTilesFromBuffer2
call PrintGameVersionOnTitleScreen
@@ -241,9 +241,9 @@
inc a
ldh [hAutoBGTransferEnabled], a
call ClearScreen
- ld a, vBGMap0 / $100
+ ld a, HIGH(vBGMap0)
call TitleScreenCopyTileMapToVRAM
- ld a, vBGMap1 / $100
+ ld a, HIGH(vBGMap1)
call TitleScreenCopyTileMapToVRAM
call Delay3
call LoadGBPal
@@ -258,7 +258,7 @@
farjp DoClearSaveDialogue
TitleScreenPickNewMon:
- ld a, vBGMap0 / $100
+ ld a, HIGH(vBGMap0)
call TitleScreenCopyTileMapToVRAM
.loop
--- a/engine/movie/trade.asm
+++ b/engine/movie/trade.asm
@@ -237,7 +237,7 @@
ld c, 10
call TextBoxBorder
call Trade_PrintPlayerMonInfoText
- ld b, vBGMap0 / $100
+ ld b, HIGH(vBGMap0)
call CopyScreenTileBufferToVRAM
call ClearScreen
ld a, [wTradedPlayerMonSpecies]
@@ -266,7 +266,7 @@
Trade_DrawOpenEndOfLinkCable:
call Trade_ClearTileMap
- ld b, vBGMap0 / $100
+ ld b, HIGH(vBGMap0)
call CopyScreenTileBufferToVRAM
ld b, SET_PAL_GENERIC
call RunPaletteCommand
--- a/engine/overworld/elevator.asm
+++ b/engine/overworld/elevator.asm
@@ -56,7 +56,7 @@
add hl, de
ld a, h
and $3
- or vBGMap0 / $100
+ or HIGH(vBGMap0)
ld d, a
ld a, l
pop hl
--- a/engine/overworld/movement.asm
+++ b/engine/overworld/movement.asm
@@ -23,7 +23,7 @@
ret
.lowerLeftTileIsMapTile
call DetectCollisionBetweenSprites
- ld h, wSpriteStateData1 / $100
+ ld h, HIGH(wSpriteStateData1)
ld a, [wWalkCounter]
and a
jr nz, .moving
@@ -395,7 +395,7 @@
ld l, a
ld [hl], $1 ; c1x1 = 1 (mark as ready to move)
notYetMoving:
- ld h, wSpriteStateData1 / $100
+ ld h, HIGH(wSpriteStateData1)
ldh a, [hCurrentSpriteOffset]
add wSpritePlayerStateData1AnimFrameCounter - wSpritePlayerStateData1
ld l, a
@@ -450,7 +450,7 @@
; calculates the sprite's screen position form its map position and the player position
InitializeSpriteScreenPosition:
- ld h, wSpriteStateData2 / $100
+ ld h, HIGH(wSpriteStateData2)
ldh a, [hCurrentSpriteOffset]
add wSpritePlayerStateData2MapY - wSpritePlayerStateData2
ld l, a
@@ -478,7 +478,7 @@
ldh a, [hIsHiddenMissableObject]
and a
jp nz, .spriteInvisible
- ld h, wSpriteStateData2 / $100
+ ld h, HIGH(wSpriteStateData2)
ldh a, [hCurrentSpriteOffset]
add wSpritePlayerStateData2MovementByte1 - wSpritePlayerStateData2
ld l, a
@@ -526,7 +526,7 @@
cp d
jr c, .spriteVisible ; standing on tile with ID >=MAP_TILESET_SIZE (top right tile)
.spriteInvisible
- ld h, wSpriteStateData1 / $100
+ ld h, HIGH(wSpriteStateData1)
ldh a, [hCurrentSpriteOffset]
add wSpritePlayerStateData1ImageIndex - wSpritePlayerStateData1
ld l, a
@@ -580,7 +580,7 @@
; e: X movement delta (-1, 0 or 1)
; set carry on failure, clears carry on success
CanWalkOntoTile:
- ld h, wSpriteStateData2 / $100
+ ld h, HIGH(wSpriteStateData2)
ldh a, [hCurrentSpriteOffset]
add wSpritePlayerStateData2MovementByte1 - wSpritePlayerStateData2
ld l, a
@@ -608,7 +608,7 @@
ld a, [hl] ; $c2x6 (movement byte 1)
inc a
jr z, .impassable ; if $ff, no movement allowed (however, changing direction is)
- ld h, wSpriteStateData1 / $100
+ ld h, HIGH(wSpriteStateData1)
ldh a, [hCurrentSpriteOffset]
add wSpritePlayerStateData1YPixels - wSpritePlayerStateData1
ld l, a
@@ -627,7 +627,7 @@
call DetectCollisionBetweenSprites
pop bc
pop de
- ld h, wSpriteStateData1 / $100
+ ld h, HIGH(wSpriteStateData1)
ldh a, [hCurrentSpriteOffset]
add $c
ld l, a
@@ -634,7 +634,7 @@
ld a, [hl] ; c1xc (directions in which sprite collision would occur)
and b ; check against chosen direction (1,2,4 or 8)
jr nz, .impassable ; collision between sprites, don't go there
- ld h, wSpriteStateData2 / $100
+ ld h, HIGH(wSpriteStateData2)
ldh a, [hCurrentSpriteOffset]
add wSpritePlayerStateData2YDisplacement - wSpritePlayerStateData2
ld l, a
@@ -665,7 +665,7 @@
and a ; clear carry (marking success)
ret
.impassable
- ld h, wSpriteStateData1 / $100
+ ld h, HIGH(wSpriteStateData1)
ldh a, [hCurrentSpriteOffset]
inc a
ld l, a
@@ -691,7 +691,7 @@
; this is always the lower left tile of the 2x2 tile blocks all sprites are snapped to
; hl: output pointer
GetTileSpriteStandsOn:
- ld h, wSpriteStateData1 / $100
+ ld h, HIGH(wSpriteStateData1)
ldh a, [hCurrentSpriteOffset]
add wSpritePlayerStateData1YPixels - wSpritePlayerStateData1
ld l, a
--- a/engine/overworld/sprite_collisions.asm
+++ b/engine/overworld/sprite_collisions.asm
@@ -53,9 +53,9 @@
DetectCollisionBetweenSprites:
nop
- ld h, wSpriteStateData1 / $100
+ ld h, HIGH(wSpriteStateData1)
ldh a, [hCurrentSpriteOffset]
- add wSpriteStateData1 % $100
+ add LOW(wSpriteStateData1)
ld l, a
ld a, [hl] ; a = [$c1i0] (picture) (0 if slot is unused)
--- a/engine/slots/slot_machine.asm
+++ b/engine/slots/slot_machine.asm
@@ -294,7 +294,7 @@
; Stop early if the middle symbol is not a cherry.
inc hl
ld a, [hl]
- cp SLOTSCHERRY >> 8
+ cp HIGH(SLOTSCHERRY)
jr nz, .stopWheel
ret
; It looks like this was intended to make the wheel stop when a 7 symbol was
@@ -303,7 +303,7 @@
ld c, $3
.loop
ld a, [hli]
- cp SLOTS7 >> 8
+ cp HIGH(SLOTS7)
jr c, .stopWheel ; condition never true
dec c
jr nz, .loop
@@ -330,7 +330,7 @@
.sevenAndBarMode
call SlotMachine_FindWheel1Wheel2Matches
ld a, [de]
- cp (SLOTSBAR >> 8) + 1
+ cp HIGH(SLOTSBAR) + 1
ret nc
.stopWheel
xor a
@@ -427,7 +427,7 @@
jr nz, .acceptMatch
; if 7/bar matches aren't enabled and the match was a 7/bar symbol, roll wheel
ld a, [hl]
- cp (SLOTSBAR >> 8) + 1
+ cp HIGH(SLOTSBAR) + 1
jr c, .rollWheel3DownByOneSymbol
.acceptMatch
ld a, [hl]
@@ -702,7 +702,7 @@
.skip1
ld [wAnimCounter], a
ld a, [wSlotMachineWinningSymbol]
- cp (SLOTSBAR >> 8) + 1
+ cp HIGH(SLOTSBAR) + 1
ld c, 8
jr nc, .skip2
srl c ; c = 4 (make the the coins transfer faster if the symbol was 7 or bar)
--- a/home.asm
+++ b/home.asm
@@ -1242,7 +1242,7 @@
; c = X coordinate of upper left corner of sprite
; de = base address of 4 tile number and attribute pairs
WriteOAMBlock::
- ld h, wOAMBuffer / $100
+ ld h, HIGH(wOAMBuffer)
swap a ; multiply by 16
ld l, a
call .writeOneEntry ; upper left
--- a/home/init.asm
+++ b/home/init.asm
@@ -82,9 +82,9 @@
ld a, CONNECTION_NOT_ESTABLISHED
ldh [hSerialConnectionStatus], a
- ld h, vBGMap0 / $100
+ ld h, HIGH(vBGMap0)
call ClearBgMap
- ld h, vBGMap1 / $100
+ ld h, HIGH(vBGMap1)
call ClearBgMap
ld a, rLCDC_DEFAULT
--- a/home/move_mon.asm
+++ b/home/move_mon.asm
@@ -197,17 +197,17 @@
ldh [hMultiplicand+1], a ; HP: (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100 + Level + 10
.noCarry4
ldh a, [hMultiplicand+1] ; check for overflow (>999)
- cp 999 / $100 + 1
+ cp HIGH(MAX_STAT_VALUE) + 1
jr nc, .overflow
- cp 999 / $100
+ cp HIGH(MAX_STAT_VALUE)
jr c, .noOverflow
ldh a, [hMultiplicand+2]
- cp 999 % $100 + 1
+ cp LOW(MAX_STAT_VALUE) + 1
jr c, .noOverflow
.overflow
- ld a, 999 / $100 ; overflow: cap at 999
+ ld a, HIGH(MAX_STAT_VALUE) ; overflow: cap at 999
ldh [hMultiplicand+1], a
- ld a, 999 % $100
+ ld a, LOW(MAX_STAT_VALUE)
ldh [hMultiplicand+2], a
.noOverflow
pop bc
--- a/home/uncompress.asm
+++ b/home/uncompress.asm
@@ -20,8 +20,8 @@
; initializes necessary data to load a sprite and runs UncompressSpriteDataLoop
_UncompressSpriteData::
ld hl, sSpriteBuffer1
- ld c, (2*SPRITEBUFFERSIZE) % $100
- ld b, (2*SPRITEBUFFERSIZE) / $100
+ ld c, LOW(2 * SPRITEBUFFERSIZE)
+ ld b, HIGH(2 * SPRITEBUFFERSIZE)
xor a
call FillMemory ; clear sprite buffer 1 and 2
ld a, $1
--- a/macros/scripts/audio.asm
+++ b/macros/scripts/audio.asm
@@ -141,8 +141,7 @@
; stored in big endian
tempo: MACRO
db $ED
- db \1 / $100
- db \1 % $100
+ db HIGH(\1), LOW(\1)
ENDM
; arguments: left output enable mask, right output enable mask
--- a/scripts/SafariZoneGate.asm
+++ b/scripts/SafariZoneGate.asm
@@ -182,9 +182,9 @@
call PrintText
ld a, 30
ld [wNumSafariBalls], a
- ld a, 502 / $100
+ ld a, HIGH(502)
ld [wSafariSteps], a
- ld a, 502 % $100
+ ld a, LOW(502)
ld [wSafariSteps + 1], a
ld a, D_UP
ld c, 3