Expand description
Instruction-cache invalidation, and the only code in the SDK that touches the cache control register or COP0 Status.IsC.
§The hazard this module exists for
Invalidating the instruction cache means changing the cache control register
(0xFFFE0130) and isolating the cache (SR.IsC). On a console, doing either while a DMA
channel is moving data makes the CPU’s next instruction fetch receive a word of the DMA’s
traffic instead of the word from ROM or RAM: a GPU linked-list walk delivered a list word to
the instruction fetch and the program jumped into the weeds (Quake’s flush with DMA2 live,
2026-10-22: six of seven variants dead, the two that flushed with DMA idle or not at all
survived). The BIOS A(44h) FlushCache does the same writes and fails the same way. The
emulator cannot reproduce it, so the SDK makes it impossible to write down.
§How the SDK enforces it
- Ownership. The cache control register and SR.IsC are named in this file and nowhere
else in the SDK (a host test scans every crate for them). There is no public constant, no
raw accessor and no BIOS flush wrapper.
psx-hwkeeps the register’s address as a hardware map entry for the emulator; guest code never uses it. - Proof token. A flush is only reachable through a
Quiescedtoken, whichwith_dma_quiescedhands to its closure after it has masked interrupts and waited (bounded, counted) for every DMA channel to go idle, re-checked inside the masked window. The token holds&mutof every DMA owner token (DmaOwners), so starting a DMA from the closure through any SDK driver does not compile.flush_instruction_cacheandflush_instruction_cache_rangeare that API with nothing else in the closure. - Link time.
cache-gate(tools/psoxide-hazard) disassembles the final image and fails the build when code outside this module’s routines writes0xFFFE0130or sets IsC. - Emulator. A tripwire hard-fails a cache-control write that changes bits 13..16, 3 or 7 while a DMA channel is busy.
The wait cannot hang: every poll loop is bounded, and a bound that runs out is counted
(psx_io::diag, shown on the stop-panel) and the flush still happens, because stale
instructions are the worse failure and a channel wedged for that long is already a fault.
§The flush itself
The R3000A’s instruction cache is 4 KiB, direct mapped, 256 lines of 16 bytes. psx-spx
(“Memory Control”, FFFE0130h, and the COP0 status register’s IsC bit 16) describes how
software reaches its tags: with the cache control register in tag test mode and SR.IsC set, a
word store to an address in 0x000..0x1000 writes the tag of the line that address maps to,
instead of memory. Storing zero marks the line invalid.
The routines:
- run from the KSEG1 (uncached) alias of their own code, so no instruction is fetched through the cache while it changes;
- clear SR before touching the cache control register, so no interrupt can run while the scratchpad is unmapped or the cache is isolated;
- select the flush setting, set SR.IsC alone, and zero the tag of every line;
- clear SR.IsC before writing the running setting back, since whether a store to the cache control register reaches the port while the cache is isolated isn’t documented;
- put the caller’s SR back and return.
Structs§
- DmaOwners
- The DMA owner tokens, borrowed for as long as the bus is held quiet.
- Quiesce
Report - What the DMA gate saw on the way to a quiet bus.
- Quiesced
- Proof that interrupts are masked and no DMA channel is running, for as long as the borrow of
with_dma_quiesced’s closure lasts. It cannot be built any other way, and it owns the right to change the cache.
Functions§
- flush_
i_ cache Deprecated - Renamed to
flush_instruction_cache. - flush_
instruction_ cache - Invalidate every line of the instruction cache, once the DMA channels are idle.
- flush_
instruction_ cache_ range - Invalidate the instruction-cache lines that hold the bytes
[start, end), once the DMA channels are idle. A range of 4 KiB or more is the whole cache. - with_
dma_ quiesced - Run
fwith interrupts masked and every DMA channel idle, and hand it theQuiescedtoken that lets it change the instruction cache.