# What the Sega CD needs from megadrive-python's sockets The design is megadrive-python's `docs/expansion-sockets.md` (branch `expansion-sockets`, `439b2ad`, 2026-10-21): eight narrow `typing.Protocol` sockets, nothing copied, the board byte-for-byte unchanged with nothing plugged in. This document is the Sega CD's side of that contract: what it needs from each socket, and **where the design is missing something or is wrong** for the CD. The board code was read at `d7882e1` (megadrive-python main, the family's own MIT code); line numbers are from there. Sources, under [restart.md](restart.md)'s rules: Sega's Mega-CD Hardware Manual, through [manual-registers.md](manual-registers.md) (cited `p.M`), MAME's BSD-3 Sega CD driver, Motorola's 68000 documentation, and reasoning about the sockets themselves. The Phase 0 version of this document also cited Genesis Plus GX and BlastEm source; those citations were struck in the 2026-11-19 scrub, and where nothing permitted replaced them the point is now an open item, to be settled by a document, a hardware log, or a closed-box run (`OBSERVED`) of the emulators through `scripts/libretro_run.py`. Findings are graded: **wrong** (the design states something the evidence contradicts), **missing** (the CD cannot work without it), **refine** (works, but the shape should change), **fine** (the socket as designed covers it). ## Summary | # | Socket | Grade | One line | | --- | --- | --- | --- | | 1 | Cartridge slot | **missing** | The slot's range is not fixed: in mode 2 the cartridge area is at `$400000-$7FFFFF` (manual p.5), so a cartridge without `/CART` (the RAM cartridge) sits there; "no cartridge" must differ from `Cartridge(b"")` | | 2 | Expansion port | **missing** | Port accesses must carry their MCLK; `/DISK` (version register bit 5) must follow the port; the TMSS overlay must sit above the port | | 3 | Bus timing | fine | Refresh already stretches every access below `$800000` (`_bus_cycle`); CD wait states, if any, go through `board.stall` | | 4 | Clock domains | **missing** | Catch-up after each instruction is too late; the CD must be caught up *inside* every port access, VDP DMA read and Z80 bank access, at that access's clock | | 5 | Interrupt sources | **wrong** | The CD does not interrupt the main 68000; level 2 is main **to sub** (manual p.56; MAME's BSD-3 driver). Nothing to add for the CD | | 6 | Picture | fine (unused) | The CD's picture is VDP DMA from word RAM: socket 9 below, not this one | | 7 | Sound | refine | Two asynchronous sources (32,552.08 Hz PCM, 44.1 kHz CD-DA) with their own filters; samples must be placed in MCLK, not counted per frame | | 8 | Reset | refine | Power-on, the reset button and the 68000's RESET instruction are three different events; the manual distinguishes power-on from other resets (p.27) but not the button by name, so what the button does to the CD is open | | 9 | VDP DMA source (not a numbered socket) | **missing** | `dma_read_word` bypasses both sockets for `$000000-$7FFFFF`, has no clock, and the word-RAM source is "output with a one cycle delay" (manual p.61), which needs a DMA-start notification | | 10 | Z80 bank window (not a numbered socket) | **missing** | Three Z80 paths read `board.cartridge` directly; the manual forbids the Z80 word RAM, PRG RAM and the 68000 side (p.62), but what such an access does is not documented | | 11 | Sub CPU (m68000-python, not a socket) | **missing in the core** | A sub-CPU access to word RAM it does not own is not acknowledged ("the CPU becomes hang up", manual p.24); m68000-python cannot suspend an instruction mid-bus-cycle | | 12 | Save states (not a socket, 2026-11-23) | **missing** | megadrive-python's state walk refuses a device's objects and knows no device CPU; segacd-python borrows its private encoder meanwhile (§12) | ## 1. Cartridge slot **Needed.** The RAM cartridge (this repository) and ordinary ROM cartridges (mode 1). **Missing: the slot's address range moves.** The design says "With a cartridge in and a CD attached, the slot keeps `$000000-$3FFFFF`." That is true only for a cartridge that grounds `/CART`. The manual (p.5) gives two system modes: in mode 1 (started from the cartridge) "the cartridge area is assigned to the MAIN-CPU starting from $000000, and the BOOT-ROM starts from $400000"; in mode 2 (started from a CD) "the cartridge area is assigned to the MAIN-CPU area from $400000 to $7FFFFF, and the BOOT-ROM starts from $000000", and mode 2 may share the slot with a "Back-up cartridge, Expansion RAM cartridge, Other functions". The RAM cartridge depends on it: it does not ground `/CART`, so the console boots the CD and the RAM cartridge's registers are in `$400000-$7FFFFF`. (The RAM cartridge's register layout is outside the hardware manual; to be sourced.) Proposed shape: the board asks the port, not the slot, who answers `$000000-$7FFFFF` when a port device is attached -- `port.decode(top) -> "port" | "slot" | None` plus the slot offset -- so that the CD's swap lives in this repository and the board learns no CD rule. With no port device the board decodes as it does today (the gate). **Missing: "no cartridge".** `Board.__init__` turns `cartridge=None` into `Cartridge(b"")` (`board.py:283`), which reads as an empty ROM. `/CART` sense needs a real "slot empty" state, and a cartridge needs to say whether it grounds `/CART` (a ROM cartridge does; the RAM cartridge does not). ## 2. Expansion port **Needed.** `$000000-$3FFFFF` or `$400000-$7FFFFF` by mode (BIOS, PRG RAM window, word RAM); the main-side gate-array registers at `$A12000` on. Whether the register block is mirrored up to `$A120FF` the manual does not say (it states no mirrors; manual-registers.md section 4); an open item for a closed-box run or a test program. **Missing: the clock of each access.** The design gives the port the `Cartridge` method set (`read_word(address)` etc.), which carries no time. A cartridge needs none; the CD does. The main and sub CPUs share registers both sides write (the communication flags, command and status words, `$A12000-$A12003`'s SBRQ/SRES/IFL2 and memory-mode bits), and the software's handshakes are polls of them, so a main-CPU read must see every sub-side write made before that read's clock and none made after. That needs the sub side caught up at the access. The VDP already gets `self.m68k_access_clock()`; the port must too: `read_word(address, mclk)`, `write_word(address, value, mclk)`, and the byte forms. **Missing: `/DISK` must follow the port.** `$A10001` bit 5 is "no expansion unit"; today it comes from `Machine.expansion`, a static field of the model (`machine.py:84`, `:112`). It should be `port is not None` (or the device's own answer). Mode 1 software reads it to find the CD. **Missing: the TMSS overlay must cover the port.** `_tmss_select` swaps `self.cartridge` between the TMSS ROM and the cartridge (`board.py:441-448`). In mode 2 the thing at `$000000` is the port, not the slot, so on a TMSS console the overlay must take precedence over the port as well (TMSS ROM selected > port in mode 2 > slot). Otherwise a TMSS Model 2 console with a CD never runs its TMSS check. **Refine: `cart_present` direction.** The design has the port *read* `/CART`. The board is what knows whether the slot holds a cartridge and whether it grounds `/CART`; it should *tell* the port (at attach and on any change), and the port decides its own mapping. Hot-swapping is not modelled. **Fine: the HINT vector.** `$A12006` supplies the low word of the HINT vector, the high word coming from the ROM's `$70` (manual p.58; manual-registers.md section 5), so every main-CPU read of `$000070-$000073` must reach the port -- exception vector fetches included. They do today (`read_word` has no fast path); keep it that way. **Refine: open bus.** Word RAM the main CPU does not own, and the PRG RAM window without SBRQ or SRES (manual p.6, 16, 57: either gives the main CPU the PRG RAM), are not the main CPU's to read. What such a read returns the manual does not say; if it is open bus the device needs the board's open-bus value (`board.open_bus()`), so the Protocol should pass the board (or that callable) at attach. Open item. ## 3. Bus timing **Fine.** `_bus_cycle` stretches any access below `$800000` for the cartridge-refresh pulse (`board.py:541-546`), which covers both CD ranges by address. Whether the CD's own RAMs add wait states on the main CPU's side is unknown (no source found; open item); if they do, `board.stall` carries them. ## 4. Clock domains **Missing: catch-up must happen inside accesses, not only after instructions.** The design calls `catch_up(mclk)` after `catch_up_z80` at the end of each 68000 step. The CD needs it at the start of every port access (§2), every VDP DMA read from CD space (§9) and every Z80 bank access to CD space (§10), each at its own clock. With that, the 68000-led scheduler works: the main CPU leads, and the sub CPU and its units are run up to the main CPU's current access before it reads or writes anything they share. The design's position that "a device that must interrupt the 68000 mid-instruction is out of scope" holds for the CD (§5), but "the sub-CPU handshakes are register polls" is only safe if the poll's read happens after the catch-up. **The quantum.** The sub CPU runs whole instructions (m68000-python's `step()`), so it can overshoot the main CPU's access clock by up to its longest instruction: DIVS, 158 clocks worst case (register operand; Motorola's M68000 instruction timing tables) = 12.6 µs at 12.5 MHz = about 680 MCLK. A sub-CPU write can then be seen up to that early by the main CPU. Record it as a stated limitation, measured against mcd-verificator's handshake rows in milestone 2. **Refine: the ratio.** The CD keeps its own rational ratio and remainder, as the design says: 12,500,000 sub clocks per 53,693,175 MCLK (NTSC), per 53,203,424 (PAL); sub CPU 12.5 MHz and "XTAL 50MHz" per the manual (p.1, p.iv). The drive's clock is not described in the manual (manual-registers.md section 5); 16,934,400 Hz (384 x 44,100, the usual CD clock) is a HYPOTHESIS until the maintenance manual's schematics or a capture says so. No fixed phase is correct (free-running crystals); zero at power-on, stated. **The reverse dependency.** The sub CPU waits on the main CPU in one place: a word-RAM access it is not entitled to (§11). Whether any main-side access withholds `/DTACK` is not known (none is documented); the board assumes not, as a documented choice. **Speed.** `catch_up` is called on every port access; when nothing is due it must cost a comparison (the design's "hooks are `None`-checked" rule). ## 5. Interrupt sources **Wrong.** The design's table says the Sega CD sends "interrupt level 2" into the 68000, and socket 5 is `set_ipl(max(vdp.ipl, *device.ipl))`. No permitted source has the CD interrupting the main 68000: - The manual (p.56): `$A12000` bit 8, IFL2, written 1 by the main CPU: "INT Level 2 is generated (When IEN2=1)"; level 2 is one of the sub CPU's six interrupt sources (manual-registers.md sections 2-3). - MAME `megacd.cpp` (BSD-3-Clause) drives input lines only on `m_scdcpu` (`segacd_68k`): levels 1, 2, 3 and the CDC/CDD levels, `RESET` and `HALT`; never `maincpu`. So the CD needs nothing from socket 5. Separately, if any expansion device ever does need to interrupt the main 68000, `max()` is the wrong model: on the Mega Drive the VDP encodes the 68000's IPL from VINT, HINT and EXINT (`vdp.py:1153-1162`), and the only external input is the VDP's `/HL` (EXINT, level 2, masked by register 11 bit 3). A device would drive EXINT through the VDP, and its acknowledge would clear it there. Whether `/HL` is on the expansion port is not in the hardware manual (it has no pinout; manual-registers.md section 5); to be read from the maintenance manual's schematics. **To settle with Aubrey: drop socket 5 from the CD's needs, and decide whether it should exist at all before the 32X shows a use** (the 32X's interrupts go to its SH-2s, per the design's own table). ## 6. Picture **Fine, unused.** The CD has no video output of its own (the LaserActive does; out of scope). Its picture reaches the screen through the VDP: §9. ## 7. Sound **Refine.** The CD has two sources, both asynchronous to MCLK: PCM at 50 MHz / 4 / 384 = 32,552.08 Hz and CD-DA at 44,100 Hz (from the drive's clock). A `(samples, rate, gain)` list works if each source's samples are placed in **MCLK time**, not counted per frame: a frame holds a non-integer, drifting number of CD samples. The device should return samples for an MCLK interval at its own rate, with its own filtering already applied (the CD-DA digital filter, the LC7883 per the manual's block diagram, p.iv; other models' parts to be read from their service manuals; and the PCM's output low-pass), and the mixer resamples. **Gains**: the Model 1 mixes the Mega Drive's headphone-jack output with its own in the CD unit; the Model 2 receives the Mega Drive's audio through the expansion port. No recording of either mix was found (validation.md, "What is not available"), so the gains are labelled assumptions, as the design already allows. ## 8. Reset **Refine.** "The reset button and the 68000's RESET output reach the devices" treats them alike. The CD needs three distinct events: power-on, the reset button, and the main 68000's RESET instruction. The manual separates power-on from other resets -- "For all resets except for Power-On, the DD2-DD0 hold previous values" (p.27) -- and gives power-on values for every register, but it never names the reset button, and it has no pinout to say whether the 68000's RESET output reaches the port (manual-registers.md section 5, "Reset"). The sub CPU is reset by the main CPU through `$A12001` SRES (a level: 0 holds it in reset, 1 runs; p.56), and the gate array's sub side by the forced-reset pattern (p.56-57). What the reset button does to the CD hardware is open: a closed-box run of the emulators (`OBSERVED`) or a hardware source will settle it. A `reset(kind)` socket, or three methods, fits either way. ## 9. VDP DMA source Not one of the eight sockets, but `Board.dma_read_word` (`board.py:829`) is listed in the design among the call sites a socket replaces, and the CD needs more than routing. **Missing: routing and time.** Today `top < 0x40` goes straight to `self.cartridge` and `$400000-$7FFFFF` falls through to `self.read_word` (open bus). With a CD, DMA from word RAM at `$200000` (mode 2) or `$600000` (mode 1) is how CD games draw their graphics. The read must go through the same `$000000-$7FFFFF` decode as §1 and carry the MCLK of the VDP's read slot, so the device can catch up (word RAM can change hands mid-frame). In 2M mode "the WORD-RAM must be switched to the MAIN side at the time of DMA" (p.61). **Missing: the word-RAM DMA latency.** The manual (p.61): "In the case where the VDP accesses the WORD-RAM by DMA, the data read from the WORD-RAM will be output with a one cycle delay"; its remedies are to "Set the source address to one word greater than the original address" and to rewrite the top two words afterwards (p.63 is a sample routine). What the first destination word receives (the bus's previous value, or nothing) is not stated: an open item for a test program or a closed-box comparison in milestone 3. Needed from the board either way: (a) a `dma_start()` notification to the slot and port devices, (b) the board's open-bus/prefetch value at that moment, and (c) the source device returning the latched word. ## 10. The Z80's bank window Not a numbered socket; the design lists `_z80_bank_read/_z80_bank_write`, `Board.z80_peek` and the pre-arbitration read in `System` among the call sites. **Missing.** All three read `board.cartridge` directly for bank addresses below `$400000` (`board.py:1038`, `:1058`; `system.py:479-480`), and `$400000-$7FFFFF` reads `$FF`. The manual's precaution 4 (p.62) says the Z80 must not access WORD-RAM, PRG-RAM or "all the area of 68000 CPU side"; what happens if it does is not documented. Needed: the same decode as §1 for the Z80's bank, at the Z80's access clock (`_z80_access_time()`), for all three paths -- the probe read included, or the arbiter's prediction will disagree with the access it predicts -- with what the CD answers left a documented choice until a source says. ## 11. The sub CPU: what m68000-python lacks Not a socket, but the board cannot be built without it. The core has what a second instance needs for most of the job: `set_ipl`, an `acknowledge(level)` callback, `reset()`, `halted`, `step_clocks` for in-instruction timing, `reset_devices` for the RESET instruction, and its own `clock` for the E-clock phase of autovectored acknowledges. (Whether the level-2 acknowledge clears IFL2 the manual does not say; p.56 gives only IFL2's read meanings.) **Missing: a bus cycle that waits for `/DTACK`.** A sub-CPU access to word RAM it does not own is not acknowledged: the manual says "the CPU becomes hang up", cleared only by a forced reset (p.24). Whether a later hand-over releases it is not stated; open. m68000-python can only finish an instruction or raise a bus error. Options, to propose as a core PR (not to be patched here): 1. a `BusWait` exception a host callback raises; the core restores the registers it had at the start of the instruction and reports "waiting", the host retries `step()` once the condition clears (exact, since the core can snapshot cheaply at the instruction boundary when the host opts in); 2. a callback that returns "not yet" and a resumable instruction -- not feasible in the core's direct-call style. Option 1 is the small one. Until it lands, the board can treat a locked access as a long stall (finish the instruction with the old data and add stall clocks up to the hand-over, or hang until a forced reset, as the manual describes), labelled as a deviation. **Also:** the sub CPU's bus request (SBRQ) halts it at a bus-cycle boundary; the core can stop only between instructions. Same quantum as §4; stated. ## 12. Save states (added 2026-11-23): no socket for a device's state megadrive-python's save states (its `docs/save-states.md`, state version 2, efd80fc) walk the objects of the packages in `state.MODULES` and save each CPU it knows by its core's `capture_state`; anything else the walk meets is refused (`StateError: cannot save a ...`). A device on the expansion port or in the slot is reached through `board.port`, `System.clocked` and `System.audio_sources`, so a console with the CD cannot be saved by megadrive-python alone, and nothing in the sockets lets a device say "walk my objects too", "this is my CPU" or "this medium is configuration". What segacd-python does meanwhile (`src/segacd_python/state.py`): it uses that module's encoder and decoder classes, adds `segacd_python` to `MODULES` for the length of a save or a load, saves the sub CPU as `sub68k` beside `m68k` and `z80`, writes the disc as its identity (a medium, as the ROM is), and keeps megadrive-python's header (with its state version) inside its own. Those names are private at the pin; `tests/test_state.py` fails if they move. **Proposed for megadrive-python** (not done here: its own agent's repository): a socket for state, for example a device attribute `state_modules` (packages whose objects are state), `state_cpus` (the device's CPU objects, saved by their `capture_state` under names it gives) and `state_media` (objects written by identity, refused on mismatch), honoured by `state.save_state` and `state.load_state`; then this board's state would be megadrive-python's own state with the CD in it, and the 32X's the same way. ## What the CD does not need - The picture socket (§6). - Socket 5 as designed (§5). - The 32X's `claims(top)` widening of the slot -- except that, for CD + 32X (the late milestone), the 32X in the slot and the CD in the port both answer parts of `$000000-$3FFFFF` in mode 2. Which wins, and how a slot device that itself holds a cartridge interacts with the CD's `/CART` swap, is the combination's question, to be recorded for that milestone (the Phase 0 `undocumented-behavior.md` that held it is to be rewritten from permitted sources during the rebuild).