# SPDX-License-Identifier: MIT AND ISC """S-PPU1 and S-PPU2: the B-bus registers $2100-$213F and the picture. The programming model (milestone 1): VRAM, OAM and CGRAM through their ports with the address rules, the read latches, the H/V counter latch, the mode 7 product at $2134-$2136, STAT77/STAT78 and the two chips' own open-bus latches; ``raw`` keeps one byte per $21xx write for the probe decoders. The picture (milestone 2) is drawn dot by dot on ares's cycle schedule (``sfc/ppu/main.cpp``: sprite range checks every 8 clocks, BG fetches every 4, two half-dots per pixel from H=56, the sliver fetch from H=1080), lazily: the board calls ``sync()`` before every $21xx access and ``end_line()`` at each line's end, and the PPU runs its schedule up to the board's clock, so a mid-line write lands where ares puts it. The units are in ``ppu_render.py``. Output: ``frame_buffer`` (512 x 478 raw DAC words, see ``ppu_render.DAC``) and ``picture()``, which gives the visible frame as RGB: 256 or 512 wide (512 when any line of it was hi-res or pseudo-hi-res), 224 or 239 lines (overscan), doubled when interlaced; ``write_png(path)`` saves it. Sources. DOC: fullsnes "SNES PPU Registers" and its port sections, anomie regs (``reference/docs/anomie_regs.txt``), wiki/PPU registers. The register behaviour follows ares 9408cb4 ``sfc/ppu/io.cpp`` (ISC, notice below and in NOTICE), where fullsnes leaves a detail open; each such rule says so. ares notice (ISC): Copyright (c) 2004-2025 ares team, Near et al. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. """ from __future__ import annotations from typing import TYPE_CHECKING from snes_python.ppu_png import write_png from snes_python.ppu_render import ( BPP2, BPP4, BPP8, DAC, INACTIVE, MODE7, Background, Mosaic, Objects, Window, ) if TYPE_CHECKING: from snes_python.board import SNES # DOC: fullsnes "2115h - VMAIN": increment 1, 32, 128, 128 words. _INCREMENTS = (1, 32, 128, 128) # Registers whose read returns PPU1's open-bus latch (DOC: fullsnes "SNES PPU # Registers": write-only registers in $2104-$212A of PPU1 read its MDR; # OBSERVED in ares sfc/ppu/io.cpp readIO, the same list). _PPU1_OPEN = frozenset( (0x04, 0x05, 0x06, 0x08, 0x09, 0x0A, 0x14, 0x15, 0x16, 0x18, 0x19, 0x1A, 0x24, 0x25, 0x26, 0x28, 0x29, 0x2A) ) # BG depth and the priorities per BG mode: (BG1-4 depth, BG1-4 priority for # tile priority 0/1, OBJ priority 0-3). Larger is in front; 0 is reserved for # "nothing" (DOC: fullsnes "2105h - BGMODE" and the layer order table in # "SNES PPU Color-Math"; the numbering is ares's updateVideoMode, OBSERVED). _N = INACTIVE _VIDEO_MODES = { 0: ((BPP2, BPP2, BPP2, BPP2), ((8, 11), (7, 10), (2, 5), (1, 4)), (3, 6, 9, 12)), 1: ((BPP4, BPP4, BPP2, _N), ((6, 9), (5, 8), (1, 3), (0, 0)), (2, 4, 7, 10)), # Mode 1 with BGMODE bit 3: BG3 priority-1 tiles in front of everything. 8: ((BPP4, BPP4, BPP2, _N), ((5, 8), (4, 7), (1, 10), (0, 0)), (2, 3, 6, 9)), 2: ((BPP4, BPP4, _N, _N), ((3, 7), (1, 5), (0, 0), (0, 0)), (2, 4, 6, 8)), 3: ((BPP8, BPP4, _N, _N), ((3, 7), (1, 5), (0, 0), (0, 0)), (2, 4, 6, 8)), 4: ((BPP8, BPP2, _N, _N), ((3, 7), (1, 5), (0, 0), (0, 0)), (2, 4, 6, 8)), 5: ((BPP4, BPP2, _N, _N), ((3, 7), (1, 5), (0, 0), (0, 0)), (2, 4, 6, 8)), 6: ((BPP4, _N, _N, _N), ((2, 5), (0, 0), (0, 0), (0, 0)), (1, 3, 4, 6)), 7: ((MODE7, _N, _N, _N), ((2, 2), (0, 0), (0, 0), (0, 0)), (1, 3, 4, 5)), # Mode 7 with EXTBG: BG2 is the 7-bit view, its bit 7 the priority. 15: ((MODE7, MODE7, _N, _N), ((3, 3), (1, 5), (0, 0), (0, 0)), (2, 4, 6, 7)), } # The BG fetch slots of each 32-clock column, per BG mode: (BG, kind, a, b) # with kind N = name table, C = character (a = plane pair, b = hi-res half), # O = offset-per-tile (a = row 0 or 8) (OBSERVED: ares cycleBackgroundFetch). _FETCH = { 0: ((3, "N", 0, 0), (2, "N", 0, 0), (1, "N", 0, 0), (0, "N", 0, 0), (3, "C", 0, 0), (2, "C", 0, 0), (1, "C", 0, 0), (0, "C", 0, 0)), 1: ((2, "N", 0, 0), (1, "N", 0, 0), (0, "N", 0, 0), (2, "C", 0, 0), (1, "C", 0, 0), (1, "C", 1, 0), (0, "C", 0, 0), (0, "C", 1, 0)), 2: ((1, "N", 0, 0), (0, "N", 0, 0), (2, "O", 0, 0), (2, "O", 8, 0), (1, "C", 0, 0), (1, "C", 1, 0), (0, "C", 0, 0), (0, "C", 1, 0)), 3: ((1, "N", 0, 0), (0, "N", 0, 0), (1, "C", 0, 0), (1, "C", 1, 0), (0, "C", 0, 0), (0, "C", 1, 0), (0, "C", 2, 0), (0, "C", 3, 0)), 4: ((1, "N", 0, 0), (0, "N", 0, 0), (2, "O", 0, 0), (1, "C", 0, 0), (0, "C", 0, 0), (0, "C", 1, 0), (0, "C", 2, 0), (0, "C", 3, 0)), 5: ((1, "N", 0, 0), (0, "N", 0, 0), (1, "C", 0, 0), (1, "C", 0, 1), (0, "C", 0, 0), (0, "C", 1, 0), (0, "C", 0, 1), (0, "C", 1, 1)), 6: ((1, "N", 0, 0), (0, "N", 0, 0), (2, "O", 0, 0), (2, "O", 8, 0), (0, "C", 0, 0), (0, "C", 1, 0), (0, "C", 0, 1), (0, "C", 1, 1)), 7: ((), (), (), (), (), (), (), ()), } # fmt: skip # 5-bit channel at brightness N to 8 bits: c * (N + 1) / 16, 0 = black (DOC: # fullsnes "2100h - INIDISP"; U10 asks the console for the real curve). _LEVELS = [ [0 if n == 0 else ((c << 3 | c >> 2) * (n + 1) + 8) // 16 for c in range(32)] for n in range(16) ] def _signed16(value: int) -> int: return value - 0x10000 if value & 0x8000 else value def _signed8(value: int) -> int: return value - 0x100 if value & 0x80 else value class PPU: """The two PPU chips' register file and memories.""" def __init__(self, board: SNES, ppu1_version: int = 1, ppu2_version: int = 3) -> None: self.board = board self.ppu1_version = ppu1_version self.ppu2_version = ppu2_version # 64 KiB VRAM as 32 Ki words; 512+32 bytes OAM; 256 words CGRAM. # HYPOTHESIS: power-on contents are zero (the console's are unknown # here; ares randomises them). A probe reading them before any write # would settle what a real unit holds. self.vram = [0] * 0x8000 self.oam = bytearray(544) self.cgram = [0] * 256 # One byte per $21xx register written (the probe decoders read it). self.raw = bytearray(0x40) self.mosaic = Mosaic(self) self.bg1 = Background(self, 0) self.bg2 = Background(self, 1) self.bg3 = Background(self, 2) self.bg4 = Background(self, 3) self.bgs = (self.bg1, self.bg2, self.bg3, self.bg4) self.obj = Objects(self) self.window = Window(self) self.dac = DAC(self) # Per BG mode, per fetch slot: the bound fetch (none in mode 7). self._fetch_tables = [ tuple((self._fetch_call(e),) if e else () for e in _FETCH[mode]) for mode in range(8) ] self.power() def power(self) -> None: # HYPOTHESIS: latches and addresses start at 0 (ares randomises them; # fullsnes does not state them). self.ppu1_mdr = 0 self.ppu2_mdr = 0 # INIDISP: forced blank at power-on (DOC: fullsnes "2100h - INIDISP", # "forced blank" after reset; ares power()). self.display_disable = True self.brightness = 0 self.oam_base_address = 0 self.oam_address = 0 self.oam_priority = False self.first_sprite = 0 self.range_over = False self.time_over = False self.vram_increment_mode = 1 self.vram_mapping = 0 self.vram_increment = 1 self.vram_address = 0 self.m7a = self.m7b = self.m7c = self.m7d = 0 self.m7x = self.m7y = 0 self.m7_hoffset = self.m7_voffset = 0 self.cgram_address = 0 self.cgram_address_latch = 0 self.interlace = False self.overscan = False self.pseudo_hires = False self.extbg = False self.bg_mode = 0 self.bg_priority = False self.bg_hires = 0 self.m7_repeat = 0 self.m7_hflip = False self.m7_vflip = False self.latched_h = 0 self.latched_v = 0 self.latch_vram = 0 self.latch_oam = 0 self.latch_cgram = 0 self.latch_bgofs1 = 0 self.latch_bgofs2 = 0 self.latch_mode7 = 0 self.latch_counters = False self.latch_h_flip = 0 self.latch_v_flip = 0 # The PPU's own OAM and CGRAM fetch addresses while it draws (the # targets of mid-frame port accesses, milestone 2 drives them). self.render_oam_address = 0 self.render_cgram_address = 0 # The picture's own position: the line being drawn and the clock in # it up to which the schedule has run; the sliver fetch in progress. self.line = 0 self.hpos = 0 self.fetch_gen = None self.frames = 0 self._update_video_mode() def _fetch_call(self, entry: tuple) -> tuple: bg_index, kind, a, b = entry bg = self.bgs[bg_index] if kind == "N": return (bg.fetch_name_table, a, b) if kind == "O": return (bg.fetch_offset, a, b) return (bg.fetch_character, a, b) def _update_video_mode(self) -> None: """BG depths and layer priorities for BGMODE/SETINI (ares updateVideoMode, OBSERVED; DOC: fullsnes "2105h - BGMODE").""" mode = self.bg_mode key = mode if mode == 1 and self.bg_priority: key = 8 elif mode == 7 and self.extbg: key = 15 depths, priorities, obj = _VIDEO_MODES[key] for bg, depth, (p0, p1) in zip(self.bgs, depths, priorities): bg.mode = depth bg.priority0 = p0 bg.priority1 = p1 self.obj.priority[:] = obj self.bg_hires = 1 if mode == 5 or mode == 6 else 0 # -- frame and line hooks (called by the board) ---------------------- def sync(self) -> None: """Catch the picture up to the board's (v, h) before a register access.""" if self.hpos < self.board.h: self._run_to(self.board.h) def end_line(self) -> None: """Finish the line that just ended (called before ``scanline``).""" self._run_to(self.board.last_hperiod) def _run_to(self, target: int) -> None: """Run the line's schedule for every clock before ``target``. OBSERVED (ares PPU::main and PPU::cycle): lines 0-240 run the schedule, later lines nothing. Work at clock H is done once the board's clock has passed H (ares's PPU is synchronised to the CPU before each access the same way). """ h = self.hpos if self.line > 240: self.hpos = target return obj = self.obj bg1, bg2, bg3, bg4 = self.bgs window = self.window dac = self.dac while h < target: if h < 1080: if not h & 3: if not h & 7 and h <= 1016: obj.evaluate(h >> 3) if h <= 1054: for call, a, b in self._fetch_tables[self.bg_mode][(h >> 2) & 7]: call(h, a, b) if h >= 56: if h == 56: bg1.begin() bg2.begin() bg3.begin() bg4.begin() x = (h - 56) >> 2 bg1.run(True, x) bg2.run(True, x) bg3.run(True, x) bg4.run(True, x) elif h >= 58: x = (h - 56) >> 2 bg1.run(False, x) bg2.run(False, x) bg3.run(False, x) bg4.run(False, x) obj.run() window.run() dac.run() h += 2 else: gen = self.fetch_gen if gen is None: h = target break try: h += next(gen) except StopIteration: self.fetch_gen = None h = max(h, target) self.hpos = h @property def vdisp(self) -> int: """The first VBlank line: 225, or 240 with overscan (DOC: wiki/Timing).""" return 240 if self.overscan else 225 def scanline(self, v: int) -> None: """At the start of line ``v`` (OBSERVED: ares PPU::main's line start).""" self.line = v self.hpos = 0 if v == 0: # The frame's interlace and overscan are taken here (ares). self.dac.frame_interlace = self.interlace self.dac.frame_overscan = self.overscan # DOC: fullsnes "213Eh - STAT77": the flags are reset at the end # of VBlank (ares Object::frame at the frame's start: OBSERVED). self.obj.frame() self.mosaic.scanline() for bg in self.bgs: bg.scanline() # Includes the OAM address reload at V=vdisp (DOC: fullsnes "OAM"). self.obj.scanline() self.window.scanline() self.dac.scanline() if v == 240: self.frames += 1 self.fetch_gen = self.obj.fetch() if v <= 240 else None # -- the picture ------------------------------------------------------- @property def frame_buffer(self) -> list[int]: return self.dac.frame_buffer def picture(self) -> tuple[int, int, bytearray]: """The last drawn frame as (width, height, RGB bytes).""" dac = self.dac lines = 239 if dac.frame_overscan else 224 if dac.frame_interlace: rows = list(range(lines * 2)) else: rows = [i << 1 for i in range(lines)] hires = any(dac.row_hires[r] for r in rows) width = 512 if hires else 256 out = bytearray(width * len(rows) * 3) buffer = dac.frame_buffer levels = _LEVELS o = 0 for r in rows: base = r * 512 if hires: span = range(base, base + 512) else: span = range(base + 1, base + 512, 2) for i in span: value = buffer[i] level = levels[(value >> 15) & 15] out[o] = level[value & 31] out[o + 1] = level[(value >> 5) & 31] out[o + 2] = level[(value >> 10) & 31] o += 3 return width, len(rows), out def write_png(self, path) -> None: width, height, rgb = self.picture() write_png(path, width, height, rgb) # -- address helpers ---------------------------------------------------- def _active(self) -> bool: """Rendering: not force-blanked and in the picture's lines.""" return not self.display_disable and self.board.v < self.vdisp def _vram_word_address(self) -> int: """VMAIN's address remapping (DOC: fullsnes "2115h - VMAIN" bits 2-3).""" a = self.vram_address mapping = self.vram_mapping if mapping == 0: return a & 0x7FFF if mapping == 1: return (a & 0xFF00 | (a & 0x1F) << 3 | (a >> 5) & 7) & 0x7FFF if mapping == 2: return (a & 0xFE00 | (a & 0x3F) << 3 | (a >> 6) & 7) & 0x7FFF return (a & 0xFC00 | (a & 0x7F) << 3 | (a >> 7) & 7) & 0x7FFF def _read_vram(self) -> int: # DOC: fullsnes "VRAM access": only during VBlank or forced blank; # OBSERVED (ares readVRAM): a read during rendering gives 0. if self._active(): return 0 return self.vram[self._vram_word_address()] def _write_vram(self, high: int, value: int) -> None: # DOC: fullsnes: writes during rendering are ignored. if self._active(): return address = self._vram_word_address() word = self.vram[address] if high: self.vram[address] = word & 0x00FF | value << 8 else: self.vram[address] = word & 0xFF00 | value def _oam_address_reset(self) -> None: self.oam_address = self.oam_base_address self._set_first_sprite() def _set_first_sprite(self) -> None: # DOC: fullsnes "2103h - OAMADDH" bit 7: OBJ priority rotation. self.first_sprite = (self.oam_address >> 2) & 0x7F if self.oam_priority else 0 def _oam_index(self, address: int) -> int: """A 10-bit OAM port address to a byte of ``oam`` (high table mirrored).""" if address & 0x200: return 0x200 | address & 0x1F return address & 0x1FF def _read_oam(self, address: int) -> int: if self._active(): # OBSERVED (ares readOAM): during rendering the access goes where # the PPU is fetching (docs/undocumented-behavior.md Q15). if address & 0x200: return self.oam[0x200 | (self.render_oam_address >> 2) & 0x1F] return self.oam[(self.render_oam_address << 2 | address & 1) & 0x1FF] return self.oam[self._oam_index(address)] def _write_oam(self, address: int, value: int) -> None: if self._active(): if address & 0x200: self.oam[0x200 | (self.render_oam_address >> 2) & 0x1F] = value else: self.oam[(self.render_oam_address << 2 | address & 1) & 0x1FF] = value return self.oam[self._oam_index(address)] = value def _cgram_target(self, address: int) -> int: # OBSERVED (ares readCGRAM/writeCGRAM): while drawing (lines 1 to # vdisp-1, H 88-1095) the access lands at the PPU's own address # (DOC: wiki/Errata, docs/undocumented-behavior.md Q16). board = self.board if ( not self.display_disable and 0 < board.v < self.vdisp and 88 <= board.h < 1096 ): return self.render_cgram_address return address # -- the CPU's B-bus port ------------------------------------------------- def read(self, address: int, mdr: int) -> int: """Read $21xx (address's low 6 bits); ``mdr`` is the CPU's open bus.""" r = address & 0x3F if r in _PPU1_OPEN: return self.ppu1_mdr if r == 0x34 or r == 0x35 or r == 0x36: # DOC: fullsnes "2134h-2136h - MPYL/M/H": signed 16-bit M7A times # the signed 8-bit last byte written to M7B. product = (_signed16(self.m7a) * _signed8(self.m7b >> 8)) & 0xFFFFFF self.ppu1_mdr = (product >> (8 * (r - 0x34))) & 0xFF return self.ppu1_mdr if r == 0x37: # DOC: anomie regs "2137 SLHV": latches only if WRIO bit 7 is set; # the value read is the CPU's open bus. if self.board.wrio & 0x80: self.latch_hv() return mdr if r == 0x38: self.ppu1_mdr = self._read_oam(self.oam_address) self.oam_address = (self.oam_address + 1) & 0x3FF self._set_first_sprite() return self.ppu1_mdr if r == 0x39: # DOC: fullsnes "2139h/213Ah - RDVRAML/H": the read returns the # prefetch latch, then reloads it and increments when the # increment-on byte is read. self.ppu1_mdr = self.latch_vram & 0xFF if self.vram_increment_mode == 0: self.latch_vram = self._read_vram() self.vram_address = (self.vram_address + self.vram_increment) & 0xFFFF return self.ppu1_mdr if r == 0x3A: self.ppu1_mdr = self.latch_vram >> 8 if self.vram_increment_mode == 1: self.latch_vram = self._read_vram() self.vram_address = (self.vram_address + self.vram_increment) & 0xFFFF return self.ppu1_mdr if r == 0x3B: # DOC: fullsnes "213Bh - RDCGRAM": low byte, then 7 bits of high # byte with bit 7 = PPU2 open bus. target = self._cgram_target(self.cgram_address) if self.cgram_address_latch == 0: self.cgram_address_latch = 1 self.ppu2_mdr = self.cgram[target] & 0xFF else: self.cgram_address_latch = 0 self.ppu2_mdr = self.ppu2_mdr & 0x80 | (self.cgram[target] >> 8) & 0x7F self.cgram_address = (self.cgram_address + 1) & 0xFF return self.ppu2_mdr if r == 0x3C: # DOC: anomie regs "213C/213D": read twice, low 8 bits then bit 8 # with the other 7 bits PPU2 open bus. if self.latch_h_flip == 0: self.latch_h_flip = 1 self.ppu2_mdr = self.latched_h & 0xFF else: self.latch_h_flip = 0 self.ppu2_mdr = self.ppu2_mdr & 0xFE | (self.latched_h >> 8) & 1 return self.ppu2_mdr if r == 0x3D: if self.latch_v_flip == 0: self.latch_v_flip = 1 self.ppu2_mdr = self.latched_v & 0xFF else: self.latch_v_flip = 0 self.ppu2_mdr = self.ppu2_mdr & 0xFE | (self.latched_v >> 8) & 1 return self.ppu2_mdr if r == 0x3E: # DOC: fullsnes "213Eh - STAT77": bits 7/6 time/range over, bits # 3-0 PPU1 version; fullsnes puts open bus in bit 5 and 0 in bit 4. # MEASURED (probe suite OBUS, the console 2026-10-25): bit 4 # follows PPU1's open bus and bit 5 reads 0 ($D1 with the latch # $FF, $C1 with $00), as ares does: the console overrules fullsnes. self.ppu1_mdr = ( self.ppu1_mdr & 0x10 | self.time_over << 7 | self.range_over << 6 | self.ppu1_version & 0x0F ) return self.ppu1_mdr if r == 0x3F: # DOC: fullsnes "213Fh - STAT78": bit 7 field, bit 6 counters # latched (cleared by this read), bit 5 open bus, bit 4 PAL, # bits 3-0 PPU2 version; the read resets the $213C/D flip-flops. self.latch_h_flip = 0 self.latch_v_flip = 0 value = self.ppu2_mdr & 0x20 | self.ppu2_version & 0x0F if self.board.machine.pal: value |= 0x10 if not self.board.wrio & 0x80: # OBSERVED (ares readIO): bit 6 reads 1 while WRIO bit 7 is low. value |= 0x40 else: value |= self.latch_counters << 6 self.latch_counters = False value |= self.board.field << 7 self.ppu2_mdr = value return value # $2100-$2103, $2107, $210B-$2113, $2117, $211B-$2123, $2127, # $212B-$2133: write-only, read as the CPU's open bus (DOC: fullsnes). return mdr def latch_hv(self) -> None: """Latch the H/V counters into OPHCT/OPVCT (a $2137 read or WRIO edge).""" board = self.board self.latched_h = board.hdot() self.latched_v = board.v self.latch_counters = True def write(self, address: int, value: int) -> None: r = address & 0x3F self.raw[r] = value if r == 0x00: # DOC: fullsnes "2100h - INIDISP"; OBSERVED (ares writeIO): a write # on the first VBlank line while force-blanked resets the OAM address. if self.display_disable and self.board.v == self.vdisp: self._oam_address_reset() self.brightness = value & 0x0F self.display_disable = bool(value & 0x80) elif r == 0x02: # DOC: fullsnes "2102h/2103h - OAMADDL/H": a word address; a write # reloads the internal byte address. self.oam_base_address = (self.oam_base_address & 0x200) | value << 1 self._oam_address_reset() elif r == 0x03: self.oam_base_address = (value & 1) << 9 | self.oam_base_address & 0x1FE self.oam_priority = bool(value & 0x80) self._oam_address_reset() elif r == 0x04: # DOC: fullsnes "2104h - OAMDATA": low-table writes are buffered # and committed as a word on the odd byte; high-table bytes go # straight in. address = self.oam_address self.oam_address = (address + 1) & 0x3FF if not address & 1: self.latch_oam = value if address & 0x200: self._write_oam(address, value) elif address & 1: self._write_oam(address & ~1, self.latch_oam) self._write_oam(address, value) self._set_first_sprite() elif r == 0x01: # DOC: fullsnes "2101h - OBSEL". obj = self.obj obj.tiledata_address = (value & 7) << 13 obj.nameselect = (value >> 3) & 3 obj.base_size = value >> 5 elif r == 0x05: # DOC: fullsnes "2105h - BGMODE". self.bg_mode = value & 7 self.bg_priority = bool(value & 8) for n, bg in enumerate(self.bgs): bg.tile_size = (value >> (4 + n)) & 1 self._update_video_mode() elif r == 0x06: # DOC: fullsnes "2106h - MOSAIC"; OBSERVED (ares writeIO): the # vertical counter reloads when mosaic becomes enabled. was = self.mosaic.enable() for n, bg in enumerate(self.bgs): bg.mosaic_enable = bool(value >> n & 1) self.mosaic.size = (value >> 4) + 1 if not was and self.mosaic.enable(): self.mosaic.vcounter = self.mosaic.size + 1 elif 0x07 <= r <= 0x0A: # DOC: fullsnes "2107h-210Ah - BGnSC". bg = self.bgs[r - 0x07] bg.screen_size = value & 3 bg.screen_address = (value >> 2) << 10 elif r == 0x0B or r == 0x0C: # DOC: fullsnes "210Bh/210Ch - BG12NBA/BG34NBA". first = 0 if r == 0x0B else 2 self.bgs[first].tiledata_address = (value & 15) << 12 self.bgs[first + 1].tiledata_address = (value >> 4) << 12 elif 0x0D <= r <= 0x14: self._write_offset(r, value) elif r == 0x15: self.vram_increment = _INCREMENTS[value & 3] self.vram_mapping = (value >> 2) & 3 self.vram_increment_mode = value >> 7 elif r == 0x16: # DOC: fullsnes "2116h/2117h - VMADDL/H": a write prefetches the # read latch. self.vram_address = self.vram_address & 0xFF00 | value self.latch_vram = self._read_vram() elif r == 0x17: self.vram_address = value << 8 | self.vram_address & 0x00FF self.latch_vram = self._read_vram() elif r == 0x18: self._write_vram(0, value) if self.vram_increment_mode == 0: self.vram_address = (self.vram_address + self.vram_increment) & 0xFFFF elif r == 0x19: self._write_vram(1, value) if self.vram_increment_mode == 1: self.vram_address = (self.vram_address + self.vram_increment) & 0xFFFF elif r == 0x1A: # DOC: fullsnes "211Ah - M7SEL". self.m7_hflip = bool(value & 1) self.m7_vflip = bool(value & 2) self.m7_repeat = value >> 6 elif 0x1B <= r <= 0x20: # DOC: fullsnes "211Bh-2120h": write twice, through the shared # mode 7 latch. word = value << 8 | self.latch_mode7 self.latch_mode7 = value if r == 0x1B: self.m7a = word elif r == 0x1C: self.m7b = word elif r == 0x1D: self.m7c = word elif r == 0x1E: self.m7d = word elif r == 0x1F: self.m7x = word else: self.m7y = word elif r == 0x21: self.cgram_address = value self.cgram_address_latch = 0 elif r == 0x22: # DOC: fullsnes "2122h - CGDATA": low byte buffered, the 15-bit # word written with the high byte. if self.cgram_address_latch == 0: self.cgram_address_latch = 1 self.latch_cgram = value else: self.cgram_address_latch = 0 target = self._cgram_target(self.cgram_address) self.cgram[target] = (value & 0x7F) << 8 | self.latch_cgram self.cgram_address = (self.cgram_address + 1) & 0xFF elif 0x23 <= r <= 0x25: # DOC: fullsnes "2123h-2125h - W12SEL/W34SEL/WOBJSEL": per layer # window 1 invert, enable, window 2 invert, enable. window = self.window first = (r - 0x23) * 2 for k, layer in enumerate((first, first + 1)): bits = value >> (4 * k) window.one_invert[layer] = bits & 1 window.one_enable[layer] = (bits >> 1) & 1 window.two_invert[layer] = (bits >> 2) & 1 window.two_enable[layer] = (bits >> 3) & 1 elif r == 0x26: self.window.one_left = value elif r == 0x27: self.window.one_right = value elif r == 0x28: self.window.two_left = value elif r == 0x29: self.window.two_right = value elif r == 0x2A: # DOC: fullsnes "212Ah/212Bh - WBGLOG/WOBJLOG": 0 OR, 1 AND, # 2 XOR, 3 XNOR. for n in range(4): self.window.mask[n] = (value >> (2 * n)) & 3 elif r == 0x2B: self.window.mask[4] = value & 3 self.window.mask[5] = (value >> 2) & 3 elif r == 0x2C or r == 0x2D: # DOC: fullsnes "212Ch/212Dh - TM/TS". main = r == 0x2C for n, bg in enumerate(self.bgs): if main: bg.above_enable = bool(value >> n & 1) else: bg.below_enable = bool(value >> n & 1) if main: self.obj.above_enable = bool(value & 0x10) else: self.obj.below_enable = bool(value & 0x10) elif r == 0x2E or r == 0x2F: # DOC: fullsnes "212Eh/212Fh - TMW/TSW". target = self.window.above_enable if r == 0x2E else self.window.below_enable for n in range(5): target[n] = value >> n & 1 elif r == 0x30: # DOC: fullsnes "2130h - CGWSEL". self.dac.direct_color = bool(value & 1) self.dac.blend_mode = bool(value & 2) self.window.col_below_mask = (value >> 4) & 3 self.window.col_above_mask = value >> 6 elif r == 0x31: # DOC: fullsnes "2131h - CGADSUB". dac = self.dac for n in range(6): dac.color_enable[n] = bool(value >> n & 1) dac.color_halve = bool(value & 0x40) dac.color_mode = bool(value & 0x80) elif r == 0x32: # DOC: fullsnes "2132h - COLDATA". dac = self.dac if value & 0x20: dac.color_red = value & 31 if value & 0x40: dac.color_green = value & 31 if value & 0x80: dac.color_blue = value & 31 elif r == 0x33: # DOC: fullsnes "2133h - SETINI". self.interlace = bool(value & 1) self.obj.interlace = bool(value & 2) self.overscan = bool(value & 4) self.pseudo_hires = bool(value & 8) self.extbg = bool(value & 0x40) self._update_video_mode() def _write_offset(self, r: int, value: int) -> None: """BGnHOFS/BGnVOFS (DOC: fullsnes "210Dh-2114h": the two PPU1/PPU2 latches; the horizontal formula's split follows ares writeIO).""" if r == 0x0D: self.m7_hoffset = value << 8 | self.latch_mode7 self.latch_mode7 = value elif r == 0x0E: self.m7_voffset = value << 8 | self.latch_mode7 self.latch_mode7 = value bg = (r - 0x0D) >> 1 if (r - 0x0D) & 1 == 0: self.bgs[bg].hoffset = ( (value << 8 | self.latch_bgofs1 & ~7 & 0xFF | self.latch_bgofs2 & 7) & 0x3FF ) self.latch_bgofs1 = value self.latch_bgofs2 = value else: self.bgs[bg].voffset = (value << 8 | self.latch_bgofs1) & 0x3FF self.latch_bgofs1 = value __all__ = ["PPU"]