diff --git a/src/main.c b/src/main.c index bdedc7cef..224774497 100644 --- a/src/main.c +++ b/src/main.c @@ -1,31 +1,33 @@ #include "global.h" - -#include "gba/flash_internal.h" - -#include "dma3.h" -#include "gpu_regs.h" -#include "malloc.h" - -#include "agb_flash.h" -#include "battle_controllers.h" #include "crt0.h" +#include "malloc.h" #include "help_system.h" -#include "intro.h" #include "link.h" #include "link_rfu.h" -#include "load_save.h" +#include "librfu.h" #include "m4a.h" -#include "menu.h" +#include "bg.h" +#include "rtc.h" +#include "scanline_effect.h" #include "overworld.h" #include "play_time.h" #include "quest_log.h" #include "random.h" -#include "rtc.h" #include "save_failed_screen.h" -#include "scanline_effect.h" +#include "dma3.h" +#include "gba/flash_internal.h" +#include "load_save.h" +#include "gpu_regs.h" +#include "agb_flash.h" #include "sound.h" -#include "test_runner.h" +#include "battle.h" +#include "battle_controllers.h" +#include "text.h" +#include "intro.h" +#include "main.h" #include "trainer_tower.h" +#include "test_runner.h" +#include "constants/rgb.h" static void VBlankIntr(void); static void HBlankIntr(void); @@ -35,9 +37,11 @@ static void IntrDummy(void); // Defined in the linker script so that the test build can override it. extern void gInitialMainCB2(void); +extern void CB2_FlashNotDetectedScreen(void); const u8 gGameVersion = GAME_VERSION; -const u8 gGameLanguage = GAME_LANGUAGE; + +const u8 gGameLanguage = GAME_LANGUAGE; // English const char BuildDateTime[] = "2004 07 20 09:30"; @@ -76,6 +80,9 @@ static EWRAM_DATA u16 sTrainerId = 0; static void UpdateLinkAndCallCallbacks(void); static void InitMainCallbacks(void); static void CallCallbacks(void); +#ifdef BUGFIX +static void SeedRngWithRtc(void); +#endif static void ReadKeys(void); void InitIntrHandlers(void); static void WaitForVBlank(void); @@ -85,9 +92,11 @@ void EnableVCountIntrAtLine150(void); void AgbMain() { - *(vu16 *)BG_PLTT = RGB_WHITE; + *(vu16 *)BG_PLTT = RGB_WHITE; // Set the backdrop to white on startup InitGpuRegManager(); - REG_WAITCNT = WAITCNT_PREFETCH_ENABLE | WAITCNT_WS0_S_1 | WAITCNT_WS0_N_3; + REG_WAITCNT = WAITCNT_PREFETCH_ENABLE + | WAITCNT_WS0_S_1 | WAITCNT_WS0_N_3 + | WAITCNT_WS1_S_1 | WAITCNT_WS1_N_3; InitKeys(); InitIntrHandlers(); m4aSoundInit(); @@ -97,6 +106,9 @@ void AgbMain() CheckForFlashMemory(); InitMainCallbacks(); InitMapMusic(); +#ifdef BUGFIX + SeedRngWithRtc(); // see comment at SeedRngWithRtc definition below +#endif ClearDma3Requests(); ResetBgs(); SetDefaultFontsPointer(); @@ -130,8 +142,8 @@ void AgbMainLoop(void) ReadKeys(); if (gSoftResetDisabled == FALSE - && (gMain.heldKeysRaw & A_BUTTON) - && (gMain.heldKeysRaw & B_START_SELECT) == B_START_SELECT) + && JOY_HELD_RAW(A_BUTTON) + && JOY_HELD_RAW(B_START_SELECT) == B_START_SELECT) { rfu_REQ_stopMode(); rfu_waitREQComplete(); @@ -149,7 +161,7 @@ void AgbMainLoop(void) gLinkTransferringData = FALSE; UpdateLinkAndCallCallbacks(); - if (Overworld_RecvKeysFromLinkIsRunning() == 1) + if (Overworld_RecvKeysFromLinkIsRunning() == TRUE) { gMain.newKeys = 0; ClearSpriteCopyRequests(); @@ -179,16 +191,18 @@ static void InitMainCallbacks(void) gMain.callback1 = NULL; SetMainCallback2(gInitialMainCB2); gSaveBlock2Ptr = &gSaveblock2.block; - gSaveBlock1Ptr = &gSaveblock1.block; + gPokemonStoragePtr = &gPokemonStorage.block; gQuestLogPlaybackState = QL_PLAYBACK_STATE_STOPPED; } static void CallCallbacks(void) { -#if TESTING || DEBUG_BATTLE_MENU == TRUE // test framework not working with help system - if (!RunSaveFailedScreen()) -#else +#if !TESTING && DEBUG_BATTLE_MENU != TRUE // test framework not working with help system if (!RunSaveFailedScreen() && !RunHelpSystemCallback()) +#else +#if !TESTING + if (!RunSaveFailedScreen()) +#endif #endif { if (gMain.callback1) @@ -207,6 +221,7 @@ void SetMainCallback2(MainCallback callback) void StartTimer1(void) { + REG_TM2CNT_L = 0; REG_TM2CNT_H = TIMER_ENABLE | TIMER_COUNTUP; REG_TM1CNT_H = TIMER_ENABLE; @@ -236,6 +251,23 @@ void EnableVCountIntrAtLine150(void) EnableInterrupts(INTR_FLAG_VCOUNT); } +// FRLG commented this out to remove RTC, however Emerald didn't undo this! +#ifdef BUGFIX +static void SeedRngWithRtc(void) +{ + #define BCD8(x) ((((x) >> 4) & 0xF) * 10 + ((x) & 0xF)) + u32 seconds; + struct SiiRtcInfo rtc; + RtcGetInfo(&rtc); + seconds = + ((HOURS_PER_DAY * RtcGetDayCount(&rtc) + BCD8(rtc.hour)) + * MINUTES_PER_HOUR + BCD8(rtc.minute)) + * SECONDS_PER_MINUTE + BCD8(rtc.second); + SeedRng(seconds); + #undef BCD8 +} +#endif + void InitKeys(void) { gKeyRepeatContinueDelay = 5; @@ -325,19 +357,22 @@ void SetVCountCallback(IntrCallback callback) gMain.vcountCallback = callback; } +void RestoreSerialTimer3IntrHandlers(void) +{ + gIntrTable[1] = SerialIntr; + gIntrTable[2] = Timer3Intr; +} + void SetSerialCallback(IntrCallback callback) { gMain.serialCallback = callback; } -extern void CopyBufferedValuesToGpuRegs(void); -extern void ProcessDma3Requests(void); - static void VBlankIntr(void) { - if (gWirelessCommType) + if (gWirelessCommType != 0) RfuVSync(); - else if (!gLinkVSyncDisabled) + else if (gLinkVSyncDisabled == FALSE) LinkVSync(); gMain.vblankCounter1++; @@ -358,9 +393,9 @@ static void VBlankIntr(void) m4aSoundMain(); TryReceiveLinkBattleData(); - if (!gTestRunnerEnabled && (!gMain.inBattle || !(gBattleTypeFlags & (BATTLE_TYPE_LINK | BATTLE_TYPE_BATTLE_TOWER | BATTLE_TYPE_RECORDED)))) + if (!gTestRunnerEnabled && (!gMain.inBattle || !(gBattleTypeFlags & (BATTLE_TYPE_LINK | BATTLE_TYPE_FRONTIER | BATTLE_TYPE_RECORDED)))) AdvanceRandom(); - // Random(); // old + UpdateWirelessStatusIndicatorSprite(); INTR_CHECK |= INTR_FLAG_VBLANK; @@ -383,6 +418,9 @@ static void HBlankIntr(void) static void VCountIntr(void) { + if (gMain.vcountCallback) + gMain.vcountCallback(); + m4aSoundVSync(); INTR_CHECK |= INTR_FLAG_VCOUNT; gMain.intrCheck |= INTR_FLAG_VCOUNT; @@ -397,12 +435,6 @@ static void SerialIntr(void) gMain.intrCheck |= INTR_FLAG_SERIAL; } -void RestoreSerialTimer3IntrHandlers(void) -{ - gIntrTable[1] = SerialIntr; - gIntrTable[2] = Timer3Intr; -} - static void IntrDummy(void) {} @@ -410,8 +442,17 @@ static void WaitForVBlank(void) { gMain.intrCheck &= ~INTR_FLAG_VBLANK; - while (!(gMain.intrCheck & INTR_FLAG_VBLANK)) - ; + if (gWirelessCommType != 0) + { + // Desynchronization may occur if wireless adapter is connected + // and we call VBlankIntrWait(); + while (!(gMain.intrCheck & INTR_FLAG_VBLANK)) + ; + } + else + { + VBlankIntrWait(); + } } void SetTrainerTowerVBlankCounter(u32 *ptr) @@ -433,7 +474,7 @@ void DoSoftReset(void) DmaStop(2); DmaStop(3); SiiRtcProtect(); - SoftReset(RESET_ALL & ~RESET_SIO_REGS); + SoftReset(RESET_ALL); } void ClearPokemonCrySongs(void)