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). | |
SRAM return codes | |
Returned by sramSave(), sramLoad(), sramSaveOffset(), sramLoadOffset() and sramClear(). The family returned nothing until 2026-09-21 and copied whatever it was asked; a refused transfer now copies NOTHING. The capacity checked against is the one your ROM declares: the SRAMSIZE byte of its header (SRAM_SIZE in your Makefile, 8 KB by default), capped by what this module addresses in one bank (32 KB on LoROM, 8 KB on HiROM). | |
| #define | SRAM_ERR_NO_SRAM 2 |
| The ROM header declares no SRAM (USE_SRAM := 1 is missing). | |
| #define | SRAM_ERR_RANGE 1 |
| offset + size exceeds the declared SRAM; nothing was copied. | |
| #define | SRAM_OK 0 |
| The transfer fits and was done (a size of 0 is also OK). | |
Functions | |
| u8 | sramChecksum (const u8 *data, u16 size) |
| Calculate simple checksum. | |
| u8 | sramClear (u16 size) |
| Clear SRAM to zero. | |
| u8 | sramLoad (u8 FAR *data, u16 size) |
| Load data from SRAM. | |
| u8 | sramLoadOffset (u8 FAR *data, u16 size, u16 offset) |
| Load data from SRAM at offset. | |
| u8 | sramSave (const u8 *data, u16 size) |
| Save data to SRAM. | |
| u8 | sramSaveOffset (const u8 *data, u16 size, u16 offset) |
| Save data to SRAM at offset. | |
SNES SRAM (Save RAM) Functions.
Functions for saving and loading game data to battery-backed SRAM.
SRAM (Static RAM) on SNES cartridges is battery-backed RAM that persists when the console is powered off. It's used for save games.
Source pointers may be in any bank (a const save template in ROM works); destination pointers are work RAM. Most games use 2 KB-8 KB of SRAM.
To enable SRAM, your ROM header (hdr.asm) must set:
| #define SRAM_ERR_NO_SRAM 2 |
The ROM header declares no SRAM (USE_SRAM := 1 is missing).
| #define SRAM_ERR_RANGE 1 |
offset + size exceeds the declared SRAM; nothing was copied.
| #define SRAM_OK 0 |
The transfer fits and was done (a size of 0 is also OK).
| #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).
Calculate simple checksum.
Calculates a simple 8-bit checksum of data. Use this to verify save data integrity.
| data | Pointer to data |
| size | Number of bytes |
Clear SRAM to zero.
Erases all SRAM contents by filling with zeros. Useful for "delete save" functionality.
| size | Number of bytes to clear (usually your save data size) |
Load data from SRAM.
Copies data from battery-backed SRAM to Work RAM.
| data | Pointer to destination buffer in Work RAM — a bank-0 object or a FAR (bank $7E) one; the copy runs as a block move into bank $7E, whose first 8 KB mirror bank 0 |
| size | Number of bytes to load |
Load data from SRAM at offset.
Like sramLoad() but reads from a specific offset within SRAM. Useful for multiple save slots.
| data | Pointer to destination buffer in Work RAM |
| size | Number of bytes to load |
| offset | Starting offset in SRAM |
Save data to SRAM.
Copies data from Work RAM to battery-backed SRAM. The data will persist when the console is powered off.
| data | Pointer to data in Work RAM to save |
| size | Number of bytes to save |
Save data to SRAM at offset.
Like sramSave() but writes to a specific offset within SRAM. Useful for multiple save slots.
| data | Pointer to data in Work RAM to save |
| size | Number of bytes to save |
| offset | Starting offset in SRAM |