# Domains, channels and speed ## Domains | Domain | Parts | Clock | | --- | --- | --- | | **Main** | TLCS-900/H (tlcs900h-python) with micro DMA, the SoC peripherals (timers, interrupt controller, serial, watchdog, clock gear, RTC, ADC), the K1GE/K2GE, work RAM, cartridge flash, the BIOS, the two DACs | 6.144 MHz crystal (the CPU's state rate is U2; the CPU clock gear divides it) | | **Sound** | Z80 (z80-python), the T6W28, the Z80's side of the shared RAM | 3.072 MHz (crystal / 2) | The K2GE is driven by the same clock as the CPU and its registers are written by the CPU directly, so it stays inside the main domain in the order of record (a later pure producer can draw lines from a register-write log). The DACs are written by the main CPU and mixed with the PSG's output on the host side. ## Channels (the only places the two domains meet) | Channel | Direction | Source | | --- | --- | --- | | Shared RAM: main 7000-7FFF = Z80 0000-0FFF | both | JT, M, ares | | Communication byte: main writes SFR 0xBC, Z80 reads/writes 8000 | both | ares | | Z80 NMI: main writes SFR 0xBA | main to sound | ares, JT | | Z80 IRQ: main CPU timer 3's output | main to sound | ares cpu/timers.cpp | | Main INT5: Z80 writes C000; Z80 `OUT (FF)` acknowledges | sound to main | ares | | Z80 run/reset: SFR 0xB9 (55 run, AA stop); PSG/DAC select: SFR 0xB8 | main to sound | ares, JT | | Audio out: T6W28 per-side samples, DAC values | both to host | ares | **Sync points** (the next moment the other domain could touch a channel): every main-CPU access to 7000-7FFF or to SFRs B8-BC, every Z80 access to 0000-0FFF, 8000 or C000, and every timer 3 output edge. Between those, each domain may run ahead. The order of record in ngp-python: main first, the Z80 stepped to the same time at each channel access (the specification steps the sound domain inside the main one at channels, as the charter allows), and the arbitration on shared RAM (U5) decided at that moment. **Memory kinds** (for a later block cache): BIOS ROM (read only), cartridge flash (read only except during the BIOS's flash commands, which invalidate), work RAM (main only), shared RAM (written by both domains: code there, the Z80 driver, invalidates on either side's writes), VRAM (written by the CPU and micro DMA). ## Instructions per second - Main CPU: MAME's NGPC BIOS trace, 66,779 instructions in 307,454 states over 3 frames = 4.6 states per instruction; 6,144,000 / 4.6 = **1.34 M instructions/s**; worst case 6,144,000 / 2 = 3.07 M/s (half of each if U2 resolves to 3.072 M states/s). - Z80: 3,072,000 Hz / about 6 cycles per instruction on average = **about 0.5 M instructions/s**, mostly a sound driver idling between NMIs. - K2GE: 160 x 152 x 59.95 = 1.46 M pixels/s (per line: 160 pixels from three layers and up to 64 sprites). - T6W28: 192 k steps/s (6.144 MHz / 32); DACs at the main CPU's write rate. ## The no-JIT path, by target Family anchors: m68000-python runs about 33 M instructions/s on PyPy (core alone), 1.4 M on CPython; m68000-rust-fast about 190 M/s and z80-rust-fast 340-390 M/s native. | Target | Verdict | | --- | --- | | Python reference (PyPy) | 1.3 M + 0.5 M instructions/s with per-access timing and a dot-level K2GE is within an order of magnitude of PyPy's core speed: real time is possible but not promised; reference boards are correct, not fast | | Desktop native (ngp-rust, -fast) | over 100x headroom on the CPUs; the K2GE line renderer is the larger cost and still small. **Real time with `recompile: none`** | | Phone (native; iOS forbids JIT) | at a third to a half of desktop speed, tens of times headroom. **Real time with `recompile: none`** | | Browser (wasm) | about half native: still tens of times headroom. **Real time with `recompile: none`**; run-ahead of 1-2 frames affordable | A cached interpreter and threads at the channels above are not needed for real time; the two-domain split is declared so that run-ahead on weak devices can use a thread for the Z80 and T6W28 if a measurement ever says so. Capabilities to report later: a frame's milliseconds; a snapshot of about 12 KiB work RAM + 4 KiB shared + 16 KiB VRAM and registers + the flash's changed blocks (the cartridge itself is named by SHA-1).