# Handoff: build `zxspectrum-python`, the ZX Spectrum board ## Context you are inheriting Chips are cores, boards compose cores; the board pins its cores by commit and raises their corners there. This board embeds `../z80-python` and, on the 128K models, `../ay8910-python`; the +3 adds a uPD765A that today lives inside `../sms-python`. The Spectrum is era 1 (charter): one domain, cycle level, Python runs the tests and Rust plays. Its one hard problem is **contention**: the ULA stops the Z80's clock in the middle of instructions, by address, by T-state, by model, and the tests that judge it were verified on real machines. Nearest models in the family: `colecovision-python` (a Z80 board on z80-python, with the M1 hook and a measured speed band), `sms-python` (Z80, the SF-7000's uPD765A), and `a2600-python` for a video chip that owns the CPU's timing. Rules: Python >= 3.11, CPython and PyPy; MIT; nothing read from GPL emulators or GPL test sources (Fuse, Rak's Timing Test: closed boxes, emu-tools-gpl); no ROM, tape, snapshot or disc in the repository (MAME screenshots stay in git-ignored `reference/mame/`); `MAME_ROMPATH`, `NO_INTRO_DIR`, `SPECTRUM_TOSEC_DIR`; local git only; no sweeps. ### Read in this order README.md; docs/start-here.md; docs/ula-and-contention.md; docs/cpu-core.md; docs/floating-bus-and-snow.md; docs/memory-and-models.md; docs/keyboard-border-sound.md; docs/tape.md; docs/fdc-upd765.md; docs/firmware.md; docs/domains-and-speed.md; docs/validation.md; docs/mame-oracle.md; docs/census.md; docs/sources.md; docs/provenance.md. Then FAQ48's contention sections in `reference/docs/` and the collection's page for each test as its rung comes up. ## Scope In: 16K/48K (Issue 2 and 3 behaviour of port FE), 128K, grey +2, +2A/+2B, +3/+3B: the ULA and gate arrays (video, border, contention, contended I/O, floating bus, snow, /INT, early and late timing), memory and paging, port FE (keyboard matrix with ghosting, EAR, MIC, beeper), the AY through ay8910-python, tape playback (TAP, TZX 1.20) at real speed through EAR, snapshots (.z80, .sna, .szx as loaders for tests), the +3's uPD765A with DSK images. Out (for now): clones (Timex, Pentagon, Scorpion, Inves); Interface 1/2, microdrives, +D/DISCiPLE, Beta disk (TR-DOS); joysticks other than through the keyboard rows (a socket later); the 128K snow crash; the +2A/+3 audio distortion and other analogue output stages; IPF until decision 6. ## Proposed names - Package **`zxspectrum_python`**; `zxspectrum_python.Spectrum(model, rom=..., issue=3, ula_timing="early")`, with `model` in `"16K"`, `"48K"`, `"128K"`, `"PLUS2"`, `"PLUS2A"`, `"PLUS3"` and the ROM checked against firmware.md's SHA-1s. - Modules: `models.py` (the per-model table: clocks, frame, contention origin and pattern, contended banks, decodes, floating bus, snow, port FE bit 6), `ula.py` (frame counters, fetch, picture, floating bus, snow, /INT; one class with the model table, since the Ferranti ULAs and the Amstrad gate arrays differ in parameters, not in structure), `contention.py` (the delay function and the per-instruction schedule from z80-python's timing table), `memory.py` (ROM, RAM banks, paging ports), `keyboard.py`, `tape.py` (`TapPlayer`, `TzxPlayer`: EAR level as a function of wall time), `snapshot.py`, `fdc.py` (wraps `upd765_python` after decision 3; drives and DSK images), `machine.py`. - Sockets (pin-shaped, so later extraction is a move): the expansion bus (Interface 1, Kempston), the AY's port A (RS-232/MIDI/keypad), the +3's disc drives, the tape deck. ## Import boundary The board imports `z80_python` (Z80CPU, `timing.instruction_timing`), `ay8910_python` (PSG, AY8912) and, after decision 3, `upd765_python`. The cores never import the board. The board owns all contention: the CPU's callbacks see addresses; the board decides delays and keeps the clock. ## Unknowns (U) and contradictions (C) - U1 ULA and gate-array part numbers per board issue (40056 vs 40077 vs Ferranti parts on late +2s) - start-here.md. - U2 The AY ports' exact decode on each model - start-here.md. - U3 How z80-python's table-plus-callbacks drive will express internal cycles until N1 lands - cpu-core.md. - U4 Whether 7FFD and 1FFD are I/O-contended on the +2A/+3 (FAQ128: "currently unknown") - ula-and-contention.md. - U5 The +2A/+3 floating bus outside the fetch cycles and under each paging state - floating-bus-and-snow.md. - U6 The +3's FDC wiring: INT, TC, the second unit-select line - fdc-upd765.md. - U7 The /INT pulse length per model, and the acknowledge's access timing (N3) - cpu-core.md. - U8 Whether MAME's drivers contend at all (not used as evidence; checked only if a rung wants MAME as a detector) - mame-oracle.md. - C1 +2A/+3 contention origin: 14,365 (FAQ128) vs 14,361 (Wiki), and the +3 fetch column at 14,367 (collection) - ula-and-contention.md, floating-bus-and-snow.md. - C2 Early/late timing: unexplained (FAQ48, 2004) vs heat drift (Wiki) vs /RFSH-dependent /INT, temperature-dependent in either direction (collection, 2025) - ula-and-contention.md. - C3 128K I/O contention for ports C000-FFFF with a contended bank paged: never (FAQ48) vs applied (Wiki) - ula-and-contention.md. - C4 128K contended banks 1, 3, 5, 7 (Wiki, observed) vs 4-7 (service manual, per the Wiki) - memory-and-models.md. - C5 Reading 7FFD on a 128K: floating bus, harmless (FAQ128) vs crash through the HAL10H8 (Wiki) - memory-and-models.md. - C6 T-state numbering origin: first /INT-low T-state is 0 (Wiki) or 1 (others) - start-here.md. - C7 FAQ48's BIT b,(ii+n) row lacks the prefix's pc:4 (a slip by its own notes). - C8 The die repository's simplified C++ arbiter requires MREQ; our reading of its gate list and the FAQ/Wiki/tests say internal cycles are contended too - ula-and-contention.md. - C9 +2A/+3 floating bus: none, always FF (FAQ128) vs present (Float+3, Hikaru's test, the collection's +3 table) - floating-bus-and-snow.md. - C10 The +3 manual calls the gate array "the ULA" (naming only). - C11 (resolved) FAQ48 says TZX states 8,064/3,220 pilot pulses; TZX 1.20 says 8,063/3,223, as FAQ48 does. - C12 The +3 manual's AY register list prints R7 bit 3 as "Channel C noise" (bit 3 is channel A noise). - C13 The T-states after an opcode fetch (INC dd, ADD HL,dd, LD A,I, PUSH, RET cc): part of the fetch, uncontended (FAQ48) vs contended with IR on the bus (Wiki) - ula-and-contention.md. - C14 EX (SP),HL write order: SP first (FAQ48) vs SP+1 first (Wiki, and z80-python after alewman/z80-python#8) - ula-and-contention.md. ## Milestones M1 48K boots to BASIC (R0), with frame timing and /INT (R1). M2 contention from the timing table plus callbacks, exact against the published breakdown (R2), with the internal-cycle entries from z80-python's extension (N1) or, until it lands, from a board-side table derived from FAQ48 and the Wiki and marked DOC. M3 the hardware-verified 48K suites (R3), early and late. M4 tape through EAR (R6). M5 128K/+2 with the AY (R4 part, R7). M6 floating bus and snow (R5). M7 +2A/+3 gate array and paging (R4). M8 +3 FDC with DSK (R8). M9 seeded faults (R9). Exit tests are the rungs in validation.md. ## Domains and speed (docs/domains-and-speed.md in full.) - **Domains:** one main domain (Z80, ULA/gate array, memory, paging, port FE, tape, beeper) because the ULA sets the length of every Z80 T-state; the AY a second domain (channel: FFFD/BFFD; writes logged, a read of FFFD is a join); the +3's FDC and drive stepped inside the main domain in the order of record (channel 2FFD/3FFD and 1FFD bit 3). - **Need, with arithmetic:** 69,888 T x 50.08 frames/s = 3.5 M T/s (48K); 70,908 x 50.02 = 3.547 M T/s (128K models). At 7-9 T per instruction that is about 390,000-500,000 instructions/s; the ULA adds 56 cells x 312 lines x 50 = 875,000 cell updates/s and 0.6-1 M contention lookups/s; the AY 222,000 internal steps/s. - **No-JIT path:** reaches real time everywhere. Estimates anchored on colecovision-python (a 3.58 MHz Z80 board: 0.3-0.5 x real time CPython, 0.6-2.9 x PyPy, MEASURED) and z80-wasm (a 3.072 MHz Z80 board at full speed in the browser, live): x-python on PyPy about 0.5-2 x real time; x-rust above real time on desktop, phone and browser; x-rust-fast with room for run-ahead and rewind. `recompile: none`. No threads needed; a turbo tier only if a weak target's run-ahead measures the need. ## Open decisions for Aubrey 1. **Machine order: 48K first, then 128K/+2, then +2A/+3 (+3 disc last).** Recommended: the 48K has the strongest evidence (the die netlist, FAQ48's breakdown, the most hardware-verified tests) and the largest library (about 4,950 48K snapshots and most of the 4,420 TAPs); the 128K adds paging and the AY (already a core) with the same ULA design; the +2A/+3 changes contention and adds the FDC, and its evidence has the most contradictions (C1, C9, U4-U6). The 16K is the 48K with less RAM and comes free. 2. **Contention through z80-python's access-timing table plus callbacks, not a cycle-stepped Z80.** Recommended (cpu-core.md): contention only delays, so the k-th callback at the table's offset plus earlier stalls gives every access's T-state. Ask z80-python (public: Aubrey's call) for the extension N1 (internal cycles with their address source, derived from SST's per-T-state addresses and judged by FAQ48 and the 48K tests) and N3 (interrupt acknowledge timing). Until N1 lands, the board keeps a table of internal cycles derived from FAQ48 and the Wiki, tagged DOC, and drops it when the core's table matches it. 3. **Extract the uPD765A from sms-python into `upd765-python`, with history, before M8.** Recommended: the chip rule (a chip a second family board needs becomes its own core); the SF-7000 model is close in scope (polled, single-sided 3-inch). sms-python then pins it. Alternative: the +3 copies nothing and waits; not recommended, as two uPD765A models would drift. 4. **The ULA and gate arrays stay inside the board.** Recommended: no other family machine uses them (the Timex and clone ULAs differ and are out of scope). 5. **Amstrad's ROM statement: record it, keep shipping nothing.** Recommended: the statement is a 1999 policy post (scope: the 48K/128K/+ ROMs only, no fee for the ROM code, keep the copyright messages, credit line welcome), not a licence, and the current holder (reportedly Sky after BSkyB bought Amstrad in 2007) has not been asked. The board loads the player's ROM by SHA-1 like every other board. If a future play-zx site wants the 48K ROM built in, Aubrey decides then, after asking the current holder; the credit line in firmware.md is what the statement asks for. 6. **IPF (No-Intro's 127 +3 discs): defer; DSK first.** Recommended: TOSEC's 866 DSK images are sector-level and need only our own parser; IPF needs a flux decoder whose reference library (SPS's CAPSImg) has its own licence, to check before any use, and which would be a closed box (or an emu-tools tool) until checked. 7. **Early/late timing as a ULA setting, early by default.** Recommended: FAQ48, the Wiki and the collection agree machines differ by one T-state; the mechanism is contested (C2), so the board exposes the observable (`ula_timing="early" | "late"`) and records the /RFSH rule as HYPOTHESIS until a distinguishing test is run. 8. **Test programs with no stated licence: run only, nothing committed (as for amidog's PS1 tests); GPL ones (Rak, Fuse Test, four of Woodmass's) only from emu-tools-gpl.** Recommended. Optionally Aubrey (not an agent) could ask Mark Woodmass and azesmbog whether their tests may be redistributed in CI, since the collection already hosts them. 9. **Chris Smith's ULA book: buy a copy for facts, or do without.** Recommended: buy it if C2 and C8 stay open after M3. It is the deepest description of the 5C/6C ULA's contention logic; facts only, cited by page, never copied, and Smith's wishes on AI use to be checked first. The die netlist and the tests may make it unnecessary. 10. **New environment variable `SPECTRUM_TOSEC_DIR`** for the folder holding the TOSEC bundles, added to the family's environment file. Recommended (census.md). 11. **Proposed sources.md rows** (docs/sources.md, 14 rows) for the register's owner to add.