OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
The Opt-In Framework Tutorial

This tutorial covers the two smallest modules in OpenSNES: gameloop (one public function) and asset (two public functions, two macros). They are the first pieces a newcomer meets — the main() and the "get my PNG on screen" step — and both are deliberately optional.

PHILOSOPHY.md lists "Mandatory framework lifecycle" under Non-goals, in these words:

A user is welcome to write a main() with their own while(1) { WaitForVBlank(); ... } loop. The framework opt-ins are recommendations, not requirements.

That is not a disclaimer, it is the design. Both modules are linked only if you name them in LIB_MODULES (Principle 3 — modules are opt-in, never all-or-nothing):

LIB_MODULES := console dma background asset gameloop

asset transitively pulls dma and background (_DEP_asset in make/common.mk). gameloop has no _DEP_ line at all — it pulls nothing beyond the runtime that every ROM already links.

The corpus votes with its feet: three examples call gameLoopRun (basics/aim_target, input/controller); two use the asset bundles (backgrounds/mode1, demos/shmup). Everything else writes the loop and the loads by hand, and nothing about those examples is second-class. demos/shmup uses bgLoad and hand-writes its loop — the two opt-ins are independent of each other.

The third member of this family is the scene stack (sceneRun / scenePush / scenePop), which has its own page — see Game States Tutorial for that one.

gameLoopRun — the boilerplate it replaces

GameLoopConfig is a pair of function pointers. It is not its own struct: it is a typedef of Scene from <snes/scene.h>, so the same value can be handed to gameLoopRun today and to sceneRun tomorrow without touching the declaration.

typedef struct {
void (*init)(void); // called once, may be NULL
void (*update)(void); // called every frame, MUST NOT be NULL
Scene GameLoopConfig
Game loop callbacks. Alias for Scene from <snes/scene.h>.
Definition gameloop.h:106
static void update(u16 held)
One image of play. Flat u8/u16 only: no call through a pointer in the hot loop.
Definition play.c:98
A single scene's callbacks.
Definition scene.h:148

The whole implementation (lib/source/gameloop.c) is five lines:

void gameLoopRun(const GameLoopConfig *cfg) {
if (cfg->init)
cfg->init();
while (1) {
cfg->update();
}
}
void WaitForVBlank(void)
Wait for next VBlank period.
void gameLoopRun(const GameLoopConfig *cfg)
Run the OpenSNES game loop. Never returns.
void(* init)(void)
Called each time the scene is pushed onto the stack (not only the first; see the Behaviour section).
Definition scene.h:158
void(* update)(void)
Called every VBlank while the scene is on top of the stack.
Definition scene.h:173

So the call site becomes:

int main(void) {
.init = on_init,
.update = on_update,
};
gameLoopRun(&cfg);
return 0; /* never reached — gameLoopRun does not return */
}
int main(void)
Definition main.c:73
static void on_init(void)
Definition main.c:174
static void on_update(void)
Definition main.c:254

cfg may live on main()'s stack, as all three examples do, precisely because main never returns while the loop runs. The return 0 exists only to keep int main(void) well-formed; cproc/QBE has no noreturn path, so the compiler cannot know the call never comes back.

There is no NULL check on cfg->update. That is the same deliberate omission documented on Scene::update — a JSL into the register page crashes immediately and is more diagnosable than a silently skipped frame, and the per-frame test is not free at 1.79 MHz.

The exact per-frame order

This is worth stating precisely, because the framework's share of it is smaller than it looks:

# What runs Who owns it
1 init(), once, before any WaitForVBlank your code
2 WaitForVBlank() — sets vblank_flag = 1, then WAI templates/crt0.asm
3 NMI fires: frame_count++, dynamic-sprite flush hook, OAM DMA, tilemap DMA, BG scroll registers, joypad/mouse/scope read the NMI handler in crt0.asm
4 NMI clears vblank_flag, WaitForVBlank returns crt0.asm
5 update() your code

gameLoopRun itself contributes steps 2 and 5 and nothing else. Everything in step 3 — including the input read that populates padHeld / padPressed — happens in the NMI handler and happens identically for a hand-written loop. The framework does not read input, does not draw, and does not flush anything.

