# 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; `tests/test_domains.py` checks the boundary below against the code. Evidence tags are the family's (methodology.md); a tag quoted from the code is the code's. The proposals' table (emulator-lineage `docs/proposals/2026-11-jit.md`, "Boundaries, family by family") gave the SNES row as a default; this file confirms it for the board as built. ## Domains | Domain | Clock | Parts (code) | Evidence | | --- | --- | --- | --- | | **S-CPU** | master clock, 21.477272 MHz NTSC (`machines.py` `NTSC.master_hz`) | the 65816 (`w65c816.W65C816`), the address decoder and its 6/8/12-clock cycles (`board.py` `SNES._speed`), DMA/HDMA (`dma.py` `DMAController`), the multiplier/divider, the H/V IRQ and NMI timer and auto-joypad (`board.py` `SNES.step`, `_irq_poll`, `_alu_edge`, `_joypad_edge`), WRAM, both PPUs (`ppu.py` `PPU`), the cartridge (`cartridge.py`), the joypad ports (`controllers.py`) | DOC fullsnes, anomie timing; the event positions OBSERVED (ares 9408cb4), named per rule in board.py | | **APU** | its own resonator, 24.576 MHz; one S-SMP cycle = 24 resonator clocks (`apu.py` `APU.__init__`, `ratio`) | the SPC700 (`spc700_python.SPC700`), the S-SMP's I/O page, IPL overlay and timers (`smp.py` `SMP`), the S-DSP (`dsp/sdsp.py` `SDSP`), 64 KiB ARAM | DOC wiki/Timing "Audio"; docs/timing.md "Two clock domains" | The S-SMP and S-DSP are one domain: the DSP is clocked once per S-SMP clock inside each SPC700 bus cycle (`smp.py` `SMP._advance`; the DSP-before-access order is HYPOTHESIS C14, settled for the spc_dsp6 timing tests through the board) and both use ARAM every cycle; splitting them gains nothing and would need a sync on every ARAM access. The PPUs are in the S-CPU domain (the same master clock; the PPU is caught up lazily, below). They are the natural **pure producer** for a threaded tier (charter: "picture drawing from a register-write log"), but nothing in this specification separates them yet: see "Wires inside the S-CPU domain". Coprocessors (DSP-n, SA-1, Super FX, ...) are not built (docs/status.md "unsupported"); each will be named here as it comes, with its mailbox as a channel. ## Channels There is **one** channel between the two domains, the four communication ports, now a named object: `smp.py` `CPUPorts`. | Channel | Ends | Direction | What | Evidence | | --- | --- | --- | --- | --- | | **APU ports** `$2140-$2143` / `$F4-$F7` | S-CPU: `board.py` `SNES.bus_read`/`bus_write` → `apu.py` `APU.cpu_read`/`cpu_write` → `CPUPorts.cpu_read`/`cpu_write`. APU: `smp.py` `SMP._read_io`/`_write_io` → `CPUPorts.smp_read`/`smp_write`; `SMP._write_control` → `CPUPorts.smp_clear` | both: two sets of four 8-bit latches, `from_cpu` and `to_cpu` | `$2140-$217F` all mirror the four ports (`bus_read`: `r & 3`) | DOC fullsnes "SNES APU Main CPU Communication Port" (two sets of latches), "00F1h - CONTROL" (bits 4-5 clear the S-CPU-to-S-SMP latches) | | APU reset | none yet | | the S-SMP's /RESET is the console's reset; the board has no reset button yet (`smp.py` `SMP.reset` exists, no caller on the board) | DOC fullsnes | | Audio out | `APU.catch_up` → `SDSP.take_samples` → `APU.samples` | APU to host | 32 kHz stereo, a producer stream (no hardware reader in the S-CPU domain) | DOC | No interrupt line, DMA or shared memory crosses between the domains: the S-SMP cannot see the A bus and the S-CPU cannot see ARAM (DOC fullsnes "SNES Memory Map", "SNES APU Memory and I/O Map"). The S-CPU's DMA cannot reach the ports either way except as B-bus addresses `$40-$43` (`dma.py` transfers go through `SNES.bus_read`/`bus_write`, so a DMA to `$2140` is an ordinary port access and the same sync point). ### Sync points Every access to the channel, from either side, is a sync point; nothing else is. - **S-CPU read or write of `$2140-$217F`** (`APU.cpu_read`, `APU.cpu_write`): the APU is first run to the S-CPU's time, `int(master_clock * ratio)` S-SMP clocks (`APU.catch_up`). The access's master clock is `SNES.clock` at the moment the data is sampled: 4 clocks before the read cycle's end (`SNES._read`), at the write cycle's end (`SNES._write`). DOC/OBSERVED (ares CPU::read). The port race (docs/port-race.md, Kawasaki and Zenki) shows the true boundary is below one S-SMP cycle; a threaded tier must sync on every port access, as here. - **S-SMP read or write of `$F4-$F7`, and a CONTROL write with bit 4 or 5 set**: the S-SMP runs ahead (it is the follower, in whole instructions); its stores are logged with their S-SMP clock (`CPUPorts.smp_write`) so an S-CPU read at an earlier time sees the earlier value (`CPUPorts.cpu_read`; the code tags it DESIGN, and it is FITTED in effect to Rendering Ranger R2's upload handshake, docs/status.md triage 2026-11-23). The other way, an S-CPU store is posted with its S-SMP cycle before the catch-up (`APU.cpu_write` → `CPUPorts.cpu_post`), and the S-SMP's port reads and clears consult the posts by their own clock (`CPUPorts.smp_read`, `smp_clear`), so the store is seen from the S-SMP cycle that overlaps it (since 2026-11-25; DESIGN, the same rule as the store log: each access at its own time, an S-SMP access at its cycle's end). - **Frame end** (`SNES.run_frame` → `APU.catch_up`): not a hardware sync point, only so the sound of the frame is out; a threaded tier may move it. The overshoot: after `APU.catch_up` the S-SMP may stand up to one SPC700 instruction **past** the S-CPU's time (`APU.catch_up` runs whole instructions). Until 2026-11-25 an S-CPU write was stored after the catch-up, so an S-SMP read or CONTROL clear made in the catch-up's last instruction saw it up to that instruction late (measured: 0-3 S-SMP cycles late, rarely up to 6; in 400 frames of Zenki's hang 80,235 S-SMP port reads missed a write and 108,216 clears were ordered before a write they came after; docs/port-race.md "The overshoot, measured"). The post above fixes that for the catch-up the write itself causes, and `CPUPorts.cleared` orders a clear against a write that arrives after the S-SMP has passed it. **What remains:** an S-SMP *read* made in an earlier catch-up's overshoot (after an S-CPU read, say) before a later S-CPU write's time has already returned the old value; fixing it needs the S-SMP to pause mid-instruction (the greenlet experiment of docs/status.md, 16 % slower). Measured over 400 frames: Zenki 0 such reads, Kawasaki (USA) 16, the port-race probe 0. With the change both Kawasaki images boot; Zenki still hangs (docs/port-race.md: the board's window is now D S-SMP cycles, Mesen2's half a cycle shorter, and Zenki's loop needs it below 166 master clocks). ### Order of record What this specification runs, serially (`board.py`, `apu.py`, `smp.py`): 1. The S-CPU leads. `SNES.run_frame` steps the 65816 one instruction at a time; the core calls `_read`/`_write`/`_idle` once per bus cycle. 2. Inside each cycle callback: deferred NMI/IRQ are flushed, pending DMA/HDMA runs first (`SNES.dma_edge`, between CPU cycles), then master time advances (`SNES.step`: 2 clocks at a time; NMI and IRQ polls at H = 2 mod 4, the auto-joypad unit every 128 clocks, a new line at H wrap with `PPU.end_line` then `PPU.scanline`; the DRAM refresh and the HDMA triggers after the advance), then the access is made (`SNES.bus_read`/`bus_write`), then one ALU step. OBSERVED (ares CPU::step, dmaEdge), with the board's MEASURED corrections named in board.py. 3. The PPU runs lazily: before any `$2100-$213F` access (`PPU.sync`, to the board's H) and at each line's end (`PPU.end_line`). OBSERVED (ares PPU::main). 4. The APU runs only when the S-CPU touches a port, and at frame end (sync points above). An S-CPU port write is posted at its S-SMP cycle first, then the APU is caught up, then what is left of the post is applied (`APU.cpu_write`). Inside, the S-SMP steps whole SPC700 instructions; per SPC700 bus cycle it first clocks the S-DSP and the three timers through the cycle, then makes the access (`SMP._advance`; HYPOTHESIS C14). Time between the domains is exact: the APU's clock is kept as a `Fraction` of master time (`APU.__init__`), so the interleaving is deterministic. ## Memory map: kinds and writers S-CPU A bus (`board.py` `SNES.bus_read`/`bus_write`; DOC fullsnes "SNES Memory Map", "SNES I/O Map"): | Region | Kind | Written by | Notes | | --- | --- | --- | --- | | `$7E0000-$7FFFFF`, and `$0000-$1FFF` of banks `$00-$3F`/`$80-$BF` | WRAM (128 KiB) | S-CPU, DMA (A bus), and DMA or the S-CPU through the B-bus port `$2180-$2183` | code runs from it (decompressors): a code-invalidation region for any cached interpreter | | `$2100-$213F` | I/O: the PPUs' B-bus ports | S-CPU, DMA, HDMA | VRAM, OAM and CGRAM are reached only through these ports (`ppu.py` `PPU.write`) | | `$2140-$217F` | I/O: **the channel** (APU ports) | S-CPU, DMA | sync point | | `$2180-$2183` | I/O: WRAM port | S-CPU, DMA | | | `$4016-$4017` | I/O: joypad serial ports | S-CPU (strobe) | | | `$4200-$421F` | I/O: the S-CPU's own registers | S-CPU (DMA is barred: `dma.py` `valid_a`) | | | `$4300-$437F` | I/O: DMA channel registers | S-CPU (DMA barred) | | | the rest | cartridge: ROM, SRAM (`cartridge.py` `Cartridge.read`/`write`) | ROM: nobody; SRAM: S-CPU, DMA | coprocessor registers will sit here | PPU memories (`ppu.py`): VRAM (32 Ki words), OAM (544 bytes), CGRAM (256 words), each written only through the PPU's ports by the S-CPU or DMA/HDMA, read by the picture as it draws. S-SMP bus (`smp.py` `SMP._read`/`_write`; DOC fullsnes "SNES APU Memory and I/O Map"): | Region | Kind | Written by | | --- | --- | --- | | `$0000-$FFFF` | ARAM (64 KiB) | the SPC700 (when TEST allows), and the S-DSP's echo buffer (`dsp/sdsp.py`, the echo write, when `ram_writable`) | | `$00F0-$00FF` | I/O page (writes also reach RAM); `$F4-$F7` **the channel** | the SPC700 | | `$FFC0-$FFFF` | IPL ROM overlay for reads while CONTROL bit 7 is set; writes go to RAM | nobody (ROM) | ARAM is a code region for the S-SMP (uploaded sound drivers) and a data region for the S-DSP (samples, echo): one domain's memory, so it needs no cross-domain sync. ## Direct pokes across the boundary (the charter's specification bugs) Found on 2026-11-25, and what was done: 1. **The ports were loose S-SMP attributes** (`SMP.input_ports`, `output_ports`, `_port_log`), written by the S-CPU side's `cpu_write_port` and read by `cpu_read_port` inside the SMP class. **Changed:** they are one object, `CPUPorts`, with a method per access from each side; `SMP.input_ports`/`output_ports` stay as read-only views of the same lists. No behaviour change (gate below). 2. **The no-IPL stand-in wrote the S-SMP's output latch from the S-CPU side** (`APU.cpu_write`: `self.smp.output_ports[0] = value`, and `APU.__init__` replacing the list). **Changed:** `CPUPorts.stub_echo` and an in-place fill of `CPUPorts.to_cpu`. Nothing else crosses: `tests/test_domains.py` checks that the S-CPU side's modules name none of the APU's state and reach it only through `APU.cpu_read`, `cpu_write` and `catch_up`, that the APU's modules import nothing of the S-CPU side, and that `APU` touches only its declared reach on the S-SMP. ### Wires inside the S-CPU domain (not cross-domain; listed for the producer split) These are reads of another chip's state inside one domain. They are not bugs by the charter's definition, but a threaded tier that runs the picture as a producer would need each as a named input: - the PPU reads the board's H/V counters and field directly (`ppu.py` `PPU.sync`, `end_line`, `_active`, `_cgram_target`, `latch_hv`, `write` for INIDISP; `ppu_render.py` reads `ppu.board.field` for interlace). On the console the counters are the PPU's own and drive the S-CPU's timing (DOC fullsnes); here the board keeps them. - the PPU reads WRIO bit 7 (`PPU.read` for `$2137` and `$213F`): on the console the EXTLATCH pin, wired from the S-CPU's I/O port (DOC anomie regs "2137 SLHV"). - the PPU reads `board.machine.pal` for STAT78 bit 4 (a strap, constant). - the board reads `PPU.vdisp` (overscan) and `PPU.interlace` for its line and frame timing (`SNES._scanline`, `_nmi_poll`, `_joypad_edge`, `_read_io` HVBJOY). - DMA reads and writes `SNES.mdr` and calls `SNES.bus_read`/`bus_write`/`dma_step`/`dma_edge` (`dma.py`): DMA is part of the S-CPU, so this is one chip. Proposal (not done, larger than mechanical): a register-write log out of `PPU.write` and the counter values at each write, so the picture can be drawn beside the S-CPU; it touches the hot path of every `$21xx` access and the picture gate's byte-identity, and belongs with the Rust tiers' design. ## Gate for this change Byte-identical outputs before and after, at the commit that adds this file (docs/status.md has the commands): unit tests; the smoke list (`smoke.py board --window 30`, every verdict and every `board-*.png`); the probe suite's battery RAM.