# Timing: the clock, what costs what, what the program uses **No timing of the HG51B169 has been measured on hardware** that anyone has published (fullsnes "CX4 Timings (Unknown)": "All opcode & DMA timings are 100% unknown"). Everything here is research tier (the crystal on the PCB), emulator tier (byuu's model as published in ares, ISC) or OBSERVED (Mesen2 run as a closed box: `scripts/cx4_probe.py`, the rung 4 recordings). The core's contract is therefore **counts in chip clocks** that the board converts, each count named so a later measurement can change it. ## The clock - **Crystal**: 20.000 MHz, "[M]20000C" or "20.0MC/TDKY" (fullsnes "External Oscillators" l.22773; PCB lists: X1 20MHz on SHVC-1DC0N-01 and SHVC-2DC0N-01) [research, from boards]. - **Internal divider**: unknown (fullsnes: "this might be internally divided"). ares (`sfc/cartridge/load.cpp` l.337: 20,000,000) runs **one chip clock per 20 MHz cycle** [emulator]; the rung 4 recordings line up when Mesen2's stamps are converted at 20 MHz over the master clock [OBSERVED]. - **Ratio to the SNES master clock**: NTSC master = 236.25/11 MHz, so one master clock is **176/189 chip clocks** exactly (20 x 11 / 236.25). PAL master 21.28137 MHz: 2,000,000/2,128,137. The board accumulates master clocks and runs the chip `owed * 176 // 189` clocks, as snes-python's `necdsp.py` does for the uPD7725 (`advance`, exact `Fraction`). ## What costs what, in chip clocks (ares `hg51b.cpp`, `instructions.cpp`; Mesen2 OBSERVED) | Event | ares (the defaults) | Mesen2, closed box (OBSERVED) | Notes | | --- | --- | --- | --- | | any instruction | 1 | 1 (every probe trace's `CC` steps by 1) | | | taken JMP/JSR (any condition), RTS | +2 | +2 (`far`, `stack` traces) | | | taken SKIP | +1 | not probed | | | cache page load | 512 bytes x (1 + ROM wait) | busy for a start's load in every host stream | with the games' $7F50 = 44h: 512 x 5 = **2,560 clocks** | | DMA, per byte | wait(source) + wait(target): ROM/SRAM 1 + wait, **anything else 0** | ROM 1 + wait, data RAM **1** (`dma-cost-w0`, `-w7`) | C8; 2 KiB ROM to data RAM: 10,240 (ares) or 12,288 (observed) clocks | | bus read via operand 2Eh | completes after **ROM wait** clocks | WAIT lasts **1 + ROM wait** (`bus`) | C7; WAIT stalls the rest | | suspend ($7F55-$7F5C) | 0 (indefinite), 32, 64, ... 224 | not probed | | | power-on wait states | ROM 3, RAM 3 | not probed | the games write 44h first thing | Reading or writing $7F5E-type status and the S-CPU's register-file accesses cost the chip nothing; the S-CPU's own cycle cost of the $6000-$7FFF window is the board's (fullsnes header: "Slow LoROM", $FFD5 = 20h; snes-python `cartridge.md` access-cycle rules apply). ## What the S-CPU can do while the chip runs - **Executing from cache**: the S-CPU's ROM reads work (ares `hitachidsp/memory.cpp` l.42-52: blocked only when `busy()` = cache load, DMA or a pending bus access). The chip and the S-CPU share the ROM bus only when the chip uses it. - **Cache load, DMA, bus access in flight**: the chip owns the ROM bus; the S-CPU reads open bus from ROM, except $00:FFE0-$FFFF (ares: ROM offset 7FC0h-7FFFh in the LoROM mapping) which read the I/O block's $7F60-$7F7F vectors, so an NMI or IRQ taken during a load still finds its vector (fullsnes "Exception Vectors": "would be REQUIRED to be same as the ROM vectors"). The games write $7F6A/$7F6B (NMI) = 1FEFh and $7F6E/$7F6F (IRQ) = 1FF3h, the ROM's own vectors (MAME host log, 2026-11-06). Whether Mesen2 blocks these reads was read from a comment in its source under the old rule (it said the blocking was not implemented); closed box from now on, never a default: C12. - The games poll $7F5E bit 6 (running) in a loop: MMX2's test makes 3,942 status reads in 200 frames on MAME (HLE, so the count is MAME's timing, not the chip's). ## The board's synchronisation As with the uPD7725 and the SA-1: before every S-CPU access to $6000-$7FFF or to ROM, the board runs the chip up to the access's master time; the access then falls between two chip clocks. A start write ($7F4F, $7F47, $7F48) takes effect at that point. ares synchronises the CPU after every chip step (`hitachidsp/hitachidsp.cpp` l.22-26). Instruction-level timing is the core's; which master clock an access lands on is the board's. ## What the program uses (`scripts/survey_program.py`, 2026-11-06) All USA/Europe/Japan X2 dumps carry one program (SHA-1 of the 8 KiB region `44e862b8...`); all X3 dumps carry it with two words changed (0008:3B `BC28`->`BC08`, 000A:C4 `6428`->`6408`: the wireframe data bank, fullsnes); the two Rockman X2 betas carry an earlier program (`483f9afe...`, 1,028 words differ). Of the 4,096 words, 1,081 are HALT (FFFFh fill) and 85 are 0000h. | Used | Count | Not used | | --- | --- | --- | | LD A,reg / ST reg,A | 614 / 582 | XNOR, CLEAR, JMP/JSR VS, RDROM imm, ST reg,MDR (`E1`), LD P,Rn | | far jumps and calls (P -> PB) | 29 | immediate shifts >= 24 | | MUL reg / MUL imm | 77 / 1 | MH/ML read by the instruction right after a MUL (0 cases: C6) | | LD MDR, BUS(ROM) + INC MAR + WAIT | 16 each (fullsnes' idiom) | bus port 2Fh, writes to the bus port | | shifted ADD (A<<8: 105, A<<16: 2, A<<1: 1) | 108 | flags of a shifted ADD are never read before being overwritten | | shifted CMP/CMPR A<<8, then JMP GE | 3 (0004:5D, 0004:60, 0004:63) | **C2 matters here** if A >= 10000h at those points | | shifts by a register (SHR/SHL reg) | 11 | (C1 matters if the count reaches 24) | | SXB / SXW | 3 / 11 | | | RDRAM / WRRAM, A and DPR+imm | 106 / 105 | byte lane 3 | ## Measured on the oracles (2026-11-06) - **Mesen2, Mega Man X2 (USA), power-on to frame 900, no input**: 361,854 Cx4 instructions, all on page 0 (`build_oam`, 340,150) and page 0Eh (the test routines the game runs at boot, 21,704). - **Mesen2, the Cx4 test (controller 2 B held)**: 18.9 M instructions in 240 frames; 393,195 in frames 120-121 (about 197,000 a frame, out of 333,000 chip clocks a frame at 20 MHz). A 2-frame trace is 2.7 MB gzipped. - **MAME HLE, the Cx4 test**: 768 DMAs of 2 KiB (the whole 1.5 MB ROM, 008000h to 2FF800h in LoROM steps, into data RAM at 006000h), each followed by a checksum command (0E:40); 802 cache preloads alternating pages 0 and 1 of PB 000Eh; sixteen 0E:00-0E:3C calls (set R0 to 0-15); sixteen 0E:54 (square); one 0E:5C and one 0E:89; reads and writes of every register-file byte.