# Handoff: build `msx-python`, the MSX board ## Context you are inheriting The family's shape: chips are cores, boards compose cores; CPUs always get their own core; a non-CPU chip used by one machine stays in its board and is extracted when a second family project needs it; never copy a file between repositories, pin the sibling by commit. Read the charter first (emulator-lineage `docs/charter.md`), then [start-here.md](start-here.md). The MSX1 is era 1 in the charter's table: one clock domain, cycle level everywhere, Python runs the tests, Rust plays. **The model board is colecovision-python**: the same Z80 with an M1 wait, the same TMS99x8 family through tms9918-python, a PSG through a family core, a BIOS run as a program, a MAME lockstep at milestone 1 with 0 unexplained differences. Read its handoff-brief.md, docs/memory-map.md and `src/colecovision_python/board.py` (how `read_m1` and `instruction_timing` give every port access its clock) before writing code. Read in order: 1. [README.md](../README.md), [start-here.md](start-here.md), [slots-and-memory.md](slots-and-memory.md). 2. [cpu.md](cpu.md), [vdp.md](vdp.md), [psg.md](psg.md), [ppi-keyboard.md](ppi-keyboard.md). 3. [cartridges.md](cartridges.md), [scc.md](scc.md), [msx-music.md](msx-music.md), [disk.md](disk.md), [cassette.md](cassette.md), [bios.md](bios.md). 4. [machines.md](machines.md), [domains-and-speed.md](domains-and-speed.md). 5. [sources.md](sources.md), [provenance.md](provenance.md), [validation.md](validation.md), [mame-oracle.md](mame-oracle.md), [census.md](census.md). Then `python -I scripts/fetch_references.py` (27 files, ~130 MB, and 3 repositories; `--verify` to check). PDF text lands in `reference/text/`; six scanned manuals were OCRed by hand into `reference/ocr/` (`ocrmypdf --skip-text`; rerun if you need them). ## Decided by Aubrey (standing rules that apply here) - Name `msx-python`, package `msx_python`. Local, no remote, until he says otherwise. - Bring your own ROMs; nothing of a maker's in the repository; romsets by `NO_INTRO_DIR` and `MAME_ROMPATH`. - Python floor = PyPy's (3.11), PyPy must work; classes under ~80 attributes; modern over compat; reference boards are correct, not fast; sound may be less exact than pictures; smoke lists before sweeps. - Technical choices are made, marked provisional and reported; policy waits for him. ## Proposed by this Phase 0 - **MSX1 first** (National CF-2700, NTSC Japan, TMS9918A + AY-3-8910A), with a `cbios-msx1` row from milestone 1, then rows in the order of [machines.md](machines.md): Canon V-20 and Sony HB-75P (PAL), mappers/SCC/FM-PAC, tape and disk, MSX2, MSX2+, turbo R. - **One timeline in CPU clocks** (the VDP brought up to each port access in master clocks = CPU clocks x 3), the M1 wait added by the board through `read_m1`, every access's in-instruction offset from `instruction_timing` shifted by the M1 waits before it. - **The slot tree as first-class structure**: four primary slots, each a device or an expander of four; devices see (page, address) and their own select. - **Cartridge types by a SHA-1 table** seeded from MAME's CC0 lists, heuristics only to propose entries. - **Disk functional at the FDC's registers**, never a DSKIO patch in the specification. ## Package and classes (proposed) ``` msx_python/ machine.py # MachineRow: generation, vdp_variant, psg_variant, engine, slot layout, # RAM size and slot, keyboard layout, region, ROM SHA-1s, # m1_wait_on_acknowledge (U1), vdp_io_waits (U7), ppi_reset_state (U20) console.py # build(machine, cartridges=(), bios_dir=...) -> Msx; run, frames, audio board.py # Msx: the Z80 with read_m1, the timeline, I/O decode, interrupts slots.py # PrimarySlot, Expander (FFFF, inverted read), SlotDevice protocol, open bus memory.py # Rom, Ram, MemoryMapper (FC-FF, MSX2 rows) ppi.py # the 8255 as wired (port A slots, B keyboard, C scan/cassette/caps/click) # until i8255-python exists (decision 6) keyboard.py # matrices per layout (TDB p. 13, TH2 5.3.1), ghosting switch (U11) joystick.py # the two general-purpose ports behind PSG ports A/B; controller sockets cartridge.py # the loader (SHA-1 table), plain ROM placement, mappers mappers.py # Konami, KonamiScc, Ascii8, Ascii16, their SRAM forms, GameMaster2, ... scc.py # Scc (051649); SccI later fmpac.py # FM-PAC and built-in MSX-MUSIC wiring to ym2413-python tape.py # TapeDeck: .wav levels, .cas synthesis; motor disk.py # Wd2793Interface (Philips-style map), sector images sockets.py # Protocols and test-only fakes (VDP, PSG, controller, tape, disk) savestate.py # whole-machine states naming ROMs by SHA-1 ``` ## What the board needs from its cores (the embedding contracts) - **z80-python**: `read_m1(address, kind)` on every M1 cycle (exists, branch `m1-fetch-hook-rebased` at `5434349`, **not on main**: decision 2); `timing.instruction_timing` (on main); IM 1 interrupts from a level; RESET. - **tms9918-python**: TMS9918A/9928A/9929A rows (exist); **TMS9118/9128/9129 rows** (requested upstream: decision 4); `run(master_clocks)`, `write/read(mode)`, `int_line`, `take_frame()`; access windows and `lost_access` as fitted to VATT. - **ay8910-python**: AY-3-8910 and YM2149 rows, `port_a_in`, `port_b_out`, the YM2149 divider (exist). - **ym2413-python**: `OPLL(YM2413)`, `write`, `run`, `take_outputs` (exist). ## Domains and speed - **Domains:** one (M: Z80, slots and their devices, VDP, PSG, PPI, cartridge sound chips) for every row through turbo R; a disk drive domain D only once the drive is modelled at bit level (stepped inside M at FDC register accesses in the order of record); the tape is a level stream (producer/consumer). Channels: VDP ports and /INT (inside M), FDC registers and DRQ/INTRQ (M-D), cassette pins (M-T), sound outputs (producers). [domains-and-speed.md](domains-and-speed.md). - **Instructions per second:** 3,579,545 clocks/s; MAME's `cf2700` ran 1,374,251 instructions in its first 3 s = **458,000 instructions/s** (7.8 clocks each with M1 waits): the need is ~0.4-0.5 M instructions/s, plus the VDP's 342 x 262 x 59.92 = 5.37 M dots/s if stepped per dot. - **No-JIT path:** Python reaches about real time on PyPy (colecovision-python, the same chips at the same clocks: 0.6-2.9 x on PyPy, 0.3-0.5 x on CPython); Rust `-fast` (z80-rust-fast: 344 M instructions/s, ~700 x the need) is far above real time on desktop, and phones and browsers (wasm) keep two orders of magnitude of margin, as the family's live Z80 sites show. **`recompile: none` meets every target; no cached interpreter, no threads needed.** A `-turbo` only if run-ahead on a weak device measurably needs it. ## The gate (defined at milestone 1, before any add-on) Lockstep against MAME's `cf2700` trace (instruction stream, registers, `totalcycles`, I/O order) over the first 3 emulated seconds with **0 unexplained** differences (MAME's M1 choice on acknowledges switched on for the run, as colecovision-python did); unit tests of every documented fact (TDB, TH2, data sheets); C-BIOS booting to its logo; then VATT's loops replayed through the board within the hardware spread tms9918-python already meets. Seeded faults: validation.md's list, each caught. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | **CPU, slots, PPI, keyboard, memory, I/O decode, VDP and PSG wired, the M1 wait; `cf2700` and `cbios-msx1` rows** | TDB, TH2, 82C55A (2); MAME trace (5) | the maker's BIOS runs from reset with the instruction stream and I/O order equal to MAME's for 3 s, or a named reason per difference; C-BIOS reaches its logo screen; slot/sub-slot/FFFF/keyboard facts as unit tests | | M2 | **Pictures and VATT** | tms9918-python's ladder; VATT (3) | BIOS and C-BIOS screens drawn; VATT's MSX1 loops (12-31 T) through the whole board inside the spread tms9918-python's rung 4 reaches, with the M1 wait coming from the board, not the test | | M3 | **Plain ROM cartridges, joysticks, sound out** | TDB p. 44 (2); MAME pictures (5) | a smoke list of 10 plain ROMs (problem ROMs and well-behaved classics) reaches gameplay; PSG audio compared with MAME's, differences named | | M4 | **Mappers and the SCC** (Konami, Konami SCC, ASCII 8K/16K and SRAM forms; the SHA-1 table) | MEGAROMS, SCC-SY (3); MAME (5) | 10 mapper games of the smoke list reach gameplay; SCC registers and mirrors as unit tests; SCC audio triaged against MAME | | M5 | **Rows: Canon V-20 (YM2149), Sony HB-75P (TMS9929A, PAL, European matrix)** | TDB; VATT rows for these machines | both boot; VATT on the HB-75P row inside its machine's spread | | M6 | **MSX-MUSIC (FM-PAC), tape** | DP2-3.1 (2); TH2 5.2 (2) | FM-PAC detected by the BIOS and playing; a `.cas` and a `.wav` load through the real BIOS's TAPION/TAPIN | | M7 | **Disk (Philips-style WD2793, functional)**; save states | WD279X (2) | a disk ROM (player's or C-BIOS's) boots MSX-DOS from a `.dsk`; save state at half way, load into a fresh machine, continue identically | | M8 | **MSX2** (after `v9938-python` exists): mapper, RP5C01, sub-ROM, S1985 waits | TH2; S1985; V9938 core's ladder | an MSX2 row (NMS 8245 or HB-F1XD) boots to BASIC and DOS; MSX2 smoke list | Full No-Intro sweeps only when the board might be ready (Aubrey's rule), after the smoke list passes; a sweep's failures become the next smoke list. ## The chip rule | Chip | Users in or near the family | Proposal | | --- | --- | --- | | Z80 | many | z80-python (pin the M1-hook commit until main has it: decision 2) | | TMS9918A family | ColecoVision (built), SG-1000 (later adopter), MSX1 | tms9918-python; MSX asks for the 91x8 rows | | AY-3-8910 / YM2149 | NES 5B (famicom-python adopted it), MSX, Atari ST, arcade | ay8910-python | | YM2413 | SMS FM unit, VRC7, MSX-MUSIC | ym2413-python | | 8255 PPI | sms-python (SC-3000, SF-7000, in-board), MSX | **extract `i8255-python` from sms-python** (decision 6) | | uPD765 / TC8566AF | sms-python's SF-7000 (in-board), MSX Panasonic disks and turbo R | extract `upd765-python` when that row starts (decision 9) | | SCC (051649), SCC-I | MSX cartridges; Konami arcade boards (none in the family) | board until a Konami arcade board (decision 8) | | WD2793 / MB8877A | MSX only, in the family | board | | V9938 / V9958 | MSX2, MSX2+, turbo R, MSX1 V9938 rows; Geneve 9640; Super Free Kick | **new core `v9938-python`** (decision 3) | | R800 | turbo R | new CPU core `r800-python`, later (decision 7) | | RP5C01 | MSX2 | board | | S3527, S1985, T9769 engines | MSX | board (as machine-row behaviour, not chips) | ## Unknowns (U) | # | Unknown | Best source now | Default | | --- | --- | --- | --- | | U1 | M1 wait on interrupt acknowledge and HALT cycles | S3527 keys its wait on /M1 (2, by inference); colecovision-python's tracings say every M1 cycle; MAME (as reported there) not on acknowledges | every M1 cycle; switch; MAME's mode for the lockstep | | U2 | Z80 clock from the VDP's CPUCLK or a separate oscillator, per row | service manuals (none read) | locked (HYPOTHESIS) | | U3 | A write to FFFF in an expanded slot: does the selected sub-slot's device also see it? | none read | no | | U4 | Open bus value for empty slots and undecoded I/O | none read | FFh (HYPOTHESIS) | | U5 | Memory mapper read-back of unused bits, per engine | S1985 (not yet read for it) | per row | | U6 | I/O mirrors decoded per machine | none read | the TH2 block (98-9B, A0-A3, A8-AB) | | U7 | Extra waits on VDP I/O | S1985 strap X7 (2); VATT "Add. waits" (3) | 0 on MSX1 rows | | U8 | The engines' YM2149 divider pin | S3527/S1985 (not found) | 1.79 MHz effective | | U9 | Mix levels: PSG, key click, SCC, FM, tape monitor | none | FITTED (good-enough rule) | | U10 | Keyboard rows 11-15 | TDB shows a 4-to-10 decoder | FFh | | U11 | Keyboard ghosting (diodes) per machine | none | none (switch) | | U12 | Plain ROM mirroring of 8/16 KB images | cartridge decode varies | place by header, no mirrors (MAME-like); per-image table entries | | U13 | Mapper registers at power-on | Young assumes 0,1,2,3 | Young's | | U14 | Mapper decode width; reads at switch addresses | MEGAROMS ranges | ranges as given; reads return ROM | | U15 | SCC: a waveform write while playing; output stepping | none | sample-and-hold (HYPOTHESIS) | | U16 | SCC deformation register in full | Young's sampling (3, partial) | Young's partial reading; unknown bits no-ops | | U17 | SCC behaviour at very small periods | none | the formula | | U18 | Each maker's disk interface map | MAME and Clock Signal (5) | Philips-style first | | U19 | The `.cas` container's definition | community convention | as emulators read it, documented when found | | U20 | PPI port A before the BIOS's first write (82C55A bus-hold pulls inputs high = slot 3 everywhere; the BIOS must start in slot 0) | 82C55A sheet (2); machines (none read) | slot 0 for every page at reset (the boot ROM must be reached), recorded as a contradiction with the CMOS part's reset state to settle per row (an NMOS 8255, pull-downs, or the engine's own reset) | ## Contradictions (C) | # | Conflict | Sides | Proposal | | --- | --- | --- | --- | | C1 | The key matrix | TDB p. 13 (drawing, row 0 = 7..0) vs TH2 figs. 5.16-5.17 as transcribed (row 0 = B L / 1 S X ,) | TDB; report the transcription error to Konamiman's repository (an outside post: Aubrey's call) | | C2 | Cassette read bit | TDB p. 44: PSG port A bit 7 (input) vs TH2 5.2.2 text: "port B ... register 15" | TDB | | C3 | What C-BIOS supports | its website and `doc/cbios.txt`: cartridges only, no disk/BASIC/cassette vs the 0.32 release: tape routines work, a WD2793 disk ROM and a minimal BASIC ROM ship | the release; test what it does | | C4 | Where the M1 hook is | emulator-lineage families.md: "M1 hook added for ColecoVision" vs z80-python's `main` (no `read_m1`; the hook is on branch `m1-fetch-hook-rebased`) | decision 2; the families.md row is a fact to correct (reported, not edited here) | | C5 | M1 wait on acknowledges | as U1 | as U1 | | C6 | PPI reset state vs booting from slot 0 | as U20 | as U20 | ## Open decisions for Aubrey (each with a recommendation) 1. **Scope and first machine.** Recommend MSX1 first with the National CF-2700 (NTSC, TMS9918A, AY-3-8910A; VATT-measured; MAME trace taken) and a `cbios-msx1` row; then the order in machines.md: V-20 and HB-75P rows, mappers/SCC/FM-PAC, tape and disk, MSX2, MSX2+, turbo R last. 2. **z80-python's M1 hook onto `main`.** The hook (branch `m1-fetch-hook-rebased`, `5434349`) has two consumers now (ColecoVision, MSX). The branch is already on GitHub (`origin/m1-fetch-hook-rebased` = `5434349`) with no pull request (checked 2026-11-26). z80-python is public, so the PR and merge are your call. Recommend merging it to `main` (and carrying it to z80-rust and -fast in turn) so both boards pin `main`: `gh pr create --repo alewman/z80-python --base main --head m1-fetch-hook-rebased --title "Add the read_m1 hook (issue 0001)"`. 3. **The V9938/V9958.** Recommend its own repository `v9938-python` with the V9958 as a row, not an extension of tms9918-python (different silicon and schedule; tms9918-python's consumers.md already chose this; the Geneve 9640 and Super Free Kick are users outside the MSX). Its own Phase 0 when MSX2 is next. 4. **TMS9118/9128/9129 rows in tms9918-python.** 43 of MAME's MSX1 rows carry them. Recommend raising the request upstream now (fix starts upstream); not needed for the first row (a TMS9918A). 5. **C-BIOS.** Recommend using it as the board's free boot path in tests and CI (fetched into `reference/`, never committed), and allowing hosts (a future play-msx) to bundle it with its BSD-2 notice so the site runs without maker firmware. A policy call: it is firmware, though not a maker's. 6. **The 8255.** sms-python is its first family user, the MSX the second: by the charter, extract `i8255-python` from sms-python with history and pin it from both. Recommend doing so before msx-python's milestone 1 (small; a data sheet judges it). 7. **The R800 (turbo R).** Recommend a separate CPU core `r800-python`, Phase 0 only when a turbo R row is wanted (last); its timing is community-measured only. 8. **The SCC.** Recommend it stays in the board behind a pin-shaped class until a Konami arcade board needs `k051649-python`. 9. **The uPD765.** When a Panasonic disk or turbo R row starts, the TC8566AF makes the MSX the uPD765's second family user (sms-python's SF-7000): recommend extracting `upd765-python` then. 10. **The msx.org wiki.** Its terms forbid any re-use; it has no AI statement. Recommend citing facts from it only where TDB/TH2/data sheets do not cover them, never copying or pinning it. 11. **sources.md rows** (emulator-lineage): recommend adding the twelve rows proposed in [sources.md](sources.md) (C-BIOS, the ASCII books, msx.org, the MSX Assembly Page, Sean Young's pages, VATT, ESE-VDP/OCM, MSX1_MiSTer, WebMSX/fMSX/blueMSX, the firmware disassemblies, and the MAME-lists and Clock Signal rows' new users), and correcting families.md's Z80 row (C4). 12. **Disk in the specification.** Recommend the FDC at its registers with sector images, and no DSKIO patch in `msx-python` (a host convenience at most, as a named concession downstream).