gbsdk.module
README.md
gbsdk
Tile allocation for Game Boy cartridges, by name.
A Game Boy cartridge has one flat tile array, and everything indexes into it with a bare number: the background map, each 8×16 sprite, the font. When two systems each pick their own numbers they overwrite each other, and the damage surfaces somewhere else entirely — a path tile landing on a courier's walk frame shows up as a glitched sprite three screens away from the cause.
This module makes every tile a named claim, made once. A second claim on an index is an error naming both owners at build time.
Use
local Sdk = require("gbdev.gbsdk")
local t = Sdk.new()
local font = t:font() -- 40 glyphs: 0-9 A-Z space . ! ?
local grass = t:tile("GRASS") -- one 8x8 tile
local courier = t:sprite("COURIER_DOWN") -- four tiles: two 8x16 frames
local parcel = t:tiles("PARCEL", 4) -- four consecutive tiles
t:index("GRASS") -- the number that name was given
t:charIndex("A") -- the glyph tile for a character
t:text("PRESS START") -- a string as tile indices, 0xFF terminated
t:equates() -- "GRASS equ 41\n..." for the assembly
t:check() -- { used =, total =, free = { ... } }
Emit t:equates() into the assembly and the game's equ constants and its
tile data come from one allocation, so they cannot disagree.
The rules it enforces
- No two claims may share a tile. The error names both.
- A new name cannot reuse an existing one.
- An 8×16 sprite starts on an even index, because the hardware ignores bit 0 of a sprite's tile number — an odd base silently draws the tile next door. The allocator pads rather than letting that happen.
- The set cannot grow past
limit(256 by default, the addressable range).
Adopting it in an existing game
A cartridge that already picks its own numbers can get the checking without a
rewrite: hand validate the ranges it uses and it raises on any overlap.
Sdk.validate({
{ "font", 0, 40 },
{ "courier", 50, 12 },
{ "path_edge", 60, 2 }, -- raises: overlaps "courier"
})