From 7ce3473b0e5858b98a2ac1c393dc46c54ecfdf22 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Sun, 18 Nov 2018 19:37:18 +0100 Subject: [PATCH] Clean up save related files --- include/decoration_inventory.h | 5 +- include/load_save.h | 1 - include/overworld.h | 2 +- include/save.h | 27 ++---- include/save_location.h | 1 - src/clear_save_data_screen.c | 146 ++++++++++++++++---------------- src/hall_of_fame.c | 2 +- src/load_save.c | 9 +- src/overworld.c | 2 +- src/recorded_battle.c | 2 +- src/save.c | 122 +++++++++++++-------------- src/save_failed_screen.c | 148 ++++++++++++++++----------------- src/save_location.c | 19 ++--- 13 files changed, 235 insertions(+), 251 deletions(-) diff --git a/include/decoration_inventory.h b/include/decoration_inventory.h index e1fec36ea..7539f7240 100644 --- a/include/decoration_inventory.h +++ b/include/decoration_inventory.h @@ -1,12 +1,15 @@ #ifndef GUARD_DECORATION_INVENTORY_H #define GUARD_DECORATION_INVENTORY_H -struct DecorationInventory { + +struct DecorationInventory +{ u8 *items; u8 size; }; extern struct DecorationInventory gDecorationInventories[]; +void SetDecorationInventoriesPointers(void); void ClearDecorationInventories(void); s8 GetFirstEmptyDecorSlot(u8 idx); u8 CheckHasDecoration(u8); diff --git a/include/load_save.h b/include/load_save.h index b8b27ade5..c64a1179b 100644 --- a/include/load_save.h +++ b/include/load_save.h @@ -30,6 +30,5 @@ void LoadPlayerBag(void); void SavePlayerBag(void); void ApplyNewEncryptionKeyToHword(u16 *hWord, u32 newKey); void ApplyNewEncryptionKeyToWord(u32 *word, u32 newKey); -void ApplyNewEncryptionKeyToAllEncryptedData(u32 encryptionKey); #endif // GUARD_LOAD_SAVE_H diff --git a/include/overworld.h b/include/overworld.h index 6b38a8782..3e9d42a22 100644 --- a/include/overworld.h +++ b/include/overworld.h @@ -73,7 +73,7 @@ void SetFixedHoleWarpAsDestination(s16 x, s16 y); void warp1_set_to_sav1w(void); void sub_8084F2C(s8 mapGroup, s8 mapNum, s8 warpId, s8 x, s8 y); void sub_8084F6C(u8 a1); -void sub_8084FAC(void); +void sub_8084FAC(int unused); const struct MapConnection *GetMapConnection(u8 dir); static bool8 SetDiveWarp(u8 dir, u16 x, u16 y); bool8 SetDiveWarpEmerge(u16 x, u16 y); diff --git a/include/save.h b/include/save.h index a5f014904..b151f1064 100644 --- a/include/save.h +++ b/include/save.h @@ -54,7 +54,12 @@ enum SAVE_HALL_OF_FAME_ERASE_BEFORE // unused }; -#define SECTION_ID_RECORDED_BATTLE 31 +#define SECTOR_SAVE_SLOT_LENGTH 14 +#define SECTOR_ID_HOF_1 28 +#define SECTOR_ID_HOF_2 29 +#define SECTOR_ID_TRAINER_HILL 30 +#define SECTOR_ID_RECORDED_BATTLE 31 +#define SECTORS_COUNT 32 extern u16 gLastWrittenSector; extern u32 gLastSaveCounter; @@ -72,26 +77,6 @@ extern struct SaveSection gSaveDataBuffer; void ClearSaveData(void); void Save_ResetSaveCounters(void); -bool32 SetDamagedSectorBits(u8 op, u8 bit); -u8 save_write_to_flash(u16 a1, const struct SaveSectionLocation *location); -u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location); -u8 HandleWriteSectorNBytes(u8 sector, u8 *data, u16 size); -u8 TryWriteSector(u8 sector, u8 *data); -u32 RestoreSaveBackupVarsAndIncrement(const struct SaveSectionLocation *location); -u32 RestoreSaveBackupVars(const struct SaveSectionLocation *location); -u8 sub_81529D4(u16 a1, const struct SaveSectionLocation *location); -u8 sub_8152A34(u16 a1, const struct SaveSectionLocation *location); -u8 ClearSaveData_2(u16 a1, const struct SaveSectionLocation *location); -u8 sav12_xor_get(u16 a1, const struct SaveSectionLocation *location); -u8 sub_8152CAC(u16 a1, const struct SaveSectionLocation *location); -u8 sub_8152D44(u16 a1, const struct SaveSectionLocation *location); -u8 sub_8152DD0(u16 a1, const struct SaveSectionLocation *location); -u8 sub_8152E10(u16 a1, const struct SaveSectionLocation *location); -u8 GetSaveValidStatus(const struct SaveSectionLocation *location); -u8 sub_81530DC(u8 a1, u8 *data, u16 size); -u8 DoReadFlashWholeSection(u8 sector, struct SaveSection *section); -u16 CalculateChecksum(void *data, u16 size); -void UpdateSaveAddresses(void); u8 HandleSavingData(u8 saveType); u8 TrySavingData(u8 saveType); bool8 sub_8153380(void); diff --git a/include/save_location.h b/include/save_location.h index 95b3a7d7d..71dbd3fd8 100644 --- a/include/save_location.h +++ b/include/save_location.h @@ -1,7 +1,6 @@ #ifndef GUARD_SAVE_LOCATION_H #define GUARD_SAVE_LOCATION_H -bool32 sub_81AFCEC(void); void TrySetMapSaveWarpStatus(void); void sub_81AFDA0(void); void sub_81AFDD0(void); diff --git a/src/clear_save_data_screen.c b/src/clear_save_data_screen.c index 8da8502b3..eb79f7b5e 100644 --- a/src/clear_save_data_screen.c +++ b/src/clear_save_data_screen.c @@ -10,6 +10,7 @@ #include "bg.h" #include "text_window.h" #include "constants/songs.h" +#include "constants/rgb.h" extern const u8 gText_ClearAllSaveData[]; extern const u8 gText_ClearingData[]; @@ -72,7 +73,7 @@ static const struct WindowTemplate sClearSaveYesNo[] = void CB2_InitClearSaveDataScreen(void) { - if(SetupClearSaveDataScreen()) + if (SetupClearSaveDataScreen()) CreateTask(Task_DoClearSaveDataScreenYesNo, 0); } @@ -86,18 +87,18 @@ static void Task_DoClearSaveDataScreenYesNo(u8 taskId) static void Task_ClearSaveDataScreenYesNoChoice(u8 taskId) { - switch(Menu_ProcessInputNoWrapClearOnChoose()) + switch (Menu_ProcessInputNoWrapClearOnChoose()) { - case 0: - FillWindowPixelBuffer(0, 17); - AddTextPrinterParameterized(0, 1, gText_ClearingData, 0, 1, 0, 0); - gTasks[taskId].func = Task_ClearSaveData; - break; - case 1: - case -1: - PlaySE(SE_SELECT); - DestroyTask(taskId); - SetMainCallback2(CB2_FadeAndDoReset); + case 0: + FillWindowPixelBuffer(0, 17); + AddTextPrinterParameterized(0, 1, gText_ClearingData, 0, 1, 0, 0); + gTasks[taskId].func = Task_ClearSaveData; + break; + case 1: + case -1: + PlaySE(SE_SELECT); + DestroyTask(taskId); + SetMainCallback2(CB2_FadeAndDoReset); } } @@ -125,55 +126,55 @@ static bool8 SetupClearSaveDataScreen(void) switch(gMain.state) { - case 0: - default: - SetVBlankCallback(NULL); - SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_MODE_0); - SetGpuReg(REG_OFFSET_BG0HOFS, 0); - SetGpuReg(REG_OFFSET_BG0VOFS, 0); - SetGpuReg(REG_OFFSET_BG3HOFS, 0); - SetGpuReg(REG_OFFSET_BG3VOFS, 0); - SetGpuReg(REG_OFFSET_WIN0H, 0); - SetGpuReg(REG_OFFSET_WIN0V, 0); - SetGpuReg(REG_OFFSET_WININ, 0); - SetGpuReg(REG_OFFSET_WINOUT, 0); - SetGpuReg(REG_OFFSET_BLDCNT, 0); - SetGpuReg(REG_OFFSET_BLDALPHA, 0); - SetGpuReg(REG_OFFSET_BLDY, 0); - DmaFill16(3, 0, (void *)VRAM, VRAM_SIZE); - DmaFill32(3, 0, (void *)OAM, OAM_SIZE); - DmaFill16(3, 0, (void *)(PLTT + 2), PLTT_SIZE - 2); - ResetPaletteFade(); - gPlttBufferUnfaded[0] = 0x7fff; - gPlttBufferFaded[0] = 0x7fff; - gPlttBufferUnfaded[1] = 0x3945; - gPlttBufferFaded[1] = 0x3945; - for (i = 0; i < 0x10; i++) - ((u16 *)(VRAM + 0x20))[i] = 0x1111; + case 0: + default: + SetVBlankCallback(NULL); + SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_MODE_0); + SetGpuReg(REG_OFFSET_BG0HOFS, 0); + SetGpuReg(REG_OFFSET_BG0VOFS, 0); + SetGpuReg(REG_OFFSET_BG3HOFS, 0); + SetGpuReg(REG_OFFSET_BG3VOFS, 0); + SetGpuReg(REG_OFFSET_WIN0H, 0); + SetGpuReg(REG_OFFSET_WIN0V, 0); + SetGpuReg(REG_OFFSET_WININ, 0); + SetGpuReg(REG_OFFSET_WINOUT, 0); + SetGpuReg(REG_OFFSET_BLDCNT, 0); + SetGpuReg(REG_OFFSET_BLDALPHA, 0); + SetGpuReg(REG_OFFSET_BLDY, 0); + DmaFill16(3, 0, (void *)VRAM, VRAM_SIZE); + DmaFill32(3, 0, (void *)OAM, OAM_SIZE); + DmaFill16(3, 0, (void *)(PLTT + 2), PLTT_SIZE - 2); + ResetPaletteFade(); + gPlttBufferUnfaded[0] = RGB_WHITE; + gPlttBufferFaded[0] = RGB_WHITE; + gPlttBufferUnfaded[1] = RGB(5, 10, 14); + gPlttBufferFaded[1] = RGB(5, 10, 14); + for (i = 0; i < 0x10; i++) + ((u16 *)(VRAM + 0x20))[i] = 0x1111; - for (i = 0; i < 0x400; i++) - ((u16 *)(VRAM + 0xF000))[i] = 0x0001; - ResetTasks(); - ResetSpriteData(); - ResetBgsAndClearDma3BusyFlags(0); - InitBgsFromTemplates(0, sClearSaveBgTemplates, ARRAY_COUNT(sClearSaveBgTemplates)); - SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_OBJ_ON | DISPCNT_OBJ_1D_MAP); - ShowBg(0); - ShowBg(3); - SetGpuReg(REG_OFFSET_BLDCNT, 0); - InitClearSaveDataScreenWindows(); - BeginNormalPaletteFade(0x0000FFFF, 0, 0x10, 0, 0xFFFF); - EnableInterrupts(1); - SetVBlankCallback(VBlankCB); - gMain.state = 1; - break; - case 1: - UpdatePaletteFade(); - if(!gPaletteFade.active) - { - SetMainCallback2(MainCB); - return TRUE; - } + for (i = 0; i < 0x400; i++) + ((u16 *)(VRAM + 0xF000))[i] = 0x0001; + ResetTasks(); + ResetSpriteData(); + ResetBgsAndClearDma3BusyFlags(0); + InitBgsFromTemplates(0, sClearSaveBgTemplates, ARRAY_COUNT(sClearSaveBgTemplates)); + SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_OBJ_ON | DISPCNT_OBJ_1D_MAP); + ShowBg(0); + ShowBg(3); + SetGpuReg(REG_OFFSET_BLDCNT, 0); + InitClearSaveDataScreenWindows(); + BeginNormalPaletteFade(0x0000FFFF, 0, 0x10, 0, 0xFFFF); + EnableInterrupts(INTR_FLAG_VBLANK); + SetVBlankCallback(VBlankCB); + gMain.state = 1; + break; + case 1: + UpdatePaletteFade(); + if(!gPaletteFade.active) + { + SetMainCallback2(MainCB); + return TRUE; + } } return FALSE; } @@ -182,18 +183,19 @@ static void CB2_FadeAndDoReset(void) { switch(gMain.state) { - case 0: - default: - BeginNormalPaletteFade(0x0000FFFF, 0, 0, 0x10, 0xFFFF); - gMain.state = 1; - break; - case 1: - UpdatePaletteFade(); - if(!gPaletteFade.active) - { - FreeAllWindowBuffers(); - DoSoftReset(); - } + case 0: + default: + BeginNormalPaletteFade(0x0000FFFF, 0, 0, 0x10, 0xFFFF); + gMain.state = 1; + break; + case 1: + UpdatePaletteFade(); + if(!gPaletteFade.active) + { + FreeAllWindowBuffers(); + DoSoftReset(); + } + break; } } diff --git a/src/hall_of_fame.c b/src/hall_of_fame.c index 22206405b..a197b7a18 100644 --- a/src/hall_of_fame.c +++ b/src/hall_of_fame.c @@ -521,7 +521,7 @@ static void Task_Hof_InitTeamSaveData(u8 taskId) static void Task_Hof_TrySaveData(u8 taskId) { gGameContinueCallback = CB2_DoHallOfFameScreenDontSaveData; - if (TrySavingData(3) == 0xFF && gDamagedSaveSectors != 0) + if (TrySavingData(SAVE_HALL_OF_FAME) == 0xFF && gDamagedSaveSectors != 0) { UnsetBgTilemapBuffer(1); UnsetBgTilemapBuffer(3); diff --git a/src/load_save.c b/src/load_save.c index 45b4d200b..960a98981 100644 --- a/src/load_save.c +++ b/src/load_save.c @@ -6,14 +6,15 @@ #include "random.h" #include "malloc.h" #include "item.h" +#include "overworld.h" +#include "decoration_inventory.h" + +static void ApplyNewEncryptionKeyToAllEncryptedData(u32 encryptionKey); extern void* gUnknown_0203CF5C; extern bool16 IdentifyFlash(void); -extern void SetDecorationInventoriesPointers(void); -extern void ApplyNewEncryptionKeyToGameStats(u32 key); extern void ApplyNewEncryptionKeyToBerryPowder(u32 key); -extern void sub_8084FAC(int unused); #define SAVEBLOCK_MOVE_RANGE 128 @@ -286,7 +287,7 @@ void ApplyNewEncryptionKeyToWord(u32 *word, u32 newKey) *word ^= newKey; } -void ApplyNewEncryptionKeyToAllEncryptedData(u32 encryptionKey) +static void ApplyNewEncryptionKeyToAllEncryptedData(u32 encryptionKey) { ApplyNewEncryptionKeyToGameStats(encryptionKey); ApplyNewEncryptionKeyToBagItems_(encryptionKey); diff --git a/src/overworld.c b/src/overworld.c index 69ac9c822..d73a89d33 100644 --- a/src/overworld.c +++ b/src/overworld.c @@ -764,7 +764,7 @@ void sub_8084F6C(u8 a1) SetWarpData(&gSaveBlock1Ptr->warp1, warp->group, warp->map, -1, warp->x, warp->y); } -void sub_8084FAC(void) +void sub_8084FAC(int unused) { gSaveBlock1Ptr->warp1 = gSaveBlock1Ptr->warp2; } diff --git a/src/recorded_battle.c b/src/recorded_battle.c index 50dd2efa8..5d79cb789 100644 --- a/src/recorded_battle.c +++ b/src/recorded_battle.c @@ -490,7 +490,7 @@ bool32 MoveRecordedBattleToSaveData(void) static bool32 TryCopyRecordedBattleSaveData(struct RecordedBattleSave *dst, struct SaveSection *saveBuffer) { - if (TryCopySpecialSaveSection(SECTION_ID_RECORDED_BATTLE, (void*)(saveBuffer)) != 1) + if (TryCopySpecialSaveSection(SECTOR_ID_RECORDED_BATTLE, (void*)(saveBuffer)) != 1) return FALSE; memcpy(dst, saveBuffer, sizeof(struct RecordedBattleSave)); diff --git a/src/save.c b/src/save.c index 0bd48cca9..3ee98f4f7 100644 --- a/src/save.c +++ b/src/save.c @@ -1,15 +1,23 @@ #include "global.h" #include "gba/flash_internal.h" #include "save.h" -#include "constants/game_stat.h" #include "task.h" #include "decompress.h" #include "load_save.h" #include "overworld.h" +#include "main.h" +#include "constants/game_stat.h" + +static u16 CalculateChecksum(void *data, u16 size); +static u8 DoReadFlashWholeSection(u8 sector, struct SaveSection *section); +static u8 GetSaveValidStatus(const struct SaveSectionLocation *location); +static u8 sub_8152E10(u16 a1, const struct SaveSectionLocation *location); +static u8 ClearSaveData_2(u16 a1, const struct SaveSectionLocation *location); +static u8 TryWriteSector(u8 sector, u8 *data); +static u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location); // for the chunk declarations -extern bool8 gSoftResetDisabled; extern u32 gUnknown_0203CF5C; // Divide save blocks into individual chunks to be written to flash sectors @@ -24,7 +32,7 @@ extern u32 gUnknown_0203CF5C; * Sectors 0 - 13: Save Slot 1 * Sectors 14 - 27: Save Slot 2 * Sectors 28 - 29: Hall of Fame - * Sector 30: e-Reader/Mystery Gift Stuff (note: e-Reader is deprecated in Emerald US) + * Sector 30: Trainer Hill * Sector 31: Recorded Battle * * There are two save slots for saving the player's game data. We alternate between @@ -105,7 +113,7 @@ void Save_ResetSaveCounters(void) gDamagedSaveSectors = 0; } -bool32 SetDamagedSectorBits(u8 op, u8 bit) +static bool32 SetDamagedSectorBits(u8 op, u8 bit) { bool32 retVal = FALSE; @@ -126,7 +134,7 @@ bool32 SetDamagedSectorBits(u8 op, u8 bit) return retVal; } -u8 save_write_to_flash(u16 a1, const struct SaveSectionLocation *location) +static u8 save_write_to_flash(u16 a1, const struct SaveSectionLocation *location) { u32 retVal; u16 i; @@ -142,11 +150,11 @@ u8 save_write_to_flash(u16 a1, const struct SaveSectionLocation *location) gLastKnownGoodSector = gLastWrittenSector; // backup the current written sector before attempting to write. gLastSaveCounter = gSaveCounter; gLastWrittenSector++; - gLastWrittenSector = gLastWrittenSector % 0xE; // array count save sector locations + gLastWrittenSector = gLastWrittenSector % SECTOR_SAVE_SLOT_LENGTH; // array count save sector locations gSaveCounter++; retVal = 1; - for (i = 0; i < 0xE; i++) + for (i = 0; i < SECTOR_SAVE_SLOT_LENGTH; i++) HandleWriteSector(i, location); if (gDamagedSaveSectors != 0) // skip the damaged sector. @@ -160,7 +168,7 @@ u8 save_write_to_flash(u16 a1, const struct SaveSectionLocation *location) return retVal; } -u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location) +static u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location) { u16 i; u16 sector; @@ -168,8 +176,8 @@ u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location) u16 size; sector = a1 + gLastWrittenSector; - sector %= 0xE; - sector += 0xE * (gSaveCounter % 2); + sector %= SECTOR_SAVE_SLOT_LENGTH; + sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2); data = location[a1].data; size = location[a1].size; @@ -189,7 +197,7 @@ u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location) return TryWriteSector(sector, gFastSaveSection->data); } -u8 HandleWriteSectorNBytes(u8 sector, u8 *data, u16 size) +static u8 HandleWriteSectorNBytes(u8 sector, u8 *data, u16 size) { u16 i; struct SaveSection *section = &gSaveDataBuffer; @@ -206,7 +214,7 @@ u8 HandleWriteSectorNBytes(u8 sector, u8 *data, u16 size) return TryWriteSector(sector, section->data); } -u8 TryWriteSector(u8 sector, u8 *data) +static u8 TryWriteSector(u8 sector, u8 *data) { if (ProgramFlashSectorAndVerify(sector, data) != 0) // is damaged? { @@ -220,20 +228,20 @@ u8 TryWriteSector(u8 sector, u8 *data) } } -u32 RestoreSaveBackupVarsAndIncrement(const struct SaveSectionLocation *location) // location is unused +static u32 RestoreSaveBackupVarsAndIncrement(const struct SaveSectionLocation *location) // location is unused { gFastSaveSection = &gSaveDataBuffer; gLastKnownGoodSector = gLastWrittenSector; gLastSaveCounter = gSaveCounter; gLastWrittenSector++; - gLastWrittenSector = gLastWrittenSector % 0xE; + gLastWrittenSector %= SECTOR_SAVE_SLOT_LENGTH; gSaveCounter++; gUnknown_03006208 = 0; gDamagedSaveSectors = 0; return 0; } -u32 RestoreSaveBackupVars(const struct SaveSectionLocation *location) // only ever called once, and gSaveBlock2 is passed to this function. location is unused +static u32 RestoreSaveBackupVars(const struct SaveSectionLocation *location) // only ever called once, and gSaveBlock2 is passed to this function. location is unused { gFastSaveSection = &gSaveDataBuffer; gLastKnownGoodSector = gLastWrittenSector; @@ -243,7 +251,7 @@ u32 RestoreSaveBackupVars(const struct SaveSectionLocation *location) // only ev return 0; } -u8 sub_81529D4(u16 a1, const struct SaveSectionLocation *location) +static u8 sub_81529D4(u16 a1, const struct SaveSectionLocation *location) { u8 retVal; @@ -267,7 +275,7 @@ u8 sub_81529D4(u16 a1, const struct SaveSectionLocation *location) return retVal; } -u8 sub_8152A34(u16 a1, const struct SaveSectionLocation *location) +static u8 sub_8152A34(u16 a1, const struct SaveSectionLocation *location) { u8 retVal = 1; @@ -282,7 +290,7 @@ u8 sub_8152A34(u16 a1, const struct SaveSectionLocation *location) return retVal; } -u8 ClearSaveData_2(u16 a1, const struct SaveSectionLocation *location) +static u8 ClearSaveData_2(u16 a1, const struct SaveSectionLocation *location) { u16 i; u16 sector; @@ -291,8 +299,8 @@ u8 ClearSaveData_2(u16 a1, const struct SaveSectionLocation *location) u8 status; sector = a1 + gLastWrittenSector; - sector %= 0xE; - sector += 0xE * (gSaveCounter % 2); + sector %= SECTOR_SAVE_SLOT_LENGTH; + sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2); data = location[a1].data; size = location[a1].size; @@ -356,13 +364,13 @@ u8 ClearSaveData_2(u16 a1, const struct SaveSectionLocation *location) } } -u8 sav12_xor_get(u16 a1, const struct SaveSectionLocation *location) +static u8 sav12_xor_get(u16 a1, const struct SaveSectionLocation *location) { u16 sector; sector = a1 + gLastWrittenSector; // no sub 1? - sector %= 0xE; - sector += 0xE * (gSaveCounter % 2); + sector %= SECTOR_SAVE_SLOT_LENGTH; + sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2); if (ProgramFlashByte(sector, sizeof(struct UnkSaveSection), 0x25)) { @@ -379,13 +387,13 @@ u8 sav12_xor_get(u16 a1, const struct SaveSectionLocation *location) } } -u8 sub_8152CAC(u16 a1, const struct SaveSectionLocation *location) +static u8 sub_8152CAC(u16 a1, const struct SaveSectionLocation *location) { u16 sector; sector = a1 + gLastWrittenSector - 1; - sector %= 0xE; - sector += 0xE * (gSaveCounter % 2); + sector %= SECTOR_SAVE_SLOT_LENGTH; + sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2); if (ProgramFlashByte(sector, sizeof(struct UnkSaveSection), ((u8 *)gFastSaveSection)[sizeof(struct UnkSaveSection)])) { @@ -402,13 +410,13 @@ u8 sub_8152CAC(u16 a1, const struct SaveSectionLocation *location) } } -u8 sub_8152D44(u16 a1, const struct SaveSectionLocation *location) +static u8 sub_8152D44(u16 a1, const struct SaveSectionLocation *location) { u16 sector; sector = a1 + gLastWrittenSector - 1; // no sub 1? - sector %= 0xE; - sector += 0xE * (gSaveCounter % 2); + sector %= SECTOR_SAVE_SLOT_LENGTH; + sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2); if (ProgramFlashByte(sector, sizeof(struct UnkSaveSection), 0x25)) { @@ -425,7 +433,7 @@ u8 sub_8152D44(u16 a1, const struct SaveSectionLocation *location) } } -u8 sub_8152DD0(u16 a1, const struct SaveSectionLocation *location) +static u8 sub_8152DD0(u16 a1, const struct SaveSectionLocation *location) { u8 retVal; gFastSaveSection = &gSaveDataBuffer; @@ -442,14 +450,14 @@ u8 sub_8152DD0(u16 a1, const struct SaveSectionLocation *location) return retVal; } -u8 sub_8152E10(u16 a1, const struct SaveSectionLocation *location) +static u8 sub_8152E10(u16 a1, const struct SaveSectionLocation *location) { u16 i; u16 checksum; - u16 v3 = 0xE * (gSaveCounter % 2); + u16 v3 = SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2); u16 id; - for (i = 0; i < 0xE; i++) + for (i = 0; i < SECTOR_SAVE_SLOT_LENGTH; i++) { DoReadFlashWholeSection(i + v3, gFastSaveSection); id = gFastSaveSection->id; @@ -468,7 +476,7 @@ u8 sub_8152E10(u16 a1, const struct SaveSectionLocation *location) return 1; } -u8 GetSaveValidStatus(const struct SaveSectionLocation *location) +static u8 GetSaveValidStatus(const struct SaveSectionLocation *location) { u16 i; u16 checksum; @@ -480,7 +488,7 @@ u8 GetSaveValidStatus(const struct SaveSectionLocation *location) u8 saveSlot2Status; // check save slot 1. - for (i = 0; i < 0xE; i++) + for (i = 0; i < SECTOR_SAVE_SLOT_LENGTH; i++) { DoReadFlashWholeSection(i, gFastSaveSection); if (gFastSaveSection->security == UNKNOWN_CHECK_VALUE) @@ -511,9 +519,9 @@ u8 GetSaveValidStatus(const struct SaveSectionLocation *location) securityPassed = FALSE; // check save slot 2. - for (i = 0; i < 0xE; i++) + for (i = 0; i < SECTOR_SAVE_SLOT_LENGTH; i++) { - DoReadFlashWholeSection(i + 0xE, gFastSaveSection); + DoReadFlashWholeSection(i + SECTOR_SAVE_SLOT_LENGTH, gFastSaveSection); if (gFastSaveSection->security == UNKNOWN_CHECK_VALUE) { securityPassed = TRUE; @@ -543,24 +551,16 @@ u8 GetSaveValidStatus(const struct SaveSectionLocation *location) if ((saveSlot1Counter == -1 && saveSlot2Counter == 0) || (saveSlot1Counter == 0 && saveSlot2Counter == -1)) { if ((unsigned)(saveSlot1Counter + 1) < (unsigned)(saveSlot2Counter + 1)) - { gSaveCounter = saveSlot2Counter; - } else - { gSaveCounter = saveSlot1Counter; - } } else { if (saveSlot1Counter < saveSlot2Counter) - { gSaveCounter = saveSlot2Counter; - } else - { gSaveCounter = saveSlot1Counter; - } } return 1; } @@ -593,7 +593,7 @@ u8 GetSaveValidStatus(const struct SaveSectionLocation *location) return 2; } -u8 sub_81530DC(u8 a1, u8 *data, u16 size) +static u8 sub_81530DC(u8 a1, u8 *data, u16 size) { u16 i; struct SaveSection *section = &gSaveDataBuffer; @@ -618,13 +618,13 @@ u8 sub_81530DC(u8 a1, u8 *data, u16 size) } } -u8 DoReadFlashWholeSection(u8 sector, struct SaveSection *section) +static u8 DoReadFlashWholeSection(u8 sector, struct SaveSection *section) { ReadFlash(sector, 0, section->data, sizeof(struct SaveSection)); return 1; } -u16 CalculateChecksum(void *data, u16 size) +static u16 CalculateChecksum(void *data, u16 size) { u16 i; u32 checksum = 0; @@ -635,7 +635,7 @@ u16 CalculateChecksum(void *data, u16 size) return ((checksum >> 16) + checksum); } -void UpdateSaveAddresses(void) +static void UpdateSaveAddresses(void) { int i = 0; @@ -668,7 +668,7 @@ u8 HandleSavingData(u8 saveType) switch (saveType) { case SAVE_HALL_OF_FAME_ERASE_BEFORE: // deletes HOF before overwriting HOF completely. unused - for (i = 0xE * 2 + 0; i < 32; i++) + for (i = SECTOR_ID_HOF_1; i < SECTORS_COUNT; i++) EraseFlashSector(i); case SAVE_HALL_OF_FAME: // hall of fame. if (GetGameStat(GAME_STAT_ENTERED_HOF) < 999) @@ -676,8 +676,8 @@ u8 HandleSavingData(u8 saveType) SaveSerializedGame(); save_write_to_flash(0xFFFF, gRamSaveSectionLocations); tempAddr = gDecompressionBuffer; - HandleWriteSectorNBytes(0x1C, tempAddr, 0xF80); - HandleWriteSectorNBytes(0x1D, tempAddr + 0xF80, 0xF80); + HandleWriteSectorNBytes(SECTOR_ID_HOF_1, tempAddr, 0xF80); + HandleWriteSectorNBytes(SECTOR_ID_HOF_2, tempAddr + 0xF80, 0xF80); break; case SAVE_NORMAL: // normal save. also called by overwriting your own save. default: @@ -692,7 +692,7 @@ u8 HandleSavingData(u8 saveType) for(i = 0; i < 5; i++) sav12_xor_get(i, gRamSaveSectionLocations); break; - // support for Ereader was removed in Emerald. + // Support for Ereader was removed in Emerald. /* case EREADER_SAVE: // used in mossdeep "game corner" before/after battling old man e-reader trainer SaveSerializedGame(); @@ -700,7 +700,7 @@ u8 HandleSavingData(u8 saveType) break; */ case SAVE_OVERWRITE_DIFFERENT_FILE: - for (i = (0xE * 2 + 0); i < 32; i++) + for (i = SECTOR_ID_HOF_1; i < SECTORS_COUNT; i++) EraseFlashSector(i); // erase HOF. SaveSerializedGame(); save_write_to_flash(0xFFFF, gRamSaveSectionLocations); @@ -710,7 +710,7 @@ u8 HandleSavingData(u8 saveType) return 0; } -u8 TrySavingData(u8 saveType) // TrySave +u8 TrySavingData(u8 saveType) { if (gFlashMemoryPresent != TRUE) { @@ -744,7 +744,7 @@ bool8 sub_8153380(void) // trade.s save bool8 sub_81533AC(void) // trade.s save { - u8 retVal = sub_81529D4(0xE, gRamSaveSectionLocations); + u8 retVal = sub_81529D4(SECTOR_SAVE_SLOT_LENGTH, gRamSaveSectionLocations); if (gDamagedSaveSectors) DoSaveFailedScreen(0); if (retVal == 0xFF) @@ -755,7 +755,7 @@ bool8 sub_81533AC(void) // trade.s save u8 sub_81533E0(void) // trade.s save { - sub_8152A34(0xE, gRamSaveSectionLocations); + sub_8152A34(SECTOR_SAVE_SLOT_LENGTH, gRamSaveSectionLocations); if (gDamagedSaveSectors) DoSaveFailedScreen(0); return 0; @@ -763,7 +763,7 @@ u8 sub_81533E0(void) // trade.s save u8 sub_8153408(void) // trade.s save { - sub_8152CAC(0xE, gRamSaveSectionLocations); + sub_8152CAC(SECTOR_SAVE_SLOT_LENGTH, gRamSaveSectionLocations); if (gDamagedSaveSectors) DoSaveFailedScreen(0); return 0; @@ -840,8 +840,8 @@ u16 sub_815355C(void) return 0; UpdateSaveAddresses(); GetSaveValidStatus(gRamSaveSectionLocations); - v3 = 0xE * (gSaveCounter % 2); - for (i = 0; i < 14; i++) + v3 = SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2); + for (i = 0; i < SECTOR_SAVE_SLOT_LENGTH; i++) { DoReadFlashWholeSection(i + v3, gFastSaveSection); if (gFastSaveSection->id == 0) @@ -859,7 +859,7 @@ u32 TryCopySpecialSaveSection(u8 sector, u8* dst) s32 size; u8* savData; - if (sector != 30 && sector != 31) + if (sector != SECTOR_ID_TRAINER_HILL && sector != SECTOR_ID_RECORDED_BATTLE) return 0xFF; ReadFlash(sector, 0, (u8 *)&gSaveDataBuffer, sizeof(struct SaveSection)); if (*(u32*)(&gSaveDataBuffer.data[0]) != 0xB39D) diff --git a/src/save_failed_screen.c b/src/save_failed_screen.c index 010279a87..ede9fe672 100644 --- a/src/save_failed_screen.c +++ b/src/save_failed_screen.c @@ -17,12 +17,12 @@ #define MSG_WIN_TOP 12 #define CLOCK_WIN_TOP (MSG_WIN_TOP - 4) -extern u8 gText_SaveFailedCheckingBackup[]; -extern u8 gText_BackupMemoryDamaged[]; -extern u8 gText_CheckCompleted[]; -extern u8 gText_SaveCompleteGameCannotContinue[]; -extern u8 gText_SaveCompletePressA[]; -extern u8 gText_GamePlayCannotBeContinued[]; +extern const u8 gText_SaveFailedCheckingBackup[]; +extern const u8 gText_BackupMemoryDamaged[]; +extern const u8 gText_CheckCompleted[]; +extern const u8 gText_SaveCompleteGameCannotContinue[]; +extern const u8 gText_SaveCompletePressA[]; +extern const u8 gText_GamePlayCannotBeContinued[]; // gSaveFailedClockInfo enum enum @@ -155,8 +155,8 @@ static void VBlankCB_UpdateClockGraphics(void); static bool8 VerifySectorWipe(u16 sector); static bool8 WipeSectors(u32); -// although this is a general text printer, it's only used in this file. -static void SaveFailedScreenTextPrint(u8 *text, u8 var1, u8 var2) +// Although this is a general text printer, it's only used in this file. +static void SaveFailedScreenTextPrint(const u8 *text, u8 var1, u8 var2) { u8 color[3]; @@ -187,72 +187,70 @@ static void CB2_SaveFailedScreen(void) { switch (gMain.state) { - case 0: - default: - SetVBlankCallback(NULL); - SetGpuReg(REG_OFFSET_DISPCNT, 0); - SetGpuReg(REG_OFFSET_BG3CNT, 0); - SetGpuReg(REG_OFFSET_BG2CNT, 0); - SetGpuReg(REG_OFFSET_BG1CNT, 0); - SetGpuReg(REG_OFFSET_BG0CNT, 0); - SetGpuReg(REG_OFFSET_BG3HOFS, 0); - SetGpuReg(REG_OFFSET_BG3VOFS, 0); - SetGpuReg(REG_OFFSET_BG2HOFS, 0); - SetGpuReg(REG_OFFSET_BG2VOFS, 0); - SetGpuReg(REG_OFFSET_BG1HOFS, 0); - SetGpuReg(REG_OFFSET_BG1VOFS, 0); - SetGpuReg(REG_OFFSET_BG0HOFS, 0); - SetGpuReg(REG_OFFSET_BG0VOFS, 0); - // how come this doesnt use the Dma manager? - DmaFill16(3, 0, VRAM, VRAM_SIZE); - DmaFill32(3, 0, OAM, OAM_SIZE); - DmaFill16(3, 0, PLTT, PLTT_SIZE); - LZ77UnCompVram(gBirchHelpGfx, (void *)VRAM); - LZ77UnCompVram(gBirchBagTilemap, (void *)(VRAM + 0x7000)); - LZ77UnCompVram(gBirchGrassTilemap, (void *)(VRAM + 0x7800)); - LZ77UnCompVram(sSaveFailedClockGfx, (void *)(VRAM + 0x10020)); - ResetBgsAndClearDma3BusyFlags(0); - InitBgsFromTemplates(0, gUnknown_085EFD88, 3); - SetBgTilemapBuffer(0, (void *)&gDecompressionBuffer[0x2000]); - CpuFill32(0, &gDecompressionBuffer[0x2000], 0x800); - LoadBgTiles(0, gTextWindowFrame1_Gfx, 0x120, 0x214); - InitWindows(gUnknown_085EFD94); - // AddWindowWithoutTileMap returns a u16/integer, but the info is clobbered into a u8 here resulting in lost info. Bug? - gSaveFailedWindowIds[TEXT_WIN_ID] = AddWindowWithoutTileMap(gUnknown_085EFD9C); - SetWindowAttribute(gSaveFailedWindowIds[TEXT_WIN_ID], 7, (u32)&gDecompressionBuffer[0x2800]); - gSaveFailedWindowIds[CLOCK_WIN_ID] = AddWindowWithoutTileMap(gUnknown_085EFDA4); - SetWindowAttribute(gSaveFailedWindowIds[CLOCK_WIN_ID], 7, (u32)&gDecompressionBuffer[0x3D00]); - DeactivateAllTextPrinters(); - ResetSpriteData(); - ResetTasks(); - ResetPaletteFade(); - LoadPalette(gBirchBagGrassPal, 0, 0x40); - LoadPalette(sSaveFailedClockPal, 0x100, 0x20); - LoadPalette(gTextWindowFrame1_Pal, 0xE0, 0x20); - LoadPalette(gUnknown_0860F074, 0xF0, 0x20); - SetWindowBorderStyle(gSaveFailedWindowIds[TEXT_WIN_ID], FALSE, 0x214, 0xE); - SetWindowBorderStyle(gSaveFailedWindowIds[CLOCK_WIN_ID], FALSE, 0x214, 0xE); - FillWindowPixelBuffer(gSaveFailedWindowIds[CLOCK_WIN_ID], 0x11); // backwards? - FillWindowPixelBuffer(gSaveFailedWindowIds[TEXT_WIN_ID], 0x11); - CopyWindowToVram(gSaveFailedWindowIds[CLOCK_WIN_ID], 2); // again? - CopyWindowToVram(gSaveFailedWindowIds[TEXT_WIN_ID], 1); - SaveFailedScreenTextPrint(gText_SaveFailedCheckingBackup, 1, 0); - BeginNormalPaletteFade(0xFFFFFFFF, 0, 16, 0, 0); - EnableInterrupts(1); - SetVBlankCallback(VBlankCB); - SetGpuReg(0, 0x1040); - ShowBg(0); - ShowBg(2); - ShowBg(3); - gMain.state++; - break; - case 1: - if (!UpdatePaletteFade()) - { - SetMainCallback2(CB2_WipeSave); - SetVBlankCallback(VBlankCB_UpdateClockGraphics); - } - break; + case 0: + default: + SetVBlankCallback(NULL); + SetGpuReg(REG_OFFSET_DISPCNT, 0); + SetGpuReg(REG_OFFSET_BG3CNT, 0); + SetGpuReg(REG_OFFSET_BG2CNT, 0); + SetGpuReg(REG_OFFSET_BG1CNT, 0); + SetGpuReg(REG_OFFSET_BG0CNT, 0); + SetGpuReg(REG_OFFSET_BG3HOFS, 0); + SetGpuReg(REG_OFFSET_BG3VOFS, 0); + SetGpuReg(REG_OFFSET_BG2HOFS, 0); + SetGpuReg(REG_OFFSET_BG2VOFS, 0); + SetGpuReg(REG_OFFSET_BG1HOFS, 0); + SetGpuReg(REG_OFFSET_BG1VOFS, 0); + SetGpuReg(REG_OFFSET_BG0HOFS, 0); + SetGpuReg(REG_OFFSET_BG0VOFS, 0); + DmaFill16(3, 0, VRAM, VRAM_SIZE); + DmaFill32(3, 0, OAM, OAM_SIZE); + DmaFill16(3, 0, PLTT, PLTT_SIZE); + LZ77UnCompVram(gBirchHelpGfx, (void *)VRAM); + LZ77UnCompVram(gBirchBagTilemap, (void *)(VRAM + 0x7000)); + LZ77UnCompVram(gBirchGrassTilemap, (void *)(VRAM + 0x7800)); + LZ77UnCompVram(sSaveFailedClockGfx, (void *)(VRAM + 0x10020)); + ResetBgsAndClearDma3BusyFlags(0); + InitBgsFromTemplates(0, gUnknown_085EFD88, 3); + SetBgTilemapBuffer(0, (void *)&gDecompressionBuffer[0x2000]); + CpuFill32(0, &gDecompressionBuffer[0x2000], 0x800); + LoadBgTiles(0, gTextWindowFrame1_Gfx, 0x120, 0x214); + InitWindows(gUnknown_085EFD94); + gSaveFailedWindowIds[TEXT_WIN_ID] = AddWindowWithoutTileMap(gUnknown_085EFD9C); + SetWindowAttribute(gSaveFailedWindowIds[TEXT_WIN_ID], 7, (u32)&gDecompressionBuffer[0x2800]); + gSaveFailedWindowIds[CLOCK_WIN_ID] = AddWindowWithoutTileMap(gUnknown_085EFDA4); + SetWindowAttribute(gSaveFailedWindowIds[CLOCK_WIN_ID], 7, (u32)&gDecompressionBuffer[0x3D00]); + DeactivateAllTextPrinters(); + ResetSpriteData(); + ResetTasks(); + ResetPaletteFade(); + LoadPalette(gBirchBagGrassPal, 0, 0x40); + LoadPalette(sSaveFailedClockPal, 0x100, 0x20); + LoadPalette(gTextWindowFrame1_Pal, 0xE0, 0x20); + LoadPalette(gUnknown_0860F074, 0xF0, 0x20); + SetWindowBorderStyle(gSaveFailedWindowIds[TEXT_WIN_ID], FALSE, 0x214, 0xE); + SetWindowBorderStyle(gSaveFailedWindowIds[CLOCK_WIN_ID], FALSE, 0x214, 0xE); + FillWindowPixelBuffer(gSaveFailedWindowIds[CLOCK_WIN_ID], 0x11); // backwards? + FillWindowPixelBuffer(gSaveFailedWindowIds[TEXT_WIN_ID], 0x11); + CopyWindowToVram(gSaveFailedWindowIds[CLOCK_WIN_ID], 2); // again? + CopyWindowToVram(gSaveFailedWindowIds[TEXT_WIN_ID], 1); + SaveFailedScreenTextPrint(gText_SaveFailedCheckingBackup, 1, 0); + BeginNormalPaletteFade(0xFFFFFFFF, 0, 16, 0, 0); + EnableInterrupts(1); + SetVBlankCallback(VBlankCB); + SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_OBJ_ON | DISPCNT_OBJ_1D_MAP); + ShowBg(0); + ShowBg(2); + ShowBg(3); + gMain.state++; + break; + case 1: + if (!UpdatePaletteFade()) + { + SetMainCallback2(CB2_WipeSave); + SetVBlankCallback(VBlankCB_UpdateClockGraphics); + } + break; } } @@ -346,7 +344,7 @@ static void CB2_ReturnToTitleScreen(void) static void VBlankCB_UpdateClockGraphics(void) { - unsigned int n = (gMain.vblankCounter2 >> 3) & 7; + u32 n = (gMain.vblankCounter2 >> 3) & 7; gMain.oamBuffer[0] = sClockOamData; gMain.oamBuffer[0].x = 112; diff --git a/src/save_location.c b/src/save_location.c index 262aaf40c..d49afa736 100644 --- a/src/save_location.c +++ b/src/save_location.c @@ -9,21 +9,18 @@ static bool32 IsCurMapInLocationList(const u16 *list) { + s32 i; u16 locSum = (gSaveBlock1Ptr->location.mapGroup << 8) + (gSaveBlock1Ptr->location.mapNum); - // im sure it was written a different way, but for the love of christ I cant figure out how to write it different where it still matches. - if (*list != 0xFFFF) + for (i = 0; list[i] != 0xFFFF; i++) { - u16 termValue = 0xFFFF; - const u16 *localList; - for (localList = list; *localList != termValue; localList++) - if (*localList == locSum) - return TRUE; + if (list[i] == locSum) + return TRUE; } + return FALSE; } -// TODO: Not require a packed u16 array for these lists static const u16 sSaveLocationPokeCenterList[] = { MAP_OLDALE_TOWN_POKEMON_CENTER_1F, @@ -72,7 +69,7 @@ static bool32 IsCurMapPokeCenter(void) return IsCurMapInLocationList(sSaveLocationPokeCenterList); } -static const u16 sSaveLocationReloadLocList[] = // there's only 1 location, and it's presumed its for the save reload feature for battle tower +static const u16 sSaveLocationReloadLocList[] = // There's only 1 location, and it's presumed its for the save reload feature for battle tower. { MAP_BATTLE_FRONTIER_BATTLE_TOWER_LOBBY, 0xFFFF, @@ -83,13 +80,13 @@ static bool32 IsCurMapReloadLocation(void) return IsCurMapInLocationList(sSaveLocationReloadLocList); } -// nulled out list. unknown what this would have been +// Nulled out list. Unknown what this would have been. static const u16 sUnknown_0861440E[] = { 0xFFFF, }; -bool32 sub_81AFCEC(void) +static bool32 sub_81AFCEC(void) { return IsCurMapInLocationList(sUnknown_0861440E); }