"""The MAME replays (docs/mame-oracle.md), when their logs exist locally: pytest -m slow. Each log is made by ``scripts/mame_trace.py`` (the command is in the table below); the replay drives the board from MAME's port writes and must agree on every read and every pixel of the exit snapshot. """ from __future__ import annotations import os import re import subprocess import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[1] TRACES = ROOT / "tests" / "mame_traces" # (log directory, MAME driver, seconds, extra replay arguments) REPLAYS = [ ("sms-SMSVDPTest-SMS-1.31", "sms", 12, []), ("sms-sonic", "sms", 20, []), ("sg1000-flicky", "sg1000", 20, ["--phase", "139536"]), # solved once; see the script ("gamegear-sonic", "gamegear", 20, []), ] RESULT_LINE = re.compile(r"^(?P\S.*?)\s+[\d,]+ agree\s+(?P[\d,]+) differ", re.M) # The one known disagreement (docs/undocumented-behavior.md, open item 7). EXPECTED_STATUS_DIFFERENCES = {"gamegear-sonic": 1} @pytest.mark.slow @pytest.mark.parametrize(("name", "driver", "seconds", "extra"), REPLAYS) def test_replay_agrees_with_mame(name, driver, seconds, extra): log = TRACES / name / "error.log" if not log.exists(): pytest.skip(f"{log} not recorded (scripts/mame_trace.py)") result = subprocess.run( [sys.executable, str(ROOT / "scripts" / "replay_mame_io.py"), str(log), "--driver", driver, "--seconds", str(seconds), *extra], capture_output=True, text=True, check=False, ) # fmt: skip differ = { match["name"]: int(match["differ"].replace(",", "")) for match in RESULT_LINE.finditer(result.stdout) } assert differ.get("frame") == 0, result.stdout assert differ.get("status", 0) == EXPECTED_STATUS_DIFFERENCES.get(name, 0), result.stdout others = {k: v for k, v in differ.items() if k not in ("frame", "status")} assert all(v == 0 for v in others.values()), result.stdout # (log directory, MAME driver, extra lockstep arguments, port reads MAME's value covers) LOCKSTEPS = [ ("sms-SMSVDPTest-SMS-1.31", "sms", [], 0), ("sms-sonic", "sms", [], 0), ("sg1000-flicky", "sg1000", [], 0), ("gamegear-sonic", "gamegear", ["--patch-reads"], 1), # open item 7 ] @pytest.mark.slow @pytest.mark.parametrize(("name", "driver", "extra", "patched"), LOCKSTEPS) def test_lockstep_with_mame(name, driver, extra, patched): log = TRACES / name / "error.log" if not log.exists(): pytest.skip(f"{log} not recorded (scripts/mame_trace.py)") result = subprocess.run( [sys.executable, str(ROOT / "scripts" / "lockstep_mame.py"), str(log), "--driver", driver, *extra], capture_output=True, text=True, check=False, ) # fmt: skip assert result.returncode == 0, result.stdout assert "instructions identical" in result.stdout assert f"{patched} port reads differed" in result.stdout, result.stdout VDPTEST = ROOT / "tests" / "test_roms" / "smspower" / "SMSVDPTest-SMS-1.31.zip" @pytest.mark.slow @pytest.mark.parametrize("machine", ["sms1", "sms2"]) def test_flubbas_vdp_test_passes(machine): # FluBBa's expected values come from real SMS1, SMS2 and Mega Drive hardware. if not VDPTEST.exists(): pytest.skip("SMS VDP Test not fetched (scripts/fetch_references.py)") result = subprocess.run( [sys.executable, str(ROOT / "scripts" / "run_vdptest.py"), "--machine", machine], capture_output=True, text=True, check=False, ) # fmt: skip assert "55 results, 0 errors" in result.stdout, result.stdout[-2000:] @pytest.mark.slow @pytest.mark.parametrize("machine", ["megadrive-pbc", "megadrive-pbc-pal"]) def test_flubbas_mega_drive_vdp_test(machine): """His Mega Drive entry, developed on a PAL Mega Drive with a Power Base Converter.""" if not VDPTEST.exists(): pytest.skip("SMS VDP Test not fetched (scripts/fetch_references.py)") result = subprocess.run( [sys.executable, str(ROOT / "scripts" / "run_vdptest.py"), "--machine", machine, "--menu", "1"], capture_output=True, text=True, check=False, ) # fmt: skip assert "55 results, 0 errors" in result.stdout, result.stdout[-2000:] GG_VDPTEST = ROOT / "tests" / "test_roms" / "smspower-forum" / "VDPTEST_GG.zip" # enik2's Game Gear tests: the 1-ASIC model's two IRQ HCount tests are open # (docs/undocumented-behavior.md, open item 8). GG_EXPECTED = { ("gg2asic", "ggsms"): (0, []), ("gg1asic", "ggsms1asic"): (2, ["Frame IRQ HCount", "Line IRQ HCount"]), } @pytest.mark.slow @pytest.mark.parametrize(("test", "machine"), sorted(GG_EXPECTED)) def test_enik2s_game_gear_vdp_test(test, machine): if not GG_VDPTEST.exists(): pytest.skip("VDPTEST_GG not fetched (scripts/fetch_references.py)") result = subprocess.run( [sys.executable, str(ROOT / "scripts" / "run_vdptest.py"), "--gg-test", test, "--machine", machine], capture_output=True, text=True, check=False, ) # fmt: skip errors, names = GG_EXPECTED[(test, machine)] assert f"55 results, {errors} errors" in result.stdout, result.stdout[-2000:] for name in names: assert f"{name}:" in result.stdout.split("results,")[-1] # Your No-Intro folders and MAME rompath, if any (scripts/romsets.py). NO_INTRO = Path(os.environ.get("NO_INTRO_DIR", "No-Intro")) MAME_ROMPATH = Path(os.environ.get("MAME_ROMPATH", "roms")) BASIC = ( NO_INTRO / "Sega - SG-1000 - SC-3000" / "BASIC Level III (Europe, Australia, New Zealand) (SC-3000) (Program).zip" ) @pytest.mark.slow @pytest.mark.parametrize(("board", "free"), [("level3", "12284"), ("level3b", "26620")]) def test_sc3000_basic_runs_a_typed_program(board, free): """Sega's BASIC Level III on the SC-3000, its keyboard typed on. The 16 KB cartridge (III A) and the 32 KB one (III B) report their own free memory; MAME 0.285's ``sc3000``, which has only the 32 KB board, reports 26620 too, and its screen after the same lines differs only at the blinking cursor. """ if not BASIC.exists(): pytest.skip("BASIC Level III is not in your romsets (NO_INTRO_DIR, MAME_ROMPATH)") sys.path.insert(0, str(ROOT / "src")) from sms_python.board import MACHINES from sms_python.cartridge import Cartridge from sms_python.io import keys_for from sms_python.system import System system = System(MACHINES["sc3000"], Cartridge.from_file(BASIC, board=board), audio=False) for _ in range(480): system.run_frame() for line in ("PRINT 6*7", '10 FOR I=1 TO 3:PRINT "HI";I:NEXT', "RUN"): for character in line + "\r": system.board.keyboard.keys.update(keys_for(character)) for _ in range(4): system.run_frame() system.board.keyboard.keys.clear() for _ in range(4): system.run_frame() for _ in range(120): system.run_frame() vdp = system.board.vdp base = (vdp.registers[2] & 0x0F) << 10 screen = [ bytes(vdp.vram[base + row * 40 : base + row * 40 + 40]).decode("latin-1").strip() for row in range(24) ] assert f"{free} Bytes free" in screen start = screen.index("PRINT 6*7") assert screen[start : start + 9] == [ "PRINT 6*7", "42", "Ready", '10 FOR I=1 TO 3:PRINT "HI";I:NEXT', "RUN", "HI 1", "HI 2", "HI 3", "Ready", ] # fmt: skip SF7000_IPL = MAME_ROMPATH / "sf7000.zip" DISK_BASIC = NO_INTRO / "Sega - SG-1000 - SC-3000" / "Disk BASIC (Japan) (SF-7000) (Program).zip" @pytest.mark.slow def test_sf7000_disk_basic_saves_and_loads_a_program(): """The SF-7000 boots Disk BASIC through its IPL, saves a typed program and loads it. The IPL checks the drive's speed by the index pulse before it reads a sector; SAVE writes three sectors (the directory, the allocation map and the file). MAME 0.285's ``sf7000``, typed the same lines, ends on the same frame with the same three sectors changed to the same bytes. """ if not (SF7000_IPL.exists() and DISK_BASIC.exists()): pytest.skip("no SF-7000 IPL ROM or Disk BASIC in NO_INTRO_DIR and MAME_ROMPATH") sys.path.insert(0, str(ROOT / "src")) import zipfile from sms_python.board import MACHINES from sms_python.cartridge import load_image, make_cartridge from sms_python.io import keys_for from sms_python.sf7000 import SECTOR_SIZE from sms_python.system import System with zipfile.ZipFile(SF7000_IPL) as archive: ipl = make_cartridge(archive.read("ipl.rom"), "rom") image = load_image(DISK_BASIC) disk = bytearray(image) system = System(MACHINES["sf7000"], None, ipl, audio=False, disk=disk) for _ in range(1200): system.run_frame() lines = ('10 FOR I=1 TO 3:PRINT "HI";I:NEXT', 'SAVE "T"', "NEW", "FILES", 'LOAD "T"', "RUN") for line in lines: for character in line + "\r": system.board.keyboard.keys.update(keys_for(character)) for _ in range(4): system.run_frame() system.board.keyboard.keys.clear() for _ in range(4): system.run_frame() for _ in range(300): system.run_frame() vdp = system.board.vdp base = (vdp.registers[2] & 0x0F) << 10 screen = [ bytes(vdp.vram[base + row * 40 : base + row * 40 + 40]).decode("latin-1").strip() for row in range(24) ] assert '"T . "' in screen start = screen.index('LOAD "T"') assert screen[start : start + 6] == ['LOAD "T"', "RUN", "HI 1", "HI 2", "HI 3", "Ready"] changed = [ index // SECTOR_SIZE for index in range(0, len(image), SECTOR_SIZE) if disk[index : index + SECTOR_SIZE] != image[index : index + SECTOR_SIZE] ] assert changed == [320, 332, 380]