Skip to content

Commit

Permalink
Use more assertions to ensure correct code and data
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Oct 27, 2020
1 parent 9dcdad5 commit c85ba78
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 17 deletions.
15 changes: 10 additions & 5 deletions audio/sfx.asm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Sfx_Menu:
Sfx_ReadText:
Sfx_ReadText2:
channel_count 1
channel 5, Sfx_ReadText_Ch5 ; aka Sfx_ReadText2_Ch5
channel 5, Sfx_ReadText_Ch5
assert Sfx_ReadText_Ch5 == Sfx_ReadText2_Ch5

Sfx_Poison:
channel_count 1
Expand Down Expand Up @@ -486,10 +487,14 @@ Sfx_Elevator:
Sfx_LevelUp:
Sfx_DexFanfare5079:
channel_count 4
channel 5, Sfx_LevelUp_Ch5 ; aka Sfx_DexFanfare5079_Ch5
channel 6, Sfx_LevelUp_Ch6 ; aka Sfx_DexFanfare5079_Ch6
channel 7, Sfx_LevelUp_Ch7 ; aka Sfx_DexFanfare5079_Ch7
channel 8, Sfx_LevelUp_Ch8 ; aka Sfx_DexFanfare5079_Ch8
channel 5, Sfx_LevelUp_Ch5
channel 6, Sfx_LevelUp_Ch6
channel 7, Sfx_LevelUp_Ch7
channel 8, Sfx_LevelUp_Ch8
assert Sfx_LevelUp_Ch5 == Sfx_DexFanfare5079_Ch5
assert Sfx_LevelUp_Ch6 == Sfx_DexFanfare5079_Ch6
assert Sfx_LevelUp_Ch7 == Sfx_DexFanfare5079_Ch7
assert Sfx_LevelUp_Ch8 == Sfx_DexFanfare5079_Ch8

Sfx_LevelUp_Ch5:
Sfx_DexFanfare5079_Ch5:
Expand Down
2 changes: 1 addition & 1 deletion audio/sfx_pointers.asm
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ SFX:
dba Sfx_MilkDrink
dba Sfx_Present
dba Sfx_MorningSun
dba Sfx_LevelUp ; aka Sfx_DexFanfare5079
dba Sfx_LevelUp
dba Sfx_KeyItem
dba Sfx_Fanfare2
dba Sfx_RegisterPhoneNumber
Expand Down
3 changes: 2 additions & 1 deletion engine/battle/core.asm
Original file line number Diff line number Diff line change
Expand Up @@ -7781,7 +7781,8 @@ HandleSafariAngerEatingStatus: ; unreferenced
jr .finish

.angry
dec hl ; wSafariMonAngerCount
dec hl
assert wSafariMonEating - 1 == wSafariMonAngerCount
ld a, [hl]
and a
ret z
Expand Down
3 changes: 2 additions & 1 deletion engine/battle_anims/anim_commands.asm
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ BattleAnim_ClearOAM:
ld c, NUM_SPRITE_OAM_STRUCTS
.loop
ld a, [hl]
and $ff ^ (PALETTE_MASK | VRAM_BANK_1) ; PAL_BATTLE_OB_ENEMY (0)
and $ff ^ (PALETTE_MASK | VRAM_BANK_1) ; zeros out the palette bits
assert PAL_BATTLE_OB_ENEMY == 0
ld [hli], a
rept SPRITEOAMSTRUCT_LENGTH - 1
inc hl
Expand Down
3 changes: 2 additions & 1 deletion engine/events/bug_contest/judging.asm
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ ContestJudging_ThirdPlaceScoreText:

LoadContestantName:
; If a = 1, get your name.
dec a ; BUG_CONTEST_PLAYER
assert BUG_CONTEST_PLAYER == 1
dec a
jr z, .player
; Find the pointer for the trainer class of the Bug Catching Contestant whose ID is in a.
ld c, a
Expand Down
3 changes: 2 additions & 1 deletion engine/gfx/load_pics.asm
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ GetMonBackpic:
push de

