From b9f5fed1cdfbf81fc18e2f90969cc23a37cf9c75 Mon Sep 17 00:00:00 2001 From: Philippe Symons Date: Thu, 2 Apr 2026 11:52:26 +0200 Subject: [PATCH] Fix hardware-only bug We need to erase the sector before copying the data. I also fixed an endianness problem in initialize_memory_locations(). This one was not caused by me. --- source/Gen3CartridgeSaveReader.cpp | 2 ++ source/flash_mem.cpp | 14 +++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/source/Gen3CartridgeSaveReader.cpp b/source/Gen3CartridgeSaveReader.cpp index 75f3b77..6b92ebc 100644 --- a/source/Gen3CartridgeSaveReader.cpp +++ b/source/Gen3CartridgeSaveReader.cpp @@ -143,6 +143,8 @@ void Gen3CartridgeSaveReader::flush() } update_memory_buffer_checksum(sector_buffer_, (sector_start_ == HALL_OF_FAME)); + // Real flash requires erase before rewriting bytes that may need 0->1 transitions. + erase_sector(sector_start_); copy_ram_to_save(sector_buffer_, sector_start_, SECTOR_SIZE); dirty_ = false; } \ No newline at end of file diff --git a/source/flash_mem.cpp b/source/flash_mem.cpp index b1f3954..43b8c5c 100644 --- a/source/flash_mem.cpp +++ b/source/flash_mem.cpp @@ -22,11 +22,19 @@ void initialize_memory_locations() u8 save_B_index[4]; copy_save_to_ram(SAVE_A_OFFSET + SAVE_INDEX_OFFSET, &save_A_index[0], 0x04); copy_save_to_ram(SAVE_B_OFFSET + SAVE_INDEX_OFFSET, &save_B_index[0], 0x04); - reverse_endian(&save_A_index[0], 0x04); - reverse_endian(&save_B_index[0], 0x04); + + // Save indices are little-endian in-sector; decode directly without byte swapping. + const u32 saveAIndex = static_cast(save_A_index[0]) | + (static_cast(save_A_index[1]) << 8) | + (static_cast(save_A_index[2]) << 16) | + (static_cast(save_A_index[3]) << 24); + const u32 saveBIndex = static_cast(save_B_index[0]) | + (static_cast(save_B_index[1]) << 8) | + (static_cast(save_B_index[2]) << 16) | + (static_cast(save_B_index[3]) << 24); // Determines if save A or B is more recent - if (*(vu32 *)save_B_index > *(vu32 *)save_A_index) + if (saveBIndex > saveAIndex) { newest_save_offset = SAVE_B_OFFSET; }