# Handoff: build `tms9918-python`, the TMS9918A-family core ## Context you are inheriting The family's shape: **chips are cores, boards compose cores; never copy code between repositories.** Each core certifies itself against the best oracle its chip has; emulators are detectors only. Aubrey decided on 2026-11-12 to extract this chip into its own core with **colecovision-python as first consumer**; **sms-python keeps its in-board VDP** and adopts this core later on a branch. Build order (colecovision-python's brief, decided): the z80-python M1 signal, the sn76489-python `SN76489A` row, and this core first, then the ColecoVision's M1. Read, in order: [chip.md](chip.md) (the device), [timing.md](timing.md) (the memory schedule), [consumers.md](consumers.md) (who needs what, and sms-python's TMS row against this core), [validation.md](validation.md) (oracles and tiers), [mame-oracle.md](mame-oracle.md) (register streams). Then `python -I scripts/fetch_references.py` (17 files, 7 repositories; `--verify` to check) and regenerate the four MAME logs with the commands in mame-oracle.md. For a finished chip core's contract and ladder, read `/data/emu/sn76489-python/docs/contract.md` and `/data/emu/ym2413-python/docs/validation.md`. ## Scope In: the TMS9918A, TMS9928A and TMS9929A as rows of one variant table; 4K/8K/16K VRAM; the CPU interface; the eight registers and status; Graphics I, Graphics II, Multicolor, Text and the four undocumented combinations; sprites; the memory schedule with CPU access windows; /INT; frames as colour indices; reset. Later rows, when a consumer needs them: the plain TMS9918 (no Graphics II; TI-99/4, CreatiVision), the TMS9118/9128/9129 (no 4/16K bit; MSX1, arcade). Out: RGB and the analogue outputs (the board's), external-video mixing and sync (the board's; the core marks where every plane is transparent), GROMCLK and CPUCLK outputs (the board derives them from its clock), the V9938/V9958 and Sega's VDPs ([consumers.md](consumers.md)), the 340-dot "rainbowless" 9918. ## Names and the import boundary | Thing | Proposal | | --- | --- | | repository / package | `tms9918-python` / `tms9918_python` (decided) | | classes | `TMS9918` (the chip), `VDPVariant` (frozen dataclass), rows `TMS9918A`, `TMS9928A`, `TMS9929A`; `VARIANTS` maps names to rows; `Frame` (indices + border) | | dependencies | none (standard library only); PyPy 3.11 floor; keep instance attributes under 80 (PyPy's limit, family knowledge) | | The core owns | The board owns | | --- | --- | | VRAM (size from the board), registers, address register, read-ahead, the first-byte flip-flop, status, the line and dot counters, the memory schedule and access windows, the lost-access rule, sprites, the /INT level, frames as indices | the master clock and its ratio to the CPU; address decode and mirrors; when to call the core (it runs the core up to each access first); CPU wait states (the chip has none; the board's other waits, such as the ColecoVision's M1 and READY, are the board's); turning /INT into an NMI or IRQ; RGB; external video; reset wiring | ## The contract (proposed) ```python from tms9918_python import TMS9918, TMS9928A vdp = TMS9918(TMS9928A, vram_size=16384) # 4096, 8192 or 16384: the DRAM fitted vdp.run(master_clocks) # advance; the host calls this up to each access vdp.write(mode, value) # MODE pin: 0 data, 1 control value = vdp.read(mode) # 0 data (read-ahead), 1 status vdp.int_line # True while /INT is asserted (F and IE) vdp.line, vdp.dot # beam position, read-only (for boards and logs) frame = vdp.take_frame() # None until a frame completes vdp.reset() # /RESET: R0, R1, flags, counters, flip-flop ``` - **Time in master clocks** (10.738635 MHz). Justification: the CPU clock is XTAL/3 on every present consumer, the pixel clock XTAL/2 and the memory cycle XTAL/4 ([timing.md](timing.md)), so master clocks are the one unit in which CPU access stamps, pixels and memory cycles are all integers. Memory cycles would put CPU stamps on thirds; CPU clocks would put pixels on halves and memory cycles on thirds. sms-python already counts "mclocks" in this unit, so its adapter is `run(cycles * 3)`. The hot loop advances by whole memory cycles (171 a line) and carries the remainder. - **Ports at the core's current time**: the host brings the core to the access's master clock, then calls `read`/`write`. The CPU-side effect (latch, buffer, flip-flop, status clear) is immediate; the VRAM side waits for an access window. - **Frames**: `Frame.pixels` is 192 rows of 256 colour indices (Text mode's 240 placed at TI's offset, borders filled), each already resolved through the plane order: a sprite colour, else the pattern colour, else the backdrop. Index 0 therefore appears only where every plane is transparent and the backdrop is 0 (TI: black, or external video if enabled); `Frame.border` is the backdrop index per line (262 or 313 entries). The full 342-dot raster is a later option, not the default. - **/INT** is a level. The board turns it into an edge if its CPU wants one (the ColecoVision's NMI) or a level (the SG-1000's and MSX's IRQ). - **Variants are a frozen dataclass**: `name`, `lines` (262/313), the vertical layout (TI's NTSC column; the PAL split per U7), `graphics2` (False on the plain 9918), `has_4k_select` (False on the 91x8), `colour_mask_masks_patterns` (True on the 99x8, C3), `output` ("composite" or "colour-difference": information only), `levels` (TI table 2-3's column for that part), `pin38` ("cpuclk" or "r-y"). A host may build its own row. - **Switches with stated defaults** (constructor keywords, recorded in the claims): `lost_access` (U3), `status_race` (off; U8), `flag_dot` (TI's wording; C10), `windows` (on; off reproduces MAME's serve-at-once model for detector comparisons). What each present consumer gets: colecovision-python: `int_line` for its NMI edge, the TMS9928A and TMS9929A rows, master-clock time locked to its Z80 (x3). sms-python: its 15 VDP calls map onto this surface through an adapter in sms-python ([consumers.md](consumers.md)); its Sega counters stay in its board, RGB stays in its board. ## Unknowns | # | Unknown | Best source, tier | Plan | | --- | --- | --- | --- | | U1 | Does the first control byte go into the address register at once, or wait for the second? | none; MAME and sms-python: at once (tier 5) | at once, as both detectors; a directed test pins the choice | | U2 | Does /RESET clear the flip-flop and read-ahead? | TI: "synchronizes all clocks", clears R0/R1 | yes (clear), provisional | | U3 | **What a too-early access does**: which byte is lost, what the address does | TI table 2-2 gives delays only (2); VATT shows lost writes leave the old byte (3) | a write-data register that a second access overwrites; decide between candidate rules at rung 4 by VATT's landing fractions | | U4 | Power-on: R2-R7, VRAM, C, the counters' "known states" | TI 2.1.7 (2); MAME zeros, number 31 (5) | zeros, seedable; C starts 0 | | U5 | Boards with less than 16K fitted (4027s): what reads above 4K return | TI 3.1 (2) | `vram_size` parameter; mirror by wiring per table 3-1 | | U6 | The plain TMS9918's behaviour with R0 bit 1 set, and its undocumented modes | none | out until a consumer needs the row | | U7 | The TMS9929A's vertical layout | TI: NTSC column only; MAME top border 13 + 51 (5) | MAME's split, provisional | | U8 | The status-read race window on a TMS99x8 | V9938/V9958 only (1, wrong chip); Guttag via forum (3, secondhand) | switch, off by default | | U9 | External sync through pin 34 | TI 3.5 (2) | out of scope | | U10 | The dot at which 5S, its number and C change | TI: C "during the generation of the pixel" (2); sprite pre-processing in the 1978 drawing (2) | from the schedule; provisional | | U11 | When register writes take effect inside a line | none | at the next memory cycle that reads the register; provisional | | U12 | Sprite "ghosting" when Graphics II masks are in use | MAME to-do: "Exact behaviour is not known" (5) | not modelled; the fetch-driven design is where it would live | | U13 | The "2 us VDP delay" in cycles | TI (2), Vermaelen guesses 11 pixel clocks (3) | a variant field; fit at rung 4 | | U14 | Fifth-sprite number reliability on warm consoles | forum reports | noted, not modelled | ## Contradictions between sources | # | Contradiction | Sides | Decision | | --- | --- | --- | --- | | C1 | What a status read clears | TI, MAME, sms-python: F, 5S, C; Young: F and C only | TI | | C2 | Mode-bit names | TI: M1 Text (R1 `0x10`), M2 Multicolor (R1 `0x08`), M3 Graphics II (R0 `0x02`); Young, colecovision-python's `vdp.md`, sms-python: M2 and M3 swapped | TI's names in the code, with the register bit always given | | C3 | Does R3's mask also mask Graphics II pattern indices? | MAME (99xx yes, 91xx no) and **TI's 1982 memo** ("a logic error"): yes; Young, sms-python: no | yes on the 99x8 rows (tier 2 over tier 3); a variant field | | C4 | Multicolor nibble order | TI 2.4.3, MAME, sms-python: high nibble left; Young 3.4: low nibble left | TI | | C5 | 5S "lines 0 to 192" | TI 2.3.3; the active area is lines 0-191 | 0-191; a sprite reaching line 192 is a test case (detectors differ there) | | C6 | The fifth-sprite number | data manual: "the number of the fifth sprite", only while F = 0; Programmer's Guide: "the lowest priority sprite on the horizontal line", no F condition | the data manual | | C7 | Off-screen sprite collisions | TI: sprites "partially or completely off the screen are also considered"; MAME, sms-python: visible columns only; Young: dropped (fifth) sprite lines never collide | TI for off-screen pixels, Young for dropped lines; flagged as a known difference from both detectors | | C8 | Which parts have 262 lines | TI 3.6.2 "TMS9918A/9929A ... 262"; the same sentence after table 3-2 says "TMS9918A/9928A" | a misprint: 9918A and 9928A | | C9 | Multicolor access windows | TI table 2-2: 0-1.5 us; the 1978 drawing (as Vermaelen reads it) and VATT on 17 machines: no better than Graphics II | the drawing and VATT | | C10 | When F sets | TI: end of the last active line's scan; Young: after line 192; MAME: drawing line 193; sms-python: Sega's measured point | TI's wording (end of line 191's active pixels), a switch | | C11 | What the 4K/16K bit does | MacDonald (tier 1 report): a different DRAM row/column mux; TI's 1982 memo: the address counter rolls over at 4K; MAME, sms-python: nothing | both effects; the roll-over's interaction with the mux is a directed test | ## The ladder (each rung's exit test) | Rung | What | Judge | Exit | | --- | --- | --- | --- | | **0** | **Manual to tables**: variants; table 3-3 layout; table 3-2 address derivations; register masks; table 2-3 levels; MacDonald's mux; the 1978 drawing's slot table per mode as data | TI (2), the 1978 drawing (2) | every table in code cites its source line; tests rebuild each table from the manual's numbers; no rendering yet | | **1** | **Registers and ports**: the flip-flop, both address writes, read-ahead (and the write that refills it), register writes destroying the address, status clearing, IE while F, /INT level, 4K mux and roll-over, reset | TI 2.1-2.3 (2), Young 2 (3) | about 40 directed tests from the manual's sentences pass; C1, C2, C11 decided in `docs/claims.md`. **First passing rung: create the GitHub remote and make it public (decided)** | | **2** | **Each mode's picture on register streams**: replay the four MAME logs and synthetic streams for every mode, the undocumented four, masks and blanking, with `windows=False` | TI tables (2); MAME and sms-python as detectors | final VRAM equals MAME's `vram.bin` byte for byte on all four logs; frames compared by index with MAME's last frame and sms-python's TMS9918A row; **every differing pixel attributed** to a named C or U | | **3** | **Sprite rules**: four a line, 5S and number with the F gate, C on- and off-screen, EC, SIZE, MAG, Y = 208, Y wrap, transparent sprites, text mode | TI 2.3, 2.4.5 (2); Young 4 (3) | directed tests pass; Flicky's stream (5S every frame) gives the same status values as MAME's log at each read, or the difference is a named C | | **4** | **Access windows**: the schedule drives memory cycles; CPU accesses wait for windows; the lost-access rule | the 1978 drawing (2); **VATT (3)** | an emulated VATT (its loop replayed as stamped writes with MSX's one-clock M1 wait) lands within the spread of the TI-part rows' raw fractions (20 machine-and-owner entries with a TMS99x8 or TMS91x8 in the sheet as fetched; the T6950 and Yamaha YM2220 rows excluded) at every period 12-31 T, all modes, display on and off; differences from openMSX's and Clock Signal's slot tables named; Flicky's 93 fast writes reported (how many the core loses) | | **5** | **Whole-frame replays on two machines**: 6 ColecoVision and 6 SG-1000 streams (Zaxxon, Flicky and ten chosen from colecovision-python's smoke list and sms-python's SG-1000 failures), windows on | MAME (5) named as the detector | **zero differing pixels against MAME with `windows=False`**; with windows on, every difference attributed to a lost access the log shows | | **6** | **Fault injection**: about 20 seeded faults (an off-by-one in each table, the flip-flop not cleared by a data access, the read-ahead not refilled, 5S without the F gate, the mux skipped...) | not an oracle | every fault caught by the tests of rungs 0-5 | | **7** | **Speed**: frames per second on CPython and PyPy for a Graphics II frame with sprites, windows on and off | none | measured and recorded; real time not required (reference cores are correct, not fast) | A rung closes when its exit holds and what it cannot decide is written into `docs/claims.md` as "provisional, tier N". ## What was not verified in Phase 0 - The 1978 drawing was read at screen resolution only; its slot positions are not yet transcribed. The other Guttag documents are handwritten and were not read in full; the patent was not fetched. - vdp18 (MiSTer), the F18A's VHDL, openMSX and Clock Signal were not built and their timing code was read only where cited. - VATT's raw sheet is a live spreadsheet export; its rows were read for the TMS99x8 machines only, and its column semantics (blank = not tested) are inferred from the wiki. - No replay was run: there is no core. The MAME logs were checked by counts and by the last frame only. ## Open decisions for Aubrey 1. **Time unit: master clocks.** *Recommend* master clocks (exact for CPU, pixel and memory clocks; the unit sms-python already uses), with the hot loop stepping whole memory cycles. The alternative, memory cycles, makes every host convert CPU clocks with a remainder. 2. **The lost-access rule's default (U3).** *Recommend* a single write-data register that a second access overwrites before its window comes (the newer byte wins, the address increments once per CPU access), with the alternatives behind the same switch; let rung 4's fit to VATT's fractions choose, and record the losers. 3. **The Graphics II colour mask on patterns (C3).** *Recommend* following MAME and TI's 1982 memo on the 99x8 rows. This differs from sms-python's TMS row today, so its adoption will move any SG-1000 picture that uses a reduced colour table; that move is correct by tier. 4. **Off-screen collisions (C7).** *Recommend* TI's sentence (off-screen pixels collide), knowing both detectors differ; the alternative is to follow the detectors and file it as provisional. Few games can tell. 5. **Frame contract.** *Recommend* resolved indices (256 x 192) plus a per-line border index, with the full 342-dot raster as a later option. sms-python and colecovision-python both draw 256 x 192 today. 6. **Variant rows in the first build.** *Recommend* the three "A" parts only; add the plain TMS9918 and the TMS91x8 rows when a TI-99 or MSX board starts (the fields are there from rung 0). 7. **Palette data.** *Recommend* the core ship TI table 2-3's levels per part as data and no RGB; each board keeps its own RGB table (sms-python's stays as it is). 8. **The remaining manufacturer documents.** *Recommend* an hour before rung 4 to read the 1978 drawing at full resolution (transcribe its slot table into rung 0's data) and the 1978 sprite-processing pages by eye, and to fetch US patent 4,243,984 from the same archive. Not needed for rungs 0-3. 9. **VATT data licence.** The raw sheet carries no licence. *Recommend* using it as a test target only (fractions written into the test as numbers with a citation, the sheet itself never committed); asking aoineko for a licence is an outside contact and stays deferred with the others.