# SPDX-License-Identifier: MIT """Board unit tests: each assertion names the document or measurement it checks.""" from __future__ import annotations import pathlib from snes_python.board import SNES from snes_python.cartridge import HIROM, LOROM, Cartridge, find_header, mirror def _rom(kind: str = LOROM, size: int = 0x20000, program: bytes = b"\xdb") -> bytes: """A minimal image: the program at $00:8000 (STP by default), a header.""" rom = bytearray(size) at = 0x7FC0 if kind == LOROM else 0xFFC0 code_at = 0x0000 if kind == LOROM else 0x8000 rom[code_at:code_at + len(program)] = program rom[at:at + 21] = b"TEST CARTRIDGE".ljust(21) rom[at + 0x15] = 0x20 if kind == LOROM else 0x21 rom[at + 0x17] = 0x07 rom[at + 0x1C:at + 0x20] = b"\xff\xff\x00\x00" rom[at + 0x3C:at + 0x3E] = b"\x00\x80" return bytes(rom) def _board(**kwargs) -> SNES: return SNES(Cartridge(_rom(**kwargs))) def test_header_detection() -> None: assert find_header(_rom(LOROM)).kind == LOROM assert find_header(_rom(HIROM, size=0x40000)).kind == HIROM def test_mirror_odd_sizes() -> None: # DOC: fullsnes "ROM Size / Checksum Notes": 10 Mbit = 8 Mbit + 4 x 2 Mbit. size = 0x140000 assert mirror(0x0FFFFF, size) == 0x0FFFFF assert mirror(0x100000, size) == 0x100000 assert mirror(0x140000, size) == 0x100000 assert mirror(0x1C0001, size) == 0x100001 assert mirror(0x200005, size) == 0x000005 def test_cycle_lengths() -> None: # DOC: fullsnes "CPU Clock Cycles", docs/start-here.md's table. b = _board() assert b._speed(0x000000) == 8 # WRAM mirror assert b._speed(0x002100) == 6 # B bus assert b._speed(0x004016) == 12 # old joypad ports assert b._speed(0x004200) == 6 # CPU registers assert b._speed(0x006000) == 8 # expansion / SRAM assert b._speed(0x008000) == 8 # ROM, bank $00 assert b._speed(0x7E0000) == 8 # WRAM assert b._speed(0x808000) == 8 # FastROM area, MEMSEL clear b.bus_write(0x00420D, 1) assert b._speed(0x808000) == 6 assert b._speed(0xC00000) == 6 assert b._speed(0x408000) == 8 def test_open_bus_and_io_reads() -> None: b = _board() # DOC: fullsnes: unmapped addresses read the MDR. assert b.bus_read(0x005000, 0x5A) == 0x5A # DOC: fullsnes "4210h": CPU version in bits 3-0, open bus in 6-4. assert b.bus_read(0x004210, 0x70) & 0x7F == 0x72 def test_wmadd_port() -> None: # DOC: fullsnes "2180h-2183h": WMDATA reads/writes WRAM at WMADD, +1. b = _board() for r, v in ((0x2181, 0x34), (0x2182, 0x12), (0x2183, 0x01)): b.bus_write(r, v) b.bus_write(0x2180, 0xAB) assert b.wram[0x11234] == 0xAB assert b.wram_address == 0x11235 def test_multiply_and_divide_steps() -> None: # DOC: fullsnes "4202h-4206h": 8 and 16 cycles; MEASURED (probe suite # MULF/DIV0): one step a cycle; division by zero = $FFFF, remainder. b = _board() b.bus_write(0x4202, 200) b.bus_write(0x4203, 100) for _ in range(8): b._alu_edge() assert b.io.rdmpy == 20000 b.bus_write(0x4204, 0x34) b.bus_write(0x4205, 0x12) b.bus_write(0x4206, 0) for _ in range(16): b._alu_edge() assert b.io.rddiv == 0xFFFF assert b.io.rdmpy == 0x1234 b.bus_write(0x4204, 100) b.bus_write(0x4205, 0) b.bus_write(0x4206, 7) for _ in range(16): b._alu_edge() assert (b.io.rddiv, b.io.rdmpy) == (14, 2) def test_mode7_product() -> None: # DOC: fullsnes "2134h-2136h": signed M7A x signed 8-bit M7B. b = _board() for value in (0x00, 0x80): # M7A = $8000 = -32768 b.bus_write(0x211B, value) b.bus_write(0x211C, 0x02) b.bus_write(0x211C, 0xFE) # M7B high byte -2 product = sum(b.bus_read(0x2134 + i, 0) << (8 * i) for i in range(3)) assert product == 65536 def test_vram_port_increment_and_prefetch() -> None: # DOC: fullsnes "2115h-2119h", "2139h/213Ah" (forced blank at power-on). b = _board() b.bus_write(0x2115, 0x80) # increment after the high byte, by 1 b.bus_write(0x2116, 0x00) b.bus_write(0x2117, 0x10) b.bus_write(0x2118, 0x11) b.bus_write(0x2119, 0x22) assert b.ppu.vram[0x1000] == 0x2211 assert b.ppu.vram_address == 0x1001 b.bus_write(0x2116, 0x00) b.bus_write(0x2117, 0x10) assert b.bus_read(0x2139, 0) == 0x11 assert b.bus_read(0x213A, 0) == 0x22 def test_frame_length() -> None: # DOC: anomie timing l.55-66: 262 lines of 1364 clocks, one 1360-clock # line on field 1 without interlace: 357,368 and 357,364 clocks. b = _board() # the program is STP: the CPU stops, time passes b.run_frame() lengths = [] for _ in range(4): start = b.clock - b.h # the clock at H=0 of line 0 b.run_frame() lengths.append(b.clock - b.h - start) assert sorted(set(lengths)) == [357364, 357368] def test_board_imports_no_reference_tree() -> None: # The restart rule: nothing in src/ names a GPL tree as a module. src = pathlib.Path(__file__).resolve().parents[1] / "src" / "snes_python" for path in src.rglob("*.py"): text = path.read_text() assert "reference/src/bsnes" not in text and "Mesen2/Core" not in text, path def test_htime_round_trip() -> None: # DOC: fullsnes "4207h/4208h": HTIME is 9 bits written in two halves; # writing 0 to both, then a low byte, gives that byte (a regression: # H=0's own encoding once corrupted the next half-write). b = _board() b.bus_write(0x4207, 0) b.bus_write(0x4208, 0) assert b.io.htime == 0 b.bus_write(0x4207, 190) assert b.io.htime == (190 + 1) * 4 b.bus_write(0x4208, 1) assert b.io.htime == (0x1BE + 1) * 4 def _sram_rom(kind: str, ram: int, chipset: int = 0x02) -> bytes: rom = bytearray(_rom(kind, size=0x40000 if kind == HIROM else 0x20000)) at = 0x7FC0 if kind == LOROM else 0xFFC0 rom[at + 0x16] = chipset rom[at + 0x18] = ram return bytes(rom) def test_sram_maps_mirrors_and_saves() -> None: # DOC: fullsnes "LoROM with SRAM" (70-7D,F0-FF:0000-7FFF) and "HiROM with # SRAM" (20-3F,A0-BF:6000-7FFF); a smaller SRAM repeats through the window. lo = Cartridge(_sram_rom(LOROM, 1)) # 2 KiB assert len(lo.sram) == 0x800 and lo.has_battery lo.write(0x700000, 0x12) assert lo.read(0x700800, 0) == 0x12 and lo.read(0xF00000, 0) == 0x12 assert lo.sram_offset(0x708000) is None and lo.rom_offset(0x708000) is not None hi = Cartridge(_sram_rom(HIROM, 3)) # 8 KiB hi.write(0x306000, 0x34) assert hi.read(0x206000, 0) == 0x34 and hi.read(0xB06000, 0) == 0x34 # DOC: SNESdev wiki "Save file formats": the .srm is the raw SRAM. saved = lo.battery() fresh = Cartridge(_sram_rom(LOROM, 1)) fresh.load_battery(saved) assert fresh.read(0x700000, 0) == 0x12 and fresh.battery() == saved def test_sram_without_battery_is_not_saved() -> None: # DOC: SNESdev wiki "ROM header" $FFD6: $01 is ROM + RAM, no battery. cart = Cartridge(_sram_rom(LOROM, 1, chipset=0x01)) assert len(cart.sram) == 0x800 and cart.battery() is None def test_headerless_image_is_lorom() -> None: # DESIGN (census 2026-11-23, docs/cartridge.md): no believable header -> LoROM. rom = bytearray(0x80000) rom[0xFFC0 + 0x3C:0xFFC0 + 0x3E] = b"\x00\xff" # a stray "reset vector" at $FFFC rom[0x7FC0 + 0x3C:0x7FC0 + 0x3E] = b"\x00\x80" assert find_header(bytes(rom)).kind == LOROM def test_unbelievable_header_without_lorom_entry_keeps_the_score() -> None: # DESIGN (full sweep 2026-11-24): a title and vectors only, at $FFC0 (Final Fight 3 # (USA) (Beta)); LoROM's "reset vector" is not in $8000-$FFFE, so no LoROM fallback. rom = bytearray(0x80000) rom[0xFFC0:0xFFD5] = b"FINAL FIGHT TOUGH".ljust(21) rom[0xFFC0 + 0x3C:0xFFC0 + 0x3E] = b"\x00\xfe" rom[0x7FC0 + 0x3C:0x7FC0 + 0x3E] = b"\x80\x04" assert find_header(bytes(rom)).kind == HIROM def test_score_tie_goes_to_the_start_up_code() -> None: # DESIGN (full sweep 2026-11-24, Mortal Kombat II (USA) (Beta)): two believable headers # of equal score; only the HiROM reset vector lands on SEI. rom = bytearray(_rom(HIROM, size=0x40000, program=b"\x78\x18\xfb")) lo = bytearray(_rom(LOROM))[0x7FC0:0x8000] rom[0x7FC0:0x8000] = lo rom[0x7FC0 + 0x3C:0x7FC0 + 0x3E] = b"\x80\x80" # LoROM entry $8080: offset $0080, a zero assert find_header(bytes(rom)).kind == HIROM def test_lorom_fallback_gives_way_to_hirom_start_up() -> None: # DESIGN (full sweep 2026-11-24, Marvel Super Heroes in War of the Gems (USA) (Beta)): # no believable header; only the HiROM vector lands on SEI. rom = bytearray(0x80000) rom[0xFFC0 + 0x3C:0xFFC0 + 0x3E] = b"\x07\xff" rom[0xFF07] = 0x78 rom[0xFFC0:0xFFD5] = b"WAR OF THE GEMS".ljust(21) rom[0x7FC0 + 0x3C:0x7FC0 + 0x3E] = b"\x1a\xca" assert find_header(bytes(rom)).kind == HIROM