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

+3 -2
View File
@@ -25,8 +25,9 @@ build/%.o: source/%.c source/game.h | build
build/actors.o: source/wild_creatures.h build/actors.o: source/wild_creatures.h
build/audio.o build/main.o: source/audio.h build/audio.o build/main.o: source/audio.h
build/game.elf: build/start.o $(GAME_OBJECTS) gba.ld # The bare-metal .data segment intentionally contains startup-relocated EWRAM code.
$(CC) -mcpu=arm7tdmi -marm -nostdlib -T gba.ld -Wl,-z,noexecstack -Wl,-Map,build/game.map build/start.o $(GAME_OBJECTS) -lgcc -o $@ build/game.elf: build/start.o $(GAME_OBJECTS) gba.ld Makefile
$(CC) -mcpu=arm7tdmi -marm -nostdlib -T gba.ld -Wl,-z,noexecstack -Wl,--no-warn-rwx-segments -Wl,-Map,build/game.map build/start.o $(GAME_OBJECTS) -lgcc -o $@
game.gba: build/game.elf tools/rom_header.py game.gba: build/game.elf tools/rom_header.py
$(OBJCOPY) -O binary $< $@ $(OBJCOPY) -O binary $< $@
+2
View File
@@ -93,6 +93,8 @@ The v6 format is 19 bytes: `FQSP` (0–3), version 6 (4), v5 flags (5: Sprout bi
The ROM uses homebrew game code `QST0`. The previous `FQ01` collided with mGBA 0.10.5's Classic NES detection, which forced EEPROM instead of SRAM. The ROM uses homebrew game code `QST0`. The previous `FQ01` collided with mGBA 0.10.5's Classic NES detection, which forced EEPROM instead of SRAM.
The SRAM backend sets eight waitstates without changing other WAITCNT bits and performs byte reads from a startup-relocated EWRAM routine; writes use byte accesses from ROM. Physical cartridges remain unverified. Hot cartridge removal and its Game Pak IRQ protection are not supported; power off before removing a cartridge.
## Music ## Music
Three original four-bar chiptunes loop automatically: a bright village tune (112 BPM), a calmer trail/indoor tune (94 BPM), and a faster battle tune (160 BPM). Two hardware square-wave channels play melody and bass, with quiet noise-channel percussion in the village and battle tracks. Menus and conversations keep the current tune playing; entering a battle switches themes, and leaving it restores the location's theme. Three original four-bar chiptunes loop automatically: a bright village tune (112 BPM), a calmer trail/indoor tune (94 BPM), and a faster battle tune (160 BPM). Two hardware square-wave channels play melody and bass, with quiet noise-channel percussion in the village and battle tracks. Menus and conversations keep the current tune playing; entering a battle switches themes, and leaving it restores the location's theme.
+2
View File
@@ -14,9 +14,11 @@ SECTIONS {
. = ALIGN(4); . = ALIGN(4);
} > ROM } > ROM
/* Startup copies this range, including the leaf SRAM reader, before main. */
.data : { .data : {
__data_start = .; __data_start = .;
*(.data*) *(.data*)
*(.ewram.text.sram_read)
. = ALIGN(4); . = ALIGN(4);
__data_end = .; __data_end = .;
} > RAM AT > ROM } > RAM AT > ROM
+20 -1
View File
@@ -5,6 +5,23 @@ static const char save_signature[] __attribute__((used)) = "SRAM_V113";
#define SAVE_MEMORY ((volatile unsigned char *)0x0e000000) #define SAVE_MEMORY ((volatile unsigned char *)0x0e000000)
#endif #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 { struct Progress {
int partner, given, rod, count, members, active; int partner, given, rod, count, members, active;
unsigned char kinds[CAUGHT_CAPACITY]; 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) static int read_progress(struct Progress *p, struct Location *location)
{ {
unsigned char bytes[SAVE_RECORD_SIZE]; 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); clear_progress(p);
default_location(location); default_location(location);
if (bytes[0] != 'F' || bytes[1] != 'Q' || bytes[2] != 'S' || bytes[3] != 'P') return 0; 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); unsigned crc = save_crc(bytes, 17);
bytes[17] = (unsigned char)(crc >> 8); bytes[18] = (unsigned char)crc; bytes[17] = (unsigned char)(crc >> 8); bytes[18] = (unsigned char)crc;
/* Invalidate first, commit last; a single record cannot recover interrupted writes. */ /* Invalidate first, commit last; a single record cannot recover interrupted writes. */
configure_sram();
SAVE_MEMORY[0] = 0; SAVE_MEMORY[0] = 0;
for (int i = 1; i < SAVE_RECORD_SIZE; ++i) SAVE_MEMORY[i] = bytes[i]; for (int i = 1; i < SAVE_RECORD_SIZE; ++i) SAVE_MEMORY[i] = bytes[i];
SAVE_MEMORY[0] = 'F'; SAVE_MEMORY[0] = 'F';