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

SA-1 save: a boot counter that survives the power switch, in BW-RAM. More...

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

Classes

struct  SaveBlock
 The saved block: the magic word, then the counter. More...

Macros

#define SAVE_AT   0x0000
 BW-RAM byte offset of the save block.
#define SAVE_MAGIC   0x5A1E
 Marks a block this program wrote (any value a powered-up chip is unlikely to hold).

Functions

int main (void)

Variables

SaveBlock found
 What the battery held at power-on (read before anything is written).
u16 had_save
 1 when found carried the magic word (a previous run saved here)
SaveBlock now
 What this run wrote back.
u16 sa1_ready
 sa1IsReady() at boot: 1 on a working SA-1 cartridge

Detailed Description

SA-1 save: a boot counter that survives the power switch, in BW-RAM.

On an SA-1 cartridge the battery-backed memory is not the LoROM SRAM at $70:0000 but the SA-1's BW-RAM, seen by the SNES CPU at $40:0000 and writable only once SBWE ($2226) is set — which crt0 does on an SA-1 build. The sram module addresses it for you: this example is the same call sequence as memory/save_game, on the other memory.

It keeps one number: how many times the console has been switched on. At boot it reads the counter and a magic word; a matching magic means a previous run saved here (a fresh cart reads whatever the chip powered up with, so the magic decides, not a zero). It adds one, saves, and prints both the value it found and the value it wrote. Power-cycle: the screen shows one more. That is the whole test — and the one a console session needs (docs/HARDWARE_VERIFICATION.md, row 25).

SNES Concepts
  • SA-1 BW-RAM as the save memory: $40:0000 for the SNES CPU, SBWE first
  • A magic word tells a saved block from power-on garbage
  • The sram module is the same API on LoROM, HiROM, SA-1 and Super FX carts
What to Observe
  • "FOUND" is the saved counter (or "NONE" on a fresh cart), "NOW" is FOUND + 1; both printed as decimal
  • Switch the console off and on: NOW increments by one each time
  • "SA-1 READY" confirms the coprocessor booted (sa1IsReady)
Modules Used
console, dma, background, text, sa1, sram
See also
sram.h, sa1.h, memory/save_game, chips/superfx_save

Macro Definition Documentation

◆ SAVE_AT

#define SAVE_AT   0x0000

BW-RAM byte offset of the save block.

◆ SAVE_MAGIC

#define SAVE_MAGIC   0x5A1E

Marks a block this program wrote (any value a powered-up chip is unlikely to hold).

Function Documentation

◆ main()

int main ( void )

Variable Documentation

◆ found

SaveBlock found

What the battery held at power-on (read before anything is written).

◆ had_save

u16 had_save

1 when found carried the magic word (a previous run saved here)

◆ now

SaveBlock now

What this run wrote back.

◆ sa1_ready

u16 sa1_ready

sa1IsReady() at boot: 1 on a working SA-1 cartridge