# Handoff: build `m6809-python`, a pure-Python 6809 core, from these documents ## Context you are inheriting `/data/emu/m6809-python` holds **documents, oracle research, fetch and trace scripts, and no core code**. It was prepared on 2026-09-11/12 in the shape of the user's two existing cores, which are the models for everything you write: - `z80-python` (`/data/emu/z80-python`, public): readable pure-Python Z80, host-owned memory, `step()` returns cycles, certified against SingleStepTests, ZEX, raxoft/z80test. Read its `docs/start-here.md`, `docs/validation.md`, `docs/conformance.md` and `src/z80_python/` for the code shape (instruction families as ordinary Python modules, explicit dispatch, one mnemonic per docstring, hardware reasons as comments on the line that encodes them). - `6502-python` (`/data/emu/6502-python`): the same contract in a smaller core; `README.md` and `docs/validation.md`. The user works by an oracle-tier rule: rank oracles by where their expected values came from, hardware-captured > hardware-corrected > emulator-derived; a low-tier oracle is a detector, never a judge; state the tier of every oracle. The 6809 has **no hardware-captured corpus for its documented instruction set**, so the judge for documented behaviour is the datasheet and the programming manual, and MAME, XRoar and the `neetandev/m6809` vectors are detectors. Hardware captures exist for the undocumented corners (hoglet67, Flamand) and those are judges for what they cover. Do not soften this in any README you write. Read, in this order, before writing code: 1. `docs/start-here.md`: registers, CC, the opcode-map regularity, the indexed postbyte with its cycle costs, prefixes, the full instruction table, stack masks, TFR/EXG, interrupts, reset. 2. `docs/undocumented-behavior.md`: every illegal opcode and undefined flag with its source and tier, and the places where MAME simplifies hardware. 3. `docs/timing.md`: cycle rules and the Williams host contract (1 MHz 6809E, 64 cycles per scanline, VA11 IRQ four times a frame, blitter cycle theft). 4. `docs/validation.md`: the oracle inventory and the six-step plan. 5. `docs/mame-oracle.md`: how to get MAME 0.285 traces, verified. ## Your task Write the core, in this repository, to the z80-python/6502-python embedding contract: a `CPU` class (or subclassable base) that takes host `read_byte` / `write_byte`, exposes A B X Y U S PC DP CC as plain attributes, samples RESET/NMI/FIRQ/IRQ at instruction boundaries as [timing](timing.md) specifies, and whose `step()` executes one instruction or one interrupt-entry sequence and returns its E-cycle count. Dependency-free, Python 3.12+, CPython and PyPy. Then pass the validation plan in order. ### Milestones, each with its acceptance test 1. **Skeleton and the datasheet table.** Registers, CC, addressing modes including every postbyte, the page-1/2/3 decoder, and every documented instruction, each handler's docstring starting with its Motorola mnemonic. Per-opcode unit tests transcribed from the table in `start-here.md` (cycles, bytes, flags), including `PSHS`/`PULS` order, `RTI` with both E values, and the interrupt entries (19/10/19 cycles, stacked frame, I/F effects). Acceptance: the unit suite passes and a readability test enforces "one mnemonic per handler docstring" as z80-python's does. 2. **neetandev/m6809 vector gate.** `python scripts/fetch_m6809_vectors.py` then a MOO reader (`testgen/moo_writer.cpp` in the fetched corpus is the format reference; the README gives the register order and the cycle encoding) and a runner comparing registers, RAM, cycle count and bus order across all 315 files. Acceptance: **249,871 of 249,871 agree, or every disagreement is listed with the higher-tier source the core follows instead** (expected: the unprefixed store-immediate flags `87/C7/8F/CF`, XADDU's `| $FF00`, possibly mixed-width TFR/EXG; see the undocumented page). Do not "fix" the core to match MAME where hoglet67 disagrees. 3. **Undocumented set from hardware sources.** Focused tests for each rule on `docs/undocumented-behavior.md` marked hoglet67/Flamand. Acceptance: all pass; every `[unverified]` on that page either stays marked or gains a source. 4. **MAME boot-segment replay.** Use `scripts/mame_trace.sh robotron 20` (about 25 s wall, 5.5 M lines) and a replay harness that loads the Robotron ROMs in place from the zip, runs the same instruction count, and compares `pc a b x y u s dp cc` and the `totalcycles` deltas per line, stopping at the first I/O read it cannot reproduce. Then add the Williams host skeleton from `timing.md` (scanline counter, PIA1 CB1 edge on VA11, IRQ level) and get through the Advance-button wait into attract mode. Acceptance: the 552,507 pre-I/O instructions agree exactly; after the host is in, the 2,069 IRQ entries land on the same scanlines with the same frame, and the first divergence, if any, is explained. 5. **Independent-emulator cross-check.** Build XRoar 1.12 (GPL-3.0; `src/mc6809/`) or vecx and run the step-2 cases through it; three-way diff. Acceptance: every three-way disagreement is resolved by the datasheet or a hardware source and recorded in `docs/validation.md`. 6. **Schwotzer's `6809test.asm`** only if its licence is settled (ask the user before fetching it from hoglet67's unlicensed repository). 7. **Optional, only after the 6809 ladder passes: an HD6309 mode.** The 6309 is a superset of this part and belongs in this repository as a mode, not in a separate project; [hd6309](hd6309.md) records its registers, its 168 extra opcodes with cycles in both modes, its traps, the 6809 undocumented opcodes it changes, the twelve places where the sources disagree, and the fact that no 6309 oracle above emulator-derived exists, so it can be built but not certified. Do not skip a rung or reorder them; each one's failure is cheapest to diagnose before the next. ## Constraints - **Do not modify the oracles.** If the `neetandev` corpus, MAME or XRoar looks wrong, record the case and the higher-tier source; do not patch the corpus locally. - **Never commit** a ROM, a MAME trace, the vector corpus, or any third-party program. `.gitignore` already excludes `tests/vectors/`, `tests/programs/`, `*.trace`, `error.log`, `mame-home/`. ROMs are read in place from `$MAME_ROMPATH`. - **Do not touch other directories under `/data/emu`.** Do not push to GitHub unless the user asks; the repository has no remote. - Mirror z80-python's module layout in spirit (`_alu`, `_loads`, `_branches`, `_stack`, `_index` for the postbyte, `_interrupts`, `_dispatch`), keep the explicit if-chain dispatch that PyPy compiles well, and put every hardware reason on the line that encodes it with its source and tier. - Same discipline the user's other projects use: every claim pinned to a commit hash and reproduced; state exactly what was and was not run; "the whole ladder" means every rung. Report failures with their output. - Commit in reviewable units with `git -c user.name=alewman -c user.email=alewman@gmail.com commit`, messages saying what the change rests on, ending with the trailer lines `Co-Authored-By: Claude Fable 5.1 ` and `Claude-Session: `. - Tools present: Python 3.14.4 at `/usr/bin/python3` (target 3.12+ anyway), git, `gh` (authenticated), MAME 0.285 at `/usr/games/mame`. No sudo. PyPy is installed for z80-python at `/data/emu/z80-python/.venv-pypy/bin/pypy3` if a fast run is needed. ## Known open questions to carry, not to resolve silently - `SYNC`: 4 cycles (datasheet) or 3 plus the wait (MAME). - `10 20` XLBRA absolute cycle count (hoglet67 "5 not 4" versus the datasheet's 5-cycle `LBRA`). - Mixed-width `TFR`/`EXG` rules: MAME's duplication of CC/DP versus `neetandev`'s FM-77AV observation versus hoglet67/Flamand; read all three before choosing, and record which was chosen and why. - Undefined indexed postbytes: MAME's EA=0 is a guess. - Whether `EXG` with S counts as "loading S" for the NMI-after-reset rule. - Reset's cycle cost. ## What done looks like A `README.md` that states, with the pinned numbers `docs/validation.md` requires (`neetandev/m6809` revision, MAME version, XRoar version, the two corpus SHA-256 values), which rungs of the ladder pass, how many cases agree, and the list of explained disagreements with their higher-tier sources; a `docs/validation.md` updated from "plan" to "record"; and the oracle-tier sentence in the README kept intact: agreement with emulators is not verification against silicon. Before starting, tell the user in a few sentences how you read this brief and what you will do first.