# Handoff: build `cpc-python`, the Amstrad CPC 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 `../ay8910-python`; it keeps the Gate Array, the 6845 and (until decisions 5-6) the 8255 and the uPD765A inside. The CPC is era 1 (charter): one domain, cycle level, Python runs the tests and Rust plays. Its two hard problems are **the Gate Array's /WAIT**, which aligns every Z80 bus cycle to the 1-µs grid, and **the five CRTC types**, whose counter rules differ in ways demos depend on and which the CRTC Compendium measured on real machines. Nearest models in the family: `zxspectrum-python` (Phase 0, 2026-11-26: a video chip that stretches the Z80's accesses, applied from z80-python's access table plus callbacks; the same plan works here), `colecovision-python` and `sms-python` (Z80 boards; sms-python holds the family's 8255 and uPD765A), `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 HDL (MAME's CPC driver, Caprice32, AMSGateArray's Verilog: closed boxes) nor from unlicensed ones (Amstrad_MiSTer); no firmware, disc, tape, snapshot or cartridge in the repository; `MAME_ROMPATH`, `NO_INTRO_DIR`, `CPC_MIRROR_DIRS`; local git only; no sweeps. **Attribution:** every module that implements CRTC or Gate Array behaviour starts with the line `# Technical information sourced from the "Amstrad CPC CRTC Compendium" by Longshot (CC BY-NC-ND).` and any host's credits carry it too (provenance.md; decision 9). ### Read in this order README.md; docs/start-here.md; docs/gate-array.md; docs/crtc-6845.md; docs/cpu-core.md; docs/ppi-psg-keyboard-tape.md; docs/memory-roms-expansion.md; docs/fdc-upd765.md; docs/plus-asic.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 SOFT 968 Appendix XII (`reference/docs/cpctech/manual_s968ap12.pdf`, 15 pages) in full, and the Compendium (`reference/docs/shaker/ACCC1.11-EN.pdf`) chapters 4, 26 and 27 before M2, the type chapters before M4-M5. ## Scope In: CPC464, CPC664, CPC6128 with CRTC types 0, 1, 2 and Gate Arrays 40007/40008/40010 as parameters: Gate Array (modes 0-3, palette, border, mode change at HSYNC, interrupt counter, ROM enables, /WAIT), CRTC per type, memory and the 6128's banking, upper ROM select, PPI, AY-3-8912 through the PPI, keyboard and joysticks, cassette from CDT at real speed, printer latch, the disc interface with DSK and extended DSK, SNA snapshots as test loaders, the expansion socket. Later rows: the cost-down 464/6128 (ASIC 40226, CRTC 4); the 464 Plus, 6128 Plus and GX4000 (ASIC 40489, CRTC 3, cartridges, plus-asic.md). Out: the KC Compact; IPF (decision as in zxspectrum-python); analogue video and audio stages; light pen hardware (the socket only). ## Proposed names - Package **`cpc_python`**; `cpc_python.CPC(model, crtc_type=0, gate_array="40010", links=..., rom=..., amsdos=...)` with `model` in `"464"`, `"664"`, `"6128"`, ROMs checked against firmware.md's SHA-1s, and `links` the PPI port B bits 4-1 (name and 50/60 Hz). - Modules: `gate_array.py` (sequencer phase, WAIT, fetch, pixel decode, palette, RMR, interrupt counter; part table for 40007/40008/40010), `crtc.py` (one 6845 class plus a per-type rule table; pin-shaped: `clock()`, MA/RA/DISPEN/HSYNC/VSYNC/CURSOR, LPSTB; select/write/read/status), `wait.py` (the per-access alignment from z80-python's table), `memory.py` (ROM/RAM map, upper ROM number, 6128 PAL), `ppi.py` (until `i8255-python`), `keyboard.py`, `tape.py` (CDT player: level as a function of T-state), `fdc.py` (until `upd765-python`; drives and DSK images), `snapshot.py` (SNA v1-3), `expansion.py` (socket), `machine.py`. - Sockets: the expansion port (bus cycles, ROMDIS, RAMDIS, /INT, /NMI, /EXP, LPSTB), the printer port, the tape deck, two disc drives, joysticks (rows 6 and 9), and later the Plus cartridge port. ## Import boundary The board imports `z80_python` (`Z80CPU`, `timing.instruction_timing`) and `ay8910_python` (`PSG`, `AY8912`); after decisions 5 and 6, `i8255_python` and `upd765_python`. The cores never import the board. The board owns the WAIT alignment: the CPU's callbacks see addresses; the board decides waits and keeps the clock. ## Unknowns (U) and contradictions (C) - U1 The Gate Array sequencer's phases: which of the four T-states is free, relative to the CRTC clock edge, the video fetch and the AY clock (Gerald 40010's sequencer sheet; ACCC 4.4.4 diagram A) - gate-array.md. - U2 Whether the per-access WAIT rule (W1) reproduces ACCC ch. 26 for every instruction (R2 decides) - cpu-core.md. - U3 The IM 2 vector byte on a floating bus (FF assumed) - gate-array.md. - U4 The 27 colours' output levels - gate-array.md. - U5 The interrupt rules behind C2 and C3 at die level - gate-array.md. - U6 No die record for any 6845, the 40007/40008, the 40226 or the 40489 - crtc-6845.md. - U7 SHAKER's real-machine photos: their exact terms and a comparable form (photos of a CRT, not pixel dumps) - crtc-6845.md, validation.md. - U8 The NMOS 8255's port reads around direction changes (no bus-hold) - ppi-psg-keyboard-tape.md. - U9 The AY with a BDIR strobe longer than ap12 C's 10 µs - ppi-psg-keyboard-tape.md. - U10 The AY's stereo wiring - ppi-psg-keyboard-tape.md. - U11 The keyboard row decoder for values 10-15 (rows 11-14 read FF, KT) and ghosting. - U12 A pinnable copy of the standard DSK format description - fdc-upd765.md. - U13 The 6128 PAL's decode of bank-register bits 5-3 - memory-roms-expansion.md. - U14 The Gate Array/PAL decode with A14 = 0 per machine (ACCC 5.1) - memory-roms-expansion.md. - U15 The Plus cartridge protection ("ACID" 40908) - plus-asic.md. - U16 What the AY's /RESET and the 6128's bank register do at system reset - memory-roms-expansion.md. - U17 Locomotive Software's position on the ROMs - firmware.md. - C1 RMR function bits: `10` (ap12, ACCC, MAME) vs KT's table printing `11`/`01` (a slip) - gate-array.md. - C2 The VSYNC interrupt condition: interrupt only if counter bit 5 = 1 (ACCC 27.3.2) vs no interrupt if counter >= 32 (KT `ints`) - gate-array.md. - C3 RMR bit 4: clears the counter's top bit (ap12 E) vs resets the counter (ACCC 27.2, KT) - gate-array.md. - C4 Which cost-down machines carry the 40226 (KT `crtcnew` vs KT `cpcptno` vs ACCC) - crtc-6845.md. - C5 Type 2's readable registers: R14-R17 (ACCC 4.3, Motorola's table) vs R16-R17 only (ACCC 28.1.9) - crtc-6845.md. - C6 FDC density: single and double (ap12 I) vs double only, MFM tied (KT `fdc`) - fdc-upd765.md. - C7 KT `crtcnew`'s address table lists A15 twice and its port text says bits 1-0 for A9-A8 (typos; ap12 B and ACCC agree with the reading used) - crtc-6845.md. - C8 MAME's instruction lengths (11, 14, 14 T-states for the first three firmware instructions; 30 % not multiples of 4) vs ACCC ch. 26 (3, 4, 3 µs): the detector disagrees with the judge; MAME is not used for timing - cpu-core.md. ## Milestones Exit tests are the rungs in validation.md. - **M1** 464 model (64 KB, tape) with a 40010 and CRTC type 0 boots to "Ready" (R0), with the firmware's frame and six interrupts per frame (R1). - **M2** The WAIT alignment from z80-python's table plus callbacks, exact against ACCC ch. 26 and 4.4.3-4.4.4 (R2); W2's acknowledge timing from a board-side table until z80-python publishes it. - **M3** Gate Array picture: modes, palette, mode-change timing, per-part pixel phase (R3). - **M4** CRTC type 0 rules and SHAKER on type 0 (R4). - **M5** CRTC types 1 and 2 (R5). - **M6** PPI, AY, keyboard, tape from CDT (R6). - **M7** 6128: banking, upper ROMs, expansion socket (R7); the 664 comes free. - **M8** Disc: AMSDOS boots DSK images (R8), after decision 6. - **M9** Seeded faults (R9). - **M10, M11** Later rows: cost-down (type 4), Plus/GX4000 (type 3, ASIC). ## Domains and speed (docs/domains-and-speed.md in full.) - **Domains:** one main domain (Z80, Gate Array, CRTC, memory, PPI, keyboard, tape), because /WAIT and the CRTC's syncs couple everything within each microsecond; the AY a second domain (channel: PPI port C strobes with port A's latch; a read needs only the register file and the keyboard, both main-side); the FDC stepped inside the main domain in the order of record (AMSDOS polls; no INT, no DMA). The Gate Array's pixel decode is a producer fed by a per-µs log. - **Need, with arithmetic:** 4 M T-states/s, but whole-microsecond instructions (ACCC ch. 26) at 2.5-3 µs typical: **330,000-400,000 instructions/s** (HYPOTHESIS on the mix), 0.5-0.8 M WAIT computations/s; CRTC 64 x 312 x 50.08 = 1,000,000 character clocks/s; Gate Array 1,000,000 two-byte fetches/s (16 M mode-2 pixel slots/s, ~0.7 M visible byte-pair decodes/s by table); AY 1 M input clocks/s, 125,000 steps/s; FDC 31,250 bytes/s when reading. - **No-JIT path:** reaches real time everywhere. Anchored on colecovision-python (0.3-0.5 x real time CPython, 0.6-2.9 x PyPy, MEASURED) and z80-wasm (3 MHz Z80 board live in the browser): x-python about 0.1-0.3 x CPython, 0.3-1 x PyPy (estimate: more work per µs than the ColecoVision); x-rust above real time on desktop, phone and browser; x-rust-fast with room for run-ahead 1-2 and rewind. `recompile: none`. No threads needed (picture and AY producers available). Turbo (whole-line paths when no CRTC/Gate Array register changes mid-line) only if a weak target's run-ahead measures the need. ## Open decisions for Aubrey 1. **Kevin Thacker's pages, Amstrad's SOFT 968 scans and Gerald's 40010 schematic: keep as used, or set aside.** KT's site never had a robots.txt; its 2024 Wayback copies pre-date a generic (not AI-specific) hosting bot check seen in 2025 snapshots; the archive now lives on cpcwiki.eu, which blocks AI crawlers. SOFT 968's scans came from KT's site the same way. Gerald's schematic was first posted on the cpcwiki forum; the copy used is on GitHub. **Recommended: keep all three** (no author has objected; the cpcwiki rule is that site's rule about its own pages, and none was fetched from it), and record the judgement in sources.md. If you read it otherwise, the fallback is the Compendium plus SOFT 968 from another host, and the die record is lost. 2. **The 6845 stays in the board**, pin-shaped with a per-type table; extract `crtc6845-python` when a second family board (BBC Micro, PC CGA/MDA) needs it. Recommended (chip rule: no other family board uses a 6845 today). Types 3 and 4 are Amstrad ASICs and would remain CPC variants. 3. **CRTC type and Gate Array part are machine parameters**; defaults: type 0 (HD6845S) and 40010 for every model. Recommended: type 0 is the type the firmware was written for (ACCC 4.2: the ROM programs R3's VSYNC width, which only types 0, 3, 4 honour; the 464 prototype Roland Perry showed in September 2023 confirmed a type 0), it is what MAME fits, and the 40010 is the only Gate Array with a die record. Types 1 and 2 follow in M5. 4. **Machine order: 464 software model first (M1-M6), then 6128 (M7-M8), the 664 free.** Recommended: the 464 boots without the FDC and banking, so M1 tests the Gate Array, CRTC and WAIT alone; the 6128 adds banking and the disc on the same chips. 5. **Extract `i8255-python` from sms-python with history** (msx-python's decision 6 too). Recommended: the CPC is the third family user; the CPC needs mode 0 only, with the latch clearing on a control write (ACCC 29.1.2) that sms-python already has; the Plus's imperfect copy becomes a variant. 6. **Extract `upd765-python` from sms-python with history, before M8** (zxspectrum-python's decision 3 too). Recommended: four boards want it (SF-7000, +3, MSX, CPC). 7. **Ask z80-python (public: your call) for W2**, the interrupt acknowledge's access timing, and optionally W1's `fetch` kind. Recommended: the Spectrum asked for the same (its N3); the CPC keeps a board-side table until it lands and drops it when the core's matches. 8. **Amstrad's ROM statement: record it, keep shipping nothing.** Recommended: it covers the CPC ("applies equally well to all the CPC stuff") but leaves Locomotive's share to Locomotive, and the current holder has not been asked; the board loads the player's ROMs by SHA-1. A future play-cpc site that wants built-in ROMs is a separate decision. 9. **Accept the Compendium's attribution directive.** Recommended yes: one comment line in each CRTC/Gate Array module and a credits line in hosts; it is what Longshot asks of AI-assisted work and costs nothing. (Its CC BY-NC-ND licence binds the document, which we never copy or redistribute; the facts we take are not restricted, but the credit is owed by courtesy and asked for explicitly.) 10. **No CPC library on the mirror.** Recommended: add the variable `CPC_MIRROR_DIRS` to the family's environment file now (the collections on hand give a ~50-title smoke group); decide on a full library (e.g. a TOSEC CPC set) only when the board reaches M8. 11. **SHAKER's real-machine photos as the CRTC judge.** Recommended: pin them (run-only, facts-only, never committed) once their terms are read from the portal; optionally you (not an agent) could write to Logon System, who invite emulator authors to submit for a "SHAKER approved" seal, which would also give an outside verdict on the board. 12. **zxspectrum-python's World of Spectrum pins** fall under today's robots.txt rule (WoS blocks ClaudeBot and others). Recommended: re-source them in that repository (the Amstrad statement from DeZog, as here); not changed from this repository. 13. **Proposed sources.md rows** (docs/sources.md, 15 rows) for the register's owner to add.