# Handoff: start `z80-wasm`, the z80-rust-fast core in the browser, certified there before it plays Galaxian ## Context you are inheriting Four repositories exist and are done, in the sense that every claim in them is reproduced and recorded: - `z80-python` (github alewman/z80-python, local clone `/data/emu/z80-python`, `main` at `d1ec9c7`, instruction core byte-identical to `cab1598`): the reference core, "the code is the spec". It ships the conformance kit: manifests, one shared host (`cpm-minimal` and `bare`), committed reference traces (`docs/trace-schema.md`, version 1), and a lockstep `diff`. - `z80-rust` (github alewman/z80-rust, `/data/emu/z80-rust`): the readable transcription, six rungs passing at `cab1598`. - `z80-rust-fast` (github alewman/z80-rust-fast, `/data/emu/z80-rust-fast`, `main` at `a2ae176`, tag `v0.1.0` at `85fb856`): the optimized fork. Same six rungs pass at `cab1598`; ZEXALL's 5,764,169,474 instructions in 16.8 s on the i9-13900K, 15.7 s on a 9800X3D. The crate is still named `z80_rust`, so `use z80_rust::{Bus, Z80}` compiles against it unchanged. Without the default `std` feature it is `no_std` + `alloc` and contains the instruction core alone; that split was made for this project. Read its `README.md`, `docs/handoff-brief.md`, and `docs/build-record.md` first. - `galemu` (github alewman/galemu, `/data/emu/galemu`, `main` at `fa5f935`): a readable, deterministic Python emulator of the 1979 Namco Galaxian board, running the real game through z80-python. Its `docs/` already write down everything the board needs: `memory-map.md`, `rom-manifest.md` (every ROM's size, CRC32, SHA-1, and load address), `video-hardware.md`, `gfx-bitplane-spec.md`, `sound-hardware.md`, `gameplay-reference.md`. `src/galemu/board.py`, `devices.py`, `video.py`, `render.py`, `roms.py` are the bus, the devices, and the video pipeline you will transcribe. It loads the standard MAME `galaxian.zip` in memory, verifies every member against the manifest, and never extracts or commits ROMs. Why the name is `z80-wasm` and not `z80-rust-fast-wasm`: the reference/fast pair ends with z80-rust-fast. There is no reason for a slow reference wasm build, so this is the one wasm build of the certified core, and its name says only that. What is already in place for you, so you do not rebuild it: - **Rung 1 without Python exists as code.** `tests/rung1.rs` in z80-rust-fast runs the three example manifests through `z80_rust::conformance::{load_manifest, trace_manifest}` and compares every record to the committed `tests/data/*.jsonl`, ignoring only the `mnemonic` and `operands` text an external producer omits. That test is the model for the browser check: same host, same comparison, same tolerance, nothing else. - **The reference traces for rung 5 are committed in z80-python** at `examples/conformance/interrupts/*.jsonl` (ten manifests, 2 to 4 records each). z80-rust-fast carries the ten manifests under `conformance/interrupts/` but not the traces; copy the traces from z80-python at `cab1598` and say so in a README like `tests/data/README.md`. - **Manifests carry memory inline.** Every rung 1 and rung 5 manifest uses `data` (hex), not `file`, so `manifest_from_json` works from a string with no file system. The `file` branch calls `std::fs::read`, which on `wasm32-unknown-unknown` fails cleanly rather than failing to compile. - **The conformance host and trace producer need `std` but not an OS.** `src/conformance.rs` and `src/trace.rs` use `std::io`, `std::path`, `std::fmt`; all of that exists on `wasm32-unknown-unknown`. The `std` feature builds there. `no_std` is what a size-minimal core would use; it is not needed to get the ladder into the browser. - **The board's interrupt path is already certified.** Galaxian drives the CPU with a vblank NMI (galemu: "MAME-verified vblank NMI" through the lifecycle API). Rung 5's `nmi-accept-while-running` and `nmi-accept-from-halt` manifests are exactly that path. ## Your task Create `alewman/z80-wasm` (private): a WebAssembly build of z80-rust-fast and a static web page, to be served at **https://galaxian.supertux.com**, that in the browser **runs the rung 1 and rung 5 manifests against the committed reference traces and shows the result before it runs anything else**, then runs ZEXALL with a measured speed, then plays Galaxian on a bus transcribed from galemu, with gamepad and keyboard controls and on-page instructions. This is the first live demo of the whole pipeline: reference core, proven transcription, measured optimization, certified in the visitor's own browser, then a real machine. A page that plays the game but cannot show its rungs green is not a result. z80-wasm is a *host* of z80-rust-fast in the sense the previous brief defined: it depends on the crate and compiles against it unchanged. That is the API-stability claim being cashed. If you need something from the crate that it does not export, that is a change to z80-rust-fast (additions are permitted there; renames and removals are not), made as its own commit in that repository, ladder-gated as that repository's README requires, and the dependency here re-pinned to it. ### What must not change - z80-rust-fast's source, from this repository. The dependency is a git dependency pinned to a tag or commit (`v0.1.0` = `85fb856` to start), never a path dependency in anything committed. The page states which commit of the core it runs and which commit of z80-python that core is certified against. - The manifests and reference traces. They are copied byte for byte from their sources at the stated commits and the copy's README says where from. - The comparison. It is the one in `tests/rung1.rs`: every record, every field, in order, with only `instruction.mnemonic` and `instruction.operands` ignored. Do not loosen it to make a demo look better. If the browser build disagrees with the reference, stop, show the manifest and the diverging record, and ask; the trace is the authority. - galemu's ROM rules. No ROM, graphics, PROM, or gameplay recording is ever committed or served. The visitor supplies `galaxian.zip`; the page reads it in memory, verifies every member against `docs/rom-manifest.md`'s sizes, CRC32s, and SHA-1s, and refuses anything else, as `roms.py` does. ### What may change Everything about the wasm and web side: the binding layer (wasm-bindgen is the recommendation, with JSON strings or serde-wasm-bindgen across the boundary; a hand-rolled export surface is acceptable if you can justify it), the JS (plain ES modules, no bundler, no framework unless you show why), the build (`wasm-pack` or `cargo build --target wasm32-unknown-unknown` + `wasm-bindgen-cli`; pin whichever you pick), and the board implementation, which is a transcription of galemu the way z80-rust was a transcription of z80-python: same module boundaries where they make sense, with the Python beside it as the spec. ### Milestones, each with its acceptance test 1. **The core builds for wasm and the ladder's browser rungs pass headless.** New repository beside the others; `Cargo.toml` depends on z80-rust-fast by git tag; a `wasm32-unknown-unknown` build with `wasm-bindgen`; an export that takes a manifest (JSON string) and its reference trace (JSONL string) and returns the records produced, the stop reason, and the first differing record index if any. Copy the 3 + 10 manifests and traces. Acceptance: a Node script (Node 22 is on the machine) loads the `.wasm`, runs all thirteen, and prints thirteen `identical` lines; `cargo test` on the native target runs the same thirteen through the same export so the check does not depend on a browser. Record the `.wasm` size (`wasm-opt -Oz` and without) in the README. 2. **The same thirteen in the browser, on the page, before anything else loads.** A static page whose first act is to run rung 1 and rung 5 and render a table: manifest, records produced, records expected, result. Nothing below that table is enabled until every row reads identical. The build produces one self-contained `dist/` directory of static files. Acceptance: the page served locally (`python3 -m http.server dist` is enough) shows thirteen green rows; CI builds `dist/` and runs the milestone 1 Node script on every push. 3. **Speed in the browser, measured the same way.** A "run ZEXALL" control using the `cpm-minimal` host that streams CP/M output to the page and reports instructions, T-states, wall time, instructions/s, and equivalent MHz, the same five numbers `z80-bench` prints. `zexall.com` is fetched by z80-rust-fast's `conformance/zex/` scripts, not committed; treat it the same way here. Run it in a Web Worker so the page stays responsive; that is also where the game will run. Acceptance: ZEXALL completes in the browser with all tests reporting OK and the exact totals 5,764,169,474 instructions and 46,734,975,782 T-states (a consistency check, not a rung); the README records the time in Chrome and Firefox on the build machine beside the native 16.8 s. 4. **Galaxian runs.** Transcribe galemu's board: memory map, ROM loading and verification from a visitor-supplied `galaxian.zip` (a zip reader in Rust or JS; verify every member before any byte reaches the bus), the input ports, the vblank NMI at galemu's documented rate ($2000/33$ Hz frame timing), and the video pipeline (tiles, sprites, bullets, starfield, palette PROMs) to a canvas. Sound is milestone 6, not here. **galemu is the oracle for the board, as z80-python is for the CPU.** It runs headless and deterministic (`python -m galemu`, `src/galemu/trace.py`) and serializes a run as canonical JSON: the typed board events (device writes, input reads, NMI boundaries) and a full board snapshot. Add a `board-trace` export here that produces the same JSON from the Rust board for the same ROM set, the same instruction count, and the same scripted inputs, and a script that diffs it against galemu's, byte for byte. Commit galemu's reference runs for a few fixed lengths of attract mode plus one scripted game with inputs at stated frames; they contain no ROM bytes, so they can be committed. Acceptance: every committed reference run is identical from the Rust board, natively and from the wasm build under Node; the attract mode runs and a game can be played to game over with the keyboard; the page reports the frame rate and whether it is keeping up; the README names the rung 5 manifests that certify the NMI path the board uses. If the Rust board and galemu disagree, stop and show the first differing event; if galemu is the one that is wrong, the fix goes to galemu under its own tests first and the pin here moves. 5. **A demo people can use.** Gamepad API support with a visible mapping (any standard pad works without configuration), keyboard controls, an instructions panel (how to get the ROM set, which file, what the controls are, what the rung table above the game means and why it is there), pause, and a clean layout at phone and desktop widths. The page tells the pipeline's story in a few lines with links to the four repositories. Acceptance: someone who has not seen the project can, from the page alone, supply the zip and play with a pad. 6. **Sound.** Transcribe galemu's discrete sound approximation (`synth.py`, `docs/sound-hardware.md`) to the Web Audio API, or to an AudioWorklet fed from the worker. Acceptance: the events at the sound ports galemu documents produce the sounds galemu produces; a short recording of each from galemu is the comparison. 7. **Done.** README with: the core commit and tag, the z80-python commit its certification rests on, the trace schema version, galemu's commit, which rungs run in the browser (1 and 5) and which do not and why (2 is 1.6 M vectors of data; 3 is nine hours against PyPy; 4 and 6 need their suites fetched), the `.wasm` size, the browser speed table, the URL, and the deploy procedure. A `docs/build-record.md` like the previous projects'. Rung 1 before rung 5, rung 5 before ZEXALL, ZEXALL before the game, the board trace identical before anyone judges the game by eye, the game before its polish. Do not reorder; a divergence is cheapest to diagnose at the lowest rung that shows it. ## Hosting and deploy `galaxian.supertux.com` is a CNAME to `supertux.com`, the user's own server, which is **this machine**: the docker stack (github alewman/docker-stack, live checkout `/docker`, compose files under `/docker/compose/{category}/{service}.yml`, Traefik on the external `t2_proxy` network with Let's Encrypt via Cloudflare DNS) runs here, and the user is in the `docker` group. Other demos are already served exactly the way this one will be. The template is `/docker/compose/apps/galaga.yml`: an `nginx:alpine` container on `t2_proxy`, `cpuset: "16-31"` (E-cores), mounting `$DOCKERDIR/appdata/galaga` read-only at `/usr/share/nginx/html`, with Traefik labels `Host(\`galaga.$DOMAINNAME_CLOUD_SERVER\`)`, entrypoint `https`, `tls=true`, middleware `chain-no-auth@file` (public, no OAuth), and service port 80. `galaga.supertux.com` answers 200 today; `galaxian.supertux.com` answers 404 because no router exists yet. The deploy is therefore: 1. `/docker/compose/apps/galaxian.yml`, a copy of `galaga.yml` with `galaga` replaced by `galaxian` throughout. If the worker needs `SharedArrayBuffer`, add a Traefik headers middleware on the router setting `Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp`; otherwise do not. `nginx:alpine` already serves `.wasm` as `application/wasm`. 2. `scripts/deploy.sh` in this repository: builds `dist/`, `rsync --delete`s it to `/docker/appdata/galaxian/` (create it; owned by the user like `/docker/appdata/galaga`), and if the container is not running, `cd /docker/compose && docker compose --env-file .env -f apps/galaxian.yml up -d`. nginx serves the new files without a restart. The script prints the commit it deployed and the page shows it. 3. Verify with `curl -sI https://galaxian.supertux.com/` (200, and the `.wasm` URL returns `application/wasm`). The `/docker` checkout has local modifications and untracked service files that are not in the GitHub repository; touch only `apps/galaxian.yml` there and leave the rest to the user. Do not run any stack-wide start/stop script. CI in this repository builds `dist/` and runs the milestone 1 check; it does not deploy, because the server is this machine and the repository is private. ## Constraints - No sudo. `rustup target add wasm32-unknown-unknown` and `cargo install wasm-bindgen-cli wasm-pack` (and `wasm-opt` via `cargo install wasm-opt` or a binaryen release under `~/.local`) need none. None of them is installed yet; Node 22.19 is (`~/.nvm`). Pin the wasm-bindgen crate and CLI to the same version or the build fails in a confusing way. - The machine is shared and z80-rust-fast's rung 3 may be running in `/data/emu/z80-rust-fast-wt`; check `uptime` before a long run and do not touch that worktree. - The ROM set for local testing is `galaxian.zip` in a local copy of MAME's non-merged sets (`$MAME_ROMPATH`) (10,327 bytes). Its eight members match galemu's `docs/rom-manifest.md` by name, size, and CRC32 (galmidw.u/v/w/y, 7l, 1h.bin, 1k.bin, 6l.bpr). Read it from there or copy it under the ignored `rom/`; never commit it, never put it under `dist/` or `/docker/appdata/galaxian`. - Start the session in `/data/emu`, not inside an existing clone; the repository already exists there as `/data/emu/z80-wasm` with this brief as `docs/handoff-brief.md` and nothing else. - Commit messages carry the measurement or the check they rest on, as the previous repositories do. ## What done looks like https://galaxian.supertux.com loads, runs thirteen conformance manifests through the z80-rust-fast core compiled to WebAssembly, and shows every one identical to the reference trace committed at a stated z80-python commit; runs ZEXALL to completion with the exact instruction and T-state totals and a measured speed; then plays Galaxian from the visitor's own verified ROM set, with a gamepad, with sound, on a board transcribed from galemu against the unchanged `z80_rust` API. The repository states every pin, CI proves the thirteen on every push, and nothing in z80-rust-fast changed unless it did so in that repository under its own ladder. Before starting, tell the user in a few sentences how you read this brief and what you will do first.