# Handoff: build `c64-python`, the Commodore 64 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 only 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 C64 is **era 1**: one clock domain on the main board, cycle level everywhere, Python runs the tests, Rust plays. Boards to model this one on: **famicom-python** and **a2600-python** (a 6502-family CPU from 6502-python on a cycle-stepped bus with a video chip that steals cycles), **a5200-python** (`docs/sally-halt.md`: HALT vs RDY on a 6502, the closest precedent for BA/RDY), **vectrex-python** (its `via.py` 6522), **megadrive-python** with **sega32x-python** (an add-on as a separate board on a socket). Read in order before writing code: 1. [README.md](../README.md), [start-here.md](start-here.md), [cpu-6510.md](cpu-6510.md), [memory-pla.md](memory-pla.md). 2. [vic-ii.md](vic-ii.md), [sid.md](sid.md), [cia.md](cia.md), [cartridges.md](cartridges.md), [drive-1541.md](drive-1541.md). 3. [domains-and-speed.md](domains-and-speed.md), [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` (14 documents, about 55 MB, and the VICE testprogs sparse checkout, about 400 MB; `--verify` to check). PDFs become text in `reference/text/`. The data-sheet zips are GIF scans: OCR them with tesseract when needed (the 6510's page 1 was read that way here). ## Decided by Aubrey (standing rules that apply here) - Name `c64-python`, package `c64_python`. Local, no remote, until he says otherwise. - Bring your own ROMs, cartridges, disks and tapes, found by `NO_INTRO_DIR` and `MAME_ROMPATH`; firmware identified by SHA-1, never copied or committed. - Python floor = PyPy's (3.11), PyPy must work; modern over compat; reference boards are correct, not fast; sound may be less exact than pictures; smoke lists before sweeps; technical choices made, marked provisional and reported. - Closed boxes (VICE, MAME, reSID, Hoxs64, ...) are run, never opened; their source is never fetched into this repository. ## Proposed by this Phase 0 - **NTSC `c64` row first** (6567R8, 6581, KERNAL 901227-03), with `c64-pal` (6569) a machine-table row from milestone 1; `c64-r56a`, `c64c` (8562/8565, 8580, 8521), `sx64`, `c64gs`, `c64-jp` later; `max` (Ultimax, 6566) last. - **The 6510 as `MOS6510` in 6502-python** (cpu-6510.md), raised upstream before M1. - **The PLA as its logic table**, evaluated per half cycle, not a hand-written map. - **BA/RDY inside the read callback** with `note_stall()` and `dma_stalled`, as a5200-python's analysis concluded for RDY on the SALLY. - **The 1541 as its own board `c1541-python`** on an `IecBus` socket (decision 2). ## Package and classes (proposed) ``` c64_python/ machine.py # rows: VIC type, SID type, CIA model, ROM SHA-1s, RAM init pattern, port fade console.py # build(machine, rom_dir, cartridge=None, iec=None) -> C64 bus.py # the half-cycle scheduler: VIC phase 1, CPU or VIC phase 2, BA/AEC, open bus pla.py # Pla: the 82S100 logic table (PLA appendix A as unit tests) vic.py # VicII: timing tables per type, accesses, display/idle, sprites, borders, IRQs sid.py # Sid: digital part per cycle, analogue model per type (pin-shaped, no C64 names) cia.py # Cia: ports, timers, TOD, SDR, ICR; 6526 and 8521 (pin-shaped) keyboard.py # the 8x8 matrix and joystick/paddle inputs presented on CIA 1 cartridge.py # CartridgeSlot, CRT and raw loaders, one class per hardware type datasette.py # tape pulse stream into CIA 1 FLAG; motor/sense/write via the 6510 port iec.py # IecBus socket (wired-AND ATN/CLK/DATA, RESET) and a null device debugcart.py # the $D7FF test register (VICE testbench convention), off unless asked savestate.py # whole-machine states naming ROMs and media by hash ``` ## What the board needs from its cores From **6502-python** (pin `origin/main`, which has CPUFrame since `828b619`): 1. **`MOS6510`** (upstream request 1): the port at $00/$01 (DDR at $00, data at $01: the data sheet, not PRG appendix L), input-pin and output-pin access for the host, the bit 6-7 fade as a setting, the bus still called on every cycle including port accesses so the board can write the VIC's phase-1 byte into RAM (VIC section 4). 2. `note_stall()` and `dma_stalled` (exist). C3 decides whether interrupt sampling during RDY matches the 6510. 3. For the 1541 (upstream request 2): an **SO input** (set overflow on a falling edge), which the drive uses for "byte ready"; 6502-python has none. From a **6522 VIA core** (for the 1541; decision 3): extracted from vectrex-python's `via.py` with history, pinned by both boards. ## The gate (defined at milestone 1, before any cartridge or drive) The boot lockstep (validation.md L1), the unit tests of documented facts (L0), the READY picture (L2), seeded faults each caught. With the cartridge slot and the IEC socket empty, the base board must not move these when a cartridge type or the drive lands. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M0 | upstream: `MOS6510` in 6502-python (and SO, for M6) | 6510DS (2); 6502-python's own suites | 6502-python's suites unchanged; port unit tests from the data sheet; a fault (DDR/data swapped) caught | | M1 | **bus, PLA, RAM, ROMs, the 6510, CIAs (timers, ports, ICR), VIC timing skeleton (line/cycle counters, BA/AEC for bad lines, raster IRQ), SID register file only** | PLA (1), VIC (3), PRG (2); MAME (5) | **boot to READY in lockstep with MAME** (593,433 instructions, NTSC) or a named reason per difference; the PAL row boots | | M2 | **VIC pictures**: the display unit, the eight modes, borders, colour RAM, the READY screen | VIC (3); MAME snapshot (5) | the READY screen equal to MAME's at frame 240 in colour indices; border flip-flop and access-table unit tests from VIC 3.6.3 and 3.9 | | M3 | **Lorenz 2.15** and the CPU/CIA test directories | Lorenz (3), VICE CPU and CIA tests with hardware notes (3) | every Lorenz part passes; each failing VICE test triaged by its readme's tier | | M4 | **sprites, collisions, light pen, the VIC test directories** | VIC 2024 text (3); VICE VICII tests (3/5) | the hardware-noted VICII tests pass; sprite crunch, FLI, FLD, VSP, open borders from the VIC tests; screenshot cases compared with the tier of each recorded | | M5 | **SID**: digital part cycle exact, analogue model per type | VICE SID tests (3) for the digital half; good-enough rule for the rest | the OSC3/ENV3 tests pass; audio compared with detectors, differences named; combined waveforms per decision 5 / U6 | | M6 | **IEC socket and the 1541** (in c1541-python if decision 2): 6502 + two VIAs, GCR, mechanism, `.d64` then `.g64` | DSM (2); drive tests (3); MAME drive trace (5) | the drive's first second equal to MAME's (283,621 instructions); `LOAD"$",8` lists a disk; the drive tests with hardware notes pass | | M7 | **cartridges**: the 22 CRT types of the No-Intro set, raw-image table from MAME's CC0 list, freezers (NMI, Ultimax); the problem images | each reaches its title (smoke list) | the smoke list passes; EasyFlash and modern types next | | M8 | **datasette, user port, remaining rows** (C64C with 8565/8580/8521, SX-64, GS, JP); save states | VICE tests' old/new variants (3) | each row boots; `greydot`, `lp-trigger` new/old variants and CIA new/old variants pass on the matching row; a save state continues identically in a fresh machine | ## Unknowns (U) and conflicts (C) | # | Unknown | Best source now | Status | | --- | --- | --- | --- | | U1 | the clock divider circuit (phiCOLOR x 4/7 or 4/9, / 8): the figures fit, the circuit is unread | SM schematic | read in M1 | | U2 | the PLA's REV2 7E17 logic (very early C64s) | none (no dump: PLA 2.3) | REV3 for every row | | U3 | power-on contents of colour RAM and the VIC's registers | none | row option, default FITTED | | U4 | open-bus value: the VIC's last phase-1 byte "not reproducible on all machines" | VIC section 4 | model it, FITTED, row switch | | U5 | the SID filter capacitors per board revision | SM | read in M5 | | U6 | **6581/8580 analogue output and combined waveforms from a source we may use** | none permissive found | blocks exact SID analogue; decision 5 | | U7 | keyboard matrix electrical behaviour (ghosting, joystick fighting the matrix) | none read | detectors, then tests | | U8 | 6510 vs 8500 differences (port fade time, anything else) | VICE cpuport readme (C2) | one class, row settings | | U9 | the 1541's read/write gate array at the bit level (sync detection, byte-ready timing, write splice) | DSM circuit text | drive tests; detectors | | U10 | which VICE tests are hardware-verified | 34 directories with keyword hits (vic-ii.md, sid.md, cia.md, cpu-6510.md, drive-1541.md) | read every readme before counting a test as a judge | | U11 | 1541 rpm and its tolerance, motor inertia | VICE `drive/rpm`, `drive/inertia` (hardware notes) | M6 | | # | Conflict | Sides | Proposal | | --- | --- | --- | --- | | C1 | 6510 port addresses | PRG appendix L p. 402: output register at $0000, DDR at $0001; 6510DS p. 1 and VIC 2.2: DDR at $0000, data at $0001 | the data sheet; PRG's appendix is a misprint (every program agrees with the data sheet) | | C2 | the port's bit 6-7 fade time | VICE cpuport readme: "some hundred microseconds" and "~200000us (gpz, new C64)" in the same text | a row constant fitted to the `delaytime` test's own measurement | | C3 | interrupts while RDY holds a read | VIC 2.2: "interrupts are only recognized if the RDY line is high"; 6502-python (perfect6502, the 6502 netlist): detectors keep sampling during the stall | settle with VICE `interrupts/irqdma` and Lorenz's IRQ tests on the board; until then follow the netlist (DIE outranks a prose summary), marked | | C4 | which register holds ECM | VIC 3.7.1: "the ECM bit in register $d016"; VIC 3.2 table: $D011 bit 6 | $D011 bit 6 (the table, PRG appendix N agree) | | C5 | the 1541's VIA addresses | DSM: serial VIA UC3 at $1C00, read/write VIA UC2 at $1800; the common account the reverse | settle by running the DOS ROM: it must find its serial port where the hardware puts it | | C6 | VIC 1996 vs 2024 text (VMLI, sprite Y expansion, priorities, raster IRQ) | Bauer's own revision history | the 2024 text | ## The chip rule | Chip | Users in or near the family | Proposal | | --- | --- | --- | | 6510 / 8500 | C64 | variant `MOS6510` in 6502-python | | 6502 (1541) | many | 6502-python `MOS6502`, plus SO | | 6522 VIA | **vectrex-python (in-board `via.py`) and the 1541**; also the VIC-20 | extract `via6522-python` from vectrex-python with history (decision 3) | | VIC-II | C64 only | board | | SID | C64 only (C128 not in the family) | board, pin-shaped (decision 4) | | CIA 6526 | C64 only (C128, 1571, Amiga not in the family) | board, pin-shaped (decision 4) | | PLA | C64 only | board | | 1541 | C64, VIC-20, SX-64, Educator 64, Plus/4, C16 (DSM) | its own board `c1541-python` (decision 2) | ## Domains and speed Full page: [domains-and-speed.md](domains-and-speed.md). In short: - **Domains:** M (6510, VIC-II, PLA, memory, CIAs, cartridge; one crystal, coupled every half cycle), S (the SID, optionally split: write-mostly, four readable registers), D (the 1541: its own 16 MHz crystal). **Channels:** the IEC lines (every change and read a sync point), SID register writes (a log) and reads (sync points), the datasette's edges, and the pure producers (video indices, audio). - **Instructions per second, NTSC:** 6510 1,022,727 cycles / 3.59 cycles an instruction (MAME boot, OBSERVED) = about **285,000**; the 1541 1,000,000 / 3.54 = about **283,000**; plus the VIC (1.02 M cycles, 8.18 M pixels), the SID (1.02 M ticks), two CIAs (2.05 M ticks), two VIAs (2.0 M ticks): about 570,000 instructions and 6 M chip-cycles a second with the drive. - **No-JIT path:** the NES (famicom-python: 3.0x real time on PyPy for a comparable load) and the 2600 and Game Boy lines reach real time with `recompile: none` on desktop, phone and browser through Rust and Rust -fast. The C64 should too: `recompile: none` on every target; a cached interpreter is not needed; threads at the IEC channel only if they measure a win (they stall during transfers). ## Open decisions for Aubrey (each with a recommendation) 1. **Scope and first row.** Recommend the NTSC C64 (6567R8, 6581, KERNAL R3) first, PAL as a row from M1, the C64C/SX-64/GS/JP rows at M8, the MAX last. 2. **The 1541: separate board or socket device.** Recommend a separate board `c1541-python` (it is a computer with its own crystal, CPU and firmware; five other Commodore machines use it; it needs its own gate), plugged into `c64-python`'s `IecBus` socket, started at M6. The alternative, a socket device inside this repository, is simpler only until a VIC-20 or Plus/4 board appears. 3. **The 6522 VIA core.** The 1541 is its second family user (vectrex-python is the first, in-board). Recommend extracting `via6522-python` (package `via6522_python`) from vectrex-python's `via.py` with history before M6, and pinning it from both. 4. **SID and CIA stay in the board.** Recommend `sid.py` and `cia.py` inside `c64-python`, pin-shaped with no C64 names, until a C128 (or another consumer) joins the family. 5. **SID analogue evidence (U6).** No permissively licensed captures of 6581/8580 output or combined waveforms were found. Recommend: the digital half exact now; the analogue half FITTED under the good-enough rule; and a decision whether libsidplayfp's perfect6581 netlist (MIT file, but the netlist comes from Lempinen's layouts with no stated licence) may be read as facts, after asking its maintainers about its provenance. 6. **Author respect: codebase64 and CSDb.** Both sites block AI crawlers (ClaudeBot and anthropic-ai named) in robots.txt. This Phase 0 stopped using them (one home-page fetch of codebase64 happened before its robots.txt was read). Recommend treating a site-wide AI-crawler block as the authors' objection under charter point 8, adding both to sources.md as "set aside (author's wishes)", and not following VICE test readmes' links into them. 7. **sources.md rows** (emulator-lineage; proposed here, not written there): Bauer's VIC article (facts only; use the 2024 revision); the PRG and MOS data sheets (facts only); Giesel's PLA Dissected (CC BY-SA 3.0, facts with credit); Mäkelä's PLA equations (no licence, facts only); MAME `hash/c64_*.xml` (CC0-1.0, data usable); Hoxs64 (no licence, closed box); Denise (GPL-3.0); VirtualC64 (split licence since 2024-05-19: app GPL-3.0+, core MPL-2.0, Peddle CPU MIT); Frodo (GPL-2.0); CCS64 (shareware, all rights reserved); Emu64 (GPL-2.0); reSID/reSIDfp (GPL-2.0+); C64_MiSTer (no licence); FPGA64 (excluded: "not allowed to copy ... or redistribute"); VIC-II Kawari (GPL-3.0); perfect6581 and SID_schematics (provenance unsettled); codebase64 and CSDb (decision 6); the VICE row extended to name the testprogs mirror and Lorenz 2.15's public-domain notice. Also worth pinning: US patents 4,551,682 and 4,561,659 (public documents, tier 2). 8. **VirtualC64's MIT CPU and MPL core.** Recommend leaving them closed box (an MPL core would bind files we take from it; the CPU is MIT but 6502-python already has a better-evidenced 6502), unless Aubrey wants a permissive detector built. 9. **Upstream requests to 6502-python:** `MOS6510` (M0) and an SO input (before M6). Recommend raising both now so they land before the board needs them. 10. **The No-Intro disk and tape sets** are not in the local mirror. Recommend fetching "Commodore - Commodore 64 (PP)" and "(Tapes)" before M6/M8 for the smoke lists (Aubrey's call: it adds to the mirror).