# CP15, the TCMs, the protection unit, the caches and the write buffer All of this is inside the ARM946E-S macrocell (DDI 0201D figure 1-1), so it belongs to the core, not the board: the board sees only what comes out of the bus interface unit (BIU) as AHB transfers. The DS's configuration (sizes, the fixed ITCM base) is Nintendo's choice of the macrocell's parameters. Sources: DDI 0201D (chapters 2-6), GBATEK "ARM CP15 System Control Coprocessor" and "DS Memory Control - Cache and TCM". Tags are DOC unless marked. ## The DS's configuration (read back on a DS by GBATEK; matches the TRM's encodings) | Register (MRC p15,0,Rd,...) | DS value | Meaning | | --- | --- | --- | | c0,c0,0 Main ID | 41059461h | ARM, v5TE, ARM946, rev 1 | | c0,c0,1 Cache type | 0F0D2112h | separate caches, write-back with lockdown format B; I-cache 8 KB, D-cache 4 KB, 4-way, 32-byte lines | | c0,c0,2 TCM size | 00140180h | ITCM 32 KB, DTCM 16 KB | | c0,c0,3..7 | as c0,c0,0 | | ITCM base is fixed at 0; its *virtual* size (c9,c1,1) can be up to 4 GB, so it mirrors every 32 KB; the DS firmware sets 32 MB, so ITCM appears throughout 00000000h-01FFFFFFh (the PU can then hide parts of it: GBATEK). DTCM's base and size are programmable (c9,c1,0; the BIOS sets base 00800000h, size code 5 = 16 KB, OBSERVED in MAME's run of the BIOS; retail games move it, GBATEK's default 027C0000h). Where the two overlap, ITCM wins (GBATEK). ## Control register (c1,c0,0) DDI 0201D table 2-9: bit 0 PU enable; 2 D-cache enable; 3-6 SBO (read as 1); 7 big-endian; 12 I-cache enable; 13 alternate vectors (FFFF0000h); 14 round-robin replacement (else pseudo-random); 15 disable loading TBIT (ARMv4 behaviour for loads into PC); 16 DTCM enable; 17 DTCM load mode; 18 ITCM enable; 19 ITCM load mode. GBATEK: on the DS bits 0, 2, 7, 12-19 are R/W, 3-6 read 1, the rest 0. **Load mode** makes that TCM write-only: loads go to what lies behind it, stores to the TCM, so code can be copied into the TCM from the same addresses; SWP does not obey it (GBATEK). ## Protection unit (c2, c3, c5, c6) - Eight regions, c6,c0..c7: bit 0 enable, bits 1-5 size N (2^(N+1) bytes, 4 KB to 4 GB), bits 12-31 base (aligned to the size). On the DS the instruction and data region registers are one set (c6,cN,1 mirrors c6,cN,0: GBATEK "Unified Region Note"). - Higher-numbered regions win where they overlap; outside every region is the background, no access (DDI 0201D 4.3). - Per region and separately for instruction and data: cachable (c2,c0,1 / c2,c0,0), bufferable for data (c3,c0,0), access permission (c5: the 2-bit form c5,c0,0/1 and the 4-bit "extended" form c5,c0,2/3, the same bits seen two ways). AP 0 none, 1 privileged R/W, 2 user R, 3 full, 5 privileged R, 6 R (GBATEK). - A denied access is an abort: prefetch abort on fetch, data abort on load/store, with the base-restored model (isa-v5te.md). - The TCMs are subject to the PU when it is on (DDI 0201D 5.5). ## The caches (DDI 0201D chapter 3) - 4-way set associative, 8-word (32-byte) lines; on the DS the I-cache has 64 sets (8 KB: index Addr[10:5]) and the D-cache 32 sets (4 KB: Addr[9:5]), DDI 0201D table 3-1. - **Allocate on read miss only**; a miss in a cachable region fetches the whole line as an eight-word burst starting at word 0 (not critical-word first), with the core advanced during the fetch ("streaming", which c15 test-state bits 11-12 can disable). - Replacement: a segment counter, round-robin or "pseudo-random" (control bit 14). **The pseudo-random sequence is not documented** (U1): it decides which line a miss evicts, which software can observe through timing and through stale data after a write-back. - Lockdown: c9,c0,0/1 (format B, the cache type says) restrict the counter so locked segments are never replaced. - D-cache writes: write-through (Cd=1, Bd=0) or write-back (Cd=1, Bd=1) per region; each line has a valid bit and dirty bits (two, one per half line: GBATEK's cache-debug tag layout "2..3 Dirty Bits"; U2 whether a write-back writes the half or the whole line). - **No snooping**: DMA and the ARM7 change memory under the caches; software cleans and invalidates (c7). The I-cache never sees stores: self-modifying code needs an invalidate (DDI 0201D 3.2.3). This is what lets a later cached interpreter be exact for cached code (handoff-brief.md, "Domains and speed"). - c7 operations on the DS (GBATEK's "ARM9: Yes" column): invalidate all / line by address (I and D), clean line by address or set/index, clean and invalidate line, prefetch I-cache line, drain write buffer is **not** listed as supported on the ARM9 in GBATEK's table (the BIOS issues it anyway: C1), wait for interrupt (c7,c0,4 and c7,c8,2). ## TCMs (DDI 0201D chapter 5) - Single-cycle, no wait states, not on the AHB: DMA cannot reach them, and code and data in TCM run on while DMA holds the bus (GBATEK "ARM CP15 Tightly Coupled Memory"). - ITCM is single-ported: an instruction fetch and a data read of ITCM in the same cycle cost one stall (DDI 0201D 5.5.3). Data accesses to DTCM stall only behind a completing DTCM write (5.4). - The DTCM is data-only: an instruction fetch from a DTCM address goes past it to the bus. ## The write buffer and BIU (DDI 0201D chapter 6) - 16 entries, each an address or a data word (so 8 single stores, more for STM bursts); used for bufferable regions only, so not at all while the PU is off. - Uncached accesses are single AHB transfers; **sequential uncached instruction fetches are made nonsequential** (6.2.6), and Thumb fetches are 32-bit AHB reads latched for the second halfword (6.3). GBATEK measured the same on a DS ("all opcode fetches are forcefully made nonsequential 32bit"). - The AHB runs from HCLKEN, a clock enable: on the DS every second core clock (bus 33.51 MHz, core 67.03 MHz). Starting an AHB transfer waits for the next HCLKEN, a synchronisation penalty of up to "the HCLK to CLK ratio minus 1" core cycles (6.4). GBATEK's DS-measured "3 wait penalty on nonsequential accesses" for the ARM9 is larger than that and its cause is not documented (U3; timing.md). ## Unknowns (U) and contradictions (C) - U1 The pseudo-random replacement sequence (and its reset state). Judge: a probe that fills a set and reads which line went (asiekierka's cached-memory-performance shows how to time it); default round-robin's documented behaviour, pseudo-random as HYPOTHESIS. - U2 Dirty granularity and the write-back burst (half line or full line). - U3 Where GBATEK's nonsequential 3-cycle penalty comes from (BIU, or the DS's bus arbiter): decides whether it belongs in this core or in nds-python. Default: the board (it also hits ARM7 main-RAM accesses, GBATEK "Main Memory is ALWAYS having the nonsequential 3 wait PENALTY (even on ARM7)"). - U4 What a BIST run does to cache and TCM contents beyond GBATEK's description; low priority (software does not use it). - U5 PU region size codes below 11 (under 4 KB): ignored, or treated as 4 KB? - C1 GBATEK marks c7,c10,4 "drain write buffer" as not an ARM9 command, but the DS BIOS issues it (OBSERVED in MAME's BIOS run) and DDI 0201D lists it in register 7's operations. Follow the TRM. - C2 The second wait-for-interrupt encoding: GBATEK lists c7,c8,2; DDI 0201D 2.3.10 gives `MCR p15,0,Rd,c15,c8,2` ("for compatibility with existing software"). Implement the TRM's c7,c0,4 and c15,c8,2; treat c7,c8,2 as a HYPOTHESIS to check (rockwrestler or a probe), since GBATEK may be reporting what the DS does.