# CPUs, interrupts and frame timing Galaga runs three Z80s on one shared bus. Every clock below derives from a single 18.432 MHz crystal (`galaga.cpp`, `MASTER_CLOCK`; the crystal also appears in the driver's CPU-board parts sketch). Facts are from MAME's `galaga` machine config (`galaga_state::galaga`) and the handlers it wires up unless another source is named. The ROM hashes of the set checked here match MAME's parent `galaga` set (`ROM_START( galaga )`). ## Clocks | Signal | Derivation | Value | Source | |---|---|---|---| | Master crystal | — | 18,432,000 Hz | `galaga.cpp` `MASTER_CLOCK` | | Main CPU (Z80) | master ÷ 6 | 3,072,000 Hz | `galaga_state::galaga`, `Z80(config, m_maincpu, ...)` | | Sub CPU (Z80) | master ÷ 6 | 3,072,000 Hz | same, `m_subcpu` | | Sub2 / sound CPU (Z80) | master ÷ 6 | 3,072,000 Hz | same, `m_subcpu2` | | Pixel clock | master ÷ 3 | 6,144,000 Hz | same, `m_screen->set_raw(...)` first argument | | 06XX custom (NMI clock) | master ÷ 6 ÷ 64 | 48,000 Hz | same, `NAMCO_06XX(config, "06xx", ...)` | | 51XX, 54XX MCUs | master ÷ 6 ÷ 2 | 1,536,000 Hz | same, `NAMCO_51XX` / `NAMCO_54XX` | One Z80 T-state is two pixels. All three CPUs run at the same rate, so "T-states per frame" is one number shared by all three. ## Frame timing From `m_screen->set_raw(MASTER_CLOCK/3, 384, 0, 288, 264, 0, 224)` in `galaga_state::galaga`: | Quantity | Value | Derivation | |---|---|---| | Pixels per line (total / visible) | 384 / 288 | raw params: htotal 384, hblank ends at 0, starts at 288 | | Lines per frame (total / visible) | 264 / 224 | raw params: vtotal 264, vblank ends at 0, starts at 224 | | Line period | 62.5 µs = **192 T-states** | 384 ÷ 6,144,000 | | Frame period | 16.5 ms = **50,688 T-states per CPU** | 384 × 264 ÷ 6,144,000; × 3,072,000 | | Frame rate | **60.6060… Hz** (= 2000/33) | 6,144,000 ÷ (384 × 264) | | Vblank start | line 224 = **T-state 43,008** into the frame | 224 × 192 | | Vblank length | lines 224–263, 40 lines = 7,680 T-states | ends at line 0 of the next frame | These are the same raster numbers as Galaxian (galemu `docs/video-hardware.md`), so the same per-frame constants apply: 50,688 T-states per frame and a vblank edge 43,008 T-states after the top of the visible area. **Where MAME's frame starts (measured 2026-09-11, and what the oracle records).** MAME's screen timeline begins at the *start of vblank*, not at the top of the visible area: its first frame notifier fires one frame period after power-on, its vblank IRQs are taken about 400 T-states into each frame as it labels them, and the sub2 NMI handlers run at about T = 20,040 and 44,620. So in the oracle's records and in the browser board, T = 0 of a frame is hardware line 224: vblank occupies T = 0 to 7,680, the visible area begins at 7,680, the sub2 NMIs (hardware lines 64 and 192) fall at 19,968 and 44,544, and a record for frame N is taken at the end of that frame, the next vblank start. The hardware facts above are unchanged; only the origin moves. ## Interrupt sources | Source | Target | Z80 line | When | Enable | Acknowledge / clear | Source | |---|---|---|---|---|---|---| | Vblank | Main | IRQ (maskable), level | rising edge of vblank, line 224, T-state 43,008 | latch bit 0 (`0x6820`) = 1 | program writes 0 to `0x6820`; the line stays asserted until then | `galaga_state::vblank_irq`, `irq1_clear_w` | | Vblank | Sub | IRQ (maskable), level | same instant | latch bit 1 (`0x6821`) = 1 | program writes 0 to `0x6821` | `vblank_irq`, `irq2_clear_w` | | Scanline timer | Sub2 | NMI, zero-width pulse (edge) | start of line 64 (T 12,288) and line 192 (T 36,864): two per frame, 128 lines apart then 136 lines apart | latch bit 2 (`0x6822`) = **0** (see below) | none needed: edge-latched | `cpu3_interrupt_callback`, `nmion_w`, `machine_reset` | | 06XX transfer clock | Main | NMI, held for half a period then released | every 64 × 2ⁿ T-states while a transfer is active, n = top three bits of the 06XX control byte | 06XX control divider ≠ 0 | none; the divider = 0 write stops the timer and drops the line | `namco06.cpp` `ctrl_w_sync`, `nmi_generate` | Notes on each row: - **Vblank IRQs are level-triggered in MAME.** `vblank_irq` asserts the line when vblank begins and the enable is set; nothing deasserts it except a write of 0 to the enable bit (`irq1_clear_w` / `irq2_clear_w` clear the line when their bit goes to 0). The same bit is therefore both enable and acknowledge, as the driver's address-map table names it ("irq enable/acknowledge"). If the bit is 0 when vblank arrives, the IRQ is simply missed; it is not latched for later. - **Sub2 NMI polarity.** The driver's map table calls bit 2 "NMION", but `nmion_w` stores the *inverse* of the bit and `cpu3_interrupt_callback` pulses NMI only while that inverse is set: NMIs are delivered while the bit is **0** and blocked while it is 1. This document follows the code. Inspection of the sound program in the parent set is consistent with it (the program writes 0 to the bit to start receiving NMIs and 1 around sections it protects). The scanline sequence comes from `machine_reset`, which arms the timer for line 64, and the callback, which adds 128 lines and wraps back to 64 once the value reaches 272 — so the NMIs land on lines 64 and 192 only. The driver notes the sync chain is clocked by H256, so the exact horizontal position inside those lines is not modelled [unverified]. - **06XX NMI to the main CPU.** The 06XX runs from a 48 kHz clock (64 main-CPU T-states per tick). `ctrl_w_sync` derives the divider as 2ⁿ from the control byte's top three bits; the timer ticks at twice the divided rate and asserts NMI on alternate ticks, so one NMI arrives every 64 × 2ⁿ T-states, asserted for half of that. The first tick is aligned to the next 48 kHz edge after the control write. When the control byte is written in read mode the first NMI pulse is suppressed. Galaga's program uses control values with n = 3 (period 512 T-states, 6 kHz) for reads and n = 5 (2,048 T-states, 1.5 kHz) for writes, per the per-game table at the top of `namco06.cpp`; a control byte of `0x10` (divider 0) stops the clock. The 06XX protocol itself is documented elsewhere; only its timing is stated here. Only the main CPU is wired to the 06XX NMI output (`n06xx.set_maincpu(m_maincpu)`). - **Interrupt mode and vectors.** Both program CPUs execute an `IM 1` instruction and no `IM 0`/`IM 2` (byte-scan of the parent set's main and sub ROMs); the sound CPU sets no mode and takes only NMI. Maskable interrupts therefore enter at `0x0038` and NMIs at `0x0066`. No custom vector callback is configured; MAME's Z80 default vector is `0xFF` (`z80.h`, `execute_default_irq_vector`), which gives `RST 38h` even in IM 0. The 51XX also receives the vblank edge (`screen_vblank().append("51xx", ...)`); that concerns the input MCU, not the Z80s. ## Shared bus All three CPUs use the same address map, `galaga_state::galaga_map`. The map's own note says the ROM window `0x0000`–`0x3fff` is the only region that differs per CPU; everything else — video RAM `0x8000`–`0x87ff`, the three work RAMs `0x8800`–`0x8bff`, `0x9000`–`0x93ff`, `0x9800`–`0x9bff`, the sound registers `0x6800`–`0x681f`, the dip-switch reads `0x6800`–`0x6807`, the misc latch `0x6820`–`0x6827`, the watchdog `0x6830`, the 06XX at `0x7000`–`0x70ff` / `0x7100`, and the video latch `0xa000`–`0xa007` — is one set of devices seen identically by all three. MAME models no bus contention or wait states for this board: each CPU runs at its full 3.072 MHz and accesses resolve instantly. How the real board arbitrates the shared RAM (phase-interleaved cycles or wait states) is not stated in the driver [unverified]. MAME interleaves the CPUs with `config.set_maximum_quantum(attotime::from_hz(6000))`: a maximum timeslice of 1/6000 s = 166.7 µs = **512 T-states**, i.e. 99 slices per frame. Any two CPUs may therefore be up to 512 T-states apart in MAME's simulation, and the game is known to run correctly at that granularity. **How MAME's slices actually fall (2026-10-02), and what the board does.** The CPUs race on shared RAM (handshake flags such as `0x92d6`/`0x92d7`, variables one CPU steps while another reads them), so which slice a read and a write land in decides what the game sees, and a different slicing makes the game play out differently within seconds. MAME's slices are not a plain 512 grid: the 51xx and 54xx are MB88 CPUs (1.536 MHz / 6, one cycle per 12 Z80 cycles) in its execution list after the three Z80s; each runs only whole cycles of its own, and a device ending short of the slice target pulls the target back, so slices fall about 504 T-states apart on the MCUs' cycle grid. The 51xx's program writes its O port twice a frame and each write synchronizes, re-anchoring that grid; the 06xx clock, vblank start and end, scanline 0, the sub2 NMI lines and the sound system's 50 Hz update cut slices too, and so do the writes MAME synchronizes on (06xx data and control, IRQ and reset line changes). `src/galaga/sched.rs` reproduces all of it in MAME's attosecond arithmetic, with the 51xx's O writes at their measured times; what it cannot know without running the MCUs' programs is where an MCU's two-cycle instruction overshoots a slice end, so long busy play can still part from MAME (docs/mame-oracle.md, "Movies on MAME"). ## Halt/reset latch and boot order The misc latch is an LS259 at CPU-board location 3C, written through `0x6820`–`0x6827` with data bit 0 and address bits A0–A2 selecting the output (`galaga_map`; the driver's map table gives the decode). Its bits, from `galaga_state::galaga`: | Address | Bit | Function | |---|---|---| | `0x6820` | 0 | Main CPU vblank IRQ enable / acknowledge | | `0x6821` | 1 | Sub CPU vblank IRQ enable / acknowledge | | `0x6822` | 2 | Sub2 NMI gate: 0 = NMIs delivered, 1 = blocked (see above) | | `0x6823` | 3 | Sub and Sub2 reset, inverted: 0 = both held in reset, 1 = running; also resets the 51XX and 54XX | | `0x6824`–`0x6827` | 4–7 | Not connected on Galaga (the MOD lines exist only for Dig Dug's 53XX) | MAME's LS259 clears all eight outputs on machine reset (`74259.cpp`, `addressable_latch_device::device_reset`). So at power-on and after every reset: both vblank IRQs are disabled, the sub2 NMI gate reads as *enabled* (bit 2 = 0), and bit 3 = 0 holds the sub and sub2 CPUs in reset. A CPU whose reset line is asserted is suspended and executes nothing; when the line is released MAME resets the CPU (PC = 0, IFF1/2 = 0, IM 0, HALT cleared — `z80.cpp` `device_reset`) and lets it run (`diexec.cpp`, reset-line handling). Boot order is therefore: the main CPU starts alone from `0x0000`, initialises shared RAM and the latch, then writes 1 to `0x6823`; the sub and sub2 CPUs then start from their own `0x0000`. The main CPU also writes 0 to `0x6823` first (a deliberate reset pulse) and 1 to `0x6822` during its own initialisation, per inspection of the parent set. The driver notes a race in the sound program: it enables NMI before it has finished clearing RAM with a block move, and its NMI handler saves no registers, so an NMI landing inside that move crashes the sound CPU. The first sub2 NMI after release from reset must therefore not come earlier than it would on hardware. `machine_start` allocates the sub2 scanline timer and registers the three mask bits for save states; `machine_reset` arms that timer for line 64. The driver does nothing else at reset; the LS259 and CPU devices reset themselves. ## Watchdog A write to `0x6830` (`galaga_map`, `watchdog_timer_device::reset_w`; the map table names it WDDIS and decodes A5–A4 = 11, so it likely mirrors across `0x6830`–`0x683f` [unverified]) reloads a counter to 8 (`WATCHDOG_TIMER(config, "watchdog").set_vblank_count(m_screen, 8)`). The counter is decremented at every vblank start (`watchdog.cpp`, `watchdog_vblank`), and when it reaches zero the whole machine is soft-reset (`watchdog_fired`). The program must therefore kick the watchdog at least once every 8 frames (132 ms); the main program kicks it from its main loop and start-up code. ## Scheduling recipe (the first plan, superseded) The board followed this recipe until 2026-10-02; it now reproduces MAME's scheduler (above, and `src/galaga/sched.rs`). The Galaxian board in `z80-wasm/src/galaxian/board.rs` keeps one T-state counter, compares it against the next vblank T-state after every instruction, and requests NMI when it passes. The same shape extends to three CPUs: 1. Keep one global frame timeline in T-states (0 to 50,687) and a per-CPU T-state counter. Run the CPUs round-robin in fixed slices: step CPU until its counter reaches the slice end, then the next CPU, then the third. A slice of **192 T-states (one scanline)** puts every scanline-based event — vblank at 43,008, sub2 NMIs at 12,288 and 36,864 — exactly on a slice boundary; MAME's 512 is the proven upper bound. 2. At each slice boundary, raise the events whose T-state has been reached: at 43,008 assert IRQ on main and sub if their latch bits are set, decrement the watchdog; at 12,288 and 36,864 request NMI on sub2 if latch bit 2 is 0. The 06XX NMI is main-only and its period (512 or 2,048 T-states) is not a multiple of 192, so run it off the main CPU's own counter and request it at the instruction boundary where the counter crosses the next due time. 3. Treat the vblank IRQ as a level: keep the line asserted until the program writes 0 to the latch bit, so a CPU that has interrupts disabled at vblank still takes it on `EI`. z80-rust-fast accepts asserted lines at instruction boundaries and wakes from HALT, which is what the programs rely on. A halted CPU must still advance its counter (4 T-states per halted cycle) so the slice ends. 4. Sub and sub2 execute nothing while latch bit 3 is 0; on a 0→1 write, reset the core and let it run from its next slice. A 1→0 write suspends it immediately. 5. Precision needed, as far as MAME demonstrates: 512-T-state skew between CPUs is sufficient. The vblank IRQ phase only needs to be once per frame at a stable point; the sub2 NMIs must arrive exactly twice per frame (they set the sound tempo) but their phase is tolerant; the 06XX NMI period must be right to within a few instructions of the main CPU because each NMI moves one byte of the 51XX/54XX handshake and the program checks transfer state in between (the driver's Bosconian note shows what goes wrong when NMIs come too slowly). Shared-RAM races between CPUs at slice granularity are coarser than hardware but MAME shows the game tolerates them at 512 T-states.