Read cartridge SRAM from RAM with safe wait states

This commit is contained in:
LyokoAgents committed 2026-10-29 00:04:08 +00:00
1 parent 59c44c2b45
commit a6937448c0
4 files changed
+27 -3

No files matched your search

+20 -1
View File
@@ -5,6 +5,23 @@ static const char save_signature[] __attribute__((used)) = "SRAM_V113";
#define SAVE_MEMORY ((volatile unsigned char *)0x0e000000)
#endif
static void configure_sram(void)
{
#ifndef GAME_TEST
volatile u16 *const waitcnt = (volatile u16 *)0x04000204;
*waitcnt = (u16)(*waitcnt | 3u); /* SRAM: eight waitstates; preserve ROM timings. */
#endif
}
/* Startup relocates this leaf reader; SRAM reads must execute from WRAM. */
#ifndef GAME_TEST
__attribute__((section(".ewram.text.sram_read"), noinline, long_call))
#endif
static void read_save_bytes(unsigned char *bytes)
{
for (int i = 0; i < SAVE_RECORD_SIZE; ++i) bytes[i] = SAVE_MEMORY[i];
}
struct Progress {
int partner, given, rod, count, members, active;
unsigned char kinds[CAUGHT_CAPACITY];
@@ -41,7 +58,8 @@ TEST_INTERNAL unsigned save_crc(const unsigned char *bytes, int length)
static int read_progress(struct Progress *p, struct Location *location)
{
unsigned char bytes[SAVE_RECORD_SIZE];
for (int i = 0; i < SAVE_RECORD_SIZE; ++i) bytes[i] = SAVE_MEMORY[i];
configure_sram();
read_save_bytes(bytes);
clear_progress(p);
default_location(location);
if (bytes[0] != 'F' || bytes[1] != 'Q' || bytes[2] != 'S' || bytes[3] != 'P') return 0;
@@ -118,6 +136,7 @@ int save_progress(const struct Location *location)
unsigned crc = save_crc(bytes, 17);
bytes[17] = (unsigned char)(crc >> 8); bytes[18] = (unsigned char)crc;
/* Invalidate first, commit last; a single record cannot recover interrupted writes. */
configure_sram();
SAVE_MEMORY[0] = 0;
for (int i = 1; i < SAVE_RECORD_SIZE; ++i) SAVE_MEMORY[i] = bytes[i];
SAVE_MEMORY[0] = 'F';