# CP0: the TLB, exceptions, interrupts and hazards Sources: UM chapter 5 (memory management), chapter 6 (exceptions), chapter 14 (interrupts), chapter 19 (CP0 hazards); R4400 UM chapters 4-6. Tier 2 unless marked. Test-ROM facts are from nemu64-test (`reference/src/nemu64-test`, MIT, tier 3: its author writes the expectations from runs on real units; we have not run it). ## Registers (UM Table 1-2, p.46) | # | Register | Notes for the build | | --- | --- | --- | | 0 | Index | P bit (probe failure) in bit 31 | | 1 | Random | "decrements as each instruction executes" between Wired and 31; 31 on cold reset and on a write to Wired; called read-only, yet "Bit 5 is readable and writable by software" (UM 5.4.2, p.147: C3). Per instruction or per PCycle (stalls) is a test-ROM question (U6) | | 2, 3 | EntryLo0, EntryLo1 | PFN, C (cache attribute), D, V, G | | 4 | Context | PTEBase, BadVPN2 | | 5 | PageMask | 4 KB, 16 KB, 64 KB, 256 KB, 1 MB, 4 MB, 16 MB (UM Table 5-7) | | 6 | Wired | | | 8 | BadVAddr | 64-bit (n64-systemtest's README: "expected to be 64 bit") | | 9 | Count | +1 every two PClocks (UM p.164) | | 10 | EntryHi | VPN2, ASID; 64-bit with the R field in 64-bit mode | | 11 | Compare | a write clears the timer interrupt (IP7) | | 12 | Status | CU3-0, RP, FR, RE, ITS, BEV, TS, SR, CH, CE, DE, IM7-0, KX, SX, UX, KSU, ERL, EXL, IE | | 13 | Cause | BD, CE, IP7-0, ExcCode; IP1-0 software-writable | | 14 | EPC | 64-bit | | 15 | PRId | 0x0B10 or 0x0B22 on retail units (n64brew: VR4300; OBSERVED) | | 16 | Config | EC (the clock ratio, read-only), EP, BE, K0 (kseg0 cache attribute), CU | | 17 | LLAddr | | | 18, 19 | WatchLo, WatchHi | the watch exception on a physical address | | 20 | XContext | | | 26, 27 | Parity Error, Cache Error | "defined to maintain compatibility with the VR4200, and not used with the hardware of the VR4300" | | 28, 29 | TagLo, TagHi | the CACHE instruction's tag transfer | | 30 | ErrorEPC | | | 7, 21-25, 31 | reserved | **TEST ROM**: on hardware a read returns the value of the last write to *any* CP0 register (one shared latch), per nemu64-test `cop0::UnusedRegistersWriteRead` and its two companions (level "Weird"). The manual says nothing. | ## The TLB (UM 5.1-5.2) 32 entries, each mapping an even/odd pair of pages (EntryLo0/EntryLo1), with a PageMask, a VPN2, an ASID and a G (global) bit; fully associative; TLBP/TLBR/TLBWI/TLBWR. Two matching entries at once: the R4400 UM's "TLB shutdown" (Status.TS) case; whether and how the VR4300 sets TS is a question (U7). The 2-entry instruction micro-TLB refills from the JTLB in 3 PCycles (UM 4.6.2, p.107). nemu64-test has 46 TLB and 11 64-bit TLB tests. Exceptions from translation: TLB refill (vector +0x000, or +0x080 for the 64-bit XTLB refill), TLB invalid, TLB modified, address error (with BadVAddr, Context, XContext and EntryHi's VPN2 filled as UM chapter 6 lists per exception). ## Exceptions (UM chapter 6) and their pipeline stage (UM Figure 4-12, p.104) Stage, highest priority first within a stage: IC: IADE, ITLB, IBE (with the ITM and ICB interlocks); RF: SYSC, BRPT, CPU (coprocessor unusable), RSVD (with LDI and MCI interlocks in EX); EX: RST, NMI, OVFL, TRAP, FPE, DADE, DTLB, WAT, INTR, DBE (with DCM, DCB, COp); WB: CP0I. "When a pipeline exception condition occurs, the pipeline stalls for 2 PCycles" (UM p.114). nemu64-test's `timing::Exceptions` ("surprisingly quick") measures BREAK and others: the cost of taking an exception is a judged number at rung 6, not a guess. Vectors: start-here.md. Cold reset, soft reset and NMI go through 0xBFC00000 with Status.ERL/SR/BEV set as UM chapter 9 says; on the N64 the PIF raises NMI on the reset button (after the pre-NMI interrupt; board). ## Interrupts (UM chapter 14) Cause.IP7 is the timer (Count == Compare); IP6-IP2 are the Int4-Int0 pins; IP1-IP0 are software. An interrupt is taken when Status.IE=1, EXL=0, ERL=0 and (IP & IM) != 0. The CP0 hazard table gives an interrupt's source hazard as 3 (Cause.IP, Status.IM, IE, EXL, ERL): the cycle at which a changed pin becomes visible is a timing rung question (U8). On the N64 the MI (in the RCP) drives Int0 (IP2); the board's Phase 0 confirms which other pins are wired (the cartridge's /INT and the 64DD, n64brew). ## CP0 hazards (UM chapter 19, Table 19-1, p.621) The CP0 registers and the TLB are **not interlocked**; software must space instructions. Table 19-1 gives hazard numbers per operation as source and destination stages (for example MTC0 destination 7, MFC0 source 4: "(7) - (4 + 1) = 2 instructions" between an MTC0 and a dependent MFC0). The numbers run up to 8, which is more stages than this five-stage pipeline has; the chapter says they are "equivalent or less than those of the VR4400". So **Table 19-1 is a compatibility guarantee, not this part's behaviour** (C2). What happens when code breaks the rule is what a cycle-accurate core must reproduce, and the evidence is nemu64-test's optional `cop0hazard` feature set (tier 3), not the manual. The recommended model (handoff-brief): each CP0 write takes effect at the stage the pipeline model says (the WB of the MTC0) and each read reads at its own stage, so the hazard behaviour falls out of the pipeline; it is judged by the `cop0hazard` tests.