# Domains and channels The charter ("Domains and channels (multiple cores)") asks every board's specification to name its **domains**, 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. families.md ("Where to go next", item 3) asks for it here before any port begins. Written 2026-11-25 from the code at the commit that adds this file (megadrive-python efd80fc, sh2-python 884aa30); `tests/test_domains.py` checks the boundaries below against the code. "Manual" is Sega's 32X Hardware Manual (docs/start-here.md), DOC; tags quoted from the code are the code's (each rule's full record is its `Quirks` entry, machine.py). The starting default was emulator-lineage `docs/proposals/2026-11-jit.md`, "Boundaries, family by family", 32X row. ## One clock, four domains The 32X has no crystal: its PLL multiplies the 68000's clock from the slot, so 7 MCLK = 1 68000 clock = 3 SH-2 states, exactly (docs/timing.md "The clock domain"; service manual `A406a`, DOC). Its domains are therefore **scheduling** domains on one locked time base: parts that keep their own time and meet the others only at the channels below, which is what lets a threaded tier run them apart. | Domain | Time | Parts (code) | | --- | --- | --- | | **Mega Drive side** | MCLK | megadrive-python's board: the 68000, Z80, VDP, sound, I/O; and the 32X's 68000-facing half, `slot.py` `Sega32X` (the claims, the system registers at `$A15100-$A1513F`, the 68000's frame-buffer and window accesses) | | **SH-2 pair** (one domain, as the charter says for tightly coupled pairs) | SH-2 states, `mclk * 3 // 7`; each chip its own `clock + skew` | `sh2side.py` `SH2Side`: the master and slave SH7604s (sh2-python), SDRAM, the boot ROMs, the shared bus and its arbitration, the frame buffer's write FIFO, the interrupt lines; the SCI link between the chips | | **32X VDP** | MCLK, from the Mega Drive VDP's line records (socket 6) | `vdp.py` `VDP32X`: registers, palette, FS, auto fill, the line's events (HBLANK, latch, VBLANK) and the rows it draws | | **PWM** | SH-2 states | `pwm.py` `PWM`: registers, FIFOs, the period counter, the timer interrupt, DREQ1, the sound | **As built, the VDP and the PWM are not run apart from the SH-2 pair.** Their events are run inside `SH2Side.catch_up`, before the step of whichever chip is next, as soon as that chip's place reaches the event's state (`vdp.next_state`, `pwm.next`), because the events raise the SH-2s' interrupts and DREQ1. So the order of record interleaves them at SH-2 instruction boundaries. They are named as domains because their channels are narrow (below): the VDP's row drawing is a producer of the picture from the frame buffer and palette, the PWM's sample making a producer of sound from the period levels (`PWM._events`). ## Channels ### Mega Drive side ↔ SH-2 pair (the 32X's system registers and shared memory) Every 68000 or Z80 access to 32X space first calls `Sega32X.catch_up(mclk)` (sockets 4 and 5): the Mega Drive's VDP publishes its line records to `mclk` (`Sega32X.video_lines_to`), then the SH-2s and the VDP's events run to it. Those accesses are the sync points on this side. The SH-2s' accesses happen at their own place on the time line (`Sega32X.sh2_mclk`), which is never past the 68000's: the pair runs only to the time the Mega Drive side has asked for. | Channel | 68000 side (`slot.py`) | SH-2 side | Evidence | | --- | --- | --- | --- | | Adapter control `$A15100` / `$20004000`: ADEN, RES, FM; CART; HEN; the SH-2s' interrupt masks | `_register_write` offset 0 (ADEN, RES → `SH2Side.set_run`; ADEN → `Board.update_claims`) | `sh2_register_write` offset 0 (FM, HEN, `SH2Side.set_mask`) | DOC Manual 3.2.1-3.2.2 | | CMD interrupt `$A15102` (INTM, INTS) | `_register_write` offset 2 → `_cmd_lines` → `SH2Side.assert_source`/`clear_source` | an SH-2's CMD clear (`$2000401A`) clears its bit | DOC Manual 3.2.2; `Quirks.cmd_interrupt` | | VRES (reset button) | `Sega32X.reset(BUTTON)` → `SH2Side.assert_source(VRES)` on both | level 14, no mask | DOC Manual 3.2.2; TEST ROM testpico `t_32x_reset_btn` | | Bank `$A15104`, RV `$A15106` bit 0 | written by the 68000 only | RV read by `SH2Side` (`Sega32X.rv`): an SH-2 cartridge access waits while RV = 1 (`_cartridge_wait`, `rv_blocked`) | DOC Manual 3.2.1; `Quirks.rv_stall` | | DREQ FIFO `$A15106`-`$A15112` / `$20004006`-`$20004012` | the 68000 fills it (`_register_write` offset `$12`) | `_fifo_pop` (SH-2 DMA channel 0 or a CPU read); **DREQ0** to both chips while it holds a word: `SH2Side.set_dreq0` | DOC Manual 3.2.1, figure 3.28; `Quirks.dreq_fifo` | | Communication ports `$A15120-$A1512F` / `$20004020-$2000402F` | read and written | read and written | DOC Manual 3.2.1 | | PWM registers `$A15130-$A1513F` / `$20004030-$2000403F` | `PWM.read`/`write(sh2=False)`, the PWM first run to the access | `PWM.read`/`write(sh2=True)` | DOC Manual 3.2.1, 3.4 | | Cartridge (ROM, `$880000-$9FFFFF` windows / `$02000000`) | `_window`, held by an SH-2 cartridge cycle under way (`SH2Side.m68k_cartridge`) | `SH2Side._cartridge_read`/write through `Sega32X.shared_cartridge`; held by a 68000 cycle (`cart_m68k`) | DOC Manual 3.1; `Quirks.cartridge_contention` | No interrupt goes from the 32X to the 68000 (Manual 3.2.2 lists the SH-2s' only; the 68000 polls). The Z80 reaches 32X space only through the board's bank window (`read_byte` with its master), the same sync point. ### Both CPUs ↔ the 32X VDP | Channel | Who | Evidence | | --- | --- | --- | | VDP registers `$A15180-$A1518F` / `$20004100-$2000410F` | the side FM gives them to (68000 with FM = 0, SH-2s with FM = 1): `VDP32X.read`/`write` at the access's MCLK | DOC Manual 3.1, 3.2.3 | | Palette `$A15200-$A153FF` / `$20004200-$200043FF` | the same, held while PEN = 0 (`VDP32X.palette_wait`; the SH-2 side's hold by `SH2Side.hold`) | DOC Manual 3.3; `Quirks.palette_wait` | | Frame buffer DRAM (`$840000-$87FFFF` / `$04000000-$0403FFFF`, the overwrite image above) | the CPUs reach the buffer FS does not show (`Sega32X._framebuffer_write`, `sh2_framebuffer_read`/`write`; the SH-2s' writes through the write FIFO, `SH2Side._framebuffer_write_wait`); the VDP's auto fill writes it; the VDP's rows read the shown one at each HBLANK fall (`VDP32X._draw`) | DOC Manual 2.1, 3.1, 3.3, 3.6; `Quirks.sh2_framebuffer_fifo`, `Quirks.framebuffer_zero_bytes` | | HINT, VINT to the SH-2s | `VDP32X._hblank_rise` → `SH2Side.raise_hint`; `_vblank_on`/`_vblank_off` → `raise_vint` | DOC Manual 3.2.2; `Quirks.hint_counter`, `Quirks.vint_latch` | | H count `$20004004`, HEN | written by the SH-2s, read by the VDP's HINT counter (`Sega32X.hcount`, `hen`) | DOC Manual 3.2.2 | | Status (VBLK, HBLK, PEN, FEN) | `VDP32X.status(mclk)`, read by both sides at their access's MCLK | DOC Manual 3.2.3, 3.3 | ### Mega Drive side → 32X VDP → the picture (sockets 6 and 7) The Mega Drive VDP's line records (`Sega32X.video_line` → `VDP32X.video_line`, socket 6) are the 32X VDP's time base; `Sega32X.video_lines_to` asks the Mega Drive VDP to publish them up to a clock (the one call into the Mega Drive board's own state: `board.vdp.sync`). The composite (`composite.py`, socket 7) lays each drawn 32X row over the Mega Drive's line: a producer, read by the host only. ### PWM → SH-2 pair, PWM → sound The timer interrupt (`PWM._timer` → `SH2Side.raise_pwm`, level 6) and, with RTP, **DREQ1** to both chips' DMAC channel 1 (`SH2Side.set_dreq1`; dropped after each chip's next step in `SH2Side.catch_up`, OBSERVED ares, `Quirks.pwm_dreq1`). The sound leaves through socket 8 (`PWM.take_samples`). ### Inside the SH-2 pair SDRAM, the shared bus (`SH2Side._arbitrate`, `Quirks.sh2_bus_arbitration`), the frame buffer's write FIFO, the crossed SCIs (`SH2Side._txd`: one chip's TxD is the other's RxD, a byte at a time, `Quirks.sci_link`), FTOA to /IRL0 (`Quirks.ftoa_to_irl0`). One domain: every access of either chip to SDRAM could be seen by the other, and the caches (each chip's own, sh2-python) make code written by one invisible to the other until purged, which is software's job (HM 8). ## Sync points, summarised 1. Every 68000 or Z80 access to 32X space (`Sega32X.read_word`/`read_byte`/`write_*`/ `dma_read_word`): the 32X side runs to the access's clock first. 2. After every 68000 instruction (megadrive-python `System.step`, socket 5): not a hardware point. 3. Inside `SH2Side.catch_up`, before each chip step: the VDP's events and the PWM's period ends due by the next chip's place. 4. Each SH-2 access to `$20004000-$200043FF`, the frame buffer and the cartridge is made at that chip's own place on the time line (`sh2_mclk`, `cycle_end`). A stated limit, recorded not fixed: the chips run whole instructions, so a chip may stand up to one instruction past the 68000's clock when the 68000 reads a communication port; an SH-2 write made in that overshoot is visible to the 68000 early (the SNES board logs its S-SMP's port stores against this; this board does not). HYPOTHESIS that any title depends on it; testpico's and the Mars Check Program's communication tests pass as it is. ## Order of record 1. megadrive-python leads: one 68000 instruction, the Z80 behind it, then the 32X to the 68000's clock (`Sega32X.catch_up`); a 68000 access to 32X space inside the instruction does the same at its own clock first. 2. `Sega32X.catch_up(mclk)`: with ADEN = 0 nothing runs. Else the Mega Drive VDP publishes its lines to `mclk` (`video_lines_to`); with the SH-2s running, `SH2Side.catch_up(mclk)`, then the VDP's events to `mclk` and its old line records pruned; with them held, the VDP's events and the PWM to `mclk`. 3. `SH2Side.catch_up`: lines published; RV-blocked chips moved on (their skew grows while RV stays 1); then, until both chips reach the target: take the chip whose place is earlier (the master on a tie); run the VDP's events due by that place, then the PWM's period ends; step that chip one instruction (DREQ1 dropped after it); at the end the PWM to the target. 4. Time is integers (`mclk * 3 // 7`, per-chip skew), so the interleaving is deterministic (save states continue identically: docs/validation.md "Save states"). ## Memory map: kinds and writers 68000 (`slot.py` `Sega32X.claims`; DOC Manual 3.1, 3.5): | Region | Kind | Written by | | --- | --- | --- | | `$000000-$0000FF` (ADEN = 1) | vector ROM; `$70-$73` the HINT vector register | 68000 (the register only) | | `$000100-$3FFFFF` (ADEN = 1, RV = 0) | undriven: open bus | nobody | | `$000000-$3FFFFF` (ADEN = 0, or RV = 1) | the cartridge, the Mega Drive's own map | (ROM) | | `$840000-$87FFFF` | frame buffer (FM = 0), the overwrite image at `$860000` | 68000 | | `$880000-$9FFFFF` | cartridge windows (RV = 0) | (ROM; cartridge SRAM/EEPROM per board) | | `$A130EC-$A130EF` | `"MARS"` | nobody | | `$A15100-$A153FF` | I/O: system registers, PWM, VDP, palette: **the channels** | 68000 | SH-2 (`sh2side.py` `SH2Side._reader`/`_writer` by `address >> 24`; DOC Manual 3.1; the `$2xxxxxxx` cache-through and `$4xxxxxxx` purge spaces are the core's, HM 8): | Region | Kind | Written by | | --- | --- | --- | | `$00000000-$00003FFF` | boot ROM (master 2 KB, slave 1 KB) | nobody | | `$00004000-$000043FF` | I/O: the system registers, PWM, VDP, palette: **the channels** | both SH-2s | | `$02000000-$023FFFFF` | cartridge (shared with the 68000) | (ROM) | | `$04000000-$0403FFFF` | frame buffer (FM = 1), the overwrite image at `$04020000` | both SH-2s (through the write FIFO), the VDP's auto fill | | `$06000000-$0603FFFF` | SDRAM: most of a game's code and data | both SH-2s and their DMACs (DREQ0 from the 68000's FIFO, DREQ1 from the PWM) | For a cached interpreter or JIT (the proposal's 32X row): code lives in SDRAM, written by both chips and both DMACs; each chip's cache sees only its own writes; the purge space and the cache-through mirror are the software's tools. ## Direct pokes (the charter's specification bugs) Found on 2026-11-25: 1. `slot.py` `_dreq_update` set `chip.dreq0` on each SH-2 directly. **Changed:** `SH2Side.set_dreq0`. 2. `pwm.py` `_timer` and `write` set `chip.dreq1` on each SH-2 directly. **Changed:** `SH2Side.set_dreq1`. 3. `slot.py` `_sh2_palette_wait` added to `chip.wait` directly. **Changed:** `SH2Side.hold`. 4. `slot.py` read the chips' clocks, skews and waits (`catch_up`'s oldest place, `sh2_mclk`, `sh2_register_read` offset 6's FIFO time). **Changed:** `SH2Side.oldest`, `state`, `cycle_end`. 5. `slot.py` `_cmd_lines` read `SH2Side.pending`. **Changed:** `SH2Side.has_source`. 6. `sh2side.py` read the 68000 side's `dreq_control & 1` for RV. **Changed:** `Sega32X.rv` (the property already there). 7. `sh2side.py` and `slot.py` each called the Mega Drive board's `vdp.sync`. **Changed:** one call, `Sega32X.video_lines_to`. 8. `sh2side.py` reached the cartridge through `device.board.game_cartridge`. **Changed:** `Sega32X.shared_cartridge`. All with no behaviour change (the gate below). Not changed: 9. `SH2Side.set_run` calls `Sega32X._cmd_lines` (a private method across modules) so that CMD follows INTM/INTS on release from reset. A named public call would be cosmetic; noted. 10. The VDP reads `Sega32X.hen` and `hcount` and the shared `framebuffers` tuple: registers and memory of the channel tables above, held by the device. For a threaded VDP the rows would need the shown buffer at each HBLANK fall: a frame-buffer snapshot or a sync there. Proposal for the fast tier, not a specification change. 11. `SH2Side.catch_up` runs the VDP's and PWM's events itself (`vdp.advance`, `pwm.run`): the order of record above. Splitting them out (an event queue the SH-2 loop consults by interface) is the step a threaded tier needs; it is larger than mechanical and touches the interleaving's code, so it is proposed, not done. 12. megadrive-python has no socket method for "publish your lines up to this clock"; the board's `vdp.sync` is called directly. Proposal for megadrive-python (upstream): a socket 6 method, then `video_lines_to` calls it. `tests/test_domains.py` checks that no module but `sh2side.py` (and the save-state host code) names a chip's state, that the others use only `SH2Side`'s declared calls, and that `board.vdp` is reached in one place. ## Gate for this change `scripts/gate.sh` on the unchanged tree and on this branch, every count and every PNG and WAV byte-identical: worklog.md and status.md record the numbers.