main.c and adjacent refactoring

This commit is contained in:
cawtds
2024-08-23 00:00:12 +02:00
parent 4afb2c96cb
commit af8a8d4024
9 changed files with 112 additions and 91 deletions

8
include/crt0.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef GUARD_CRT0_H
#define GUARD_CRT0_H
extern u32 IntrMain[];
extern void ReInitializeEWRAM();
#endif //GUARD_CRT0_H

View File

@@ -5,10 +5,6 @@ typedef void (*MainCallback)(void);
typedef void (*IntrCallback)(void);
typedef void (*IntrFunc)(void);
#include "global.h"
extern IntrFunc gIntrTable[];
struct Main
{
/*0x000*/ MainCallback callback1;
@@ -44,13 +40,20 @@ struct Main
/*0x439*/ u8 field_439_x4:1;
};
extern struct Main gMain;
extern bool8 gSoftResetDisabled;
extern bool8 gLinkVSyncDisabled;
#define GAME_CODE_LENGTH 4
extern const char RomHeaderGameCode[GAME_CODE_LENGTH];
extern const char RomHeaderSoftwareVersion;
extern const u8 gGameVersion;
extern const u8 gGameLanguage;
extern u16 gKeyRepeatStartDelay;
extern u8 gLinkTransferringData;
extern struct Main gMain;
extern bool8 gSoftResetDisabled;
extern IntrFunc gIntrTable[];
extern bool8 gLinkVSyncDisabled;
void AgbMain(void);
void AgbMainLoop(void);
void SetMainCallback2(MainCallback callback);
@@ -69,11 +72,4 @@ void StartTimer1(void);
void SeedRngAndSetTrainerId(void);
u16 GetGeneratedTrainerIdLower(void);
#define GAME_CODE_LENGTH 4
extern const char RomHeaderGameCode[GAME_CODE_LENGTH];
extern const char RomHeaderSoftwareVersion;
extern u8 gLinkTransferringData;
extern u16 gKeyRepeatStartDelay;
#endif // GUARD_MAIN_H

View File

@@ -47,16 +47,24 @@ struct CreditsOverworldCmd
#define MUSIC_DISABLE_STOP 1
#define MUSIC_DISABLE_KEEP 2
extern const struct Coords32 gDirectionToVectors[];
extern struct LinkPlayerObjectEvent gLinkPlayerObjectEvents[4];
extern MainCallback gFieldCallback;
extern struct WarpData gLastUsedWarp;
extern struct LinkPlayerObjectEvent gLinkPlayerObjectEvents[4];
extern u8 gExitStairsMovementDisabled;
extern u8 gFieldLinkPlayerCount;
extern u16 *gBGTilemapBuffers1;
extern u16 *gBGTilemapBuffers2;
extern u16 *gBGTilemapBuffers3;
extern u16 gHeldKeyCodeToSend;
extern MainCallback gFieldCallback;
extern bool8 (* gFieldCallback2)(void);
extern u8 gLocalLinkPlayerId;
extern u8 gFieldLinkPlayerCount;
extern u8 gExitStairsMovementDisabled;
extern u8 gDisableMapMusicChangeOnMapLoad;
extern u8 gGlobalFieldTintMode;
extern const struct Coords32 gDirectionToVectors[];
void IncrementGameStat(u8 index);
@@ -115,11 +123,6 @@ void ResetInitialPlayerAvatarState(void);
void CleanupOverworldWindowsAndTilemaps(void);
u32 ComputeWhiteOutMoneyLoss(void);
extern u8 gDisableMapMusicChangeOnMapLoad;
extern u8 gGlobalFieldTintMode;
extern bool8 (* gFieldCallback2)(void);
void SetLastHealLocationWarp(u8 healLocaionId);
void LoadMapFromCameraTransition(u8 mapGroup, u8 mapNum);
void CB2_ReturnToFieldFromDiploma(void);
@@ -180,9 +183,4 @@ void UpdateEscapeWarp(s16 x, s16 y);
bool8 SetDiveWarpEmerge(u16 x, u16 y);
bool8 SetDiveWarpDive(u16 x, u16 y);
extern u16 *gBGTilemapBuffers1;
extern u16 *gBGTilemapBuffers2;
extern u16 *gBGTilemapBuffers3;
extern u16 gHeldKeyCodeToSend;
#endif //GUARD_OVERWORLD_H

View File

@@ -14,7 +14,7 @@ start_vector:
msr cpsr_cf, r0
ldr sp, sp_usr
ldr r1, =INTR_VECTOR
adr r0, intr_main
adr r0, IntrMain
str r0, [r1]
ldr r1, =AgbMain
mov lr, pc
@@ -29,8 +29,7 @@ sp_irq: .word IWRAM_END - 0x60
.arm
.align 2, 0
.global intr_main
intr_main:
IntrMain::
mov r3, #REG_BASE
add r3, r3, #OFFSET_REG_IE
ldr r2, [r3]
@@ -120,6 +119,22 @@ intr_return:
msr spsr_cf, r0
bx lr
.thumb
@ Called from C code to reinitialize working memory after a link connection failure
ReInitializeEWRAM::
ldr r0, =__ewram_lma
ldr r1, =__ewram_start
ldr r2, =__ewram_end
cmp r1, r2
beq EndReinitializeEWRAM
subs r2, r1
movs r3, #1
lsls r3, r3, #26
orrs r2, r2, r3
swi 0x0B
EndReinitializeEWRAM:
bx lr
.pool
.align 2, 0 @ Don't pad with nop.

View File

