# SPDX-License-Identifier: MIT """Rung 1: PLOT, RPIX, COLOR, CMODE and the pixel caches (BK2 8.1; U20, U21, C5, C15).""" from __future__ import annotations import pytest from conftest import make_gsu, run_to_stop, start from gsu_asm import CMODE, COLOR, GETC, PLOT, RPIX, STOP, dec, ibt, iwt, program from gsu_python import GSU PLANES = (0x00, 0x01, 0x10, 0x11, 0x20, 0x21, 0x30, 0x31) DEPTH = {0: 2, 1: 4, 3: 8} def expected_row(x: int, y: int, height: int, depth: int, scbr: int, obj: bool) -> int: """fullsnes "Bitmap I/O Ports" (with BK2 8.1.5's misprints corrected: C5).""" if obj: tile = (y // 0x80) * 0x200 + (x // 0x80) * 0x100 + (y // 8 & 0xF) * 0x10 + (x // 8 & 0xF) else: tile = (x // 8) * (0x10, 0x14, 0x18)[height] + y // 8 return tile * depth * 8 + scbr * 0x400 + (y & 7) * 2 def plot_program(por: int, color: int, x: int, y: int) -> list[int]: return program( ibt(0, por), CMODE, iwt(0, color), COLOR, iwt(1, x), iwt(2, y), PLOT, dec(1), RPIX, STOP ) def run_plot(code, scmr: int, scbr: int = 0, cls=GSU, ram=None) -> GSU: g = make_gsu(code, cls=cls) if ram: for address, value in ram.items(): g.ram[address] = value g.write_io(0x3038, scbr) start(g, scmr=scmr) run_to_stop(g) return g @pytest.mark.parametrize("md", [0, 1, 3]) @pytest.mark.parametrize("mode", ["128", "160", "192", "OBJ (POR)", "OBJ (SCMR)"]) def test_address_in_every_height_and_depth(md, mode): """4 heights x 3 depths: PLOT then RPIX lands every plane bit where fullsnes puts it.""" height = {"128": 0, "160": 1, "192": 2, "OBJ (POR)": 0, "OBJ (SCMR)": 3}[mode] obj = mode.startswith("OBJ") por = 0x10 if mode == "OBJ (POR)" else 0 scmr = 0x18 | md | (height & 1) << 2 | (height >> 1) << 5 depth = DEPTH[md] color = 0xA5 & (0xFF >> (8 - depth)) | 1 x, y = 0x93, 0x9D g = run_plot(plot_program(por, color, x, y), scmr, scbr=0x0B) row = expected_row(x, y, height, depth, 0x0B, obj) for plane in range(depth): bit = g.ram[row + PLANES[plane]] >> (7 - (x & 7)) & 1 assert bit == (color >> plane & 1), plane assert g.r[0] == color # RPIX reads it back from RAM def test_transparency_by_depth(): """U20: with POR bit 0 = 0 a colour whose low 2 bits are 0 is not plotted in 4-colour mode (fullsnes); ares tests the low 4 bits there.""" g = run_plot(plot_program(0x00, 0x04, 8, 0), 0x18) # 2 bits per pixel, colour 4 assert g.r[0] == 0 assert g.r[1] == 8 # R1 still advanced (and DEC R1 undid it) class Ares(GSU): TRANSPARENCY_BY_DEPTH = False g = run_plot(plot_program(0x00, 0x04, 8, 0), 0x18, cls=Ares) assert g.r[0] == 0 # plotted, but colour 4 has no bits in 2 planes assert g.pixel_secondary[1] == 0x80 def test_opaque_plots_colour_zero(): g = run_plot(plot_program(0x01, 0x00, 0, 0), 0x19, ram={0: 0xFF, 1: 0xFF, 16: 0xFF, 17: 0xFF}) assert g.ram[0] == 0x7F # pixel 0 cleared in plane 0 def test_freeze_high_transparency_in_256_colours(): """POR bit 3 in 256 colours: only the low 4 bits decide transparency.""" g = run_plot(plot_program(0x08, 0xF0, 0, 0), 0x1B) assert g.r[0] == 0 def test_dither(): """POR bit 1 (4/16 colours): the high nibble when (X xor Y) is odd.""" g = run_plot(plot_program(0x02, 0x21, 1, 0), 0x19) assert g.r[0] == 0x2 g = run_plot(plot_program(0x02, 0x21, 1, 1), 0x19) assert g.r[0] == 0x1 def test_color_high_nibble_and_freeze(): """BK2 figure 2-8-5: COLOR with POR bit 2 takes the source's high nibble; with bit 3 keeps COLR's high nibble.""" g = make_gsu(program(iwt(0, 0x5A), COLOR, ibt(0, 0x04), CMODE, iwt(0, 0xC3), COLOR, STOP)) start(g) run_to_stop(g) assert g.colr == 0x5C g = make_gsu(program(iwt(0, 0x5A), COLOR, ibt(0, 0x08), CMODE, iwt(0, 0xC3), COLOR, STOP)) start(g) run_to_stop(g) assert g.colr == 0x53 def test_getc_reads_the_rom_buffer(): code = program(iwt(14, 0x8040), GETC, STOP) code += [0] * (0x40 - len(code)) + [0x77] g = make_gsu(code) start(g) run_to_stop(g) assert g.colr == 0x77 def test_partial_row_is_merged_with_ram(): """U20: a row with fewer than 8 plotted pixels keeps RAM's other pixels.""" ram = {0x00: 0b1010_1010, 0x01: 0b0000_0000} code = program(ibt(0, 0), CMODE, ibt(0, 3), COLOR, iwt(1, 7), iwt(2, 0), PLOT, RPIX, STOP) g = run_plot(code, 0x18, ram=ram) assert g.ram[0x00] == 0b1010_1011 assert g.ram[0x01] == 0b0000_0001 def test_full_row_hands_over_at_once(): """A full primary row moves to the secondary without waiting for another PLOT.""" code = program(ibt(0, 1), COLOR, iwt(1, 0), iwt(2, 0)) + PLOT * 8 + STOP g = run_plot(code, 0x18) assert g.ram[0x00] == 0xFF assert g.pixel_primary[1] == 0 assert g.pixel_secondary[1] == 0xFF def test_new_row_hands_over_previous(): code = program(ibt(0, 1), COLOR, iwt(1, 0), iwt(2, 0), PLOT, iwt(2, 1), PLOT, STOP) g = run_plot(code, 0x18) assert g.ram[0x00] == 0x80 # row 0 written at the hand-over assert g.ram[0x02] == 0x00 # row 1 still in the primary cache (no RPIX) assert g.pixel_primary[1] == 0x40