#include "game.h" static const char save_signature[] __attribute__((used)) = "SRAM_V113"; #ifndef GAME_TEST #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]; }; static void clear_progress(struct Progress *p) { p->partner = p->given = p->rod = p->count = p->members = p->active = 0; for (int i = 0; i < CAUGHT_CAPACITY; ++i) p->kinds[i] = 0xff; } static int valid_progress(const struct Progress *p) { if (p->count < 0 || p->count > 99 || (!p->given && p->count != 0) || p->members < 0 || p->members > CAUGHT_CAPACITY || (p->members && !p->partner) || p->active < 0 || p->active > p->members || (p->active && !p->partner)) return 0; for (int i = 0; i < CAUGHT_CAPACITY; ++i) if (i < p->members ? p->kinds[i] >= SPECIES_COUNT : p->kinds[i] != 0xff) return 0; return 1; } /* CRC-16/CCITT-FALSE: init FFFF, polynomial 1021, high byte first. */ TEST_INTERNAL unsigned save_crc(const unsigned char *bytes, int length) { unsigned crc = 0xffff; for (int i = 0; i < length; ++i) { crc ^= (unsigned)bytes[i] << 8; for (int bit = 0; bit < 8; ++bit) crc = ((crc << 1) ^ ((crc & 0x8000) ? 0x1021u : 0u)) & 0xffff; } return crc; } static int read_progress(struct Progress *p, struct Location *location) { unsigned char bytes[SAVE_RECORD_SIZE]; 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; /* Keep Sprout from the original version-one save format. */ if (bytes[4] == 1) { p->partner = bytes[5] == 1 && bytes[6] == 0xa5 && bytes[7] == 0x5a; return p->partner && valid_progress(p); } unsigned flags = bytes[5], count = bytes[6], version = bytes[4]; if (flags > (version >= 5 ? 47u : version == 4 ? 7u : 3u)) return 0; if (version == 2) { if (bytes[8] != 0x5a || bytes[7] != (0xa5 ^ 2 ^ flags ^ count)) return 0; } else if (version >= 3 && version <= 6) { unsigned checksum = 0xa5 ^ version ^ flags ^ count ^ bytes[7]; for (int i = 0; i < CAUGHT_CAPACITY; ++i) { p->kinds[i] = bytes[8 + i]; checksum ^= bytes[8 + i]; } if (version == 6) { if (save_crc(bytes, 17) != ((unsigned)bytes[17] << 8 | bytes[18])) return 0; location->room = bytes[13]; location->facing = bytes[14]; location->x = bytes[15]; location->y = bytes[16]; } else if (bytes[14] != 0x5a || bytes[13] != checksum) return 0; p->members = bytes[7]; } else return 0; p->partner = !!(flags & 1); p->given = !!(flags & 2); p->rod = !!(flags & 4); p->count = (int)count; p->active = version >= 5 ? (int)(flags >> 3) : 0; return valid_progress(p); } void load_progress(void) { struct Progress p; struct Location location; if (!read_progress(&p, &location)) clear_progress(&p); partner_owned = p.partner; balls_given = p.given; rod_owned = p.rod; ball_count = p.count; caught_count = p.members; active_member = p.active; for (int i = 0; i < CAUGHT_CAPACITY; ++i) caught_species[i] = p.kinds[i]; } void saved_location(struct Location *out) { struct Progress p; if (!read_progress(&p, out) || !location_valid(out)) default_location(out); } int save_progress(const struct Location *location) { struct Progress p, readback; struct Location restored; p.partner = !!partner_owned; p.given = !!balls_given; p.rod = !!rod_owned; p.count = ball_count; p.members = caught_count; p.active = active_member; for (int i = 0; i < CAUGHT_CAPACITY; ++i) p.kinds[i] = i < caught_count ? caught_species[i] : 0xff; if (!valid_progress(&p) || !location_valid(location)) return 0; unsigned char bytes[SAVE_RECORD_SIZE]; bytes[0] = 'F'; bytes[1] = 'Q'; bytes[2] = 'S'; bytes[3] = 'P'; bytes[4] = 6; bytes[5] = (unsigned char)(p.partner | (p.given << 1) | (p.rod << 2) | (p.active << 3)); bytes[6] = (unsigned char)p.count; bytes[7] = (unsigned char)p.members; for (int i = 0; i < CAUGHT_CAPACITY; ++i) bytes[8 + i] = p.kinds[i]; bytes[13] = (unsigned char)location->room; bytes[14] = (unsigned char)location->facing; bytes[15] = (unsigned char)location->x; bytes[16] = (unsigned char)location->y; 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'; if (!read_progress(&readback, &restored) || p.partner != readback.partner || p.given != readback.given || p.rod != readback.rod || p.count != readback.count || p.members != readback.members || p.active != readback.active || location->room != restored.room || location->x != restored.x || location->y != restored.y || location->facing != restored.facing) return 0; for (int i = 0; i < CAUGHT_CAPACITY; ++i) if (p.kinds[i] != readback.kinds[i]) return 0; return 1; }