# Handoff: build `cp1610-python`, the General Instrument CP1600-family CPU core ## Context you are inheriting The family's shape is **chips are cores, boards compose cores**. Each core is certified on its own against its oracles and pinned by commit by the boards that use it. This core's first user is `intellivision-python` (`/data/emu/intellivision-python`, being prepared in parallel; do not edit it). A possible second user is a Unisonic Champion 2711 board (MAME `unichamp`: a CP1610 with GI's GIC video chip). A board that finds a corner of this core raises it **on the core** (an issue or a PR here; re-certification is fine), never as a workaround in the board. **The core must know nothing about the Intellivision**: no addresses, no vectors, no 10-bit ROMs, no STIC. Everything machine-specific arrives through the bus callback and the request lines. Models for the code and the tooling: - `huc6280-python` (`/data/emu/huc6280-python`, HEAD): the newest CPU core built from a Phase 0 like this one: module layout, `harness.py`, tooling. - `m6800-python` (HEAD): the tooling documents (`api-stability.md`, `trace-schema.md`, `cpu-state.md`, `debug-session.md`, `disassembly.md`) and the rung scripts that drive MAME. - `z80-python`: the readability contract (one instruction family per module, one mnemonic per handler docstring, the source and tier as a comment on the line that encodes each fact). The rules that bind this work: - **Oracle tiers**: hardware-captured > manufacturer > hardware-corrected community material and tests run on hardware > HDL > emulators. A low tier is a detector, never a judge; state the tier of every claim. **This chip has no tier-1 source** ([validation.md](validation.md)). - **Python floor 3.11** (the newest PyPy supports), CPython and PyPy both green. Fewer than 80 instance attributes on the CPU object (PyPy turns bigger instances into dicts: about 3x slower), no per-microcycle generator, plain integers in the hot path, so a Rust transcription stays possible. - **Reference cores are correct, not fast.** Speed is a later `-fast` fork's job. - **Modern over compat**: dataclasses, callables, no shims. - **No dangerous-looking shell one-liners** in scripts or docs; CI minimal (PyPy only, main + PRs, cancel in progress) if a GitHub repository is ever made. **Local git only** until Aubrey says otherwise. - **Never copy code between repositories**: HW26's MIT tests and tools are fetched by pin and run as data or tools, not copied in. - **Nobody here has the hardware.** No rung needs a console. - Run any Python that reads `reference/` with `python -I`. ### Read, in this order, before writing code 1. `README.md`. 2. [start-here.md](start-here.md): registers, the opcode map, flags, modes, SDBD, shifts, branches, jumps, control instructions. 3. [timing.md](timing.md): the microcycle, the eight bus phases with pin levels, the building blocks, every instruction's phases and cycles with the votes. 4. [interrupts-and-bus.md](interrupts-and-bus.md): the interruptible rule, INTR/INTRM, BUSRQ/BUSAK, BDRDY, MSYNC, STPST/HLT, PCIT, EBCA/EBCI. 5. [undocumented-behavior.md](undocumented-behavior.md): C1-C21, U1-U20 and the switch table. 6. [validation.md](validation.md): every oracle by tier, the rung ladder. 7. [mame-oracle.md](mame-oracle.md), [rom-census.md](rom-census.md). 8. `/data/emu/intellivision-python/docs/bus.md` (the board's draft requirements; section "The bus contract" below answers them point by point). Then `python -I scripts/fetch_references.py` (about 350 MB: ten GI and Osborne PDFs, jzIntv's source, 14 wiki pages, five repositories; the `--no-catalogs` flag skips the four 30-40 MB catalogs) and `python -I scripts/fetch_references.py --verify`. ## Scope and names **One stand-alone, bus-accurate, pure-Python model of the CP1600 family**: the CP1600, CP1600A and CP1610 have one instruction set, one bus and one pin list and differ only in clock limit and supply (G82 p.5; start-here.md). The core models one microcycle per bus phase; the board supplies the clock. Proposed (Open decisions 1): distribution **`cp1610-python`**, import package **`cp1610`**, class **`CP1610`**. Why the base name is `cp1610` and not `cp1600`: - every machine the family can run has a CP1610 (Intellivision and its relatives, Sears, INTV88/TutorVision with the CP1610A, the Unisonic Champion); no CP1600 machine (the GIC1600 development system) is emulated anywhere found; - it is the name MAME's device, both HDL cores and HW26's model use, so traces and issues line up; - the CP1600 manuals (S1, the 1975 title) are cited as the family's documents in the README; nothing would change in the code for a CP1600. All three names are free on PyPI (checked 2026-11-12: `cp1610`, `cp1600`, `cp1610-python` return 404). No alias class. ## The import boundary | Piece | Owner | Why | | --- | --- | --- | | R0-R7 (R6 the stack pointer, R7 the PC), S Z OV C, the interrupt enable I, the double-byte flag D | core | the CPU's state | | decode of the 10-bit opcode (the upper six bits of an opcode word ignored: U1), SDBD, every addressing mode | core | | | **the sequence of bus phases** of every instruction, of interrupt entry, reset, bus-request waits and halts, one callback per microcycle | core | the CPU's microcode decides it (timing.md) | | sampling INTR, INTRM and BUSRQ at interruptible boundaries; the priority BUSRQ > INTR > INTRM (S1 p.25); BUSAK and HALT levels; the `interruptible` state | core | | | EBCA (the condition number during BEXT) and the EBCI answer's use; TCI and SIN pulses as hooks | core | pins of the CPU | | the cycle-table and behaviour switches (undocumented-behavior.md) | core | | | **the address latch** (BAR, INTAK, and ADAR's "returned word becomes the address") | board | the CPU never drives a direct address; every device latches (timing.md) | | memory, decoding, **10-bit ROM masking** (top six lines read 0), 8-bit devices, open-bus values | board | | | the **IAB vector** (reset and interrupt) | board | the Intellivision's EXEC ROM chip supplies it; the core must ask | | when INTRM, INTR, BUSRQ change; the 16 external conditions; the clock and its unit | board | | | INTAK-to-BAR remapping, bus copy, STIC windows | board | Intellivision logic | **Sockets**: none. A CPU core has no expansion; the board's sockets (cartridge, ECS, Intellivoice) never reach the core except as bus answers. **Chip rule**: CPUs always own a core. Users of the CP1610: Intellivision (board in progress), Unisonic Champion 2711 (MAME only), INTV88/TutorVision (CP1610A, nobody emulates them), the GIC1600 development system (not emulated). No split question arises. ## The bus contract (required) ```python from cp1610 import CP1610, NACT, ADAR, IAB, DTB, BAR, DW, DWS, INTAK cpu = CP1610(bus, ebci=None, on_tci=None, on_sin=None, timing="doc") # bus(phase, value) -> int # called once per microcycle, in chip order, including every NACT. # phase = BDIR<<2 | BC2<<1 | BC1: NACT 0, ADAR 1, IAB 2, DTB 3, # BAR 4, DW 5, DWS 6, INTAK 7 (the board's and HW26's coding) # value = the 16-bit word the CPU drives in BAR, DW, DWS, INTAK # (BAR and INTAK: an address; DW and DWS: the same data word); # 0 in the other phases # return = the 16-bit word on the bus in DTB, ADAR and IAB (the core # uses DTB's and IAB's; ADAR's it ignores, as the CPU does); # ignored in the other phases # ebci(condition) -> bool BEXT's external condition (EBCA0-3 = condition), # asked once in BEXT's fifth microcycle; None = always false # on_tci(), on_sin() the TCI pin pulse and SIN's PCIT pulse; None = ignored cycles = cpu.step() # microcycles consumed == number of bus callbacks made ``` - **Input lines are plain attributes** the board may change at any time, including inside a callback: `cpu.intrm`, `cpu.intr`, `cpu.busrq` (True = asserted, i.e. the pin low). They are sampled as the core's switches say (`INTRM_EDGE`, C16); a change made inside the callback of microcycle *n* counts as seen at the end of *n* (U9). - **Outputs readable in a callback**: `cpu.busak`, `cpu.halted`, `cpu.interruptible` (the state left by the last instruction), and the registers (`cpu.r[0..7]`, `cpu.flags`, `cpu.i`, `cpu.d`). - **`step()` runs exactly one of**: the reset sequence; a bus-request wait (NACT callbacks with `busak` True for as long as `busrq` stays asserted, then the two exit NACTs, S1 p.36, G82 Fig. 16; the boundary stays open, so the next `step()` re-checks BUSRQ, then the interrupts); an interrupt sequence (9 callbacks by default: INTAK NACT DW DWS NACT IAB NACT NACT NACT); one instruction (SDBD is its own instruction and its own step; MAME merges it, rung 3's replay must merge too); or one NACT while halted. - **Latencies, with sources** (the board's point 8): BUSRQ is acted on only at the end of an interruptible instruction; BUSAK goes low in the first NACT after it (no entry overhead, S1 p.36); after BUSRQ is released, two NACTs with BUSAK low, then BAR or INTAK (S1 p.36, G82 Fig. 16, O81). HW26's console fit sees only release + exit together, and keeps 2 (TIMING_FIT). Interrupts wait for an interruptible boundary in the same way; the first instruction of a service routine always runs (U10). - **Reset**: `cpu.reset()` stands for MSYNC rising; the next `step()` issues NACT IAB NACT NACT NACT (C1, switch `RESET_SEQUENCE`), R7 = IAB's answer, I = 0, D = 0; registers zero unless the constructor was given others (U2). Holding MSYNC low is the board not calling `step()`. - **Not in the first build**: BDRDY waits, STPST, the PCIT input (U14). Each is an attribute added later without changing the callback. - `cp1610.harness` (validation.md "The minimal harness") turns phases into `read(addr)` / `write(addr, value)` with its own latch, for tests and the replay host. Boards do not use it. ### Against the board's twelve points (`intellivision-python/docs/bus.md`, DRAFT) | # | Board asks | This contract | | --- | --- | --- | | 1 | one callback per microcycle with the phase code; CPU word in BAR, DW, DWS, INTAK; board returns ADAR, IAB, DTB; NACTs reported | **agrees** (same 3-bit coding) | | 2 | direct addressing not folded: `BAR NACT ADAR NACT` then `DTB` or `DW DWS` | **agrees**: the core issues BAR(PC+1) and never learns the direct address | | 3 | INTAK distinct from BAR; the PC pushed by DW/DWS | **agrees** | | 4 | IAB returns the vector, "on reset (the first microcycle after MSYNC)" | **differs in position**: IAB is the *second* microcycle after MSYNC (S1 p.27, O81: NACT IAB NACT NACT NACT). Open decisions 4 | | 5 | full 16 bits both ways; the core masks opcodes to 10 bits | **agrees** (U1) | | 6 | input levels set inside callbacks: INTRM, BUSRQ, MSYNC; also INTR, BDRDY, STPST, EBCI, PCIT | **agrees** for INTRM, INTR, BUSRQ; MSYNC is `reset()`; EBCI is the `ebci` callable; BDRDY, STPST, PCIT later (Open decisions 9) | | 7 | outputs: BUSAK level; TCI, HALT, EBCA as events | **agrees**: `busak`, `halted`; TCI as `on_tci`; EBCA as the argument of `ebci` | | 8 | callbacks continue while BUSAK is asserted; latencies stated with sources | **agrees**: NACT callbacks with `busak` True; latencies above | | 9 | INTRM and BUSRQ are **levels**, never edge-latched; a request withdrawn before a boundary is lost | **agrees for BUSRQ; differs for INTRM** by default: the core latches INTRM's falling edge, clears it when taken or when the line rises (C16, switch `INTRM_EDGE`). Withdrawn requests are lost under both. They differ only when INTRM is still low after its interrupt was taken: an edge latch takes it once, a level re-enters. Open decisions 3 | | 10 | `interruptible` exposed read-only | **agrees** | | 11 | deterministic power-up, no module state | **agrees** | | 12 | PyPy: under 80 attributes, `step()` per instruction, no generator | **agrees** | ## Timing and the switches The default cycle table is the manuals' (`timing="doc"`), which is also what MAME runs and so what rung 3 needs. `timing="console"` adds one trailing NACT to the jump family, `PULR R7` and `SDBD; MVI@ R4/R5,R7`: exactly the cases HW26's one-console fit needs (C5), nothing more. The full switch table, with defaults and the rows they settle, is in [undocumented-behavior.md](undocumented-behavior.md#switches-one-name-for-each-choice-no-judge-settles). Every switch is read in one place; every test that depends on one names it. ## Contradictions (C) and unknowns (U) One line each; the full rows with every source's tier are in [undocumented-behavior.md](undocumented-behavior.md). - C1 reset sequence length (S1/O81 five cycles; MAME none; HW26 RTL one) - C2 interrupt sequence 9 (S1, G82, MAME) vs 12 (jzIntv) vs 7 or 10 (wiki); HW26: path +2 - C3 MAME's SETC 6 and `SARC r,2` 6 against the manuals' 4 and 8 - C4 +1 cycle for R6/R7 destinations: MOVR only (manuals, MAME) vs every form (jzIntv) - C5 jumps 12/13, PULR R7 11/12, `SDBD; MVI@ R4,R7` 14/15 (console measured 15) - C6 NEGR of 0 sets C (console, S1) vs not (MAME) - C7 `SWAP r,2`: low byte to both halves (console) vs "swap twice" (S1) - C8 HLT: after the next interruptible instruction (G82) vs at once vs no-op - C9 SDBD + stack: one decrement, the same word twice (console) vs two (MAME) - C10 SDBD + direct: one more word consumed (console); what for is U15 - C11 SDBD + MVO: D ignored (console) - C12 MVO@ with the pointer as source writes the incremented value (console) vs old (MAME, jzIntv) - C13 jump with II = 11 taken and enables interrupts (console) vs not taken (MAME) - C14 GSWD/RSWD interruptible (manuals, wiki, MAME) vs not (jzIntv); undecided by hardware - C15 SIN interruptible (S1, MAME, jzIntv) vs not (one wiki page) - C16 INTRM/INTR edge vs level - C17 errors on single wiki pages - C18 branch not taken: BAR NACT NACT, no DTB (G82 Fig. 12a) - C19 which part "the CP1600" of each document means (clock) - C20 branch reach +1025 / -1022 (encoding) vs -1024 (S1) - C21 86 or 87 instructions - U1 opcode words with bits 15-10 set; U2 power-up values; U3 SDBD before a non-memory instruction; U4 pointer and destination the same register; U5 `MVO@ R7`; U6 where a stack read's extra cycles sit; U7 the TCI pulse's microcycle; U8 which PC `MVO R7` writes; U9 how late a request is still taken; U10 requests between interrupt entry and the first handler instruction; U11 BUSRQ again during the exit NACTs; U12 BDRDY on writes; U13 whether an interrupt ends a halt; U14 PCIT held across a multiword instruction; U15 SDBD + direct's eaten word; U16 where the 13th jump cycle sits; U17 bus value in an interrupt's NACTs; U18 opcodes `034-035`; U19 flags after two-position shifts; U20 the 1978 Mattel instruction notes. ## Proposed layout (the build session may refine) `src/cp1610/`: `cpu.py` (`CP1610`: step, boundary, interrupt entry, bus wait, reset, halt), `_core.py` (registers, flags, the phase helpers `_fetch`, `_read`, `_direct_read`, `_write`, `_direct_write`, `_read2` (SDBD), `_idle`), `_tables.py` (rung 0's data: the opcode map, forms, base cycles, the `console` additions), `_dispatch.py`, `_alu.py`, `_implied.py` (HLT SDBD EIS DIS TCI CLRC SETC, GSWD NOP SIN RSWD), `_single.py` (INCR DECR COMR NEGR ADCR), `_shifts.py`, `_regreg.py`, `_jumps.py`, `_branches.py` (including BEXT), `_external.py` (MVO MVI ADD SUB CMP AND XOR in every mode), `state.py`, `disasm.py`, `debug.py`, `trace.py`, `console.py`, `harness.py`, `__main__.py`, `py.typed`. Docs to add with the tooling: `api-stability.md`, `trace-schema.md` (JSONL; one record per microcycle with `phase` and `value`, plus one per instruction), `cpu-state.md`. ## The rung ladder The full table, with each rung's judge and tier, is in [validation.md](validation.md#the-rung-ladder). In order, one rung per commit or short series: | Rung | What | Exit test | | --- | --- | --- | | 0 | manuals to tables | `_tables.py` equals HW26's `g82_cycles.py` and the wiki table except rows with a C number; data sheets' AC tables and the 1978 scan read | | 1a | directed tests from S1/G82: every phase block, every family, every switch | `pytest -m "not slow"` green on CPython and PyPy | | 1b | HW26's console results (`hw_cpuodd`, the grant rule, SDBD jump 15) | each reproduced, citing HW_TESTS | | 1c | HW26's 42 directed programs via `as1610.py` or `as1600` | every `;! expect` met (cycles under `timing="console"`) or listed with its C number | | 2 | MAME Lua single-step corpus (detector) | zero differences outside MAME's known-difference list | | 3 | replay of MAME traces (Astrosmash, more titles, `unichamp` for BEXT) | registers, flags and cycle deltas equal over each trace, masks as listed | | 4 | second referee: lockstep with HW26's `tools/cp1610.py` bus log; Intv_MiSTer VHDL under GHDL | identical phases, or each difference listed | | 5 | tooling: `state`, `disasm` (against MAME's text), `debug`, `trace`, `console`, `harness`, `python -m cp1610` | the family's tooling tests; schema documents written | Rung 6 belongs to the board: HW26's ten console cartridges in the full machine. ## Decided by the user Nothing yet. No decision was passed to either Phase 0 session. ## Open decisions for Aubrey **Decided by Aubrey on 2026-11-12** (recorded after a walk through all fourteen Phase 0 briefs): - The Python package is `cp1610_python` (family rule set the same day: a repository `x-python` always imports as `x_python`); read any other package name in these documents as that. - Decision 1: package `cp1610_python`, class `CP1610`. Decisions 3 and 5: the edge latch and the `doc` default with `console` chosen by the board are **confirmed**. - Decision 12 (asking for one more console probe cartridge): deferred with the other outside contacts. - Referees built from die-derived material are built just before the rung that needs them, not up front. - Outside contacts proposed below are **deferred until the core or board exists**; Aubrey sends them himself, from a draft. - Remote: local through Phase 0; after the first passing rung or milestone, public like the other CPU cores, after the usual history audit. Where a recommendation below names a different point, this replaces it. - Every other recommendation below is **accepted as written** unless a line here says otherwise. Each with a recommendation. 1-3 block the first line of code. 1. **Names.** `cp1610-python` / package `cp1610` / class `CP1610`, one class for the CP1600, CP1600A, CP1610 and CP1610A (reasons under "Scope and names"). Alternative: `cp1600` (the family's name in GI's 1975 manual). **Recommend `cp1610`.** 2. **Bus contract shape.** One callable `bus(phase, value) -> int` per microcycle (above), which matches the board's point 1 and the hardware's one multiplexed bus. Alternative: one callable per phase (`bar(addr)`, `dtb() -> int`, `dw(v)`, ...), as huc6280-python splits read/write/idle: more calls to wire, no gain, since the board must keep its own latch either way. **Recommend the single callable.** 3. **INTRM edge or level** (C16; the board's point 9 asks for levels). The core latches a falling edge and clears it when taken or when the line rises (`INTRM_EDGE = True`), which matches the wiki, MAME, HW26 and the console's row W, and works with either STIC model the board might pick (MAME/HW26: INTRM held 3,790 cycles; jzIntv: released at acknowledge). A pure level re-enters the handler under the first STIC model. **Recommend the edge latch as default, level behind the switch**, and the board's bus.md updated to say so. 4. **Where IAB falls after reset** (C1; the board's point 4). S1's figure and O81 put it second (NACT IAB NACT NACT NACT). **Recommend S1's sequence**; the board only answers IAB, so nothing in it depends on the position. 5. **Default cycle table.** `doc` (the manuals, and MAME, so the rung-3 replays agree) as the core's default; the Intellivision board passes `timing="console"` (HW26's three one-cycle additions, the only counts measured on a console). Alternative: `console` as default. **Recommend `doc` default, `console` chosen by the board.** jzIntv's wider +1 rule and 12-cycle interrupt are not offered: they make the interrupt path 3 cycles longer than the console measurement. 6. **Interrupt sequence length** (C2): 9 (S1, G82), switch `INTERRUPT_CYCLES`. HW26's fit also allows 11 with documented jump and PULR counts. **Recommend 9**, with `console` carrying the 2 cycles on the jump and PULR as HW26 does. 7. **GSWD and RSWD interruptible** (C14): manuals, wiki and MAME say yes, jzIntv no, no hardware test covers it. **Recommend yes**, switch `GSWD_RSWD_INTERRUPTIBLE`, flagged for the board's rung 6. 8. **Unsupported cases** (C6, C7, C9-C13): follow HW26's console results and raise an `undefined` event. **Recommend yes.** U15 (what SDBD + direct does with the extra word) stays open. 9. **BDRDY, STPST and the PCIT input** after the first build (the Intellivision uses none). **Recommend later**, as attributes, no callback change. 10. **Depend on HW26 by pin** for rung 0's second transcription, rung 1b-1c's tests and assembler, and rung 4's referee (MIT, fetched into `reference/`, run with `python -I`, never copied). **Recommend yes.** Writing a family assembler is not needed for rungs 0-5. 11. **Public or local.** **Recommend local until rung 1 passes, then public** (the chip cores went public for free CI; nothing third-party is committed). 12. **A console probe by someone else.** Two rows (C14 GSWD/RSWD, U15 SDBD + direct) would be settled by one more cartridge in HW26's style, and its owner has the console. Asking means an external post (name the model; see the upstream AI-policy notes). **Recommend: Aubrey's call; not needed to build.** 13. **Rust and `-fast` forks.** Not now; the board needs correct first.