# MMU, caches and store queues Sources: HM sections 3 (MMU) and 4 (caches, store queues). Tier 2. Each is a module of the core (as sh2-python's `cache.py` is), because each is visible to software. ## MMU (HM 3) - **TLBs**: a 4-entry fully associative ITLB and a 64-entry fully associative UTLB (instructions and operands); page sizes 1 KB, 4 KB, 64 KB, 1 MB; 8-bit ASIDs; single virtual mode (MMUCR.SV=1, one address space shared, ASIDs used only for protection) or multiple virtual mode (HM 3.1.1, 3.4). - **Hardware ITLB refill**: an ITLB miss searches the UTLB in hardware and copies the entry; only a UTLB miss raises the instruction TLB miss exception (HM 3.5.4). - **Software refill**: TLB misses raise exceptions to VBR+400h; software loads PTEH, PTEL, PTEA and executes LDTLB, which writes the UTLB entry chosen by MMUCR.URC (HM 3.5.3). URC "is incremented each time the UTLB is accessed" and wraps at URB when URB > 0 (HM 3.2, MMUCR): replacement depends on how many UTLB accesses happened, so an exact URC needs exact counting of translated accesses (U6). - **Exceptions** (HM 3.6): ITLB multiple hit and data TLB multiple hit (both reset-class, code 140h), ITLB miss (040h), ITLB protection (0A0h), data TLB miss read/write (040h/060h), data protection read/write (0A0h/0C0h), initial page write (080h). - **Memory-mapped arrays** in P4 (HM 3.7): ITLB and UTLB address and data arrays, readable and writable by software (with the associative write of the UTLB address array). - **Synonyms** (HM 3.5.5): the O-cache is indexed by virtual bits 13:5; with 1 KB or 4 KB pages, bits 13:10 or 13:12 are translated, so two virtual aliases of one physical line can occupy two cache lines. Real behaviour, to be reproduced, not "fixed". - Who uses it: the Dreamcast's boot ROM and most games run with MMUCR.AT=0; Windows CE titles turn translation on (141 of 1,509 Redump discs have the Windows CE bit: dreamcast-python docs/census.md). Store-queue bursts are translated through the UTLB when AT=1 (HM 4.7.3). ## Caches (HM 4) - **I-cache**: 8 KB, direct mapped, 256 lines of 32 bytes; index mode (CCR.IIX) indexes with address bit 25 instead of bit 12 (HM 4.4). - **O-cache**: 16 KB, direct mapped, 512 lines of 32 bytes; copy-back or write-through (CCR.CB for P1, CCR.WT for P0/U0/P3; the TLB's C and WT bits when translating); one write-back buffer and one write-through buffer (HM 4.3.4, 4.3.5); index mode (CCR.OIX, bit 25: HM 4.3.7); **RAM mode** (CCR.ORA: half the O-cache becomes 8 KB of on-chip RAM at 7C000000-7FFFFFFF in P0, HM 4.3.6 and HM 3.3.4). Dreamcast software uses the OC RAM as fast scratch memory (HYPOTHESIS for which titles: count from traces later). - **Cache control by software**: OCBI (invalidate), OCBP (purge: write back and invalidate), OCBWB (write back), MOVCA.L (allocate without fetch), PREF (prefetch), and the P4 address and data arrays (HM 4.5): software can read and write tags and data directly. CCR.ICI/OCI invalidate everything. - **No coherency with DMA**: the HM leaves cache/external-memory coherency to software (HM 4.3.8). DMA into RAM that the O-cache holds leaves stale lines; games flush with OCBP or OCBWB. A cache that silently stayed coherent would hide game bugs and change timing: the family's seeded-fault idea "a cache that snoops" (saturn-python's list) applies here. ## Store queues (HM 4.7) - Two queues, SQ0 and SQ1, of eight longwords each. A `MOV.L` (or a pair `FMOV` with SZ=1, quadword) to E0000000-E3FFFFFC writes a longword: bit 5 selects the queue, bits 4:2 the longword (HM 4.7.2). - `PREF @Rn` with Rn in that window bursts the 32 bytes to external memory. With the MMU off the target is QACRn[4:2] (address bits 28:26) plus Rn's bits 25:5; with the MMU on, the UTLB translates (HM 4.7.3). While one queue is being written out, the other can be written without penalty; a write to the queue being transferred waits (HM 4.7.3). - Protection by MMUCR.SQMD (user access allowed or not), and by the UTLB when AT=1 (HM 4.7.4). Reading the queues back exists only on the SH7750R (HM 4.7.5): out of scope. - **This is the Dreamcast's main graphics channel**: games write the TA's vertex stream into the store queues and PREF them to the TA FIFO (10000000, area 4) or into texture memory (Comstedt "pvr": "fed graphic primitives (either using DMA or directly by the CPU using the Store Queues or direct writes)"). Each SQ burst is a board-visible transaction and a sync point between the CPU domain and the video domain (dreamcast-python docs/domains-and-speed.md). ## What the core must expose for the board and for speed - Each external transaction with its kind (fetch, data, line fill, write-back, SQ burst, DMAC) and the I-clock time it was issued, so the board can charge B-clock waits. - Code-invalidation facts for a cached interpreter: ICI, writes to the I-cache arrays, any write to memory that holds decoded code (DMA, SQ bursts, the other bus masters), LDTLB and TLB array writes when AT=1 (translations change what a virtual PC means).