OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
main.c File Reference

SRAM battery-backed save with multiple save slots. More...

#include <snes.h>
#include <snes/sram.h>
#include "res/terminal.inc"
#include "res/icon.inc"

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.

Detailed Description

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.

SNES Concepts
  • SRAM battery-backed persistence for save games
  • Multiple save slots via byte offset addressing
  • ROM header SRAM size declaration (affects emulator behavior)
  • Struct serialization to/from SRAM
  • Mode 0: the text on BG1 and a 2bpp picture on BG2, each with its own palettes (BG2's start at CGRAM 32)
What to Observe
  • Press A to write test data to Slot 1, B to read it back
  • Press X to write different data to Slot 2, Y to read it back
  • Loaded values (camX, camY, posX, posY) appear as hex numbers in the slot's card, with Kit's icon
  • Data persists across emulator reset if SRAM saving is enabled
Modules Used
console, dma, text, background, sprite, input
See also
sram.h, console.h, text.h

Macro Definition Documentation

◆ CARD1_ROW

#define CARD1_ROW   13

The cards' first text row: slot 1's card is rows 13-16, slot 2's 19-22.

◆ CARD2_ROW

#define CARD2_ROW   19

◆ OBJ_VRAM

#define OBJ_VRAM   0x4000

◆ SAVE_SIZE

#define SAVE_SIZE   8

Size in bytes of one save slot (must equal sizeof(SaveState)).

◆ SLOT1

#define SLOT1   0

SRAM byte offset for save slot 1 (starts at byte 0).

◆ SLOT2

#define SLOT2   1

SRAM byte offset multiplier for save slot 2 (starts at byte 8).

◆ TERM_MAP

#define TERM_MAP   0x3C00

◆ TERM_TILES

#define TERM_TILES   0x1000

Function Documentation

◆ main()

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:

  • A: write test values to Slot 1
  • B: read Slot 1 and display values as hex
  • X: write different test values to Slot 2
  • Y: read Slot 2 and display values as hex

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).

Returns
0 (never reached – infinite game loop)

◆ show_slot()

void show_slot ( u8 row,
u8 id,
const char * what,
const SaveState * s )
static

Write a slot's status in its card, and show Kit's icon there.

Parameters
rowThe card's first text row (CARD1_ROW or CARD2_ROW)
idThe slot's sprite (0 or 1): its icon in the card's well
what"SAVED" or "LOADED"
sThe values to print, or NULL after a save

Variable Documentation

◆ pad0

u16 pad0

Current joypad state (read each frame in the main loop).

◆ vtl

SaveState vtl

Read buffer: populated by sramLoadOffset() when loading from SRAM.

◆ vts

SaveState vts

Write buffer: filled with test data before saving to SRAM.