# w65c02-python **Lineage:** w65c02-python → w65c02-rust · fixes start in w65c02-python and flow down by merges · [family charter](https://github.com/alewman/emulator-lineage/blob/main/docs/charter.md) A readable, pure-Python core for the CMOS 65C02 family, correct to the bus cycle, in the shape of the family's other CPU cores (`6502-python`, `65816-python`, `huc6280-python`). It is built on `6502-python`'s `CPUFrame` (bus callables, interrupt requests, the poll, `step()`), with its own instruction handlers and the family's tooling by subclassing. The first consumer is the Atari Lynx board (`lynx-python`), whose Mikey chip holds a 65C02 cell. Later consumers could be Apple IIe/IIc-class machines, the BBC Master and arcade boards. ```python from w65c02_python import G65SC02 # or W65C02S, R65C02, Lynx2Cell memory = bytearray(0x10000) cpu = G65SC02(memory.__getitem__, memory.__setitem__) cpu.request_reset() cycles = cpu.step() # one instruction, entry or reset; == callbacks made ``` Every cycle is exactly one callback, a read or a write, in the chip's order, dummy reads included. The core keeps no time. Inside a callback the host can read `cpu.sync` (the opcode fetch), `cpu.fetch` (a read of the instruction stream at PC) and `cpu.opcode`; a board that holds a cycle calls `cpu.note_stall()` from that read or write callback. The modelling level (cycle: every bus access) and the whole contract a board may rely on are in `docs/start-here.md`, "Modelling level, and the contract boards rely on". ## The parts, as data rows One instruction core (`CMOS65C02`), one class per part; a row is class attributes, so a board can make its own (docs/start-here.md has the evidence for every cell): | Class | Part | Bit instructions (`RMB` `SMB` `BBR` `BBS`) | `WAI` / `STP` | Reset's three stack cycles | | --- | --- | --- | --- | --- | | `W65C02S` | WDC W65C02S | yes | yes | reads (unknown: U1) | | `R65C02` | Rockwell R65C02 | yes | no | reads (captured) | | `G65SC02` | GTE/CMD G65SC02 and G65SC12; NCR65C02; VLSI VL65NC02; **the Lynx I's cell** | no: one-byte, one-cycle NOPs (captured) | no | writes (captured) | | `Lynx2Cell` | **the Lynx II's cell**: no part number is known | yes (a hardware test ROM says so: C12) | no | writes (assumed: U2) | ## What is claimed, and on what `docs/validation.md` has every judge, its tier and the measured counts; `docs/undocumented-behavior.md` every unknown (U1 to U13) and every place where sources contradict each other (C1 to C19). - **Tier 1, real chips.** Logic-analyser captures of a G65SC12 and a Rockwell R65C02 replay through the `G65SC02` and `R65C02` rows in lockstep: 168 million instructions, 65,772 interrupt entries and two resets, with every cycle's direction, SYNC, written byte and read byte agreeing. That fixes the cycle counts, the read/write shapes and, through the data each read returns, almost every dummy-read address. - **The `W65C02S` row has no tier-1 judge of its own** beyond a data-bus-only capture of a part with bit instructions (1.5 million instructions, agreeing, unable to tell it from the R65C02). Its `WAI` and `STP` rest on WDC's data sheet and bench notes (tiers 2 and 3). **The `Lynx2Cell` row has none**: it is the `G65SC02` row plus the bit instructions of the `R65C02` row, and the Lynx board's own test ROM (rung 9, with `lynx-python`) is its judge. - **Tier 5, a detector.** SingleStepTests' three CMOS sets: all 7,660,000 cases run, none fails; 1,882,157 of them are counted under ten named contradictions, in each of which a higher tier decides against the corpus. MAME 0.285: 5.09 million instructions on four machines with no register or written byte differing. - **Not claimed.** When the real chips poll for interrupts (U5: the frame's NMOS rule, with one-cycle NOPs not polling; what real hardware evidence there is agrees), `WAI`'s wake timing (U6), anything about RDY beyond `note_stall()`, the SOB, VPB, MLB and BE pins. ## What is here - `src/w65c02_python/`: `cpu.py` (the classes and their rows), `_core.py` (registers, fetch helpers, addressing modes cycle by cycle), `_dispatch.py` (the opcode table and the rows' replacements), `_alu.py`, `_loads.py`, `_shifts.py`, `_bits.py`, `_control.py`, `_nops.py` (one mnemonic per handler, its source cited in the docstring), and the tooling: `state.py`, `disasm.py`, `debug.py`, `trace.py`, `console.py`, `harness.py`, `python -m w65c02_python`. - `docs/`: `status.md` (every rung and count at a named commit; start here), `start-here.md` (the part, the rows), `timing.md` (every cycle as a bus access), `undocumented-behavior.md`, `validation.md` (oracles, rungs, what was measured), `mame-oracle.md`, `handoff-brief.md` (the bus contract and what the Lynx board can pin), `build-progress.md` (state and traps for whoever continues). - `tests/`: one file per rung, and the judges' harnesses (`single_step.py`, `capture_replay.py`, `mame_replay.py`, `programs.py`, `datasheet.py`). - `scripts/`: `fetch_references.py` and `build_tools.py` (every outside source by URL + SHA-256 or repository + commit, into the gitignored `reference/`, about 2 GB), and the whole-corpus runners: `run_single_step.py`, `run_programs.py`, `run_capture.py`, `run_mame_replay.py`, `seeded_faults.py`. ## Working on it python3 -m venv .venv && .venv/bin/pip install -e ".[dev]" pypy3.11 -m venv .venv-pypy && .venv-pypy/bin/pip install -e ".[dev]" .venv/bin/python -m pytest -m "not slow" # the quick loop .venv/bin/ruff check . && .venv/bin/ruff format --check . CPython and PyPy, Python 3.11 and later. Run anything that reads `reference/` with `python -I`. Tests that need a corpus, a capture or a trace skip with a hint when it is not there. Nothing in the repository names a path on a particular machine. Outside data is found like this: | What | Where | Used by | | --- | --- | --- | | SingleStepTests, the captures, Dormann's and Clark's programs, MAME's and the emulators' sources | `reference/` inside the repository (gitignored), filled by `scripts/fetch_references.py` and `scripts/build_tools.py` | rungs 0 to 5, 8 | | MAME traces | `tests/mame_traces/` (gitignored), recorded by `scripts/mame_trace.py` | rung 5 | | A merged MAME ROM set | `MAME_ROMPATH` (or `--rompath`); the script stops when neither is set | `scripts/mame_trace.py` | | The MAME binary | `--mame`, else `MAME`, else `mame` on PATH | `scripts/mame_trace.py` | | A Lynx cart for the `lynx` trace | its own path, passed as `--cart` (for example under `NO_INTRO_DIR`) | `scripts/mame_trace.py lynx` | | cc65 2.19 (`ca65`, `ld65`, `da65`) | on PATH, or the directory `CC65_BIN` names; `build_tools.py` stops, the `da65` test skips, when neither has it | `scripts/build_tools.py`, `tests/test_tooling.py` | ## Licence MIT (see `LICENSE`). Nothing fetched into `reference/` is committed; GPL programs there are run as external programs and never copied from.