#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run the 32xspd memory benchmark on the board and set its figures beside the hardware's. 32xspd (bakemono, "busting 32x performance myths", sega-16 forum, 2015; the ROM posted in jgenesis issue 493 as ``32xspd.zip``, SHA-1 of ``32xspd.32x`` fc456e2f...) runs eleven loops on the master SH-2, each for 30 frames (the 68000 counts VBlanks in COMM6), and prints KB/s = 16 x the 8 KB batches done. The photograph of a run on a 32X (sh2-python ``reference/docs/32xspd-hardware.jpg``; research tier: one console, one photograph, NTSC) is the only hardware measurement of the 32X's bus this repository has (docs/validation.md, "Milestone 5b"). Each loop moves 16 bytes per iteration in four longword accesses (``sparse``: one longword in each of four 16-byte lines), 512 iterations a batch, so SH-2 clocks per iteration = 30 frames' clocks / (KB/s / 16 x 512). python scripts/run_32xspd.py 32xspd.32x --bios-dir DIR [--frames 700] The results are read from SDRAM ($06002008-$06002030, where the program stores them), so no picture is needed: they are cleared once the master reaches the program (the boot ROM leaves other values there) and the run stops once the last one is stored. """ from __future__ import annotations import argparse import sys import time from pathlib import Path from megadrive_python.cartridge import load_image from sega32x_python.console import build, cartridge_for #: (SDRAM offset of the result, the program's name for the loop, KB/s in the photograph). TESTS = ( (0x2008, "cached read", 31840), (0x200C, "SDRAM read", 16304), (0x2010, "ROM read", 4704), (0x2014, "sparse SDRAM read", 6992), (0x2018, "sparse ROM read", 1328), (0x202C, "sparse SDRAM uncached", 7008), (0x2030, "sparse ROM uncached", 4800), (0x201C, "SDRAM write", 26608), (0x2020, "VRAM write", 8752), (0x2024, "SDRAM-VRAM copy", 7728), (0x2028, "ROM-VRAM copy", 3552), ) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("cartridge", type=Path) parser.add_argument("--bios-dir", type=Path, required=True) parser.add_argument("--machine", default="genesis") parser.add_argument("--frames", type=int, default=700) args = parser.parse_args() system, device = build(args.machine, cartridge_for(load_image(args.cartridge)), args.bios_dir) system.board.fm.enabled = False sh2 = device.sh2 assert sh2 is not None sdram = sh2.sdram start = time.time() def result(offset: int) -> int: return int.from_bytes(sdram[offset : offset + 4], "big") master = sh2.chips[0] cleared = False frame = 0 for frame in range(1, args.frames + 1): system.run_frame() if frame % 50 == 0: print(f"frame {frame}: {time.time() - start:.0f} s", flush=True) if not cleared: if master.pc & 0x1FFFF000 == 0x02001000: # the master runs the program for offset, _, _ in TESTS: sdram[offset : offset + 4] = bytes(4) cleared = True print(f"frame {frame}: the program runs; results cleared", flush=True) elif result(TESTS[-1][0]): break machine = system.machine window = 30 * machine.lines_per_frame * 3420 * 3 / 7 # SH-2 clocks in 30 frames print(f"{args.machine}, {frame} frames, {time.time() - start:.0f} s") print(f"{'loop':<24}{'board KB/s':>11}{'clk/iter':>10}{'hardware':>10}{'clk/iter':>10}") for offset, name, hardware in TESTS: board = result(offset) per = window / (board / 16 * 512) if board else float("nan") per_hw = window / (hardware / 16 * 512) print(f"{name:<24}{board:>11}{per:>10.2f}{hardware:>10}{per_hw:>10.2f}") return 0 if __name__ == "__main__": sys.exit(main())