Build a Python 3.12+ pure-Python NMOS 6502 instruction-core reference implementation, named "6502-python-r2", modeled directly on the sibling "z80-python" project's design philosophy: readable, dependency-free, oracle-validated, and embeddable rather than a complete machine. ## Revision note (round 2, 2026-08-26) This is a fresh second attempt after round 1 produced a working core but exposed operational friction. It retains round 1's technical specification and adds verified postmortem facts; CPU correctness remains this attempt's work to implement and prove. Make all 151 documented-opcode vector files an explicit intermediate milestone before implementing the 105 undocumented values. This supports a matched autonomy comparison but is not permission to stop or retain expected failures: completion requires all 256 files to pass. Work independently in the new repository; do not read, copy, or depend on round 1's `6502-python` code. ## Scope (v1 -- all 256 NMOS opcode bytes) - Implement all 151 officially documented NMOS 6502 opcodes and addressing modes: accumulator, implied, immediate, zero page, zero page,X / zero page,Y, absolute, absolute,X / absolute,Y, indirect, (indirect,X), (indirect),Y, relative. - Also implement the behavior represented by all 105 undocumented opcode byte values in the pinned SingleStepTests NMOS 6502 corpus. This includes the stable combined-operation families (SLO, RLA, SRE, RRA, SAX, LAX, DCP, and ISC/ISB), undocumented NOP forms, immediate and unstable families, the high-byte-masked store family, LAS, and all 12 JAM/KIL byte values. Preserve aliases as distinct opcode definitions even where their operation is shared. - The pinned SingleStepTests revision is the acceptance authority for the undocumented instructions, especially unstable instructions whose results can vary with chip revision or analog effects. Document that qualification; do not overclaim that this software model predicts every physical NMOS 6502. - JAM/KIL is a supported terminal CPU condition, not an unsupported-opcode exception. Match the oracle's one-instruction outcome (one-byte PC advance, 11 cycles, registers and status unchanged), then retain an explicit jammed state so a later `step()` cannot fetch another instruction until RESET. RESET clears the jammed state through the normal reset lifecycle. - Implement decimal (BCD) mode faithfully, INCLUDING the real NMOS 6502's well-documented quirk that N, V, and Z flags after a decimal-mode ADC/SBC do not reliably reflect the decimal result (they are computed from the intermediate binary result). Do not "fix" this to be more sensible -- match real silicon behavior, which the oracle below encodes. - Implement the RESET, IRQ, NMI, and BRK lifecycle at instruction boundaries: 7-cycle vector fetch sequence, the B flag's behavior on the byte pushed to the stack (set for BRK/PHP, clear for hardware IRQ/NMI, unaffected when read back via PLP/RTI), and the accepted IRQ-during-NMI / NMI-hijacking-BRK edge cases. - Do NOT implement a memory map, cartridge/device emulation, or any specific host machine (NES, Apple II, C64, Atari, BBC Micro, etc.). The host supplies memory read/write and IRQ/NMI line state; this project is the instruction core only, exactly like z80-python leaves memory/devices to its host. ## Packaging conventions (match z80-python and this repo's house style) - hatchling build backend in pyproject.toml - flat top-level package layout, e.g. `sixfiveohtwo/` or similar valid Python identifier (not `6502_python`, which is not a legal module name -- pick a sensible importable name and document the mapping in the README) - pytest for tests, ruff for lint/format - a README describing the embeddable host contract (the memory/IO methods a host must supply, and how the core signals IRQ/NMI/RESET acceptance) - no runtime dependencies ## Validation plan -- mirror z80-python's oracle-driven approach exactly Every project task must read and write only relative paths inside this checkout. Never use `../`, absolute sibling paths, or assumed external directories. Fetch external artifacts beneath `tests/`, gitignore them, and never commit them, matching z80-python's in-repository validation layout. Use these already-verified immutable inputs; do not spend task turns rediscovering them: - `SingleStepTests/65x02` commit `2f6980a2d95757486c7bee24355c360e40e2a224`. - `Klaus2m5/6502_65C02_functional_tests` commit `7954e2dbb49c469ea286070bf46cdd71aeb29e4b`; functional binary SHA-256 `fa12bfc761e6f9057e4cc01a665a7b800ff01ae91f598af1e39a1201d01953f`. - `amb5l/6502_65C02_functional_tests` commit `966b1a35049f9d8be44ad092ec6d43d5ba1831b3`; round-1 decimal binary SHA-256 `b179ca4c5a305de2d0cde9ccaa04861be965e2a85b9d3d1230dcc47a396ca43` (warn rather than fail if a different cc65 version produces a new hash). - This Windows host has `ca65` and `ld65` in WSL Ubuntu-24.04 at `/usr/bin`. If they are absent from native PATH, invoke them through `wsl -d Ubuntu-24.04 -- ca65` and `wsl -d Ubuntu-24.04 -- ld65`. 1. **Single-step vector oracle**: the `SingleStepTests/65x02` GitHub repository (MIT) contains `6502/` per-opcode JSON vectors with complete CPU and memory state before and after one instruction. Write a `scripts/` fetcher that clones a pinned revision into the gitignored project-relative `tests/6502_test_vectors/`; never vendor the corpus. **Corpus scale (round 1 finding -- design around this from the start):** there are 256 opcode files (one per possible opcode byte, official and unofficial alike), 10,000 vector records each, ~2.56 million records total, ~1GB uncompressed. Write the pytest suite parametrized ONE TEST PER OPCODE FILE (256 pytest IDs), streaming its 10,000 records inside one test, as z80-python does. Never parametrize 2.56 million individual records: round 1 consumed 20+ GB and ran for hours before being killed. Per-file streaming collects in under ~10 seconds. All 256 files must pass when present: no undocumented xfail/skip, first-record shortcut, or `UnsupportedOpcodeError` for any byte. Clone with `git clone --filter=blob:none --sparse` and `sparse-checkout set 6502` to fetch only the needed subdirectory -- a full non-sparse clone of the upstream repo includes several unrelated CPU-variant subdirectories (nes6502, rockwell65c02, synertek65c02, wdc65c02) and can exhaust disk space on a constrained drive. 2. **Final comprehensive exerciser**: Klaus Dormann's `6502_65C02_functional_tests` GitHub repository (https://github.com/Klaus2m5/6502_65C02_functional_tests, GPL-3.0, external, not bundled) provides prebuilt `bin_files/6502_functional_test.bin`. Fetch it from an immutable commit into gitignored `tests/dormann/bin_files/`, verify SHA-256, and add an opt-in `@pytest.mark.integration` test that runs it from its documented load address to success trap with a finite instruction budget. **Decimal exerciser (round 1 finding -- do not skip this, it is buildable):** Bruce Clark's `6502_decimal_test.a65` has no prebuilt upstream binary; do not research that again. Klaus2m5 names the CA65-compatible https://github.com/amb5l/6502_65C02_functional_tests fork, whose `ca65/` directory contains `6502_decimal_test.ca65` and `example.cfg`. Build a pinned revision with `ca65`/`ld65` (`apt install cc65` on Linux/WSL, or the official Windows snapshot). If unavailable, skip provisioning with an actionable message rather than dropping the exerciser. Keep all fetch and build scratch paths strictly beneath `tests/dormann/`, including any `TemporaryDirectory(dir=...)`; round 1 initially violated this boundary. 3. Record exact pinned revisions/hashes for both external oracles in a `docs/validation.md` file, the same way z80-python's own `docs/validation.md` records its pinned SingleStepTests revision and ZEX binary SHA-256 hashes. State plainly what is and is not claimed (e.g. not a cycle-accurate bus-pin claim, not a complete machine), matching z80-python's own validation-scope framing. ## Full test suite runtime (round 1 finding plus all-opcode adjustment) Once the SingleStepTests corpus is fetched, the full pytest suite takes roughly 230-250 seconds in round 1, where the 105 undocumented files failed or xfailed on their first record rather than running all 10,000 records. This round must execute about 1.05 million additional vectors, so budget roughly 390-450 seconds and treat that as an estimate, not a guarantee. Any task that runs the full suite (including read-only audit/verification tasks) should request a verification/command timeout of at least 600 seconds, or scope its run to specific files/markers if a faster check suffices. ## Explicitly out of scope for this plan - Any specific host machine, PPU/APU, cartridge/mapper, or video output. - 65C02 or 65816 extended instruction support (a future project could add these; this plan targets the original NMOS 6502 only).