# Handoff: build `coco-python`, the Color Computer 1/2 and Dragon 32/64 board ## Context you are inheriting Chips are cores, boards compose cores; the board pins its cores by commit. The CPU core is `m6809-python` (`../m6809-python`), which is **still Phase 0: documents and oracles, no code** (last commit b35c084). It must be built first, with the requests below, before this board's milestone 1. Era 1 (charter): one clock domain, cycle level everywhere, Python runs the tests, Rust plays. Nearest models in the family: `williams-python` (tonight's Phase 0: the same CPU, the same MC6821 PIAs, a MAME lockstep plan), `colecovision-python` and `msx-python` (home machines with cartridges, cassette and disk), `a2600-python` (one-domain, cycle-level video). Rules: Python >= 3.11, CPython and PyPy; MIT; XRoar, VCC, MAME and the MiSTer cores are closed boxes; no Color BASIC, Extended/Disk BASIC or Dragon ROM in the repository (SHA-1 only, from `MAME_ROMPATH`); software through `NO_INTRO_DIR`/`TOSEC_DIR`; local git only; no sweeps; at most one MAME process, short runs. ### Read in this order README.md; docs/start-here.md; docs/machines.md; docs/sam.md; docs/vdg.md; docs/pia-io.md; docs/cartridge-cassette.md; docs/disk.md; docs/firmware.md; docs/reuse.md; docs/validation.md; docs/mame-oracle.md; docs/census.md; docs/sources.md; docs/provenance.md; docs/gime.md (later row). Then `python -I scripts/fetch_references.py` (25 files; `--verify` checks them). The scans have no text layer: OCR them as this phase did (`ocrmypdf -j 2 --force-ocr --output-type pdf --sidecar out.txt in.pdf out.pdf`, under `nice`), and read figures as page images (`pdftoppm -r 110 -f N -l N`): the SAM's timing diagrams and the VDG's colour table are rotated or garbled in OCR. ## Scope In: the CoCo 1/2 board (MC6809E socket, MC6883 SAM, MC6847/T1 VDG, two MC6821 PIAs, the DAC, multiplexer and comparator, keyboard, joysticks, single-bit sound, cassette, cartridge port with ROM packs, Tandy's FD1793 disk interface), the Dragon 32/64 rows (PAL padding, printer port, Dragon 64 ACIA and ROM switch, DragonDOS), the CoCo 2B (MC6847T1), save states. Later: the CoCo 3 (GIME) after its own study; add-on cartridges (Orch-90, Speech/Sound, Multi-Pak); HD6309 rows. Out: the CPU core (m6809-python); a shared PIA or WD179x core if Aubrey decides to extract them (decisions 3 and 5); hosts, windows, sites. ## Proposed names - Package **`coco_python`**; `coco_python.ColorComputer(row="coco2", ram=65536, firmware=...)`, rows in `rows.py` ([machines.md](machines.md)): `coco1`, `coco2`, `coco2b`, `dragon32`, `dragon64`, `tanodr64`, `dragon200`, `dragon200e`, later `coco3`. - Modules: `machine.py` (the cycle loop), `sam.py` (control register, decode to S, rate and cycle length, refresh, the VDG address counter), `vdg.py` (MC6847 and T1 as one class with a variant), `pia.py` (or the shared core), `analog.py` (DAC, MC14529B, comparator, joystick voltages, the sound bus), `keyboard.py` (both matrices), `cartridge.py` (the `Cartridge` socket; `RomPack`, `BankedPack`), `cassette.py` (level streams, `.cas` synthesis, `.wav`), `disk/` (`tandy.py` latch and HALT/NMI gating, `dragondos.py`, `wd179x.py` or the shared core, `images.py` for `.dsk`/`.vdk`), `acia6551.py` (Dragon 64), `firmware.py` (the SHA-1 table), `domains.py` (the channels as code). - Sockets (pin-shaped, so later extraction is a move): CPU (m6809-python's bus callbacks and lines), `Cartridge` (reads/writes C000-FEFF and FF40-FF5F, CART level, SND voltage, HALT and NMI outputs, SLENB), joystick ports (four voltages, up to four buttons), cassette (level in, voltage out, motor), serial/printer (CoCo bit-banged RS-232 lines; Dragon parallel port), VDG output (indices plus burst flag and the power-up clock phase, for the host's artifact filter). ## Import boundary The board imports `m6809_python` (and `m6821_python`, `wd179x_python` if decisions 3 and 5 create them). The core never imports the board. The SAM is the board's heart: every CPU bus cycle goes through `sam.py`, which decides the device and the cycle's length; the VDG never reads memory itself, the SAM's counter feeds it. ## What the board needs from m6809-python (raise there first) Every E cycle as a bus callback with dummy cycles marked (the SAM's address-dependent rate sees them); HALT with its dead cycles; NMI and FIRQ entry; IRQ resampled each boundary; the 3.11 floor; the HD6309 mode later. Detail in [reuse.md](reuse.md); williams-python asks the same. ## Domains and speed - **Domains:** one, M: CPU, SAM, VDG, PIAs, DAC/mux/comparator, keyboard and cartridge ROM, all from the SAM's one crystal and interleaved inside every E cycle (the CPU half and the VDG/refresh half), so nothing can be separated without the per-cycle order. The disk drive (D) meets M at the FDC registers, DRQ (as HALT, every byte) and INTRQ (as NMI): stepped inside M at those points in the order of record. Tape (T) is a timestamped level stream; sound output is a pure producer of DAC and single-bit samples. Channels: FDC registers, DRQ/HALT, INTRQ/NMI (M-D); PIA1 PA0 and CA2 (M-T); the sound bus (M to host). - **Instructions per second:** E = 14,318,180 / 16 = 894,886 Hz; MAME's traces give 4.39 cycles per instruction while the firmware boots and idles (894,950 cycles / 203,671 instructions in 60 frames), so **~0.2 M instructions/s** in slow mode and at most ~0.4 M in fast or address-dependent mode; the VDG adds 15,734 lines/s (3,579,545 / 227.5) of 256 elements. - **No-JIT path:** trivial. The family's 6800 core runs 3-4.5 M instructions/s on CPython and 45-82 M on PyPy (m6800-python README), 8-400 times this need before board overhead; a Rust transcription is orders of magnitude above real time on desktop, phone and wasm. **`recompile: none` on every target; no cached interpreter, no threads, no `-fast` or `-turbo` unless a measurement (run-ahead on a weak device) says otherwise.** ## The gate (defined at milestone 1) Lockstep against MAME's `coco` (Color BASIC 1.2 + Extended 1.1, no cartridge) and `dragon32` traces from reset: instruction stream, registers, `totalcycles`, PIA/SAM/FDC write order, over 3 emulated seconds, 0 unexplained differences; unit tests of every documented fact; the board's own test programs under MAME as detectors; seeded faults from [validation.md](validation.md), each caught. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | **CPU socket, SAM (map type 0, slow rate, decode, refresh slots), PIAs, memory map and mirrors, VDG timing (HS/FS, no picture), `coco2` row** | SAM, PIA sheets (2); MAME trace (5) | Color BASIC 1.2 + Extended 1.1 reach `OK` with the stream equal to MAME's for 3 s or a named reason per difference (C2 will be one) | | M2 | **Pictures** | VDG sheet (2); MAME pictures (5) | text, SG4, SG6 and the eight graphics modes drawn; the SAM cross modes produce MAME's pictures; the board's own SAM/VDG test programs pass under MAME | | M3 | **Keyboard, joysticks, DAC and single-bit sound, the mux; ROM packs with CART autostart** | TRM (2); CA (1, Dragon); MAME (5) | a smoke list of 10 `coco_cart` titles (problem and classic) reaches gameplay; joystick reads by successive approximation give the set voltage to 6 bits | | M4 | **Cassette** | TRM format (2) | a `.cas` synthesised into PA0 loads through the real ROM's CLOAD and CLOADM; a `.wav` too | | M5 | **Disk: Tandy's FD1793 interface, `.dsk`** | WD sheet, DSM (2) | Disk BASIC lists, loads and saves on a `.dsk`, with HALT per byte and NMI at the end on the right cycles | | M6 | **Dragon rows** (32, 64 with ACIA and ROM switch, Tano, 200/200-E), PAL padding, DragonDOS and `.vdk` | CA (3); MAME (5) | both Dragons reach their prompt in lockstep with MAME (C1 decided first); DragonDOS reads a `.vdk` | | M7 | **CoCo 2B (MC6847T1), rate modes, map type 1, save states** | VDG-T1, SAM (2) | lower case and borders per the T1 sheet; address-dependent and fast modes time as the SAM sheet says; a save state at half way loads into a fresh machine and continues identically | | M8 | **CoCo 3** (after its own Phase 0 note, [gime.md](gime.md)) | Tandy's CoCo 3 service manual (2) | `coco3` reaches its BASIC prompt | Full sweeps only when the board might be ready, after the smoke list passes; a sweep's failures become the next smoke list. ## The chip rule | Chip | Other family users | Proposal | | --- | --- | --- | | MC6809E | Williams, Vectrex, many arcade boards | its own core, m6809-python (exists, Phase 0) | | MC6821 PIA | williams-python (3-4 per board), atari8bit-python (6520) | shared core (decision 3) | | WD179x | msx-python, bbcmicro-python, atarist-python, ti99-python | shared core (decision 5) | | MC6847 VDG | none in the family (MC-10, Atom, APF, Laser 310 are not family boards) | in the board (decision 4) | | MC6883 SAM, MC14529B, DAC, 6551 ACIA | none (the ACIA: apple2-python's Super Serial Card, not built) | in the board | ## Unknowns (U) | # | Unknown | Where | Best route | | --- | --- | --- | --- | | U1 | where each VDG line starts in E cycles (227.5 VDG clocks against a 4-clock E grid; the SAM's sync window) | sam.md | the SAM's rotated timing diagrams read as pictures; then a hardware probe (Aubrey's queue) | | U2 | map type 1 ROM/RAM double select on the CoCo (seen on the Dragon 32 by CA) | sam.md | schematics (SM1) | | U3 | the E stretch at a rate change | sam.md | SAM figure 8 as a picture | | U4 | which SAM part (LS783/LS785) on each board | sam.md | parts lists | | U5 | PIA output readback and flag timing against E | pia-io.md | shared with williams-python U10 | | U6 | the Dragon 32's PAL padding and HS during it | vdg.md | Dragon 32 schematic; probe | | U7 | PAL colour on the Dragon (LM1889) | vdg.md | schematic | | U8 | artifact hues and the power-up phase on a real CoCo | vdg.md | composite capture (probe queue) | | U9 | the CoCo and Dragon keyboard matrices | pia-io.md | TRM figure; a Dragon document | | U10 | the joystick comparator at equality and after a DAC write | pia-io.md | TRM circuit; probe | | U11 | the CoCo's sound-bus mixing levels | pia-io.md | probe | | U12 | how Disk BASIC is entered | cartridge-cassette.md | MAME trace of `coco` with the disk cartridge | | U13 | the cassette input's threshold and hysteresis | cartridge-cassette.md | TRM circuit values | | U14 | the banked ROM pack's bank register | cartridge-cassette.md | the three `banked_16k` titles' documentation | | U15 | DragonDOS's latch bits and HALT/NMI wiring | disk.md | Dragon Data's DragonDOS manual or schematic | | U16 | pinned descriptions of `.dsk` (JVC), `.vdk`, `.dmk` | disk.md | a licence-checked document | | U17 | the CoCo 2B board's other changes | machines.md | 26-3134B/3136B service material | | U18 | the Dragon 200-E's extra 4K ROM | machines.md | MAME's set as a hint; a Eurohard document | | U19 | no test programs with hardware verdicts | validation.md | the board's own (decision 9) | ## Contradictions (C) | # | Contradiction | Sources | Proposed resolution | | --- | --- | --- | --- | | C1 | Dragon crystal: 14.31818 MHz, "some Dragon 64s" 14.218 MHz (CA) vs every Dragon at 14.218 MHz (MAME 0.285: E 888,625 Hz) | CA (3) vs MAME (5) | CA is the better evidence; decision 11 | | C2 | VDG line 227.5 clocks (data sheet) vs 228 (MAME's htotal 456 half clocks, 59.92 Hz) | VDG (2) vs MAME (5) | the sheet, with U1 deciding how the SAM fits it to E; expect a named lockstep difference | | C3 | VDG left border 28.5 clocks (text) vs 29.5 (AC table); T1 sheet 29 | VDG (2) internal | the AC table, checked against the T1 sheet and MAME pictures | | C4 | LOM's semigraphics-4 colour 000 "cyan" (and its M0/M1 addresses given as FFD6-FFD9) vs its own colour list and the SAM sheet | LOM (3) vs VDG/SAM (2) | the manufacturer sheets | | C5 | CTS covers "C000 through FFEF" (TRM text) vs C000-FEFF (TRM map, SAM figure 14) | TRM (2) internal, SAM (2) | the SAM figure | | C6 | Tandy's disk interface has a WD1793 at 1 MHz (DSM) vs MAME's `coco_fdc` WD1773 at 8 MHz | DSM (2) vs MAME (5) | the DSM for the 26-3022 row; a later Tandy controller may differ (the CCA has "New FDC" schematics, not pinned) | ## Open decisions for Aubrey (each with a recommendation) 1. **Scope and order.** Recommend the CoCo 2 row first (Color BASIC 1.2 + Extended 1.1, 64K, MAME's `coco` default), then the Dragon 32/64 rows, the CoCo 2B, and the CoCo 3 last after its own short Phase 0 note. 2. **One repository for the CoCo 1/2 and the Dragon**, `coco-python` (package `coco_python`, class `ColorComputer`) with rows. Recommend: the same Motorola reference design; the differences are wiring, PAL padding and firmware ([machines.md](machines.md)). 3. **The MC6821 as a shared core** with williams-python (its decision 3, name `m6821-python` provisional). Recommend yes: one core extracted from whichever board builds it first; this board needs two instances and no machine logic inside. 4. **The MC6847 stays in this board** behind a pin-shaped class (no second family user). Recommend. 5. **A shared WD179x core** (`wd179x-python`, rows FD1771/FD1793/WD1773/WD2793/WD2797/ WD1772): four other Phase 0 boards tonight need a member. Recommend: extracted by the first board that needs it at command level; each board keeps its latch and wiring. 6. **MAME's BSD-3 CoCo/Dragon/VDG/SAM/PIA/WD files stay closed** (their `license:` lines would allow reading with notice). Recommend closed: the Motorola, WD and Tandy documents cover what they would add, and provenance stays simple. 7. **Artifact colours are a host display filter**, not a board observable, until a capture exists. Recommend: the board outputs element indices, the burst flag and the power-up clock phase; hosts offer off / red-blue / blue-red / composite. 8. **Software sets.** No CoCo or Dragon software is mirrored (No-Intro has no such DAT here; the local TOSEC tree is X68000 only). Recommend mirroring TOSEC's "Tandy Radio Shack TRS-80 Color Computer" and "Dragon Data Dragon" trees and setting `TOSEC_DIR` in the profile; until then the smoke list is built from MAME's CC0 lists' names. 9. **The board's own MIT test programs** (SAM modes and offsets, cross modes, HS/FS counts, rate modes, PIA edges, joystick reads), run from a cartridge image so no firmware is needed. Recommend: they are the only path to hardware verdicts (through the probe queue). 10. **m6809-python first, with this board's and williams-python's requests in its brief.** Recommend: the core's build session before this board's milestone 1. 11. **The Dragon's crystal (C1).** Recommend 14.31818 MHz for the Dragon 32 row and a crystal option on the Dragon 64 rows (14.31818 or 14.218), marked HYPOTHESIS, with a probe-queue entry for anyone with a Dragon; MAME's lockstep run uses MAME's value. 12. **XRoar as a second closed box** (GPL-3.0+, built in emu-tools-gpl `tools/xroar`) for the Dragon's PAL padding and artifact colours, where MAME is weak. Recommend later, at M6, run only.