// SPDX-License-Identifier: MIT //! The SH7604 cache: 4 KB, four ways of 64 sixteen-byte lines, write-through, pseudo-LRU. //! //! sh2-python's `cache.py`. HM chapter 8. An address in the cache area (A31-A29 = 000) //! splits into a 19-bit tag (A28-A10), a 6-bit entry (A9-A4) and a byte in the line //! (A3-A0) (HM figure 8.2). Each entry has four ways, each with a tag, a valid bit and 16 //! bytes; each entry has six LRU bits (HM 8.4.5, figure 8.8). The same storage is //! reachable directly: //! //! - the **address array** at H'60000000-H'600003FF (A31-A29 = 011), longword only, the //! way chosen by CCR.W1-W0 (HM 8.4.9, figure 8.11); //! - the **data array** at H'C0000000-H'C0000FFF (A31-A29 = 110), any size, way in //! A11-A10 (HM 8.4.8, figure 8.10): 4 KB of RAM with the cache off, ways 0-1 as 2 KB of //! RAM in two-way mode (CCR.TW); //! - the **associative purge** space at H'40000000- (A31-A29 = 010): a write invalidates //! the line holding that address in whichever way holds it (HM 8.4.7). //! //! This module holds the storage and the rules; the chip (``core.rs``) decides which //! accesses reach it and makes the line fills on the bus (``kind`` = ``FILL``). use crate::bus::Bus; use crate::cpu::Sh7604; use crate::module::{reg, value_of, ModuleState, Register}; // CCR bits (HM 8.2, table 8.1). pub const CCR_CE: u32 = 0x01; // cache enable pub const CCR_ID: u32 = 0x02; // instruction replacement disable pub const CCR_OD: u32 = 0x04; // data replacement disable (printed "OC" in HM appendix B: C7) pub const CCR_TW: u32 = 0x08; // two-way mode: ways 0-1 are RAM, ways 2-3 cache pub const CCR_CP: u32 = 0x10; // write 1: purge every valid bit and the LRU; always reads 0 pub const CCR_W: u32 = 0xC0; // W1-W0: the way the address array shows // HM table 8.3, "LRU Information after Update": for each way, the bits a hit or a // replacement clears and sets (bit 5..0 of the six LRU bits). pub const LRU_CLEAR: [u32; 4] = [0x38, 0x06, 0x01, 0x00]; pub const LRU_SET: [u32; 4] = [0x00, 0x20, 0x14, 0x0B]; pub const REGISTERS: [Register; 1] = [reg(0xFFFFFE92, "CCR", 8, Some(0x00))]; /// The way HM table 8.4 ("Selection Conditions for Replaced Way") replaces. /// /// Way 0 when bits 5, 4, 3 are 1, 1, 1; way 1 when bits 5, 2, 1 are 0, 1, 1; way 2 /// when bits 4, 2, 0 are 0, 0, 1; way 3 when bits 3, 1, 0 are 0, 0, 0. LRU = 0 after /// a purge selects way 3, then 2, 1, 0 (HM 8.4.5). The six bits can hold a value no /// row matches only if software writes one through the address array (HM: "be sure /// not to write a value other than 0"); this core then replaces way 3, a choice no /// source settles (recorded in docs/undocumented-behavior.md, U8). pub fn lru_victim(lru: u32) -> u32 { if lru & 0x38 == 0x38 { return 0; } if lru & 0x26 == 0x06 { return 1; } if lru & 0x15 == 0x01 { return 2; } 3 } /// The cache storage, its LRU, CCR, and the three direct-access spaces. #[derive(Clone, Debug)] pub struct Cache { /// way * 1024 + entry * 16 + byte pub data: Vec, /// entry * 4 + way pub tags: [u32; 256], pub valid: [u8; 256], pub lru: [u32; 64], pub ccr: u32, /// True when CCR.CE is set: the core's hot path reads this plain attribute. pub enabled: bool, } impl Cache { pub fn new() -> Cache { // Cache contents survive reset (HM 8.5.1: "Cache memory is not initialized in a // reset"); they start zeroed here, the "undefined" choice of U19. Cache { data: vec![0; 4096], tags: [0; 256], valid: [0; 256], lru: [0; 64], ccr: 0, enabled: false } } // -- CCR ------------------------------------------------------------------- /// CCR write (HM 8.2). CP = 1 purges and reads back 0 (HM 8.2 bit 4; C7). pub fn write_ccr(&mut self, value: u32) { if value & CCR_CP != 0 { self.purge_all(); } // Bit 5 is reserved and reads 0; CP always reads 0. self.ccr = value & !(CCR_CP | 0x20) & 0xFF; self.enabled = self.ccr & CCR_CE != 0; } /// CCR.CP = 1: every valid bit and all LRU information to 0 (HM 8.4.6). pub fn purge_all(&mut self) { self.valid = [0; 256]; self.lru = [0; 64]; } // -- lookup, replacement ---------------------------------------------------- /// The way holding ``address``, or -1 on a miss (HM 8.4.1). /// /// Tags are compared in all four ways, two-way mode included (HM 8.4.5). Two ways /// holding the same tag can only be made through the address array; the first in /// way order 0-3 wins (U8, a choice). pub fn lookup(&self, address: u32) -> i32 { let tag = (address >> 10) & 0x7FFFF; let base = (((address >> 4) & 63) << 2) as usize; for way in 0..4 { if self.valid[base + way] != 0 && self.tags[base + way] == tag { return way as i32; } } -1 } /// Record an access to ``way`` in the entry's LRU (HM table 8.3). /// /// HM 8.4.5: the LRU is rewritten on a read hit, a write hit and a replacement. pub fn touch(&mut self, entry: u32, way: u32) { let e = entry as usize; self.lru[e] = (self.lru[e] & !LRU_CLEAR[way as usize]) | LRU_SET[way as usize]; } /// The way a miss in ``entry`` replaces (HM table 8.4; two-way mode HM 8.5.4). pub fn victim(&self, entry: u32) -> u32 { let lru = self.lru[entry as usize]; if self.ccr & CCR_TW != 0 { // Two-way mode replaces only ways 2 and 3; with the LRU at 0 "ways 3 and 2 // are initially used, in that order" (HM 8.5.4). Table 8.4's bit 0 alone // separates them. return if lru & 1 != 0 { 2 } else { 3 }; } lru_victim(lru) } /// Write the tag and valid bit for a replacement; return the line's data offset. /// /// HM 8.4.5: "Writing for the tag address and valid bit for cache replacement does /// not wait for the read from memory to be completed." pub fn allocate(&mut self, address: u32, way: u32) -> u32 { let entry = (address >> 4) & 63; let index = ((entry << 2) + way) as usize; self.tags[index] = (address >> 10) & 0x7FFFF; self.valid[index] = 1; self.touch(entry, way); (way << 10) | (entry << 4) } // -- line data --------------------------------------------------------------- /// ``width`` bits of the data array at ``offset`` (big-endian). pub fn get(&self, offset: u32, width: u32) -> u32 { let d = &self.data; let o = offset as usize; if width == 32 { return ((d[o] as u32) << 24) | ((d[o + 1] as u32) << 16) | ((d[o + 2] as u32) << 8) | d[o + 3] as u32; } if width == 16 { return ((d[o] as u32) << 8) | d[o + 1] as u32; } d[o] as u32 } pub fn put(&mut self, offset: u32, width: u32, value: u32) { let d = &mut self.data; let o = offset as usize; if width == 32 { d[o] = ((value >> 24) & 0xFF) as u8; d[o + 1] = ((value >> 16) & 0xFF) as u8; d[o + 2] = ((value >> 8) & 0xFF) as u8; d[o + 3] = (value & 0xFF) as u8; } else if width == 16 { d[o] = ((value >> 8) & 0xFF) as u8; d[o + 1] = (value & 0xFF) as u8; } else { d[o] = (value & 0xFF) as u8; } } /// Write-through's cache half: update a hit line and its LRU (HM 8.4.2). pub fn write_hit(&mut self, address: u32, width: u32, value: u32) -> bool { let way = self.lookup(address); if way < 0 { return false; } let way = way as u32; let entry = (address >> 4) & 63; self.put((way << 10) | (entry << 4) | (address & 15), width, value); self.touch(entry, way); true } // -- the direct-access spaces -------------------------------------------------- /// Associative purge (HM 8.4.7): invalidate the line holding ``address`` in any way. pub fn purge_line(&mut self, address: u32) { let tag = (address >> 10) & 0x7FFFF; let base = (((address >> 4) & 63) << 2) as usize; for way in 0..4 { if self.valid[base + way] != 0 && self.tags[base + way] == tag { self.valid[base + way] = 0; } } } /// Address array read (HM 8.4.9, figure 8.11): tag, LRU and V of the CCR.W way. pub fn read_address_array(&self, address: u32) -> u32 { let entry = (address >> 4) & 63; let index = ((entry << 2) + ((self.ccr & CCR_W) >> 6)) as usize; (self.tags[index] << 10) | (self.lru[entry as usize] << 4) | ((self.valid[index] as u32) << 2) } /// Address array write (HM figure 8.11): tag and V from the address, LRU from the data. pub fn write_address_array(&mut self, address: u32, value: u32) { let entry = (address >> 4) & 63; let index = ((entry << 2) + ((self.ccr & CCR_W) >> 6)) as usize; self.tags[index] = (address >> 10) & 0x7FFFF; self.valid[index] = ((address >> 2) & 1) as u8; self.lru[entry as usize] = (value >> 4) & 63; } pub fn state(&self) -> ModuleState { vec![("CCR", self.ccr as i64)] } pub fn load_state(&mut self, values: &ModuleState) { self.ccr = value_of(values, "CCR") as u32; self.enabled = self.ccr & CCR_CE != 0; } /// The storage: 4,096 data bytes, then 256 tags, 256 valid bits, 64 LRU fields. pub fn internal(&self) -> Vec { let mut out: Vec = self.data.iter().map(|v| *v as i64).collect(); out.extend(self.tags.iter().map(|v| *v as i64)); out.extend(self.valid.iter().map(|v| *v as i64)); out.extend(self.lru.iter().map(|v| *v as i64)); out } pub fn load_internal(&mut self, values: &[i64]) { self.data = values[..4096].iter().map(|v| *v as u8).collect(); for (i, v) in values[4096..4352].iter().enumerate() { self.tags[i] = *v as u32; } for (i, v) in values[4352..4608].iter().enumerate() { self.valid[i] = *v as u8; } } } impl Default for Cache { fn default() -> Self { Cache::new() } } impl Sh7604 { pub(crate) fn cache_reset(&mut self) { // HM 8.4.6: "CCR's CE bit is cleared to 0 by a power-on reset or manual reset"; // CCR's other bits too (table 8.1, initial value H'00). Contents are kept. self.ccr_at = 0; self.cache.write_ccr(0); } pub(crate) fn cache_read8(&mut self, address: u32) -> u32 { if self.ccr_at != 0 && self.clock >= self.ccr_at { self.settle_ccr(); } if address == 0xFFFFFE92 { self.cache.ccr } else { 0 } } /// A CPU's CCR write: it waits behind a buffered external write (U9). /// /// DN (hardware, a CPS3; the judge for purge ordering): a cache-through store, a /// CCR purge, then a cached read returns the line from before the purge, and the /// eight values of DN's sequence fit "the CCR write waits behind the pending store /// while hits can still use the old line". HM 7.11.2 says on-chip module accesses /// do not wait for a buffered write (C20). So a CCR write made while the BSC's /// write buffer holds a write is kept, and lands when that write's cycles end /// (the core's ``settle_ccr``); the CPU and its cache hits go on. One still /// waiting when another arrives lands first. pub(crate) fn cache_write8(&mut self, address: u32, value: u32) { if address != 0xFFFFFE92 { return; } if self.ccr_at != 0 { self.settle_ccr(); } if self.wb_end > self.clock { self.ccr_at = self.wb_end; self.ccr_value = value; } else { self.cache.write_ccr(value); } } }