# Handoff: build `mac-python`, the compact Macintosh 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 this repository's [start-here.md](start-here.md). The compact Mac is an era-2 machine: one 68000, one clock, every bus cycle observable, `recompile: none`. Boards to model this one on: **amiga-python** (Phase 0: a 68000 sharing chip RAM with DMA by slots, CIA accesses on the E clock: the same two problems), **megadrive-python** (a 68000 board with ROMs by SHA-1 and save states naming them by hash), **apple2-python** (Phase 0: the Disk II, of which the IWM is the integrated form; WOZ images, a drive model per bit cell). The core it embeds, read and not touched by this phase: **m68000-python**. Read in order before writing code: 1. [README.md](../README.md), [start-here.md](start-here.md), [memory-and-bus.md](memory-and-bus.md). 2. [video-and-sound.md](video-and-sound.md), [floppy.md](floppy.md), [peripherals.md](peripherals.md), [cores.md](cores.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` (19 files, about 140 MB, and four sparse repositories; `--verify` to check). Text versions of the PDFs land in `reference/text/`; the IWM specification is a scan without text: OCR it (`ocrmypdf --sidecar`, as this phase did into `reference/ocr/`) or read its page images. ## Decided by Aubrey (standing rules that apply here) - Name `mac-python`, package `mac_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. Firmware by SHA-1 only. - 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 are made, marked provisional and reported; policy waits for him. - Fixes start upstream: a 68000 behaviour found here goes to m68000-python with a repro. ## Proposed by this Phase 0 - **The Plus first** (`macplus`, ROM v3 `4D1F8172`, 1 MB, 800K drive, SCSI present but empty), then the 128K/512K (64K ROM, 400K drive with PWM speed) and 512Ke as machine rows from milestone 1; the SE as a later row (BBU, two-word video, ADB). - **One timeline in C8M clocks** (the 68000's), with the pixel clock at two per clock and the E clock at one per ten. - **The glue as the specification's spine**: one function says, for every C8M clock, whether RAM belongs to the video, the sound fetch or the CPU; every CPU stall is a row of the per-cycle trace the Rust tiers must match. Its phase constants are named, tagged HYPOTHESIS, and switchable until U3/U4 are settled (C1). - **The keyboard at protocol level** first; the 8021/8048 on mcs48-python later behind the same socket. - **Disks**: raw and Disk Copy 4.2 sector images through a track synthesiser first; MOOF (bit streams) second, as the native drive format. ## Package and classes (proposed) ``` mac_python/ machine.py # machine rows: ROM SHA-1s and checksums, RAM sizes and decode, drive type, SCSI, RTC model, BBU/PAL console.py # build(machine, rom=..., disks=[...], scsi=[...]) -> Macintosh bus.py # the decode (overlay, mirrors, byte lanes, DTACK/VPA), phase read, the $F80000 probe answer glue.py # the slot table: video / sound / CPU ownership per clock, CPU stall arithmetic, interrupt priority (/IPL) video.py # the scan counters, the word fetch, VBL and H4 outputs, the picture producer interface sound.py # the sound/disk-speed fetch, PWM, volume, PB7 gating (good enough) via_wiring.py # the 6522 core's pins wired to the board (overlay, page bits, SEL, RTC, mouse, keyboard, VBL, one-second) scc.py # Z8530: registers, BRG, external/status (DCD) interrupts, /W/REQ iwm.py # IWM: state bits, mode/status/handshake/data registers, the data separator, write path, timer drive.py # Sony400k (PWM speed, TACH) and Sony800k (zones, two heads): registers via CA0-2/SEL/LSTRB, the bit stream under the head gcr.py # 6-and-2 GCR, the sector and track layout, the 3-byte checksum (DRV400) images.py # raw 400K/800K, Disk Copy 4.2, MOOF readers -> tracks rtc.py # the clock chip: serial protocol, seconds, PRAM, write-protect, one-second output keyboard.py # KeyboardProtocol: M0110/M0110A command/response over CB1/CB2 with IM3's timing mouse.py # quadrature X1/X2/Y1/Y2 + button from host motion scsi.py # NCR 5380 and the ScsiTarget socket (raw hard-disk image) sockets.py # socket Protocols (drive, keyboard, mouse, SCSI target, serial) and test fakes savestate.py # whole-machine states naming the ROM and images by hash ``` ## What the board needs from its cores (the embedding contracts) From **m68000-python** (one instance; [cores.md](cores.md)): 1. Byte/word bus callables with `step_clocks` readable inside them (has); the board keeps a running stall count per step to place later accesses (as amiga-python plans) — or R1, a callback-returned wait, if that bookkeeping proves fragile. 2. `acknowledge(level)` -> AUTOVECTOR for levels 1-7 (has; the E-clock acknowledge wait is modelled). 3. **R2: VPA data cycles** for the VIA, with the same E-clock rule as the acknowledge; to raise with the IFR-loop repro before milestone 2 (shared with amiga-python). 4. `reset_devices()` for RESET (has; what it resets on the Mac is U6). From **mos6522-python** (to be created; the shared VIA core): phase-2 ticks, port pins with direction and external inputs, CA1/CA2/CB1/CB2, the timer-1 PB7 output, the shift register clocked from CB1, IRQ. From **mcs48-python** (only with open decision 3): the 8021 and 8048 with port pins. ## The gate (defined at milestone 1, before any add-on) The board's lockstep cases: the Plus ROM's instruction stream against MAME's trace (with `curpc`), `0 unexplained` over the first N instructions; then, because MAME charges no RAM contention, **the lockstep compares instruction order and register state, not clocks**, once contention is on (the clock difference is the named concession of the detector). Unit tests for every documented fact (map, overlay, IWM register select, RTC commands, keyboard codes). Then the ROM's own self-test reaching the "?" disk with no sad-Mac code, and a System 6 boot from a raw 800K image. With the SCSI bus empty and no external drive, the base board must not move these when an add-on lands. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | **Bus, decode, overlay, ROM by SHA-1, RAM sizes, the glue's slot table (contention switchable), interrupt priority, the VIA wiring on a stub 6522 (until mos6522-python exists), the RTC, the sockets and their fakes** | IM3, IM4, GMFH (2); MAME trace (5) as detector | the Plus ROM runs from reset through its RAM test with the instruction stream equal to MAME's (contention off) for the first 120 frames, or a named reason for each difference; map facts as unit tests | | M2 | **Video and the real VIA**: scan counters, word fetch, VBL/H4, the picture; mos6522-python pinned; R2 in m68000-python | IM3 (2); MAME pictures (5) | the grey desktop and the pointer; the ROM's VIA wait loops take E-aligned time; picture equal to MAME's frame 900 on the 128K (the "?" icon) | | M3 | **IWM and drives, track synthesis**: state/mode/status/handshake, the data separator per FCLK, the 800K drive with zones, raw and DC42 images | IWMSPEC, DRV800, IM3 (2) | a System 6.0.8 raw 800K image boots to the Finder on the Plus row; IWM mode $1F and the status reads as observed | | M4 | **The 400K drive with PWM speed**, the 128K/512K rows, the sound fetch and PWM audio | DRV400, IM3 (2) | an MFS 400K image boots on the 128K row with the drive speed following the buffer; the startup beep audible; TACH within the driver's tolerance | | M5 | **SCC, mouse, keyboard protocol** | SCCTM, IM3 (2) | the pointer follows host motion; typing in a desk accessory; the SCC's DCD interrupts as the ROM expects | | M6 | **SCSI** (5380 + raw hard-disk target) | IM4, NCR5380 (2) | the Plus boots System 6 from a SCSI disk image | | M7 | **MOOF images**, the drive per bit cell, weak bits as a named model | MOOF (2-3) | a MOOF original from a player's collection loads (smoke list from `mac_flop_orig`: open decision 7) | | M8 | **The interleave settled** per U3/U4's evidence; the SE row (BBU, two-word video, ADB HLE) | per decisions 6 and 8 | contention constants tagged with their evidence; the SE ROM reaches the "?" disk | ## Unknowns | # | Unknown | Where it bites | How to settle | | --- | --- | --- | --- | | U1 | the value read from unmapped space (every address answers /DTACK or /VPA) | ROM probes, buggy software | a probe (decision 8); meanwhile the last bus value, HYPOTHESIS | | U2 | the phase-read and phase-adjust behaviour (the ROM's check; 128 ns per SCC word access) | boot | trace what the ROM reads at $F00000-$F7FFFF and what it accepts | | U3 | **which 4-clock slot is the CPU's during video, refresh, the slot phase at reset** | every RAM access timing | PAL equations (decision 6) or a probe (decision 8); detectors disagree (C1) | | U4 | where in the 352-clock line and 370-line frame blanking, H4 and the VBL edge fall | VBL interrupt timing, raster tricks | same as U3 | | U5 | the SE's ADB transceiver part and protocol timing | SE row | GMFH ch. 8; Snow's `adb/`; MAME is HLE | | U6 | what the 68000 RESET instruction resets on the board | rare | schematics (SCHEM) | | U7 | the meaning of the ROM's `MOVEM.L $F80000` / `$55AAAA55` probe | boot path | a ROM trace with a different answer | | U8 | the PWM pulse position and the audio filter | sound | good enough rule; detectors | | U9 | the 400K drive's speed response to the PWM (the transfer function, settling) | 128K/512K disk reads | DRV400's limits; a FITTED model | | U12 | the IWM's behaviour beyond its window table (long gaps) | weak bits, protections | MOOF notes; detectors | | U13 | the Plus RTC's extended PRAM commands | PRAM beyond 20 bytes | GMFH, IM4 do not give them; Snow, CLK | | U14 | SCSI blind transfers when the target is slow (Plus vs SE) | SCSI | NCR5380, GMFH | | U15 | TAS on the Mac bus | rare | none needed for the ROM | Resolved in this phase: the IWM mode value ($1F, OBSERVED in MAME and consistent with DRV400's 489.6 kbit/s at 8M fast mode) and the one-second timer (2^23 ±100 FCLK, read from the page image). ## Contradictions between sources | # | Contradiction | Resolution | | --- | --- | --- | | C1 | RAM contention: MAME none (OBSERVED), Clock Signal every other 8-half-cycle slot, Snow `cycles % 8 >= 4`; Apple says "one out of every two RAM access cycles" without clocks | none in hand: U3; Apple's words make MAME's "none" wrong for timing, not for order | | C2 | the sound fetch: "the first access after each scan line" (IM3 III-18) vs "the last RAM access before the beginning of a scan line" (GMFH ch. 5) | possibly the same slot described from either side of the blanking; decided with U4 | | C3 | IM3 calls the VIA's "a level-0 processor interrupt" and the SCC's "level-1"; GMFH Table 3-1 gives levels 1 and 2 (autovectors $19, $1A); IM3's own vector table agrees with GMFH | IM3's text names the /IPL0 and /IPL1 lines; use the table | | C4 | the VIA part: IM3 "Synertek SY6522"; MAME "MOS 6522" (128K, Plus) and "Rockwell R65NC22" (SE) | boards used several sources; a `mos6522-python` row choice, the same for all unless a difference shows | ## Domains and speed One domain on one 15.6672 MHz crystal (C8M for the CPU and IWM, E = C8M/10 for the VIA, 3.672 MHz for the SCC); the keyboard MCU is a separate domain only if modelled at chip level. Channels: the video fetch (a picture producer from a write log), the sound fetch (an audio producer), the interleave (a pure function of the clock), the VIA (catch-up to the next timer underflow), the IWM and drive (catch-up between CPU accesses), the SCC, the keyboard and RTC lines, SCSI. Need: 7,833,600 CPU clocks a second; **0.5-1.0 M instructions a second** (MAME: 969,119 instructions in 120 boot frames = 486,000 a second at 16.1 clocks each); 658,000 video words, 22,255 sound samples, 783,360 E ticks, up to 7.83 M IWM steps a second while a drive is on. **No-JIT path**: PyPy reference near real time with the drive idle, slower with the IWM per FCLK (HYPOTHESIS); Rust `-fast` several times real time on desktop, real time on phones and in the browser by the m68000-rust-fast figures (191 M instructions a second native, 112 M under Node, bare); threads only for producers. **`recompile: none` everywhere; no cached interpreter, no `-turbo` expected.** Details: [domains-and-speed.md](domains-and-speed.md). ## Open decisions for Aubrey 1. **Rows and order.** Recommend the Plus first, the 128K/512K/512Ke as rows from M1, the SE as a later row of the same board, and the SE FDHD/Classic (SWIM, 1.44 MB) out of scope until a SWIM Phase 0. 2. **MAME's Macintosh files are BSD-3-Clause.** May an agent read them (as fact sources, with the notice), or do they stay closed boxes as in this phase? Recommend: closed box for behaviour (MAME models no RAM contention, so its timing is not a judge anyway); reading allowed for register-level facts with the BSD notice, file by file, as the PCE and NES boards did. 3. **Keyboard level.** Recommend protocol level first (IM3/IM4 give the whole protocol and timing); the 8021/8048 on mcs48-python later only if a program depends on the MCU's timing. 4. **The IWM as a core.** apple2-python's IIc/IIgs rows will need it. Recommend: build it inside mac-python behind a pin-shaped interface and extract it as `iwm-python` when apple2-python's IIc row starts (the chip rule's second consumer). 5. **The 400K drive's speed model.** Recommend a first-order response to the PWM duty averaged over the buffer, inside DRV400's limits, tagged FITTED, with the closed loop (the driver reading TACH) as the test. 6. **Apple's PAL/HAL equations** (bitsavers: the 1983 prototype's JED fuse maps, the Plus TSM HAL die photos and a community equation set marked "not verified"). They would be the closest thing to a hardware-tier source for the interleave (U3/U4). They are Apple's programmed logic: is reading them for facts acceptable under the firmware rule, never committing them? Recommend yes, facts only, by SHA-256 pin into `reference/`, with the community equation set treated as tier 3 until checked against the fuse maps. 7. **Disks for the smoke list.** The census has no MOOF images and no protected originals; MAME's `mac_flop_orig` names 151 entries (354 MOOF files). Recommend asking whether Aubrey has (or wants) a set of Applesauce MOOF images; otherwise the smoke list is the System 6 raw images plus a few MFS 400K disks from the MiSTer collection. 8. **A hardware probe.** Aubrey has no Mac (his hardware list). Recommend adding one cycle-counting probe program (RAM vs ROM loop timing during and outside video) to the probe batch, for anyone with a working Plus; without it, U3/U4 rest on the PALs (decision 6) and stay HYPOTHESIS. 9. **mos6522-python first.** The board's M2 needs the shared VIA core. Recommend starting mos6522-python's Phase 0/build before mac-python's M2, with the Mac's wiring list in [peripherals.md](peripherals.md) as one of its consumers' requirements. 10. **Requests to m68000-python.** Recommend raising R2 (VPA data cycles, shared with amiga-python) with the IFR-loop repro before M2, and R1 (callback-returned waits) only if the board's own stall bookkeeping proves fragile. 11. **New environment variables.** `MAC_DISK_DIRS` and `MAC_FIRMWARE_ZIPS` (census only). Recommend adding them to the family profile, or generalising to one `CURATED_ROMS` variable as the profile already lists. 12. **sources.md rows.** The fourteen proposed rows in [sources.md](sources.md) ("Proposed rows"), including the IWM specification's CC BY-NC-ND 2.5 note and retrocomputing.stackexchange.com set aside for the site's wishes, for Aubrey or a lineage pass to add.