moycore 0.4
Contents

Running moy carts on your hardware#

You have a device — a handheld, a firmware, a simulator, an emulator front-end — and you want the catalogue to run on it. This is the door for you.

SPEC.md is the contract and settles every question of behaviour. This document is the part it does not contain: what to build first, what you may skip, what you must refuse, and how to prove you got it right.

The other door is GUIDE.md, for writing carts rather than running them.

What a console owes a cart#

Less than you think. The console is a fixed-size machine with a small verb table, and the platform underneath it supplies four things: somewhere to put pixels, a way to read buttons, a millisecond clock, and somewhere to keep 256 integers. Audio is a fifth and it is optional — §8.2 makes silence a valid rendering.

Everything else — the raster, the palette, the font, the cart layout, the sandbox, the tick model — is the spec's, and you can have all of it as a C library without writing it.

Two routes get you there.

libmoy/ is the console as C99: no dependencies, no allocation, the verb table, the Lua binding with §4.1's sandbox already enforced, and the §8 synthesizer as an optional module. Three ports ship with it — SDL2 desktop, ESP-IDF component, WebAssembly — and port/sdl2/main.c is a complete playable console in a couple of hundred lines, written to be read as the worked example of a porting layer. Read it down to its hot reload comment and stop there: above that line is what a platform owes the console, and below it is an opt-in file watcher for whoever is writing the cart, which yours will not want.

This is the recommended route, and adopting moy this way should cost you a shim rather than a project. libmoy/README.md is its own documentation.

Route B — implement it yourself#

Entirely legitimate — a spec whose only implementation is somebody else's library has not proven anything. You are writing the raster in your own language against your own constraints, and the rest of this document is mostly for you. Route A implementers should still read the conformance section, refusal semantics, and sideload, because those are yours either way.

PURR OS took this route, and it is worth reading before you start. It runs moy as an app on ESP32 hardware with its own raster, its own cart loader and its own Lua binding — none of it libmoy, and written against the spec as it stood at 0.1. Its platform binding is around two hundred lines, which is the claim this document keeps making, made by somebody who did not write the spec: the console is the part you link or copy, and the platform is a shim. It is also the only host this project knows of that shares no code with this repository, which makes anything it agrees with worth more than another agreement from inside.


The order to build it in#

The point of this order is that each step is provable before the next one exists. Nothing here asks you to have a working console before you can find out whether your circles are right.

1. The raster, from verb traces#

Start here, before any Lua, any cart loader, any frame loop.

