From 1110d0aeb9d52be1bc1a3735307d8b11994242f6 Mon Sep 17 00:00:00 2001 From: Martin Griffin Date: Mon, 23 Oct 2023 21:12:17 +0100 Subject: [PATCH] ROM space on debug menu (#3453) --- data/scripts/debug.inc | 19 +++++++++++-- ld_script.txt | 2 ++ ld_script_modern.txt | 2 ++ ld_script_test.txt | 2 ++ src/debug.c | 64 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 3 deletions(-) diff --git a/data/scripts/debug.inc b/data/scripts/debug.inc index b29291a7f9..7385101c1e 100644 --- a/data/scripts/debug.inc +++ b/data/scripts/debug.inc @@ -104,13 +104,26 @@ Debug_CheckSaveBlock:: end Debug_SaveBlock1Size:: - .string "SaveBlock1 size: {STR_VAR_1}/{STR_VAR_2}.$" + .string "SaveBlock1 size: {STR_VAR_1}b/{STR_VAR_2}b.\n" + .string "Free space: {STR_VAR_3}b.$" Debug_SaveBlock2Size:: - .string "SaveBlock2 size: {STR_VAR_1}/{STR_VAR_2}.$" + .string "SaveBlock2 size: {STR_VAR_1}b/{STR_VAR_2}b.\n" + .string "Free space: {STR_VAR_3}b.$" Debug_PokemonStorageSize:: - .string "{PKMN}Storage size: {STR_VAR_1}/{STR_VAR_2}.$" + .string "{PKMN}Storage size: {STR_VAR_1}b/{STR_VAR_2}b.\n" + .string "Free space: {STR_VAR_3}b.$" + +Debug_CheckROMSpace:: + callnative CheckROMSize + msgbox Debug_ROMSize, MSGBOX_DEFAULT + release + end + +Debug_ROMSize:: + .string "ROM size: {STR_VAR_1}MB/32MB.\n" + .string "Free space: {STR_VAR_2}MB.$" Debug_HatchAnEgg:: lockall diff --git a/ld_script.txt b/ld_script.txt index a68d1a2a2c..27b340f490 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -1325,6 +1325,8 @@ SECTIONS { data/*.o(.rodata); } = 0 + __rom_end = .; + /* DWARF debug sections. Symbols in the DWARF debugging sections are relative to the beginning of the section so we begin them at 0. */ diff --git a/ld_script_modern.txt b/ld_script_modern.txt index 082d694291..ab68976661 100644 --- a/ld_script_modern.txt +++ b/ld_script_modern.txt @@ -130,6 +130,8 @@ SECTIONS { src/graphics.o(.rodata); } =0 + __rom_end = .; + /* DWARF debug sections. Symbols in the DWARF debugging sections are relative to the beginning of the section so we begin them at 0. */ diff --git a/ld_script_test.txt b/ld_script_test.txt index f4abad98ab..c7e19d7189 100644 --- a/ld_script_test.txt +++ b/ld_script_test.txt @@ -114,6 +114,8 @@ SECTIONS { test/*.o(.rodata*); } =0 + __rom_end = .; + /* DWARF debug sections. Symbols in the DWARF debugging sections are relative to the beginning of the section so we begin them at 0. */ diff --git a/src/debug.c b/src/debug.c index 8279033bf4..3d8d6f33bc 100644 --- a/src/debug.c +++ b/src/debug.c @@ -83,6 +83,7 @@ enum { // Util DEBUG_UTIL_MENU_ITEM_WARP, DEBUG_UTIL_MENU_ITEM_POISON_MONS, DEBUG_UTIL_MENU_ITEM_SAVEBLOCK, + DEBUG_UTIL_MENU_ITEM_ROM_SPACE, DEBUG_UTIL_MENU_ITEM_WEATHER, DEBUG_UTIL_MENU_ITEM_CHECKWALLCLOCK, DEBUG_UTIL_MENU_ITEM_SETWALLCLOCK, @@ -303,6 +304,7 @@ static void DebugAction_Util_Warp_SelectWarp(u8 taskId); static void DebugAction_FlagsVars_RunningShoes(u8 taskId); static void DebugAction_Util_PoisonMons(u8 taskId); static void DebugAction_Util_CheckSaveBlock(u8 taskId); +static void DebugAction_Util_CheckROMSpace(u8 taskId); static void DebugAction_Util_Weather(u8 taskId); static void DebugAction_Util_Weather_SelectId(u8 taskId); static void DebugAction_Util_CheckWallClock(u8 taskId); @@ -390,6 +392,7 @@ extern const u8 Debug_HatchAnEgg[]; extern const u8 PlayersHouse_2F_EventScript_SetWallClock[]; extern const u8 PlayersHouse_2F_EventScript_CheckWallClock[]; extern const u8 Debug_CheckSaveBlock[]; +extern const u8 Debug_CheckROMSpace[]; extern const u8 Debug_BoxFilledMessage[]; #include "data/map_group_count.h" @@ -432,6 +435,7 @@ static const u8 sDebugText_Util_WarpToMap_SelectWarp[] = _("Warp:{CLEAR_TO 90 static const u8 sDebugText_Util_WarpToMap_SelMax[] = _("{STR_VAR_1} / {STR_VAR_2}"); static const u8 sDebugText_Util_PoisonMons[] = _("Poison all mons"); static const u8 sDebugText_Util_SaveBlockSpace[] = _("SaveBlock Space…{CLEAR_TO 110}{RIGHT_ARROW}"); +static const u8 sDebugText_Util_ROMSpace[] = _("ROM Space…{CLEAR_TO 110}{RIGHT_ARROW}"); static const u8 sDebugText_Util_Weather[] = _("Set weather…{CLEAR_TO 110}{RIGHT_ARROW}"); static const u8 sDebugText_Util_Weather_ID[] = _("Weather Id: {STR_VAR_3}\n{STR_VAR_1}\n{STR_VAR_2}"); static const u8 sDebugText_Util_CheckWallClock[] = _("Check Wall Clock…{CLEAR_TO 110}{RIGHT_ARROW}"); @@ -595,6 +599,7 @@ static const struct ListMenuItem sDebugMenu_Items_Utilities[] = [DEBUG_UTIL_MENU_ITEM_WARP] = {sDebugText_Util_WarpToMap, DEBUG_UTIL_MENU_ITEM_WARP}, [DEBUG_UTIL_MENU_ITEM_POISON_MONS] = {sDebugText_Util_PoisonMons, DEBUG_UTIL_MENU_ITEM_POISON_MONS}, [DEBUG_UTIL_MENU_ITEM_SAVEBLOCK] = {sDebugText_Util_SaveBlockSpace, DEBUG_UTIL_MENU_ITEM_SAVEBLOCK}, + [DEBUG_UTIL_MENU_ITEM_ROM_SPACE] = {sDebugText_Util_ROMSpace, DEBUG_UTIL_MENU_ITEM_ROM_SPACE}, [DEBUG_UTIL_MENU_ITEM_WEATHER] = {sDebugText_Util_Weather, DEBUG_UTIL_MENU_ITEM_WEATHER}, [DEBUG_UTIL_MENU_ITEM_CHECKWALLCLOCK] = {sDebugText_Util_CheckWallClock, DEBUG_UTIL_MENU_ITEM_CHECKWALLCLOCK}, [DEBUG_UTIL_MENU_ITEM_SETWALLCLOCK] = {sDebugText_Util_SetWallClock, DEBUG_UTIL_MENU_ITEM_SETWALLCLOCK}, @@ -728,6 +733,7 @@ static void (*const sDebugMenu_Actions_Utilities[])(u8) = [DEBUG_UTIL_MENU_ITEM_WARP] = DebugAction_Util_Warp_Warp, [DEBUG_UTIL_MENU_ITEM_POISON_MONS] = DebugAction_Util_PoisonMons, [DEBUG_UTIL_MENU_ITEM_SAVEBLOCK] = DebugAction_Util_CheckSaveBlock, + [DEBUG_UTIL_MENU_ITEM_ROM_SPACE] = DebugAction_Util_CheckROMSpace, [DEBUG_UTIL_MENU_ITEM_WEATHER] = DebugAction_Util_Weather, [DEBUG_UTIL_MENU_ITEM_CHECKWALLCLOCK] = DebugAction_Util_CheckWallClock, [DEBUG_UTIL_MENU_ITEM_SETWALLCLOCK] = DebugAction_Util_SetWallClock, @@ -1800,6 +1806,7 @@ void CheckSaveBlock1Size(struct ScriptContext *ctx) u32 maxSb1Size = SECTOR_DATA_SIZE * (SECTOR_ID_SAVEBLOCK1_END - SECTOR_ID_SAVEBLOCK1_START + 1); ConvertIntToDecimalStringN(gStringVar1, currSb1Size, STR_CONV_MODE_LEFT_ALIGN, 6); ConvertIntToDecimalStringN(gStringVar2, maxSb1Size, STR_CONV_MODE_LEFT_ALIGN, 6); + ConvertIntToDecimalStringN(gStringVar3, maxSb1Size - currSb1Size, STR_CONV_MODE_LEFT_ALIGN, 6); } void CheckSaveBlock2Size(struct ScriptContext *ctx) @@ -1808,6 +1815,7 @@ void CheckSaveBlock2Size(struct ScriptContext *ctx) u32 maxSb2Size = SECTOR_DATA_SIZE; ConvertIntToDecimalStringN(gStringVar1, currSb2Size, STR_CONV_MODE_LEFT_ALIGN, 6); ConvertIntToDecimalStringN(gStringVar2, maxSb2Size, STR_CONV_MODE_LEFT_ALIGN, 6); + ConvertIntToDecimalStringN(gStringVar3, maxSb2Size - currSb2Size, STR_CONV_MODE_LEFT_ALIGN, 6); } void CheckPokemonStorageSize(struct ScriptContext *ctx) @@ -1816,6 +1824,7 @@ void CheckPokemonStorageSize(struct ScriptContext *ctx) u32 maxPkmnStorageSize = SECTOR_DATA_SIZE * (SECTOR_ID_PKMN_STORAGE_END - SECTOR_ID_PKMN_STORAGE_START + 1); ConvertIntToDecimalStringN(gStringVar1, currPkmnStorageSize, STR_CONV_MODE_LEFT_ALIGN, 6); ConvertIntToDecimalStringN(gStringVar2, maxPkmnStorageSize, STR_CONV_MODE_LEFT_ALIGN, 6); + ConvertIntToDecimalStringN(gStringVar3, maxPkmnStorageSize - currPkmnStorageSize, STR_CONV_MODE_LEFT_ALIGN, 6); } static void DebugAction_Util_CheckSaveBlock(u8 taskId) @@ -1825,6 +1834,61 @@ static void DebugAction_Util_CheckSaveBlock(u8 taskId) ScriptContext_SetupScript(Debug_CheckSaveBlock); } +enum RoundMode +{ + ROUND_CEILING, + ROUND_NEAREST, + ROUND_FLOOR, +}; + +static u8 *ConvertQ22_10ToDecimalString(u8 *string, u32 q22_10, u32 decimalDigits, enum RoundMode roundMode) +{ + string = ConvertIntToDecimalStringN(string, q22_10 >> 10, STR_CONV_MODE_LEFT_ALIGN, 10); + + if (decimalDigits == 0) + return string; + + *string++ = CHAR_PERIOD; + + q22_10 &= (1 << 10) - 1; + while (decimalDigits-- > 1) + { + q22_10 *= 10; + *string++ = CHAR_0 + (q22_10 >> 10); + q22_10 &= (1 << 10) - 1; + } + + q22_10 *= 10; + switch (roundMode) + { + case ROUND_CEILING: q22_10 += (1 << 10) - 1; break; + case ROUND_NEAREST: q22_10 += 1 << (10 - 1); break; + case ROUND_FLOOR: break; + } + *string++ = CHAR_0 + (q22_10 >> 10); + + *string++ = EOS; + + return string; +} + +void CheckROMSize(struct ScriptContext *ctx) +{ + extern u8 __rom_end[]; + u32 currROMSizeB = __rom_end - (const u8 *)ROM_START; + u32 currROMSizeKB = (currROMSizeB + 1023) / 1024; + u32 currROMFreeKB = ((const u8 *)ROM_END - __rom_end) / 1024; + ConvertQ22_10ToDecimalString(gStringVar1, currROMSizeKB, 2, ROUND_CEILING); + ConvertQ22_10ToDecimalString(gStringVar2, currROMFreeKB, 2, ROUND_FLOOR); +} + +static void DebugAction_Util_CheckROMSpace(u8 taskId) +{ + Debug_DestroyMenu_Full(taskId); + LockPlayerFieldControls(); + ScriptContext_SetupScript(Debug_CheckROMSpace); +} + static const u8 sWeatherNames[22][24] = { [WEATHER_NONE] = _("NONE"), [WEATHER_SUNNY_CLOUDS] = _("SUNNY CLOUDS"),