Files
brad ArantandClaude Opus 5.5 62d0883a7f Bastion Raid: isometric scrolling shooter, graphics scroll and border
Ports 35h-37h scroll the graphics layer (wrapping at 320 x 240); bit 1 of
36h hides a border (4 columns, 2 rows) where a program draws the strip
coming into view unseen. The video loads its own column/row counters from
the scroll registers each line and frame. CLS resets both. Testbench
checks scroll, wrap and border.

games/raid: an original Zaxxon-style game in C - fly over a fortress that
scrolls diagonally, climb over low walls and through doorways in tall
ones, shoot turrets, fuel tanks, radar and fighters, destroy the reactor.
The world is drawn pixel by pixel from a formula, two columns and a row a
frame (about 40% of the Z-80). test-raid.py flies the level with an
autopilot and checks the frame budget; tools/boardemu.py is now shared by
both games' tests. send-hex.py sets the date before saving.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 10:00:25 -08:00

113 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""Load an Intel HEX file (as SDCC writes it) into the Basys 3 Z-80's memory
through the monitor, and optionally save it as a memory file in flash.
The board needs SW15 and SW14 up and BASIC at READY; nothing else may be
using the serial port, and the baud rate must match SW1:0. The tool types
MONITOR, sends every record (the monitor writes each one into memory),
saves the loaded range if asked, and leaves the monitor again.
tools/send-hex.py games/keep/keep.ihx --save KEEPGAME --baud 3000000
Code must go at 8000h or above. Memory files are loaded back with BLOAD in
BASIC or LOAD in the monitor.
"""
import argparse
import os
import re
import select
import termios
import time
BAUDS = {9600: termios.B9600, 115200: termios.B115200, 921600: termios.B921600,
3000000: termios.B3000000}
ap = argparse.ArgumentParser()
ap.add_argument("file", help="Intel HEX file")
ap.add_argument("--save", metavar="NAME", help="save the loaded bytes in flash under this name")
ap.add_argument("--port", default="/dev/ttyUSB1")
ap.add_argument("--baud", type=int, default=115200, choices=sorted(BAUDS))
ap.add_argument("--no-date", action="store_true", help="don't set BASIC's clock (which dates the file)")
a = ap.parse_args()
records = [l.strip() for l in open(a.file) if l.startswith(":")]
if not records or not records[-1].upper().startswith(":00000001"):
records.append(":00000001FF")
for r in records:
if len(r) > 79:
raise SystemExit("record too long for the monitor (79 characters): " + r[:20])
if a.save and not re.fullmatch(r"[A-Za-z][A-Za-z0-9]{0,7}", a.save):
raise SystemExit("a file name is 1-8 letters and digits, starting with a letter")
fd = os.open(a.port, os.O_RDWR | os.O_NOCTTY)
t = termios.tcgetattr(fd)
t[0] = termios.IXON
t[1] = 0
t[2] = termios.CS8 | termios.CREAD | termios.CLOCAL
t[3] = 0
t[4] = t[5] = BAUDS[a.baud]
t[6][termios.VMIN] = 0
t[6][termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSANOW, t)
termios.tcflush(fd, termios.TCIFLUSH)
buf = b""
def send(s):
os.write(fd, s.encode("ascii"))
def wait_for(pattern, timeout=10.0):
"""Read until the output since the last call matches; return it."""
global buf
end = time.time() + timeout
while time.time() < end:
m = re.search(pattern, buf, re.S)
if m:
out, buf = buf[:m.end()], buf[m.end():]
return out
r, _, _ = select.select([fd], [], [], 0.05)
if r:
buf += os.read(fd, 65536)
raise SystemExit("no answer from the board (waiting for %r); last output: %r" % (pattern, buf[-200:]))
# the monitor's command line, redrawn after each command; its message comes
# after the prompt, and the cursor is turned back on last
PROMPT = rb"\x1b\[30;1H> .*?\x1b\[\?25h"
send("\r")
time.sleep(0.3)
if not a.no_date: # the file gets the PC's date
send(time.strftime('DATE "%Y-%m-%d %H:%M:%S"\r'))
wait_for(rb"READY\r\n")
send("MONITOR\r")
wait_for(rb"Z-80 MONITOR")
wait_for(PROMPT)
n = 0
for r in records:
send(r.upper() + "\r")
out = wait_for(PROMPT)
if re.search(rb"BAD HEX|CHECKSUM|ROM: NOT|NO HEX", out):
send("X\r")
raise SystemExit("the monitor refused record %d: %s" % (n + 1, r))
n += 1
if n % 50 == 0:
print("\r%d/%d records" % (n, len(records)), end="", flush=True)
m = re.search(rb"HEX LOADED ([0-9A-F]{4})-([0-9A-F]{4})", out)
if not m:
send("X\r")
raise SystemExit("the monitor did not report the loaded range")
lo, hi = m.group(1).decode(), m.group(2).decode()
print("\rloaded %s-%s (%d bytes) from %d records" % (lo, hi, int(hi, 16) - int(lo, 16) + 1, len(records)))
if a.save:
send('SAVE "%s" %s %s\r' % (a.save.upper(), lo, hi))
out = wait_for(PROMPT, 30)
if b"SAVED" not in out:
send("X\r")
raise SystemExit("saving failed: %r" % out[-120:])
print('saved as "%s"; BLOAD "%s" in BASIC loads it at %sh' % (a.save.upper(), a.save.upper(), lo))
send("X\r")
wait_for(rb"\x1b\[\?47l", 5) # the monitor's screen goes away
os.close(fd)