From b03c802e01eaf41c5b8e3da0e9f1abf6202fc9ba Mon Sep 17 00:00:00 2001 From: GriffinR Date: Sat, 15 Jan 2022 15:01:18 -0500 Subject: [PATCH] Clean up main.c --- payload/common_syms/main.txt | 4 +- payload/include/event_data.h | 2 +- payload/include/main.h | 38 ---- payload/include/rtc.h | 2 +- payload/src/event_data.c | 4 +- payload/src/main.c | 373 ++++++++++++++++++++--------------- payload/src/rtc.c | 8 +- 7 files changed, 226 insertions(+), 205 deletions(-) diff --git a/payload/common_syms/main.txt b/payload/common_syms/main.txt index b62c721..a1a1402 100644 --- a/payload/common_syms/main.txt +++ b/payload/common_syms/main.txt @@ -3,7 +3,7 @@ gHeldKeys gNewKeys gIntrVector gUpdateSuccessful -gUnknown_3001194 -gUnknown_30011A0 +gUnusedVar +gUnusedBuffer gMainCallbackState gGameVersion diff --git a/payload/include/event_data.h b/payload/include/event_data.h index db83834..c0668f7 100644 --- a/payload/include/event_data.h +++ b/payload/include/event_data.h @@ -1,7 +1,7 @@ #ifndef GUARD_EVENT_DATA_H #define GUARD_EVENT_DATA_H -bool32 BerryFix_ShouldResetPacifidlogTM(void); +bool32 BerryFix_IsPacifidlogTMCorrect(void); bool32 BerryFix_ResetPacifidlogTM(void); #endif //GUARD_EVENT_DATA_H diff --git a/payload/include/main.h b/payload/include/main.h index cb58d59..0b74434 100644 --- a/payload/include/main.h +++ b/payload/include/main.h @@ -1,45 +1,7 @@ #ifndef GUARD_MAIN_H #define GUARD_MAIN_H -#include "gba/gba.h" - -enum RomHeaderValidationResult -{ - SAPPHIRE_UPDATABLE = 2, - RUBY_UPDATABLE, - SAPPHIRE_NONEED, - RUBY_NONEED, - INVALID -}; - -enum MainCallbackState -{ - MAINCB_INIT = 0, - MAINCB_CHECK_RTC, - MAINCB_CHECK_FLASH, - MAINCB_READ_SAVE, - MAINCB_CHECK_TIME, - MAINCB_FIX_DATE, - MAINCB_NO_NEED_TO_FIX, - MAINCB_YEAR_MAKES_NO_SENSE, - MAINCB_FINISHED, - MAINCB_CHECK_PACIFIDLOG_TM, - MAINCB_FIX_PACIFIDLOG_TM, - MAINCB_ERROR -}; - extern IntrFunc gIntrTable[]; -extern u16 gHeldKeys; -extern u16 gNewKeys; -extern u8 gIntrVector[]; -extern u32 gUpdateSuccessful; -extern u32 gUnknown_3001194; -extern u32 gUnknown_30011A0[]; -extern u32 gMainCallbackState; -extern u32 gGameVersion; - -extern u8 gSharedMem[0x8000]; - extern const IntrFunc gIntrFuncPointers[]; #endif //GUARD_MAIN_H diff --git a/payload/include/rtc.h b/payload/include/rtc.h index daaa1b3..bef6262 100644 --- a/payload/include/rtc.h +++ b/payload/include/rtc.h @@ -19,7 +19,7 @@ extern struct Time gTimeSinceBerryUpdate; extern struct Time gRtcUTCTime; bool32 BerryFix_TryInitRtc(void); +void BerryFix_TryFixDate(void); bool32 BerryFix_CalcTimeDifference(u8 *); -void BerryFix_SetDate(void); #endif //GUARD_RTC_H diff --git a/payload/src/event_data.c b/payload/src/event_data.c index c636c53..a2bd4e1 100644 --- a/payload/src/event_data.c +++ b/payload/src/event_data.c @@ -27,7 +27,7 @@ static u16 * GetVarPointer(u16 id) return NULL; } -bool32 BerryFix_ShouldResetPacifidlogTM(void) +bool32 BerryFix_IsPacifidlogTMCorrect(void) { u8 year; u16 * var = GetVarPointer(VAR_PACIFIDLOG_TM_RECEIVED_DAY); @@ -41,7 +41,7 @@ bool32 BerryFix_ShouldResetPacifidlogTM(void) bool32 BerryFix_ResetPacifidlogTM(void) { u8 year; - if (BerryFix_ShouldResetPacifidlogTM() == TRUE) + if (BerryFix_IsPacifidlogTMCorrect() == TRUE) return TRUE; BerryFix_CalcTimeDifference(&year); if (gRtcUTCTime.days < 0) diff --git a/payload/src/main.c b/payload/src/main.c index 272c209..a2b3ef8 100644 --- a/payload/src/main.c +++ b/payload/src/main.c @@ -1,4 +1,3 @@ -#include "gba/gba.h" #include "global.h" #include "main.h" #include "rtc.h" @@ -6,43 +5,75 @@ #include "event_data.h" #include "message_box.h" -static s32 gInitialWaitTimer; +#define ROM_HEADER_MAGIC 0x96 +#define ROM_GAME_TITLE_LEN 15 + +// States for the main BerryFix function +enum { + STATE_INIT, + STATE_CHECK_RTC, + STATE_CHECK_FLASH, + STATE_READ_SAVE, + STATE_CHECK_TIME, + STATE_FIX_DATE, + STATE_NO_NEED_TO_FIX, + STATE_ERROR_YEAR, + STATE_FINISHED, + STATE_CHECK_PACIFIDLOG_TM, + STATE_FIX_PACIFIDLOG_TM, + STATE_ERROR +}; + +// Return values for ValidateRomHeader +enum +{ + UPDATE_SAPPHIRE = 2, + UPDATE_RUBY, + NO_UPDATE_SAPPHIRE, + NO_UPDATE_RUBY, + INVALID +}; + +static s32 sInitialWaitTimer; IntrFunc gIntrTable[16]; u16 gHeldKeys; u16 gNewKeys; u8 gIntrVector[0x100]; u32 gUpdateSuccessful; -u32 gUnknown_3001194; -u32 gUnknown_30011A0[0x19]; +u32 gUnusedVar; +u32 gUnusedBuffer[25]; u32 gMainCallbackState; u32 gGameVersion; -EWRAM_DATA u8 gSharedMem[0x8000] = {}; +static EWRAM_DATA u8 sSharedMem[0x8000] = {}; -void IntrMain(void); -void ReadKeys(void); -void dummy_intr_0(void); -void dummy_intr_1(void); -void main_callback(u32 *, void *, void *); +extern void IntrMain(void); +static void ReadKeys(void); +static void IntrDummy(void); +static void SerialIntr(void); +static void BerryFix(u32 *, void *, void *); + +static const char sBerryFixGameCode[] = "AGBJ"; -const char gBerryFixGameCode[] = "AGBJ"; const IntrFunc gIntrFuncPointers[] = { - dummy_intr_0, - dummy_intr_1, - dummy_intr_0, - dummy_intr_0, - dummy_intr_0, - dummy_intr_0, - dummy_intr_0, - dummy_intr_0, - dummy_intr_0, - dummy_intr_0, + IntrDummy, + SerialIntr, + IntrDummy, + IntrDummy, + IntrDummy, + IntrDummy, + IntrDummy, + IntrDummy, + IntrDummy, + IntrDummy, + NULL, NULL, NULL, - NULL }; -const char gVersionData[][2] = { + +// Language character code, followed by version number +static const char sVersionData[][2] = { {'J', 1}, {'E', 2}, {'D', 1}, @@ -50,230 +81,255 @@ const char gVersionData[][2] = { {'I', 1}, {'S', 1} }; -const char gRubyTitleAndCode[] = "POKEMON RUBYAXV"; -const char gSapphireTitleAndCode[] = "POKEMON SAPPAXP"; -const u16 sDebugPals[20] = { - RGB(00, 00, 00), - RGB(31, 00, 00), - RGB(00, 31, 00), - RGB(00, 00, 31) +static const char sRubyTitleAndCode[ROM_GAME_TITLE_LEN] = "POKEMON RUBYAXV"; +static const char sSapphireTitleAndCode[ROM_GAME_TITLE_LEN] = "POKEMON SAPPAXP"; +static const u16 sDebugPals[20] = { + RGB_BLACK, + RGB_RED, + RGB_GREEN, + RGB_BLUE }; -const u16 sDebugDigitsGfx[] = INCBIN_U16("graphics/debug_digits.4bpp"); +static const u16 sDebugDigitsGfx[] = INCBIN_U16("graphics/debug_digits.4bpp"); void AgbMain(void) { - RegisterRamReset(0x1E); - DmaCopy32(3, gIntrFuncPointers, gIntrTable, sizeof gIntrFuncPointers); + RegisterRamReset(RESET_IWRAM | RESET_PALETTE | RESET_VRAM | RESET_OAM); + DmaCopy32(3, gIntrFuncPointers, gIntrTable, sizeof(gIntrFuncPointers)); DmaCopy32(3, IntrMain, gIntrVector, sizeof(gIntrVector)); INTR_VECTOR = gIntrVector; REG_IE = INTR_FLAG_VBLANK; - if (*RomHeaderMagic == 0x96 && *(u32 *)RomHeaderGameCode == *(u32 *)gBerryFixGameCode) + if (*RomHeaderMagic == ROM_HEADER_MAGIC && *(u32 *)RomHeaderGameCode == *(u32 *)sBerryFixGameCode) REG_IE |= INTR_FLAG_GAMEPAK; REG_DISPSTAT = DISPSTAT_VBLANK_INTR; REG_IME = INTR_FLAG_VBLANK; MessageBox_Load(); - gMainCallbackState = MAINCB_INIT; - gUnknown_3001194 = 0; + gMainCallbackState = STATE_INIT; + gUnusedVar = 0; for (;;) { VBlankIntrWait(); ReadKeys(); - main_callback(&gMainCallbackState, gUnknown_30011A0, gSharedMem); + BerryFix(&gMainCallbackState, gUnusedBuffer, sSharedMem); } } -void dummy_intr_1(void) +static void SerialIntr(void) {} -void dummy_intr_0(void) +static void IntrDummy(void) {} -void ReadKeys(void) +static void ReadKeys(void) { u16 keyInput = REG_KEYINPUT ^ KEYS_MASK; gNewKeys = keyInput & ~gHeldKeys; gHeldKeys = keyInput; } -void fill_palette(const u8 * src, u16 * dest, u8 value) +// Unused +static void fill_palette(const u8 * src, u16 * dest, u8 value) { s32 i; for (i = 0; src[i] != 0; i++) dest[i] = src[i] | value << 12; } -bool32 berry_fix_memcmp(const char * src1, const char * src2, size_t size) +static bool32 CheckSameString(const char * a, const char * b, size_t size) { s32 i; for (i = 0; i < size; i++) { - if (src1[i] != src2[i]) + if (a[i] != b[i]) return FALSE; } return TRUE; } -s32 validate_rom_header_internal(void) +static s32 ValidateGameVersion(void) { - char languageCode = *(RomHeaderGameCode + 3); + char languageCode = RomHeaderGameCode[3]; s32 softwareVersion = *RomHeaderSoftwareVersion; s32 shouldUpdate = -1; s32 i; - for (i = 0; i < ARRAY_COUNT(gVersionData); i++) + + // Check rom header data to see if games of this + // language and revision need the berry fix. + for (i = 0; i < ARRAY_COUNT(sVersionData); i++) { - if (languageCode == gVersionData[i][0]) + if (languageCode == sVersionData[i][0]) { - if (softwareVersion >= gVersionData[i][1]) - { - shouldUpdate = 0; - } + if (softwareVersion >= sVersionData[i][1]) + shouldUpdate = FALSE; else - { - shouldUpdate = 1; - } + shouldUpdate = TRUE; break; } } if (shouldUpdate != -1) { - if (berry_fix_memcmp(RomHeaderGameTitle, gRubyTitleAndCode, 15) == TRUE) + // A valid language/revision was found, check game title + // and code to see if it's Ruby or Sapphire + + if (CheckSameString(RomHeaderGameTitle, sRubyTitleAndCode, ROM_GAME_TITLE_LEN) == TRUE) { - if (shouldUpdate == 0) - return RUBY_NONEED; + if (shouldUpdate == FALSE) + { + return NO_UPDATE_RUBY; + } else { gGameVersion = VERSION_RUBY; - return RUBY_UPDATABLE; + return UPDATE_RUBY; } } - else if (berry_fix_memcmp(RomHeaderGameTitle, gSapphireTitleAndCode, 15) == TRUE) + else if (CheckSameString(RomHeaderGameTitle, sSapphireTitleAndCode, ROM_GAME_TITLE_LEN) == TRUE) { - if (shouldUpdate == 0) - return SAPPHIRE_NONEED; + if (shouldUpdate == FALSE) + { + return NO_UPDATE_SAPPHIRE; + } else { gGameVersion = VERSION_SAPPHIRE; - return SAPPHIRE_UPDATABLE; + return UPDATE_SAPPHIRE; } } } return INVALID; } -s32 validate_rom_header(void) +static s32 ValidateRomHeader(void) { - if (*RomHeaderMakerCode == '0' && *(RomHeaderMakerCode + 1) == '1' && *RomHeaderMagic == 0x96) - return validate_rom_header_internal(); + if (RomHeaderMakerCode[0] == '0' && RomHeaderMakerCode[1] == '1' && *RomHeaderMagic == ROM_HEADER_MAGIC) + return ValidateGameVersion(); else return INVALID; } -void main_callback(u32 * state, void * unused1, void * unused2) +static void BerryFix(u32 * state, void * unused1, void * unused2) { u8 year; switch (*state) { - case MAINCB_INIT: - MessageBox_Display(MSG_WILL_NOW_UPDATE); - if (++gInitialWaitTimer >= 180) + case STATE_INIT: + // "The Berry Program Update will now begin..." + MessageBox_Display(MSG_WILL_NOW_UPDATE); + if (++sInitialWaitTimer >= 180) + { + sInitialWaitTimer = 0; + gUpdateSuccessful = 0; + switch (ValidateRomHeader()) { - gInitialWaitTimer = 0; - gUpdateSuccessful = 0; - switch (validate_rom_header()) - { - case SAPPHIRE_UPDATABLE: - case RUBY_UPDATABLE: // Should Update Ruby - ++(*state); // MAINCB_CHECK_RTC - break; - case INVALID: // Invalid header - *state = MAINCB_ERROR; - break; - case SAPPHIRE_NONEED: // Should not update Sapphire - case RUBY_NONEED: // Should not update Ruby - *state = MAINCB_NO_NEED_TO_FIX; - break; - } + case UPDATE_SAPPHIRE: + case UPDATE_RUBY: + ++(*state); // STATE_CHECK_RTC + break; + case INVALID: // Invalid header + *state = STATE_ERROR; + break; + case NO_UPDATE_SAPPHIRE: + case NO_UPDATE_RUBY: + *state = STATE_NO_NEED_TO_FIX; + break; } - break; - case MAINCB_CHECK_RTC: - if (!BerryFix_TryInitRtc()) - *state = MAINCB_ERROR; + } + break; + case STATE_CHECK_RTC: + if (!BerryFix_TryInitRtc()) + *state = STATE_ERROR; + else + ++(*state); // STATE_CHECK_FLASH + break; + case STATE_CHECK_FLASH: + if (BerryFix_IdentifyFlash() == TRUE) + ++(*state); // STATE_READ_SAVE + else + *state = STATE_ERROR; + break; + case STATE_READ_SAVE: + if (BerryFix_LoadSave(0) == SAVE_STATUS_OK) + ++(*state); // STATE_CHECK_TIME + else + *state = STATE_ERROR; + break; + case STATE_CHECK_TIME: + if (BerryFix_CalcTimeDifference(&year) == TRUE) + { + // Time difference is okay, only fix the date if + // the Berry Glitch hasn't happened yet (if year is 2000) + if (year == 0) + ++(*state); // STATE_FIX_DATE else - ++(*state); // MAINCB_CHECK_FLASH - break; - case MAINCB_CHECK_FLASH: - if (BerryFix_IdentifyFlash() == TRUE) - ++(*state); // MAINCB_READ_SAVE + *state = STATE_CHECK_PACIFIDLOG_TM; + } + else + { + // Time difference is incorrect, if the year is 2001 + // then the Berry Glitch is occurring. If the year is + // not 2001 then some error has occurred. + if (year != 1) + *state = STATE_ERROR_YEAR; else - *state = MAINCB_ERROR; - break; - case MAINCB_READ_SAVE: - if (BerryFix_LoadSave(0) == SAVE_STATUS_OK) - ++(*state); // MAINCB_CHECK_TIME - else - *state = MAINCB_ERROR; - break; - case MAINCB_CHECK_TIME: - if (BerryFix_CalcTimeDifference(&year) == TRUE) - { - if (year == 0) - ++(*state); // MAINCB_FIX_DATE - else - *state = MAINCB_CHECK_PACIFIDLOG_TM; - } - else - { - if (year != 1) - *state = MAINCB_YEAR_MAKES_NO_SENSE; - else - ++(*state); // MAINCB_FIX_DATE - } - break; - case MAINCB_FIX_DATE: - BerryFix_SetDate(); + ++(*state); // STATE_FIX_DATE + } + break; + case STATE_FIX_DATE: + // Set the clock forward to fix the Berry Glitch + // If the date is late enough that it is no + // longer affected then this does nothing. + BerryFix_TryFixDate(); + gUpdateSuccessful |= 1; + *state = STATE_CHECK_PACIFIDLOG_TM; + break; + case STATE_CHECK_PACIFIDLOG_TM: + if (BerryFix_IsPacifidlogTMCorrect() == TRUE) + *state = STATE_FINISHED; + else + *state = STATE_FIX_PACIFIDLOG_TM; + break; + case STATE_FIX_PACIFIDLOG_TM: + // "Updating. the Berry Program. Please wait..." + MessageBox_Display(MSG_UPDATING); + if (BerryFix_ResetPacifidlogTM() == TRUE) + { gUpdateSuccessful |= 1; - *state = MAINCB_CHECK_PACIFIDLOG_TM; - break; - case MAINCB_CHECK_PACIFIDLOG_TM: - if (BerryFix_ShouldResetPacifidlogTM() == TRUE) - *state = MAINCB_FINISHED; - else - *state = MAINCB_FIX_PACIFIDLOG_TM; - break; - case MAINCB_FIX_PACIFIDLOG_TM: - MessageBox_Display(MSG_UPDATING); - if (BerryFix_ResetPacifidlogTM() == TRUE) - { - gUpdateSuccessful |= 1; - *state = MAINCB_FINISHED; - } - else - *state = MAINCB_ERROR; - break; - case MAINCB_FINISHED: - if (gUpdateSuccessful == 0) - *state = MAINCB_NO_NEED_TO_FIX; - else - MessageBox_Display(MSG_HAS_BEEN_UPDATED); - break; - case MAINCB_NO_NEED_TO_FIX: - MessageBox_Display(MSG_NO_NEED_TO_UPDATE); - break; - case MAINCB_YEAR_MAKES_NO_SENSE: - MessageBox_Display(MSG_UNABLE_TO_UPDATE); - break; - case MAINCB_ERROR: - MessageBox_Display(MSG_UNABLE_TO_UPDATE); - break; + *state = STATE_FINISHED; + } + else + { + *state = STATE_ERROR; + } + break; + // The below 4 cases are all the possible end states + // The Berry Fix Program will remain in these states until + // the player turns off the GBA. + case STATE_FINISHED: + if (gUpdateSuccessful == 0) + *state = STATE_NO_NEED_TO_FIX; + else // "Your Berry Program has been updated" + MessageBox_Display(MSG_HAS_BEEN_UPDATED); + break; + case STATE_NO_NEED_TO_FIX: + // "There is no need to update your Berry Program" + MessageBox_Display(MSG_NO_NEED_TO_UPDATE); + break; + case STATE_ERROR_YEAR: + // "Unable to update the Berry Program" + MessageBox_Display(MSG_UNABLE_TO_UPDATE); + break; + case STATE_ERROR: + // "Unable to update the Berry Program" + MessageBox_Display(MSG_UNABLE_TO_UPDATE); + break; } } -void DBG_LoadDigitsPal(void) +static void Debug_LoadDigitsPal(void) { s32 i; const u16 * src; vu16 * dest = (vu16 *)BG_PLTT + 1; - DmaFill16(3, RGB(31, 31, 31), (vu16 *)BG_PLTT, BG_PLTT_SIZE); + DmaFill16(3, RGB_WHITE, (vu16 *)BG_PLTT, BG_PLTT_SIZE); src = sDebugPals; for (i = 0; i < 4; i++) { @@ -283,9 +339,10 @@ void DBG_LoadDigitsPal(void) } } -void DBG_LoadDigits(void) +// Unused +static void Debug_LoadDigits(void) { DmaFill16(3, 0x1111, (void *)VRAM + 0x8420, 0x1800); DmaCopy32(3, sDebugDigitsGfx, (void *)VRAM + 0x8600, 0x200); - DBG_LoadDigitsPal(); -} + Debug_LoadDigitsPal(); +} \ No newline at end of file diff --git a/payload/src/rtc.c b/payload/src/rtc.c index 7f2571b..ea52fed 100644 --- a/payload/src/rtc.c +++ b/payload/src/rtc.c @@ -347,13 +347,15 @@ static bool32 RtcNeedsLeapDayIncrement(struct SiiRtcInfo * rtc) if (ConvertBcdToBinary(rtc->year) == 1) return TRUE; - // After 2001 just set clock forward 365 days + // After 2001 (shouldn't occur) return FALSE; } -void BerryFix_SetDate(void) +void BerryFix_TryFixDate(void) { RtcGetRawInfo(&sRtcInfoWork); + + // If the year is anything but 2000 or 2001 then the Berry Glitch has already passed if (ConvertBcdToBinary(sRtcInfoWork.year) == 0 || ConvertBcdToBinary(sRtcInfoWork.year) == 1) { if (ConvertBcdToBinary(sRtcInfoWork.year) == 1) @@ -367,7 +369,7 @@ void BerryFix_SetDate(void) } else { - // Berry Glitch hasn't begun yet (or was already passed) + // Year is 2000, the Berry Glitch hasn't begun yet // Set the date forward 365/366 days to avoid the glitch if (RtcNeedsLeapDayIncrement(&sRtcInfoWork) == TRUE) {