# Handoff: build `vic20-python`, the Commodore VIC-20 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 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 VIC-20 is era 1: one clock domain, cycle level everywhere, "Python runs the tests, Rust plays". Boards to model this one on: **a2600-python** and **famicom-python** (a 6502-python core driven per bus cycle, the chip advanced inside the bus callbacks), **vectrex-python** (the 6522's first user, Phase 0 only), and tonight's **c64-python** (the same 6502 family, the 1541, the IEC bus) and **bbcmicro-python** (two 6522s, Phase 0 in progress). Read in order: [memory-map.md](memory-map.md), [vic.md](vic.md), [via.md](via.md), [expansion-cartridges.md](expansion-cartridges.md), [cassette.md](cassette.md), [drive-1541.md](drive-1541.md), [domains-and-speed.md](domains-and-speed.md), [validation.md](validation.md), [mame-oracle.md](mame-oracle.md), [census.md](census.md), [sources.md](sources.md), [provenance.md](provenance.md). Then `python -I scripts/fetch_references.py` (15 files, about 50 MB, and VICE-testprogs' `VIC20/` tree; `--verify` to check). ## Decided by Aubrey (standing rules that apply here) - Name `vic20-python`, package `vic20_python`. Local, no remote, until he says otherwise. - Bring your own ROMs: KERNAL, BASIC, character and drive ROMs by SHA-1 (census.md); nothing of Commodore's in the repository. - 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. - The 6522 lives in `mos6522-python` once a second machine needs it (2026-11-12, recorded in vectrex-python's brief). The VIC-20 is such a machine (open decision 2). - Technical choices are made, marked provisional and reported; policy waits for him. ## Reuse first: each chip's family cores | Chip | Existing family core | What it needs | | --- | --- | --- | | 6502 (NMOS) | **6502-python** (`sixfiveohtwo.MOS6502`, pin `81e0eb6`); 6502-rust for the Rust tiers | nothing for the computer: no RDY, no DMA, IRQ/NMI requests from bus callbacks already supported. For the drive: an **SO input** (upstream request, open decision 7) | | 6522 VIA x 2 | none yet: vectrex-python has only Phase 0 documents (no `via.py`) | a new core **`mos6522-python`** (open decision 2) | | 6560/6561 VIC | none; only the VIC-20 and VIC-1001 use it | stays in the board (`vic20_python/vic.py`), pin-shaped, no board names inside | | 1540/1541 | none | `c1541-python`, shared with c64-python (open decision 3) | | Datasette, expansion devices | none | in the board | No other family chip applies (no AY, SN, YM, TMS9918 on this machine). ## Package and classes (proposed) ``` vic20_python/ machine.py # rows: vic20 (NTSC), vic20p (PAL), vic1001 (JP), vic20_se; ROM SHA-1s; RAM rows board.py # Vic20: the bus, the order of record, build(machine, expansion, cartridge, ...) memory.py # the decoders (UB1 blocks, UD2 I/O, UC1 RAM), colour RAM, open bus, RAM init vic.py # Vic6560 / Vic6561 (one class, a row each): fetch, raster, pixels, sound, pots, pen expansion.py # memory rows (3k, 8k, 16k, 24k, 35k) and cartridges as {block: bytes} cartridge.py # No-Intro image naming -> blocks; the 16K split rule; .prg/.crt in a host helper keyboard.py # the 8x8 matrix on VIA2; joystick and paddles on the control port datasette.py # pulse source into VIA2 CA1; motor, sense, write iec.py # IecBus socket (three wired-AND lines + RESET) and a test-only fake device sockets.py # Protocols: ExpansionDevice, IecDevice, ControlPortDevice, TapeDeck savestate.py # whole-machine states naming ROMs by hash ``` The VIAs come from `mos6522_python`; the CPU from `sixfiveohtwo`. ## The import boundary The board owns: decoding, the phase interleave, colour RAM, open bus, the VIC, the wiring of VIA pins to keyboard/joystick/serial/tape/NMI/IRQ, expansion and cartridge mapping. `mos6522-python` owns the VIA's registers, timers, shift register, handshakes and IRQ output, with pin callbacks and a per-cycle tick, and knows nothing of the VIC-20. The drive owns everything on its side of `IecBus`. ## The gate (defined at milestone 1, before any add-on) The board's lockstep case (the KERNAL's instruction stream against MAME's `:ue10` trace, by CPU cycle, `0 unexplained`), its unit tests of the documented facts, and later the VIA tests, the split-tests dumps and picture rows. With nothing on the expansion port, IEC socket or tape port the base board must not move these when an add-on lands. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M0 | **`mos6522-python`** core (in its own repository, decision 2): registers, both timers, IFR/IER, ports with and without handshake, CA/CB modes, PB7, the shift register's eight modes | data sheets (2); unit tests per data-sheet statement | the core's own rungs; its VIC-20 gate comes in M3 | | M1 | **Bus, decoders, RAM rows, colour RAM, both VIAs wired, the VIC's register file and raster counters (no pixels), NMI edge and IRQ level** | SCH-E, PRG (2); MAME trace (5) as detector | the KERNAL reaches BASIC; the CPU stream equals MAME's to cycle 503,667 (the first VIA write) exactly and to 2 s with every difference named (MAME's 60.000 Hz frame makes $9004 reads differ: C1) | | M2 | **VIC pictures**: fetches, 8x8/8x16 cells, multicolour, reverse, borders, origin and size registers, interlace | VDS (2), VICI (3); **split-tests/timing dumps (1)**; vic_vert0, vic_9000test, vic_line0 (3) | the BASIC screen drawn; the timing dumps reproduced for 6560-101, 6561E and 6561-101 rows; the three corner tests match their pictures | | M3 | **The VIAs on the VIC-20** | VICE via_pb7, via_t1irqack, viavarious refs, via_sr dumps, via_defaults (3) | every test with a hardware reference passes; each failure without one listed | | M4 | **Expansion rows and cartridges** | the images; MAME's CC0 list for block placement | a smoke list (problem images and plain ones) boots; then all 288 No-Intro zips reach a title screen or are listed with a cause | | M5 | **Sound, paddles, light pen** | VICI (3); split-tests/lightpen dumps (1); paddlescope behaviour (3) | frequency formulas exact; noise per U1 tagged; light-pen dumps reproduced | | M6 | **Datasette** | the KERNAL (its own loader) | a `.tap` loads through the KERNAL's routines; motor timing tagged FITTED | | M7 | **IEC socket and the drive** (c1541-python, decision 3) | DSM (2), the DOS ROM, VICE drive tests (3) | `LOAD"$",8` lists a disk on the 1540 and 1541 rows | | M8 | **PAL/JP/SE rows complete, save states** | | a save state loads into a fresh machine and continues identically | ## Unknowns (U) and conflicts (C) | # | Unknown | Best source now | Status | | --- | --- | --- | --- | | U1 | the VIC's noise generator (LFSR, taps, clocking) | Ewing's die reading, second hand (vic.md) | HYPOTHESIS; ask (decision 6) | | U2 | sound output: voice waveform mixing, volume DAC, analogue stage | none | good-enough rule | | U3 | what reads of the VIC's registers return in unused bits and for registers only written | none pinned | detectors; a test program | | U4 | write-to-effect delay per VIC register within a line | split-tests/timing dumps (1) | settle in M2 | | U5 | open bus details: colour RAM's upper nybble, the idle fetch address ($001C vs $181C) | VICI | `unconnected` as a detector | | U6 | both VIAs selected at $9130; the VIC and a VIA both selected in $9010-$90FF | SCH-E decode | provisional: both drivers ANDed on the bus, tagged HYPOTHESIS; a test program on hardware would settle it | | U7 | power-on RAM | raminitpattern dumps (per machine) | selectable, one dump as default | | U8 | the palette (luminance, chroma) of the 6560/6561 | none measured | HYPOTHESIS table; host's choice | | U9 | paddle conversion count vs resistance; its video-coupled noise | paddlescope (behaviour only) | FITTED | | U10 | light-pen latch offsets | split-tests/lightpen dumps (1) | settle in M5 | | U11 | the 6560's interlace (field order, raster per field) | vic_line0 (one case) | detectors | | U12 | whether the KERNAL uses the VIA shift register for the serial bus | the KERNAL (read as a program) | check before M7 | | U13 | the 4 unsigned $A000 images, the `.bin`, 4 unmatched 16K images | Cartzilla, MAME's list | before M4 | | U14 | Datasette motor and read-line timing | none | FITTED | | U15 | the VIC's state at power-on (no reset pin on the -101) | none | registers cleared, provisional | | # | Conflict | Sides | Proposal | | --- | --- | --- | --- | | C1 | NTSC lines per frame | VDS p. 16: 262 (262.5 interlaced); VICI: 261 (525), measured; MAME: a 60.000 Hz frame of 17,045 cycles | 261; confirm with the 6560 timing dumps in M2 | | C2 | the 6561's clock | VDS p. 10: 4.436187 MHz; VICI and the PAL board: 4,433,618 Hz | 4,433,618 Hz | | C3 | I/O2 address | SCH-E decoder and PRG p. 177: $9800; PRG p. 243 pin table: $9600; Shrake: $9130 | $9800 (the schematic) | | C4 | T1 high-latch write clears the T1 flag | Synertek's notes: no; via_t1irqack on MOS and Synertek VIAs in real VIC-20s: yes | yes | | C5 | 1541 VIA addresses | DSM: serial VIA at $1C00, R/W VIA at $1800; the common account: the reverse (c64-python's C5) | the DOS ROM decides when run | ## Domains and speed Full arithmetic in [domains-and-speed.md](domains-and-speed.md). **Domains:** M (6502, VIC, two VIAs, memory: one crystal, the VIC's two phases) and, when attached, D (the drive: its own 16 MHz crystal); the tape is a timestamped edge producer. **Channels:** the IEC lines and RESET (M <-> D), sound and video as pure producers, tape edges into VIA2 CA1. **Instructions per second:** NTSC 14,318,181 / 14 = 1,022,727 CPU cycles a second; at MAME's observed 3.37 cycles an instruction, about **300,000 instructions a second** (PAL 1,108,405 cycles, about 330,000); the VIC adds 1.02 M fetches and 4.09 M pixels a second; the drive another 1.0 M cycles (about 284,000 instructions). **No-JIT path:** vic20-rust, a plain interpreted transcription, reaches real time on desktop; by the NES/2600/Game Boy precedents a Rust board of this size (with `-fast` if a phone needs it) meets real time on phone and browser (wasm) without recompilation or a cached interpreter; threads at the IEC channel are the only multi-core lever and pay only with the drive attached. `recompile: none` on every target. Python on PyPy: expected 0.5x-2x real time (HYPOTHESIS), enough for its tests. ## The chip rule | Chip | Users in or near the family | Proposal | | --- | --- | --- | | 6502 | many | 6502-python | | 6522 x 2 | Vectrex, VIC-20, 1540/1541, BBC Micro (tonight), later PET, Oric | `mos6522-python` (decision 2) | | 6560/6561 | VIC-20, VIC-1001 only | board | | 1540/1541 | VIC-20, C64 (tonight), Plus/4, C16 | `c1541-python` (decision 3) | ## Open decisions for Aubrey (each with a recommendation) 1. **First machine.** Recommend NTSC `vic20` and PAL `vic20p` as rows from milestone 1: MAME's default and the trace are NTSC, but most hardware-referenced tests and dumps were taken on PAL machines (6561E, 6561-101). `vic1001` and `vic20_se` as rows later. 2. **The 6522 core.** Recommend creating **`mos6522-python`** now (your 2026-11-12 name; c64-python's brief proposes `via6522-python` for the same thing, bbcmicro-python's draft uses `mos6522-python`), as a new core (vectrex-python has no VIA code to extract), built before vic20-python's M1, and gated by the VICE VIC-20 VIA tests run through vic20-python. One brief for it should come from the three Phase 0s together. 3. **The drive.** Recommend one board `c1541-python` with rows `c1540` and `c1541`, on an `IecBus` socket pinned by vic20-python and c64-python (c64-python's decision 2 agrees), started by whichever board reaches its IEC milestone first. 4. **Modern expansions** (Final Expansion 3, UltiMem, Mega-Cart, Vic-2020 style boards). Recommend out of scope until the No-Intro set passes; they are later expansion rows, with VICE-testprogs' `fe3test` and `ultimem` as their tests. 5. **Denial's content signal** (`ai-train=no, use=reference`, Cloudflare-managed). Recommend treating it as not an objection to reading for facts (it reserves training only), but it is your call; this Phase 0 did not fetch the forum and lists the facts that reach the board only through it (provenance.md). 6. **Lance Ewing's 6561 die work.** Recommend a contact (drafted for you to send, after the board exists, per your 2026-11-12 rule on outside contacts) asking for a licence on `mos-6561-reverse` and the noise generator's details: it is the only die-level source for U1 and possibly U2. 7. **Upstream request to 6502-python: an SO input.** Recommend raising it when the drive work starts (c64-python asks the same), with a repro (a falling SO edge sets V). 8. **Palette.** Recommend a HYPOTHESIS table, host-selectable, and no claim of accuracy; ask whether you know of a measured 6560/6561 palette. 9. **MAME's VIC-20 files are BSD-3-Clause** by their `license:` lines. Recommend keeping them closed (run only) as tonight's brief says, matching the PlayStation decision, until you decide whether BSD MAME files may be read as documentation. 10. **zimmers.net.** Its robots.txt forbids automated fetching of PDFs and archives; this repository fetches from funet.fi (the original archive). c64-python's pins fetch from zimmers: recommend repointing them to funet.fi. 11. **sources.md rows** (eight, in [sources.md](sources.md)): recommend adding them. 12. **Remote.** Recommend local until milestone 1 passes, then a private GitHub remote.