# SPDX-License-Identifier: MIT AND ISC """The gate array's graphics: the font converter ($FF804C-$FF8056) and the stamp rotation and scaling unit ($FF8058-$FF8066). Sources: Sega's *Mega-CD Hardware Manual* (1991), p.34 (font) and p.35-54 (rotation), as read in docs/manual-registers.md (DOC). Where the scan leaves the arithmetic open -- the order of the stamp transforms, the duration of an operation -- ares 9408cb4 (``ares/md/mcd/gpu.cpp``, ISC; notice in NOTICE) was read and its behaviour used (OBSERVED: an emulator fact, not a hardware fact). The code is written from that understanding, not transcribed. """ from __future__ import annotations from typing import TYPE_CHECKING from .wiring import unplugged if TYPE_CHECKING: from .memory import WordRAM #: Sub CPU clocks per pixel of an image-buffer line. The manual's duration formula #: (p.53) is not reliably legible; ares takes 5 clocks a horizontal dot (OBSERVED), #: used here as the documented default. FITTED nothing yet; provisional. CLOCKS_PER_DOT = 5 class Graphics: def __init__(self, wram: WordRAM) -> None: self.wram = wram #: Set by the device: the gate array's ``request`` (INT1) and its PM1-0 / mode. self.request = unplugged self.priority = unplugged self.in_2m = unplugged self.reset() def reset(self) -> None: self.font_color = 0 # $FF804C low byte: bits 7-4 for a 1 bit, 3-0 for a 0 bit self.font_bits = 0 # $FF804E self.repeat = 0 # RPT self.stamp_size = 0 # STS: 0 = 16x16 dots, 1 = 32x32 self.map_size = 0 # SMS: 0 = 1x1 screen (256 dots), 1 = 16x16 screens (4096) self.map_base = 0 # $FF805A as written self.vcells = 0 # VCS4-0 (cells - 1) self.image_base = 0 # $FF805E as written self.offset = 0 # LN2-0, DOT2-0 self.hdots = 0 # HW8-0 self.vdots = 0 # VW7-0, counts down during an operation (p.35) self.vector_base = 0 # $FF8066 as written self.busy = False # GRON self.clocks = 0 # sub clocks into the current line self.image_address = 0 # nibble address of the next line's first pixel self.vector_address = 0 # byte address of the next trace-table line # -- registers -------------------------------------------------------------- def read(self, offset: int) -> int: if offset == 0x4C: return self.font_color if offset == 0x4E: return self.font_bits if 0x50 <= offset < 0x58: # Each bit of $FF804E becomes a nibble: the colour in bits 7-4 for a 1, in # bits 3-0 for a 0; $FF8050 holds font bits 15-12, $FF8056 bits 3-0 (p.34). # DOC. one, zero = self.font_color >> 4, self.font_color & 0xF first = 12 - ((offset - 0x50) << 1) word = 0 for k in range(3, -1, -1): word = (word << 4) | (one if self.font_bits >> (first + k) & 1 else zero) return word if offset == 0x58: return (self.busy << 15) | (self.map_size << 2) | (self.stamp_size << 1) | self.repeat if offset == 0x5A: return self.map_base if offset == 0x5C: return self.vcells if offset == 0x5E: return self.image_base if offset == 0x60: return self.offset if offset == 0x62: return self.hdots if offset == 0x64: return self.vdots # $FF8066 is write-only (p.35); it reads 0 here: HYPOTHESIS. return 0 def write(self, offset: int, data: int, upper: bool, lower: bool) -> None: if offset == 0x4C: # One byte from the data lines (gatearray.py's docstring). self.font_color = data & 0xFF elif offset == 0x4E: if upper: self.font_bits = (self.font_bits & 0x00FF) | (data & 0xFF00) if lower: self.font_bits = (self.font_bits & 0xFF00) | (data & 0x00FF) elif offset == 0x58: if lower: self.repeat = data & 1 self.stamp_size = (data >> 1) & 1 self.map_size = (data >> 2) & 1 elif offset == 0x5A: # Bit 15 is A17, and the low bits a size cannot use are 0 (p.35-36). DOC. self.map_base = data & 0xFFE0 elif offset == 0x5C: self.vcells = data & 0x1F elif offset == 0x5E: self.image_base = data & 0xFFF8 # A17-A5 (p.35). DOC. elif offset == 0x60: self.offset = data & 0x3F elif offset == 0x62: self.hdots = data & 0x1FF elif offset == 0x64: self.vdots = data & 0xFF elif offset == 0x66: # "Coordinate conversion ... are begun when this location is set" (p.35). self.vector_base = data & 0xFFFE self.start() # -- the operation -------------------------------------------------------------- def start(self) -> None: """Begin an operation. Level 1 is for "only 2M mode" (p.15, p.30): in 1M no operation runs here (ares does the same; OBSERVED). DOC.""" if not self.in_2m(): return self.busy = True self.clocks = 0 # $FF805E holds A17-A5 (address / 4); LN and DOT place the first pixel in its # cell (p.35): a nibble address = byte address x 2. self.image_address = ((self.image_base << 2) << 1) + self.offset self.vector_address = self.vector_base << 2 def run(self, clocks: int) -> None: if not self.busy: return self.clocks += clocks line = CLOCKS_PER_DOT * max(self.hdots, 1) while self.busy and self.clocks >= line: self.clocks -= line self.render_line() self.image_address += 8 # the next line in the cell (8 pixels a line) self.vdots = (self.vdots - 1) & 0xFF if self.vdots == 0: # "When these operations complete, a LEVEL1 interrupt will be # generated" (p.35); "the Vdot size value becomes 0" (p.35). DOC. self.busy = False self.request(1) def _word(self, address: int) -> int: data = self.wram.data address &= 0x3FFFE return (data[address] << 8) | data[address + 1] def render_line(self) -> None: wram = self.wram # Trace vector table line: X start, Y start (13.3 fixed point), dX, dY (sign, # 4 integer, 11 fraction bits) (p.39-40). DOC. Kept as 11-bit fractions. at = self.vector_address x = self._word(at) << 8 y = self._word(at + 2) << 8 dx = self._word(at + 4) dy = self._word(at + 6) dx -= (dx & 0x8000) << 1 dy -= (dy & 0x8000) << 1 self.vector_address = at + 8 big = self.stamp_size # 32x32 stamps stamp_shift = 5 if big else 4 # log2 of a stamp's side # Map width in stamps: a 1x1 screen is 256 dots, 16x16 screens 4096 (p.35). map_dots_log2 = 12 if self.map_size else 8 map_shift = map_dots_log2 - stamp_shift map_mask = (1 << (map_dots_log2 + 11)) - 1 pixel_mask = (1 << stamp_shift) - 1 # The stamp map's base: $FF805A is the address / 4, only the bits a size can # use (p.36; manual-registers.md's assignment). DOC (derived). usable = {(0, 0): 0x3FE00, (0, 1): 0x3FF80, (1, 1): 0x20000, (1, 0): 0x38000} map_address = (self.map_base << 2) & usable[(self.map_size, big)] number_mask = 0x7FC if big else 0x7FF # SNO1-0 are 0 for 32x32 stamps (p.36) priority = self.priority() cell_stride = (self.vcells + 1) << 6 # nibbles in a column of cells address = self.image_address for _ in range(self.hdots): if self.repeat: x &= map_mask y &= map_mask pixel = 0 if not (x | y) & ~map_mask: px, py = x >> 11, y >> 11 entry = self._word(map_address + (((py >> stamp_shift) << map_shift) + (px >> stamp_shift)) * 2) # fmt: skip number = entry & number_mask # Stamp number 0 is "no character" (p.36). DOC. if number: # HFLP then RT1-0 (p.42); the transforms' order and direction are # ares's (OBSERVED): flip x, then 180 degrees, then 90. if entry & 0x8000: px = ~px if entry & 0x4000: px, py = ~px, ~py if entry & 0x2000: px, py = ~py, px sx, sy = px & pixel_mask, py & pixel_mask # A stamp is MD cells in columns from the start of word RAM; a # 16x16 stamp is $80 bytes (p.36-37). DOC. pixel = wram.read_nibble( (number << 8) | ((sx & ~7) << stamp_shift) | (sy << 3) | (sx & 7) ) old = wram.read_nibble(address) # PM1-0 on the image buffer (p.23): 1 underwrite (only where the buffer # is 0), 2 overwrite (only non-zero stamp pixels). DOC. 3 is "Prohibited"; # it leaves the buffer as it was (ares, OBSERVED). if priority == 1: pixel = old if old else pixel elif priority == 2: pixel = pixel if pixel else old elif priority == 3: pixel = old wram.write_nibble(address, pixel) address += 1 if not address & 7: address += cell_stride - 8 x += dx y += dy __all__ = ["CLOCKS_PER_DOT", "Graphics"]