# Timing: cycle rules, and what a Williams host needs from the core The 6809 counts everything in **E-clock cycles** (bus cycles). Every instruction's cost in [start-here](start-here.md) is a count of bus cycles, and on the real part every one of those cycles is a bus access: opcode and operand fetches, the operand reads and writes, and "internal" cycles that are really dummy reads, either of the byte after the instruction or of address `$FFFF` with VMA (valid memory address) low. MAME models exactly that (`read_opcode_arg`, `dummy_read_opcode_arg`, `dummy_vma` in `m6809.h`, each costing one cycle), and the `neetandev/m6809` corpus records each dummy read as a real read. A host that maps something at `$FFFF` (the low byte of the reset vector, on every 6809 board) therefore sees those reads; none of them has side effects on ordinary ROM. ## Cycle rules - **Base cost** per instruction is the datasheet table. The MC6809 and MC6809E tables are the same; the parts differ in clock generation, not in instruction timing. - **Prefix** `10` or `11`: +1 cycle, +1 byte. - **Indexed postbyte** additions, from the table in start-here: 0 for `,R`; 1 for a 5-bit offset, `A,R`, `B,R`, 8-bit offset and 8-bit PCR; 2 for `,R+` and `,-R`; 3 for `,R++` and `,--R`; 4 for 16-bit offset and `D,R`; 5 for 16-bit PCR and `[n]`; **+3 more** for any indirect form. - **PSHS/PULS/PSHU/PULU**: 5 + one per byte moved (`PSHS PC,U,Y,X,DP,B,A,CC` is 17). - **RTI**: 6 with E clear, 15 with E set. - **Long conditional branches**: 5, or 6 when taken. `LBRA` 5, `LBSR` 9. Short branches 3 either way; `BSR` 7. - **Interrupt entry** (MAME's model, consistent with the datasheet's timing diagram and with the measured trace in [mame-oracle](mame-oracle.md)): NMI and IRQ 19, FIRQ 10, from the end of the interrupted instruction to the first cycle of the handler. `SWI` 19, `SWI2`/`SWI3` 20, `CWAI` 20 plus the wait, `SYNC` 4 (datasheet) or 3 (MAME) plus the wait. - **Reset**: MAME does not charge cycles for the vector fetch on reset (it is done in `device_reset` by setting the effective address and going through the vector state on the first `execute_run`). The datasheet shows RESET timing in E cycles; a host cares only that PC is loaded before the first instruction. `[cost unverified]`. - **HCF** (`14`/`15`/`CD`): the address bus walks one address per cycle until RESET. A core should treat the state as "halted, consuming cycles at one per cycle" so a host's timers keep running. The `neetandev/m6809` corpus's `cycles` chunk (one entry per bus cycle, address and data recorded) is the cheapest way to check all of the above at once for 315 opcodes; it is MAME's model, so a pass means "matches MAME", and the datasheet table is the judge where the two disagree. ## What an arcade host needs The embedding contract is the one z80-python and 6502-python use: the host owns memory and I/O and calls `step()`; the core executes one instruction or one interrupt-entry sequence and returns its cycle count; the host advances its timers by that count and sets the interrupt lines before the next step. For a 6809 the lines are: | Line | Kind | Core behaviour at the next instruction boundary | | --- | --- | --- | | RESET | level | While asserted: DP=0, I=F=1, PC from `$FFFE`; no stacking. On release, execution begins there; NMI stays inhibited until S is loaded | | NMI | edge | Latched on the falling edge; taken regardless of I and F, and only after S has been loaded since reset | | FIRQ | level | Taken when F=0; stays pending while asserted. Stacks PC and CC only (E=0) | | IRQ | level | Taken when I=0; stays pending while asserted. Stacks everything (E=1) | Because FIRQ and IRQ are levels, the device that asserted them must be the thing that clears them. On Williams boards that device is a 6821 PIA: its IRQA/IRQB outputs stay low until the program reads the associated data register, which clears the flag. The core therefore must re-sample the level every boundary and must not "consume" an IRQ the way it consumes an NMI edge, or the host will lose interrupts. ## Williams (Defender, Robotron, Joust, Sinistar, Bubbles, Stargate ...) Restated from MAME 0.285 `src/mame/midway/williams.cpp` (BSD-3-Clause), with the values MAME uses and their derivation: | Item | MAME value | Source line | | --- | --- | --- | | Master clock | 12 MHz crystal (`MASTER_CLOCK = XTAL(12'000'000)`) | `williams.cpp:1530` | | CPU | `MC6809E` at `MASTER_CLOCK/3/4` = **1,000,000 Hz** E clock | `williams.cpp:1536` (and `:1833` for the second-generation boards) | | Video | `set_raw(MASTER_CLOCK*2/3, 512, 6, 298, 260, 7, 247)`: 8 MHz pixel clock, 512 pixel-clocks per line, 260 lines per frame, 292 x 240 visible | `williams.cpp:1555` | | Line rate | 8,000,000 / 512 = 15,625 lines/s: **64 CPU cycles per scanline** | derived | | Frame rate | 15,625 / 260 = **60.096 Hz**: 16,640 CPU cycles per frame | derived | | VA11 timer | fires every 32 scanlines from line 0, skipping line 256; sets PIA1 **CB1** to bit 5 of the scanline number | `williams.cpp:1545`, `williams_m.cpp:19-29` | | COUNT240 timer | fires at scanline 240; sets PIA1 **CA1** to (scanline >= 240) | `williams.cpp:1548`, `williams_m.cpp:32-38` | | IRQ | PIA1 IRQA and IRQB, ORed by an `INPUT_MERGER_ANY_HIGH`, drive `M6809_IRQ_LINE` | `williams.cpp:1565,1576,1577` | | FIRQ | not connected on the first-generation boards; on the second-generation `mysticm` config PIA0 IRQA drives `M6809_FIRQ_LINE` | `williams.cpp:1913` | | NMI | not connected | | | Blitter | each blit **steals CPU cycles**: MAME estimates the blit length at 4 MHz as `4*(accesses+2)` or `2*(accesses+3)` clocks (by mode) plus 4, divides by 4 and subtracts that many cycles from the CPU's slice (`m_cpu->adjust_icount(...)`) | `williamsblitter.cpp:123-132` | | Watchdog | writes of `$39` to `$CBFF` | seen in every trace | So "the video-counter-driven IRQ at 4 per frame" is precisely this: VA11 is the sixth bit of the vertical counter, it changes state at lines 32, 64, 96, ..., 224 and (because the 256 event is suppressed) 0, and the 6821 raises IRQB on whichever edge the program selected in control register B. Robotron selects the **falling** edge: the measured trace shows IRQ entry at scanlines 64, 128, 192 and 0, four per frame, 516 consecutive frames with exactly four ([mame-oracle](mame-oracle.md)). COUNT240 on CA1 gives a fifth, once-per-frame interrupt at line 240 if the program enables IRQA; Robotron's attract mode did not in the 20 s sampled. A Python host for these boards therefore needs, from the core, only `step()` returning cycles and level-sensitive IRQ; from itself, a scanline counter advanced 1 per 64 cycles, the two PIA edge detectors, and the blitter's cycle theft applied as an extra advance of the host clock (not as a change to the core). The second-generation boards (`williams2_base`: Mystic Marathon, Turkey Shoot, Inferno, Joust 2) use the same VA11 signal into PIA0 CB1 **and** PIA1 CA1, an `/ENDSCREEN` timer at line 254 (`254_timer`, `endscreen_callback`), and a slightly different raster (`set_raw(MASTER_CLOCK*2/3, 512, 8, 284, 260, 8, 248)`, `williams.cpp:1854`). ## Other 6809 arcade hosts (not measured here) Named so a later session knows where to look, all in MAME 0.285 sources: - **Konami Gyruss / Time Pilot era**: Time Pilot's main CPU is a Z80; Gyruss has a Z80 main CPU, an M6809 sub CPU (`gyruss.cpp`) and an i8039; the 6809 runs at 2 MHz on that board. Konami's later boards (Nemesis-era onward) move to 68000 or to the Konami-1 custom 6809, which is out of scope. - **Namco System 86** (`namcos86.cpp`): two MC6809 at 1.536 MHz (49.152 MHz / 32) plus an HD63701 for I/O and sound. - **Data East** (`dec8.cpp`, `karnov.cpp` ...): MC6809E or HD6309 main CPUs at 1.5-3 MHz with `M6809` sound CPUs; the HD6309 ones are out of scope until a 6309 mode is added. - **Vectrex** (`vectrex.cpp`, a home machine): 6809 at 1.5 MHz with a 6522 VIA driving the vector hardware, the usual second test bed for 6809 cores because of the timing-sensitive vector drawing. Each of those hosts would be a new `docs/hosts/.md` page like the Williams section above, written from the MAME driver and then measured with a trace.