# Timing: the clock, the scanline, and when things happen This is the part of the board that game code notices most. Split-screen effects, raster colour changes, the light phaser, and many games' 50/60 Hz detection all depend on it. FluBBa's VDP test checks it to the H-counter unit. Every number names its source; the tier of each source is in [validation.md](validation.md). ## The units - **CPU cycle**: 1/3,579,545 s (NTSC) or 1/3,546,893 s (PAL). - **Dot** (pixel clock): 2/3 of a CPU cycle. **342 dots per line = 228 CPU cycles.** - **H counter**: a 9-bit dot counter. Port $7F returns its upper 8 bits, and only as latched on a TH edge (`msvdp-20021112.txt` section 11). FluBBa counts the line as HClock −47 … 294 and gives HCount = HClock ÷ 2, or equivalently (MClock − 94) ÷ 4 with MClock 0 … 683 (`VDPTest-ReadMe.txt`). - **V counter**: an 8-bit line counter that jumps back partway through the frame so it fits in 8 bits. Port $7E returns it at any time. ## Frame layout and V counter values From `msvdp-20021112.txt` section 11. Charles MacDonald: "now I've checked the results against a NTSC SMS 2 fitted with a PAL/NTSC switch". The V counter counts up linearly, and "a, b" means it jumps from a to b on the next line. | Mode | Active | Bottom border | Bottom blank | V sync | Top blank | Top border | V counter sequence | | --- | ---: | ---: | ---: | ---: | ---: | ---: | --- | | NTSC 256×192 | 192 | 24 | 3 | 3 | 13 | 27 | $00–$DA, $D5–$FF | | NTSC 256×224 | 224 | 8 | 3 | 3 | 13 | 11 | $00–$EA, $E5–$FF | | NTSC 256×240 | does not work: no border, blanking or retrace; the picture rolls | | | | | | $00–$FF, $00–$06 | | PAL 256×192 | 192 | 48 | 3 | 3 | 13 | 54 | $00–$F2, $BA–$FF | | PAL 256×224 | 224 | 32 | 3 | 3 | 13 | 38 | $00–$FF, $00–$02, $CA–$FF | | PAL 256×240 | 240 | 24 | 3 | 3 | 13 | 30 | $00–$FF, $00–$0A, $D2–$FF | FluBBa's test "VCounter values" checks every value for NTSC and PAL 192-line displays. The TMS9918A has no readable V counter; its frame is 262 lines at NTSC (TI data manual section 3; the TMS9929A is 313 lines). ## Horizontal layout `msvdp-20021112.txt` section 11. Charles MacDonald notes these came from the TMS9918 manual and an NTSC Genesis, "a real SMS may have different results": | Dots | H counter | Area | | ---: | --- | --- | | 256 | $00–$7F | Active display | | 15 | $80–$87 | Right border | | 8 | $87–$8B | Right blanking | | 26 | $8B–$ED | Horizontal sync | | 2 | $ED–$EE | Left blanking | | 14 | $EE–$F5 | Colour burst | | 8 | $F5–$F9 | Left blanking | | 13 | $F9–$FF | Left border | As read, the H counter is not continuous: it runs $00 … $93, then $E9 … $FF. That is FluBBa's HClock −47 … 294 halved, and MAME's `sega315_5124_device::hcount()` (`((hpos − 1 − 46) >> 1) & $FF`, with a comment that the value "seems to be based on the previous hpos") gives the same sequence. "HCounter correct" in FluBBa's test checks every value; take the mapping from there, not from this table. ## When events happen within a line This is the information Charles MacDonald did not have in 2002: "Currently, I don't have information about where events occur within a single scanline." FluBBa measured it on a PAL SMS1 (50 and 60 Hz) and a PAL SMS2, and on a PAL Mega Drive with a Power Base Converter (`VDPTest-ReadMe.txt`, forum thread 10695). His readme lists each event as an HClock and an HCount, and the two columns disagree by a few dots ("HClock -16 (HCount 0xF4)", but -16 / 2 is $F8). The HCounts are what his tests measure, so the board is fitted to *the tests*, which all pass on the SMS1 and SMS2 variants (docs/validation.md, 2026-09-21): | Event | FluBBa's readme (SMS) | The board (MClock; line-local m) | Fitted to | | --- | --- | --- | --- | | H counter | (MClock - 94) / 4 | (MClock - 93) / 4 | his 228-value table, "HCounter correct" | | V counter changes | HCount $F4 | MClock 47; m = 0 | "VCounter chg time" | | Frame IRQ asserted | HCount $F3 | MClock 46; m = 683 of the previous line | "Frame IRQ HCount" | | Line IRQ asserted | HCount $F3, one dot after VINT | MClock 48; m = 1 (one dot after VINT) | "Line IRQ HCount", "Line IRQ VCount" | | VINT and OVR flags visible | HCount $F5 | at the V counter change | "VINT flag HCount", "Spr ovr correct HC" | | COL flag | "at the pixel"; x = $83 reads HCount $47 | m = 2x + 70 | "Spr col correct HC" | | X scroll latched | between HClock -18 and -17 | per line, at line start | "Testing X-Scroll latchtime" (visual) | | Y scroll latched | not determined | at the end of the active display (msvdp) | none | Two CPU-side facts the fit depends on, both from Zilog UM0080: an I/O instruction's access lands in T3 of its I/O machine cycle (10 T-states into `IN A,(n)`/`OUT (n),A`, 11 into `IN r,(C)`/`OUT (C),r`, 12 into `INI`, 15 into `OUTI`; `sms_python.system.IO_ACCESS_T`), and INT is sampled on the rising edge of an instruction's last clock, so a line rising in the final T-state is taken one instruction later. The IRQ HCount tests end a `HALT` with the interrupt; HALT's 4-T-state NOPs quantize the acceptance, which is what makes these tests sensitive to single mclocks. ## Interrupt lines `msvdp-20021112.txt` section 12: - **Frame interrupt**: Charles MacDonald gives line $C1 (192-line), $E1 (224) and $F1 (240); MAME's `315_5124.cpp` says "VINT fires at the end of the first logical line of the bottom border". Both describe the end of line $C0, just before the V counter turns $C1; the board raises it at m = 683 of line 192 and shows the flag at the V counter change (table above). - **Line interrupt**: a down-counter is reloaded from register 10 on every line *outside* the active display, except the line immediately after it, and decremented on every active line and on that following line. When it underflows from $00 to $FF it is reloaded and sets the line-interrupt pending flag (msvdp section 12). The board steps it as each line begins (m = 1), for that line, so with register 10 = N the handler reads V counter N, as FluBBa's "Line IRQ VCount" requires. Writing register 10 takes effect only at the next reload. - **IRQ output** = (frame pending AND reg1 bit 5) OR (line pending AND reg0 bit 4), re-evaluated whenever the enables change: turning an enable on with the flag already set raises INT at once. Reading the control port clears both pending flags and the IRQ. ## Sub-instruction timing z80-python's `step()` returns an instruction's total T-states, not when each access happened. The board places the port accesses itself (option 1 of the original plan, no z80-python change): `System.step` looks up the opcode's access T-state before the step and advances the VDP and PSG to it when the port callback fires, then by the rest of the instruction afterwards. Memory accesses are not placed; nothing on these boards is sensitive to them. MAME's trace does not settle any of this: it stamps each I/O event with the `totalcycles` of the *instruction start* ([mame-oracle.md](mame-oracle.md)), and its interrupt acceptance wanders by about ten cycles. FluBBa's hardware-derived expected values are the judge. ## SG-1000 specifics - The TMS9918A's interrupt is only the frame interrupt (status bit 7, enabled by register 1 bit 5), set at the end of the active display. There is no line interrupt, no readable V or H counter, and no Mode 4. - The PSG's READY line can hold the Z80 in wait states during a write: "The SN76489AN requires approximately 32 clock cycles to load the data into the control register. The open collector READY output is used to synchronize the microprocessor to this transfer" (`reference/manuals/TI_SN76489_datasheet_console5.pdf`). MAME 0.285 does not connect it (`sg1000.cpp` TODO), so MAME lockstep on the SG-1000 will diverge in cycle counts after PSG writes if the board models it. The board does not model it: no cycle-exact SG-1000 hardware test is pinned to judge it by ([undocumented-behavior.md](undocumented-behavior.md), open item 4). - VRAM access windows: the TMS9918A needs a gap between CPU VRAM accesses during active display: "the time it takes to transfer data to or from VRAM memory can vary from 2 to 8 microseconds" (TI data manual section 2, table 2-2, "Memory access delay times"). A write that comes too early is lost on real hardware. Games that ignore this are rare. Model it only when a test shows it matters. ## Mega Drive specifics A Mega Drive with a Power Base Converter runs the same frame (228 cycles a line, 262 or 313 lines) with its own VDP, the 315-5313, in Mode 4. FluBBa developed his test on a PAL Mega Drive with the converter as well, and his ROM has a second menu entry with that machine's expected values; his readme gives its event times beside the SMS's ("-14 (Hcount 0xF6) on MD"). The board's machine `megadrive-pbc` is fitted to that entry the way the SMS was fitted to the first: | | SMS | Mega Drive | Fitted to (Mega Drive entry) | | --- | --- | --- | --- | | V counter change (MClock) | 47 (HCount $F4) | 56 (HCount $F6) | "VCounter chg time"; 53 fails it | | Frame IRQ (m) | 683 | 681 (680-683 pass) | "Frame IRQ HCount" | | Line IRQ (m) | 1 | 1 | "Line IRQ HCount" | | VINT and OVR flags visible | at the V change | 3 mclocks before it (-2 to +3 fail) | "VINT flag HCount", "Spr ovr correct HC" | | COL flag | at the pixel, m = 2x + 70 | at one point of the line whatever the pixel: m = 17 (16-18 pass; 15 and 19 fail) | "Spr col correct HC" | The collision flag is the one that differs in kind: the readme's "COL flag is set at the pixel it occurs. -13 (HCount 0xFF) on MD". His test reads the status twice, one CPU cycle apart, as the line with the two sprites begins, and wants the flag in the second read only; so the board looks for a collision on a line as that line starts, before its pixels go out. The H counter table and the V counter values are the SMS's (the entry's "HCounter correct" and "VCounter values" pass unchanged). Result: 55 of 55 on `megadrive-pbc` and `megadrive-pbc-pal`. What the port rules are is in [undocumented-behavior.md](undocumented-behavior.md). ## Game Gear specifics The GG VDP runs the SMS frame: 228 cycles per line, 262 lines, NTSC only. It shows active lines 24-167 and columns 48-207 of the 256x192 picture (`msvdp-20021112.txt` section 14; `GGVDP.pmwiki`). Its timing within the line is not the SMS's. In 2019 "enik2" measured a 2-ASIC and a 1-ASIC Game Gear (forum thread 10695), found they differ from the SMS and from each other, contributed the timings to MAME 0.200, and published `VDPTEST_GG.sms` with expected values per model. It runs in the Game Gear's SMS-compatibility mode, the only mode in which TH (and so the H counter latch) can be driven. He also found that FluBBa's "HClock" figures are Z80 cycles relative to HCount $00 ("ZClock"), which explains why they looked inconsistent with the HCounts (above). | | SMS | GG 2 ASIC | GG 1 ASIC | | --- | --- | --- | --- | | V counter change (MClock) | 47 | 56 (3 Z80 cycles later, enik2) | 53 (2 cycles later, enik2) | | Frame IRQ (m) | 683 | 680 (fitted; 677-680 pass) | 680 (best found) | | Flags visible | at the V change | 3 mclocks before it (fitted) | at the V change | | COL flag (MClock) | 2x + 117 | 2x + 123 (HCount $49 for x = $83) | 2x + 123 | | `VDPTEST_GG` result | — | 55 of 55 | 53 of 55: Frame and Line IRQ HCount open | The 1-ASIC unit's IRQ tests give the *same* value in both passes (enik2: $83, $83 and $89, $89), which no single IRQ position in this model reproduces. enik2 also saw the 1-ASIC H-counter pattern change across power cycles; both point at a CPU/VDP sub-cycle phase that differs by model or by power-on, which the board fixes. See [undocumented-behavior.md](undocumented-behavior.md), open item 8.