Metadata-Version: 2.5 Name: 65816-python Version: 0.1.0.dev0 Summary: Readable, pure-Python WDC 65C816 core, cycle by cycle, for the SNES and other boards Author: alewman License: MIT License Copyright (c) 2026 alewman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. License-File: LICENSE Classifier: Development Status :: 2 - Pre-Alpha Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Topic :: System :: Emulators Requires-Python: >=3.11 Requires-Dist: 6502-python @ git+https://github.com/alewman/6502-python@741419a33d1de400a63f7c24c7916bb600694ca2 Provides-Extra: dev Requires-Dist: pytest>=8; extra == 'dev' Requires-Dist: ruff==0.16.8; extra == 'dev' Description-Content-Type: text/markdown # 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` (10 repositories at full commit SHAs, sparse where large): SingleStepTests 65816 and the emulator that generated it, bsnes, ares, MAME 0.285's source, Mesen2, srg320's P65C816 VHDL with logic-analyser captures, and krom's, gilyon's and undisbeliever's test ROMs. ```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. The fetched material keeps its own terms, recorded per entry in the pin lists.