# Timing: every cycle as a bus access A 65C02 puts an address on the bus and reads or writes in every cycle, so the core makes exactly one bus callback per cycle, in the chip's order, as `6502-python` does. This file gives each sequence, says where it differs from the NMOS part, and says what each line rests on. Three things are known at three different strengths: 1. **How many cycles, and which are writes**: measured on two real parts (CAP: SYNC and RnW sampled every cycle), a G65SC12 and a Rockwell R65C02. 2. **Which addresses the dummy cycles use**: the captures hold no address bus, but they hold the *data* of every read, and different addresses hold different bytes. The capture lockstep (rung 4, `tests/capture_replay.py`) keeps a model of memory and compares every read's byte with it; a dummy read from the wrong address differs from the model in most executions. That settles almost every dummy address on both parts, at tier 1, and in seven places against the SingleStepTests corpus (C10, C13 to C18). What it could not reach is listed at the end (U4). 3. **When interrupts are polled**: nothing above emulator tier (U5). The captures do not sample the IRQ line; they do show at which boundary each of 65,772 interrupt entries began, and none began where the core's rule forbids one. Notation: `PC` is the address of the opcode; `r` a read, `W` a write; `ea` the effective address; `zp` the operand byte; `base` the unindexed 16-bit address; `PC+2` etc. are addresses in the instruction stream. ## Measured: cycles per opcode on real chips (CAP) `python -I scripts/run_capture.py --cycles` replays the eight captures through the core in lockstep (rung 4) and counts each instruction's cycles **from SYNC to SYNC**, with the cycles in which the machine held the processor (RDY low: the Master's slow I/O) dropped. Run 2026-11-12: 83,856,649 instructions on the G65SC12 (reset and boot, Dormann's NMOS functional test, his 65C02 extended test, Clark's decimal test) and 84,037,666 on the R65C02, with no mismatch. Cycle counts seen per opcode, G65SC12 first where the R65C02 differs (`G|R`): ``` 01 6 02 2 03 1 04 5 05 3 06 5 07 1|5 08 3 09 2 0A 2 0B 1 0C 6 0D 4 0E 6 0F 1|5/6 10 2/3/4 11 5 12 5 13 1 14 5 15 4 16 6 17 1|5 18 2 19 4 1A 2 1B 1 1C 6 1D 4 1E 6 1F 1|5/6 20 6 21 6 22 2 23 1 24 3 25 3 26 5 27 1|5 28 4 29 2 2A 2 2B 1 2C 4 2D 4 2E 6 2F 1|5/6 30 2/3|2/3/4 31 5 32 5 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6/7 F2 5/6 F3 1 F4 4 F5 4/5 F6 6 F7 1|5 F8 2 F9 4/5/6 FA 4 FB 1 FC 4 FD 4/5/6 FE 7 FF 1|5/6 ``` - Only `x7` and `xF` truly differ between the two parts: one cycle on the G65SC12, five or six on the R65C02. `30`, `71` and `D9` differ only because one machine's runs happened to cross a page where the other's did not. - The `ADC`/`SBC` rows with several counts are binary, decimal, and a page crossed (`69 2/3`, `65 3/4`, `79 4/5/6`, ...). `F1` (and `71` on the R65C02) never ran without one of the two, so its smallest count is one above the base. - The `abs,X` shifts `1E 3E 5E 7E` took 6 in every execution (no run crossed a page with them); `DE FE` took 7 in every execution (C11). - Reset: 7 cycles on both; cycles 3 to 5 are writes on the G65SC12 and reads on the R65C02. Interrupt entry: 7 cycles, cycles 3 to 5 writes: 65,772 entries in the eight captures. - Never executed in any capture: `00` (`BRK`), `CB`, `DB`. ## Measured: the SingleStepTests grid (SST, emulator-generated) `python -I scripts/survey_sst.py --cases 1000` reads the three sets and prints cycles, lengths and read/write shapes per opcode. The three sets are identical in all of that except for 34 opcodes: | Opcodes | `wdc65c02` | `rockwell65c02` | `synertek65c02` | | --- | --- | --- | --- | | `07 27 47 67 87 A7 C7 E7` | `RMB`/`SMB`: 5 cycles `rrrrW` | same | 2 bytes, 3 cycles `rrr` (C2) | | `17 37 57 77 97 B7 D7 F7` | `RMB`/`SMB`: 5 cycles | same | 2 bytes, 4 cycles `rrrr` (C2) | | `0F 2F 4F 6F 8F AF CF EF` | `BBR`/`BBS`: 5 to 7 cycles | same | 3 bytes, 3 cycles (C2) | | `1F 3F 5F 7F 9F BF DF FF` | `BBR`/`BBS` | same | 3 bytes, 4 cycles (C2) | | `CB` | no cases (the file is empty) | 1 byte, 2 cycles `rr` | 1 byte, 2 cycles (C3) | | `DB` | no cases | 2 bytes, 4 cycles `rrrr` | 2 bytes, 4 cycles (C3) | Against the captures the corpus is wrong about `$5C` (4 cycles in all three sets; 8 on both real parts: C1), about `x7`/`xF` on a part without bit instructions (C2), and about the address of seven kinds of dummy read (C10, C13 to C18, marked below). Its cycle counts, directions, results and every other address agree with the core, which rung 1 checks case by case. ## Addressing modes Cycle counts and read/write order are CAP's. The address of each dummy cycle is CAP's too (rung 4: the captured byte agrees with the memory model in every execution on both parts) unless the line says otherwise. | Mode | Cycles | Bus, cycle by cycle | Differs from NMOS | | --- | --- | --- | --- | | immediate | 2 | `r PC`, `r PC+1` | no | | zero page | 3 | `r PC`, `r PC+1`, `r/W zp` | no | | `zp,X` `zp,Y` | 4 | `r PC`, `r PC+1`, `r PC+1` again, `r/W (zp+X)&$FF` | **yes**: the NMOS part reads `zp` in cycle 3; so does SST (C18) | | absolute | 4 | `r PC`, `r PC+1`, `r PC+2`, `r/W ea` | no | | `abs,X` `abs,Y` read | 4, +1 on a page cross | `r PC`, `r PC+1`, `r PC+2`, [cross: `r PC+2` again], `r ea` | **yes**: the extra cycle re-reads the last instruction byte, not `base-page:ea-low` (DS Table 7-1) | | `abs,X` `abs,Y` write (`STA`, `STZ abs,X`) | 5 | `r PC`, `r PC+1`, `r PC+2`, then `r ea` without a page cross or `r PC+2` again with one, `W ea` | only across a page; SST always re-reads `PC+2` (C14) | | `(zp,X)` | 6 | `r PC`, `r PC+1`, `r PC+1` again, `r (zp+X)&$FF`, `r (zp+X+1)&$FF`, `r/W ea` | **yes**, as `zp,X` (C18) | | `(zp),Y` read | 5, +1 on a page cross | `r PC`, `r PC+1`, `r zp`, `r (zp+1)&$FF`, [cross: `r (zp+1)&$FF` again], `r ea` | **yes**: the extra cycle re-reads the pointer's high byte; SST re-reads `PC+1` (C15) | | `(zp),Y` write | 6 | as above with the `r (zp+1)&$FF` again always, then `W ea` | **yes** (C15) | | `(zp)` (new) | 5 | `r PC`, `r PC+1`, `r zp`, `r (zp+1)&$FF`, `r/W ea` | new mode: `ORA AND EOR ADC STA LDA CMP SBC` at `$x2` | One rule covers every extra cycle above and the taken branch across a page and both indirect jumps below: **the chip repeats the read of the cycle before**. The manufacturers describe it for one case ("extra read of last instruction byte", DS Table 7-1); the captures show it for all of them. The exception is the indexed store that does not cross a page, where there is no wrong address to avoid and the cycle reads the operand's own address, as on the NMOS part. ### Read-modify-write "Two read and one write cycle" (DS Table 7-1; RDS, GDS, NDS; CAP shows one write and both reads returning the operand's byte): the operand is read twice and written once. `ASL LSR ROL ROR INC DEC TSB TRB RMB SMB`. | Form | Cycles | Bus after the addressing cycles | | --- | --- | --- | | zero page | 5 | `r zp`, `r zp`, `W zp` | | `zp,X` | 6 | `r PC+1` again, `r ea`, `r ea`, `W ea` | | absolute | 6 | `r ea`, `r ea`, `W ea` | | `abs,X`: `ASL LSR ROL ROR` | **6, 7 on a page cross** | [cross: `r PC+2` again], `r ea`, `r ea`, `W ea` (DS Table 7-1 "absolute indexed in same page: six cycles"; CAP: 6; the 7 is not captured) | | `abs,X`: `INC DEC` | **7 always** | `r ea` without a page cross or `r PC+2` again with one, then `r ea`, `r ea`, `W ea` (CL section 7; CAP: `$DE $FE` 7; C11, C14) | `TSB`/`TRB`: Z is set from `A AND M` (the test), the memory gets `M OR A` or `M AND NOT A`; N and V are untouched (DS Table 6-4; unlike the HuC6280). `RMB`/`SMB` change one bit and no flag. ### Decimal mode `ADC` and `SBC` take **one more cycle when D is set** (DS Table 7-1; CAP: `69 2/3`, `65 3/4`, `75 4/5`, `6D 4/5`, `72 5/6`, `E9 2/3`...), after the operand read. The extra cycle reads **the next opcode's address** (CAP: in every decimal `ADC` and `SBC` of Clark's test on both parts the captured byte is the opcode fetched next; SST re-reads the operand, C13). N and Z are those of the decimal result, V that of the NMOS part, C the decimal carry; for invalid BCD digits the accumulator follows BC Appendix A (65C02: `ADC` sequence 1, `SBC` sequence 4), which both captured parts computed across the whole of Clark's test. ### One-byte and stack instructions | Instruction | Cycles | Bus | | --- | --- | --- | | implied, accumulator (`INC A`, `DEC A` included) | 2 | `r PC`, `r PC+1` (dropped) | | `PHA PHP PHX PHY` | 3 | `r PC`, `r PC+1`, `W stack` | | `PLA PLP PLX PLY` | 4 | `r PC`, `r PC+1`, `r stack` (dropped, before the increment), `r stack+1` | | `JSR` | 6 | `r PC`, `r PC+1`, `r stack` (dropped), `W PCH`, `W PCL`, `r PC+2` | | `RTS` | 6 | `r PC`, `r PC+1`, `r stack`, `r PCL`, `r PCH`, `r` target-1 (dropped) | | `RTI` | 6 | `r PC`, `r PC+1`, `r stack`, `r P`, `r PCL`, `r PCH` | | `JMP abs` | 3 | `r PC`, `r PC+1`, `r PC+2` | | `JMP (abs)` | **6** | `r PC`, `r PC+1`, `r PC+2`, `r PC+2` again, `r ptr`, `r ptr+1`; the pointer's high byte comes from `ptr+1` with the carry into the page (DS Table 7-1). SST: `r ptr`, `r ptr-page:(ptr+1)-low`, `r ptr+1` (C17) | | `JMP (abs,X)` | 6 | `r PC`, `r PC+1`, `r PC+2`, `r PC+2` again, `r base+X`, `r base+X+1`. SST re-reads `PC+1` (C17) | | branch, `BRA` | 2 not taken, 3 taken, 4 across a page (`BRA`: 3 or 4) | `r PC`, `r PC+1`, [taken: `r PC+2`], [cross: `r PC+2` again]. SST: `r` old-page:new-low (C16) | | `BBR`/`BBS` | 5, 6 taken, 7 taken across a page (CAP saw 5 and 6; the 7 is SST's) | `r PC`, `r PC+1`, `r zp`, `r zp`, `r PC+2`, [taken: `r PC+3`], [cross: `r PC+3` again] | | `STZ` | as `STA` in `zp`, `zp,X`, `abs`, `abs,X` | writes `$00` | | `BIT #imm` | 2 | Z only; N and V untouched (CL section 2; GDS Table IV note 5) | ## Undefined opcodes All are NOPs: no register, flag or memory changes (DS Table 7-1). Lengths and cycles, with the row they apply to: | Opcodes | Bytes | Cycles | Bus | Evidence | | --- | --- | --- | --- | --- | | `02 22 42 62 82 C2 E2` | 2 | 2 | `r PC`, `r PC+1` | DS, GDS, VDS, NDS; CAP | | `x3`, `xB` (32; minus `CB DB` on the W65C02S) | 1 | **1** | `r PC` only | the same; CAP; LK | | `x7`, `xF` (32) on the `G65SC02` row | 1 | **1** | `r PC` only | GDS, VDS, NDS; CAP on the G65SC12; against SST (C2) | | `44` | 2 | 3 | `r PC`, `r PC+1`, `r zp` | DS; CAP; SST; LK ("load and discard") | | `54 D4 F4` | 2 | 4 | as `zp,X` read | DS; CAP | | `DC FC` | 3 | 4 | `r PC`, `r PC+1`, `r PC+2`, `r` the absolute address | count: DS, CAP; address: CL note i, LK, and CAP (the captured byte is the one at the absolute address in each of the 8 executions where the model knew it). SST re-reads `PC+2` (C10) | | `5C` | 3 | **8** | `r PC`, `r PC+1`, `r PC+2`, `r $FF00+low operand`, `r $FFFF` four times (LK, observed on a chip; CAP: the last four bytes are the byte at `$FFFF` in all 6 executions) | count: DS, GDS, VDS, NDS, CAP on both parts; SST says 4 (C1) | A one-cycle NOP has no second-to-last cycle. Jeff Laughton observed that an interrupt is never taken directly after one: "the NOP's execution apparently attaches or merges with that of the following instruction", even through a run of them (CL section 9). The core's rule: **a one-cycle NOP does not poll** (U7). ## Reset, interrupts, BRK | Sequence | Cycles | Bus | Evidence | | --- | --- | --- | --- | | reset, `R65C02` | 7 | `r PC`, `r PC`, `r stack`, `r stack-1`, `r stack-2`, `r $FFFC`, `r $FFFD`; S ends 3 lower | CAP: 7 samples from SYNC, RnW high throughout; cycles 1 and 2 return the same byte; their address is not known (U4) | | reset, `G65SC02` | 7 | the same with the three stack cycles **writes** of PCH, PCL, P (B clear) | CAP on the G65SC12 (RnW low for three samples; `$AF` pushed for P); GDS Table V. Data sheets say six cycles (C4) | | reset, `W65C02S` | 7 (DS 3.11) | reads or writes: U1 | | | IRQ, NMI | 7 | `r PC` (SYNC high), `r PC`, `W PCH`, `W PCL`, `W P` (B clear), `r vector`, `r vector+1`; then I set, D clear | CAP: 65,772 entries; the two reads return the opcode that would have run | | `BRK` | 7 | `r PC`, `r PC+1`, `W PCH`, `W PCL`, `W P` (B set), `r $FFFE`, `r $FFFF`; return address `PC+2`; I set, D clear | SST (`00`: `rrWWWrr`, D clear in `final.p`); GDS Table IV: 7; not in CAP | There is no interrupt hijack of `BRK` on the CMOS part: if an NMI or IRQ is pending while `BRK` runs, `BRK` completes with B set through `$FFFE`, and the interrupt is taken afterwards (DS Table 7-1). How soon afterwards is U5: the default is the frame's NMOS rule, that `BRK` does not poll, so one instruction of the `BRK` handler runs before the pending interrupt is taken. ## WAI and STP (`W65C02S` row) | Instruction | Cycles | Bus | Evidence | | --- | --- | --- | --- | | `WAI` | 3, then one cycle per waiting cycle | `r PC`, `r PC+1`, `r PC+1`; each waiting cycle `r PC+1` (the next opcode's address) | 3 cycles: CL, GF, MAME; "the next opcode repeated": GF | | `STP` | 3, then stopped | `r PC`, `r PC+1`, `r PC+1`; then `r PC+1` per step until reset | CL, GF | `WAI` ends on NMI, IRQ (whatever I says) or reset. With IRQ and I = 1 the next instruction runs; with I = 0, or NMI, the interrupt entry follows. If IRQ is already low when `WAI` executes it does not wait (GF). SST has no cases for either opcode ("incompatible with this style of testing"). ## Interrupt polling Default (U5): `6502-python`'s frame as it is. Requests are sampled at the end of each instruction's second-to-last cycle; `CLI`, `SEI` and `PLP` take effect on I after their own poll; `BRK`, interrupt entries and reset do not poll; a host that stretches a cycle with RDY calls `note_stall()`. Two CMOS points: - one-cycle NOPs do not poll (above); - RDY halts the CMOS part on write cycles as well (DS Table 7-1 and 3.10: "on all cycles including write cycles"; RDS; GDS Table II), so a host may stretch a write callback and call `note_stall()` there. The Lynx does exactly this when Suzy takes the bus. The NMOS part's branch quirk (a taken branch without a page cross polls a cycle early) is **not** assumed: GW notes "there seems to be a difference between NMOS and CMOS in this area", without detail (U5). What the captures add (tier 1, both parts): each of the 65,772 interrupt entries they hold began at a boundary where the core's rule allows one. In particular none began directly after `CLI`, `PLP` or a one-cycle NOP. That is agreement, not proof: the IRQ line is not sampled, so the captures cannot show an interrupt that the chip would have taken earlier than the core does. ## What the captures could not show (U4, what is left) - The addresses of reset's first two cycles (the program counter is unknown at reset; the two bytes are equal, so it is one address read twice). - `BRK` (never executed), `WAI`, `STP`, and `$CB`/`$DB` on the other rows. - The seventh cycle of `BBR`/`BBS` and the page-crossing cycle of the `abs,X` shifts (never executed across a page). - The fourth cycle of `$5C` (`$FF00 + low operand`): the model knew the byte there in 2 of 6 executions, and it agreed in both. - Anything about the W65C02S and the Lynx cells: the captured parts are a G65SC12 and an R65C02.