# Cycles, and what an arcade host needs ## The cycle model The 6800 family uses one bus cycle per clock. There is no prefetch queue, no wait-state logic in the CPU, and no instruction whose cost depends on its data — **every opcode has a fixed cycle count**, including branches: `BNE` costs 4 cycles on the 6800 whether it branches or not (unlike the 6502, where a taken branch is longer). A core therefore needs one 256-entry table per part and nothing else. Cycle counts per opcode are in the table in [start-here.md](start-here.md), generated by `scripts/extract_manual_tables.py` from the per-instruction pages of M68PRM Appendix A (MC6800) and M6801RM Appendix A (MC6801/6803). MAME's `cycles_6800[]` and `cycles_6803[]` agree with the manuals on every one of the 197 and 220 documented opcodes (checked 2026-09-18, `--report`). For the opcodes the manuals do not document, both MAME tables use a sentinel: `#define XX 4 // illegal opcode unknown cycle count`, under the comment "to prevent the possibility of MAME locking up, don't use 0 cycles here" (`m6800.cpp:248-249`, `m6801.cpp:133-134`). **That 4 is an invention, not a measurement** — and it was 5 in MAME 0.261 — see [undocumented-behavior.md](undocumented-behavior.md). ## Where the 6801/6803 differ on opcodes the 6800 already had This is the part most easily got wrong, because the 6801 is usually described as "the 6800 plus ten instructions". Of the 197 opcodes both manuals document, the 6801 is **faster on 72** and **slower on three** (the non-indexed `CPX` forms); the other 122 are unchanged. Every row below is a difference between M68PRM's and M6801RM's Appendix A pages, and the rows add up to the 75: | Group | Opcodes | 6800 | 6801/6803 | | --- | --- | --- | --- | | `BRA` and the fourteen conditional branches | `$20`, `$22`-`$2F` (15) | 4 | **3** | | `INX`, `DEX` | `$08`, `$09` | 4 | **3** | | `TSX`, `INS`, `DES`, `TXS`, `PSHA`, `PSHB` | `$30`,`$31`,`$34`-`$37` | 4 | **3** | | Indexed read/modify/write: `NEG` `COM` `LSR` `ROR` `ASR` `ASL` `ROL` `DEC` `INC` `TST` `CLR` | `$60`,`$63`,`$64`,`$66`-`$6A`,`$6C`,`$6D`,`$6F` (11) | 7 | **6** | | `JMP` indexed | `$6E` | 4 | **3** | | Indexed 8-bit accumulator operations (`SUB` `CMP` `SBC` `AND` `BIT` `LDA` `EOR` `ADC` `ORA` `ADD`) | `$A0`-`$A2`,`$A4`-`$A6`,`$A8`-`$AB`, and the same at `$Ex` (20) | 5 | **4** | | `STAA`/`STAB` indexed | `$A7`, `$E7` | 6 | **4** | | `STAA`/`STAB` direct, extended | `$97`,`$D7`; `$B7`,`$F7` | 4; 5 | **3; 4** | | `LDS`/`LDX` indexed | `$AE`, `$EE` | 6 | **5** | | `STS`/`STX` direct, extended, indexed | `$9F`,`$DF`; `$BF`,`$FF`; `$AF`,`$EF` | 5; 6; 7 | **4; 5; 5** | | `BSR` | `$8D` | 8 | **6** | | `JSR` indexed, extended | `$AD`, `$BD` | 8, 9 | **6, 6** | | **`CPX`** immediate, direct, extended | `$8C`,`$9C`,`$BC` | 3, 4, 5 | **4, 5, 6** — *slower*: the 6801 does a true 16-bit compare and sets C (M6801RM p. A-39), where the 6800 compares two bytes and leaves C alone (M68PRM p. A-33) | Unchanged: `CPX` indexed (`$AC`, 6 on both), the other extended accumulator operations (4), direct accumulator operations (3), 8-bit immediates (2), `LDS #`/`LDX #` (3), `LDS`/`LDX` direct (4) and extended (5), every inherent two-cycle instruction including `TAP`/`TPA` (2), `JMP` extended (3), extended read/modify/write (6), `PULA`/`PULB` (4), `RTS` (5), `RTI` (10), `WAI` (9) and `SWI` (12). `PULA`/`PULB` staying at 4 while `PSHA`/`PSHB` drop to 3 is not a typo; both manuals say so (M68PRM p. A-53, M6801RM p. A-64). The two 6801-only opcodes that reuse old mnemonics are `BRN` (`$21`, 3) and `JSR` direct (`$9D`, 5). MAME also runs both on the 6800, at 4 and 6 cycles; neither exists in M68PRM ([undocumented-behavior.md](undocumented-behavior.md)). ## Interrupt and reset costs | Event | 6800 and 6801/6803 | Where MAME encodes it | | --- | --- | --- | | `IRQ`/`NMI` entry, not from `WAI` | **12** cycles charged after the interrupted instruction completes — MCSDD Figure 13 (MC6800) and M6801RM §5.3, Figure 5-12 (MC6801) | `m6800.cpp:449-473`, `cycles_to_eat = 12` | | `IRQ`/`NMI` entry out of `WAI` | **4** on both: APPS p. A-14, Q20 (MC6800); M6801RM §5.4.2 (MC6801) | same, `cycles_to_eat = 4` | | `SWI` | 12 (the opcode's own cycle count) | `cycles_6800[0x3F] = 12` | | `WAI` | 9, then the CPU idles | `cycles_6800[0x3E] = 9` | | `RTI` | 10 | `cycles_6800[0x3B] = 10` | | Reset | not modelled; MAME jumps straight to the vector | `m6800.cpp:577-585` | **Measured, not assumed.** In the Drag Race run described in [mame-oracle.md](mame-oracle.md), `totalcycles` across an accepted IRQ goes `5501 → 5517` where the interrupted instruction is `BNE` (4 cycles) and `9599 → 9615` where it is `INX` (4 cycles): 16 = 4 + 12 both times. In the Robotron sound-CPU run the interrupted instruction is `BEQ` (4 cycles) and `totalcycles` goes `10188921 → 10188937`: again 16. **Settled from the manuals (2026-09-18): 12, not 13.** MCSDD's MC6800 data sheet, Figure 13 "Interrupt Timing" (printed p. 17), counts the entry cycle by cycle: the next opcode is fetched and discarded, one internal cycle, seven pushes, one internal cycle in which I is set, two vector reads — twelve — and the routine's first opcode fetch follows. The MC6801 is the same (M6801RM §5.3, "the interrupt sequence requires 12 MPU E-cycles to complete once it has begun", and Figure 5-12, p. 5-14). The **13** usually quoted is the *response time* M6801RM gives from `NMI` going active to the routine's first fetch: one cycle of recognition plus the 12 (and 14 for `IRQ1`, which spends an extra cycle synchronising). For a core that samples at instruction boundaries the 12 is the number to charge, and MAME's measured 16 = 4 + 12 above agrees. **Out of `WAI`: 4, on both parts.** For the MC6800, APPS answers it in so many words: "Four MPU cycles are required to start the interrupt sequence after a WAI instruction" (p. A-14, Q20). For the 6801, M6801RM §5.4.2 (pp. 5-19 to 5-21) gives five cycles from `NMI` to the first fetch after a `WAI` (six for `IRQ1`), which on the same accounting is 4 cycles of sequence. MAME's 4 is right for both. (An earlier revision of this page read MCSDD's Figure 14 as 5 for the MC6800 and left the question open; APPS settles it.) **What an instruction-stepping core cannot express.** APPS (pp. A-10 to A-13) documents three MC6800 races that happen *inside* an instruction or an interrupt sequence: an `NMI` arriving during `SWI` fetches its vector from the **IRQ** location (Q9); an `NMI` arriving during the nine stacking cycles of an `IRQ` entry wins, and the `IRQ` pulse is lost (Q11; M6801RM §5.3 says the same for the 6801); an `IRQ` pulse shorter than two cycles, or arriving while I is set or during `SWI`, is lost (Q10). The core samples its inputs at instruction boundaries, so a host cannot place an edge inside an instruction and none of these arise; they are recorded here, not modelled. ## When interrupts are sampled A real 6800 samples `IRQ`/`NMI` at the end of every instruction. **MAME does not**: `m6800_cpu_device::execute_run()` calls `check_irq_lines()` once at the top of a scheduler timeslice and then runs instructions until `m_icount` expires (`m6800.cpp:606-624`). Interrupts are otherwise checked only inside `TAP`, `CLI`, `RTI` and `WAI` (`6800ops.hxx:80-92`, `135-144`, `431+`, `458-475`), which is what makes the common idioms work, but a line asserted mid-timeslice is not seen until the scheduler next breaks. In practice MAME breaks the timeslice on the timer that raises the interrupt, so the latency is usually right. For this project it means: **interrupt latency is exactly where a MAME trace is least trustworthy as an oracle**, and `m6800-python` should sample at every instruction boundary (which is what hardware does) and expect small divergences against MAME traces at IRQ entry, not treat them as core bugs. The `CLI`/`TAP` one-instruction delay is real hardware behaviour and *must* be modelled: see [start-here.md](start-here.md), "Entry rules". MAME changed exactly this code between 0.261 and 0.285, so it is also the place where a trace comparison is most sensitive to which MAME produced the trace. ## What an arcade host has to supply ### The concrete example: the Williams arcade sound board Williams's first- and second-generation arcade boards (Defender, Stargate, Robotron: 2084, Joust, Bubbles, Splat!, Sinistar, Blaster) pair a **MC6809E main CPU** with a **MC6808 sound CPU**. From MAME's driver, read on disk, not from memory: ``` src/mame/midway/williams.cpp:1530 static constexpr XTAL MASTER_CLOCK = (XTAL(12'000'000)); src/mame/midway/williams.cpp:1531 static constexpr XTAL SOUND_CLOCK = (XTAL(3'579'545)); src/mame/midway/williams.cpp:1536 MC6809E(config, m_maincpu, MASTER_CLOCK/3/4); src/mame/midway/williams.cpp:1539 M6808(config, m_soundcpu, SOUND_CLOCK); // internal clock divider of 4, // effective frequency is 894.886kHz ``` So the sound CPU is a **6808, not a 6800**, it is given the whole 3.579545 MHz colourburst crystal, and the *part* divides by four. MAME encodes that divider on the device, not in the driver: ``` src/devices/cpu/m6800/m6800.h:371 virtual u64 execute_clocks_to_cycles(u64 clocks) const noexcept override { return (clocks + 4 - 1) / 4; } // on m6802_cpu_device, inherited by m6808 ``` 3,579,545 ÷ 4 = **894,886.25 Hz**, one cycle every 1.1175 µs. Confirmed against the running binary: `mame -listxml robotron` reports `` (MAME 0.285). The plain `M6800` device has no such override, so for a 6800 the number in the driver *is* the bus rate; the `M6801` has the ÷4 and the `M6803E` does not (`m6801.h:84` and `m6801.h:264`). Two boards double this up: Sinistar's cockpit and Blaster each add a second 6808 at the same clock (`williams.cpp:1709,1794`), and the later `williams2` hardware (Mystic Marathon, Turkey Shoot, Inferno, Joust 2) runs its 6808 from `MASTER_CLOCK/3` = 4 MHz, i.e. **1.000 MHz** after the divider, with MAME's source commenting "yes, this is different from the older games" (`williams.cpp:1835`). Two variants are worth knowing about: the `nextcent` Defender conversion runs its sound CPU from a 3.12 MHz crystal (`williams.cpp:1608`), and some Defender bootlegs fit an **M6802** instead of the 6808 (`williams.cpp:1615`, `defender_6802snd`). **The host contract this implies.** The sound 6808 has no timer of its own. Everything that happens to it comes through one MC6821 PIA: - The main 6809 writes a sound command to its own PIA; that lands on the sound PIA's port A and pulls **CB1**, and the PIA's IRQ outputs are merged into the 6808's `IRQ` line (`williams.cpp`, `williams_base()`: `INPUT_MERGER_ANY_HIGH(config, "soundirq").output_handler().set_inputline(m_soundcpu, M6808_IRQ_LINE)`). - The 6808's handler reads the command from the PIA, which clears the interrupt, and drives a DAC through port B. - Between commands the ROM sits in a one-instruction self-loop. In the trace of Robotron's `:soundcpu` it is literally `FB83: beq $FB83`, executed 3,883,733 times out of 4,555,721 instructions in a twenty-second run. So the host a `m6800-python` sound board needs is: a memory map, a PIA model with CB1 edge detection and an IRQ output, a DAC sink, and a cycle budget of 894,886 per emulated second. No timer, no NMI, no DMA, no bus contention. That is the simplest useful 6800 host there is, and it is why the Williams sound board is the target the handoff picks first. ### What a 6801/6803 host needs instead The 6801 and 6803 carry their own 16-bit free-running counter with input capture and output compare, and an SCI — and those generate **four extra interrupt vectors** below `$FFF8` ([start-here.md](start-here.md)). Under this project's contract those are **host** devices: the core exposes a request per vector and the host owns the counter, the registers at `$0000-$001F` and the serial shift registers. `scripts/m6803_board.py` builds exactly this (TCSR, the counter, output compare) for Escape from the Lost World's pinball MPU, whose captured trace shows it reading TCSR 7,710 times and taking 6,241 output-compare interrupts, every one of them now the board's own timer deciding, verified against MAME — a 6803 host is materially more work than a 6808 one, and that script is the worked example. **Not Knuckle Joe.** An earlier draft of this page said Knuckle Joe's sound ROM spends its captured ten seconds in the timer interrupt handler. It does not: the trace shows zero accesses anywhere in `$0008-$0014` and zero timer-sourced interrupts; its 40,050 taken interrupts are NMI and IRQ, both off-chip. The ROM does contain one instruction that reads the counter (`$7C4D LDAB $09`), but it never runs in the captured window, and the ROM never writes TCSR or the output-compare register anywhere in its 8 KiB, so no capture length would show an armed timer interrupt there. Checked directly against the ROM and the trace, not assumed; see `scripts/m6803_board.py`'s module docstring. ### Arcade clocks worth knowing Collected from MAME's drivers at the **`mame0285` tag** — the version of the installed binary, not the 0.261 tree that happens to be on this machine (`docs/validation.md`). File and line are given so each can be re-checked, and the bus-rate column is `-listxml`-confirmed where the table says so: | Board / game | CPU | Clock as configured | Bus rate | Source | | --- | --- | --- | --- | --- | | Williams gen-1/2 sound | MC6808 | 3.579545 MHz | 894,886 Hz | `midway/williams.cpp:1539` | | Williams gen-3 sound (`williams2`) | MC6808 | 12 MHz ÷ 3 | 1,000,000 Hz | `midway/williams.cpp:1835` | | Atari Drag Race | MC6800 | 12.096 MHz ÷ 12 | 1,008,000 Hz | `atari/dragrace.cpp:437`; `-listxml dragrace` agrees | | Atari Fire Truck | MC6800 | 12.096 MHz ÷ 12, **switched to ÷ 16 in service mode** | 1,008,000 / 756,000 Hz | `atari/firetrk.cpp:668` `set_unscaled_clock(...)` — a host that changes the CPU clock at runtime | | Atari Orbit, Sky Diver, Destroyer | MC6800 | 12.096 MHz ÷ 16 | 756,000 Hz | `atari/orbit.cpp:441`, `atari/skydiver.cpp:516`; `-listxml orbit` agrees | | Taito Qix sound | MC6802 | 3.6864 MHz | 921,600 Hz | `taito/qix_a.cpp:147` | | Irem M52/M62 sound | MC6803 | 3.579545 MHz | 894,886 Hz | `irem/irem.cpp:403,469,503` ("verified on pcb") | | Knuckle Joe sound | MC6803 | 3.579545 MHz | 894,886 Hz | `seibu/kncljoe.cpp:535` ("verified on PCB") | | Zaccaria melody + audio | 2 × MC6802 | 3.579545 MHz | 894,886 Hz each | `zaccaria/zaccaria_a.cpp:232,430` ("verified on pcb") | | Konami Hyper Olympic bootleg ADPCM sub-board | MC6802 | 14.318181 MHz ÷ 8 | 447,443 Hz | `konami/trackfld.cpp:1050` and `konami/hyperspt.cpp:676`, both commented "unknown clock" | | Tora Tora (Game Plan) | MC6800 | 5.185 MHz ÷ 8 | 648,125 Hz | `misc/toratora.cpp:405` | | Field Goal (Taito) | MC6800 | 10.065 MHz ÷ 10 | 1,006,500 Hz | `taito/fgoal.cpp:538` (MAME marks it "?") | Where MAME's own comment says "?" or "unknown clock", treat the number as MAME's guess and not as a measurement.