SRAM battery-backed save with multiple save slots. More...
Classes | |
| struct | SaveState |
| Data structure representing one save slot's persistent state. More... | |
Macros | |
| #define | CARD1_ROW 13 |
| The cards' first text row: slot 1's card is rows 13-16, slot 2's 19-22. | |
| #define | CARD2_ROW 19 |
| #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). | |
VRAM (word addresses): textModeInit() keeps $0000 (font) and $3800 (text map) | |
| #define | OBJ_VRAM 0x4000 |
| #define | TERM_MAP 0x3C00 |
| #define | TERM_TILES 0x1000 |
Functions | |
| int | main (void) |
| Main entry point – SRAM save/load demo with two slots. | |
| static void | show_slot (u8 row, u8 id, const char *what, const SaveState *s) |
| Write a slot's status in its card, and show Kit's icon there. | |
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 screen is a save terminal of the Rue basse: two slot cards on a terminal's glass (BG2), the text on BG1 in front. 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 in the slot's card to verify round-trip integrity, and Kit's icon appears in a card once the slot has been written or read.
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 CARD1_ROW 13 |
The cards' first text row: slot 1's card is rows 13-16, slot 2's 19-22.
| #define CARD2_ROW 19 |
| #define OBJ_VRAM 0x4000 |
| #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).
| #define TERM_MAP 0x3C00 |
| #define TERM_TILES 0x1000 |
| 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).
Write a slot's status in its card, and show Kit's icon there.
| row | The card's first text row (CARD1_ROW or CARD2_ROW) |
| id | The slot's sprite (0 or 1): its icon in the card's well |
| what | "SAVED" or "LOADED" |
| s | The values to print, or NULL after a save |
| 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.