# The instruction cache, the bus and timing Keys as in [start-here.md](start-here.md). ## The instruction cache (DOC: ARCH 2.2.7, ch. 7; STS "Instruction Cache", "CHCW") - 1 KB, **direct-mapped, 128 entries x 8-byte blocks**; address bits 31-10 tag, 9-3 index, 2-0 offset. **Each block has two 4-byte sub-blocks with a valid bit each** (ARCH fig. 7-1: "Valid bit (1 bit for each 4 bytes)"). STS draws one valid bit per entry: C1. A sub-block valid bit means a miss can fill 4 bytes, not 8; which the CPU does on a miss (fill the missed sub-block only, or both) is not stated (U1). - **Enable:** CHCW.ICE plus the ICHEEN pin (ARCH 2.2.7 note 2). The Virtual Boy and PC-FX boards must say whether ICHEEN is tied active (U2; the BIOS and games set ICE, so the working assumption is yes, HYPOTHESIS). - **The cache becomes valid "when an instruction next to the LDSR instruction has been fetched"** (ARCH 2.2.7): the instruction after the LDSR that enables it is fetched uncached. - **Clear:** ICC with CEN (start entry) and CEC (count, clamped to 128; nothing if CEN >= 128; stops at entry 127). - **Dump/restore:** ICD/ICR with SA (the high 24 bits of a 256-byte aligned address): 128 data blocks then 128 tag words, 1,536 bytes (STS gives the tag word's layout: 22-bit tag, a valid field, a 5-bit NEC-reserved field). With two valid bits per entry (ARCH) the tag word's valid field must hold both: how they sit in the word is U3. - **Interrupts during clear/dump/restore** are held until the operation ends (a maskable one only when NP, EP and ID are all 0; ARCH 2.2.7 note 1). - **No snooping:** stores do not update the cache (no write path is described; ARCH ch. 7 shows only fills). Self-modifying code must clear the cache; a stale line is observable, so the cache belongs in the specification, not in an optimisation. - **Bus status shows fetches:** ST1/ST0 with MRQ say "instruction fetch", "fetch after branch" (not output when the cache is on), "data access", "I/O access" (DOC: HW 2.3 (5)). The bus contract's `kind` comes straight from this table. ## The bus (DOC: HW ch. 3) - 32-bit mode (dynamic bus sizing by SZRQ per access) or 16-bit fixed mode (SIZ16B and SZRQ tied at reset). A word on a 16-bit bus is two bus cycles, low halfword first. - A bus cycle is T1 + T2 (two clocks) plus wait states while READY is inactive, sampled in T2; additional cycles for the second half of a split word are T1S/T2S. - **Byte enables and A1** in 16-bit mode follow HW table 3-3; the board decodes them. The Virtual Boy's wait controller adds 1 or 2 waits to game pak ROM and expansion accesses (DOC: STS "Wait Controller"); the PC-FX's memory and chips have their own waits (unknown: pcfx-python U-list). ## Instruction timing (DOC: ARCH 5.4) ARCH table 5-11 gives **minimum clocks "with cache hit, no hazard, and no wait"**: - 1 clock: MOV, ADD, SUB, CMP, SHL, SHR, SAR, OR, AND, XOR, NOT (register and imm5 forms), SETF, MOVEA, ADDI, ORI, ANDI, XORI, MOVHI. - MUL 13, MULU 13, DIV 38, DIVU 36. - TRAP 15, RETI 10, CAXI 22 (32-bit bus) / 26 (16-bit bus). - JMP, JR, JAL 3; Bcond taken 3, not taken 1. - LD.B/H 1-3; LD.W 1-3 on a 32-bit bus, 1-5 on a 16-bit bus: **3 (5) when alone, 2 (4) after another load, 1 after a long instruction with no conflict** (notes 1-2). - ST.x, OUT.x 1, but 2 (4 for a word on a 16-bit bus) "after two or more consecutive executions of the same instruction" (note 3): a store buffer one deep, HYPOTHESIS. - IN.x 3 (IN.W 5 on a 16-bit bus). - FPU: the ranges in [fpu-and-bitstrings.md](fpu-and-bitstrings.md). - Not in table 5-11: LDSR, STSR, HALT, and the NVC's additions (STS gives CLI/SEI 12, MPYHW 9, REV 22, XB 6, XH 1; their source is not stated: U13). **What is missing for cycle timing** (and why this core is modelled at instruction level with bus transactions, charter axis B): - no stage diagram: when an instruction's operand fetch, execute and write-back happen, so how a load's result interlocks with the next instruction (the 3/2/1 rule above is the only statement), U14; - how a cache miss's fill overlaps execution, U15; - how wait states from the board add to the table's minimum (the table is "no wait"), U16; - the FPU and DIV data-dependent ranges, U10. So the timing model is: **table 5-11's minimum, plus the load-pairing rule, plus the store rule, plus the bus cycles each access makes (two clocks plus the board's waits per 16-bit transfer), plus a fill on each cache miss.** Each element is tagged DOC where it is the table and FITTED where it is our arithmetic, until a hardware timing probe (the Virtual Boy's timer counts 20 µs ticks; a probe would loop a code sequence N times and read it) settles it. We have no Virtual Boy or PC-FX hardware (family rule: emulation only), so these probes are queued for whoever does, never run here. ## Proposed modelling level Instruction level with bus transactions (as sh2-python and r3000a-python): each instruction makes its fetches (or cache hits) and data accesses through the bus contract in order, and is charged a clock count built as above. A stage-level model would claim more than the evidence supports.