# SPDX-License-Identifier: MIT """Opcode fetch: the pipeline byte, the 512-byte code cache, and the cache's S-CPU window. **The pipeline** (BK2 2-6-1..2-6-3; U1, U2). The GSU fetches one byte ahead: each instruction starts by taking the pipeline byte as its opcode and fetching ``[PBR:R15]`` into the pipeline (R15 then reads as the address of the next opcode); operand bytes come through the same byte (``_pipe``: R15 + 1, then fetch). A jump writes R15, so the byte already in the pipeline (the one after the jump) runs before the target: the delay slot. **The cache** (BK2 6.8; fullsnes "Code-Cache"; U14, U15, C10). 512 bytes in 32 lines of 16; an opcode fetch at ``PBR:R15`` with ``R15 - CBR`` below 512 is a cache access. The cache RAM is addressed by the GSU address's low 9 bits (BK2 6.8.3: "The address in the CBR cache RAM complies with the address indicated by the value of the low 9 bits of the CBR"), which is also where the S-CPU sees it: ``$3100 + (address AND 1FFh)``. A valid line costs 1 GSU cycle a byte; otherwise the byte comes from ROM or RAM. BK2 2-6-10 fills lines **while executing**: a jump out of a partly loaded line first loads the rest of it, and a jump into the middle of an unloaded line first loads its leading bytes (figure 2-6-1); the emulators load the whole line at the first miss (C10: ``INCREMENTAL_CACHE_FILL = False``). The contents are the same either way; the time differs. """ from __future__ import annotations from gsu_python import timing class CacheMixin: r: list[int] pbr: int cbr: int clsr: int clock: int pipeline: int pipeline_address: int cache: bytearray cache_loaded: list[int] filling: int #: C10. True (default): BK2's incremental line fill. False: the whole line at the first #: miss (ares ``memory.cpp`` l.46-53; MAME). INCREMENTAL_CACHE_FILL = True #: The cache window (U14). True (default): ``$3100 + offset`` is cache RAM byte ``offset``, #: the GSU address's low 9 bits (BK2 6.8.3 and its CBR = C3A0h example; fullsnes "Cache #: Area"). False: ares (``memory.cpp`` l.91-99) and MAME index the window from CBR #: (``(offset + CBR) & 1FFh`` into a buffer that starts at CBR), which agrees only #: when CBR & 1FFh = 0. CACHE_WINDOW_BY_ADDRESS = True def _fetch(self, address: int) -> int: """One opcode byte at ``PBR:address``, with its time.""" if (address - self.cbr) & 0xFFFF < 512: index = address & 0x1FF if self.cache_loaded[index >> 4] == 16: self.clock += timing.CACHE_FETCH[self.clsr] return self.cache[index] return self._cache_miss(address, index) if self.filling >= 0: self._finish_line() return self._fetch_memory(address) def _fetch_memory(self, address: int) -> int: """An opcode byte from ROM or RAM. From ROM it waits for the ROM buffer (BK2 7.1.2: "The instruction following a change in register R14 will not begin execution until the ROM buffer is loaded"); from RAM for the write buffer (BK2 7.2.2).""" bank = self.pbr if bank < 0x60: self._sync_rom() else: self._sync_ram() self.clock += timing.MEMORY_ACCESS[self.clsr] return self._read(bank, address) def _cache_miss(self, address: int, index: int) -> int: line = index >> 4 base = address & 0xFFF0 if not self.INCREMENTAL_CACHE_FILL: for i in range(16): self.cache[line << 4 | i] = self._fetch_memory((base + i) & 0xFFFF) self.cache_loaded[line] = 16 return self.cache[index] if self.filling >= 0 and self.filling != line: self._finish_line() # BK2 2-6-10: leaving a partly loaded line finishes it first position = index & 15 loaded = self.cache_loaded[line] if position < loaded: # already loaded in this partial line self.clock += timing.CACHE_FETCH[self.clsr] return self.cache[index] for i in range(loaded, position + 1): # leading bytes first (figure 2-6-1), then this self.cache[line << 4 | i] = self._fetch_memory((base + i) & 0xFFFF) self.cache_loaded[line] = position + 1 self.filling = line if position < 15 else -1 return self.cache[index] def _finish_line(self) -> None: line = self.filling self.filling = -1 loaded = self.cache_loaded[line] if loaded == 16: return # The GSU address of the line: the one inside CBR..CBR+511 whose low 9 bits match. base = (self.cbr + (((line << 4) - self.cbr) & 0x1FF)) & 0xFFFF for i in range(loaded, 16): self.cache[line << 4 | i] = self._fetch_memory((base + i) & 0xFFFF) self.cache_loaded[line] = 16 def _pipe(self) -> int: """An operand byte: the pipeline byte, then R15 + 1 is fetched behind it.""" r15 = (self.r[15] + 1) & 0xFFFF self.r[15] = r15 value = self.pipeline self.pipeline_address = self.pbr << 16 | r15 self.pipeline = self._fetch(r15) return value def _flush_cache(self) -> None: """Every line invalid (CACHE with a new CBR, LJMP, the S-CPU's GO = 0 or PBR write).""" self.cache_loaded[:] = [0] * 32 self.filling = -1 def _op_cache(self, n: int) -> None: """CACHE (BK2 9.27): CBR = R15 AND FFF0h (R15 = the address after CACHE); the lines are invalidated only when that changes CBR (U15).""" base = self.r[15] & 0xFFF0 if base != self.cbr: self.cbr = base self._flush_cache() # ----- the S-CPU window ($3100-$32FF) ---------------------------------------------------- def _window_index(self, offset: int) -> int: if self.CACHE_WINDOW_BY_ADDRESS: return offset return (offset + 2 * self.cbr) & 0x1FF # the emulators' CBR-relative buffer def read_cache_window(self, offset: int) -> int: """The cache RAM byte the S-CPU reads at ``$3100 + offset`` (no access check).""" return self.cache[self._window_index(offset & 0x1FF)] def write_cache_window(self, offset: int, value: int) -> None: """An S-CPU write at ``$3100 + offset``. Writing a line's last byte (``xxxFh``) marks the line valid (BK2 6.8.3: "To set the cache flag, write any data to the XXXFH address of that block"; U14).""" index = self._window_index(offset & 0x1FF) self.cache[index] = value & 0xFF if index & 15 == 15: self.cache_loaded[index >> 4] = 16 if self.filling == index >> 4: self.filling = -1 __all__ = ["CacheMixin"]