@@ -1,32 +1,37 @@
#include "global.h"
#include "gflib.h"
#include "m4a.h"
#include "scanline_effect.h"
#include "bg_regs.h"
#include "decompress.h"
#include "save.h"
#include "gpu_regs.h"
#include "malloc.h"
#include "string_util.h"
#include "battle.h"
#include "quest_log.h"
#include "link_rfu.h"
#include "librfu.h"
#include "random.h"
#include "task.h"
#include "decompress.h"
#include "event_data.h"
#include "item_menu.h"
#include "trade.h"
#include "menu.h"
#include "overworld.h"
#include "new_menu_helpers.h"
#include "link.h"
#include "graphics.h"
#include "strings.h"
#include "help_system.h"
#include "reset_save_heap.h"
#include "item_menu.h"
#include "librfu.h"
#include "link.h"
#include "link_rfu.h"
#include "m4a.h"
#include "menu.h"
#include "new_menu_helpers.h"
#include "overworld.h"
#include "palette.h"
#include "quest_log.h"
#include "random.h"
#include "reload_save.h"
#include "save.h"
#include "scanline_effect.h"
#include "sound.h"
#include "strings.h"
#include "task.h"
#include "trade.h"
#include "constants/battle.h"
#include "constants/songs.h"
extern u16 gHeldKeyCodeToSend;
struct BlockTransfer
{
u16 pos;

View File

@@ -1,7 +1,13 @@
#include "global.h"
#include "gba/flash_internal.h"
#include "gflib.h"
#include "dma3.h"
#include "gpu_regs.h"
#include "malloc.h"
#include "battle_controllers.h"
#include "crt0.h"
#include "help_system.h"
#include "intro.h"
#include "link.h"
@@ -16,11 +22,10 @@
#include "rtc.h"
#include "save_failed_screen.h"
#include "scanline_effect.h"
#include "sound.h"
#include "test_runner.h"
#include "trainer_tower.h"
extern u32 intr_main[];
static void VBlankIntr(void);
static void HBlankIntr(void);
static void VCountIntr(void);
@@ -33,11 +38,7 @@ extern void gInitialMainCB2(void);
const u8 gGameVersion = GAME_VERSION;
const u8 gGameLanguage = GAME_LANGUAGE;
#if MODERN
const char BuildDateTime[] = __DATE__ " " __TIME__;
#else
const char BuildDateTime[] = "2004 07 20 09:30";
#endif //MODERN
const IntrFunc gIntrTableTemplate[] =
{
@@ -354,7 +355,7 @@ void InitIntrHandlers(void)
for (i = 0; i < INTR_COUNT; i++)
gIntrTable[i] = gIntrTableTemplate[i];
DmaCopy32(3, intr_main, IntrMain_Buffer, sizeof(IntrMain_Buffer));
DmaCopy32(3, IntrMain, IntrMain_Buffer, sizeof(IntrMain_Buffer));
INTR_VECTOR = IntrMain_Buffer;

View File

@@ -1,17 +1,22 @@
#include "global.h"
#include "gflib.h"
#include "m4a.h"
#include "gpu_regs.h"
#include "malloc.h"
#include "crt0.h"
#include "load_save.h"
#include "save.h"
#include "m4a.h"
#include "main.h"
#include "new_game.h"
#include "overworld.h"
#include "save.h"
void ReloadSave(void)
{
u16 imeBackup = REG_IME;
REG_IME = 0;
RegisterRamReset(RESET_EWRAM);
ReInitializeEWRAM();
ClearGpuRegBits(REG_OFFSET_DISPCNT, DISPCNT_FORCED_BLANK);
REG_IME = imeBackup;
gMain.inBattle = FALSE;

View File

@@ -1,22 +1,29 @@
#include "global.h"
#include "gflib.h"
#include "task.h"
#include "new_menu_helpers.h"
#include "m4a.h"
#include "scanline_effect.h"
#include "bg.h"
#include "gpu_regs.h"
#include "malloc.h"
#include "berry_fix_program.h"
#include "clear_save_data_screen.h"
#include "decompress.h"
#include "event_data.h"
#include "graphics.h"
#include "help_system.h"
#include "intro.h"
#include "load_save.h"
#include "new_game.h"
#include "random.h"
#include "save.h"
#include "event_data.h"
#include "m4a.h"
#include "main_menu.h"
#include "clear_save_data_screen.h"
#include "new_game.h"
#include "new_menu_helpers.h"
#include "palette.h"
#include "random.h"
#include "reset_rtc_screen.h"
#include "berry_fix_program.h"
#include "decompress.h"
#include "save.h"
#include "scanline_effect.h"
#include "sound.h"
#include "task.h"
#include "constants/songs.h"
enum TitleScreenScene
@@ -747,20 +754,6 @@ static void SetTitleScreenScene_Cry(s16 *data)
case 2:
if (!gPaletteFade.active)
{
// These are commented out as they’ve been relocated to intro.c.
// This also allows RTC reset to work without file corruption.
// It also has the side effect of allowing you to run flags
// and functions as early as the intro.
// SeedRngAndSetTrainerId();
// SetSaveBlocksPointers();
// ResetMenuAndMonGlobals();
// Save_ResetSaveCounters();
// Save_LoadGameData(SAVE_NORMAL);
// if (gSaveFileStatus == SAVE_STATUS_EMPTY || gSaveFileStatus == SAVE_STATUS_INVALID)
// Sav2_ClearSetDefault();
// SetPokemonCryStereo(gSaveBlock2Ptr->optionsSound);
// InitHeap(gHeap, HEAP_SIZE);
SetMainCallback2(CB2_InitMainMenu);
DestroyTask(FindTaskIdByFunc(Task_TitleScreenMain));
}