# Domains and channels The charter ("Domains and channels (multiple cores)") asks every board's specification to name its **domains** (parts on their own clock), the **channels** between them, which accesses are **sync points**, the memory map's **kinds** and who writes each region, and the **order of record**: the serial interleaving this specification runs. Written 2026-11-25 from the code at the commit that adds this file (megadrive-python efd80fc, m68000-python 4454d54); `tests/test_domains.py` checks the boundaries below against the code. `p.N` is Sega's Mega-CD Hardware Manual (docs/manual-registers.md), DOC unless tagged otherwise; tags quoted from the code are the code's. The default for this row in emulator-lineage `docs/proposals/2026-11-jit.md` ("Boundaries, family by family") was "main 68000; sub 68000; CD drive; PCM"; as built, PCM is part of the sub side (below). ## Domains | Domain | Clock | Parts (code) | Evidence | | --- | --- | --- | --- | | **Main** (the Mega Drive) | MCLK 53.693175 MHz NTSC (`megacd.py` `MegaCD.attach`: `board.machine.mclk`) | megadrive-python's whole board: the main 68000, Z80, VDP, YM2612, PSG, I/O, cartridge slot | megadrive-python | | **Sub** | the sub CPU's 12.5 MHz from its own 50 MHz crystal (`megacd.py` `SUB_HZ`; p.1, p.iv) | the sub 68000 (`m68000_python.M68000CPU`, `MegaCD.sub`), PRG RAM, word RAM, backup RAM, the gate array (`gatearray.py` `GateArray`: registers, timer, stopwatch, interrupt controller), the graphics unit (`graphics.py`), the CDC (`cdc.py`, LC8951), the PCM (`pcm.py`, RF5C164) | DOC p.1, p.iv, p.5, p.12-16 | | **Drive** (CDD) | its own controller on the console's side of a 10-nibble exchange, 75 a second; **here stepped on the sub clock** (`cdd.py` `CDD.run`: `EXCHANGE_HZ` per `SUB_HZ`) | `cdd.py` `CDD` (seek, play, the TOC, subcode), its CD-DA output `CDDA` | DOC p.32-33; drive timing FITTED to KRIKzz's logs (cdd.py, validation.md) | The PCM is not its own domain: the sub CPU writes its registers and wave RAM, the CDC's DMA fills wave RAM, and it is clocked from the sub side's crystal in `MegaCD._units`; only its output leaves, as a producer stream (below). It is the natural pure producer of a threaded tier (synthesis from register state over time), as CD-DA is for the drive. The drive is a domain in hardware (a separate controller with its own clock, behind a serial exchange the gate array performs; p.32-33) but in this specification it advances on sub clocks inside `MegaCD._units`, so its time is the sub side's exactly; a threaded tier may run it apart, synchronised at its channel. ## Channels ### Main to sub Every one goes through `MegaCD`, megadrive-python's expansion-port device (socket 2), and is a **sync point**: `MegaCD.catch_up(mclk)` first runs the sub side to the access's master clock (`mclk * SUB_HZ // mclk_hz`) and stamps that sub clock into `GateArray.now`. | Channel | Main side (code) | Sub side | Sync | Evidence | | --- | --- | --- | --- | --- | | Gate array registers `$A12000-$A1203F` (mirrors to `$A120FF`) | `MegaCD.read_word`/`read_byte`/`write_word`/`write_byte` → `GateArray.main_read`/`main_write` | the same state through `GateArray.sub_read`/`sub_write` (`$FF8000-$FF81FF`) | every access | DOC p.21-30, p.56-60; mirrors OBSERVED (ares) | | ↳ SRES, SBRQ (`$A12000` low byte) | `GateArray._write_reset_bits` | `GateArray.on_sub_reset` → `MegaCD._sub_reset_line` (the sub CPU's RESET); SBRQ stops the sub CPU between instructions (`MegaCD.run_sub`) | at the write | DOC p.56; SRES forcing SBRQ OBSERVED (ares); SBRQ at instruction, not bus-cycle, granularity is a stated deviation (megacd.py) | | ↳ IFL2: the main CPU's level-2 interrupt to the sub CPU | `GateArray.main_write` offset 0 → `request_later(2, now + IFL2_LATENCY)` | `GateArray.ipl(sub_clock)` before each sub instruction | the write; arrives 14 sub clocks later | DOC p.56; latency FITTED (IRQ TEST 9-19; gatearray.py) | | ↳ memory mode, DMNA/RET, WP, BK (`$A12002`/`$FF8002`) | `main_write` offset 2 | `sub_write` offset 2; read by both through `memory_mode_bits` | every access | DOC p.23-25, p.57; the 2M-owner model OBSERVED (ares `wramSwitch`) | | ↳ communication flags and words (`$A1200E`, `$A12010-$A1202F`) | CFM, command words written; CFS, status words read | CFS, status words written; CFM, command words read | every access | DOC p.28-29, p.59-60 | | ↳ HINT vector `$A12006` | written | (read by the main CPU at `$72`, `MegaCD.read_word`) | at the read of `$72` | DOC p.58 | | ↳ CDC host data `$A12008`, EDT/DSR/DD `$A12004`, stopwatch `$A1200C` | read (a write takes a word too) | `CDC.host_read(main=True)` | every access | DOC p.26-28, p.58; the write's word TEST ROM (CDC DMA3) | | PRG RAM window `$020000-$03FFFF` | `MegaCD._prg_window_read`, `_main_write` (only while SBRQ: the bank BK1-0) | the sub CPU's `$000000-$07FFFF` | every access | DOC p.6-7, p.57 | | Word RAM `$200000-$23FFFF` | `MegaCD._word_ram_read`/`_word_ram_write` (2M while main-owned; 1M the RET bank and its cell image) | `MegaCD._sub_word_ram_read`/`_write` (`$080000-$0DFFFF`), and the hang on RAM the sub CPU does not own | every access | DOC p.12-13, p.16-18, p.24-25; the hang's release TEST ROM (WORD RAM) | | VDP DMA from word RAM (socket 3) | `MegaCD.dma_start` latches the bus; `MegaCD.dma_read_word` | word RAM, one word late | every word | DOC p.61; the first word HYPOTHESIS | | Boot ROM `$000000-$01FFFF` (mode 2) | `MegaCD.read_word`/`read_byte`, no catch-up | nobody writes it | **not** a sync point (ROM) | DOC p.5, p.12 | No interrupt goes from the sub side to the main CPU (p.56; docs/sockets.md §5). Reset: power-on resets the CD (`MegaCD.reset`); the button and the 68000's RESET reach the sub side only through the BIOS writing SRES (HYPOTHESIS, megacd.py). ### Sub to drive | Channel | Sub side | Drive side | Evidence | | --- | --- | --- | --- | | Host registers: `$FF8036` (HOCK, D/M), status `$FF8038-$FF8041`, command `$FF8042-$FF804B` | `GateArray.sub_read`/`sub_write` | `CDD.control`/`set_control`, `CDD.status_word`, `CDD.command_word`/`write_command` (nibble 9 sends: `CDD.send`) | DOC p.32-33 | | INT4, the exchange's status ready | `GateArray.request(4)` | `CDD._present` via `CDD.request` | DOC p.30, p.32 | | Subcode buffer `$FF8068`, `$FF8100-$FF817F`, INT6 | `GateArray.sub_read` → `CDD.subcode_address`/`subcode_word`; `request(6)` | `CDD._subcode` | DOC p.55 | | Fader `$FF8034` | `GateArray.sub_write` → `CDD.set_fader` | `CDDA.volume` | DOC p.31 | | Periphery reset (RES0 written 0) and the forced reset | `GateArray.sub_write` offset 0, `reset_sub_side` → `CDD.reset_host` | | DOC p.22; what "the periphery" covers HYPOTHESIS | | Sectors to the decoder | `CDC.decode` | `CDD.decode` (wired by `MegaCD.__init__`) | DOC (LC8951 manual); the paused-loop delivery FITTED | The exchange takes no time here (DTS/DRS read 0, `CDD.control`); a sub-side read of the status registers is therefore a sync point with the drive only at exchange boundaries, which the drive reaches every `SUB_HZ / EXCHANGE_HZ` sub clocks. ### Producers (out of the machine) PCM (`PCM.take_samples`, 32,552 Hz) and CD-DA (`CDDA.take_samples`, 44.1 kHz) are read by megadrive-python's mixer through `MegaCD.audio_sources` (socket 8); nothing in either domain reads them back. ## Sync points, summarised 1. Every main-CPU (or Z80 bank, or VDP DMA) access to CD space except the boot ROM (the channels table): the sub side runs to the access's clock first. 2. After every main 68000 instruction (megadrive-python `System.step` socket 5, `device.catch_up`): not a hardware point, it keeps the sub side within one instruction of the main CPU. 3. Inside the sub side, each sub instruction boundary: IPL is sampled (`GateArray.ipl`), SBRQ, SRES and the hang are checked (`MegaCD.run_sub`). 4. The drive's exchange boundaries (`CDD.block`): status, INT4, the next sector. Stated limitation (megacd.py "Time"; docs/sockets.md §4): the sub CPU runs whole instructions, so a main-CPU read can find it up to one instruction (at most a DIVS, 158 clocks) past the access's clock. ## Order of record 1. The main side leads (megadrive-python `System.step`): one 68000 instruction, the Z80 caught up behind it, then every clocked device to the 68000's clock (`MegaCD.catch_up`). 2. A main access to CD space inside that instruction first catches the sub side up to its own clock (`MegaCD.read_word` and the rest). 3. `MegaCD.run_sub`: while behind, the sub CPU steps one instruction at a time, its IPL set from the gate array first; after each, `_units(clocks)` runs, in this order, the gate array's timer and stopwatch, the graphics unit (while busy), the CDC (its decoder pulse, inserted syncs and DMA), the drive (its exchanges, which deliver sectors to the CDC and request INT4/INT6) and the PCM. Held in reset, under SBRQ, or hung, the CPU makes no instruction and the units run on to the target. 4. Time is exact integers: the sub clock is `mclk * SUB_HZ // mclk_hz` (`MegaCD.catch_up`), so the interleaving is deterministic (save states continue identically: docs/save-states.md). ## Memory map: kinds and writers Main CPU, mode 2 (`MegaCD._main_offset`, `claims`; p.5, p.12; mode 1 moves the CD to `$400000`): | Region | Kind | Written by | | --- | --- | --- | | `$000000-$01FFFF` (mirrors to `$1FFFFF`) | boot ROM; `$70-$73` the HINT vector (`$FFFF` and `$A12006`) | nobody | | `$020000-$03FFFF` (mirrors) | PRG RAM window: shared memory, one 128 KB bank | main CPU while SBRQ; else sub CPU and the CDC's DMA | | `$200000-$23FFFF` (mirrors to `$3FFFFF`) | word RAM: shared memory with a hand-over | main CPU (2M main-owned, or 1M its bank) | | `$400000-$7FFFFF` | cartridge slot (RAM cartridge, mode 2) | main CPU | | `$A12000-$A120FF` | I/O: the gate array, **the channel** | main CPU | Sub CPU (`MegaCD.sub_read_word`/`sub_write_*`; p.12-15): | Region | Kind | Written by | | --- | --- | --- | | `$000000-$07FFFF` | PRG RAM (all of the sub CPU's code) | sub CPU above WP x `$200`; the CDC's DMA (DD=5, ignoring WP); the main CPU through its window while SBRQ | | `$080000-$0DFFFF` | word RAM: 2M whole, 1M the dot image and the sub's bank | sub CPU (2M sub-owned; 1M its bank, with PM priority writes); the CDC's DMA (DD=7); the graphics unit (`graphics.py`, the image buffer) | | `$FE0000-$FEFFFF` | backup RAM, odd bytes | sub CPU | | `$FF0000-$FF7FFF` | PCM registers and wave RAM window, odd bytes | sub CPU; the CDC's DMA (DD=4) | | `$FF8000-$FF81FF` (mirrors) | I/O: the gate array | sub CPU | Every RAM above is code space for one CPU or the other (a CD game's code is all RAM), so a cached interpreter must watch writes from both CPUs, the CDC's DMA and the graphics unit. ## Direct pokes (the charter's specification bugs) Found on 2026-11-25: 1. **The CDC's DMA reached into the device** (`cdc.py` `CDC.run`): the gate array's `mode`, `given_to_sub`, `sbrq` and `ret`, and `prg`, `wram` and `pcm` directly. Inside the sub domain, but a hidden dependency on the hand-over state that the main CPU changes. **Changed:** the DMA channel is five named calls on `MegaCD` (`cdc_dma_word_ram_held`, `cdc_dma_prg_held`, `cdc_dma_pcm`, `cdc_dma_prg`, `cdc_dma_word_ram`); no behaviour change. 2. **The gate array wrote and read the drive's command and status nibbles as lists** (`GateArray.sub_read`/`sub_write`: `cdd.command[i] = ...`, `cdd.status[i]`). This is the channel to the drive's domain. **Changed:** `CDD.status_word`, `command_word` and `write_command` (which sends at nibble 9); no behaviour change. Not changed, and why: 3. `GateArray.main_read`/`sub_read`/`sub_write` read and set the CDC's `edt`, `dsr`, `destination`, `address`, `dma_address` and call its register methods. The gate array decodes the LC8951 on the sub bus; one domain, and the registers are the CDC's own interface. A named "CDC host port" would be cosmetic. 4. `MegaCD.catch_up` sets `GateArray.now` (the sub clock of the main access being served, which IFL2's latency counts from). It is the channel's timestamp; a cleaner form is a `main_write(offset, data, upper, lower, at)` argument. Proposal: do it with the next change that touches the gate array, gated the same way. 5. The main side's paths call megadrive-python's `board.open_bus()` and `board.game_cartridge` (the RAM cartridge's mode-2 pass-through): main-domain state read during a main-domain access, so not a crossing. 6. Save states (`state.py`) and the command line's lid (`__main__.py` → `CDD.open_lid`/`close_lid`) reach everything: whole-machine host operations at frame boundaries, not channels. `tests/test_domains.py` checks that the sub side's modules never name the Mega Drive board, that the CDC reaches the device only through the five DMA calls, and that the gate array reaches the drive only through its host-register methods. ## Gate for this change Byte-identical before and after (docs/status.md has the commands): the unit tests on `.venv-pypy-pin4`; mcd-verificator as BIOS, `--ram-cart`, Sonic CD, 1,600 frames (17 of 18, every PNG and the WAV); the Sonic CD recipe to frame 7,500 (every PNG every 300 frames and the WAV). The referee compares the board's frames 300-3600 with Genesis Plus GX's fixed output, so the recipe's identical frames (the same boot, the first press at 4,830) carry its verdict.