# SPDX-License-Identifier: MIT """The GSU's own memory map, the ROM buffer, the RAM write buffer and the RAM address register. The map is the chip's address pins (BK2 3.2; fullsnes "GSU2 Memory Map (at GSU Side)"; U13): 00-3F:8000-FFFF ROM, LoROM order 00-3F:0000-7FFF the same ROM halves again 40-5F:0000-FFFF ROM, linear (HiROM order) 70-71:0000-FFFF Game Pak RAM PBR and ROMBR are 7 bits wide (ares ``io.cpp`` l.93, ``instructions.cpp`` l.378; MAME); a bank outside the map reads 0 (ares ``memory.cpp`` l.29 with ``superfx.hpp`` l.61's default). The core indexes the board's ``bytes``/``bytearray`` modulo their lengths (handoff-brief bus contract 5: no copy, no callback). **Values vs time.** The buffers decide *when* an instruction may continue, never *what* it reads: the ROM buffer holds ``[ROMBR:R14]`` as of the R14 write (ROMB waits for a pending read, so a later bank change cannot reach it: U7), and a store reaches RAM at once (every GSU-side RAM access waits for the write buffer first, BK2 7.2.1). Time is kept in absolute input clocks: ``rom_ready``, ``ram_ready`` and ``pixel_ready`` are the clock at which each background transfer completes. """ from __future__ import annotations from gsu_python import timing class MemoryMixin: r: list[int] sfr: int sreg: int dreg: int clsr: int clock: int rombr: int rambr: int ramaddr: int romdr: int rom_ready: int ram_ready: int pixel_ready: int rom: bytes ram: bytearray _rom_size: int _ram_size: int #: C13. True (default, BK2 examples 5 and 6): a RAM data read costs one memory access #: per byte inside the instruction (LM = 4 fetches + 10 cycles at 21.4 MHz). False: free #: (ares ``timing.cpp`` l.39-42 ``readRAMBuffer``; MAME). RAM_READS_CHARGED = True #: C13. True (default, BK2 examples 3 and 4): a word store is handed to the write buffer #: whole and occupies it for two accesses; the next store waits for it. False: the #: emulators' one-byte buffer, so a word store already waits for its own first byte. WORD_WRITE_BUFFER = True # ----- the map --------------------------------------------------------------------------- def _read(self, bank: int, address: int) -> int: """One byte of the GSU's map, no time (U13).""" if bank < 0x40: # LoROM: bank bits 5-0 and A14-A0; A15 ignored (the 0000-7FFF mirror) return self.rom[((bank & 0x3F) << 15 | (address & 0x7FFF)) % self._rom_size] if bank < 0x60: # linear return self.rom[((bank & 0x1F) << 16 | address) % self._rom_size] if bank in (0x70, 0x71) and self._ram_size: return self.ram[((bank & 1) << 16 | address) % self._ram_size] return 0 def _ram_index(self, address: int) -> int: """Where ``[70h + RAMBR : address]`` is in the RAM buffer.""" return (self.rambr << 16 | address) % self._ram_size # ----- waits ----------------------------------------------------------------------------- def _sync_rom(self) -> None: """Wait for a pending ROM buffer read (BK2 7.1.1: GETxx and ROMB; 7.1.2: an opcode fetch from ROM).""" if self.clock < self.rom_ready: self.clock = self.rom_ready def _sync_ram(self) -> None: """Wait until the RAM is free: the write buffer (BK2 7.2.1-7.2.2) and the pixel write-back (one RAM bus: the core's model, C15).""" ready = self.ram_ready if self.ram_ready > self.pixel_ready else self.pixel_ready if self.clock < ready: self.clock = ready # ----- the ROM buffer (BK2 7.1; U7, U28) ------------------------------------------------- def _start_rom_buffer(self) -> None: """An R14 write (by an instruction, at its end; or by the S-CPU, U9) starts a read of ``[ROMBR:R14]``; SFR.R reads 1 until it completes; a new write restarts it.""" self.romdr = self._read(self.rombr, self.r[14]) self.rom_ready = self.clock + timing.MEMORY_ACCESS[self.clsr] def _read_rom_buffer(self) -> int: self._sync_rom() return self.romdr def _op_getb(self, n: int) -> None: """GETB (BK2 9.35): Dreg = the buffered byte, zero-extended.""" self._set(self.dreg, self._read_rom_buffer()) def _op_getbh(self, n: int) -> None: """GETBH (BK2 9.36): Dreg = byte : Sreg low byte.""" self._set(self.dreg, self._read_rom_buffer() << 8 | (self.r[self.sreg] & 0xFF)) def _op_getbl(self, n: int) -> None: """GETBL (BK2 9.37): Dreg = Sreg high byte : byte.""" self._set(self.dreg, (self.r[self.sreg] & 0xFF00) | self._read_rom_buffer()) def _op_getbs(self, n: int) -> None: """GETBS (BK2 9.38): Dreg = the byte, sign-extended (U32).""" value = self._read_rom_buffer() self._set(self.dreg, value | 0xFF00 if value & 0x80 else value) def _op_romb(self, n: int) -> None: """ROMB (BK2 9.75; U7): wait for a pending ROM buffer read, then ROMBR = Sreg (7 bits, ares/MAME; fullsnes "Rs & FFh").""" self._sync_rom() self.rombr = self.r[self.sreg] & 0x7F def _op_ramb(self, n: int) -> None: """RAMB (BK2 9.73; 7.2.1): wait for the write buffer, then RAMBR = Sreg bit 0.""" self._sync_ram() self.rambr = self.r[self.sreg] & 1 # ----- RAM reads (BK2 7.3; C13; U17) --------------------------------------------------- def _ram_byte(self, address: int) -> int: if self.RAM_READS_CHARGED: self.clock += timing.MEMORY_ACCESS[self.clsr] return self.ram[self._ram_index(address)] if self._ram_size else 0 def _ram_word(self, address: int) -> int: """A word at ``address``: the low byte there, the high byte at ``address ^ 1`` (U17: at an odd address the bytes come from the even word, swapped).""" self._sync_ram() low = self._ram_byte(address) return self._ram_byte(address ^ 1) << 8 | low def _op_ldw(self, n: int) -> None: """LDW (Rn) (BK2 9.46): Dreg = the word at Rn.""" self.ramaddr = self.r[n] self._set(self.dreg, self._ram_word(self.ramaddr)) def _op_ldb(self, n: int) -> None: """LDB (Rn) (BK2 9.45): Dreg = the byte at Rn, zero-extended.""" self.ramaddr = self.r[n] self._sync_ram() self._set(self.dreg, self._ram_byte(self.ramaddr)) def _op_lm(self, n: int) -> None: """LM Rn,(xx) (BK2 9.50): Rn = the word at xx (16-bit address).""" low = self._pipe() self.ramaddr = self._pipe() << 8 | low self._set(n, self._ram_word(self.ramaddr)) def _op_lms(self, n: int) -> None: """LMS Rn,(yy) (BK2 9.51; U32): Rn = the word at yy x 2 (0-510).""" self.ramaddr = self._pipe() << 1 self._set(n, self._ram_word(self.ramaddr)) # ----- stores through the write buffer (BK2 7.2; C13; U17, U29) -------------------------- def _store(self, address: int, value: int, word: bool) -> None: """Hand a byte or word to the write buffer: wait for the previous store, write the RAM, keep the buffer busy for its accesses (overlapping later instructions).""" access = timing.MEMORY_ACCESS[self.clsr] if self._ram_size: self.ram[self._ram_index(address)] = value & 0xFF if word: self.ram[self._ram_index(address ^ 1)] = value >> 8 & 0xFF if self.WORD_WRITE_BUFFER: if self.clock < self.ram_ready: self.clock = self.ram_ready start = self.clock if self.clock > self.pixel_ready else self.pixel_ready self.ram_ready = start + (2 * access if word else access) return for _ in range(2 if word else 1): # one byte per buffer slot (ares writeRAMBuffer) if self.clock < self.ram_ready: self.clock = self.ram_ready self.ram_ready = self.clock + access def _op_stw(self, n: int) -> None: """STW (Rn) (BK2 9.85): the word Sreg to Rn (high byte at Rn - 1 when Rn is odd).""" self.ramaddr = self.r[n] self._store(self.ramaddr, self.r[self.sreg], True) def _op_stb(self, n: int) -> None: """STB (Rn) (BK2 9.83): Sreg's low byte to Rn.""" self.ramaddr = self.r[n] self._store(self.ramaddr, self.r[self.sreg], False) def _op_sm(self, n: int) -> None: """SM (xx),Rn (BK2 9.81): the word Rn to xx.""" low = self._pipe() self.ramaddr = self._pipe() << 8 | low self._store(self.ramaddr, self.r[n], True) def _op_sms(self, n: int) -> None: """SMS (yy),Rn (BK2 9.82): the word Rn to yy x 2.""" self.ramaddr = self._pipe() << 1 self._store(self.ramaddr, self.r[n], True) def _op_sbk(self, n: int) -> None: """SBK (BK2 9.79; U29): the word Sreg back to the last RAM address an LM, LMS, LDW, LDB, STW, STB, SM or SMS used (ares ``regs.ramaddr``; BK2 7.3).""" self._store(self.ramaddr, self.r[self.sreg], True) __all__ = ["MemoryMixin"]