# rick-roll-32x — build plan The same joke as `rick-roll-md` (the whole 3:31 video with sound, from a cartridge), rebuilt for a **Sega 32X cartridge** to answer one question: *how good could it have been?* Forked 2026-10-24 from `rick-roll-md` at `83650e1`; the MD project's plan and worklog are kept in `docs/md-PLAN.md` and `docs/md-WORKLOG.md`, and the MD history is in this repo's git log (remote `md-upstream` = `D:\dev\rick-roll-md`). The verification oracle is the **32X emulator `sega32x-python`** (`D:\dev\sega32x-python`, GitHub `alewman/sega32x-python`, private), built in a separate session; pull it for PWM (milestone 5, in progress). Nothing here is verified until it runs there; the MD project's rule still holds: fix for hardware semantics, never bend the ROM to one emulator. > **Status of the facts below.** Section 2 was checked on 2026-11-04 against the > emulator's docs; section 1's *(unverified)* tags are settled there. One fact (the > SH-2 cache size) is still unchecked. --- ## 1. What the 32X changes | | Mega Drive build | 32X build | |---|---|---| | Picture | 4bpp tiles, 4 palette lines × 16 + S/H, VRAM cache of ~1,790 tiles | **framebuffer**: 8bpp packed pixel from a 256-entry RGB555 palette, or 15bpp direct colour *(unverified)* | | Decoder | none — the 68000 is a DMA-list executor, all intelligence offline | **two SH-2s at ~23 MHz** *(unverified)*: a real decoder (block transform / motion compensation / VQ) is affordable | | Audio | Z80 + YM2612 DAC, 8-bit, 17.6 kHz | **PWM**, 2 channels, rate set by a cycle register *(unverified)*; YM2612/PSG still there | | ROM | 16 MB on Krikzz's Extended SSF mapper | **4 MB** SH-2 cart window, no mapper *(unverified; see D1)* | | Bytes per video frame (song, all-in) | ~3,180 (16 MiB / 5,274) | **~795** (4 MiB / 5,274) | The MD build was bound by bytes and by the tile cache. The 32X build has a quarter of the bytes and orders of magnitude more decoder: the question becomes **codec quality per byte**, not hardware tricks. That is what makes it a different project, not a port. ### 1.1 The budget, before anything is measured 4 MiB = 4,194,304 B over 211 s ≈ **19.9 KB/s total**. | audio choice | bytes for 211 s | left for video | per video frame | |---|---|---|---| | 8-bit PCM 22 kHz | 4.65 MB | — | does not fit | | 4-bit IMA ADPCM 22 kHz | 2.33 MB | 1.86 MB | ~350 B | | 4-bit IMA ADPCM 11 kHz | 1.16 MB | 3.03 MB | ~575 B | | transform codec ~24 kbit/s, decoded on an SH-2 | 0.63 MB | 3.56 MB | ~675 B | | transform codec ~16 kbit/s | 0.42 MB | 3.77 MB | ~715 B | So under D1(a) audio *must* be a real codec, and video lands around **600–700 B per frame**. For scale: the MD build spends ~2,330 B/frame on video. The 32X has to win on decoder intelligence (motion compensation, per-pixel colour, a transform or VQ codec that the SH-2s can run at 25 fps), not on bytes. ## 2. Hardware facts, checked against sega32x-python (phase 0, 2026-11-04) Checked against `D:\dev\sega32x-python` (GitHub `alewman/sega32x-python`, private; master `5f2067c`): `docs/start-here.md`, `docs/undocumented-behavior.md`, `docs/claims.md`. Its sources are Sega's 32X Hardware Manual (scan), the service manual, and testpico/Mars Check for hardware-corrected behaviour. "Manual" below means specification tier, not a hardware measurement. | fact | status | source / note | |---|---|---| | 2 × SH-2 (SH7604 core) at VCLK × 3 = MCLK × 3/7: 23.011 MHz NTSC, 22.8 MHz PAL; 7 MCLK = 3 SH-2 clocks exactly | **confirmed** (Manual 3.3) | phase-locked to the 68000 | | 256 KB SDRAM shared by both SH-2s | **confirmed** (Manual) | simultaneous writes from both SH-2s may lock up (wiki): design the decoder so they don't | | 4 KB cache per SH-2 | *(unverified)* | belongs to sh2-python, not checked here | | 2 × 128 KB frame buffers, FS swaps at the next VBlank; 256-word line table at the start of the displayed buffer | **confirmed** (Manual 3.2.3) | a line runs into the overwrite image if it must | | Modes: packed pixel (8bpp), direct colour (BGR555 + priority bit), run length (8-bit length + 8-bit index, 1–256 px) | **confirmed** (Manual) | direct colour 320×224 = 143,360 B > 128 KB: does not fit a full frame | | Palette: 256 words BGR555 + through bit; access only in blanking (PEN) or in direct-colour mode | **confirmed** (Manual) | a mid-picture palette change waits up to a line | | **320×240 on PAL** (240 bit), non-interlaced only; MD must be H40 and V28/V30 to match | **new** (Manual) | a PAL build can be 320×240 — a phase-2 candidate against dual-region V28 | | Frame-buffer `$00` byte writes are ignored (word `$0000` works) | **new** (Manual, every emulator) | the decoder must write words | | Auto fill: 7 + 3 × length cycles, wraps in a 256-word page; SH-2s can't touch the DRAM meanwhile | **new** (Manual, wiki) | cheap flat fills | | The MD VDP composites with the 32X by priority bit/PRI and YS | **confirmed**, but **detectors only** | the emulator's composite and its RGB levels are assumptions (`claims.md`) | | PWM: 2 channels, 12-bit width, **3-entry FIFO** per channel, period set by the cycle register in SH-2 clocks, timer interrupt every TM periods, paces DREQ1 for SH-2 DMA ch. 1 | **confirmed** (Manual 3.2.1) | ~22 kHz → period ≈ 1,045 clocks ≈ 10 bits of range, as assumed. Period `cycle` vs `cycle − 1`: **contested**. PWM output level: unmeasured | | Cartridge: SH-2 sees 4 MB at `$02000000` **only while RV = 0**; the 68000 sees 512 KB fixed at `$880000` and a 1 MB window at `$900000` (4 banks, `$A15104`) | **confirmed** (Manual 3.1) | **for D1**: cartridge writes (a mapper's bank register) need RV = 1, which stalls the SH-2s on cartridge access; with RV = 1 and SH-2s running from the cart they can hang (wiki) — park them in SDRAM first. There is no SH-2-side bank register: the 4 MB window is all the SH-2s see | | Communication: 8 comm-port words, CMD interrupt (68000 → SH-2), DREQ FIFO (68000 → SH-2 DMA ch. 0); nothing interrupts the 68000 | **confirmed** (Manual) | simultaneous writes to one port: undefined | | Boot: Sega's initial program at `$3F0` byte for byte (security), user header at `$3C0–$3EF` names the initial SDRAM load; master writes `M_OK`, slave `S_OK` | **confirmed** (Manual 5.1–5.2) | the emulator needs the user's own boot ROMs (checked by SHA-1) | | SH-2 interrupts: VRES 14, VINT 12, HINT 10, CMD 8, PWM 6; FRT output A wired into /IRL0 | **new** (Manual, Bulletin #27) | use Sega's unified handler | **The oracle's limits** (from `claims.md` and its worklog): no hardware capture of a 32X exists or is planned, so the composite and colours rest on documents and detectors; PWM sound is milestone 5, in progress (pull again later); speed is about **0.4 MD frames/s on PyPy** (~138k SH-2 instructions/s for both), so a 15 s PAL clip (750 frames) is ~30 min and the full song (~10,550 frames) ~7 hours. A `-fast` fork is its plan for speed. ## 3. Architecture (proposed) - **Offline encoder (Python, grows from `rrmd/`)**: ingest and audio extraction carry over; the tile/VQ/cache-sim/S-H stages do not apply. New: a codec designed *with* the decoder budget, with the encoder rendering the exact output so verification stays byte-exact (MD claim 4: "every frame identical to the encoder's prediction"). - **SH-2 master**: video decode into the back frame buffer, swap at VBlank, audio as the master clock (the MD's A/V-sync design carries over unchanged). - **SH-2 slave**: audio decode into the PWM FIFO, or half the video decode — decide from measured cycle counts. - **68000**: boot, then idle (or drives the MD planes for a title/credits layer). - **Z80**: unused, or kept for YM2612 if PWM proves weak. ## 4. Phases | phase | build | exit criterion | |---|---|---| | **0 Facts** | check section 2 against the hardware manual and the new emulator; SH-2 toolchain in WSL | every *(unverified)* tag removed or corrected | | **1 Hello, 32X** | boot, security block, one 8bpp frame in the frame buffer | frame pixel-identical to the encoder's render in the 32X emulator. **DONE 2026-11-04**: pixel-identical in sega32x-python from frame 120; BlastEm agrees up to its colour levels | | **2 Codec study** | offline only: codec candidates measured on the MD project's test clips at 600–700 B/f, plus their SH-2 cycle cost | a chosen codec with measured error and a decoder that fits 25 fps on one SH-2. *2026-11-04: C1 (MC + DCT) measured, see WORKLOG* | | **3 Streaming** | decoder on SH-2, 15 s clip | every frame identical to the encoder's prediction, in order, 2:1 PAL | | **4 Audio** | PWM driver, chosen audio codec, audio-master sync | sample stream exact against the encoder's decode; 0 drift | | **5 Full song** | rate control to exactly 4 MiB | full song verified; side-by-side against the MD build's frames | | **6 Hardware** | run on a real 32X (flashcart) | matches the emulator on the compared frames | ## Decisions - **D1 — Cartridge size: a mapper, decided 2026-10-24.** Getting a mapper working on the 32X is itself a goal of the project (with the emulator session); the video is the demo that proves it. That adds a phase between 1 and 3: define the mapper (which bus side pages it, the SH-2 window, how the 68000's `$900000` bank interacts), model it in the emulator, test it with a synthetic ROM like `tools/mapper_test`, then on a flashcart. The 4 MB budget in 1.1 stays as the fallback and as the "as shipped" comparison point. - **D2 — Audio codec:** decided in phase 2 from the table in 1.1. - **D3 — Picture mode:** 8bpp packed pixel 320×224 is the working assumption (fits one buffer, 256 colours per frame); direct colour and lower resolutions are phase-2 candidates. - **Carried over from the MD project:** V28 320×224 dual-region, audio is the master clock, the user runs ffmpeg extraction, the agent reports numbers and does not describe the video's content.