#!/usr/bin/env python3 """Run FluBBa's SMS VDP Test v1.31 on our system and print every result page. The test is menu-driven: button 1 on the first entry runs the SMS tests, each page of results waits for a button, and the last page returns to the menu. This script presses the buttons through the joypad model and reads each page back as text from the name table: the program writes character code - 32 as the tile index with a zero high byte (``StdOut_Char`` in ``testmain.asm``). python scripts/run_vdptest.py # SMS2 VDP python scripts/run_vdptest.py --machine sms1 # SMS1 VDP FluBBa developed the expected results on real PAL SMS1, SMS2 and Mega Drive hardware (docs/validation.md); every line should end "Ok." (or "NTSC Ok." for the V counter values on an NTSC machine). """ from __future__ import annotations import argparse import sys import zipfile from dataclasses import replace from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) from sms_python.board import MACHINES from sms_python.cartridge import Cartridge from sms_python.io import BUTTON_1, DOWN from sms_python.system import System from sms_python.vdp import VDPTiming ROOT = Path(__file__).resolve().parents[1] ARCHIVE = ROOT / "tests" / "test_roms" / "smspower" / "SMSVDPTest-SMS-1.31.zip" # enik2's build with Game Gear 2-ASIC and 1-ASIC tests (forum thread 10695, 2019). GG_ARCHIVE = ROOT / "tests" / "test_roms" / "smspower-forum" / "VDPTEST_GG.zip" # Its menu: 0 SMS, 1 GG 2 ASIC, 2 GG 1 ASIC, then the MD tests. GG_MENU = {"sms": 0, "gg2asic": 1, "gg1asic": 2} def screen_text(system: System) -> list[str]: """The 32x24 name table at $3800 as text (tile index + 32 = character).""" vram = system.board.vdp.vram rows = [] for row in range(24): chars = [] for column in range(32): low, high = vram[0x3800 + row * 64 + column * 2 : 0x3800 + row * 64 + column * 2 + 2] code = low + 32 chars.append(chr(code) if high == 0 and 32 <= code < 127 else " ") rows.append("".join(chars).rstrip()) return rows def press(system: System, frames_down: int = 6, frames_up: int = 6, button: int = BUTTON_1) -> None: pad = system.board.io.pad1 pad.pressed.add(button) for _ in range(frames_down): system.run_frame() pad.pressed.discard(button) for _ in range(frames_up): system.run_frame() def settle(system: System, limit: int = 600) -> list[str]: """Run until the screen shows words and stops changing for 30 frames; return it.""" last, steady = None, 0 for _ in range(limit): system.run_frame() text = screen_text(system) worded = any(sum(c.isalpha() for c in line) >= 4 for line in text) steady = steady + 1 if text == last and worded else 0 last = text if steady >= 30: break return last or [] def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--machine", choices=sorted(MACHINES), default="sms2") parser.add_argument("--pages", type=int, default=12, help="result pages to read at most") parser.add_argument( "--timing", help="override the VDP's timing for fitting: origin,frame_irq,line_irq,flags,collision", ) parser.add_argument( "--gg-test", choices=sorted(GG_MENU), help="run enik2's VDPTEST_GG and pick this menu entry", ) parser.add_argument( "--menu", type=int, help="pick this menu entry (0 is the first) instead: FluBBa's ROM lists its SMS " "tests, then its Mega Drive ones", ) args = parser.parse_args() archive_path, member = ( (GG_ARCHIVE, "VDPTEST_GG.sms") if args.gg_test else (ARCHIVE, "VDPTEST.sms") ) with zipfile.ZipFile(archive_path) as archive: image = archive.read(member) machine = MACHINES[args.machine] if args.timing: timing = VDPTiming(*(int(v) for v in args.timing.split(","))) machine = replace(machine, vdp=replace(machine.vdp, timing=timing)) system = System(machine, Cartridge(image), audio=False) menu = settle(system) print("\n".join(menu), "\n" + "-" * 32) entry = args.menu if args.menu is not None else GG_MENU.get(args.gg_test, 0) for _ in range(entry): press(system, button=DOWN) press(system) # button 1 runs the entry under the cursor results: list[str] = [] for _ in range(args.pages): page = settle(system) if page == menu: break print("\n".join(page), "\n" + "-" * 32, flush=True) results += [line for line in page if line.endswith(("Ok.", "Error."))] press(system) errors = [line for line in results if line.endswith("Error.")] print(f"{len(results)} results, {len(errors)} errors on {system.board.machine.name}") for line in errors: print(" " + line.strip()) return 1 if errors else 0 if __name__ == "__main__": sys.exit(main())