# The tool contract (version 1) Every family tool, in either repository, follows this contract. It is written here, under MIT, without any GPL code, so that an MIT emulator repository can consume any tool's output knowing only this document. Changes to the formats bump the version and are listed at the end. ## 1. The process boundary - An emulator repository runs a tool as a **separate program** (command line, files, pipes, a socket). It never imports, links, includes or `dlopen`s a tool, and never copies a tool's code, patches or comments. - A tool reads its inputs from its command line and environment and writes its outputs to an output directory. Nothing else is shared. - Exit status: `0` success; `1` the run completed but a check failed (stated in the manifest); `2` usage error; `3` a missing input (ROM, BIOS, build); anything else is a crash. ## 2. Command line conventions | Option | Meaning | | --- | --- | | `--out DIR` | output directory (created; the manifest is `DIR/manifest.json`) | | `--rom PATH` / `--disc PATH` | the program to run (a path the player supplies) | | `--machine NAME` | which variant or machine the tool models (tool-specific names, listed in its README) | | `--frames N`, `--cycles N`, `--until EVENT` | how long to run | | `--trace KIND[,KIND]` | which traces to write (`bus`, `insn`, `pins`, `events`, tool-specific kinds listed in its README) | | `--trace-window START:END` | limit traces to a window (frames or cycles, as the tool's README says) | | `--frames-out`, `--audio-out`, `--state-out` | write pictures, sound, state snapshots | | `--input FILE` | a controller input script (format below) | | `--seed N` | any randomness (power-on contents, reset phase) is seeded and recorded | | `--version` | prints ` ` and the pinned upstream commit | ### Ending a run early: `--until` and a script's stop (version 1) `--frames N` (or `--cycles N`) is how long a run lasts; with `--until EVENT`, or when a script the tool runs asks to stop, it is the **most** the run lasts. The run then ends at the end of the frame (or the tool's documented unit) in which the event came, and every output (pictures, traces, script logs, sound) ends there, as a run of that many frames would. | `--until` form | Ends the run when | | --- | --- | | `log:TEXT` | a script given to the tool (`--lua`, or the tool's documented script option) logs a line starting with TEXT; the line itself is kept in the script log. TEXT may not start with the tool's own log tag | Other forms (`frame:N`, a memory condition) are added here, by name, before a tool uses them. A tool that offers a script stop (Mesen2's Lua `emu.stop(code)`, for instance) ends the run the same way, at the end of that frame, and records the script's code. Neither changes the exit status: a run that ended as asked exits `0` with `status` `ok`, and an `--until` never reached is still a complete run of `--frames` (exit `0`). What a stop code means is the consumer's to decide (blargg's result: 0 passed); it reads the code from the manifest's `end` (section 3), never from the exit status, whose `1` stays the tool's own failed check (section 1). Pictures asked for at frames after the end are not written and are listed in `end`. ROMs, BIOS and firmware are found only through the player's paths or environment variables (`NO_INTRO_DIR`, `MAME_ROMPATH`, and per-console names a tool's README lists). No tool commits, embeds or downloads console makers' firmware. ## 3. The run manifest (`manifest.json`) ```json { "contract": 0, "tool": "mesen2-runner", "tool_version": "0.1.0", "tool_commit": "", "tool_licence": "GPL-3.0-or-later", "upstream": {"name": "Mesen2", "commit": "", "licence": "GPL-3.0-or-later", "patches": ["trace-hooks.patch"]}, "machine": "snes-ntsc-cpu2-ppu1-3", "inputs": [{"role": "rom", "sha1": "", "size": 1048576}], "args": ["--frames", "600", "--trace", "bus"], "seed": 0, "host": {"os": "linux", "arch": "x86_64"}, "started": "2026-11-19T12:00:00Z", "outputs": [{"path": "bus.jsonl.gz", "format": "trace/bus", "records": 123456}], "status": "ok", "evidence": "OBSERVED" } ``` **`end`** (version 1): how the run ended, present only when it ended before `--frames` or `--until` was given (without it, the run ran its full length). Fields: | Field | Meaning | | --- | --- | | `by` | `script-stop` (a script asked to stop), `until` (the `--until` event came), or `frames` (`--until` was given and never came: the run ran its full length) | | `frame` | the last frame run (the frame the event came in, or `--frames`) | | `stop_code` | with `script-stop`: the code the script gave (the first non-zero one if it asked more than once) | | `until` | with `--until`: the form as given, e.g. `"log:DONE"` | | `reached` | with `--until`: `true` if the event came, else `false` | | `frames_not_reached` | frames asked for (`--frames-at`) after `frame`, which were not written | For example `{"by": "script-stop", "frame": 37, "stop_code": 0}` or `{"by": "until", "frame": 1200, "until": "log:DONE", "reached": true}`. A tool counting another unit (`--cycles`) names it in the field (`cycle`) and says so in its README. `evidence` is `OBSERVED` for every emulator or model run, and `MEASURED` only for tools that read real hardware (probe-cart results, captures). A consumer quotes the manifest when it tags a value: `OBSERVED (Mesen2 @ , via mesen2-runner 0.1.0)`. ## 4. Output formats All text formats are UTF-8; large files may be gzipped (`.gz`). Times are in the clock the record names (`mclk`, `cpu`, `dot`, `sample`), always integers counted from power-on or reset as the manifest's `time_origin` says. - **`trace/insn`** (JSON Lines): one record per instruction: `{"t": 1234, "clock": "cpu", "cpu": "main", "pc": 32768, "op": [169, 0], "regs": {...}}`. Register names are the CPU's documented names (manufacturer's manual), never an emulator's internal names. - **`trace/bus`** (JSON Lines): one record per bus cycle: `{"t": 1234, "clock": "mclk", "bus": "a", "kind": "read", "addr": 8448, "data": 255}`. `kind` is `read`, `write`, `fetch`, `idle`, `dma-read`, `dma-write`, `refresh`, or a documented tool-specific kind. - **`trace/pins`** (JSON Lines): pin-level samples for die-level models: `{"t": 1234, "clock": "mclk", "pins": {"BUSY": 1, "D": 255}}`. Pin names as the chip's data sheet names them. **Only pins**: a tool never exports a model's internal nodes. - **`trace/events`** (JSON Lines): named events (`vblank`, `irq`, `line`, `frame`) with time. - **Frames**: PNG, one per frame, `frame-000123.png`, at the machine's native resolution, with the palette and any conversion stated in the manifest. - **Audio**: WAV (PCM, rate and channels in the header and the manifest). - **State snapshots**: JSON (`state-000123.json`) with documented register and memory names; memories as base64 with their size and address range. - **Input scripts**: JSON Lines `{"frame": 120, "port": 1, "buttons": ["start"]}`. A binary twin of a JSON Lines format may be added for volume; it must be specified here first, field for field. ## 5. Finding a tool One environment variable per tool repository (Aubrey, 2026-11-24), each naming a checkout of that repository: | Variable | Repository | Licence | | --- | --- | --- | | `EMU_TOOLS` | `emu-tools` | MIT | | `EMU_TOOLS_GPL` | `emu-tools-gpl` | GPL-3.0-or-later | | `EMU_TOOLS_GPL2` | `emu-tools-gpl2` | GPL-2.0-only | - **Fixed paths.** Every tool sits at a fixed path under its repository's root, listed in that repository's `tools/README.md` (for example `$EMU_TOOLS_GPL/tools/mesen2/run.py`). A consumer joins the variable and the documented path, and nothing else: no search, no guessed sibling folders, no machine paths in a repository. When a tool moves, its README and `tools/README.md` say so and each consumer changes its path once. - **Optional per-tool overrides.** A tool's README may offer an override for one thing the tool uses (Mesen2's library through `MESEN_CORE`, for instance). An override is optional: the tool works without it at its documented path. There are no per-consumer variables; the old `_GPL_TOOLS` names are retired as each consumer moves. - **Unset is a missing input.** A consumer script that needs a tool and finds its repository's variable unset, or the documented file absent under it, stops with exit status `3` (a missing input, section 1) and a message naming the variable and the path it expected. A test suite skips such a test, naming the variable, rather than failing. ## 6. Versions | Contract | Date | Change | | --- | --- | --- | | 0 | 2026-11-19 | first draft (skeleton) | | 0 | 2026-11-24 | section 5, finding a tool (one variable per tool repository); no format change | | 0 | 2026-11-24 | draft, for review: `--until log:TEXT` and a script's stop (section 2) and the manifest's `end` field (section 3), as mesen2-runner 0.3.0 has them; additions only, no existing field changes | | 1 | 2026-11-25 | accepted by Aubrey (decisions page T1, emulator-lineage docs/proposals/2026-11-decisions.md): `--until`, a script's stop and the manifest's `end` field as drafted on 2026-11-24; additions only |