# SPDX-License-Identifier: MIT """Unit tests of the picture (milestone 2) on a PPU driven line by line. Expectations come from the documents (fullsnes "SNES PPU Registers", "SNES PPU Color-Math", "OBJ"; wiki/Sprites) and, for positions inside a line, from the cycle schedule ``ppu.py`` follows (ares sfc/ppu/main.cpp). """ from __future__ import annotations from types import SimpleNamespace from snes_python.machines import NTSC from snes_python.ppu import PPU from snes_python.ppu_png import read_png, write_png class FakeBoard(SimpleNamespace): """The few board fields the PPU reads.""" def make_ppu() -> tuple[PPU, FakeBoard]: board = FakeBoard(v=0, h=0, field=0, last_hperiod=1364, wrio=0xFF, machine=NTSC) ppu = PPU(board) # type: ignore[arg-type] return ppu, board def run_frame(ppu: PPU, board: FakeBoard, hook=None) -> None: """One 262-line frame; ``hook(v, ppu, board)`` runs at each line start.""" for v in range(262): board.v = v board.h = 0 ppu.scanline(v) if hook: hook(v, ppu, board) board.h = 1364 ppu.sync() board.h = 0 ppu.end_line() def w(ppu: PPU, register: int, *values: int) -> None: for value in values: ppu.write(register, value) def colour(ppu: PPU, x: int, y: int) -> int: """The main-screen word at screen (x, y) without brightness.""" return ppu.frame_buffer[(y << 1) * 512 + (x << 1) + 1] & 0x7FFF def setup_mode0(ppu: PPU) -> None: """Force blank, BG1 map at word $0000 all tile 1, tile 1 at $1000 solid colour 1 (2bpp), CGRAM 1 = red, backdrop = blue, BG1 on the main screen.""" w(ppu, 0x00, 0x80) w(ppu, 0x05, 0x00) w(ppu, 0x07, 0x00) w(ppu, 0x0B, 0x01) # BG1 characters at word $1000 w(ppu, 0x15, 0x80) w(ppu, 0x16, 0x00, ) w(ppu, 0x17, 0x00) for _ in range(32 * 32): w(ppu, 0x18, 0x01, ) w(ppu, 0x19, 0x00) w(ppu, 0x16, 0x08) # tile 1 = word $1008 w(ppu, 0x17, 0x10) for _ in range(8): w(ppu, 0x18, 0xFF) w(ppu, 0x19, 0x00) w(ppu, 0x21, 0x00) w(ppu, 0x22, 0x00, 0x7C) # colour 0: blue w(ppu, 0x22, 0x1F, 0x00) # colour 1: red w(ppu, 0x2C, 0x01) w(ppu, 0x00, 0x0F) def test_png_round_trip(tmp_path) -> None: rgb = bytes(range(256)) * 3 write_png(tmp_path / "x.png", 16, 16, rgb) assert read_png(tmp_path / "x.png") == (16, 16, bytearray(rgb)) def test_bg1_solid_tile_and_backdrop() -> None: ppu, board = make_ppu() setup_mode0(ppu) run_frame(ppu, board) assert colour(ppu, 0, 0) == 0x001F assert colour(ppu, 255, 223) == 0x001F w(ppu, 0x2C, 0x00) run_frame(ppu, board) assert colour(ppu, 100, 100) == 0x7C00 width, height, rgb = ppu.picture() assert (width, height) == (256, 224) assert rgb[:3] == bytearray((0, 0, 255)) def test_force_blank_is_black() -> None: ppu, board = make_ppu() setup_mode0(ppu) w(ppu, 0x00, 0x8F) run_frame(ppu, board) assert colour(ppu, 10, 10) == 0 def test_brightness_scales_the_picture() -> None: # DOC: fullsnes "2100h - INIDISP": brightness * (N + 1) / 16, 0 = black. ppu, board = make_ppu() setup_mode0(ppu) w(ppu, 0x00, 0x07) run_frame(ppu, board) _, _, rgb = ppu.picture() assert rgb[0] == (255 * 8 + 8) // 16 def test_colour_math_add_fixed_and_halve() -> None: # DOC: fullsnes "SNES PPU Color-Math": main + fixed colour, clamped per # channel; halved when CGADSUB bit 6 is set. ppu, board = make_ppu() setup_mode0(ppu) w(ppu, 0x32, 0x20 | 0x1F) # fixed red = 31 w(ppu, 0x32, 0x40 | 0x10) # fixed green = 16 w(ppu, 0x31, 0x01) # BG1 adds run_frame(ppu, board) assert colour(ppu, 50, 50) == 0x001F | 16 << 5 w(ppu, 0x31, 0x41) run_frame(ppu, board) assert colour(ppu, 50, 50) == 31 | 8 << 5 def test_colour_math_subtract_clamps_at_zero() -> None: ppu, board = make_ppu() setup_mode0(ppu) w(ppu, 0x32, 0x20 | 0x05) w(ppu, 0x32, 0x40 | 0x05) w(ppu, 0x31, 0x81) run_frame(ppu, board) assert colour(ppu, 50, 50) == 31 - 5 def test_window_one_masks_bg1() -> None: # DOC: fullsnes "2123h", "2126h-2129h", "212Eh": window 1 from 16 to 31 # masks BG1 on the main screen there. ppu, board = make_ppu() setup_mode0(ppu) w(ppu, 0x23, 0x02) w(ppu, 0x26, 16) w(ppu, 0x27, 31) w(ppu, 0x2E, 0x01) run_frame(ppu, board) assert colour(ppu, 15, 40) == 0x001F assert colour(ppu, 16, 40) == 0x7C00 assert colour(ppu, 31, 40) == 0x7C00 assert colour(ppu, 32, 40) == 0x001F w(ppu, 0x23, 0x03) # inverted run_frame(ppu, board) assert colour(ppu, 15, 40) == 0x7C00 assert colour(ppu, 20, 40) == 0x001F def test_mid_line_cgram_and_brightness_split() -> None: """A write at H=600 changes the picture from dot (600 - 58) / 4 on (ares's schedule: a dot's main half is drawn at H = 58 + 4x).""" ppu, board = make_ppu() setup_mode0(ppu) def hook(v, ppu, board): if v == 100: board.h = 600 ppu.sync() w(ppu, 0x00, 0x00) # brightness 0 from here if v == 101: w(ppu, 0x00, 0x0F) run_frame(ppu, board, hook) row = 99 * 2 * 512 buffer = ppu.frame_buffer split = (600 - 58) // 4 + 1 assert buffer[row + ((split - 1) << 1) + 1] >> 15 == 15 assert buffer[row + (split << 1) + 1] >> 15 == 0 def oam_fill(ppu: PPU, sprites: list[tuple[int, int, int, int]], high: bytes = b"") -> None: """Write OAM: (x, y, tile, attr) per sprite, the rest at Y=240.""" w(ppu, 0x02, 0x00) w(ppu, 0x03, 0x00) for n in range(128): x, y, tile, attr = sprites[n] if n < len(sprites) else (0, 240, 0, 0) w(ppu, 0x04, x & 0xFF, y, tile, attr) high = high.ljust(32, b"\x00") for value in high: w(ppu, 0x04, value) def setup_sprite_tile(ppu: PPU) -> None: """OBJ tile 0 at word $4000 (OBSEL base 2) solid colour 1; OBJ palette 0 colour 1 (CGRAM 129) = green.""" w(ppu, 0x00, 0x80) w(ppu, 0x01, 0x02) w(ppu, 0x16, 0x00) w(ppu, 0x17, 0x40) for _ in range(8): w(ppu, 0x18, 0xFF) w(ppu, 0x19, 0x00) for _ in range(8): w(ppu, 0x18, 0x00) w(ppu, 0x19, 0x00) w(ppu, 0x21, 129) w(ppu, 0x22, 0xE0, 0x03) def test_sprite_drawn_one_line_below_its_y() -> None: # DOC: wiki/Sprites: OBJ Y is the line before the first drawn line # (the evaluation happens on the line before). ppu, board = make_ppu() setup_mode0(ppu) setup_sprite_tile(ppu) oam_fill(ppu, [(40, 20, 0, 0x30)]) w(ppu, 0x2C, 0x11) w(ppu, 0x00, 0x0F) run_frame(ppu, board) run_frame(ppu, board) assert colour(ppu, 40, 20) == 0x03E0 # screen row 20 = line 21 assert colour(ppu, 47, 27) == 0x03E0 assert colour(ppu, 48, 20) == 0x001F assert colour(ppu, 40, 19) == 0x001F assert colour(ppu, 40, 28) == 0x001F def _flags_at_line_100(ppu: PPU, board: FakeBoard) -> dict: """The STAT77 flags mid-frame (they clear at the frame's start; the parked objects at Y=240 are evaluated on line 240, so the end-of-frame value is not this line's).""" seen = {} def hook(v, ppu, board): if v == 100: seen["range"] = ppu.range_over seen["time"] = ppu.time_over run_frame(ppu, board, hook) run_frame(ppu, board, hook) return seen def test_range_over_on_33_sprites() -> None: # DOC: fullsnes "213Eh - STAT77" bit 6: more than 32 OBJs on a line. ppu, board = make_ppu() setup_sprite_tile(ppu) oam_fill(ppu, [(n * 6, 50, 0, 0) for n in range(32)]) w(ppu, 0x00, 0x0F) assert _flags_at_line_100(ppu, board) == {"range": False, "time": False} w(ppu, 0x00, 0x80) oam_fill(ppu, [(n * 6, 50, 0, 0) for n in range(33)]) w(ppu, 0x00, 0x0F) assert _flags_at_line_100(ppu, board) == {"range": True, "time": False} def test_time_over_on_35_slivers() -> None: # DOC: fullsnes "213Eh - STAT77" bit 7: more than 34 8x1 slivers. ppu, board = make_ppu() setup_sprite_tile(ppu) w(ppu, 0x01, 0x02 | 0x20) # 8x8 / 32x32 high = bytes([0xAA] * 3) # objects 0-11 large oam_fill(ppu, [(n * 16, 50, 0, 0) for n in range(8)], high) w(ppu, 0x00, 0x0F) assert _flags_at_line_100(ppu, board) == {"range": False, "time": False} w(ppu, 0x00, 0x80) oam_fill(ppu, [(n * 16, 50, 0, 0) for n in range(9)], high) w(ppu, 0x00, 0x0F) assert _flags_at_line_100(ppu, board) == {"range": False, "time": True} def test_mode7_identity_shows_the_map() -> None: # DOC: fullsnes "211Bh-2120h": with A = D = 1.0 and B = C = 0 the map # is drawn unscaled; tile number in the low byte of VRAM words $0000- # $3FFF, pixels in the high bytes of each tile's 64 words. ppu, board = make_ppu() w(ppu, 0x00, 0x80) w(ppu, 0x05, 0x07) w(ppu, 0x15, 0x00) # increment after the low byte w(ppu, 0x16, 0x00) w(ppu, 0x17, 0x00) for i in range(128 * 128): w(ppu, 0x18, 1 if (i & 127) >= 16 else 0) w(ppu, 0x15, 0x80) w(ppu, 0x16, 0x00) w(ppu, 0x17, 0x00) for i in range(128): w(ppu, 0x19, 0 if i < 64 else 2) w(ppu, 0x1B, 0x00, 0x01) w(ppu, 0x1C, 0x00, 0x00) w(ppu, 0x1D, 0x00, 0x00) w(ppu, 0x1E, 0x00, 0x01) w(ppu, 0x21, 0x02) w(ppu, 0x22, 0x1F, 0x00) w(ppu, 0x2C, 0x01) w(ppu, 0x00, 0x0F) run_frame(ppu, board) assert colour(ppu, 127, 10) == 0 assert colour(ppu, 128, 10) == 0x001F def test_hires_mode5_is_512_wide() -> None: ppu, board = make_ppu() setup_mode0(ppu) w(ppu, 0x05, 0x05) run_frame(ppu, board) width, height, _ = ppu.picture() assert (width, height) == (512, 224)