Files
Basys3RiscV/games/warden/test-warden.py
T
brad ArantandClaude Opus 5.5 2dafda5fe5 Run the CPU at 100 MHz
The FPU picks the FADD/FSUB operands and works out exponents and leading
zeros as it takes them, and finds the back end's leading one a clock
before using it; those were the paths that held the design to 91 MHz.
Add, multiply and the multiply-adds now take 13 clocks, divide and
square root 37, int -> float 9. Setup slack at 100 MHz is +0.286 ns.
On the board: an empty FOR loop 6.4 us (was 8.3), SIN 33 us (was 37).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 07:26:59 -08:00

117 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""Run Star Warden in tools/rv32emu.py on a model of the board (tools/boardemu.py)
and take screenshots.
python3 test-warden.py [--shots DIR] [--play]
It checks the title screen, measures the Z-80 instructions each frame takes
(a 60 Hz frame has about 610,000 at 100 MHz), flies along the planet checking
that the picture scrolls and the scanner fills in, and with --play lets an
autopilot hunt down every enemy until the wave is cleared. The autopilot
can't be killed (it keeps the ship shielded), so this checks the rules and the
waves, not its skill.
"""
import argparse
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "..", "tools"))
from boardemu import Board, load_ihx, load_font, load_syms # noqa: E402
UP, DOWN, LEFT, RIGHT, FIRE, ENTER, SHIFT, ESC = 1, 2, 4, 8, 16, 32, 64, 128
WORLD = 1280
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--shots", help="write PNG screenshots here")
ap.add_argument("--play", action="store_true", help="let the autopilot clear a wave")
a = ap.parse_args()
mem, hi = load_ihx(os.path.join(HERE, "warden.ihx"))
b = Board(mem, code_end=hi)
font = load_font() if a.shots else None
if a.shots:
os.makedirs(a.shots, exist_ok=True)
sym = {"_" + n: a for n, a in load_syms(os.path.join(HERE, "out", "warden.sym")).items()}
errors = 0
def check(what, ok):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
errors += not ok
def u16(n, k=0):
return b.m[sym[n] + 2 * k] | b.m[sym[n] + 2 * k + 1] << 8
def u8(n, k=0):
return b.m[sym[n] + k]
def screen():
return b.screen_text().display
b.run_frames(5)
check("title screen", "S T A R W A R D E N" in "\n".join(screen()))
if a.shots: b.shot(os.path.join(a.shots, "1-title.png"), font)
b.run_frames(3, FIRE)
b.run_frames(5)
check("game starts", "WAVE 1" in screen()[0] and "SHIPS 3" in screen()[0])
c0 = b.steps
b.run_frames(60, RIGHT)
per = (b.steps - c0) / 60
print("%.0f instructions a frame while flying (%.0f%% of a 60 Hz frame)" % (per, per / 5500))
check("a frame fits in a 60th of a second", per < 500000)
check("flying right scrolls the planet", b.scroll_x != 0)
check("the scanner shows enemies", sum(r.count("o") for r in screen()[1:4]) >= 4)
if a.shots: b.shot(os.path.join(a.shots, "2-flying.png"), font)
s0 = b.scroll_x
b.run_frames(80, LEFT)
check("turning round scrolls the other way", b.scroll_x != s0)
if a.shots: b.shot(os.path.join(a.shots, "3-turned.png"), font)
if a.play:
b.m[sym["_shield"]] = 1
worst = 0
frame = 0
while frame < 9000 and b.pc != 0:
b.m[sym["_shield"]] = 1
px, py = u16("_px"), u8("_py")
best, bd = None, 9999
for k in range(16):
if u8("_etype", k):
ex = u16("_ex", k) >> 4
d = (ex - px + WORLD // 2) % WORLD - WORLD // 2
if abs(d) < abs(bd):
bd, best = d, k
j = 0
if best is not None:
ey = u8("_ey", best)
if ey < py - 1: j |= UP
elif ey > py + 1: j |= DOWN
if abs(bd) > 90 or (bd > 0) != (b.spr[0][3] & 2 == 0):
j |= RIGHT if bd > 0 else LEFT
if abs(ey - py) < 6 and abs(bd) < 260:
j |= FIRE if frame % 2 else 0
c0 = b.steps
b.run_frames(1, j)
worst = max(worst, b.steps - c0)
frame += 1
if frame == 120 and a.shots: b.shot(os.path.join(a.shots, "4-battle.png"), font)
if "WAVE 2" in screen()[0]:
break
print(screen()[0])
check("autopilot cleared wave 1 in %d frames" % frame, "WAVE 2" in screen()[0])
print("slowest frame: %d instructions (%.0f%% of a 60 Hz frame)" % (worst, worst / 5500))
check("every frame fits in a 60th of a second", worst < 550000)
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc gives up the game", "PRESS SPACE TO BEGIN" in "\n".join(screen()))
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc returns to BASIC", b.pc == 0)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()