conformance/traces/*.json is every scene as a flat list of verb calls with integer arguments. A replayer for that format is roughly forty lines in any language. Write it, point it at a scene, and diff your framebuffer against conformance/golden/*.png.

That gives you a checkable rasterizer on day one. The alternative — build everything, then find out your circ is a pixel wide at r=1 — is how ports go badly.

The verbs are §6 (primitives, text, camera, clip, pal, palt, layers), §7.1 (sprites) and §7.2 (the map); a trace's three layer calls are described at the top of conformance/trace.py. The scenes are chosen so that each one fails for a distinct reason; conformance/README.md has the table of what each catches.

Two that catch nearly everyone:

  • clip is screen space and applies after camera. An implementation that clips in world space passes both features tested separately and fails camera_clip.
  • Both palettes are draw time, and they compose. pal(c0, c1, 1) is a second remap applied after the first as pixels are written; neither moves a pixel already on the canvas (§6, §12.1). A port that applies the screen palette as a pass over the finished frame fails screen_pal -- and, on a direct-colour canvas, would have paid half a frame for it.

2. The palette and the font are data — generate, never transcribe#

palette.json and font.bin sit beside SPEC.md and are normative. Read them or generate your source from them; do not hand-copy the numbers. A transcribed array of colours compiles, runs, looks correct, and disagrees with every other implementation about one entry in the extended range. libmoy's make data exists precisely to make that impossible, and records which spec commit it came from.

The font is one byte per column, LSB at the top, 96 glyphs covering ASCII. Text conformance is all-or-nothing: any glyph off by a bit fails every text scene.

3. The cart loader, and what to refuse#

A cart is a folder (§3). Reading it is the easy half; the rules about what to do when something is unexpected are the half that makes carts portable, and they are not symmetric:

the cart saysyou should
a manifest field you do not knowignore it — vendors annotate carts, and minor versions add fields
a "format" level above yoursrefuse the cart as a newer console's, before running a frame (§13)
"extensions": ["something"] you lackrefuse the cart, by name, before running a frame (§10)
"runtime" naming a binding you lackrefuse the cart (§3.1, §15) — never hand the file to your Lua VM anyway
"canvas" outside the closed setrefuse the cart (§1) — running at a size it did not ask for breaks every coordinate
"sources" listing several scriptsrun them all, in order, each its own chunk (§4); refuse a list that omits main or names a file the folder lacks
an "icon" out of range or past the sheetignore it and choose your own (§3.4)
a "genre" off §3.7's list, a tag off its rule, a "description" past 240 bytesignore the genre and that tag, cut the description (§3.7)
a cover.png outside §3.6's profile, or one that fails to decodeignore it: draw the cart as if it had none (§3.6)
a map larger than the format allowsreject it rather than allocating past your budget (§3.3)

The line, in §3.4's words, is that capability fields refuse and cosmetic ones degrade. An icon you cannot draw costs the player nothing; a capability you do not have costs them a crash halfway through a frame, which is the failure this format exists to prevent.

Refuse cleanly — tell the user which cart, and which requirement. A crash is not a refusal.

sources is the one row above that is neither a refusal nor a degrade: it is work you have to do. Load each named file with your VM's own load call, one chunk each, in the listed order, and name the chunk after the file — that last part is what makes §4.3's line number land in a file the author can open. Never join the scripts into one buffer first: a local would then cross a file boundary on your console and nowhere else, which is a cart that runs only on you. Absent, sources is [main], so the one-file path you already have is the general path with a list of one.

4. The Lua sandbox#

§4.1 fixes the standard library at exactly base (minus a handful of escape hatches), math, string, table and coroutine. Absent entirely: io, os, debug, package.

This is a ceiling, not a floor. A host that exposes more does not fail today; it accumulates carts that run only on it, and that breaks the format for everyone. The temptation is real, because luaL_openlibs is one call and doing it correctly is a dozen.

The robust implementation is to not compile the libraries in at all, rather than opening everything and nil-ing entries out afterwards — that is what libmoy does, and a sandbox by subtraction is one refactor away from being no sandbox. PURR OS reached the same conclusion independently, from the other side: it vendors Lua privately for the cart app and leaves liolib.c, loslib.c and loadlib.c out of the build, so a bug in its sandbox setup has nothing to reach.

Build Lua with LUA_32BITS (§4.2). Nothing stops you shipping a 64-bit build, and §4.2 says why it still conforms — but its floats diverge from the reference's in the last digits, so frames captured from it are not golden.

Then write a must-fail test: a cart that reaches for io has to fail to load, on every conforming host. The suite does not check this for you (see below), so this is a test you own.

5. The tick and the buttons#

30 Hz, or 60 if the cart asks — and the tick rate is the cart's, never yours to lower (§5). Place the ticks on your own clock: catch a late frame up with extra ticks only while a tick is cheap (under half the period), and write off what you cannot pay rather than bursting.

_update(dt) then _draw(), once per tick, and never _draw before the first _update. Under load, degrade only the way §5 sanctions — the draw divisor, whose rule and its one forbidden alternative are stated there. What §5 cannot tell you is which way to lean when you write the loop: take the divisor rather than let the tick rate sag, and hand dt the tick period rather than the wall time you actually spent.

Map your hardware onto the logical buttons of §7.3. Four directions plus a and b are mandatory; run is not. Buttons your device has that the console does not name are yours to keep — a cart polling for a button you do not implement must simply read not-pressed.

btnp's edge rule is §7.3's, and the trap is on your side of it: an input layer that already autorepeats for a menu system has to stop doing it for the cart.

Two rules about exit that are easy to get backwards: you own the exit gesture — a cart is never required to provide one — and quit() is the cart ending itself, which you honour by returning to wherever it was launched from. A Lua error also ends the cart, and you must report it with the script line number rather than swallowing it (§4.3).

6. Persistence#

256 signed 32-bit slots, per cart (§9). You may defer writes; you must land them before the cart exits. Keying them by cart identity is yours to design — the title is not unique, so most hosts hash the cart.

A device with nowhere to write can return 0 for every slot and accept every write, and carts will behave. That is a real degradation, not a conformance failure, but say so in your documentation.

7. Audio, last and optional#

§8 is a synthesizer, not a sample player: eight waveforms generated on the fly, per-note effects numbered as PICO-8 numbers them, four channels with music claiming from the top and effects round-robining what is left.

Audio is excluded from pixel conformance (§8.3) and two hosts will not produce bit-identical samples. What matters is the semantics — channel claiming, effect behaviour, the keyed-rest rule that makes imported slides land, per-row durations — because ported music depends on them musically.

If you want it and you are in C, libmoy/include/moy_audio.h renders §8 into a buffer and asks the platform only for a sample rate and somewhere to push samples. If you do not want it, implement the verbs as no-ops. They must not error.


Memory: what to reserve, and where#

§1.1 states the floor as a table of allocations and a total, and it is a floor, not a target. Three things about it are worth reading twice:

  • The kind of RAM is unspecified. Internal SRAM, PSRAM, any mix; §1.1's only test is whether your tick survives it. That makes the floor trivial on a board with PSRAM and binding only on single-die parts.
  • "However you like" includes shutting things down. A game mode that suspends other subsystems while a cart runs is a perfectly good way to free the budget.
  • One full-screen layer is inside the floor. make_layer must therefore succeed at least once. Beyond that you may decline and return nil, and carts are written to handle it (§6).

Where to put what is a quality question you should measure on your own board, not a conformance one — a cart can observe none of it. RATIONALE.md's memory section records what the reference implementation measured on its.

Two pixel formats, one raster#

You may keep the canvas as palette indices, or render directly to RGB565 and pay roughly double the framebuffer for it. §1.1 makes that the host's choice explicitly. In libmoy it is a compile flag over the same source, and both builds are judged by the same golden frames — the suite resolves back to indices before diffing.

Which is faster depends on your display pipeline, and the difference is smaller than the argument about it.

Your physical display need not match the canvas: scale and/or letterbox onto your glass, integer factors recommended. A cart honouring view(w, h) is telling you it only uses a centred region, which is your chance to blow that region up instead of framing it in black. The cart never learns your resolution.

Proving it: the conformance suite#

§11 is the definition: an implementation conforms when it runs the suite and produces pixel-identical output.

python3 conformance/run.py --player "./build/yourplayer --headless --dump {out} {cart}"

The runner substitutes {cart} and {out} and invokes your command once per scene. conformance/README.md specifies the two frame formats it will accept and the exact byte layout of each; the thing to know before you read it is that one of them is your framebuffer written out verbatim, so a firmware port owes the suite a single fwrite and no image codec whatsoever.

Nothing in a scene moves, and nothing reads a clock, a random number or a button — so your capture can be any frame you like, and a headless build with a frame counter is a perfectly good adapter.

There are two suites and you need both:

  • The traces check a raster, and you can reach them on day one.
  • examples/verbs.moy checks a host — the tick model, input edges, the host-dependent verbs, the 3D verbs. It is played and looked at; there is no golden for it.

conformance/run.py --diff out/ writes difference frames for whatever failed, which is how you find the one row you are wrapping.

The §6.1 verbs (tri, trib, sspr, tline) get scenes of their own, and those scenes are counted (§11) since 0.3 — they were reported but excluded before that, so a port written against the older core has them left to do. That pair is where a real board was caught disagreeing by thousands of pixels; do not skip them because the names still say "provisional".

What the suite does not cover#

Three gaps, stated plainly so you do not mistake a green run for a full one:

  1. The §4.1 sandbox. No scene reaches for io, so a player passes everything here with its sandbox wide open. The suite asks players for frames, and a correctly refused cart produces none — so checking the ceiling needs a runner that can demand a failure, which has not been designed. Write that test yourself; this repository does it for its C core in CI.
  2. Audio, by design (§8.3).
  3. Anything about your device — thermals, battery, how it behaves when the SD card is pulled. Nobody else can test those.

Also worth knowing about provenance: the goldens are rendered by moycore, libmoy is a transcription of it, and the WebAssembly player agrees with both — but all three share one lineage. An independent implementation that agrees is worth more to this project than another one that descends from the same raster. conformance/README.md is candid about what was lost when the last independent one was retired, and PURR OS is currently the only implementation outside this repository that could replace it. If you are writing Route B, your agreement is evidence nobody here can generate.

Compiled carts: "runtime": "wasm"#

A cart can carry a WebAssembly module in place of its Lua. That binding is SPEC.md §16, and it is optional: refusing such a cart cleanly (§3.1) is all a conforming host owes it. A host that takes it on implements §16's import table, and libmoy/src/moy_wasm.c is that table already written in C, compiled only when you ask for it: over WAMR with MOY_WASM, or with MOY_WASM_JS for a host whose own JavaScript engine runs the cart. moy check tells an author and a host alike whether a cart's module keeps to §16's shape.

Three hosts in this repository take the binding on, and between them they show the whole of the job:

  • libmoy's harness (libmoy/test/wasm_test.c) is the smallest: WAMR, the table registered, the module checked before its memory exists, the three hooks called. Start there.
  • The desktop player (libmoy/port/sdl2/wasm_cart.c) is the same over WAMR's interpreter inside a real frame loop, with the part §16 leaves to a host: the cart's footprint checked against what the player gives a cart, and a trapped frame never shown. A Lua cart and a compiled one share the player because the raster is linked twice, the direct-colour copy under moy565_ names (libmoy/port/moy565.h).
  • The web player runs the cart as a sibling module on the browser's engine, never an engine inside the console: each import is a JavaScript adapter over the same C (libmoy/port/wasm/cart.c, libmoy/port/wasm/page/cart.js), and a pointer into the cart's memory crosses as a copy. What the binding asks of a page in C is one file, libmoy/port/wasm/embed.c, which another page takes as it is.

How your host executes the module#

That is host policy, like where you put PSRAM (§1.1), and it is nowhere in the contract: an interpreter, AOT, a cache, XIP — whatever holds the tick on your board. What the reference console and this repository learned doing it, so the next implementer inherits it:

  • Browser, desktop: run the .wasm directly. No install step, and the browser is the fastest tier. The web player instantiates the cart as a sibling module whose imports are adapters over the console's verbs — never a WebAssembly interpreter nested inside a WebAssembly console — and the desktop player runs the module on WAMR's interpreter, which a desktop has the speed for.
  • A microcontroller: compile ahead of time. An interpreter does not pay for itself there (RATIONALE.md, "The WebAssembly binding"). On the reference console's RISC-V board (ESP32-P4) external RAM carries no PMP entry, so a plain AOT module loads from a file straight into PSRAM and runs there, the caches synced after the loader writes the text — no flash partition and no per-install wear; XIP from a partition stays an option for a host that wants the text out of RAM. The Xtensa floor board (ESP32-S3) loads the same way, through the chip's instruction-bus alias of external RAM; that alias is fetch-only, so the module is compiled without literal pools (wamrc --size-level=0). Doom runs from that path on both (moybyte#158).
  • A compiled module is native code. Whatever the sandbox promises lives in the compiler's output, not in your loader, so a host that runs AOT modules decides whom it trusts to have compiled them. The reference console loads a module only if a key inside it names the .wasm it came from, the runtime build and the compiler flags, and only if it is signed or its owner has chosen to run unsigned ones.
  • Distribution. A store may serve pre-compiled variants per architecture beside the canonical .wasm. wamrc ships prebuilt for x86-64 and RISC-V targets; the Xtensa backend is not in that binary and needs a build against Espressif's LLVM fork, which a store does once. Runtime and compiler versions must match.
  • Frictions already paid for. WAMR's LIBC_WASI defaults on and fails riscv32 builds; REF_TYPES defaults differ between its linux and esp-idf paths; a br_table-heavy module loads on linux and is rejected by the esp-idf build (dispatch through a function-pointer table instead); WAMR must run on a real pthread under ESP-IDF; and its esp-idf platform layer needed fixes on both boards (the S3's dual-bus mirror, executable PSRAM on the P4), which the reference implementation carries in its pinned fork of WAMR (moybyte-org/wasm-micro-runtime, the pin libmoy's Makefile fetches).

A compiled cart's sound is snd, a stream of samples. A host with audio hands the binding a queue — libmoy's moy_stream is one — and adds it into its output after the synth, under the same volume; the desktop player does it in its SDL callback, the web player in the pull its page makes each frame. A host without audio leaves the binding's snd callback NULL, and the binding drains the queue by the host's clock and drops the samples, so the cart sees the same backpressure either way.

A cart's files are §9.1's and §16.12's: the paths its manifest declares writable, kept per cart and outside its folder. libmoy's bindings hold every path to the declaration and the 1 MiB cap and hand the host four calls (written, write, erase and list, in moy_wasm.h for a compiled cart and in moy_host for a Lua cart, beside read for the cart's own folder; moy_files_bind and moy_files_bind_host fill either); the host decides where the files live and how it knows one cart from another, which, like pmem's keying, is yours. Two things are not: a write is whole or not at all — write a new copy beside the old and swap it in, and finish or discard a half-made one the next time the store opens — and it is on the medium before write returns. A host on a filesystem can take libmoy/port/moy_files.c as it is: one flat folder per cart, each path kept under a lowercase file name every filesystem holds, which is what the desktop player and libmoy's harness keep their files in. The web player keeps them in the page's localStorage (libmoy/port/wasm/page/cart.js's storageFiles). Removing a cart removes its files: on a console with a store that is the store's job, and a player whose carts are plain folders has nothing to see.

conformance/wasm_run.py holds a host to the compiled-cart scenes' RGB565 goldens and to the refusals, by the same kind of player protocol as SPEC.md 11's; its write scene runs twice on one store, as a cart does across a restart.

Extensions: adding your own without forking core#

Consoles will do more than core, and that is expected — a shell, a radio, an authoring format, a second cart language. None of that costs you anything, provided it arrives as an extension (§10) rather than as an edit.

§10 states the single rule, and it is worth reading there rather than paraphrased here: your additions may not reach into territory core has already settled, and when the two disagree it is your console that gives way. That is what lets a cart author treat core as a floor everywhere instead of as your console's particular dialect.

The mechanism is a manifest declaration, and the register of standard extensions is empty — the README explains why every candidate so far collapsed back into core. Apply §10's test before you reach for the mechanism: if a cart can be shielded from the absence of your feature, declaring it buys the author nothing and costs them every console that lacks it.

A cart may also use your extension opportunistically — check the verb exists, declare nothing — which means your extra capabilities can light up on your console without making a cart non-portable. That is the pattern to document for your users.

A compiled cart reaches an extension's verbs by importing them from the module the extension names (SPEC.md §16.2). libmoy types those imports for you: hand moy_wasm_check_bytes_ext (or, over WAMR, moy_wasm_check_ext) a moy_wasm_ext per extension you carry that the cart declares, its rows in the same NativeSymbol form as the "moy" table, and register them with the engine under the extension's module name before the load. How a row is served — which thread runs it, what it may touch — is yours.

Getting carts onto the device#

SPEC.md says nothing about how a cart travels, and that stays true. What exists is a ladder of conventions in proposals/sideload.md, with the floor at zero code:

  • Tier 0 — file drop. Carts are folders in a directory on storage a user can reach, and the root of that storage carries a moy-console.json marker naming where they live and when you rescan. You implement this by shipping a text file. That is the whole tier.
  • Tier 1 — serial. A line-oriented protocol over the USB/UART console you already have, around a hundred lines, no network stack. Replies are prefixed so they interleave safely with your logging.
  • Tier 2 — network. mDNS plus a few HTTP endpoints, for consoles that already carry a stack. Nobody is asking you to add WiFi for this.

moy push probes all three in order and carries no device database — a console that answers is supported, including one from a vendor this repository has never heard of. The reference console answers tier 1 over the serial console it already drives its tests with; the table at the end of proposals/sideload.md says who answers what.

Showing carts in a launcher#

If you draw a shelf, "icon" names a small block of tiles from the cart's own sheet (§3.4). It is a pointer, not an image: no new file, no new codec, no reserved tiles, and bounded so that a grid of thirty carts cannot cost you megabytes you never budgeted for. Honour the aspect ratio, scale by integer factors, and fall back to your own choice when it is absent or out of range.

A cart may also carry cover.png (§3.6), its author's picture of it: a PNG, 8-bit, non-interlaced, indexed or RGB, opaque, exactly 128 × 128 and at most 65,536 bytes. A reader is an inflater, the five scanline filters and a palette lookup, and it decodes into a fixed 48 KB, so a screenful of covers is a budget you can write down. Draw it uncropped, at a scale §3.6 allows (a whole-number factor, or a half to shrink it), and show it where the icon is absent too: a compiled cart has no sheet, so the cover is the only art it has. Anything outside the profile, or anything that does not decode, you ignore: the cart shows as if it had no cover and is never refused for it. conformance/covers/ is the reader's suite — PNG files and the verdict on each (conformance/README.md, "Covers").

To say what a cart is, the manifest may carry a description (plain text, at most 240 bytes), a genre from §3.7's twelve and up to eight tags (§3.7). Show them as given; a genre you do not find in the list is no genre, a tag outside the rule is dropped and the others kept, and a store filtering ports looks for the reserved tag pico8. conformance/listing.json is the reader's suite — manifest fields and what a host reads from them — and moycore/cart.py reads all three as a host must, in a page of code.

The checklist#

Before you claim conformance:

  • Every counted scene is pixel-identical, from a LUA_32BITS build
  • examples/verbs.moy plays and looks right on the real device
  • A cart reaching for io fails to load
  • Every case in conformance/format.json reaches its verdict: a format level above yours refuses the cart, before a frame
  • An unknown extensions entry refuses the cart by name, before a frame
  • An unknown runtime refuses it too, rather than reaching for the Lua VM
  • A canvas outside the set refuses; an out-of-range icon, an unknown genre and a bad tag are ignored
  • If you show covers: every file in conformance/covers/ gets the verdict its expected.json gives, and an ignored cover never refuses its cart
  • If you show a description, genre or tags: every case in conformance/listing.json reads as it says
  • Unknown manifest fields are ignored, not fatal
  • A cart listing several sources runs them in order, and a local in one is invisible in the next
  • 30 Hz holds; a "fps": 60 cart ticks at 60, drawing on a divisor if it must
  • dt is the tick period, and dropped frames drop _draw only
  • The first make_layer succeeds
  • pmem survives a power cycle
  • If you run compiled carts: a write that returned 0 survives a power cycle, and one cut short leaves the last whole copy
  • A Lua error ends the cart and reports its line number
  • The player can always exit, without the cart's help
  • Audio is either implemented or a set of no-ops that never error

If the numbers do not fit your board#

Say so, in an issue, with the board.

Three numbers are the likeliest to be wrong on a board nobody here has held: the memory floor (§1.1), the button set (§7.3) and the raster (§1). All three were picked from a small sample of ESP32-class devices, and a spec that only its author's hardware can implement has failed at the one job it had.

That is the most valuable thing a second implementer can contribute, and it is worth more than a patch.