# Timing ## What NEC states (tier 2) PUM Appendix A, Table A-1 (11 pages) gives one clock count per instruction form under seven conditions (PUM p. 73): decoding finished; no wait states; no bus hold; word data at even addresses; the registers an effective address uses not written by the previous instruction; one register in the effective address; branch targets at even addresses. The notes give the corrections: | Condition broken | Correction (PUM p. 73 notes) | | --- | --- | | wait states | add the wait clocks | | word at an odd address | +1 | | EA register written by the previous instruction | +1 (not for LDEA, nor for consecutive PUSH/POP, which stay at 1 each) | | two registers in the EA (`[BW+IX]`) | +1 | | branch to an odd address | +1 | Representative rows: ADD reg,reg 1; ADD reg,mem 2; ADD mem,reg 3; MOV reg,mem 1; PUSH/POP reg16 1; MUL/MULU reg 3, mem 4; DIVU reg8 15, reg16 23; DIV reg8 17, reg16 24; Bcc 1 not taken, 4 taken; BR short/near 4; BR far 7; CALL near 5, far 10; BRK 3 9; BRK imm8 10; IN/OUT 6; HALT 9; ADJ4A/ADJ4S 10, ADJBA/ADJBS 9. "Decoding finished" is the hole: the table assumes the prefetch queue is ahead. After a branch the queue refills from the target over the bus, and the cartridge bus may be 8-bit and add a wait state (port A0 bits 2-3), so real code pays more than the table on branches. NEC gives no refill model. ## What hardware tests say (tier 3) WSTIME (Robert Peip, then FluBBa; run on a pink WonderSwan Color, README) reports, per opcode, the scanlines a 1,000-iteration loop takes; "clock cycles x 4 almost should be the test result". Its README warns that the numbers are whole-loop figures, that branches flush the pipeline, that branching to an odd address costs 1 more, that the cost seems to depend on the destination instruction, and that the loop itself costs 1 cycle more than expected. Its expected values are **not** written down anywhere in its sources: they are read off the screen of a real console. With no WonderSwan here, WSTIME can only compare two emulators, unless its author's screenshots or numbers are published (U2). ## What MAME does (tier 5) The 1-frame boot-ROM trace (mame-oracle.md) gives MAME 0.285's per-instruction charge as the difference of `totalcycles` between lines: MOV 1 (2,307 times) or 2; PUSH and POP 1; OUT and IN 6; far BR 7; DBNZ 5 taken, 2 not; CALL far 10 or 11 (11 at the odd target 0847h: PUM's odd-branch note); **conditional branches taken 10 or 11** (262 + 17 times); not taken 1. One frame of 40,703 clocks (WSDEV: 159 lines x 256 = 40,704). ## Contradictions - **C1. A taken conditional branch.** PUM: 4 clocks. MAME 0.285: 10-11 in the boot ROM. The boot ROM sits in the SoC (WSDEV "Boot ROM": FF000h-FFFFFh mono, FE000h-FFFFFh colour), so cartridge wait states do not explain it; MAME may charge a queue refill. A hardware number from WSTIME (taken JZ in its table) would decide; until then the core follows PUM and tags the refill penalty HYPOTHESIS (U1). - **C2. Where the extra branch cost lives.** WSTIME says the cost after a branch depends on the destination instruction; PUM's table has no such term. A prefetch-queue model is what both point at; this is the core's milestone M5. ## Interrupt latency PUM 1.3: the V30MZ may take a hardware interrupt "after performing a larger number of instructions than the V30HL", except around I/O accesses; 5.3 lists where it is never taken. The WonderSwan's interrupt controller asserts INT while any enabled request is latched (WSHWTest README, WSDEV "Interrupts"). How many clocks the acknowledge takes is a TODO on WSDEV ("Document how many cycles interrupt processing takes"): U3.