# Handoff: build `segacd-python`, the Sega CD / Mega-CD board This brief was written in Phase 0 (2026-10-21) and scrubbed on 2026-11-19 for the restart: the rules every source, document and line of code follows are in [restart.md](restart.md), and they take precedence over anything below. ## Context you are inheriting This is the family's first **add-on board**: a board that does not stand alone but plugs into another board's socket. It sits on: - **megadrive-python** (`/data/emu/megadrive-python`, private GitHub alewman/megadrive-python): the Mega Drive, milestones 1-8 done, exact against BlastEm and Nuked-MD far into real titles. It is pinned by git revision and **never copied from**. Its `docs/expansion-sockets.md` (branch `expansion-sockets`, `439b2ad`) is the agreed design for the sockets this board plugs into; [sockets.md](sockets.md) is this board's side of it, with the findings that must reach that design before its PR merges. - **m68000-python** (public, PyPI 0.1.0): the sub CPU is a second instance of the same core the Mega Drive uses, pinned to the same revision (`38dbd0f`) so the resolver sees one. Read, in order, before writing code: 1. [restart.md](restart.md): the rules (what may be read, what may only be run), what was carried over. 2. `README.md`: scope, what is fetched, the bring-your-own BIOS rule. 3. [manual-registers.md](manual-registers.md): the machine's register reference, read from Sega's hardware manual. The Phase 0 machine overview (`start-here.md`: memory maps, word RAM, the graphics unit, CDC/CDD, sound, BIOS, discs, models), timing document (`timing.md`: the three clock domains, the scheduler) and quirks list (`undocumented-behavior.md`) were not carried over; they are to be rewritten from permitted sources during the rebuild, and manual-registers.md is the reference meanwhile. 4. [sockets.md](sockets.md): what the CD needs from each socket, and what is wrong or missing in the design. 5. [validation.md](validation.md): every oracle with its tier and pin, what ran here, the certification record to fill. 6. [mame-oracle.md](mame-oracle.md): MAME's recipe, its traces and its limits. 7. megadrive-python's `docs/expansion-sockets.md`, `src/megadrive_python/board.py` and `system.py` (the call sites the sockets replace); m68000-python's `README.md` and `docs/claims.md`. Then `python scripts/fetch_references.py --verify` (or without `--verify` on a fresh clone; about 140 MB) and `python scripts/build_referees.py` (Genesis Plus GX and BlastEm libretro cores, about 110 MB of build trees). Building and running them is allowed; opening their source files is not. ## Prerequisites and decisions already made - **Separate repository, pinned dependency, nothing copied** (Aubrey, 2026-10-21; the sockets design). The Mega Drive must be byte-for-byte unchanged with nothing in its port; that gate is megadrive-python's, not ours. - **Python floor 3.11; PyPy is the platform.** Two 68000s (7.67 MHz + 12.5 MHz) plus a Z80, a VDP and an FM chip will not run in real time on CPython, and the reference board is not required to (Aubrey: "correct, not fast"). Speed is a later `-fast` fork's job. - **No dependencies beyond megadrive-python and m68000-python** for the board. A frontend, if any, rides on megadrive-python's. - **Bring your own BIOS.** Never fetch, commit or vendor Sega's BIOS or any commercial disc. Tests that need a BIOS skip without one. mcd-verificator *replaces* the BIOS (its readme's option 1), so the first milestones need no Sega code at all. - **HLE the CDD's protocol**, not its microcontroller (no program dump is known). - **RF5C164 stays in this board** until a second consumer (the RF5C68 of System 18 or the FM Towns) arrives; then it is extracted to a core with this board's history (the never-copy-extract-on-second-consumer rule). - **Do not modify megadrive-python or m68000-python from here.** Propose changes there as PRs. Two are foreseeable and listed below; until they land, a labelled workaround in this board is acceptable. - **Tiers** (validation.md): hardware-captured > die-derived > specification > research > emulator-derived. There is **no die-derived model** of any Sega CD chip, so where nothing hardware covers a question the judge is the specification or, failing that, a documented choice; emulators are detectors only. ## The changes this board needs elsewhere (propose, do not patch) **megadrive-python, the sockets PR** -- before it merges, so the design is right for the CD (details and evidence in [sockets.md](sockets.md)): 1. Port (and slot) accesses carry their MCLK; the device catches up inside the access. 2. The `$000000-$7FFFFF` decode is asked of the port when one is attached (the `/CART` swap; a RAM cartridge in the slot at `$400000` in mode 2); "no cartridge" is a state, and a cartridge says whether it grounds `/CART`. 3. `$A10001` bit 5 follows the port; the TMSS overlay sits above the port. 4. VDP DMA reads go through the same decode, with their clock, a `dma_start()` notification and the open-bus value (the word-RAM latency). 5. The three Z80 bank paths (read, write, the arbiter's probe) go through the same decode. 6. Reset distinguishes power-on, the reset button and the RESET instruction. 7. Socket 5 (interrupt sources) is not needed by the CD; the design's table is wrong that the CD interrupts the main 68000. **m68000-python** -- a bus cycle that waits for `/DTACK`: a `BusWait` the host raises from a callback, after which the core restores the instruction's starting state and `step()` reports a wait; the host retries once the condition clears ([sockets.md](sockets.md) §11). Until then: finish the instruction with stale data and stall to the hand-over, labelled. ## Your task Build `src/segacd_python`: a `MegaCD` device for megadrive-python's expansion port, a RAM-cartridge device for its slot, the sub CPU, the gate array, the memories, the graphics unit, the CDC, the CDD protocol, a disc-image reader, PCM, CD-DA, and a small runner (`python -m segacd_python DISC --bios BIOS`). ### Milestones, each with its acceptance test, in oracle-tier order 0. **Prerequisites.** The sockets PR merged in megadrive-python with the findings above, behind its own gate; this repository re-pinned to that merge. The `BusWait` PR proposed in m68000-python. *Accept:* `pip install -e .` resolves both by revision, on CPython and PyPy; `import segacd_python` works; the Mega Drive's own gate passes with the port empty. 1. **Gate array, memories and the sub CPU, under mcd-verificator as BIOS.** Main- and sub-side registers, PRG RAM (banking, write protect, SBRQ/SRES), word RAM in 2M and 1M with the hand-over and the dot image, backup RAM, the timer, the stopwatch, interrupts 1-3, the HINT vector. *Judge:* mcd-verificator (hardware-derived expectations), rows VAR TESTS, IRQ TEST, REG X000, REG X002, REG 2006, REG X00C, REG 8030, PROG RAM, WORD RAM, WRAM PMOD, COLOR CALC. *Detectors* (closed-box runs, validation.md): Genesis Plus GX (passes all), BlastEm (fails REG 2006), MAME (fails most). Read every register against the hardware manual ([manual-registers.md](manual-registers.md)) first and record differences in the quirks document as it is rewritten. 2. **Time.** A scheduler (the Phase 0 timing document is to be rewritten from permitted sources): catch-up inside accesses ([sockets.md](sockets.md) §4), the sub CPU's quantum, PRG-RAM and word-RAM refresh read off the MiSTer captures. *Judge:* the MiSTer SignalTap captures (refresh, interrupt acknowledge, register writes) to the clock they resolve; Ti_'s speed test (12.16-12.22 M sub-CPU iterations, if the ROM is obtained). *Detector:* a lockstep of the main and sub 68000 instruction streams against MAME's traces through the BIOS boot (scripts/mame_cd.py --trace), every difference traced to MAME or explained. 3. **Picture: VDP DMA from word RAM and the graphics unit.** The word-RAM DMA latency (the manual's "one cycle delay", p.61; what the first destination word receives is decided with a test, or recorded open), the 1M cell-image view, the stamp unit following KRIKzz's access order (his notes). *Judge:* the MiSTer `graphics_operations_and_68k_wordram_access` capture for timing; the hardware manual for the arithmetic. *Detectors:* Genesis Plus GX and BlastEm frames (scripts/libretro_run.py) of sega-cd-asic-demo and jvisser's mode-1 test. 4. **The disc: CDC, CDD protocol, BIN/CUE.** LC8951 registers, decoder, buffer, the five transfer destinations and DMA timing; the CDD exchange, TOC, seek, play, pause, tray; the BIN/CUE reader with pregaps and subcode. *Judge:* mcd-verificator's CDC rows (CDC REGS, CDC INIT, CDC FLAGS, CDC DMA1-3); KRIKzz's CDD logs (hardware-captured) replayed exchange by exchange, statuses identical; the MiSTer DMA captures for transfer timing. *Then:* boot real discs with your own BIOS to their title screens (detector: GPGX). 5. **Sound.** RF5C164 (in-board), CD-DA, the fader, the audio socket. *Judge:* the Sega PCM chapter (specification) and the MiSTer PCM captures for read slots. *Detectors:* GPGX/BlastEm audio of the 240p suite's Sega CD PCM tests. Output levels relative to the Mega Drive are labelled assumptions until a recording exists. 6. **Mode 1, backup RAM, the RAM cartridge.** Cartridge boot with the CD at `$400000`; the RAM cartridge in mode 2. *Judge:* mcd-verificator as a mode-1 cartridge (its option 2, needs your BIOS) and its RAM CART row. *Detectors:* megacdTest, msu-md's sample, the Mode-1 CD hacks collection (`T-En Collection/Sega - Mega Drive - Mode 1 CD`, 3.3 GB), Pier Solar's mode-1 audio. 7. **Compatibility sweep and models.** Every Redump disc you have (548 in `/data/emu/roms/megacd`) booted with your own BIOS for N frames, frames compared with GPGX's, differences triaged as megadrive-python's sweep does; Model 1/2, CDX, Wondermega and X'Eye differences that a BIOS or a filter selects; CHD if decided. 8. **CD + 32X** (after `sega32x-python` exists): both sockets occupied; the 13 Sega CD 32X discs. The acceptance test of the socket design as a whole. ## Working agreement for the first build session - Start from milestone 1 with mcd-verificator as the BIOS: no disc logic is needed for its first eleven rows, and it needs no Sega code. - Every register's behaviour gets a citation and an evidence tag (manual page, KRIKzz note, MiSTer capture, or `OBSERVED` from a closed-box run with the emulator and pin named -- the last is a detector-level claim and says so). - Run the referees for every question before guessing: `libretro_run.py gpgx`, `libretro_run.py blastem`, `mame_cd.py`. Where they disagree, say which is right and why from a document or hardware source, or record it open; a difference from a closed box is written down, not fixed, unless such a source says the board is wrong. - Keep a `docs/worklog.md` as megadrive-python does. ## Constraints - Pure Python, no compiled extensions, no dependencies in the board beyond the two pins. - CPython and PyPy, Python 3.11+. - MIT, under [restart.md](restart.md)'s rules: Genesis Plus GX, PicoDrive, BlastEm, jgenesis, clownmdemu-core, the FPGA cores' HDL, mcd-verificator's source and MAME's GPL files are run as closed boxes, never read; permitted sources are listed there. - Never commit a BIOS, a disc, a CDD log or a capture: they live under `reference/` and `runs/`. - No `rm -rf`, `sed -i` or `ln -sfn` one-liners; use the editor. ## Size and speed, honestly The main 68000 needs about 1 M instructions a second, the sub 68000 about 1.6 M, the Z80 about 1 M; m68000-python does about 20 M a second on PyPy. megadrive-python alone is not real time on PyPy (FM is its bottleneck); with the CD it will be slower. That is expected for a reference board. ## What done looks like Milestones 1-7 pass with every claim in `docs/validation.md`'s certification record at the tier its judge has; every mcd-verificator row passes or has a recorded reason; the sockets PR merged with the CD's needs; the board boots and plays your own discs with your own BIOS on PyPy. ## Open decisions for Aubrey 1. **Names**: DECIDED by Aubrey 2026-10-21: `segacd-python` / `segacd_python`. 2. **The socket findings** ([sockets.md](sockets.md)): accept them into the sockets PR before it merges -- in particular dropping socket 5 for the CD, three kinds of reset, clocks on port accesses, the `/CART` swap decode, and DMA-start + open bus for the word-RAM DMA latency. 3. **The m68000-python `BusWait` PR**: propose it now (recertification of the core afterwards), or start with the labelled stall workaround? 4. **CHD**: BIN/CUE only, or a pure-Python CHD reader (zlib and LZMA are in the standard library; CHD's FLAC and Huffman codecs are not -- roughly 1,000 lines)? Your local CD library is BIN/CUE (548 discs) and CHD (178). 5. **mcd-verificator in CI**: its source is GPL-3.0 (its repository's `LICENSE`; run only, never read), and it was deleted from its repository's HEAD (pinned at the parent commit). Fetching it at CI time from that pin is what the fetch script does; is that acceptable, given CI minutes are to be kept minimal and the board repository is private? 6. **A free boot ROM** (clownmdemu-mcd-boot, 0BSD) for runs that need a real boot path without Sega code: worth building (needs a 68000 assembler) and fetching, or is mcd-verificator-as-BIOS plus your own BIOS enough? 7. **Which BIOS is "the" reference**: Model 1 US v1.10 (MAME `segacd`, in the MAME romset mirror) or Model 2 US v2.00W (the `bios_CD_U.bin` in your Batocera pack)? The tests should name one. 8. **The six Mega CD 2 service-manual PDFs** on archive.org (about 217 MB, not pinned): fetch them for the Model 2 board, or rely on the 43-page service manual and the Model 1 maintenance manual already pinned? 9. **Public or private**: boards stay private (your rule); confirm for this one, and that it stays local until milestone 1 passes.