Loading...
Searching...
No Matches
sram.h File Reference

SNES SRAM (Save RAM) Functions. More...

#include <snes/types.h>

Go to the source code of this file.

Macros

#define SRAM_SIZE_16KB   0x04
 16KB SRAM (128Kbit)
 
#define SRAM_SIZE_2KB   0x01
 2KB SRAM (16Kbit)
 
#define SRAM_SIZE_32KB   0x05
 32KB SRAM (256Kbit)
 
#define SRAM_SIZE_4KB   0x02
 4KB SRAM (32Kbit)
 
#define SRAM_SIZE_8KB   0x03
 8KB SRAM (64Kbit) - Most common
 
#define SRAM_SIZE_NONE   0x00
 No SRAM (0KB)
 

Functions

u8 sramChecksum (u8 *data, u16 size)
 Calculate simple checksum.
 
void sramClear (u16 size)
 Clear SRAM to zero.
 
void sramLoad (u8 *data, u16 size)
 Load data from SRAM.
 
void sramLoadOffset (u8 *data, u16 size, u16 offset)
 Load data from SRAM at offset.
 
void sramSave (u8 *data, u16 size)
 Save data to SRAM.
 
void sramSaveOffset (u8 *data, u16 size, u16 offset)
 Save data to SRAM at offset.
 

Detailed Description

SNES SRAM (Save RAM) Functions.

Functions for saving and loading game data to battery-backed SRAM.

SRAM Overview

SRAM (Static RAM) on SNES cartridges is battery-backed RAM that persists when the console is powered off. It's used for save games.

Memory Layout (LoROM)

SRAM is mapped at bank $70, addresses $0000-$7FFF (32KB max). Most games use 2KB-8KB of SRAM.

Usage Example

// Define your save data structure
typedef struct {
u8 magic[4]; // "SAVE" to detect valid save
u8 checksum;
} SaveData;
SaveData save;
// Load save data at game start
sramLoad((u8*)&save, sizeof(SaveData));
if (save.magic[0] != 'S' || save.magic[1] != 'A') {
// No valid save, initialize defaults
save.score = 0;
save.level = 1;
save.lives = 3;
}
// Save when needed (e.g., at checkpoint)
save.magic[0] = 'S'; save.magic[1] = 'A';
save.magic[2] = 'V'; save.magic[3] = 'E';
sramSave((u8*)&save, sizeof(SaveData));
static u16 lives
Definition main.c:165
static u16 level
Definition main.c:164
static u16 score
Definition main.c:161
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:52
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:46
void sramLoad(u8 *data, u16 size)
Load data from SRAM.
void sramSave(u8 *data, u16 size)
Save data to SRAM.

ROM Header Requirement

To enable SRAM, your ROM header (hdr.asm) must set:

  • CARTRIDGETYPE $02 (ROM + SRAM)
  • SRAMSIZE $03 (8KB) or appropriate size
Author
OpenSNES Team

Macro Definition Documentation

◆ SRAM_SIZE_16KB

#define SRAM_SIZE_16KB   0x04

16KB SRAM (128Kbit)

◆ SRAM_SIZE_2KB

#define SRAM_SIZE_2KB   0x01

2KB SRAM (16Kbit)

◆ SRAM_SIZE_32KB

#define SRAM_SIZE_32KB   0x05

32KB SRAM (256Kbit)

◆ SRAM_SIZE_4KB

#define SRAM_SIZE_4KB   0x02

4KB SRAM (32Kbit)

◆ SRAM_SIZE_8KB

#define SRAM_SIZE_8KB   0x03

8KB SRAM (64Kbit) - Most common

◆ SRAM_SIZE_NONE

#define SRAM_SIZE_NONE   0x00

No SRAM (0KB)

Function Documentation

◆ sramChecksum()

u8 sramChecksum ( u8 data,
u16  size 
)

Calculate simple checksum.

Calculates a simple 8-bit checksum of data. Use this to verify save data integrity.

Parameters
dataPointer to data
sizeNumber of bytes
Returns
8-bit checksum (XOR of all bytes)
// Before saving:
save.checksum = 0;
save.checksum = sramChecksum((u8*)&save, sizeof(save));
sramSave((u8*)&save, sizeof(save));
// After loading:
u8 storedChecksum = save.checksum;
save.checksum = 0;
if (sramChecksum((u8*)&save, sizeof(save)) != storedChecksum) {
// Save data corrupted!
}
u8 sramChecksum(u8 *data, u16 size)
Calculate simple checksum.

◆ sramClear()

void sramClear ( u16  size)

Clear SRAM to zero.

Erases all SRAM contents by filling with zeros. Useful for "delete save" functionality.

Parameters
sizeNumber of bytes to clear (usually your save data size)
// Clear first 256 bytes (one save slot)
sramClear(256);
void sramClear(u16 size)
Clear SRAM to zero.

◆ sramLoad()

void sramLoad ( u8 data,
u16  size 
)

Load data from SRAM.

Copies data from battery-backed SRAM to Work RAM.

Parameters
dataPointer to destination buffer in Work RAM
sizeNumber of bytes to load (max 32KB)
u8 saveData[64];
sramLoad(saveData, 64);
Note
If no valid save exists, SRAM contents are undefined

◆ sramLoadOffset()

void sramLoadOffset ( u8 data,
u16  size,
u16  offset 
)

Load data from SRAM at offset.

Like sramLoad() but reads from a specific offset within SRAM. Useful for multiple save slots.

Parameters
dataPointer to destination buffer in Work RAM
sizeNumber of bytes to load
offsetStarting offset in SRAM (0-32767)
// Load slot 2 (each slot is 256 bytes)
sramLoadOffset(saveData, 256, 512);
void sramLoadOffset(u8 *data, u16 size, u16 offset)
Load data from SRAM at offset.

◆ sramSave()

void sramSave ( u8 data,
u16  size 
)

Save data to SRAM.

Copies data from Work RAM to battery-backed SRAM. The data will persist when the console is powered off.

Parameters
dataPointer to data in Work RAM to save
sizeNumber of bytes to save (max 32KB)
u8 saveData[64] = { ... };
sramSave(saveData, 64);
Note
Uses bank $70 (LoROM SRAM), starting at address $0000
Warning
Ensure ROM header has SRAM enabled!

◆ sramSaveOffset()

void sramSaveOffset ( u8 data,
u16  size,
u16  offset 
)

Save data to SRAM at offset.

Like sramSave() but writes to a specific offset within SRAM. Useful for multiple save slots.

Parameters
dataPointer to data in Work RAM to save
sizeNumber of bytes to save
offsetStarting offset in SRAM (0-32767)
// Save slot 2 (each slot is 256 bytes)
sramSaveOffset(saveData, 256, 512);
void sramSaveOffset(u8 *data, u16 size, u16 offset)
Save data to SRAM at offset.