Loading...
Searching...
No Matches
main.c File Reference

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

#include <snes.h>
#include <snes/sram.h>

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.
 

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

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
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
  • 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

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

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)

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.