# Handoff: build `f8-python`, the Fairchild F8 core ## Context you are inheriting The family's shape: chips are cores, boards compose cores, CPUs always get their own core (charter, emulator-lineage `docs/charter.md`). This core's first user is `channelf-python` (`../channelf-python`, Phase 0 done in parallel; do not edit it from here). A board that finds a corner of this core raises it **on the core**. Era 1 (charter): one clock domain, cycle level everywhere. Python runs the tests and, by the estimate below, probably plays in real time; the Rust tiers are for the browser and phones. The F8 is the family's first **multi-chip CPU**: the program counter, stack register and data counter live in the memory chips, which follow the CPU through five ROMC lines ([romc-and-the-psus.md](romc-and-the-psus.md)). The specification models that literally. Models to read before writing code: - `z80-python` (`../z80-python`): the readability contract (one instruction family per module, one mnemonic per handler docstring, the hardware reason as a comment on the line that encodes it), tooling bar, docs layout, trace format. - `mcs48-python` (`../mcs48-python`): a small 8-bit controller core with on-chip ports and a timer, the nearest family relative in size. - `sh2-python` (`../sh2-python`): a decode table **generated** from the manual's tables (`scripts/gen_decode.py`) and timing as data. Copy the shape, never the code. - emulator-lineage `docs/proposals/2026-11-jit.md` (accepted 2026-11-25): one pure-looking handler per decode row, named state, timing as data. For an era-1 CPU no JIT or cached interpreter is planned (below), so do not bend anything for one. Rules: Python >= 3.11, CPython and PyPy; modern forms; no dangerous-looking shell one-liners; local git only; MIT; nothing read from GPL emulators; MAME's BSD-3 F8 files stay closed until Aubrey decides (Open decision 3); no BIOS or game bytes committed; run Python that reads `reference/` with `-I`. ### Read in this order README.md; docs/start-here.md; docs/cpu.md; docs/romc-and-the-psus.md; docs/table3.tsv; docs/validation.md; docs/mame-oracle.md; then in `reference/docs/`: the 1982 data book's F3850 pages (PDF 27-51; Table 3 on 42-46, Table 5 on 47-48), F3851/F3856 (PDF 52-75) and F3852/F3853 sections; the *F8 Guide to Programming* (1977) sections 2, 6 and 8; the *F8 User's Guide* (11/77) sections 2-5 (no text layer: `pdftoppm -r 200 -gray` + `tesseract`, pages 70-72, 98-99, 119-121 already located). ## Scope In: the F3850 CPU (A, W, ISAR, 64-byte scratchpad, ports 0 and 1, decode, interrupt logic, reset); the ROMC bus protocol; the **F3851 PSU** (1 KB ROM, page-select mask, PC0/PC1/DC0, ports A/B, interrupt control, polynomial timer, mask-option vector); the **F3853 SMI** (PC0/PC1/DC0/DC1, programmable vector at ports 0Ch/0Dh, interrupt control 0Eh, timer 0Fh, an external static RAM behind it); the **F3856 PSU** (2 KB, DC1) as a variant of the F3851; a disassembler; trace and debug tools. Out (boards): what sits on the port pins (Channel F VRAM latches, controllers, sound); the RAM chips behind an F3853 (the board supplies a byte store); the F3852 DMI and F3854 DMA (no planned board uses them; add when one does); the F3870/F3872 single-chip parts. ## Proposed names - Repository `f8-python`, package **`f8_python`** (the 2026-11-12 naming rule). - `f8_python.F3850`: the CPU. - `f8_python.F3851`, `f8_python.F3856`: PSUs, constructed with their mask options (`rom: bytes`, `page: int`, `port_base: int`, `vector: int`). - `f8_python.F3853`: the SMI, constructed with its address range and a `memory` object (`read(addr) -> int`, `write(addr, value)`) the board supplies. - `f8_python.bus.RomcBus`: the shared lines (ROMC state, data bus, INT REQ, PRI daisy chain) and the list of attached chips; resolves each cycle. - `f8_python.memchip.MemoryChip`: the protocol every memory-side chip implements. - `f8_python.disasm`, `f8_python.trace`. ## The core boundary (proposal, to settle in the first commit) The hardware has no address bus, so the boundary is the ROMC bus, cycle by cycle: ```python class MemoryChip(Protocol): def drive(self, romc: int, cpu_byte: int | None) -> int | None: ... def latch(self, romc: int, bus: int, length: int) -> None: ... def int_req(self) -> bool: ... # this chip's interrupt request (after PRI IN) ``` - `F3850.cycle()` decides the next machine cycle from its decode state and returns `(romc, length, cpu_byte)`: the ROMC state, 4 or 6 phi clocks, and the byte the CPU itself drives (A for ROMC 05/0A/1A, a scratchpad byte for 14-19, 00h for 08, the port address in the cycle before 1A/1B), or `None`. - `RomcBus.run_cycle()` asks every chip `drive()`, resolves the data bus (exactly one driver is normal; **none** and **two or more** are recorded as bus events and resolved by a named provisional rule, Open decision 5), then calls every chip's `latch()` with the resolved value, and hands the CPU the value for the cycles where the CPU reads (fetch, operand, memory read, register read, port read). - Each chip owns its PC0, PC1, DC0 (and DC1 where it has one): **one copy per chip**, updated in its own `latch()`. Nothing outside a chip writes them. Debug views show each chip's copies and whether they agree. - Ports: the CPU's ports 0/1 and each chip's ports are `Port` objects with an output latch and an input function the board connects (pin logic: cpu.md "I/O port pins", U8). - Time: the bus accumulates phi clocks; `run(phi_budget)` returns at an instruction boundary; the board advances its own parts (video, sound) by the returned count. Ports report writes with the phi stamp at which they happen (the cycle's start plus the data sheet's write point, to settle from DB Figures 9-11). This puts the hardware's real seam in the code: the specification of "who answers a fetch" is the chips' page masks, exactly as on the board. ## Unknowns (U) and contradictions (C) - U1 Opcodes 2Dh, 2Eh, 2Fh: not in Table 3. - U2 Register operand r = 15 (3Fh, 4Fh, 5Fh, CFh, DFh, EFh, FFh). - U3 INS/OUTS to ports 2 and 3 ("reserved"). - U4 What the CPU reads when no chip drives the bus (floating bus), e.g. `LR Q,DC` with DC0 outside every page. - U5 The electrical result of two chips driving different bytes (contention). - U6 The flags AMD and ASD leave (Table 3 says all four change; the Guide gives the adjust rule but not each flag). - U7 What reset clears besides PC0 and ICB (A, other W bits, ISAR, port latches). - U8 The port pin model: what a read returns from a port the program wrote (latch, pin, or both), and pull-ups. - U9 When the CPU drives ROMC 10 (no Table 3 row uses it). - U10 The F3851 timer: the polynomial sequence, prescaler and what happens on overflow (UG 3.5 not yet read in full). - U11 Whether an interrupt request is sampled at the start or end of the last cycle. - (U12, settled 2026-11-26) The F3856 has DC1 and swaps it on XDC (DOC: DB F3851/F3856 sheet, "Data counter DC1 is provided on the F3856 as an additional ...", PDF p. 57). So a cartridge built from F3856s next to the console's F3851 BIOS PSUs shows the XDC divergence. - C1 The no-interrupt-after list: Guide 8.2.1 (PK, PI, POP, JMP, OUTS/OUT if not port 0 or 1, EI, LR W,J) vs Table 3 (adds LR P0,Q; marks OUT unconditionally). Default: Table 3, marked provisional, both selectable in the specification. - C2 veswiki's Opcode table gives PK 2.5 cycles; Table 3 gives L L S (= 4). Table 3 wins (tier 2 over tier 3). - C3 Table 3 gives DS as one long cycle with ROMC 00 (the fetch overlapped); community tables count "1.5 cycles", consistent. Not a contradiction, but an unusual shape the decode must not normalise. - C4 MAME keeps one PC0/PC1/DC0/DC1; the hardware keeps one per chip (romc-and-the-psus.md). A limit of the detector, recorded so nobody "fixes" the core to agree with MAME. ## Milestones (rungs; exit tests in validation.md) 0. Tooling and decode generated from table3.tsv; disassembler text equal to MAME's on the recipe's traces. 1. Single-chip semantics from the documents, one case per Table 3 row and Guide example. 2. The memory chips and the ROMC protocol: the UG 4.3.3 tables reproduced. 3. MAME lockstep on the BIOS and cartridges (players' images via `MAME_ROMPATH` / `NO_INTRO_DIR`). 4. Timing per instruction and over loops. 5. Interrupts and timers. 6. Seeded faults, each caught. ## Domains and speed **Domains.** The F8 system is one domain: the CPU and every memory chip move together on the CPU's phi clock, cycle by cycle; nothing in them runs on another clock. The core's channels to a board are its **ports** (each write and read, with its phi stamp) and **INT REQ** (raised by a chip, sampled by the CPU). Those are the sync points a board declares. On the Channel F the board's video scan-out and tone generator are pure producers fed from port writes (channelf-python docs/domains.md), so the whole machine is one domain plus producers. **Instructions per second needed (arithmetic).** - phi = 3,579,545 Hz / 2 = **1,789,772.5 Hz** on NTSC units (MAME `-listxml`: 1,789,772; the schematic's 3.58 MHz crystal; OBSERVED + DOC) and 2,000,000 Hz on PAL units (OBSERVED MAME). - The fastest instruction is one short cycle = 4 phi, so the **ceiling is 1,789,772 / 4 = 447,443 instructions/s** (500,000 on PAL), and at most the same number of machine cycles (ROMC states) per second. - Real code is slower: MAME runs the BIOS's polling loop at **211,186 instructions/s** (8.4 phi per instruction, OBSERVED mame-oracle.md). - ROMC fan-out: a Channel F has 3-5 memory chips (two BIOS PSUs, one to three cartridge chips), so the specification makes at most 447,443 x 5 x 2 = **4.5 M chip calls per second** (drive + latch) at the ceiling, about 2 M on the measured mix. **Does the no-JIT path reach real time?** | Target | Expected (estimates, HYPOTHESIS until measured) | Need | Verdict | | --- | --- | --- | --- | | f8-python, CPython | ~0.3-1 M instructions/s (m6800-python measures 3-4.5 M on CPython without per-cycle fan-out; the ROMC fan-out costs a few calls per chip per cycle) | 0.21-0.45 M | about real time, maybe below at the ceiling | | f8-python, PyPy | ~5-20 M (m6800-python: 45-80 M on PyPy) | 0.45 M | 10x real time or more | | f8-rust (transcription), desktop native | tens of M (z80-rust ~50 M, sh2-rust 30-47 M, measured) | 0.45 M | 50-100x real time | | f8-rust, browser wasm (phone) | ~0.5-0.6x native (sh2-rust wasm measured 0.55-0.65x), then a phone at ~0.8x desktop | 0.45 M | still tens of times real time | | low-cost ARM handheld (~0.2x desktop) | several M | 0.45 M | real time with room for run-ahead | So `recompile: none` everywhere; **no cached interpreter, no JIT, no threads are needed**, and by the charter's rule ("tiers exist only where a target needs them") an `f8-rust-fast` should be built only if the Channel F board's measurements on a weak target call for one. The interesting cost is the ROMC fan-out, which `-fast` could collapse (one shared register file while all chips agree, splitting only after an XDC) without changing a cycle. ## Open decisions for Aubrey 1. **The memory chips (F3851, F3856, F3853) live in f8-python, not in the board.** Recommended: they hold the F8's program counter and data counter; without them the CPU cannot fetch an instruction, every F8 board needs them, and their ROMC behaviour is CPU specification, not board wiring. The board supplies only mask options (ROM bytes, page, port base, vector) and what hangs on their ports. 2. **Per-chip register copies in the specification (one PC0/PC1/DC0/DC1 per chip), not one CPU-side copy.** Recommended: it is what Fairchild documents (UG 3.1.4, 4.3.3), it makes the XDC divergence and the no-driver read emerge instead of being special-cased, and the cost is affordable (Domains and speed). MAME's single copy is a detector limit. 3. **MAME's BSD-3 F8 files (`cpu/f8/f8.cpp`, `f8dasm.cpp`, `machine/f3853.cpp`) stay closed.** Recommended: closed, as for the PlayStation; Fairchild's tier-2 documents are complete enough for the instruction set, and closed files keep MAME an independent detector. 4. **libpressf (MIT) as a second detector, after a provenance check.** Recommended: yes, run it as a detector only (not read for code) until Aubrey accepts its relicensing from the author's own GPL-3.0 Press_F (sole author on GitHub; one outside commit on Press_F by rzumer). BizHawk's ChannelFHawk: leave out until its provenance is stated. 5. **Default provisional rules for the unknowns** (Aubrey's 2026-11-12 rule): U1/U2/U3 act as NOP of one short cycle and are logged; U4 floating bus reads FFh; U5 contention resolves as wired-AND and is logged; C1 follows Table 3. Each marked HYPOTHESIS in code. 6. **No hardware probe** (emu-only rule) unless Aubrey wants an F8 exerciser cartridge written for a Channel F owner to run; recommended: write it later as a homebrew test program in the board's repository, offered to the community, since it would be the only route above tier 2. 7. **f8-rust as a transcription after rung 3; no f8-rust-fast unless measured need.** Recommended (Domains and speed).