From bc409baeff4ef2ec9ab774e0ecd1d0cca991eaaf Mon Sep 17 00:00:00 2001 From: GriffinR Date: Tue, 19 Oct 2021 16:30:00 -0400 Subject: [PATCH] Fix incorrect NUM_SECTORS_PER_SLOT define --- include/save.h | 4 +--- src/save.c | 53 +++++++++++++++++++++++++------------------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/include/save.h b/include/save.h index 4eaa72458..406e2e492 100644 --- a/include/save.h +++ b/include/save.h @@ -34,8 +34,6 @@ struct SaveSectionOffsets #define SECTOR_FOOTER_SIZE 128 #define SECTOR_SIZE (SECTOR_DATA_SIZE + SECTOR_FOOTER_SIZE) -// Emerald changes this definition to be the sectors per slot. -#define NUM_SECTORS_PER_SLOT 16 #define NUM_SAVE_SLOTS 2 #define UNKNOWN_CHECK_VALUE 0x8012025 @@ -66,7 +64,7 @@ enum #define SECTOR_ID_SAVEBLOCK1_END 4 #define SECTOR_ID_PKMN_STORAGE_START 5 #define SECTOR_ID_PKMN_STORAGE_END 13 -#define SECTOR_SAVE_SLOT_LENGTH 14 +#define NUM_SECTORS_PER_SLOT 14 // Save Slot 1: 0-13; Save Slot 2: 14-27 #define SECTOR_ID_HOF_1 28 #define SECTOR_ID_HOF_2 29 diff --git a/src/save.c b/src/save.c index 40fce4e8a..3c8f4360d 100644 --- a/src/save.c +++ b/src/save.c @@ -81,7 +81,7 @@ u16 gUnknown_03006208; u16 gSaveUnusedVar; u16 gSaveFileStatus; void (*gGameContinueCallback)(void); -struct SaveSectionLocation gRamSaveSectionLocations[SECTOR_SAVE_SLOT_LENGTH]; +struct SaveSectionLocation gRamSaveSectionLocations[NUM_SECTORS_PER_SLOT]; u16 gSaveUnusedVar2; u16 gSaveAttemptStatus; @@ -92,10 +92,11 @@ void ClearSaveData(void) { u16 i; - for (i = 0; i < NUM_SECTORS_PER_SLOT; i++) + // Clear the full save two sectors at a time + for (i = 0; i < SECTORS_COUNT / 2; i++) { EraseFlashSector(i); - EraseFlashSector(i + NUM_SECTORS_PER_SLOT); // clear slot 2. + EraseFlashSector(i + SECTORS_COUNT / 2); } } @@ -143,11 +144,11 @@ static u8 SaveWriteToFlash(u16 sectorId, const struct SaveSectionLocation *locat gLastKnownGoodSector = gLastWrittenSector; // backup the current written sector before attempting to write. gLastSaveCounter = gSaveCounter; gLastWrittenSector++; - gLastWrittenSector = gLastWrittenSector % SECTOR_SAVE_SLOT_LENGTH; // array count save sector locations + gLastWrittenSector = gLastWrittenSector % NUM_SECTORS_PER_SLOT; // array count save sector locations gSaveCounter++; status = SAVE_STATUS_OK; - for (i = 0; i < SECTOR_SAVE_SLOT_LENGTH; i++) + for (i = 0; i < NUM_SECTORS_PER_SLOT; i++) HandleWriteSector(i, location); if (gDamagedSaveSectors != 0) // skip the damaged sector. @@ -169,8 +170,8 @@ static u8 HandleWriteSector(u16 sectorId, const struct SaveSectionLocation *loca u16 size; sector = sectorId + gLastWrittenSector; - sector %= SECTOR_SAVE_SLOT_LENGTH; - sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % NUM_SAVE_SLOTS); + sector %= NUM_SECTORS_PER_SLOT; + sector += NUM_SECTORS_PER_SLOT * (gSaveCounter % NUM_SAVE_SLOTS); data = location[sectorId].data; size = location[sectorId].size; @@ -227,7 +228,7 @@ static u32 RestoreSaveBackupVarsAndIncrement(const struct SaveSectionLocation *l gLastKnownGoodSector = gLastWrittenSector; gLastSaveCounter = gSaveCounter; gLastWrittenSector++; - gLastWrittenSector %= SECTOR_SAVE_SLOT_LENGTH; + gLastWrittenSector %= NUM_SECTORS_PER_SLOT; gSaveCounter++; gUnknown_03006208 = 0; gDamagedSaveSectors = 0; @@ -292,8 +293,8 @@ static u8 ClearSaveData_2(u16 sectorId, const struct SaveSectionLocation *locati u8 status; sector = sectorId + gLastWrittenSector; - sector %= SECTOR_SAVE_SLOT_LENGTH; - sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % NUM_SAVE_SLOTS); + sector %= NUM_SECTORS_PER_SLOT; + sector += NUM_SECTORS_PER_SLOT * (gSaveCounter % NUM_SAVE_SLOTS); data = location[sectorId].data; size = location[sectorId].size; @@ -362,8 +363,8 @@ static u8 sav12_xor_get(u16 sectorId, const struct SaveSectionLocation *location u16 sector; sector = sectorId + gLastWrittenSector; // no sub 1? - sector %= SECTOR_SAVE_SLOT_LENGTH; - sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % NUM_SAVE_SLOTS); + sector %= NUM_SECTORS_PER_SLOT; + sector += NUM_SECTORS_PER_SLOT * (gSaveCounter % NUM_SAVE_SLOTS); if (ProgramFlashByte(sector, sizeof(struct UnkSaveSection), 0x25)) { @@ -385,8 +386,8 @@ static u8 sub_8152CAC(u16 sectorId, const struct SaveSectionLocation *location) u16 sector; sector = sectorId + gLastWrittenSector - 1; - sector %= SECTOR_SAVE_SLOT_LENGTH; - sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % NUM_SAVE_SLOTS); + sector %= NUM_SECTORS_PER_SLOT; + sector += NUM_SECTORS_PER_SLOT * (gSaveCounter % NUM_SAVE_SLOTS); if (ProgramFlashByte(sector, sizeof(struct UnkSaveSection), ((u8 *)gFastSaveSection)[sizeof(struct UnkSaveSection)])) { @@ -408,8 +409,8 @@ static u8 sub_8152D44(u16 sectorId, const struct SaveSectionLocation *location) u16 sector; sector = sectorId + gLastWrittenSector - 1; // no sub 1? - sector %= SECTOR_SAVE_SLOT_LENGTH; - sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % NUM_SAVE_SLOTS); + sector %= NUM_SECTORS_PER_SLOT; + sector += NUM_SECTORS_PER_SLOT * (gSaveCounter % NUM_SAVE_SLOTS); if (ProgramFlashByte(sector, sizeof(struct UnkSaveSection), 0x25)) { @@ -447,10 +448,10 @@ static u8 sub_8152E10(u16 a1, const struct SaveSectionLocation *location) { u16 i; u16 checksum; - u16 slotOffset = SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % NUM_SAVE_SLOTS); + u16 slotOffset = NUM_SECTORS_PER_SLOT * (gSaveCounter % NUM_SAVE_SLOTS); u16 id; - for (i = 0; i < SECTOR_SAVE_SLOT_LENGTH; i++) + for (i = 0; i < NUM_SECTORS_PER_SLOT; i++) { DoReadFlashWholeSection(i + slotOffset, gFastSaveSection); id = gFastSaveSection->id; @@ -481,7 +482,7 @@ static u8 GetSaveValidStatus(const struct SaveSectionLocation *location) u8 saveSlot2Status; // check save slot 1. - for (i = 0; i < SECTOR_SAVE_SLOT_LENGTH; i++) + for (i = 0; i < NUM_SECTORS_PER_SLOT; i++) { DoReadFlashWholeSection(i, gFastSaveSection); if (gFastSaveSection->security == UNKNOWN_CHECK_VALUE) @@ -512,9 +513,9 @@ static u8 GetSaveValidStatus(const struct SaveSectionLocation *location) securityPassed = FALSE; // check save slot 2. - for (i = 0; i < SECTOR_SAVE_SLOT_LENGTH; i++) + for (i = 0; i < NUM_SECTORS_PER_SLOT; i++) { - DoReadFlashWholeSection(i + SECTOR_SAVE_SLOT_LENGTH, gFastSaveSection); + DoReadFlashWholeSection(i + NUM_SECTORS_PER_SLOT, gFastSaveSection); if (gFastSaveSection->security == UNKNOWN_CHECK_VALUE) { securityPassed = TRUE; @@ -739,7 +740,7 @@ bool8 sub_8153380(void) // trade.c bool8 sub_81533AC(void) // trade.c { - u8 status = sub_81529D4(SECTOR_SAVE_SLOT_LENGTH, gRamSaveSectionLocations); + u8 status = sub_81529D4(NUM_SECTORS_PER_SLOT, gRamSaveSectionLocations); if (gDamagedSaveSectors) DoSaveFailedScreen(SAVE_NORMAL); if (status == SAVE_STATUS_ERROR) @@ -750,7 +751,7 @@ bool8 sub_81533AC(void) // trade.c bool8 sub_81533E0(void) // trade.c { - sub_8152A34(SECTOR_SAVE_SLOT_LENGTH, gRamSaveSectionLocations); + sub_8152A34(NUM_SECTORS_PER_SLOT, gRamSaveSectionLocations); if (gDamagedSaveSectors) DoSaveFailedScreen(SAVE_NORMAL); return FALSE; @@ -758,7 +759,7 @@ bool8 sub_81533E0(void) // trade.c bool8 sub_8153408(void) // trade.c { - sub_8152CAC(SECTOR_SAVE_SLOT_LENGTH, gRamSaveSectionLocations); + sub_8152CAC(NUM_SECTORS_PER_SLOT, gRamSaveSectionLocations); if (gDamagedSaveSectors) DoSaveFailedScreen(SAVE_NORMAL); return FALSE; @@ -835,8 +836,8 @@ u16 GetSaveBlocksPointersBaseOffset(void) return 0; UpdateSaveAddresses(); GetSaveValidStatus(gRamSaveSectionLocations); - slotOffset = SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % NUM_SAVE_SLOTS); - for (i = 0; i < SECTOR_SAVE_SLOT_LENGTH; i++) + slotOffset = NUM_SECTORS_PER_SLOT * (gSaveCounter % NUM_SAVE_SLOTS); + for (i = 0; i < NUM_SECTORS_PER_SLOT; i++) { DoReadFlashWholeSection(i + slotOffset, gFastSaveSection);