; These are assumed to be at the same address in their respective banks.
ld hl, PokemonPicPointers ; UnownPicPointers
assert PokemonPicPointers == UnownPicPointers
ld hl, PokemonPicPointers
ld a, b
ld d, BANK(PokemonPicPointers)
cp UNOWN
Expand Down
3 changes: 2 additions & 1 deletion engine/overworld/map_object_action.asm
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ SetFacingBoulderDust:
and 2
ld a, FACING_BOULDER_DUST_1
jr z, .ok
inc a ; FACING_BOULDER_DUST_2
inc a
assert FACING_BOULDER_DUST_1 + 1 == FACING_BOULDER_DUST_2
.ok
ld [hl], a
ret
Expand Down
3 changes: 2 additions & 1 deletion engine/overworld/warp_connection.asm
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ EnterMapWarp:
; MOUNT_MOON_SQUARE and TIN_TOWER_ROOF are outdoor maps within indoor maps.
; Dig and Escape Rope should not take you to them.
ld a, [wPrevMapGroup]
cp GROUP_MOUNT_MOON_SQUARE ; aka GROUP_TIN_TOWER_ROOF
cp GROUP_MOUNT_MOON_SQUARE
assert GROUP_MOUNT_MOON_SQUARE == GROUP_TIN_TOWER_ROOF
jr nz, .not_mt_moon_or_tin_tower
ld a, [wPrevMapNumber]
cp MAP_MOUNT_MOON_SQUARE
Expand Down
4 changes: 4 additions & 0 deletions engine/pokegear/radio.asm
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ OaksPKMNTalk8:
; so no need for a retry loop
call Random
maskbits NUM_OAKS_POKEMON_TALK_ADVERBS
assert_power_of_2 NUM_OAKS_POKEMON_TALK_ADVERBS
ld e, a
ld d, 0
ld hl, .Adverbs
Expand Down Expand Up @@ -433,6 +434,7 @@ OaksPKMNTalk9:
; so no need for a retry loop
call Random
maskbits NUM_OAKS_POKEMON_TALK_ADJECTIVES
assert_power_of_2 NUM_OAKS_POKEMON_TALK_ADJECTIVES
ld e, a
ld d, 0
ld hl, .Adjectives
Expand Down Expand Up @@ -1130,6 +1132,7 @@ PeoplePlaces5:
; so no need for a retry loop
call Random
maskbits NUM_PNP_PEOPLE_ADJECTIVES
assert_power_of_2 NUM_PNP_PEOPLE_ADJECTIVES
ld e, a
ld d, 0
ld hl, .Adjectives
Expand Down Expand Up @@ -1263,6 +1266,7 @@ PeoplePlaces7:
; so no need for a retry loop
call Random
maskbits NUM_PNP_PLACES_ADJECTIVES
assert_power_of_2 NUM_PNP_PLACES_ADJECTIVES
ld e, a
ld d, 0
ld hl, .Adjectives
Expand Down
4 changes: 2 additions & 2 deletions gfx/trade/mobile_border.pal
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
RGB 31, 31, 63
dw $ffff ; RGB 31, 31, 63
RGB 00, 00, 00
RGB 00, 00, 00
RGB 00, 00, 00

RGB 31, 31, 63
dw $ffff ; RGB 31, 31, 63
RGB 00, 00, 00
RGB 00, 00, 00
RGB 00, 00, 00
Expand Down
1 change: 1 addition & 0 deletions macros/code.asm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ maskbits: MACRO
; maskbits 26
; cp 26
; jr nc, .loop
assert 0 < (\1) && (\1) <= $100, "bitmask must be 8-bit"
x = 1
rept 8
if x + 1 < (\1)
Expand Down
4 changes: 4 additions & 0 deletions macros/data.asm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ percent EQUS "* $ff / 100"
; e.g. 1 out_of 2 == 50 percent + 1 == $80
out_of EQUS "* $100 /"

assert_power_of_2: MACRO
assert (\1) & ((\1) - 1) == 0, "\1 must be a power of 2"
ENDM

; Constant data (db, dw, dl) macros

dwb: MACRO
Expand Down
8 changes: 8 additions & 0 deletions macros/gfx.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
assert_valid_rgb: MACRO
rept _NARG
assert 0 <= (\1) && (\1) <= 31, "RGB channel must be 0-31"
shift
endr
ENDM

RGB: MACRO
rept _NARG / 3
assert_valid_rgb \1, \2, \3
dw palred (\1) + palgreen (\2) + palblue (\3)
shift 3
endr
Expand Down
5 changes: 5 additions & 0 deletions macros/scripts/audio.asm
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
channel_count: MACRO
assert 0 < (\1) && (\1) <= NUM_MUSIC_CHANS, \
"channel_count must be 1-{d:NUM_MUSIC_CHANS}"
_num_channels = \1 - 1
ENDM

channel: MACRO
assert 0 < (\1) && (\1) <= NUM_CHANNELS, \
"channel id must be 1-{d:NUM_CHANNELS}"
dn (_num_channels << 2), \1 - 1 ; channel id
dw \2 ; address
_num_channels = 0
Expand Down Expand Up @@ -46,6 +50,7 @@ FIRST_MUSIC_CMD EQU const_value

const octave_cmd ; $d0
octave: MACRO
assert 0 < (\1) && (\1) < 8, "octave must be 1-8"
db octave_cmd | 8 - (\1) ; octave
ENDM

Expand Down
4 changes: 1 addition & 3 deletions macros/scripts/battle_anims.asm
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
anim_wait: MACRO
if \1 >= $d0
fail "anim_wait argument must be less than $d0."
endc
assert (\1) < $d0, "anim_wait argument must be less than $d0"
db \1
ENDM

Expand Down
2 changes: 2 additions & 0 deletions macros/scripts/maps.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
map_id: MACRO
;\1: map id
assert DEF(GROUP_\1) && DEF(MAP_\1), \
"Missing 'map_const \1' in constants/map_constants.asm"
db GROUP_\1, MAP_\1
ENDM

Expand Down

0 comments on commit c85ba78

Please sign in to comment.