# COP0, the TLB, the caches, the scratchpad and the bus buffers Abbreviations as in [isa.md](isa.md): ECUM (Sony EE Core User's Manual 6.0), ECISM, TX79 (Toshiba's C790 core manual, Rev. 2.0). All DOC unless tagged otherwise. ## COP0 registers Index, Random, EntryLo0/1, Context, PageMask, Wired, BadVAddr, Count, EntryHi, Compare, Status, Cause, EPC, PRId, Config, BadPAddr, the breakpoint set (BPC, IAB/IABM, DAB/DABM, DVB/DVBM), the performance counters (PCCR, PCR0/PCR1), TagLo/TagHi, ErrorEPC (ECUM §3.2, one page per register). Count increments every EE cycle and Count == Compare raises the timer interrupt; writing Compare clears it (ps2tek "EE COP0 Timer", tier 3; ECUM §3.2 "Count", "Compare"). **PRId tells the two CPUs apart.** The same boot ROM starts both the EE and the IOP; its first instructions read PRId and branch to the IOP's code when PRId < 59h (ps2tek "BIOS Boot Process"; OBSERVED in MAME's traces of both CPUs from reset, docs/mame-oracle.md: `mfc0 $k0,PRId; slti $at,$k0,0x59; bnez $at,...` at BFC00000h, the EE falling through to BFC00800h, the IOP branching to BFC02000h). The EE's PRId value itself is **U4** (ECUM's PRId page gives the field layout; the value is read from a console or from the test `cpu/ee_cop0`). ## Exceptions Two levels (ECUM §4.1). Level 1 (TLB, address error, bus error, syscall, break, reserved instruction, coprocessor unusable, trap, overflow, interrupt) sets Status.EXL, EPC and Cause.BD; a level-1 exception taken while EXL = 1 does not update EPC and goes to the common vector. Level 2 (reset, NMI, performance counter, debug) sets Status.ERL, ErrorEPC and Cause.BD2. Vectors (ECUM Table 4-1): reset/NMI BFC00000h; with BEV = 0, TLB refill 80000000h, counter 80000080h, debug 80000100h, common 80000180h, **interrupt 80000200h** (the EE has a separate interrupt vector, unlike the R4000); with BEV = 1 the same offsets from BFC00200h. ECUM §4.1.2 gives the level-1 procedure as pseudo-code, which the specification's handler follows in prose. Interrupts: two lines from the rest of the EE, INT0 from the INTC and INT1 from the DMAC (EE User's Manual §1.1, Table "Built-in devices"), plus the COP0 timer; masked by Status.IM and the EE-specific **Status.EIE** (set and cleared by EI and DI; ECUM "Status"; ECISM "EI", "DI"). The board drives the two lines; the core samples them at instruction boundaries (decision E4). Manufacturer errata the specification follows (TX79 §1.5, items FLX01-FLX06): a TLB exception right after a bus error masks the bus error; bus errors are masked while EXL or ERL is set (the CPU executes whatever the bus returned); index-type ICACHE/BTAC CACHE instructions raise AdEL; kuseg becomes uncached while ERL = 1; the first two instructions of a handler entered on a bus error execute as NOPs. Each is a behaviour with a manufacturer source, so the specification models it (tagged DOC: TX79 §1.5). ## Address spaces and the TLB - 32-bit virtual and physical addresses only; the upper 32 bits of a 64-bit address are ignored, with no address error for them, and a PC that wraps from kseg3 to kuseg in kernel mode raises none either (ECUM §1.5, §5.1.2). - Modes User, Supervisor, Kernel; kuseg/suseg/useg 2 GB mapped, kseg0 (cached by Config.K0) and kseg1 (uncached) unmapped onto physical 0-1FFFFFFFh, ksseg and kseg3 mapped; kuseg unmapped while ERL = 1 (ECUM Figure 5-1). - **TLB:** 48 entries of even/odd page pairs (96 pages), fully associative; page sizes 4 KB to 16 MB; an ITLB of 2 entries and a DTLB of 4 entries in front of it (ECUM §1.5). An entry is 128 bits: MASK, VPN2, G, ASID, and per page PFN, C, D, V, with **S (bit 63) marking a scratchpad mapping** (ECUM §5.2.4, Figure 5-9). - **Cache modes per page** (C field): 2 uncached, 3 cached write-back write-allocate, 7 uncached accelerated (ECUM §5.2.6). The physical memory map (main RAM's uncached and accelerated mirrors at 20000000h and 30100000h in kernel use) is the board's (ps2-python docs/memory-map.md); the core sees only translated addresses and the C mode. - Whether the ITLB/DTLB micro-TLBs have observable effects (a TLB write not seen until they refill) is **U5**; ECUM §1.3.2 says the ITLB translates in stage Q, nothing about coherency after TLBWI. SYNC.P after TLBWI is what the manuals' sample code does (ECUM §6.4.3). ## Caches | | Instruction cache | Data cache | | --- | --- | --- | | Size, ways, line | 16 KB, 2-way, 64 bytes (128 sets) | 8 KB, 2-way, 64 bytes (64 sets) | | Index / tag | virtual bits 12..6 / physical | virtual bits 11..6 / physical | | Tag state | V, LRF | V, D, LRF, L (lock) | | Policy | refill 4 qwords, critical qword first; restart after the last qword | write-back, write-allocate; early restart on the first qword | | Source | ECUM §6.1-6.3, Tables 6-1, 6-2 | same | - **Replacement is least-recently-filled**: a refill flips the line's LRF bit; **loads and stores that hit do not change it** (ECUM §6.3.1, Table 6-4). A locked way is never refilled; an invalid I-cache way is filled first. Because a hit changes no state, a decoded-instruction cache in the Rust tiers is exact by construction for cached code, exactly as for the VR4300 (n64-python execution-architecture.md) and unlike the SH-2. - **No snooping**: DMA does not update or invalidate either cache; software uses CACHE operations (ECUM §1.6, §6.1). So DMA into memory that holds cached code is invisible to the I-cache until software invalidates it (DOC): a game that forgets is wrong on hardware too, and the specification must reproduce that, not repair it. - **Non-blocking loads and hit under miss** (ECUM §6.3.2): GPR loads that miss do not stall until a dependent instruction issues or pipe 0 stalls; COP1/COP2 loads always block; uncached loads continue until a dependent access, another miss or another uncached load. These shape timing only (docs/pipeline-timing.md). - CACHE operations (IXIN, IXLTG, IXSTG, IXLDT, IXSDT, IHIN; DXWBIN, DXIN, DXLTG, DXSTG, DXLDT, DXSDT, DHWBIN, DHIN, DHWOIN; BXLBT, BXSBT, BFH, BHINBT for the BTAC and BHT) read and write tags and data, so the tag state is visible to software (ECUM ch. 6, ECISM "CACHE"). The specification therefore keeps real tags, not only contents. ## Scratchpad RAM (SPRAM) 16 KB, 1024 x 128 bits, reached only through a TLB entry with S = 1 that maps one 16 KB page on a 16 KB boundary; MASK must be zero and both halves' D and V equal; PFN and C are ignored (C reads back 2). The paired odd 16 KB cannot be mapped as a 16 KB page and must be mapped as four 4 KB pages or left unused (ECUM §5.2.5, §6.4.3). The BIOS and games map it at 70000000h (ps2tek memory map, tier 3). **The DMAC reads and writes it directly** (channels 8 and 9, SPR_FROM/SPR_TO; ps2-python docs/dmac-intc-timers.md) through a dedicated SPRAM write signal and a 10-bit qword index; CPU and DMA accesses in the same cycle alternate, the DMAC first (ECUM §6.4.2). The SPRAM is therefore a **channel** between the core and the board's DMAC: the core owns its array, the board's DMAC calls `spram_read(index)`/`spram_write(index, qword)` on the core, and each such call is a sync point. ## Bus buffers: WBB and UCAB - **WBB**, the write-back buffer: 8 entries of one qword, a FIFO between the core and the CPU bus; it also gathers sequential uncached-accelerated stores within one qword until a store to another qword or attribute, a load, SYNC/SYNC.L, or an exception (ECUM §1.2.8, §5.2.6). - **UCAB**, the uncached accelerated buffer: 8 qwords; an uncached-accelerated load miss fetches 128 bytes, and later loads that hit it are served from it (ECUM §1.2.9, §6.5). - Both change **when** and **in what size** bus writes and reads happen, which the board sees (a GS privileged register write, a VIF FIFO write). The specification models them as transaction producers: every bus transaction leaves the core with its time and size (decision E3). ## Unknowns from this page U4 the EE's PRId value; U5 micro-TLB coherency after TLB writes; U6 cache behaviour of CACHE operations on the scratchpad-mapped page; U7 what the WBB does with a store to the same address as a pending entry of a different size; U8 UCAB invalidation (ECUM §6.5 says when it is filled, not when stale data in it is dropped).