# Timing Everything here is in master clocks (MCLK) unless it says otherwise. SH-2 clock = MCLK x 3/7; 68000 clock = MCLK / 7. Sources are named per row; "Manual" is Sega's *32X Hardware Manual* (see [start-here.md](start-here.md)), a provisional June 1994 document: its numbers are design targets, and **no hardware capture of any 32X timing exists that this repository could find** ([validation.md](validation.md)). ## The clock domain The 32X has no crystal of its own. Its PLL takes the 68000's clock (VCLK) from the cartridge slot and makes 23 and 46 MHz (service manual block diagram, scan `A406a`); the SH-2s run at 3 x VCLK. So: - 7 MCLK = 1 68000 clock = 3 SH-2 clocks, exactly, for ever. The board needs no rational drift handling for the 32X: an integer remainder of `3 * mclk mod 7`. - PAL machines run the SH-2s at 22.8 MHz (Manual, Cycle Register), the same ratio. - The PWM's period unit is the SH-2 clock. - The 32X VDP is clocked by the Mega Drive's **EDCLK** (pixel clock) and timed by its **HSYNC** and **VSYNC** (`A408a`). In H40 EDCLK is MCLK/8 except around HSYNC, where the Mega Drive VDP mixes clocks (megadrive-python's `docs/timing.md`, "The line and the frame", from Jorge Nuno's oscilloscope work, t=519). Whatever the Mega Drive's H40 line does to EDCLK, the 32X's pixels do too. **What EDCLK does in H32 has no permitted source here.** The board assumes it keeps its H40 dots in H32 (420 dots of 8 MCLK a line, the slow run from HSYNC; `Quirks.edclk_h32`): a **HYPOTHESIS**, provisional. The reasons it was first taken (a reading of Nuked-MD's source, a probe built on it, a PicoDrive source comment) are out under the 2026-11-19 rules and are no longer evidence. What remains for it: the 32X's 420-dot line could not fit a 3,420-MCLK line at 10 MCLK a dot, and both detectors run here keep 320 32X pixels over the Mega Drive's 256 in H32 (OBSERVED, [validation.md](validation.md), "Milestone 4"). ## The line (Manual 3.3, "HBlank and Display Periods") All on the H40 line of 3,420 MCLK: | Interval | Dots | MCLK | | --- | --- | --- | | blank | 100 | 860 | | display | 320 | 2,560 | | HBLANK to VBLANK (on the last line) | 27 | 224 | | HBLANK to XHDISP | 3 | 24 | | HBLANK to PEN | 3 | 24 | | HBLANK to register latch | 76 | 668 | | FEN pulse (VDP-side DRAM refresh) | 40 SH-2 clocks | ~93 | Registers written in the window before the latch take effect on line n, after it on line n+1 (bitmap mode, PRI and screen shift are latched per line: "valid from the next line"). Dots in blanking are not 8 MCLK each (100 dots = 860 MCLK, and 76 dots = 668 MCLK): the Manual's figures follow the Mega Drive's EDCLK, slower around HSYNC, so converting a dot count to MCLK needs the Mega Drive's per-dot clock, not x 8. ### Where the board puts it (milestone 3) The Manual does not say where HBLANK falls in the Mega Drive's line, but it gives four spans from HBLANK in dots *and* in MCLK: PEN 3 dots = 24 MCLK, VBLANK 27 = 224, the latch 76 = 668, blank 100 = 860. Dots around HSYNC are slower than 8 MCLK, so the MCLK figures pin where the spans lie. Counted on megadrive-python's measured H40 line (socket 6's `pixels`), all four come out exactly for one place only: HBLANK rising **12 dots before the Mega Drive's HBlank flag**, H $15A (external $AD); 11 dots before gives 226 for the VBLANK span, 13 gives 222 (the scan is in [worklog.md](worklog.md), Session 4). The board uses it (`vdp.py`, `Quirks.hblank_phase`). In the Mega Drive's dots from there: | Edge | Dot after HBLANK | MCLK after | H40 H counter | Source | | --- | ---: | ---: | --- | --- | | HBLANK rises, FEN refresh pulse (40 Sclk = 93 MCLK) starts, HINT | 0 | 0 | $15A | Manual figure 3.17 | | PEN rises | 3 | 24 | $15D | figure 3.17, A | | VBLANK edge (on the frame's two lines) | 27 | 224 | $1D1 | figure 3.15, C | | register latch | 76 | 668 | $002 | figure 3.17, C | | HBLANK and PEN fall; the row's 320-dot display starts | 100 | 860 | $01A | figure 3.15, A | So the display runs H $01A-$159 of the Mega Drive's H counter. No hardware source places it; the place rests on the Manual's own figures counted on megadrive-python's line. The same place, counted from what the 32X actually sees on the slot: HBLANK rises **23 EDCLK dots (184 MCLK) before HSYNC's fall**, where EDCLK's slow run begins. Only there do 27 dots take 224 MCLK (8 slow EDCLK cycles in the span); the other three figures hold for any place within a few dots, so this one fixes it, on megadrive-python's pixel table. ### Where the 32X's picture sits against the Mega Drive's (Session 9) Figure 3.15's 100 blank dots are counted from HBLANK (the board) or from XHDISP, which rises and falls 3 dots after HBLANK; the figure draws A and B between XHDISP's edges. The board keeps HBLANK's fall for the row (XHDISP is not modelled; below). Session 9 placed both pictures with figures from Nuked-MD; that work read Nuked-MD's source, so its table and the alignments derived from it are struck (scrub of 2026-11-19). What stands: H40: one on one, which Sega's *32X H/W Information* (MAR-25, item 4, "Display dot distortion with MD") gives for production units: "Between 1/2 dot left to 1/2 dot right". Kabuto's oscilloscope note ("starting 24 pixels after the right edge (or 10 after the last rendered pixel)") puts pixel 0 76 dots after the slow run. H32: the board puts the 32X's pixel 0 26 MCLK (3.25 of its pixels) right of the Mega Drive's (`Quirks.composite_alignment`), ares's figure (OBSERVED: 13 quarter H40 pixels). **Provisional**: BlastEm's frames put the two pictures about 3.75 pixels apart (OBSERVED, closed box), the board's earlier 30 MCLK; no hardware source places it. The earlier value's derivation is struck (2026-11-19). Milestone 4's "5 pixels right" came from megadrive-python's socket 7: its `display_at + output_delay x pixel_mclk` (21 pixels in H40, 24 in H32) places where a CRAM write shows, not where pixel 0 leaves the RGB pins. XHDISP's fall would sit 3 dots after HBLANK's, and if XHDISP is the display window the 32X's pixels would leave later than the H/W Information's half dot allows; nothing here tells. **H32.** On the hypothesis above (EDCLK keeps its H40 dots), `vdp.py` places HBLANK 23 EDCLK dots before HSYNC, taken as 368 MCLK into the H32 line (`Quirks.edclk_h32`, HYPOTHESIS: the slow run as far after the H32 HBlank flag as the H40 line puts it after the H40 flag, 88 MCLK): HBLANK at 184 MCLK into the record, PEN 208, VBLANK's edges 408, the latch 852, the display 1,044. Before 2026-11-19 it was 374, from a source-read die-model probe (struck). Until Session 9 it counted the Mega Drive's H32 dots (10 MCLK), giving spans of 30, 270, 760 and 1,000 MCLK for the Manual's 24, 224, 668 and 860. ## The frame (Manual 3.3, "VBlank and Display Periods") 224 active lines on NTSC; 224 or 240 on PAL (the 240 bit). The 32X's VBLK follows the Mega Drive's VSYNC-derived blanking. The scan (PDF page 56, figure 3.16) gives blank periods of 38, 89 and 73 lines (NTSC, PAL 224, PAL 240); the transcription's `38h/89h/73h` is the scan's "38H", H meaning lines. The board counts rows from the Mega Drive's V counter: VBLANK rises 27 dots after the HBLANK that follows row 223 (or 239) and falls 27 dots after the one before row 0. Interlace is not supported by the 32X VDP ("non-interlace mode only"). ## Interrupts to the SH-2s | Source | Level | Raised by | Notes | | --- | --- | --- | --- | | VRES | 14 | the Mega Drive's reset button | no mask; testpico `t_32x_reset_btn` | | VINT | 12 | start of VBlank | testpico `t_32x_irq_vint` (2025) | | HINT | 10 | HBlank, every H count + 1 lines | HEN allows it inside VBlank; the H count register is reloaded when HBlank ends, so a new value takes effect one line late (Manual 3.2.2, two worked examples) | | CMD | 8 | 68000 writes INTM/INTS | cleared by the SH-2; differs from the others (Manual 3.2.2, point 1) | | PWM | 6 | PWM timer | every TM periods, TM = 0 means 16 | The board delivers a level per SH-2 (masks are per CPU, HEN is shared) and ORs the FRT's FTOA output into bit 0 of /IRL (32XDK wiki "FRT Bumping"; Supplement 2; Bulletins #10, #27). An interrupt is held until cleared. The SH-2 "2.5 cut" can take the wrong vector when two levels arrive within 3 cycles (Toshiyasu Morita's FAQ, in the wiki): a **core** question for sh2-python, with this board supplying the IRL timing. ## Bus waits (Manual 4.1, "Access Timing of each CPU to 32X Block") | Target | SH-2 | 68000 | | --- | --- | --- | | cartridge (32X mode) | 6-15 waits | 0-5 waits | | frame buffer read | 5-12 | 2-4 | | frame buffer write | 1-3 (4-word FIFO: 3 clocks, 5 when full) | 0 | | palette | 5 to 64 us (a line) | read 2, write 3, to 64 us | | VDP register | 5 | read 2, write 0 | | system register | 1 | 0 | | boot ROM | 1 | - | | SDRAM | read 12 clocks per 8-word burst (cache-through reads also burst), write 2 per word | - | "Wait" is a bus cycle plus one clock of that CPU (68000 minimum 4 clocks, SH-2 minimum 2). Ranges are the design's; where the cycle lands inside them depends on contention: the SH-2 has priority on the cartridge (Manual 3.4, "ROM Access Competition"); the master owns the SH-2 bus and the slave asks (Manual 2.2); a CPU touching the palette outside blanking waits for the next blank. Auto fill costs 7 + 3 x length VDP cycles and blocks SH-2 DRAM access (FEN = 1). The 68000 waits through the 32X's own DTACK; the board must express that through megadrive-python's `board.stall` (socket 3) at the 68000 clock the access lands on. ### What the board models (milestone 5b) These costs rest on documents (the Manual, Hitachi's SH7604 hardware manual "HM") and are checked against one published hardware result, the 32xspd benchmark's photograph ([validation.md](validation.md), "Milestone 5b"). No 32X has been measured here. - **A cycle is 2 + n states for the Manual's n waits** (its own definition). The boot ROMs leave the BSC at BCR1 $0001, BCR2 $00A8, WCR $0055, MCR $0AB8: CS0-CS2 16-bit ordinary space with one programmed wait each, CS3 16-bit SDRAM, CAS latency 2, bank-active, with refresh. WAIT is sampled only as the last programmed Tw ends (HM 7.4.2), so the programmed wait and the 32X's own overlap: the board drives `n - 1` (`sh2_wait_overlap`). System register and boot ROM: 3 states; VDP register and palette: 7 (palette then held to PEN); cartridge: 8; frame-buffer read: 7 at least. - **CS0 is 16 bits** (`sh2_area0_width`), so a longword from the boot ROM or the registers is two cycles. - **The frame-buffer write FIFO** (`sh2_framebuffer_fifo`): 4 words, one leaving every 5 states; a write is the BSC's 3 states with room, else waits for the oldest word: 3 per word for the first four, 5 per word once full (Manual 4.1), shorter after idle time. A read waits for the word leaving now, then takes its 7; the FIFO's next word leaves after the read. `$20004006` FULL/EMPTY read the FIFO. Sega's *32X H/W Information* ("32X I/F Implementation Time", Target Ver. 2.0) lists the same shape: 1st-4th write 3 clocks, 5th and after 5. The FIFO drains whatever else the bus is doing (a cartridge fill overlaps it); the 32xspd photograph's copy loops say that costs something the board does not model, about 9 states an iteration with a cartridge fill, 5 with an SDRAM fill, and no document says what (validation.md, "The ROM-VRAM copy, investigated"). - **One SH-2 bus** (`sh2_bus_arbitration`, HM 7.10): a cycle waits for the other chip's access under way (an access's two port cycles and a line fill's 8 cycles are one and never split); the slave asks for the bus before each access (2 states); the master takes it back 1 state after a slave access ends. - **The cartridge between an SH-2 and the 68000** (`cartridge_contention`, Manual 3.4): whoever arrives second waits for the first's access to end; the 68000 through `Board.hold`, its own access taken as 4 68000 clocks. - **SDRAM is the core's** (`sh2_sdram`): WAIT is not sampled for synchronous DRAM (HM 7.2.3), so the cycle's shape is sh2-python's bus state controller's. Since pin 8755c3a (sh2-python PR #1, from [sh2-python-sdram-pr.md](sh2-python-sdram-pr.md)) it is HM 7.5's: with the boot ROMs' registers a line fill is 10 states (same row), 11 (bank idle) or 12 (other row); a cache-through or DMA read makes the same burst; a word write 1, 2 or 3 by row state; a refresh of 7 states every 356, master only. Before (pin d55bbb0) every SDRAM cycle was an ordinary 3-state one: a line fill 24. ## PWM Period = cycle register x Scyc per the Manual; ares runs `cycle - 1` clocks per sample and treats cycle = 1 as "PWM will no longer operate"; run here, PicoDrive's tones fit `cycle - 1` and BlastEm's `cycle - 2` (OBSERVED, [validation.md](validation.md), "Milestone 5"). Each channel's FIFO is 3 deep; writing when full drops the oldest (Manual). With both channels off the cycle counter stops and FULL sticks. Contested: see [undocumented-behavior.md](undocumented-behavior.md). The board (`pwm.py`): the counter in SH-2 states, lazily, brought up to each register access and, through `SH2Side.catch_up`'s loop, to each period's end (`PWM.next`), so the interrupt and DREQ1 fall on the period's end give or take the instruction in flight. NTSC: cycle 1047 gives 21,999.4 Hz, cycle 521 44,252.6 Hz. ## What megadrive-python's timing gives this board The Mega Drive side's line, counters, HBLANK/VBLANK positions and output delay are measured there (megadrive-python `docs/timing.md`: HV captures, Nemesis's logic analyser, the item-20 output delay of 21/24 pixels). This board needs them as **events** (HSYNC and VSYNC edges at MCLK precision, EDCLK's per-dot length, the YS of each pixel), not only as a finished line: see [handoff-brief.md](handoff-brief.md), "Sockets".