# Handoff: build `tms34010-python`, a pure-Python TMS34010 core ## Context you are inheriting `/data/emu/tms34010-python` holds no core code. It holds the groundwork: documents whose every fact names its source and the source's licence, a fetch script that pulls the only machine-readable test corpus that exists at a pinned commit with per-file hash verification, and a working, re-verified recipe for per-instruction MAME traces of a TMS34010. The author's existing cores set the shape you are to match: - `/data/emu/z80-python` (github alewman/z80-python): the identity is "the code is the spec" — readable, oracle-verified, embeddable. Read its `docs/start-here.md`, `docs/validation.md`, `docs/conformance.md`, `src/z80_python/_core.py` and `_dispatch.py` for the dispatch shape. - `/data/emu/6502-python`: the same discipline, smaller. `README.md` for the host contract (`read_byte`/`write_byte` on a host-owned memory object, `CPU.step()`, interrupt lines as levels and edges). - `/data/emu/i8086-python`, `m68000-python`, `m6809-python`, `i8080-python`: the same document set as this one, built the same day. Read these, in this order, before writing any code: 1. `docs/start-here.md` — the bit-addressed memory model, fields, the register file, XY addressing, the instruction set, the I/O registers, interrupts, reset, and **the core/host split with its reasoning**. 2. `docs/mame-oracle.md` — the bit-address verification and a real trace. Run `scripts/mame/trace_mk.sh` yourself before writing a line; the six instructions it prints at reset teach more than a page of prose. 3. `docs/validation.md` — what can and cannot be proved, and by what. 4. `docs/timing.md` and `docs/undocumented-behavior.md`. 5. `python3 scripts/fetch_test_vectors.py --summary`, then open `tests/tms34010_sv_vectors/mame_graphics_vectors.txt` and read one case, so the format is real to you before you write a runner. ## Read this before anything else **There is no hardware oracle for this processor.** No captured corpus, no bench trace, no TI conformance suite; this was searched for and the negative is documented. Everything you can check against is either TI's manual, another emulator, or a board diagnostic that says pass or fail. That changes how you work, in two ways: - **You cannot buy correctness with a corpus run.** On z80-python a green SingleStepTests run settled the question. Here the equivalent artifact (137 graphics cases) is *MAME's opinion in a file*, and the general instruction set has nothing at all. Correctness comes from reading section 12 of the User's Guide carefully, instruction by instruction, and writing a test per instruction that cites the page. - **Every divergence is a question, not a bug report.** When you differ from MAME, the manual decides. MAME's TMS34010 core has no unit tests, was never validated against a suite, does not implement plane masking at all, and had a graphics correctness fix committed in February 2026. It is a detector. ## Your task Build a readable, dependency-free Python 3.12+ TMS34010 instruction core in this repository, in the shape of z80-python, and certify it against the oracles in tier order. The goal at each rung is a stated, reproducible number, not "it runs Mortal Kombat". The host contract, fixed now so hosts can be written in parallel (`docs/start-here.md` argues for this split; do not renegotiate it without reading that argument): - The host supplies `read_word(word_index)` and `write_word(word_index, value)` over its own memory. **The core does all bit arithmetic.** A host never sees a bit address. `word_index = bit_address >> 4`. - The host supplies `read_io(reg)` / `write_io(reg)` for the I/O registers it owns (the video timing generator, DPYADR, REFCNT, the host port). The core owns CONTROL, PSIZE, PMASK, CONVSP, CONVDP, INTENB, INTPEND and the HLT/NMI bits of HSTCTLH, and does not consult the host about them. - Interrupt requests go through a lifecycle API — `set_int1(level)`, `set_int2(level)` as levels, `set_display_interrupt()`, `set_host_interrupt()`, `set_nmi()` — never by writing PC or INTPEND. - The host may install a shift-register transfer callback; the core invokes it when DPYCTL bit 11 is set and a pixel access occurs. - `step()` executes one instruction or one accepted interrupt and returns a machine-state count. Registers, SP, PC and ST are readable and writable as attributes; a `CPUState` snapshot type lists every processor-owned field and nothing else. ### Milestones, each with its acceptance test, in oracle-tier order **Start with the boring half. The difficulty is not where you think it is.** The field machinery and the ordinary instruction set are where a core lives or dies; a real attract-mode trace of Mortal Kombat is 99.995 % non-graphics instructions. 1. **The field machinery and the register file** (tier: documentation). `CPUState`; the two banks with SP shared; ST with the correct bit positions; `SETF`/`GETST`/`PUTST`/`PUSHST`/`POPST`; field read and write for **every width 1-32, signed and unsigned, at every bit alignment**, including the rule that **a field size of 32 is encoded as 0**. Acceptance: an exhaustive test over all 32 widths × 16 alignments × both extensions, in both directions, against a straightforward bit-array reference implementation written independently. This is the foundation everything else stands on; make it exhaustive, not representative. 2. **The general instruction set from the manual** (tier: documentation). Moves, arithmetic, logic, shifts, jumps, calls, `MMTM`/`MMFM`, `DSJ`/`DSJS`, the sixteen conditions, `ADDXY`/`SUBXY`/`CMPXY`, `CVXYL`, the interrupt sequence, reset. A 4096-entry dispatch table on `op >> 4`, as MAME does. Acceptance: a test per instruction, each naming the *User's Guide* section 12 page it encodes; and every rule in `docs/start-here.md` §8 "Interrupts" has a test. Include the illegal-opcode split from `docs/undocumented-behavior.md`: some reserved encodings trap to TRAP 30 and some do not, and two shipped games depend on the latter. 3. **MAME lockstep on `mk`, non-graphics only** (tier: emulator-derived, **a detector**). A T-unit host with `midtunit`'s memory map, stepping against `scripts/mame/trace_mk.sh` output, comparing the 33 register fields per instruction. Acceptance: **N instructions with no undecided divergence, N stated**, every divergence decided by the User's Guide and recorded in `docs/validation.md`. Do not compare cycle counts (`docs/timing.md` says why). Choose a trace window with no `RETI` in it until the host's scanline model is trustworthy. 4. **End-to-end: the board diagnostics** (tier: hardware-corrected). `yunittst` with the `RAM Test` and `ROM Checksums` DIPs asserted must reach `-- DONE --` with every chip green; `smashtv` must reach **`CPU BOARD OK`**. Both were verified reachable under MAME here, so a failure is yours. This is the rung that proves the core survives megabytes of real code rather than a test harness. Acceptance: a screenshot and the instruction count it took. 5. **The graphics instructions** (tier: documentation, then emulator-derived). `PIXT`, `PIXBLT` in all six forms including the binary-source form, `FILL`, `LINE`, `DRAV`; the sixteen pixel operations; transparency; all four window-checking modes; **plane masking, which MAME does not implement at all**; XY↔linear conversion with CONVSP/CONVDP. Write it as one generic loop with the pixel operation as a callable, not as 80 generated specialisations — readability over speed, the same trade z80-python made. Acceptance: **137/137 cases of `tests/tms34010_sv_vectors/`**, with the five classes in `mame_graphics_divergences.json` as the expected-failure list; a case that fails for any other reason is a bug. 6. **End-to-end for the graphics instructions** (tier: emulator-derived). MAME lockstep on **`xtheball`**, which executes 2,739 graphics instructions in two frames where Mortal Kombat executes two. Acceptance: N instructions clean, N stated. 7. **Cycle totals** (tier: documentation). `step()` returns *User's Guide* section 13 figures for the non-graphics instruction set. Unit tests pin a representative row per family. The corpus cannot judge this and MAME's counts are partly invented; say so in the README. Graphics-instruction timing stays out of scope, and `docs/timing.md` gives TI's own reason. Do not skip a rung or reorder them. Rung 1's failure is invisible until rung 4 and agonising to find there. **Explicitly later, or never**: a TMS34020 mode (MAME implements 39 of its 53 new opcodes as do-nothing stubs, so there is no oracle for it); the TMS34082 coprocessor; the instruction cache (TI's appendix C says software must not depend on it); DRAM refresh; cycle-exact bus timing. ## Constraints - **Never commit a ROM, a corpus, a trace, a disk image or a TI PDF.** `tests/`, `traces/`, `*.log`, `*.zip`, `*.pdf` are ignored; keep them so. ROM sets are read in place with `-rompath`. The TI workload cases in the corpus reference do-not-redistribute media — do not go looking for it. - **Do not modify the corpus.** If you believe a vector is wrong, say so with the case number and the User's Guide section that contradicts it, and add it to the expected-failure list with that citation. - **Tier discipline.** State the tier of every oracle in every claim. **Never write "verified against hardware" in this repository**, because nothing here is. "Agrees with MAME" means exactly that and no more. - **Attribution.** MAME's TMS34010 files are BSD-3-Clause, copyright Alex Pasadyn and Zsolt Vasvari (Aaron Giles on `34010gfx.hxx`). The licence permits transcribing behaviour with attribution; any file here that derives from MAME must carry the copyright notice and the three conditions. Cite the tag `mame0285` — those files moved from `src/mame/midway/` to `src/mame/williams/` afterwards. - **Shape.** Mirror z80-python: a small public facade, instruction families as modules, an explicit 4096-entry dispatch table, each handler's docstring beginning with its TI mnemonic, hardware reasons as comments on the lines that encode them, a readability test that enforces the docstring rule. No dependencies. CPython 3.12+ and PyPy. - **Commits** in reviewable units with messages that say what the change rests on. Author `alewman `. Push and open PRs only when the user asks; the repository is local-only until then. - Tools on this machine: Python 3.14.4 at `/usr/bin/python3` (venv for anything extra), `git`, MAME 0.285 at `/usr/games/mame`, the ROM collection at `$MAME_ROMPATH`. No sudo. ## How big is this Honestly: **two to three times z80-python**, which is 4,884 lines of `src/`. The estimate, by part: | Part | Rough size | Why | | --- | --- | --- | | Field access (1-32 bits, signed/unsigned, any alignment) | 300-600 lines | Small if written generically, and it should be. MAME spends 675 lines generating 95 specialised functions; do not. | | Register file, ST, dispatch table | 400 lines | The 4096-entry table is generated, not typed. | | General instruction set (~150 instructions) | 3,000-4,000 lines | Comparable to z80-python's whole core. More instructions than a Z80, but far more regular. | | Interrupts, reset, the I/O registers the core owns | 300 lines | | | Graphics instructions | 1,500-2,500 lines | The pixel operations are 22 small functions; the four transfer shapes and the window/transparency/plane-mask logic are the bulk. Written generically, not as 80 specialisations. | | **Total `src/`** | **~6,000-8,000 lines** | | | Tests and runners | 3,000-5,000 lines | Rung 1's exhaustive field test is mechanical; the lockstep runner and the corpus runner are each a few hundred. | Two honest caveats on that number. It is **larger than any other core in this set** and it will be **slower**: a pure-Python `PIXBLT` over a 320×240 region is a hundred thousand Python-level pixel operations, and nothing will make that fast. Decide early that you are building a reference implementation, say so in the README, and do not contort the code chasing a speed you cannot reach in this language. And the real difficulty is not the size. It is that **you will not know when you are right.** Budget for reading the manual, not for running tests. ## What done looks like A README that states, with numbers: the length of the clean `mk` lockstep and the MAME version; `137/137` on the pinned corpus with the five documented divergence classes named as expected failures; a screenshot of `smashtv` at `CPU BOARD OK` and of `yunittst` at `-- DONE --`, with the instruction counts they took; the length of the clean `xtheball` lockstep; which User's Guide sections the cycle totals come from; and a plain paragraph saying that **no hardware oracle for this processor exists**, so the strongest available claim is agreement with the manual, with MAME except where MAME is documented wrong, and with two shipped board diagnostics. A CI job that runs the unit tests on every push; the lockstep and corpus gates run locally and are recorded in `docs/validation.md`. Before starting, tell the user in a few sentences how you read this brief and what you will do first.