# 65816-python A readable, pure-Python **WDC 65C816** CPU core: the 8/16-bit processor inside the SNES/Super Famicom's Ricoh **5A22** (S-CPU), the Apple IIGS, and WDC's W65C816S. This repository is only the CPU. The 5A22's on-chip extras (DMA and HDMA, the multiplier and divider, the H/V timers, joypad auto-read, the per-address memory speed) and everything else on the console belong to the board, [snes-python](../snes-python). **Status (2026-10-23): the core is built and passes rungs 0-5** of [docs/validation.md](docs/validation.md): every cycle is the datasheet's (Table 5-7), all 5,080,136 SingleStepTests cases agree on every cycle (264 excluded as decided), gilyon's cputest and all 23 of krom's hardware-verified CPU tests pass, and seven commercial games replay through it against MAME 0.285 for 55.6 million instructions with no register divergence; srg320's P65C816 VHDL, run on 508,400 corpus states, never sides with the corpus against it. Local only for now; rung 6 (inside [snes-python](../snes-python)) is next. It builds on [6502-python](https://github.com/alewman/6502-python)'s frame (the bus callables, the interrupt requests and their poll, `step()`'s skeleton) and its tooling (debug session, traces, console), subclassed through their documented hooks; a pinned dependency, never copied files. Like 6502-python it makes **one bus callback per CPU cycle, in chip order**, and unlike it, internal cycles are callbacks too, each with its address and VDA/VPA/VPB/MLB kind, because the 5A22 times every cycle by what it is (6, 8 or 12 master clocks) and the board must decide that length cycle by cycle. ## Using it ```python from w65c816 import W65C816 memory = bytearray(1 << 24) cpu = W65C816(memory.__getitem__, memory.__setitem__, idle=lambda address: None) cpu.request_reset() cycles = cpu.step() # one callback per cycle; inside one, cpu.vda/vpa/vpb/mlb state = cpu.capture_state() # w65c816.state.CPUState; cpu.restore_state(state) ``` | Module | What | | --- | --- | | `w65c816.disasm` | M/X-aware disassembly in WDC syntax, built from the core's opcode map; agrees with MAME's on every ROM instruction of the rung 4 games | | `w65c816.debug` | `W65C816Session`: 24-bit breakpoints, watchpoints, bounded runs, history; tracked accesses carry each cycle's pins (`"rdp"`, `"wd"`, `"i"`) | | `w65c816.trace` | JSON Lines boundary traces (6502-python's schema with this core's state and kinds), read back equal, compared lazily | | `w65c816.console` | `W65C816Debugger`, the family's command debugger | | `w65c816.harness` | the minimal SNES-shaped host the CPU test ROMs and rung 4 run in (not a SNES) | ```sh python -m w65c816 CPUADC.sfc --screen 0x7C00 # run a test ROM, print its text python -m w65c816 cputest-full.sfc --trace full.jsonl # every boundary, accesses included python -m w65c816 game.zip --debug -c "break \$008000" -c continue ``` ## What is fetched Nothing third-party is committed. `scripts/fetch_references.py` rebuilds `reference/` from `scripts/pinned_files.json` (15 files pinned by SHA-256: WDC's datasheets, fullsnes, five SNESdev wiki revisions, three 6502.org tutorials, gilyon's and undisbeliever's release ROMs, a GHDL tool bundle) and `scripts/pinned_repos.json` (8 repositories at full commit SHAs, sparse where large): SingleStepTests 65816 and the emulator that generated it, ares, MAME 0.285's source, srg320's P65C816 VHDL with logic-analyser captures (built for rung 5, never opened), and krom's, gilyon's and undisbeliever's test ROMs. bsnes and Mesen2 are closed boxes whose source is not fetched (Aubrey, 2026-11-24; docs/provenance.md). ```sh python -I scripts/fetch_references.py # everything missing (~3.5 GB) python -I scripts/fetch_references.py --skip 65816 # without the 2.8 GB corpus python -I scripts/fetch_references.py --no-tools # without the 750 MB GHDL bundle python -I scripts/fetch_references.py --verify # check what is present python -I scripts/build_referees.py # GHDL analysis of P65C816, MAME IPL python -I scripts/datasheet_timing.py # WDC Table 5-7 vs SingleStepTests ``` They need `git`, `curl`, and `7z` for games kept as `.7z`. No sudo. ## Scripts | Script | What | | --- | --- | | `scripts/fetch_references.py` | the pinned references, test ROMs and tool bundle | | `scripts/build_referees.py` | unpacks GHDL, analyses srg320's P65C816 VHDL and builds the rung 5 testbench; writes the substitute S-SMP boot ROM for MAME | | `scripts/datasheet_timing.py` | WDC's Table 5-7 transcribed literally as per-cycle schedules, compared with every SingleStepTests case ([docs/timing.md](docs/timing.md)) | | `scripts/mame_trace.py` | MAME 0.285 per-instruction 5A22 trace of a SNES ROM, with a free substitute S-SMP boot ROM; `--io` logs I/O reads for the replay ([docs/mame-oracle.md](docs/mame-oracle.md)) | | `scripts/mame_replay.py` | rung 4: replays such a trace through the core and reports every difference | | `scripts/run_single_step.py` | rung 1: every SingleStepTests 65816 case, in parallel processes | | `scripts/run_p65c816.py` | rung 5: SingleStepTests states through srg320's P65C816 under GHDL, each difference put to a vote with the core and the corpus | ## Licence MIT (see `LICENSE`), for what is in this repository, except one function of the rung 4 test tool: `tests/mame_replay.py` is `MIT AND BSD-3-Clause` (MAME's `bus_5A22_cycle_burst`, notice in `NOTICE.md`). The fetched material keeps its own terms, recorded per entry in the pin lists.