One consequence deserves emphasis: update() starts inside VBlank, but VBlank does not last through it. WaitForVBlank returns only after the NMI handler has run to completion and cleared the handshake flag (crt0.asm, the stz.w vblank_flag at the very end of the ISR). The handler takes 11-16 % of the VBlank (docs/PERF.md), so most of it is still ahead when update() starts — a small DMA at the very top of update() is safe, as in the DMA tutorial. But the game logic that follows runs into active display. For anything larger, write to the shadow buffers the lib gives you (the text tilemap buffer, oamMemory, the scroll shadows) and let the next NMI upload them — which is what the examples do.

What gameLoopRun will not do for you

It does not call consoleInit(), does not setMode(), does not touch palettes, does not setScreenOn(), and does not install or configure the NMI handler. All of that stays in your init callback, which is why any existing hand-written main() converts by moving two blocks of code into two static functions and changing nothing else. The canonical init body is the same one you would have written inline:

static void on_init(void) {
textPrintAt(9, 8, "VBLANK COUNTER");
WaitForVBlank(); /* let the first NMI DMA the tilemap buffer */
}
void setScreenOn(void)
Enable screen display.
void textModeInit(void)
Initialize text display mode — one-call setup for text-based examples.
void textPrintAt(u8 x, u8 y, const char *str)
Print a string at specific position.

Cost: one indirect call plus one direct call per frame, on the order of 30 cycles — the indirect call through cfg is not tail-call-optimised.

Asset bundles — the tileset as a value

A background needs three blobs (tiles, palette, tilemap), two numbers describing them (colour depth, map size) and two VRAM addresses. Written by hand that is six extern declarations, three subtractions and three separate calls, all positional. GfxAsset and BgAsset bundle the first five into one value:

