# Handoff: build `tms9900-python`, the TI-99/4A's CPU core ## Context you are inheriting The family's shape: chips are cores, boards compose cores, CPUs always get their own core (charter). This core's first user is `ti99-python` (`../ti99-python`, Phase 0 done in parallel today; do not edit it from here). A board that finds a corner of this core raises it **on the core**. Era 1-2 (charter): one 16-bit CPU at 3 MHz, modelled at **cycle level**: every machine cycle (ALU, memory, CRU) in TI's documented order, with the board's wait states on memory cycles. The evidence supports this level directly: TI published the machine-cycle sequence of every instruction (docs/timing-and-bus.md). Models to read before writing code: - `z80-python` (`../z80-python`): the readability contract (one instruction family per module, one mnemonic per handler docstring, the hardware reason as a comment on the line that encodes it), the tooling bar (state, trace, debug, faults), the docs layout. Copy its **shape**, never its code. - `m68000-python` (`../m68000-python`): a microcoded 16-bit CPU whose bus cycles are the contract; its `docs/claims.md` boundary is the model for this core's claims page. - `huc6280-python` (`../huc6280-python`): a Phase 0 brief carried through to finished rungs. - `../emulator-lineage/docs/proposals/2026-11-jit.md` (accepted 2026-11-25): declare sync points, memory kinds, timing as data, and keep handlers inside the named subset its lint will check (sh2-python's `handler-subset` branch is the pilot). This core needs no recompiler (Domains and speed below), but timing as data is how C1-C5 stay switchable. Rules: Python >= 3.11, CPython and PyPy; modern forms; no dangerous-looking shell one-liners; local git only; MIT; nothing read from GPL or LGPL code (MAME's TI-99 side is LGPL-2.1+, its tms9900 device BSD-3-Clause but kept closed, see open decision 4); no ROM or GROM bytes committed; run Python that reads `reference/` with `-I`. ### Read in this order README.md; docs/start-here.md; docs/cpu.md; docs/timing-and-bus.md; docs/validation.md; docs/mame-oracle.md; then [DM] §2-3 in full and [FSD] pp. 4-38..4-41 and 4-89..4-110 (the machine cycles) from `reference/text/`, with the page images where the OCR drops a step. ## Scope In: the TMS9900 programmer's model (PC, WP, ST; workspace registers in memory), all 69 instructions and the undefined-opcode behaviour (U1), the six addressing modes, interrupts on 16 levels with the mask, LOAD, RESET, IDLE and the external-instruction codes, the CRU (single and multi-bit), HOLD/HOLDA, READY/WAIT, IAQ, and the machine-cycle sequences with their bus contents. Out (ti99-python): every memory map, the 16-to-8-bit multiplexer and its wait states, the TMS9901 (interrupt encoder, keyboard, timer), what the external instructions do on a board, the clock generator. ## Proposed names - Repository `tms9900-python`, package **`tms9900_python`** (the 2026-11-12 naming rule). - `tms9900_python.TMS9900`: the core. No variants as configurations: the TMS9980A and TMS9995 have their own machine-cycle sequences (open decision 3). - `tms9900_python.bus`: the `Bus` protocol below and the cycle `kind` constants: `FETCH` (IAQ high), `OPERAND` (immediate or address word), `REGISTER` (a workspace access), `DATA`, `CONTEXT` (vector and context-switch words), `READ`, `WRITE`; `CRU_IN`, `CRU_OUT`, `EXTERNAL`; `ALU` (observe-only, no board callback). - `tms9900_python.timing`: the machine-cycle sequences as data, one row per instruction and addressing mode, each with its source ([FSD] page) and, where [DM] disagrees, the alternative (C1-C5) as a named switch. - `tms9900_python.disasm`, `trace`, `debug`, `state` in z80-python's shape. ## The bus contract (proposal, to settle in the first commit) - `read(address: int, kind: int, clock: int) -> tuple[int, int]` returns (word, wait clocks); `write(address: int, value: int, kind: int, clock: int) -> int` returns wait clocks. Address is the 16-bit byte address with bit 0 = 0 (A0-A14 plus a zero); `clock` is the CPU clock at the start of the cycle, so a board can place the access against its other parts (VDP, GROM). Every read-before-write is a real `read` call. - `cru_in(bit: int, clock: int) -> int` and `cru_out(bit: int, value: int, clock: int)` with the 12-bit CRU bit address; `external(code: int, clock: int)` for the five external instructions (code from A0-A2) and each IDLE repetition. - Inputs the board drives between steps: `set_intreq(level: int | None)` (the IC0-IC3 code, or no request), `set_load(active: bool)`, `set_reset(active: bool)`, `set_hold(active: bool)`. - `run(clocks: int) -> int` runs whole machine cycles until the budget is spent and returns the clocks used; it stops on instruction boundaries only when asked (`step()`), because a board with HOLD (the 99/4A's PEB DMA, later) needs to stop at a non-memory cycle. - Observation: an optional `observe(cycle)` hook gets every machine cycle, ALU ones included, as (clock, kind, address, data, iaq, waits): the trace the Rust tiers must reproduce. Time is the CPU clock (3.000 MHz on the 99/4A). The board's other domains (the VDP and everything clocked from it, the speech chip) meet the CPU at channels ti99-python declares; the core only needs to stop where the board asks (`run` to a stamp). ## Unknowns (U) and contradictions (C) - U1 What the undefined opcodes (0000-01FF, 0320-033F, 0780-07FF, 0C00-0FFF) do beyond "6 clocks, 1 access" ([DM] table 3's last row, poorly scanned): status bits? PC? Default provisional: a 6-clock no-op that changes nothing (HYPOTHESIS). - U2 Whether DIV with overflow leaves ST0-ST3 and ST5 unchanged (only ST4 is documented). - U3 Whether the "don't care" bits of formats VII and VIII (bit 11 of LI.. LIMI; bits 11-15 of IDLE.. LREX, RTWP) are really ignored by the decoder. - U4 The exact wording of C/CB/CI's L> and A> conditions (the OCR of [DM] 3.4 is garbled); settle from [FSD] ch. 6 and the page images before coding. - U5 The DIV data-dependent length Ni (32 ≤ Ni ≤ 48 cycles, "dependent upon the partial quotient after each clock cycle", [DM] table 3 note): the rule per bit is not documented. Default: one extra cycle per quotient bit whose trial subtraction succeeds (HYPOTHESIS, FITTED to the 92-124 range) until a measurement exists. - U6 IDLE: the repetition period of the code-and-CRUCLK cycle, and whether HOLD is granted in IDLE. - U7 The values the RESET sequence writes into the new workspace's R13-R15 (whatever the internal registers held at power-on): visible on the bus. - U8 The interrupt sampling point: on the last clock of an instruction, or at its last machine cycle's start; matters for the 9901 timer and VDP interrupt edges. - U9 X executing X, and X executing an instruction that itself takes an immediate word: the documented rule (extra words from PC) at cycle level. - U10 What the data bus shows on an ALU cycle (the SD register) when a board samples it (only if a board ever floats a read; not the 99/4A as far as known). - C1 MPY: [FSD] p. 4-93 numbers steps 7 + Ns (16 ALU) then 24 + Ns; read with the skip, 26 cycles = [DM]'s 52 clocks. Resolved by reading; keep the note. - C2 DIV with overflow: [DM] 16 clocks / 3 accesses; [FSD]'s sequence exits after 7 cycles (14 clocks). - C3 LIMI: [DM] 16 clocks; [FSD] 7 cycles (14 clocks). - C4 NEG: [DM] 12 clocks / 3; [FSD] groups NEG with CLR..SWPB, 5 cycles (10 clocks). - C5 STCR: [DM] 42 (C 1-7), 44 (C 8), 58 (C 9-15), 60 (C 0, i.e. 16); [FSD]'s formula 12 + Ns + C + C' (C' = 8 - C - 1 for C ≤ 8, 16 - C above) gives 40, 40, 58, 58. Judges for C2-C5: a test program timing these instructions on a real console against the 9901 timer or the VDP interrupt (no such capture exists for us; open decision 5). ## Milestones (rungs; exit tests in validation.md) 0. Tooling and decode: `gen_decode.py` from [DM]/[FSD] (facts) into a committed `_decode.py`; disassembler matching MAME's text on the Phase 0 traces (166,776 lines); state, trace, debug. 1. Semantics and flags from [DM]. 2. Machine-cycle sequences from [FSD] with [DM]'s totals; C1-C5 switchable. 3. MAME lockstep on ti99-python's milestone 1 (console boot to the title screen and 1 s at it). 4. Test programs on ti99-python (TI Diagnostics; PeteE's test if licensed; EP994A's test as a closed box in emu-tools-gpl). 5. Interrupts, LOAD, RESET, IDLE, HOLD, READY at cycle level. 6. Seeded faults, each caught. ## Domains and speed **Domains.** The CPU belongs to the TI-99/4A's CPU domain (C): the TMS9900, the console ROM, the scratchpad, the multiplexer, the TMS9901, cartridge ROM and the expansion RAM, all timed by the TIM9904 from the 48 MHz crystal (3.000 MHz). The VDP runs from its own 10.738635 MHz crystal and supplies the 447 kHz GROMCLK that clocks the GROMs and the sound chip, so the VDP, GROMs and sound form a second domain (V); the speech synthesizer's TMS5220 runs from its own 640 kHz oscillator (S). ti99-python's docs/domains-and-speed.md declares them and their channels; for this core the only channel is its bus: every memory cycle to the 8-bit side (VDP, GROM, sound, speech) is a sync point. **Instructions per second needed, with the arithmetic.** - Ceiling: the shortest instructions take 8 clocks with no wait states (B, STST, STWP, a jump not taken, [DM] table 3): 3,000,000 / 8 = **375,000 instructions/s**. - Observed: MAME 0.285 at the console's title screen ran 161,014 instructions in 1.001 s, all from the 16-bit console ROM (detector figure, mame-oracle.md): **0.16 M instructions/s**, or about 0.8 M machine cycles/s (an average of 18.7 clocks per instruction ≈ 9 machine cycles, wait states included). - Code running from the 8-bit bus (cartridge ROM, 32K expansion) is slower per instruction (every access + 4 clocks), so the instruction rate only falls: the CPU never needs more than 0.375 M instructions/s, about 1.5 M machine-cycle callbacks/s at the cycle-level contract. **Does the no-JIT path reach real time?** | Target | Expected | Why | | --- | --- | --- | | Python (PyPy), reference | about real time to a few times slower | sh2-python's harness runs 0.1-0.2 M instructions/s (sh2-rust docs/status.md); this core needs 0.16-0.375 M with a machine-cycle trace, so expect 0.3-1 x real time: enough for every rung, and reference boards are "correct, not fast" | | Rust (`tms9900-rust`), desktop | many times real time (to measure) | a transcription runs orders of magnitude faster than its Python reference; 0.375 M instructions/s is a small load for any of the family's Rust CPU cores | | Rust -fast, phone and browser (wasm, no JIT) | far above real time | 0.375 M instructions/s is under 1 % of what an interpreted Rust core does on a phone; no cached interpreter needed | | Threads at domain channels | not needed for the CPU | the board's cost will be the VDP's dots, not the CPU | So `recompile: none` on every target, and neither a cached interpreter nor a code generator is worth building for this CPU (prune what does not pay). The JIT proposal's declarations still apply: the sync points are the 8-bit-bus accesses and the CRU; the memory kinds are console ROM, scratchpad (code runs there: the console copies routines into it), cartridge ROM (banked by writes), cartridge RAM and expansion RAM; timing is data (`timing.py`). ## Open decisions for Aubrey 1. **Machine-cycle level, with [FSD]'s sequences as the contract.** Recommended: TI published the bus contents of every cycle, the 99/4A's wait states make the position of each access observable, and the cost is small at this CPU's speed. An instruction-level core with summed cycles would claim less than the evidence gives. 2. **Where TI's two documents disagree (C2-C5), follow [FSD]'s cycle sequence and keep [DM]'s total as a named alternative.** Recommended: the sequence is the more detailed statement and the only one that places the accesses; both readings stay in `timing.py` until a console measurement settles them. 3. **No TMS9980A/TMS9995 configurations in this core.** Recommended: they have different bus widths, on-chip memory and microcode (their own cycle sequences). A `tms9995-python` would be a separate core repository when a TI-99/8, Geneve or Tomy Tutor board is planned. 4. **MAME's tms9900 files stay closed although they say BSD-3-Clause.** Recommended: they sit in a tree where the same author's TI-99 board code is LGPL-2.1+, and TI's two documents are enough for the specification; revisit only for a question no document answers. 5. **A console timing probe.** Aubrey has no TI-99/4A (no probe possible). Recommended: record C2-C5 and U1, U2, U5 in the family's probe queue (`PROBE-QUEUE.md` in the family's root folder, Aubrey's batching rule) in case a console or an owner who will run a test becomes available; until then, the provisional choices below. 6. **Nouspikel's Tech Pages are set aside for now (site's wishes: a bot challenge at the host).** Recommended: ask Aubrey whether the author's own downloadable zip of the site, or an e-mail to the author asking permission, is an acceptable route; the pages are the best hardware-verified community source for this CPU and the 99/4A. 7. **Default provisional choices** (Aubrey's 2026-11-12 rule): U1 undefined opcodes are 6-clock no-ops with one fetch; U2 DIV overflow leaves the other flags alone; U3 don't-care bits are ignored; U5 DIV's extra cycles per successful trial subtraction; U8 interrupts sampled at the end of the instruction's last machine cycle. Each marked HYPOTHESIS in code.