# Timing: pipeline, interlocks, caches, write buffer, SysAD Sources: UM chapter 4 (pipeline), 11 (caches), 12 (system interface), Table 3-12 (multiply/divide), Table 7-14 (FPU). Tier 2 unless marked. TEST ROM lines are nemu64-test's expectations (tier 3). ## The pipeline (UM 4.1, p.90-93) Five stages, one PCycle each, each PCycle split into phases F1/F2: | Stage | Work begun there (UM Table 4-1) | | --- | --- | | IC | instruction cache fetch; micro-TLB read | | RF | I-cache tag check; register file read; decode; instruction virtual address (branches); branch compare | | EX | ALU; data virtual address | | DC | data cache read; joint-TLB read (data); load alignment; data cache tag check | | WB | data cache write; register file write | - Branch delay: one slot (UM 4.2); the target's IC follows the delay slot's. - Load delay: the next instruction may use the loaded register; a 1-cycle **LDI** interlock is inserted (UM 4.3, 4.6.5), imprecisely detected on the N64 (start-here.md). - Store then load: a store keeps the D-cache busy in DC and WB; an immediately following D-cache access stalls one PCycle (UM p.61, "Store Delay Slot"; 4.6.7 DCB). ## Interlocks (UM 4.6, p.106-113) | Interlock | Stage | Cost (UM) | | --- | --- | --- | | ITM, instruction micro-TLB miss | RF | 3 PCycles to refill from the JTLB | | ICB, I-cache busy (miss) | RF | until the whole 32-byte line is in the cache (bus-dependent: board) | | MCI, multicycle instruction | EX | the instruction's latency (MULT 5, DIV 37, DMULT 8, DDIV 69 PCycles; FPU Table 7-14) | | LDI, load use | EX | 1, plus the DCB time on a miss, plus 1 | | DCM, D-cache miss | DC | 1 for the detection, then DCB | | DCB, D-cache busy | DC | store-then-access 1; a miss until the 16-byte line is read (board) | | COp, CACHE instruction | DC | an I-cache op until done; some D-cache ops 2 PCycles | | CP0I, CP0 bypass | DC | 1 (an exception's CP0 write in WB against a CP0 read in DC) | Priorities when several arise: UM 4.7.4-4.7.8 (a DC stall resolves before an RF stall; EX may complete a multicycle op unless a load dependency exists). **The integer multiply/divide stalls the whole pipeline**, not only a later MFHI/MFLO: UM p.76 ("the VR4300 stalls the entire pipeline"), and TEST ROM: nemu64-test `timing` "MULT; MFLO" 6 cycles, "MULT; 1 NOP; MFLO" 7, "MULT; 4 NOPs; MFLO" 10 (each NOP adds one: the stall is not hidden by independent work); DIV; MFLO 38; DDIV; MFLO 70. This differs from the R4000 (whose HI/LO interlock only stalls a reader), so an R4000-based mental model gives the wrong cycles (C4). ## Caches (UM chapter 11, p.274-285) - I-cache: 16 KB, direct mapped, 512 lines of 32 bytes, index VA[13:5], physical tag PA[31:12] + valid. - D-cache: 8 KB, direct mapped, write-back, 512 lines of 16 bytes, index VA[12:4], tag PA[31:12] + valid + dirty. - Cache attribute from the TLB entry's C field, or Config.K0 for kseg0; kseg1 uncached. - CACHE instruction (UM p.402-405): op[1:0] selects I or D; op[4:2]: Index_Invalidate (I) / Index_Write_Back_Invalidate (D), Index_Load_Tag, Index_Store_Tag, Create_Dirty_Exclusive (D), Hit_Invalidate, Hit_Write_Back_Invalidate (D) / Fill (I), Hit_Write_Back (D: if dirty; I: "unconditionally"). Undefined: unlisted combinations, a secondary cache, uncached addresses (U12: what hardware does there). - **The N64 needs the caches modelled.** Games and libultra rely on writeback and invalidation around DMA (osWritebackDCache before a PI/SP DMA reads RDRAM; osInvalDCache after a DMA writes it): a core without a D-cache shows DMA data the hardware would hide (stale lines) and hides bugs the hardware shows. Test ROMs: `Mr-Wiseguy/N64-Cache-Emulation-Tests` (stale lines and writeback), nemu64-test `cache` (53 tests, incl. "UnusedBitsInCacheReadWrite"). The cost is also the speed: every load and store does a tag check in Python (domains-and-speed in n64-python). ## Write buffer (UM 4.9, p.120) Four entries, each a physical address, a size and up to a doubleword; it holds uncached stores and the dirty line of a miss; "the pipeline continues"; a load or store needing the bus while the buffer is full stalls. The text also says it holds "eight words (32 bytes), equal to the data cache line size", but the D-cache line is 16 bytes (C1). The order of a buffered uncached store against a later uncached load to an RCP register is observable (an RCP register write followed by a read of a status register): the board needs to see the store first, so the buffer drains in order before a read goes out (UM chapter 12 "Successive Processing of Request"; to be read in full at rung 6). ## SysAD (UM chapter 12) Processor requests: single read (1 to 8 bytes), block read (an I-cache line = 8 words, a D-cache line = 4 words), single write, block write (a dirty line). External requests (the RCP does not use them on the N64 as far as we know: U13). The VR4300 side of each transaction is the core's; how many SClocks the RCP takes to answer is the board's (n64brew: SysAD Interface, RDRAM Interface and the RI registers; the board's Phase 0). PClock:SClock is 3:2 on the N64 (93.75 : 62.5 MHz), so a bus transaction's length in PCycles is not an integer number of SClocks: the core keeps time in PClock half-phases or in MasterClock thirds. **Recommended: time in units of 1/3 of a PCycle-pair**, i.e. the VR4300 counts in PClock cycles and the board's SClock events are placed at multiples of 1.5 PClocks, kept exact as integers by counting in units of half a PClock (one SClock = 3 half-PClocks). The Count register then advances every 4 half-PClocks. ## The modelling level (charter axis B) | Option | What it models | Evidence available | Cost | | --- | --- | --- | --- | | instruction | results; a fixed cycle cost per instruction (MAME: 1) | DOC + tests | cheapest | | **instruction + stall accounting** (recommended) | results; per-instruction base 1 PCycle plus the interlocks of UM 4.6 computed from a small pipeline state (last load's destination, multiply busy-until, D-cache busy, the write buffer), cache hits and misses with the bus time the board returns | UM chapter 4 tables; nemu64-test `timing` (18 tests) and `cache`; n64-systemtest `cycle`/`timing` features | moderate | | cycle (stage by stage) | every stage of every instruction each PCycle, as cen64 does | the same; no extra judge decides stage-internal behaviour | 3-5x the above in Python | The charter's era 3 row says "transaction level appears in specifications". For the VR4300 the recommendation is **instruction-level semantics with transaction-level bus timing and the documented interlocks as stall rules**, because (1) every judge we can run observes cycles only through Count (half-PClock resolution) and through bus order, (2) the UM gives interlocks as rules with fixed costs, and (3) a stage-by-stage model adds no observable the judges can check. A stage-level model stays possible later (it would be a refinement inside the same core, judged by the same timing tests). Handoff Open decision 2.