# Timing: six clocks a cycle, and what that is worth on a Namco board ## The rule **One instruction cycle is six clock periods.** Two independent statements of it: - DS, Table 1, /RESET row: "The /RESET pulse must be low for at least two instruction cycles (12 clock periods: 6 µs at 4 MHz crystal with prescaler)". Two cycles = twelve periods. - DS, Table 7: minimum instruction execution time 3 µs at 2 MHz without prescaler (MB8842/43/44/46/47/48) and 2 µs at 3 MHz (MB8841H/45H). 2 MHz ÷ 6 = 333 kHz = 3 µs; 3 MHz ÷ 6 = 500 kHz = 2 µs. MAME states the same rule twice in `mb88xx.h`: ```text execute_clocks_to_cycles(clocks) = (clocks + 6 - 1) / 6 execute_cycles_to_clocks(cycles) = cycles * 6 ``` ### The prescaler, and why it does not bite here The divide-by-two prescaler is a **mask option** (DS Table 4, "Clock CLK"). With it, the permitted pin frequency doubles: | Series | Without prescaler | With prescaler | | --- | --- | --- | | MB8840 (MB8842/43/44) | 0.5 - 2 MHz | 1 - 4 MHz | | MB8840H (MB8841H/45H) | 0.5 - 3 MHz | 1 - 6 MHz | "Clock period" in the six-clocks rule means the period *after* the prescaler, so a part with the option fitted runs at half the instruction rate its pin frequency suggests. MAME divides the device clock by six flat, with no prescaler option at all, which is the same as asserting that every Namco part was ordered without the prescaler. Namco's 1.536 MHz sits inside the no-prescaler range (0.5 - 2 MHz), so the assertion is at least legal. It is nowhere stated: if a Namco MCU were in fact a prescaler part, every instruction rate below would halve to 128 kHz. **Unverified.** The trace measurements in [mame-oracle.md](mame-oracle.md) confirm MAME's arithmetic, not Fujitsu's mask order. ## Per-opcode cycles There are two values and no third. DS Table 5's cycle column reads "1/1" (bytes/cycles) for every one-byte instruction and "2/2" for the five two-byte ones; MAME charges the same, starting each instruction at `oc = 1` and doing `oc++` in exactly those five cases. | Cycles | Opcodes | Count | | --- | --- | --- | | **2** | `JPA` 3D, `EN` 3E, `DIS` 3F, `CALL` 60-67, `JPL` 68-6F | 19 | | **1** | everything else | 237 | | **+3** | an accepted interrupt, charged at the boundary after the instruction that was running | — | Two consequences worth stating because they catch people: - A **not-taken** `CALL` or `JPL` costs its full two cycles. The operand byte is fetched and skipped regardless of ST. - The 3-cycle interrupt entry is **MAME's guess** (`burn_cycles(3); // ?`). The datasheet is silent. It is also the figure a MAME lockstep will hold a core to, so a core should charge 3 and record that it is unproven. See [validation.md](validation.md). There is no memory-access cost, no page-crossing penalty, no prefix and no variable-length operand. Total cycles for a straight-line run are countable from the instruction bytes alone. ## What the Namco parts run at | Board | Master crystal | Divider to the MCU pin | MCU pin clock | Instruction rate | Cycle | | --- | --- | --- | --- | ---: | ---: | | Galaga, Bosconian, Xevious, Dig Dug (`namco/galaga.cpp`, `MASTER_CLOCK/6/2`) | 18.432 MHz | ÷ 12 | **1.536 MHz** | 256,000 Hz | 3.906 µs | | Pole Position (`namco/polepos.cpp`, `MASTER_CLOCK/8/2`) | 24.576 MHz | ÷ 16 | **1.536 MHz** | 256,000 Hz | 3.906 µs | Every Namco custom in scope — 50xx, 51xx, 52xx, 53xx, 54xx, on either board — runs at the same 1.536 MHz and therefore the same 256 kHz instruction rate. That is the number to hold: **256,000 instruction cycles per second per chip**, about a quarter of a million, against 3.072 MHz for the Z80 beside it. Derived numbers that follow, all exact: | Quantity | Value | Where it comes from | | --- | --- | --- | | MCU cycles per Z80 T-state | 1 / 12 | Z80 at master/6, MCU cycle at master/72 | | **Z80 T-states per MCU instruction cycle** | **exactly 12** | the same, on both boards | | MCU cycles per video frame (Galaga, 60.606061 Hz) | **4,224** | 256,000 / 60.606061 | | MCU cycles per 06xx byte at control divider 2⁵ (1.5 kHz) | 170.67 | 256,000 / 1500 | | MCU cycles per 06xx byte at divider 2³ (6 kHz) | 42.67 | 256,000 / 6000 | The 12-T-state figure is the useful one: it is an integer, it is the same on both boards, and it means a host can schedule the MCU against the Z80 with integer arithmetic and no drift. The 4,224-cycles-per-frame figure is how the trace in [mame-oracle.md](mame-oracle.md) was checked: Galaga holds its MCUs in reset until the main program releases the LS259 latch at frame 699, and a 2,500-frame run produced 7,608,979 MCU cycles — 1,801 frames × 4,224 = 7,607,424, a 0.02 % difference accounted for by where in the frame the latch is written. ## Scheduling an MCU against a Z80 timeline The MB88xx is a slave here. It has no bus, it cannot stall the CPU, and nothing it does is visible except through port callbacks and the /IRQ, /TC and /RESET pins a host drives. So the scheduling problem is small, and three approaches are ordered by cost: **1. Catch-up on demand (cheapest, and enough for Galaga).** Keep the MCU's cycle count as a debt measured on the host's master timeline. Run it forward only when something crosses the boundary: before delivering an /IRQ edge, before a /TC edge, before reading a port the MCU drives, and once at each frame boundary so the debt never grows unbounded. Between those points the MCU's internal state cannot be observed and its execution order does not matter. ```text target_cycles = (master_clocks_elapsed * 1) // 72 # Galaga while mcu.cycles < target_cycles: mcu.step() ``` **2. Fixed quantum.** Run the MCU for a whole quantum (a scanline, 192 T-states, 16 MCU cycles) interleaved with the Z80. Simpler to reason about, slightly wrong at edges. MAME does something close: Galaga's driver sets `config.set_maximum_quantum(attotime::from_hz(6000))`, a 166 µs slice, which is 42 MCU cycles. **3. Per-cycle lockstep.** Step the MCU every 12 T-states. Only needed if a program is found that reacts inside one 06xx byte period, which none of the five does. Whichever is used, three ordering rules are not optional: - **/IRQ is an edge.** The MCU must be run up to the moment of the edge *before* the edge is delivered, or the interrupt lands in the wrong instruction and the pending mask can swallow a second one. - **/TC is an edge too**, and Namco's 51xx counts vblank on it. Deliver it at the same point in the frame every time. - **/RESET is a level.** Galaga's LS259 misc latch bit 3 holds the 51xx, the 54xx and both sub-Z80s in reset from power-on until the main program releases them; a host that starts the MCU at frame 0 will have it running 699 frames of program before the game expects it to exist. ## What `step()` should return, and what it should not claim `step()` returns the instruction's cycle count from the table above: 1, 2, or, when the call accepts an interrupt instead of executing an instruction, 3. Multiply by 6 for clock periods. It does not claim sub-cycle bus behaviour, because there is nothing to claim: no instruction on this chip performs more than one data-memory access, ports are latches, and the datasheet publishes no bus timing for the internal ROM or RAM. The only timing a host can observe is *when* a port callback fires relative to other chips, and that is the host's scheduling decision, not the core's.