# SPDX-License-Identifier: MIT """Bitmap emulation: COLOR, CMODE, GETC, PLOT, RPIX and the two pixel caches (BK2 8.1). PLOT writes COLR at (R1, R2) (their low bytes) into SNES character data at SCBR, then adds 1 to R1. The character number and address (BK2 8.1.5 with C5's misprints corrected; fullsnes "Bitmap I/O Ports"; ares ``core.cpp`` ``rpix``): 128 high: CN = X/8 x 10h + Y/8 OBJ: CN = Y[7] x 200h + X[7] x 100h 160 high: CN = X/8 x 14h + Y/8 + Y[6:3] x 10h + X[6:3] 192 high: CN = X/8 x 18h + Y/8 address = SCBR x 400h + CN x (10h | 20h | 40h for 2/4/8 bits) + (Y AND 7) x 2, bit planes 0-1 at +0/+1, 2-3 at +10h/+11h, 4-5 at +20h/+21h, 6-7 at +30h/+31h. **The pixel caches** (BK2 8.1.3; U20). The primary cache holds one 8-pixel row of one character (X AND F8h, Y) with a flag per plotted pixel. A PLOT to another row, or a full row, hands it to the secondary cache, which is written to RAM **while later instructions run**; only a hand-over that finds the secondary still busy waits (C15: the emulators write it at once and charge the PLOT instead, ``OVERLAPPED_PIXEL_WRITEBACK = False``). A partial row is merged into RAM with a read before each plane's write ("the pixels ... which have not been plotted are written after a game pak RAM read", BK2 example 2). The core writes the RAM at the hand-over (the values the write-back will leave); the time is the secondary's. **RPIX** flushes both caches, waiting, then reads the pixel from RAM, never from a cache (BK2 8.1.3.3). """ from __future__ import annotations from gsu_python import timing from gsu_python._sfr import S, Z _SZ = ~(S | Z) #: Bits per pixel by SCMR MD (0 = 4 colours, 1 = 16, 2 = "not used": 16 as in ares #: ``core.cpp`` l.59, 3 = 256). DEPTHS = (2, 4, 4, 8) #: Byte offset of each bit plane in a character row. PLANE_OFFSETS = (0x00, 0x01, 0x10, 0x11, 0x20, 0x21, 0x30, 0x31) class PlotMixin: r: list[int] sfr: int sreg: int dreg: int clsr: int clock: int colr: int por: int scbr: int scmr: int ram: bytearray ram_ready: int pixel_ready: int _ram_size: int #: The primary and secondary caches: [row offset (Y x 32 + X/8, -1 none), flag byte, #: then the 8 pixel colours, leftmost pixel last (slot = (X AND 7) XOR 7)]. pixel_primary: list[int] pixel_secondary: list[int] #: Transparency (U20). True (default): with POR bit 0 = 0, a colour whose low 2/4/8 bits #: (by depth; only the low 4 with freeze-high) are zero is not plotted (fullsnes "POR"). #: False: ares ``core.cpp`` ``plot`` (l.12-22), which tests the low 4 bits in #: 4-colour mode too, so colours 4, 8 and 12 are skipped there only by fullsnes. TRANSPARENCY_BY_DEPTH = True #: C15. True (default): the secondary cache goes to RAM in parallel with execution (BK2 #: 8.1.3). False: the write-back stalls the PLOT that hands over (ares #: ``core.cpp`` l.31-45, l.93-99). OVERLAPPED_PIXEL_WRITEBACK = True # ----- COLOR, GETC, CMODE (BK2 9.28, 9.30, 9.39; figure 2-8-5) --------------------------- def _color(self, source: int) -> int: """What COLOR/GETC put in COLR: POR bit 2 (high nibble) takes the source's high nibble into the low one; POR bit 3 (freeze high) keeps COLR's high nibble.""" por = self.por if por & 0x04: return (self.colr & 0xF0) | (source >> 4) if por & 0x08: return (self.colr & 0xF0) | (source & 0x0F) return source def _op_color(self, n: int) -> None: self.colr = self._color(self.r[self.sreg] & 0xFF) def _op_getc(self, n: int) -> None: """GETC: COLR from the ROM buffer (waiting for it as GETB does).""" self.colr = self._color(self._read_rom_buffer()) def _op_cmode(self, n: int) -> None: """CMODE: POR = Sreg bits 4-0.""" self.por = self.r[self.sreg] & 0x1F # ----- addressing ------------------------------------------------------------------------ def _depth(self) -> int: return DEPTHS[self.scmr & 3] def _row_address(self, x: int, y: int) -> int: """The RAM offset (from 70:0000h) of plane 0 of the row holding pixel (x, y).""" scmr = self.scmr height = (scmr >> 4 & 2) | (scmr >> 2 & 1) if self.por & 0x10 or height == 3: # OBJ mode (POR bit 4 or SCMR HT = 3) cn = (y & 0x80) << 2 | (x & 0x80) << 1 | (y & 0x78) << 1 | (x & 0x78) >> 3 else: cn = (x >> 3) * (0x10, 0x14, 0x18)[height] + (y >> 3) return self.scbr * 0x400 + cn * self._depth() * 8 + (y & 7) * 2 # ----- PLOT ------------------------------------------------------------------------------ def _op_plot(self, n: int) -> None: """PLOT (BK2 9.72): plot COLR at (R1, R2), then R1 = R1 + 1.""" x = self.r[1] & 0xFF y = self.r[2] & 0xFF color = self.colr depth = self._depth() plotted = True if not self.por & 0x01: # transparency on test = color & 0x0F if self.por & 0x08 else color if self.TRANSPARENCY_BY_DEPTH: plotted = bool(test & (0xFF >> (8 - depth))) else: plotted = bool(test if depth == 8 else color & 0x0F) if plotted: if self.por & 0x02 and depth != 8: # dither: high nibble on odd (X xor Y) if (x ^ y) & 1: color >>= 4 color &= 0x0F primary = self.pixel_primary offset = y << 5 | x >> 3 if offset != primary[0]: if primary[1]: self._hand_over() primary[0] = offset slot = (x & 7) ^ 7 primary[2 + slot] = color primary[1] |= 1 << slot if primary[1] == 0xFF: self._hand_over() self._set(1, (self.r[1] + 1) & 0xFFFF) def _write_row(self, cache: list[int]) -> int: """Write a cache's row to RAM (merging a partial row); return its time.""" flags = cache[1] offset = cache[0] address = self._row_address((offset & 31) << 3, offset >> 5) access = timing.MEMORY_ACCESS[self.clsr] cost = 0 for plane in range(self._depth()): byte = 0 for slot in range(8): byte |= (cache[2 + slot] >> plane & 1) << slot if self._ram_size: index = (address + PLANE_OFFSETS[plane]) % self._ram_size if flags != 0xFF: byte = (byte & flags) | (self.ram[index] & ~flags & 0xFF) self.ram[index] = byte cost += 2 * access if flags != 0xFF else access return cost def _hand_over(self) -> None: """Primary -> secondary (BK2 8.1.3.1), waiting while the secondary is still busy.""" if self.clock < self.pixel_ready: self.clock = self.pixel_ready primary = self.pixel_primary self.pixel_secondary[:] = primary cost = self._write_row(primary) if self.OVERLAPPED_PIXEL_WRITEBACK: start = self.clock if self.clock > self.ram_ready else self.ram_ready self.pixel_ready = start + cost else: self.clock += cost primary[1] = 0 # ----- RPIX ------------------------------------------------------------------------------ def _op_rpix(self, n: int) -> None: """RPIX (BK2 9.77; U21): flush both caches (waiting), then Dreg = the pixel at (R1, R2) read from RAM, one access per plane. S from bit 15 (the emulators: always 0; BK2's "negative" cannot happen for an 8-bit result: undecidable here), Z.""" if self.clock < self.pixel_ready: self.clock = self.pixel_ready primary = self.pixel_primary if primary[1]: self.pixel_secondary[:] = primary self.clock += self._write_row(primary) primary[1] = 0 self._sync_ram() x = self.r[1] & 0xFF y = self.r[2] & 0xFF address = self._row_address(x, y) shift = (x & 7) ^ 7 access = timing.MEMORY_ACCESS[self.clsr] value = 0 for plane in range(self._depth()): self.clock += access if self._ram_size: byte = self.ram[(address + PLANE_OFFSETS[plane]) % self._ram_size] value |= (byte >> shift & 1) << plane self._set(self.dreg, value) self.sfr = (self.sfr & _SZ) | (S if value & 0x8000 else 0) | (0 if value else Z) __all__ = ["DEPTHS", "PLANE_OFFSETS", "PlotMixin"]