diff --git a/Makefile b/Makefile index 09b78e9..839f67c 100644 --- a/Makefile +++ b/Makefile @@ -25,8 +25,9 @@ build/%.o: source/%.c source/game.h | build build/actors.o: source/wild_creatures.h build/audio.o build/main.o: source/audio.h -build/game.elf: build/start.o $(GAME_OBJECTS) gba.ld - $(CC) -mcpu=arm7tdmi -marm -nostdlib -T gba.ld -Wl,-z,noexecstack -Wl,-Map,build/game.map build/start.o $(GAME_OBJECTS) -lgcc -o $@ +# The bare-metal .data segment intentionally contains startup-relocated EWRAM code. +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 $(OBJCOPY) -O binary $< $@ diff --git a/README.md b/README.md index 7bfd361..dc26f13 100644 --- a/README.md +++ b/README.md @@ -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 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 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. diff --git a/gba.ld b/gba.ld index 6f9631b..355c0de 100644 --- a/gba.ld +++ b/gba.ld @@ -14,9 +14,11 @@ SECTIONS { . = ALIGN(4); } > ROM + /* Startup copies this range, including the leaf SRAM reader, before main. */ .data : { __data_start = .; *(.data*) + *(.ewram.text.sram_read) . = ALIGN(4); __data_end = .; } > RAM AT > ROM diff --git a/source/save.c b/source/save.c index 3be8c6e..16fbd16 100644 --- a/source/save.c +++ b/source/save.c @@ -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';