# Build Approach — Read This First ## Decision (confirmed) **Option A — true emulator.** A Z80 CPU interpreter executes the actual ROM bytes in `rom/galaxian/`, driven through the exact memory map in [memory-map.md](memory-map.md). Video RAM, sprite RAM, and I/O ports are read/written by that real code, the same way MAME does it. Not cycle-accurate, not a goal to reproduce exact bus timing — the bar is a correct, playable game loop. Two specific areas are explicitly approved for pragmatic simulation rather than faithful circuit-level emulation, per direct instruction: - **Starfield** — the LFSR table-and-scroll-offset approach in [video-hardware.md](video-hardware.md) already *is* a simulation, not a scanline- exact replica of the hardware's twinkle-gate timing, and that's fine as-is. - **Sound** — the discrete 555-timer circuit is approximated with synthesized placeholder `.wav` triggers (see [sound-hardware.md](sound-hardware.md)), not simulated at the RC-circuit level. Everything else the CPU actually touches (memory map, video RAM/sprite RAM layout, palette, input ports) should be correct, since the whole point of running the real ROM is that the game logic comes along for free *only if* the plumbing around the CPU is right. ## CPU core: released dependency The verified instruction core has now been extracted and publicly released as the MIT-licensed [`z80-python`](https://pypi.org/project/z80-python/) package. Galemu pins `z80-python==0.3.0` and subclasses its public `z80_python.Z80CPU` host boundary. The board owns memory, devices, I/O, input state, timing, and reset policy; `step()` executes exactly one instruction and returns its documented T-state count. The package was validated independently against 1,604,000 SingleStepTests state transitions and ZEXDOC/ZEXALL under CPython and PyPy. Its validation evidence and scope are published with the package. Galemu deliberately does not retain a fork, vector corpus, or duplicate CPU test suite after consumer cutover. The boundary matters: version 0.3.0 remains an instruction core, not a complete Z80 machine. It provides public host-owned maskable and non-maskable interrupt lifecycle APIs, including NMI acceptance and HALT wakeup, plus processor inspection tooling; Galemu uses the lifecycle API for its MAME-verified vblank NMI. The board still owns device scheduling and must never fake an interrupt by directly altering CPU registers. See [machine-layer-plan.md](machine-layer-plan.md) for the observable, deterministic consumer milestones and the division of interactive and autonomous work. ## Staged plan 0. **Consumer boundary.** Install the pinned released `z80-python` package and keep Galemu's board logic separate from CPU instruction semantics. Verify that a fresh environment resolves the installed package rather than local historical sources. 1. **Tracer bullet — CPU + real memory map, no video, no sound.** Load the actual ROM from `rom/galaxian/`, execute it against the memory map in [memory-map.md](memory-map.md), and confirm it behaves sanely for a few thousand instructions (doesn't crash, doesn't jump into unmapped program space, IRQ enable gets written, video/sprite RAM gets written to with plausible-looking values). Entirely headless — no pygame window yet. 2. **Video: tiles + palette only.** Decode video RAM + the column-attribute scheme into a rendered frame using the palette formula in [video-hardware.md](video-hardware.md). At this point the attract-mode title screen and scrolling text should be visible and recognizable, even with no sprites, no stars, and no input handling yet. 3. **Sprites, bullets, starfield.** Layer these on top of stage 2's tilemap renderer. 4. **Input wiring.** Map real keyboard events onto the IN0/IN1/IN2 bits from [memory-map.md](memory-map.md) so coin-insert, start, joystick, and fire actually reach the running program. 5. **Sound synthesis.** Watch writes to the discrete-sound registers (`0x6004`-`0x6807`, `0x7800`) and map them to the placeholder `.wav` events described in [sound-hardware.md](sound-hardware.md). 6. **Polish (optional, low priority).** DIP switch configuration, cocktail/flip support, exact watchdog behavior (skippable — see memory-map.md). Each stage should have its own automated tests where that's meaningful: address-decode, ROM-loader, and structured-trace tests for stage 1; pure palette-decode and tilemap-layout unit tests for stage 2. Pygame's actual render loop itself is not the target of automated tests, but the pure logic feeding it is.