# Build Approach — Read This First ## What this project is `emu-galaga` is the 1981 Namco Galaga arcade board, emulated in the browser at **https://emu.supertux.com**, on the same certified Z80 pipeline the Galaxian demo at https://galaxian.supertux.com runs on. It differs from that demo in two deliberate ways: 1. **The board is much bigger.** Galaga is three Z80s on one shared bus, three Namco custom chips (06xx bus controller, 51xx I/O MCU, 54xx noise MCU), a 05xx starfield generator, and a 3-voice wavetable sound chip. Galaxian was one Z80, TTL, and a discrete sound board. See the hardware documents in this directory; each was written from MAME's source for this exact board. 2. **The page is for players, not for reviewers.** The Galaxian page runs the conformance ladder in the visitor's browser and shows the table before the game. This page does not. The verification still happens, in CI and in the repository, and the page links to it in one line for anyone who cares. See [site-plan.md](site-plan.md). ## Decisions taken **True emulation, no recreation.** The real ROMs run on real CPU cores against the real memory map. Nothing about the game is hand-written. This was galemu's decision too and it is what makes the game logic come for free once the plumbing is right. **The CPU cores are z80-rust-fast, unchanged, by git tag.** Three instances of the same certified core. Nothing in this repository modifies the core; if something is missing from its API, that is a change made in z80-rust-fast under its own ladder, tagged, and re-pinned here. Same rule as z80-wasm. **The board is written in Rust directly, compiled to WebAssembly.** There is no Python reference board first. galemu existed because Galaxian was the first board and the Python was the readable spec; its real oracle for the *facts* was MAME's source. For Galaga the facts are in `docs/` and the oracle for *behaviour* is MAME itself (below), which is a stronger check than galemu ever had. A Python board would cost weeks and add no evidence. **MAME 0.285 is the board oracle**, driven headless by Lua to dump the memory-mapped state at frame boundaries and the writes to every device window, for fixed-length attract-mode runs and scripted games. The browser board must produce the same dumps. How, and what was verified about it, is in [mame-oracle.md](mame-oracle.md). MAME is emulator-derived evidence, not hardware-captured; that tier is stated wherever it is used, per the survey method: a lower-tier oracle is a detector, not a judge. For a board there is no higher tier available short of a real cabinet. **The consumer shell is separate from the board.** The page's ROM intake, storage, controls, audio output, layout, and offline support know nothing about Galaga beyond a small interface (screen size, a frame function, an audio sample function, a control map, a ROM manifest). That layer is what a later generalised `emu.supertux.com` reuses; the board is what stays specific. Do not generalise before a second game exists; keep the seam clean now. **Nothing from z80-wasm is depended on; useful pieces are copied.** z80-wasm is a page, not a library, and it is private. Its build scripts (`scripts/build-wasm.sh`, `build-dist.sh`, `deploy.sh`), its in-browser zip reader, its AudioWorklet feed, its gamepad and touch handling, and its frame-pacing fix (an early animation frame at 120 Hz owes no board frame) are known good and should be copied with a note saying so, then adapted. ## Decisions still open (the user's calls) **51xx and 54xx: HLE, decided 2026-09-11.** Both MCUs are hand-written high-level models, not MB88xx cores running the MCU ROMs. The 51xx follows the protocol in [custom-io.md](custom-io.md) command by command and is checked against MAME's observed responses in the oracle dumps of the 0x7000 window. The 54xx is a noise generator shaped to MAME's recorded output through the filter network in [sound-hardware.md](sound-hardware.md), judged by ear against a MAME `-wavwrite` capture and recorded honestly. If that ear test fails, the fallback is an MB88xx core running `54xx.bin`; it is not built ahead of that failure. The two MCU ROMs are still verified as members of the set, because the set is the set, but no byte of them is executed. **Bit-exact frames, or bit-exact at frame boundaries?** Three CPUs on a shared bus interleave; MAME slices them at a stated quantum. The browser board cannot reproduce MAME's intra-frame interleave exactly without copying its scheduler, and should not try. The acceptance test is therefore video RAM, sprite RAM, and device-write streams identical **at frame boundaries** over the reference runs. If that proves unreachable because the game's CPUs race on shared flags within a frame, the fallback is stated in [cpu-and-timing.md](cpu-and-timing.md)'s scheduling recipe, and the README records which runs match and which do not, never a loosened comparison. **Starfield fidelity.** The 05xx generator in MAME is a reconstruction from hardware observation. The browser board transcribes that algorithm: every MAME file these documents rest on is BSD-3-Clause (see the table in [README.md](README.md)), so transcription with attribution is permitted, and [video-hardware.md](video-hardware.md) gives it in full. Stars are compared against MAME's screenshots; if they differ in the twinkle timing only, that is recorded as a known difference, not chased. ## Staged plan Each stage has an acceptance test; do not reorder. A divergence is cheapest to find at the lowest stage that shows it. 0. **Documents and oracle.** This directory complete; `oracle/mame_dump.lua` proven deterministic (two runs, identical dumps); `oracle/runs.json` defines the reference runs (attract mode at several lengths, one scripted game with coin, start, moves and shots at stated frames); reference dumps committed (they contain RAM contents and device writes, no ROM bytes). 1. **Three CPUs boot.** ROM loader that verifies every member of `galaga.zip` by size, CRC32 and SHA-1 against [rom-manifest.md](rom-manifest.md) before any byte reaches the bus; the memory map with its latches; the three cores scheduled per [cpu-and-timing.md](cpu-and-timing.md); vblank IRQs, the sub2 NMI timer, the 06xx NMI; enough 51xx HLE for the boot self-test to pass. Acceptance: the boot sequence's video RAM matches the oracle at every dumped frame boundary through the end of the self-test and into attract mode, natively (`cargo test`) and under Node through the wasm. Headless, no canvas yet. 2. **Video.** Tiles with the Namco scan layout, palette and lookup PROMs, sprites with flip and double-size, the 05xx starfield, to a 224x288 canvas. Acceptance: rendered frames identical to MAME's screenshots at the dumped frames for tiles and sprites (starfield compared, differences recorded). 3. **Inputs.** The 51xx HLE's switch and credit modes complete; keyboard wired. Acceptance: the scripted game run matches the oracle at frame boundaries, and a person can coin up, start, and play to game over. 4. **Sound.** The 3-voice WSG from the wave PROM at its exact sample rate through an AudioWorklet; the 54xx explosion and the filter network. Acceptance: the WSG register-write stream matches the oracle's; WSG output compared sample for sample against a MAME `-wavwrite` capture resampled to the same rate; the 54xx by ear against the same capture, recorded honestly. 5. **The page.** [site-plan.md](site-plan.md) in full: ROM intake and on-device storage, attract mode on load, sound unlock on first gesture, portrait layout, gamepad, touch, settings, save state, offline. Acceptance: someone who has never seen the project plays it on a phone from the link alone. 6. **Done.** README with every pin (core tag, z80-python commit, MAME version, reference run list, `.wasm` size, frame budget on a phone), the deploy procedure, and `docs/build-record.md` in the shape of the earlier projects. ## Risks, named - **Interleave.** Stage 1's acceptance may need the scheduling quantum tuned; budget for that before touching video. - **The 06xx NMI period** and the sub2 NMI phase within the frame are the two timings most likely to be wrong first. The oracle dumps of the 0x7000 window traffic will show it. - **MCU HLE gaps.** The 51xx protocol has corners (coinage tables, two-coin modes) the game never exercises with default DIPs. Implement what the reference runs exercise, refuse the rest loudly, and say so. - **License hygiene.** The MAME files used are all BSD-3-Clause, so transcribing an algorithm is allowed with attribution; anything transcribed says which file it came from and carries the notice. Nothing is taken from a GPL-licensed file. Every document names its source file.