SRAM battery-backed save with multiple save slots. More...
Classes | |
| struct | SaveState |
| Data structure representing one save slot's persistent state. More... | |
Macros | |
| #define | SAVE_SIZE 8 |
| Size in bytes of one save slot (must equal sizeof(SaveState)) | |
| #define | SLOT1 0 |
| SRAM byte offset for save slot 1 (starts at byte 0) | |
| #define | SLOT2 1 |
| SRAM byte offset multiplier for save slot 2 (starts at byte 8) | |
Functions | |
| int | main (void) |
| Main entry point – SRAM save/load demo with two slots. | |
Variables | |
| u16 | pad0 |
| Current joypad state (read each frame in the main loop) | |
| SaveState | vtl |
| Read buffer: populated by sramLoadOffset() when loading from SRAM. | |
| SaveState | vts |
| Write buffer: filled with test data before saving to SRAM. | |
SRAM battery-backed save with multiple save slots.
Demonstrates SRAM (Static RAM) persistence on the SNES. SRAM is a small battery-backed memory region (typically 2-32KB) mapped at $70:0000-$7D:FFFF (LoROM) that retains data when the console is powered off. This is how SNES cartridges implement save games.
The example uses two save slots at different SRAM offsets, each storing a SaveState struct (player position and camera coordinates). sramSaveOffset() copies RAM data to SRAM, and sramLoadOffset() reads it back. The loaded values are displayed as hex on screen to verify round-trip integrity.
The ROM header must declare SRAM support (USE_SRAM := 1) and size (SRAM_SIZE := 3 for 8KB) so emulators and flash carts allocate the battery-backed region.
| #define SAVE_SIZE 8 |
Size in bytes of one save slot (must equal sizeof(SaveState))
| #define SLOT1 0 |
SRAM byte offset for save slot 1 (starts at byte 0)
| #define SLOT2 1 |
SRAM byte offset multiplier for save slot 2 (starts at byte 8)
| int main | ( | void | ) |
Main entry point – SRAM save/load demo with two slots.
Sets up a text display with instructions and enters a loop where each button performs a different SRAM operation:
The SRAM region is battery-backed, so saved data persists across power cycles on real hardware and across emulator sessions (if SRAM saving is enabled in the emulator settings).
| u16 pad0 |
Current joypad state (read each frame in the main loop)
| SaveState vtl |
Read buffer: populated by sramLoadOffset() when loading from SRAM.
| SaveState vts |
Write buffer: filled with test data before saving to SRAM.