# Timing: every cycle, with its address and kind Units: CPU cycles. On the SNES each cycle then lasts 6, 8 or 12 master clocks by its kind and address ([start-here.md](start-here.md) "The bus"); that conversion is the board's. Sources: WDC W65C816S datasheet, 2024-03-13, **Table 5-7** "Instruction Operation" (pp. 36-42) with its notes 1-17 (p. 43) (manufacturer tier), transcribed literally in `scripts/datasheet_timing.py`; SingleStepTests 65816 @ `307c63f` (`src/65816/v1`, emulator tier); bsnes `processor/wdc65816` @ `78474e5` (emulator tier). Agreement is in [validation.md](validation.md); contradictions are numbered in [undocumented-behavior.md](undocumented-behavior.md). ## Agreement measured (2026-10-21) `python -I scripts/datasheet_timing.py --cases 10000` runs Table 5-7, read literally (no emulation-mode wrapping, no knowledge from any emulator), on the initial state of **every one of the 5,080,400 SingleStepTests cases** (508 files x 10,000, plus MVN/MVP 4 x 100 whole moves) and compares cycle by cycle (a few minutes on CPython 3.14; reading the 2.8 GB of JSON dominates). | Measure | Cases agreeing | Where they disagree | | --- | --- | --- | | number of cycles | **5,080,400 / 5,080,400** | nowhere: every opcode, both modes, every M/X/D.l/page-cross/branch path, WAI and STP (3 cycles), MVN/MVP per byte | | VDA, VPA, VPB, RWB, MLB on every cycle | 5,020,400 | exactly three opcodes, all cases of each, both modes: `42` WDM, `7C` JMP (a,x), `FC` JSR (a,x) (C1, C2) | | address of every VDA/VPA/VPB cycle | 5,079,305 | 1,095 cases, all emulation mode, all explained by U1 (direct page wraps when D.l = 0) and U2 (old instructions wrap the stack in page one) | | address of every IO cycle | 5,080,202 | 198 cases, the same U1/U2 cases' IO cycles (RTS's last cycle at S; RMW's modify cycle) | Before comparing, the script applies two chip facts the corpus leaves out of its initial states: in emulation mode SH is $01 (DS §2.11), and with X = 1 XH = YH = 0 (DS §2.7). The corpus's emulation-mode initial `s` often has a high byte other than $01 (`48.e` case 0: `s` = $0FD3, the push lands at $01D3); a harness loading SST states must do the same. Two literal readings were needed and are recorded as datasheet ambiguities: Table 5-7 prints the read-modify-write modify cycle's address as `AA+1` in every row, which exists only for 16-bit data; with 8-bit data the corpus uses the operand address `AA`. The `WDM` row is missing from Table 5-7; Table 5-4 says 2 bytes, 2 cycles. So, for the core: **Table 5-7 is the bus schedule**, cycle counts and kinds exact against 5 million emulator-derived cases, with three kind disagreements (C1, C2) and the emulation-mode wrapping rules (U1-U4) layered on top. The corpus, not the datasheet, is silent on: IO cycle addresses where Table 5-7 calls them invalid (they agree anyway), interrupts, ABORT, RDY, and the (d,x) emulation-mode quirk (C4). ## The schedule, by Table 5-7 row Notation: one entry per cycle, starting with the opcode fetch. `F@PC` opcode fetch (VDA VPA), `P@x` program read (VPA), `R@x`/`W@x` data read/write (VDA), `I@x` IO (neither), `V@x` vector read (VDA, VPB low), `+` marks a cycle that only happens under the note in brackets. Program addresses are PBR,PC+n with PC wrapping at 16 bits; `0,x` is bank 0 with x wrapped at 16 bits; `DBR,x` and long addresses carry through 24 bits. `w` = data width (M, or X for the index-register instructions); a "+1 (w16)" cycle reads or writes the high byte at address + 1, after the low byte, except as noted for RMW. | Row | Mode / instructions | Cycles | | --- | --- | --- | | 1a | `a` ADC AND BIT CMP CPX CPY EOR LDA LDX LDY ORA SBC STA STX STY STZ | `F@PC`, `P@PC+1`, `P@PC+2`, `R/W@DBR,AA`, +`R/W@DBR,AA+1` (w16) | | 1b | `a` JMP | `F`, `P@PC+1`, `P@PC+2` | | 1c | `a` JSR | `F`, `P@PC+1`, `P@PC+2`, `I@PC+2`, `W@0,S` (PCH), `W@0,S-1` (PCL) | | 1d | `a` RMW ASL DEC INC LSR ROL ROR TRB TSB | `F`, `P@PC+1`, `P@PC+2`, `R@AA` (MLB), +`R@AA+1` (w16), `I@AA` / `I@AA+1` (w16) (**E = 1: a write of the old value**, note 17), +`W@AA+1` (w16), `W@AA` -- **high byte written first** | | 2a | `(a,x)` JMP | `F`, `P@PC+1`, `P@PC+2`, `I@PC+2`, `P@PBR,AA+X`, `P@PBR,AA+X+1` -- SST marks the last two VDA, not VPA (C1) | | 2b | `(a,x)` JSR | `F`, `P@PC+1`, `W@0,S` (PCH), `W@0,S-1` (PCL), `P@PC+2`, `I@PC+2`, `P@PBR,AA+X`, `P@PBR,AA+X+1` -- pushes before the high operand byte; same C1 | | 3a | `(a)` JML ($DC) | `F`, `P@PC+1`, `P@PC+2`, `R@0,AA`, `R@0,AA+1`, `R@0,AA+2` | | 3b | `(a)` JMP ($6C) | `F`, `P@PC+1`, `P@PC+2`, `R@0,AA`, `R@0,AA+1` | | 4a | `al` ADC AND CMP EOR LDA ORA SBC STA | `F`, `P@PC+1..+3`, `R/W@AAB,AA`, +`AA+1` (w16) | | 4b | `al` JML ($5C) | `F`, `P@PC+1..+3` | | 4c | `al` JSL | `F`, `P@PC+1`, `P@PC+2`, `W@0,S` (PBR), `I@0,S`, `P@PC+3`, `W@0,S-1` (PCH), `W@0,S-2` (PCL) | | 5 | `al,x` | `F`, `P@PC+1..+3`, `R/W@AAB,AA+X`, +`+1` (w16) | | 6a, 7 | `a,x`, `a,y` reads and stores | `F`, `P@PC+1`, `P@PC+2`, +`I@DBR,AAH,(AAL+XL)` (note 4: page crossed, or a store, or X = 0), `R/W@DBR,AA+X`, +`+1` (w16) | | 6b | `a,x` RMW | as 6a with the IO cycle always, then the 1d tail at AA+X | | 8 | `A` accumulator ASL DEC INC LSR ROL ROR | `F`, `I@PC+1` | | 9a, 9b | `xyc` MVN MVP, per byte | `F@PC`, `P@PC+1` (dest bank), `P@PC+2` (source bank), `R@SBA,X`, `W@DBA,Y`, `I@DBA,Y`, `I@DBA,Y`; repeated (opcode refetched) until C = $FFFF | | 10a | `d` | `F`, `P@PC+1`, +`I@PC+1` (note 2: D.l ≠ 0), `R/W@0,D+DO`, +`+1` (w16) | | 10b | `d` RMW | `F`, `P@PC+1`, +`I@PC+1` (D.l), then the 1d tail at 0,D+DO | | 11 | `(d,x)` | `F`, `P@PC+1`, +`I@PC+1` (D.l), `I@PC+1`, `R@0,D+DO+X`, `R@0,D+DO+X+1`, `R/W@DBR,AA`, +`+1` (w16) | | 12 | `(d)` | `F`, `P@PC+1`, +`I@PC+1` (D.l), `R@0,D+DO`, `R@+1`, `R/W@DBR,AA`, +`+1` | | 13 | `(d),y` | `F`, `P@PC+1`, +`I` (D.l), `R@0,D+DO`, `R@+1`, +`I@DBR,AAH,(AAL+YL)` (note 4), `R/W@DBR,AA+Y`, +`+1` | | 14 | `[d],y` | `F`, `P@PC+1`, +`I` (D.l), `R@0,D+DO`, `R@+1`, `R@+2`, `R/W@AAB,AA+Y`, +`+1` | | 15 | `[d]` | as 14 without Y | | 16a, 17 | `d,x`, `d,y` | `F`, `P@PC+1`, +`I` (D.l), `I@PC+1`, `R/W@0,D+DO+X`, +`+1` | | 16b | `d,x` RMW | `F`, `P@PC+1`, +`I` (D.l), `I@PC+1`, then the 1d tail at 0,D+DO+X | | 18 | `#` immediate | `F`, `P@PC+1`, +`P@PC+2` (w16); **REP, SEP: `F`, `P@PC+1`, `I@PC+1` always** (notes 1, 8) | | 19a | implied, 25 instructions incl. XCE, NOP | `F`, `I@PC+1` | | 19b | XBA | `F`, `I@PC+1`, `I@PC+1` | | 19c | STP | `F`, `I@PC+1`, `I@PC+1`, then stopped until RESB | | 19d | WAI | `F`, `I@PC+1`, `I@PC+1`, then RDY low until NMI/IRQ/ABORT/RESET (DS: "wait at cycle 2 for 2 cycles after NMIB or IRQB active", note 9) | | 20 | relative branches incl. BRA | `F`, `P@PC+1`, +`I@PC+1` (taken), +`I@PC+1` (taken and E = 1 and page crossed) | | 21 | `rl` BRL | `F`, `P@PC+1`, `P@PC+2`, `I@PC+2` | | 22a | IRQ, NMI, ABORT, RESET | `F@PC` (discarded), `I@PC`, +`W@0,S` (PBR, native only), `W@0,S-1` (PCH), `W@0,S-2` (PCL), `W@0,S-3` (P), `V@0,VA`, `V@0,VA+1`. RESET: the push cycles are reads (note 10) | | 22b | PLA PLB PLD PLP PLX PLY | `F`, `I@PC+1`, `I@PC+1`, `R@0,S+1`, +`R@0,S+2` (16-bit: PLD always, PLB/PLP never) | | 22c | PHA PHB PHD PHK PHP PHX PHY | `F`, `I@PC+1`, +`W@0,S` (high byte), `W@0,S-1` / `W@0,S` (8-bit) | | 22d | PEA | `F`, `P@PC+1`, `P@PC+2`, `W@0,S` (high), `W@0,S-1` (low) | | 22e | PEI | `F`, `P@PC+1`, +`I` (D.l), `R@0,D+DO`, `R@+1`, `W@0,S`, `W@0,S-1` | | 22f | PER | `F`, `P@PC+1`, `P@PC+2`, `I@PC+2`, `W@0,S`, `W@0,S-1` | | 22g | RTI | `F`, `I@PC+1`, `I@PC+1`, `R@0,S+1` (P), `R@S+2` (PCL), `R@S+3` (PCH), +`R@S+4` (PBR, native only) | | 22h | RTS | `F`, `I@PC+1`, `I@PC+1`, `R@0,S+1`, `R@0,S+2`, `I@0,S+2` (PC + 1 computed) | | 22i | RTL | `F`, `I@PC+1`, `I@PC+1`, `R@0,S+1..+3` | | 22j | BRK, COP | `F`, `P@PC+1` (signature), +`W@0,S` (PBR, native only), `W` PCH, `W` PCL, `W` P, `V@0,VA`, `V@0,VA+1` | | 23 | `d,s` | `F`, `P@PC+1`, `I@PC+1`, `R/W@0,S+SO`, +`+1` (w16) | | 24 | `(d,s),y` | `F`, `P@PC+1`, `I@PC+1`, `R@0,S+SO`, `R@+1`, `I@0,S+SO+1`, `R/W@DBR,AA+Y`, +`+1` | | -- | `$42` WDM | `F`, then cycle 2 at PC+1: SST records it as IO (VDA = VPA = 0) and skips the byte; not in Table 5-7 (C2) | Vectors (VA): emulation $FFFE IRQ/BRK, $FFFC RES, $FFFA NMI, $FFF8 ABORT, $FFF4 COP; native $FFEE IRQ, $FFEA NMI, $FFE8 ABORT, $FFE6 BRK, $FFE4 COP (DS Tables 5-2, 5-3). Always bank 0; PBR becomes 0. ## The extra-cycle rules in one place (DS notes, p. 43) | Note | Rule | Applies to | | --- | --- | --- | | 1 | +1 cycle (and +1 byte for immediate) for 16-bit data: M = 0 or, for index instructions, X = 0 | every data access, pushes/pulls of A/X/Y | | 2 | +1 cycle when D.l ≠ 0 | every direct-page mode and PEI | | 4 | +1 cycle for indexing across a page, or a write, or X = 0 | `a,x` `a,y` `(d),y`; the IO cycle drives `DBR,AAH,AAL+XL`, "invalid" | | 5, 6 | +1 for a taken branch; +1 more if E = 1 and the branch crosses a page | relative branches (not BRL) | | 7 | -1 cycle in emulation mode | the PBR push/pull of interrupts, BRK, COP, RTI | | 8 | REP and SEP always 3 cycles | | | 9 | WAI waits at cycle 2 | | | 10 | RWB stays high (reads) during reset's stack cycles | | | 17 | in emulation mode RMW's modify cycle has RWB low (a write) | 1d, 6b, 10b, 16b | No other condition changes a cycle count: decimal mode adds nothing (unlike the 65C02), the 65C02's "+1 for page crossing in `JMP (a)`" does not apply, and native-mode branches never pay for a page crossing. ## Interrupt sampling The datasheet gives no sampling point. ares (ISC, read) samples immediately before the final bus cycle of every instruction and interrupt entry, testing NMI's edge and IRQ's level there; bsnes and Mesen2 were read to give the same one-cycle-early sample (HYPOTHESIS for those two: read from their GPL source before the 2026-11-14 rule; for review, 2026-11-24). This is the 6502's rule (the poll in the last cycle sees lines as they were at the end of the second-to-last), which 6502-python already implements, including its special cases for branches (SST cannot check any of it: it has no interrupt cases). Points to carry over or verify on the board (each HYPOTHESIS: read from bsnes's GPL source before the 2026-11-14 rule; for review, 2026-11-24): - if an interrupt will be taken, the IO cycle of implied 2-cycle instructions becomes a read of PC (U9, length 8 instead of 6 on the SNES in slow ROM); - the 5A22 does not let an interrupt in for one instruction after a DMA or a write to $4200 (board); - WAI: IO cycles while waiting, polling as a last cycle each time, and one more IO cycle after waking. ## MVN / MVP measured Each corpus case is a whole move; the per-byte 7-cycle pattern of rows 9a/9b (opcode refetch included, IO cycles at DBA,Y before the step) agrees on all 400 cases, emulation (X and Y stepping in 8 bits) and native. ## WAI and STP in the corpus `cb.*.json` and `db.*.json` record three cycles (fetch, two IO at PC+1, as Table 5-7) and then a fourth row `[null, null, "--------"]`, the corpus's mark for the stopped clock, not a bus cycle; `final.pc` is PC + 1. A rung-1 harness compares the first three and treats the fourth as "the core is now waiting/stopped".