# Proposal: recompilation (JIT) in the turbo tier, and what a specification gives it Status: **accepted** by Aubrey 2026-11-25 (decisions page 2026-11-decisions.md: J1, J2, J4, J5 and J6 yes; J3 deferred). J1 and J5 are now in the charter (tiers; capabilities); the rest is the plan this file records. Drafted 2026-11-24. ## The question The family's identity is the readable Python specification, with Rust tiers that prove they match it. The charter already plans two ways past an interpreter: - **threads** from `x-rust-fast` on, synchronised at the domains and channels each specification declares ("Domains and channels"); - **turbo with recompilation** in era 4 (the era table), and "a turbo with block caching" for the SH-2 (families.md). The next boards are era 3: the 32X (two SH-2s at 23 MHz beside a whole Mega Drive), then Saturn, PlayStation and N64. On those, an interpreter is likely too slow for real use on weak targets, phones and the browser above all (play-md would host the 32X). So we will meet recompilation sooner than era 4. This proposal says where it sits, how it is proven, and what a specification should declare so that a JIT is built from it rather than beside it. ## Where it sits: a turbo fast path, nothing more A JIT fits the turbo definition exactly: `-fast`'s code plus a fast path that hands over to the inherited code whenever its conditions are not met. So: - **No new tier.** Recompilation lives in `x-rust-turbo`, as one fast path among others (block caching first, then a real code generator if block caching does not pay). - **The interpreter is the fallback, always present.** Any block the JIT cannot handle exactly (an unusual delay-slot case, a write into the block being run, a debugger hook) runs on the inherited `-fast` interpreter. Upstream fixes reach that interpreter by merge; a compiled path made wrong by a fix fails the gate on exactly the cases it affects. - **Off is always available.** The host can turn the JIT off; the board is still exact. - **"Prune what does not pay" holds.** A JIT must win on every layout and pay on the real target, or it is removed and recorded in not-kept.md like any other speed claim. ## How it is proven The charter's turbo obligation already is the right one: **execution-model fidelity on the observable contract** (every value the CPU reads and the cycle it reads it on; every interrupt as taken; video; audio; save states that continue identically). For a JIT, add: 1. **Block-boundary lockstep.** At every compiled block's exit, the CPU state and the time (cycles and bus time charged) equal the interpreter's at the same instruction. The lockstep harness runs the JIT against `-fast` with the JIT on and off, case by case. 2. **Bus-stream equality where the contract needs it.** Every read the CPU makes from a device another domain can see, at the same cycle. Inside a block, accesses to plain RAM no one else can observe may be merged or reordered; that is the room the JIT uses, the same room turbo already has. 3. **Seeded faults aimed at JIT bugs.** The methodology's rule that every gate proves it can fail, with faults chosen for what JITs get wrong: a flag computed lazily and never materialised; a stale block after self-modifying code; a block that runs past an interrupt it should have taken; a delay slot compiled outside its branch; cycle counts summed instead of charged per bus access. Each must be caught. 4. **Invalidation tests.** Self-modifying code, DMA into code, cache purges (the SH-2's associative purge, PM/HM chapter 8), and code in memory another CPU writes (the 32X's SH-2 pair sharing SDRAM). 5. **Never reported as hardware accuracy.** "The JIT matches the interpreter" is a differential claim, like every other tier step. Most emulator JITs are judged by "games seem to work". A JIT held to block-level lockstep and seeded faults against a readable specification would be unusual, and it is the part of this proposal that most belongs to the family's identity. ## What a specification declares for it A JIT needs four things. Each is useful in the specification for its own sake, so asking for them does not break "x-python must not grow complicated to suit downstream speed": | A JIT needs | The specification declares | Already useful for | | --- | --- | --- | | where a block must end | **sync points**: the next moment another domain could touch a channel, and where interrupts are sampled | threads (the same points), and readers: "when can the outside world interfere?" | | which writes invalidate compiled code | the **memory map's kinds** (ROM, RAM, I/O, shared, cache-purge space) and which domains can write each | the map itself, DMA, save states | | what each instruction costs | per-instruction **timing as data**: pipeline class, states, bus accesses and their kinds | the specification's own timing, and the docs' tables | | what each instruction does | **semantics in a restricted form**: one small, pure-looking handler per decode row, reading and writing named state, calling the bus through one interface | readers: it reads like the manual's own pseudo-code | ### The strongest version: semantics as data (CPU cores only) If each instruction's handler is written in a small, declared subset of Python (integer arithmetic on named registers and flags; bus reads and writes through one interface; no hidden state), a generator can read the handlers and emit both the Rust interpreter's arms and the JIT's templates from the same source. Then "the JIT does what the specification says" is partly true by construction, and the lockstep checks what is left (timing, invalidation, the generator itself). ARM's Sail and Ghidra's SLEIGH are the known examples of instruction semantics written once and used by several tools; we would cite them as ideas only, after a licence check in sources.md. sh2-python is already close: - `_decode.py` is **generated** from the programming manual's table A.50 and table 7.2 (`scripts/gen_decode.py`), and every row names its mnemonic, code pattern, handler, pipeline class, execution states and PM section. - the handlers (`_arith.py` and the rest) are small methods, one per instruction, with the manual's section in the docstring, e.g. `ADDC Rm,Rn -- Rn + Rm + T -> Rn, the carry -> T`. - the pipeline (`pipeline.py`) times every step from table 7.2's classes. So the step for the SH-2 is mostly to **name the subset** the handlers already keep to, and check it with a lint (a test that walks each handler's syntax tree and rejects anything outside the subset). ### Limits - **CPUs only.** Video chips, buses and sound are not instruction streams; they stay ordinary readable Python. A JIT for them makes no sense; their speed comes from threads and turbo's other fast paths. - **Readability decides.** If the restricted form ever reads worse than plain Python, the plain form wins and the generator gives way. The specification is the authority and the longest-lived asset; the generator is a convenience. - **x-rust stays a transcription.** A generated interpreter would change how `x-rust` is written for that CPU (generated arms instead of a hand transcription, with the generator itself in the repository and gated). That is a methodology change for Aubrey to accept or refuse per core; the default is a hand transcription, as now. ## Concessions it may need Cycle exactness at block edges is hard. Two techniques are exact and need no concession: charging cycles per bus access inside compiled code, and ending blocks at every declared sync point. Two are not exact and would be named concessions (off by default, with their measured cost): - **coarser sync**: running a block past a sync point and reconciling after (fewer exits, occasional observable drift); - **idle-loop skipping** inside compiled code. The browser adds one target-specific question: a JIT there emits WebAssembly modules at run time (as v86 does for x86). Whether that pays on phones is a measurement, not a guess. ## Targets that forbid recompilation (added 2026-11-25) Aubrey wants the family on the iPhone. Apple does not let ordinary native apps create executable memory, so **a native iOS build cannot JIT** (to confirm against Apple's current guidelines before relying on it). Web pages are different: Safari compiles WebAssembly itself, and a page may hand it WebAssembly generated at run time (v86 runs x86 this way on iPhones), so the **web hosts may keep a "recompile to WebAssembly" path** even on iOS (to measure, not assume). Consequences: - **The no-JIT path must be good enough by itself.** Every board's speed plan, in order: turbo's scheduling (catch-up, whole-line paths), a **cached interpreter** (decode once, replay the handler sequence: no executable memory, allowed everywhere), threads around domains, and only then a code generator, on targets that allow one. - **A JIT is a capability, not an assumption.** The capability contract (charter, "Capabilities and the adaptive host") gains `recompile: native | wasm | none`, and the adaptive host chooses; every board must meet its stated target with `none`, or say plainly on which targets it cannot. - **The specification serves both.** Sync points, memory kinds and timing data serve the cached interpreter and threads as much as a JIT; restricted semantics serve a generated interpreter even where no JIT is allowed. ## Boundaries, family by family (added 2026-11-25) Where each board's blocks must end, what a fast path must watch, and which speed tool is expected to carry it. "Sync points" are the domain channels the specification declares (charter, "Domains and channels"); a block or a thread may run freely up to the next one. Proposed defaults, to be confirmed in each specification as its domains are named. | Family | Domains (families.md) | Block / sync boundaries | Code invalidation to watch | Expected speed tools | JIT? | | --- | --- | --- | --- | --- | --- | | Era 1 (2600, NES, Game Boy, SMS, Coleco, Atari 8-bit) | one | every bus cycle is observable (a mid-instruction register write changes a pixel) | ROM only; mapper bank switches; RAM code (rare) | `-fast` as now; turbo's whole-line paths | **no**: the CPU is a small share of the frame; nothing to gain | | Mega Drive | 68000 side; Z80 sound side | 68000 VDP accesses and the Z80 bus request/bank window; Z80 accesses to the 68000 bus and the YM2612 | ROM; 68000 RAM code (some games); Z80 RAM (uploaded drivers) | threads (FM synthesis first), turbo for the VDP | no | | SNES (confirmed 2026-11-25) | S-CPU with both PPUs, DMA and the cartridge; APU (SPC700 + S-DSP, one domain) behind 4 ports; coprocessors per cartridge | the four APU ports (the port race shows the boundary is sub-cycle: sync on every port access); PPU register writes; coprocessor mailboxes | WRAM code (common: decompressors run from RAM); SPC700 RAM (uploaded drivers) | threads (SPC700 + S-DSP domain), turbo | no; Super FX / SA-1 maybe a cached interpreter | | PC Engine (+CD) | main; CD drive and ADPCM stream | VDC/VCE writes; CD and ADPCM registers | ROM; RAM code; CD-loaded code (all of a CD game's code is RAM) | turbo; CD stream as a producer | no | | Game Boy Advance | one, plus the sound FIFOs | I/O register writes; DMA starts; the FIFO DMA requests | IWRAM/EWRAM code (common: ARM code copied to IWRAM for speed) | cached interpreter; turbo for the PPU | maybe (ARM7 at 16.8 MHz on phones) | | Sega CD (confirmed 2026-11-25) | main (the Mega Drive); sub (68000, gate array, CDC, graphics unit, PCM); CD drive stepped on sub clocks | the gate array's shared registers and word-RAM swaps; the CDC/CDD channel | word RAM and PRG RAM (all code is RAM) | threads (main / sub / drive), turbo | no; cached interpreter for two 68000s if needed | | **32X** (confirmed 2026-11-25) | one locked clock, four scheduling domains: Mega Drive side; SH-2 pair (one domain); 32X VDP; PWM (as built, VDP and PWM events run inside the SH-2 pair's catch-up) | the 68000/SH-2 communication registers, the FIFO, frame-buffer swaps; between the two SH-2s: shared SDRAM, caches (purge space), FRT/interrupts | SDRAM code (most of a game), the SH-2 caches, the associative purge (HM ch. 8), each SH-2 seeing the other's writes | threads (MD side / SH-2 pair / VDP / PWM), cached interpreter for the SH-2s | **pilot**: SH-2 cached interpreter first, recompile only where measured need and allowed | | Saturn | 2 SH-2s; 68EC000 sound + SCSP; SCU (+DSP); VDP1; VDP2; CD block | SCU registers and DMA; the SCSP's RAM; VDP1 command list; CD block | work RAM code everywhere; SCU DSP program RAM | threads (most domains of the planned boards after Jaguar), cached interpreters | likely for the SH-2s on weak targets | | PlayStation | R3000A + GTE; GPU; SPU; CD; MDEC; DMA | DMA channels; GPU command FIFO; SPU registers; CD controller; the root counters and interrupts | main RAM code (all code is RAM); the instruction cache (isolation for cache flushes) | threads (GPU, SPU, CD), cached interpreter | maybe: desktop interpreters reach full speed elsewhere; phones are the question | | N64 | VR4300; RSP; RDP; audio, video and PI/SI interfaces | RSP/RDP command lists; the shared RDRAM; interrupts from each interface | RDRAM code; the VR4300 caches and TLB; RSP IMEM (microcode uploaded per task) | threads (RSP, RDP), cached interpreters | **yes** where allowed; on no-JIT targets expect named concessions (RSP microcode at a higher level) | | Jaguar | 68000; GPU and DSP (JRISC); blitter; object processor | the shared bus arbitration; GPU/DSP local RAM; blitter and OP registers | GPU/DSP local RAM (code uploaded constantly) | threads, cached interpreters for the JRISC pair | maybe | What every specification adds, in the order the boards get there: name the domains and channels (done for sega32x, segacd and snes on 2026-11-25); mark each channel's accesses as sync points; give the memory map's kinds and who can write each; for CPUs, keep the handlers to the named subset. None of it changes behaviour, so each step is gated by byte-identical traces. ## The order this would happen in 1. sh2-rust (transcription), sh2-rust-fast (interpreter, threads around the SH-2 pair): the fallback must exist first. 2. Name the handler subset in sh2-python and add its lint, with no behaviour change (the gate: byte-identical traces). 3. sh2-rust-turbo: block caching first (decode once, run cached handler sequences), with the proof obligations above. Measure in the browser through a 32X build of play-md. 4. Only if block caching does not pay: a code generator, native and WebAssembly. 5. Then the same pattern for the next CPUs: the Saturn (SH-2 again, plus the SCU DSP), the PlayStation (R3000A: load delay slots, the GTE as a coprocessor), the N64 (VR4300, and the RSP's vector microcode, where high-level emulation would be a concession). ## Decisions for Aubrey 1. Accept "recompilation is a turbo fast path with the interpreter as fallback" into the charter, with the five proof additions above. 2. Accept sync points, memory kinds, timing data and (for CPUs) restricted semantics as things a specification declares, starting with sh2-python. 3. Allow, per core and only by his decision, a generated `x-rust` interpreter from the specification's handlers, instead of a hand transcription. 4. The SH-2 as the pilot, in the order above. 5. (2026-11-25) The no-JIT path as the main plan: every board meets its target with `recompile: none` or says where it cannot; a JIT is an extra capability the adaptive host may choose (native where allowed, WebAssembly in the browser). 6. (2026-11-25) The family-by-family boundaries above as the starting defaults, each confirmed in its specification when its domains are named.