typedef struct {
const u8 *tiles;
const u8 *tiles_end;
const u8 *palette;
const u8 *palette_end;
u16 color_mode; /* BG_4COLORS, BG_4COLORS0, BG_16COLORS, BG_256COLORS */
typedef struct {
GfxAsset gfx;
const u8 *tilemap;
const u8 *tilemap_end;
u8 map_size; /* SC_32x32, SC_64x32, SC_32x64, SC_64x64 */
static u16 tilemap[896]
Generated 32x28 tilemap (krom's fixed pattern — see buildTilemap).
Definition main.c:56
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47
A full background bundle: tileset + tilemap + map size.
Definition asset.h:129
A tileset bundle: graphics + palette + color depth.
Definition asset.h:104
static const u8 palette[]
Definition main.c:83

The struct stores begin/end pairs rather than sizes on purpose: the difference of two extern symbols is not a compile-time constant in C, so a size field could not be initialised in static const storage. The subtraction is done at load time instead.

Because the bundle is a value, it can be passed to a function, or stored in an array indexed at runtime as a level table, or swapped at a scene boundary. The addresses stay at the call site — the same tileset can be loaded to a different VRAM address on a different screen.

What one bgLoad call actually moves

lib/source/asset.c composes three existing calls, in this order:

void bgLoad(u8 bg, const BgAsset *asset, u8 palette_slot,
u16 tiles_vram, u16 map_vram) {
bgSetMapPtr(bg, map_vram, asset->map_size);
gfxLoad(bg, &asset->gfx, palette_slot, tiles_vram); /* tiles + palette */
dmaCopyVram(asset->tilemap, map_vram,
(u16)(asset->tilemap_end - asset->tilemap));
}
void gfxLoad(u8 bg, const GfxAsset *asset, u8 palette_slot, u16 tiles_vram)
Load a tileset (tiles + palette) and configure a BG's tile graphics pointer. (It used to be described...
void bgLoad(u8 bg, const BgAsset *asset, u8 palette_slot, u16 tiles_vram, u16 map_vram)
Load a full background bundle: tiles, palette and tilemap in one call. (The BG_LOAD macro this used t...
void bgSetMapPtr
Definition background.h:140
void dmaCopyVram(const u8 *source, u16 vramAddr, u16 size)
Copy data to VRAM (PVSnesLib compatible).
const u8 * tilemap
Pointer to tilemap data (the .map that opensnes-tileset writes).
Definition asset.h:133
u8 map_size
Tilemap layout. Use one of SC_32x32, SC_64x32, SC_32x64, SC_64x64.
Definition asset.h:140
const u8 * tilemap_end
One past the end of the tilemap data.
Definition asset.h:135
GfxAsset gfx
Tileset (tiles + palette + color mode).
Definition asset.h:131

and gfxLoad is the bgInitTileSet call you would otherwise write out:

void gfxLoad(u8 bg, const GfxAsset *asset, u8 palette_slot, u16 tiles_vram) {
bgInitTileSet(bg, asset->tiles, asset->palette, palette_slot,
(u16)(asset->tiles_end - asset->tiles),
(u16)(asset->palette_end - asset->palette),
asset->color_mode, tiles_vram);
}
void bgInitTileSet
Definition background.h:209
const u8 * tiles_end
One past the end of tile graphics. Size is computed at load time as tiles_end - tiles.
Definition asset.h:109
u16 color_mode
Color depth selector. Use one of: BG_4COLORS, BG_4COLORS0 (Mode 0), BG_16COLORS, BG_256COLORS....
Definition asset.h:120
const u8 * tiles
Pointer to tile graphics data.
Definition asset.h:106
const u8 * palette
Pointer to BGR555 palette data.
Definition asset.h:111
const u8 * palette_end
One past the end of the palette data.
Definition asset.h:113

So one bgLoad fans out into three transfers and two register writes: tiles to VRAM at tiles_vram, palette to CGRAM at a slot-derived colour index, tilemap to VRAM at map_vram, plus BGnSC (map address + size) and BGnNBA (tile address).

That fan-out is pinned by a functional test rather than by prose. testing/manifests/dma_mode1_bgasset.toml runs examples/backgrounds/mode1 — whose entire body is bgLoad(0, &bg, 0, 0x4000, 0x0000) — and asserts all three destinations independently: bytes in CGRAM at colour 0, bytes in VRAM under the tilemap at word $0000, and bytes in VRAM under the tiles at word $4000 (byte $8000; VRAM byte address is word address × 2). The manifest also asserts unsafe_writes = 0, i.e. every one of those transfers happened under forced blank. A field-order slip in BgAsset — swapped tiles/map pointers, a stale _end label — fails a named block instead of quietly changing the picture.

The palette slot argument

palette_slot is an index, not a CGRAM address. bgInitTileSet converts it according to color_mode, and the conversion is not uniform:

color_mode CGRAM colour index
BG_16COLORS slot * 16
BG_4COLORS slot * 4
BG_4COLORS0 (Mode 0) bg * 32 + slot * 4 — the BG number participates
BG_256COLORS always 0 — palette_slot is ignored

For instance, three BG_16COLORS bundles loaded at slots 2, 4 and 0 land at CGRAM colours 32-47, 64-79 and 0-15 respectively:

bgLoad(0, &bg1, 2, 0x2000, 0x0000);
bgLoad(1, &bg2, 4, 0x3000, 0x0400);
bgLoad(2, &bg3, 0, 0x4000, 0x0800);
#define DECLARE_BG_ASSET(name, color_mode_, map_size_)
Declare a static const BgAsset from opensnes-tileset-style symbol pairs.
Definition asset.h:212
#define BG_16COLORS
Definition background.h:47
#define SC_32x32
Definition background.h:36

These calls belong in setup, under forced blank

bgLoad and gfxLoad DMA into VRAM and CGRAM with no VBlank gating of their own. They are setup calls, not per-frame calls. Two idioms in the corpus, both correct:

Prefer the explicit form in new code; it survives being moved into a scene's init, where the screen is already on.

Producing the symbols the macros expect

DECLARE_BG_ASSET(name, color_mode, map_size) expands to six extern declarations plus a static const BgAsset name initialised from them. It requires the symbol naming convention <name>_tiles, <name>_pal, <name>_map, each with an _end sibling. DECLARE_GFX_ASSET(name, color_mode) is the same without the map pair.

You do not write those symbols yourself. The blobs come out of opensnes-tileset, driven by a settings file beside the picture — backgrounds/mode1 has res/opensnes.png.toml:

tool = "opensnes-tileset"
[convert]
colors = 16

make/common.mk runs the converter before the first C object and links the res/opensnes_data.as it writes (one ASSET_SECTION per blob — tiles, map, palette — so nothing lands in bank $00). The generated res/opensnes.inc is the entire contract between the converter and the macro: the six extern declarations in that naming, and the DECLARE_BG_ASSET itself, so the C side is one line:

#include "res/opensnes.inc" /* DECLARE_BG_ASSET(opensnes, BG_16COLORS, SC_32x32) */

A picture converted with lz = true, or one whose tileset is cut in 32 KB parts, gets the plain extern declarations instead of a bundle, with a comment saying why (bgLoad() would copy compressed or partial bytes).

The macro is sugar; the struct is the contract. Nothing stops you from filling a BgAsset yourself when the symbols do not follow the convention, or when the pointers are computed rather than linked.

ASSET_SECTION (templates/assets.inc, included in every assembled file) is the way to declare asset data: SEMISUPERFREE BANKS from the last bank down to 1 (ASSET_BANKS, ROM_BANKS - 1 to 1), so bank $00 is never a candidate. A bare superfree section lets the linker pick the first bank that fits — bank $00 — and fills the code bank with data.

Side by side with the hand-written equivalent

The honest measure of a convenience is how much it removes. Here is backgrounds/mode0, which loads four layers manually, against the same shape written with bundles.

Manual — examples/backgrounds/mode0/main.c, one layer's worth (the example gets these declarations from res/bg0.inc):

extern const u8 bg0_tiles[], bg0_tiles_end[]; /* tiles */
extern const u8 bg0_pal[], bg0_pal_end[]; /* palette */
extern const u8 bg0_map[], bg0_map_end[]; /* tilemap */
bgInitTileSet(0, bg0_tiles, bg0_pal, 0, bg0_tiles_end - bg0_tiles, 8, BG_4COLORS0, VRAM_BG0_TILES);
dmaCopyVram(bg0_map, VRAM_BG0_MAP, bg0_map_end - bg0_map);
#define VRAM_BG0_TILES
Definition vram_map.h:11
#define VRAM_BG0_MAP
Definition vram_map.h:7
#define BG_4COLORS0
Definition background.h:51

Bundled:

Three externs and three calls become one declaration and one call. The saving compounds: mode0 repeats that block four times, so the layer-loading body goes from twelve statements to four. What actually reaches the hardware is identical — same bgInitTileSet, same dmaCopyVram, same bgSetMapPtr, in a slightly different order.

The same comparison for the loop, the shape of examples/input/controller against its hand-written form:

/* hand-written */ /* framework */
int main(void) { int main(void) {
textPrintAt(9, 8, "..."); .init = on_init,
WaitForVBlank(); .update = on_update,
while (1) { gameLoopRun(&cfg);
WaitForVBlank(); return 0;
tick(); }
}
return 0;
}
static u16 tick
Frames since boot, for the walk and the hover.
Definition main.c:102

That is the whole delta — two lines of structure. The framework's value here is not line count, it is that the shape of an SNES program (init once, then one body per VBlank) is named in the API instead of being folklore a beginner has to infer from an example.

When the framework gets in the way

Reach for the hand-written version when:

  • The synchronisation rhythm is not one update per VBlank. Half-rate logic, a fixed-step accumulator, audio polled twice per frame, a busy loop that deliberately skips WaitForVBlank during a long load — all of those want an explicit loop. gameLoopRun has exactly one cadence and no hook to change it.
  • The per-frame body swaps between screens. Dispatching a switch from inside update works, but the scene stack exists for this — see Game States Tutorial. Because GameLoopConfig is Scene, promoting a game is a one-line change at the call site: gameLoopRun(&cfg) becomes sceneRun(&cfg).
  • You need something between WaitForVBlank and the update, such as a profiling marker or a scanline-timed write. There is no pre-update hook.
  • The asset does not fit the bundle. Sprite assets have their own CGRAM offset (128) and OAM conventions and are out of scope for asset — use oamInitGfxSet. Tiled map data (tilesetdef, tilesetatt, large levels) goes through mapLoad instead, see Scrolling maps.

Sharp edges worth knowing

  • Sizes are truncated to u16. Both loaders cast the pointer difference to u16, so a single bundle component of 64 KB or more wraps silently. In practice no tileset gets close, but a concatenated multi-screen tilemap could.
  • bg is not range-checked. bgSetMapPtr and bgSetGfxPtr switch on bg and do nothing at all for a value above 3. A typo'd background number produces a silent no-op, not an error.
  • bgLoad writes BGnSC before the tilemap DMA. The register is set from asset->map_size first; the map bytes follow. Under forced blank this is invisible, but do not call bgLoad on a live layer mid-frame and expect a clean swap.
  • The asset struct holds raw pointers with no ownership. The blobs live in ROM via .incbin; there is no copy and no lifetime tracking. Nothing here allocates — consistent with the Non-goal on hidden allocations.
  • gameLoopRun never returns, and the compiler does not know it. Expect a return 0 after every call site; that is the documented idiom, not a smell.

See also