# Handoff: build `apple2-python`, the Apple II family 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 Apple II is era 1 in the charter's table: one clock domain, cycle level everywhere, Python runs the tests, Rust plays. Boards to model this one on: **famicom-python** (a 6502 board where each bus callback is one CPU cycle and the PPU runs inside it), **a2600-python** (a 6502 board with a floating bus and a picture made by the beam), **pcengine-python** (firmware as a program to run, identified by SHA-1), **sega32x-python** (how an add-on board pins a base board: the IIgs later). Cores it embeds, read and not touched by this phase: **6502-python** at `828b619`, **w65c02-python** at `fca677f`, **ay8910-python** at its published main (`76cace3`). Read in order before writing code: 1. [README.md](../README.md), [start-here.md](start-here.md). 2. [cpu.md](cpu.md), [memory.md](memory.md), [video.md](video.md), [disk-ii.md](disk-ii.md), [peripherals.md](peripherals.md), [iigs.md](iigs.md). 3. [domains-and-speed.md](domains-and-speed.md). 4. [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` (10 files, about 190 MB, and two sparse repositories; `--verify` to check). Text versions of every document land in `reference/text/` for grepping; Sather's books are two-column OCR, so read the page images (`pdftoppm`) for any table you transcribe as facts. ## Decided by Aubrey (standing rules that apply here) - Name `apple2-python`, package `apple2_python` (the x-python naming rule). - Local, no remote, until he says otherwise. Bring your own ROMs and disks; nothing of Apple's in the repository, and nothing from a repository that carries Apple's code (a2audit's disks and Monitor routines, apple2js's `js/roms/`). - 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; romsets by environment variable. - Technical choices are made, marked provisional and reported; policy waits for him. ## Proposed by this Phase 0 - **The order of rows** (open decision 1): **II+ first** (Applesoft, Autostart, 48K, language card, Disk II 16-sector, Revision 7 video), with the **II** (Integer BASIC, old Monitor) and the board revisions (0, 1, 7, RFI), the Europlus (50 Hz) and the IIj-plus as machine rows from milestone 1 because they differ only in ROMs and settings; then the **Mockingboard**; then the **IIe** (unenhanced, 6502) and the **enhanced IIe** (65C02) as rows of the same board with the MMU/IOU; then the **IIc** (IWM, built-ins) and **IIc Plus**; the **IIgs** as a separate board pinning this one. Why this order: the II+ is the base every later model keeps compatible with, most of the library runs on it (census: 54 of 134 WOZ2 5.25" images are marked for the II+ or earlier, and DOS 3.3 sector images boot on it), and its hardware is fully covered by Sather's first book; the IIe needs the second book's MMU/IOU detail. - **The video output contract**: the 1-bit 14M signal per line plus burst and mode, not colours (open decision 5; [video.md](video.md)). - **The Disk II from the player's P6 PROM** (open decision 3; [disk-ii.md](disk-ii.md)). - **Images identified by content**, firmware by SHA-1. ## Package and classes (proposed) ``` apple2_python/ machine.py # machine rows: model, board revision, ROM SHA-1s, CPU class, RAM, 50/60 Hz, keyboard layout console.py # build(machine, rom_dir=..., slots={6: DiskII(...), 4: Mockingboard(...)}) -> Apple2 clock.py # 14M time, the long cycle, Q3 edges, PHASE 0/1 bus.py # page table, soft-switch dispatch, the floating bus, slot select/strobe, $CFFF langcard.py # LanguageCard: banks, PRE-WRITE counter (II/II+ slot 0; built in on IIe/IIc) mmu.py, iou.py # the IIe's chips (rows IIe, IIe enhanced, IIc) video.py # Scanner (counters, address, fetch), VideoGenerator (modes, delays, char ROM, 14M bits) keyboard.py # encoder rows (MM5740, AY-5-3600, IIe AY-3600 + keyboard ROM) gameport.py # 558 timers, buttons, annunciators, strobe speaker.py # toggle log; cassette out/in slots.py # SlotCard Protocol and the socket signals (IRQ, NMI, RDY, DMA, INH) diskii.py # DiskIIController (P5, P6, sequencer, data register, switches, motor timer) drive.py # Drive525 (stepper in quarter tracks, spindle, MC3470 model, write path) images/ # woz.py, dsk.py (DOS/ProDOS order, 13-sector), nib.py, gcr.py mockingboard.py # two VIAs (mos6522-python, decision 4) + two ay8910-python AY8913s savestate.py # whole-machine states naming ROMs and disks by hash ``` ## What the board needs from its cores (the embedding contracts) From **6502-python** (`MOS6502`) and **w65c02-python** (`G65SC02`): the two bus callables, one call per cycle with dummy accesses (exists); the IRQ level and NMI latch API with the poll at the end of the second-to-last cycle (exists); `note_stall` for RDY (exists; enough for the SoftCard row is to be checked then); per-chip `ane_constant` and `lxa_constant` for the NMOS part (exists; values U5). **Nothing new is requested from either core now.** From **ay8910-python**: `AY8913` clocked once per CPU cycle (exists). From a future **mos6522-python** (decision 4): a cycle step, register access, port pins as callbacks, CA/CB lines, IRQ output, a state snapshot. ## The gate (defined at milestone 1, before any card) The board's lockstep case: the II+ from power-on to the Autostart screen and the Disk II's boot read loop, its CPU instruction stream and every $C0xx access equal to MAME's `apple2p` trace ([mame-oracle.md](mame-oracle.md)) over 300 frames, or a named reason for each difference; its unit tests of the documented facts (17,030 cycles a frame, the long cycle's position, the scanner's 17,030 addresses against Sather's maps, the language card's PRE-WRITE table); later its picture rows against Sather's figures and the detectors. With every slot empty, the base board must not move these when a card lands. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | **Clock, bus, scanner, soft switches, keyboard, speaker, II+ ROMs; the II and revision rows** | UA2 (3), A2RM (2); MAME trace (5) as detector | the Autostart screen's text page equals MAME's snapshot; 300 frames of instructions and $C0xx accesses equal MAME's or each difference named; unit tests of the long cycle, the 65 x 262 counters and the scanner address for every state | | M2 | **Video generator**: TEXT (normal, inverse, flash), LO-RES, HI-RES with the delay bit, MIXED, the three-RAS' mode-switch delay, the floating bus | UA2 ch. 3, 5, 8 (3); PAT-VIDEO (2); MAME and Clock Signal pictures (5) | Sather's split-screen program (UA2 Fig. 3.10) produces its documented picture; a floating-bus program (a2audit `floatbus`) syncs; the 1-bit signal of the boot screen matches MAME's monochrome picture pixel for pixel or each difference triaged | | M3 | **Language card** | UA2 p. 5-29 (3); a2audit's language-card tests (3) | every row of Sather's command table; a2audit's language-card section passes (built from its source with ACME, or its routines run from memory) | | M4 | **Disk II**: controller, P5 boot, P6 sequencer from the player's PROM, the drive (stepper, spindle, MC3470 model, motor delay), WOZ, DSK/DO/PO/D13/NIB | PAT-DISK (2), UA2 ch. 9 (3), WOZ notes (3); MAME (5) | the boot read stream of a WOZ image equals MAME's $C0EC stream to the first named difference; DOS 3.3 and ProDOS boot from sector images; a smoke list of 10 WozADay protected images (problem ones and well-behaved ones: the smoke-gate rule) reach their title screens; writing a sector and reading it back | | M5 | **Mockingboard** (with `mos6522-python` or an in-board VIA by decision 4) | VIA and AY data sheets (2); mb-audit on real cards (3) | mb-audit passes to its end on the board in its Mockingboard C configuration, or each stop named | | M6 | **IIe** (unenhanced): MMU, IOU, auxiliary memory, 80 columns, double hi-res, VBL at $C019, IIe keyboard | UA2E (3), A2ETRM (2); a2audit (3) | a2audit passes on the IIe row; the IIe self-test (from the player's ROM) passes | | M7 | **IIe enhanced** (w65c02-python `G65SC02`) and Platinum | as M6; mb-audit's 65C02 false-read tests | a2audit and mb-audit pass on the enhanced row | | M8 | **IIc** (ROM 255, 0, 3, 4): IWM, built-in serial and mouse, the 3.5" drive later | A2CTRM (2) | the IIc boots DOS 3.3 and ProDOS through the IWM; its self-test passes | | M9 | **Save states, smoke list grown from failures, cards as rows** (Thunderclock, Super Serial, mouse, SoftCard) | per card | a save state loads into a fresh machine and continues identically; the smoke list passes | Full library sweeps only when the board might be ready (Aubrey's smoke-sweep rule). ## The chip rule | Chip | Users in or near the family | Proposal | | --- | --- | --- | | NMOS 6502 | many | `6502-python` (exists) | | 65C02 | Lynx, BBC Master, PC Engine (as a cell), Apple IIe enh./IIc | `w65c02-python` (exists) | | 65C816 | SNES, IIgs | `65816-python` (exists; the IIgs board) | | Z80 (SoftCard) | many | `z80-python` (exists) | | AY-3-8913 | Mockingboard; the 8910 family across the family | `ay8910-python` (exists) | | 6522 VIA | Vectrex (planned in board), Mockingboard, mouse card; later Commodore and Acorn machines | **`mos6522-python`** on this second consumer (decision 4) | | Video generator, IOU, MMU, language card, Disk II controller and drive, keyboard encoders, 558 game port | Apple II family only | stay in the board | | IWM | IIc, IIgs (Macintosh, outside the plans) | stays in this board; the IIgs board pins this one | | Ensoniq 5503 DOC, VGC, FPI, Mega II, SCC | IIgs | the IIgs board | | IIgs ADB microcontroller (an M50740-family part with its own ROM, census: `341s0632-2.bin`) | IIgs | a CPU: its own core if it is run from its ROM (decision for the IIgs Phase 0) | ## Unknowns (U) and conflicts (C) **Unknowns** (the best source available for each; none settleable without hardware unless noted): | # | Unknown | Best source now | Status | | --- | --- | --- | --- | | U1 | Power-on contents of RAM (DRAM pattern) and of the video and soft-switch latches | UA2 ch. 5 (to read) | follow Sather, option for random | | U2 | The scanner's exact adder (the OCR of UA2 pp. 5-8..5-9 is garbled at the carry input) | UA2 Figure 5.4 and the maps of Figures 5.5-5.10 | read the page images; unit test all states | | U3 | The IIc Plus's CPU and accelerator: part, cache, slow-down rules | A2CTRM 2nd ed.? (to check) | later row | | U4 | What Apple's IIc manual says about the Rockwell bit instructions | A2CTRM (to read) | cite before relying | | U5 | `ane_constant`/`lxa_constant` of the Apple II's NMOS parts (MOS, Synertek, Rockwell) | none | $EE default, HYPOTHESIS; a test program on a real II would settle it | | U6 | The IIe's language-card state after RESET | UA2E ch. 5, A2ETRM ch. 4 (to read) | | | U7 | Bits 0-6 of IIe status reads ($C011-$C01F); which IIe revisions have IOUDIS/DHIRES | UA2E ch. 7, A2ETRM p. 61 footnotes (to read) | settle before M6 | | U8 | The 13-sector controller's P6 (MAME pairs 341-0009 with 341-0028) | none in hand | 13-sector row deferred until a source says | | U9 | The 14M positions of Q3's falling edges within a CPU cycle, the long cycle included (the sequencer's clock) | UA2 ch. 3 timing diagrams (Figure 3.2) | read the figure; matters for WOZ timing | | U10 | Quarter-track stepping with adjacent phases on, the head's response time | WOZ (TMAP: quarter tracks), UA2 p. 9-14 ("approximately 20 milliseconds") | FITTED until a source | | U11 | Spindle speed tolerance and spin-up time | WOZ (300 rpm; "almost 5 full revolutions" in 1 s) | nominal, FITTED | | U12 | The Mockingboard's wiring of VIA ports to the AYs and its IRQ routing per model | the card's manual (not yet found) | read before M5 | | U13 | The Thunderclock Plus's clock chip | its manual | later row | | U14 | The IIgs's ADB microcontroller part and clocking | GSHR | the IIgs Phase 0 | | U15 | Which a2audit expectations were run on which real models | its `notes.org`, its author | ask or read before calling it tier 3 for a given test | **Conflicts** (sources disagree): | # | Conflict | Sides | Proposal | | --- | --- | --- | --- | | C1 | The CPU clock | A2RM: "1.023 MHz"; UA2 p. 3-18: 1.0205 MHz average, 1.0227 MHz for normal cycles, the manual "confuses frequency with time periods" | Sather: model 14M periods and the long cycle; 1.023 MHz is the normal-cycle rate | | C2 | Disk encoding in the patent | PAT-DISK's preferred embodiment stores a byte as two nibbles of four data and four clock bits; shipped DOS uses 5-and-3 then 6-and-2 GCR on the same hardware (UA2 ch. 9) | the hardware is the sequencer and PROM; encodings are software. Follow the shipped PROM | | C3 | An empty drive's reads | MAME `apple2p` returns $80 at $C0EC forever with no disk; WOZ: with no flux the MC3470 produces random bits | the WOZ author's analysis (3) over MAME (5); an empty drive gives noise through the sequencer, seeded | | C4 | The IIc's disk hardware | A2CTRM: an IWM; MAME `apple2c` lists a "Disk II NG controller" in "sl6" | the manual | | C5 | The text flash rate | II: a "2 Hz text flasher" (UA2 ch. 8); IIe: vertical/32 = 1.87 Hz, by Sather's inference (UA2E Table 3.3) | different machines: per-row settings; the IIe figure tagged HYPOTHESIS in its source | ## Domains and speed (Full page: [domains-and-speed.md](domains-and-speed.md).) - **Domains**: one. Everything runs from the 14.31818 MHz crystal: CPU (PHASE 0 with the long cycle), video scanner (PHASE 1 fetch each cycle), Disk II sequencer (Q3, only with a drive on), slot cards. Order of record per CPU cycle: scanner fetch, the sequencer's Q3 edges in that cycle, cards' per-cycle work, the CPU's access, the interrupt poll. - **Channels** (named for `-fast`): video fetch -> picture (a producer from a mode-switch log; the floating-bus byte on demand from cycle and RAM); the Disk II data register (sequencer catch-up to each controller access: the bus trace is unchanged, so this is `-fast`, not `-turbo`); speaker and PSG audio (producers); VIA timers and the 558 (catch-up, next IRQ as a sync point); slot DMA (a sync point, later). - **Instructions per second**: 14,318,180 x 65 / 912 = 1,020,484 CPU cycles a second; MAME's traces average 2.53-2.61 cycles per instruction on boot code, typical code 3-4: **0.3-0.4 M instructions a second**, plus 1.02 M scanner fetches, plus **2.04 M sequencer steps a second while the drive runs**, plus 4 x 1.02 M chip ticks with a Mockingboard. The IIgs: about 0.8-1.0 M 65C816 instructions a second. - **No-JIT path**: Python on PyPy near or above real time (HYPOTHESIS, from famicom-python's 0.89x with three times the events); `-fast` many times real time on desktop, real time with margin on a phone (to measure), real time in the browser (as play-2600's wasm build). **`recompile: none` everywhere; no cached interpreter; threads optional, producers only; no `-turbo` expected for the II/IIe/IIc.** ## Open decisions for Aubrey (each with a recommendation) 1. **The order and shape of rows.** Recommend the II+ first with the II, its board revisions, the Europlus and the IIj-plus as machine rows from milestone 1; then the Mockingboard; then the IIe and enhanced IIe as rows of this board; then the IIc and IIc Plus; **the IIgs as a separate board `apple2gs-python` pinning this one**, started after the IIe rows pass (it is a 16-bit machine with its own video, sound and timing). 2. **Firmware handling.** Recommend the same rule as every board: the player's MAME sets or loose ROMs, identified by SHA-1 (census.md lists them), no built-in or replacement ROM. (A free replacement Monitor/BASIC would be its own project.) 3. **The Disk II's P6 sequencer as the player's PROM, run as data**, not a built-in table. Recommend yes: the PROM is Apple's firmware; a table in our source would be a copy of it. The P6 is in every MAME Apple set and the `a2diskiing` device. 4. **The 6522.** Recommend building it once as `mos6522-python`, by whichever of the Vectrex board and this one reaches its VIA first, with its own ladder (data sheets, VICE's VIA tests, mb-audit), pinned by both; it should not be written twice. 5. **The video output contract.** Recommend the board emits the 1-bit 14M signal per line with the burst and mode, and colour decoding lives in the host as named decoders (monochrome, idealised palette, an NTSC filter). This keeps every hardware claim testable and every colour choice out of the specification. 6. **Protection test corpus.** The WOZ test-image set (applesaucefdc.com) and the census's WozADay images are commercial software imaged from real disks. Recommend using only the player's local copies through `APPLE2_DISK_DIRS` (never fetched or committed), with a smoke list naming images by SHA-1; ask Aubrey whether the WOZ test set may be fetched into `reference/` (git-ignored) as other boards fetch test ROMs. 7. **MAME's BSD-3-Clause Apple II files.** Every Apple II file checked (drivers, Disk II, WOZ format, IWM, Mockingboard, 6522, ES5503) says `license:BSD-3-Clause`. Recommend keeping them closed boxes for the specification (Sather, Apple, the patents and the WOZ notes are better evidence), and allowing them, like Clock Signal, as a readable tier-5 comparator for `-fast`/tooling only if a difference needs explaining; the family's PlayStation Phase 0 left the same question open. 8. **a2audit's build.** Recommend building the ACME assembler (its licence to check: if it is GPL, it lives in emu-tools-gpl, and running it never touches our licence) in the tool repositories when M3 needs a2audit, building `audit.bin` from source without Apple's DOS, and loading it straight into memory, so no DOS image is needed. 9. **`APPLE2_DISK_DIRS`** as a new variable in the family's environment profile (the Apple II library is spread over five collections outside No-Intro and Redump). Recommend adding it to `profile-env`. 10. **sources.md rows** (emulator-lineage, Aubrey's register): the eleven rows proposed at the end of [sources.md](sources.md), including a2central.com as "set aside (site's wishes)".