# Handoff: build `samcoupe-python`, the MGT SAM Coupé 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` (a 6 MHz Z80B) and keeps two chips of its own, the MGT ASIC and the Philips SAA1099, plus one WD1772-02 model per disk drive that should become (or come from) the shared `wd177x-python` the ST, BBC, CoCo and Oric Phase 0s proposed. The SAM is era 1: one main domain, cycle level, Python runs the tests and Rust plays. Its hard problem is **contention**: the ASIC delays the Z80's access to internal RAM and to the ASIC ports on a 4- or 8-T-state grid that depends on where the beam is, and mode 1 adds measured forced delays in alternating 8-cell blocks. Nearest models: `zxspectrum-python` (Phase 0 today: a Z80 board with contention, the SAM's mode 1 is Spectrum-compatible), `colecovision-python` (a built Z80 board with a measured speed band), `atarist-python` (the WD1772 survey). Rules: Python >= 3.11, CPython and PyPy; MIT; nothing read from GPL sources (SimCoupé, the SAM ROM source, MAME's SAM driver: closed boxes); no ROM or disk in the repository; `MAME_ROMPATH`, `NO_INTRO_DIR`, `REDUMP_DIR`; local git only; no sweeps. ### Read in this order README.md; docs/start-here.md; docs/asic.md; docs/cpu-and-reuse.md; docs/saa1099.md; docs/fdc-wd1772.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 TECH pages 13-22 and SO-M1 in `reference/docs/`. ## Scope In: the SAM Coupé with 256K or 512K internal RAM; the ASIC (modes 1-4, CLUT, border, SOFF, LPEN/HPEN, paging through LMPR/HMPR/VMPR, contention including mode 1, LINE and FRAME interrupts, STATUS, keyboard matrix with the ninth row, ATTRIBUTES port); the SAA1099; beeper and EAR/MIC; one or two slot-in drives (VL-1772-02 each) with `.mgt` images; ROM 3.0 by SHA-1 (others selectable). Out (for now): MIDI (a socket: the port and the STATUS bits exist, the serial timing waits), mouse (a socket), external memory packs (HMPR b7 raises a socket call), the SDI, printer, network, light pen beyond LPEN/HPEN's scan values, ATOM/Trinity hard-disk interfaces, copy-protected EDSK and flux images, the Spectrum ROM emulators (software). ## Proposed names - Package **`samcoupe_python`**; `samcoupe_python.SamCoupe(rom=..., ram_kb=512, drives=1)` with the ROM checked against firmware.md's SHA-1s. - Modules: `asic.py` (frame counters, display fetch per mode, CLUT, border, SOFF, LPEN/HPEN, interrupts, STATUS; one class), `contention.py` (the grid: region and mode lookups, wait for an access at T-state t of kind memory/io), `memory.py` (32 pages, LMPR/HMPR/VMPR, ROM0/1, write protect, external-memory socket), `keyboard.py`, `saa1099.py` (pin-shaped: open decision 3), `fdc.py` (wraps the WD177x model and a drive per slot), `disk.py` (`.mgt`), `machine.py`, `savestate.py`. - Sockets: expansion connector (external memory, MIDI, printer), mouse port, drive slots, tape. ## Import boundary The board imports `z80_python` (`Z80CPU`, `timing.instruction_timing`) and, after decision 4, `wd177x_python`. The cores never import the board. The board owns contention: the CPU's callbacks see addresses and ports; the board decides waits from the access offset table and keeps the clock. ## Unknowns (U) and contradictions (C) - U1 The contention grid's phase relative to the frame interrupt and the cell boundaries (SO-M1's diagrams are images; SO-T's table is SimCoupé output) - asic.md. - U2 Whether the ASIC delays only MREQ/IORQ cycles or also cycles with an address and no request - asic.md, cpu-and-reuse.md. - U3 The line on which the FRAME interrupt falls, and the border lines above and below the 192 - asic.md. - U4 How mode 1/2 attributes' FLASH and BRIGHT map onto the CLUT - asic.md. - U5 Page numbers above 15 on a 256K machine (mirror or open bus) - asic.md. - U6 Mode 2's extra slow-down - asic.md. - U7 The interrupt pulse length (TECH: ~20 us) and re-triggering - asic.md. - U8 The mouse protocol on port FE - asic.md. - U9 The SAA1099's exact port decode on A8-A15 - saa1099.md. - U10 Whether the SAA1099's DTACK reaches the Z80's /WAIT - saa1099.md. - U11 DOSBox's SAA1099 hardware findings and the crowd-sourced recordings SAASound cites: where they are, and their terms - saa1099.md. - U12 Whether the drive connector really carries no INTRQ/DRQ (pin list) - fdc-wd1772.md. - U13 What MAME's ROM 3.1 is - firmware.md. - U14 Whether MGT or a later owner holds rights in the ROM beside Andy Wright - firmware.md. - U15 The data bus during an IM 2 acknowledge - cpu-and-reuse.md. - C1 Port FE bit 7 on read: "SOFF ... status bit show if external memory is set" (TECH, a confused line) vs the screen-off state (UTM) - asic.md. - C2 Whether the samrom repository's GPL-2.0 covers the ROM binaries or only the source (ReadMe: binaries "released with kind permission") - firmware.md. - C3 WD1772 step rates: WD177X (preliminary) vs WD86 (production); follow WD86 (shared with atarist-python's C3). ## Milestones M1 ROM 3.0 boots to the MGT screen (R0) with the frame and interrupts (R1) and paging (R2). M2 video modes 1-4, CLUT, border, SOFF (R4). M3 contention from the access table and the grid, with the mode 1 blocks, judged by SO-M1 through our own border-bar program (R3). M4 keyboard and beeper; tape through EAR (optional). M5 disk: WD177x model and `.mgt` boot of SAMDOS (R5). M6 SAA1099 (R6). M7 seeded faults (R7), save states. Exit tests are the rungs in validation.md. ## Domains and speed (docs/domains-and-speed.md in full.) - **Domains:** M main (Z80, ASIC, memory, keyboard, beeper) because the ASIC sets every access's length; S the SAA1099, a pure producer (its ports are write-only: channel 1FF/FF, writes logged); F the disk controllers, stepped inside M at each port access (channel E0-E7/F0-F7; reads are joins); I MIDI and mouse, optional. - **Need, with arithmetic:** 6,000,000 T/s; a frame is 384 T x 312 lines = 119,808 T at 50.08 Hz. At 6.5-10 T per instruction (MAME's boot trace: 55,296 instructions in 359,419 T) that is **0.6-0.92 M instructions/s**; the ASIC 48 cells x 312 lines x 50 = 750,000 cell steps/s; the SAA1099 0.1-0.5 M internal steps/s; the WD1772 31,250 bytes/s while busy. - **No-JIT path:** trivial. About 1.7 x a ColecoVision's Z80 work: x-python on PyPy roughly 0.35-1.7 x real time (scaled from colecovision-python's MEASURED band: an estimate); x-rust and x-rust-fast well above real time on desktop, phone and browser (estimate, by comparison with the live z80-wasm board); `recompile: none`; no threads needed, S may run beside M if a phone's audio wants it. ## Open decisions for Aubrey 1. **Contention from z80-python's access-offset table plus a board-side grid, not a cycle-stepped Z80.** Recommended: the SAM's rules are per access (SO-T), the table on z80-python origin/main gives each access's offset, and the grid's phase is one fitted constant judged by SO-M1. Ask z80-python (public: Aubrey's call) for the interrupt acknowledge and HALT timing (N2), the same request zxspectrum-python makes. 2. **The SAM ROM: record its status, ship nothing from this MIT repository.** Recommended: the v3.0 source is GPL-2.0 "as agreed with Dr Andy Wright" (Simon Owen's commit, 2012; the permission e-mail is not public) and the binaries are distributed "with kind permission"; neither makes a ROM image MIT-compatible. If a future play-sam site wants a built-in ROM, it would be a **GPL** site (charter point 4) building ROM 3.0 from that source with the source offered, after Aubrey (not an agent) confirms the grant with Andy Wright or Andrew Owen; until then the player supplies the ROM by SHA-1. 3. **The SAA1099 stays inside the board** (`saa1099.py`, pin-shaped). Recommended: no other family board uses one today; the PC sound cards (Game Blaster, Sound Blaster 1.0) and the SGI and arcade users are possible second users later, when it is extracted with history. 4. **The WD1772: join the shared `wd177x-python` (atarist-python decision 6 and its siblings).** Recommended: whichever board reaches its disk milestone first writes the model behind a pin-shaped interface, the second extracts it; the SAM needs two instances and side select on A2, which the proposed instantiable chip-plus-drive shape already allows. 5. **Default ROM 3.0, not MAME's 3.1.** Recommended: 3.0 is the last official release; 3.1's origin is unknown (U13). 6. **MAME's BSD-3 SAA1099 and WD FDC files stay closed for now.** Recommended: their `license:` lines would allow reading with notice, but the data sheets cover what the board needs and keeping MAME closed keeps provenance simple (the same choice coco-python recommends); SAASound (BSD-3, pinned) is the sound comparator. 7. **A smoke list of SAM disks.** Recommended: none is on the mirror; ask Aubrey whether to pull a small set (World of SAM titles with recorded author grants, or the 29 titles named in MAME's CC0 software list) into a new `SAM_DISKS_DIR` outside the repository, after checking World of SAM's notice (no mirroring) with its owner. Until then the board's rungs use only the ROM and our own test programs. 8. **SimCoupé in emu-tools-gpl as the main detector.** Recommended: its contention model is the most worked-out one (its author measured the hardware), and its debugger reports per-instruction timing; built and run there, read only through its outputs. 9. **Proposed sources.md rows** (docs/sources.md, 13 rows) for the register's owner, including enterpriseforever.com as "set aside (site's wishes)".