Files
Basys3RiscV/games/swarm/gen.py
T
brad ArantandClaude Opus 5.5 5930b958b4 Basys 3 RISC-V computer
The Basys 3 Z-80 computer with the Z-80 replaced by an RV32IM processor
written from scratch (rtl/rv32_cpu.v): multi-cycle, 90 MHz, CSRs and
traps, interrupts, two hardware triggers and a resumable NMI for the
monitor. Passes riscv-tests rv32ui/um/mi.

rv32_system.v keeps all the Z-80 machine's peripherals, with I/O port N
at FFFFFF00h + N, 80 KB of RAM with BASIC preloaded, and a 32 KB monitor
ROM. BASIC, the five games and the monitor (with an RV32 disassembler,
assembler, breakpoints and single step) are ported to RISC-V GCC. Python
emulator and tests in tools/; full-system simulation in sim/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 18:09:18 -08:00

136 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""Make data.h for swarm.c: the sprite shapes (also drawn into the graphics
for the aliens waiting in formation) and a sine table for the dives.
Shapes are 16 rows of 16 hex digits, one colour per pixel (0 is see-through).
"""
import math
import random
import sys
Z = "0" * 16
DRONE_A = [Z, Z,
"000C00000000C000",
"0000C000000C0000",
"00000CCCCCC00000",
"0000CC4CC4CC0000",
"000CCCCCCCCCC000",
"0CCCCCCCCCCCCCC0",
"0C0CCCCCCCCCC0C0",
"0C0C00000000C0C0",
"0000CC0000CC0000",
] + [Z] * 5
DRONE_B = [Z, Z,
"0000C000000C0000",
"0000C000000C0000",
"00000CCCCCC00000",
"0000CC4CC4CC0000",
"000CCCCCCCCCC000",
"00CCCCCCCCCCCC00",
"0CCCCCCCCCCCCCC0",
"C00C00000000C00C",
"000CC000000CC000",
] + [Z] * 5
FLAG_A = [Z,
"0000000BB0000000",
"000000BBBB000000",
"00B00BB99BB00B00",
"00BB0BBBBBB0BB00",
"00BBBBBBBBBBBB00",
"000BB9BBBB9BB000",
"0000BBBBBBBB0000",
"00000B9BB9B00000",
"0000B000000B0000",
"000B00000000B000",
] + [Z] * 5
FLAG_B = [Z,
"0000000BB0000000",
"000000BBBB000000",
"00000BB99BB00000",
"B0BB0BBBBBB0BB0B",
"0BBBBBBBBBBBBBB0",
"000BB9BBBB9BB000",
"0000BBBBBBBB0000",
"00000B9BB9B00000",
"00000B0000B00000",
"0000B000000B0000",
] + [Z] * 5
def recolour(img, table):
return [row.translate(str.maketrans(table)) for row in img]
WARRIOR_A, WARRIOR_B = recolour(DRONE_A, {"C": "D", "4": "B"}), recolour(DRONE_B, {"C": "D", "4": "B"})
ESCORT_A, ESCORT_B = recolour(DRONE_A, {"C": "9", "4": "F"}), recolour(DRONE_B, {"C": "9", "4": "F"})
SHIP = [Z,
"0000000FF0000000",
"0000000FF0000000",
"000000FFFF000000",
"000000F99F000000",
"00F00FFFFFF00F00",
"00F0FFFFFFFF0F00",
"00FFFFFFFFFFFF00",
"00FFF99FF99FFF00",
"00FF00FFFF00FF00",
] + [Z] * 6
SHOT = [Z] * 5 + ["0000000BB0000000"] * 6 + [Z] * 5
BULLET = [Z] * 6 + ["00000009F0000000", "0000000F90000000", "000000099000000" + "0"] + [Z] * 7
def burst(seed, cols, size):
r = random.Random(seed)
img = [["0"] * 16 for _ in range(16)]
for _ in range(80):
a = r.uniform(0, 2 * math.pi)
d = r.uniform(0, size)
x, y = int(8 + d * math.cos(a)), int(8 + d * math.sin(a))
if 0 <= x < 16 and 0 <= y < 16:
img[y][x] = cols[0] if d < size / 3 else cols[1] if d < 2 * size / 3 else cols[2]
return ["".join(row) for row in img]
BOOM1 = burst(21, "FB9", 5)
BOOM2 = burst(23, "B91", 7.5)
SHIPBOOM = burst(25, "FFB", 7.9)
def stars(seed):
r = random.Random(seed)
img = [["0"] * 16 for _ in range(16)]
for _ in range(2):
img[r.randrange(16)][r.randrange(16)] = r.choice("FE7BD6")
return ["".join(row) for row in img]
SHAPES = [("S_DRONE", DRONE_A), ("S_DRONE2", DRONE_B), ("S_WARRIOR", WARRIOR_A), ("S_WARRIOR2", WARRIOR_B),
("S_ESCORT", ESCORT_A), ("S_ESCORT2", ESCORT_B), ("S_FLAG", FLAG_A), ("S_FLAG2", FLAG_B),
("S_SHIP", SHIP), ("S_SHOT", SHOT), ("S_BULLET", BULLET),
("S_BOOM1", BOOM1), ("S_BOOM2", BOOM2), ("S_SHIPBOOM", SHIPBOOM),
("S_STARS1", stars(1)), ("S_STARS2", stars(2)), ("S_STARS3", stars(3))]
def main(out):
L = ["/* made by gen.py - edit that instead */", ""]
for i, (name, img) in enumerate(SHAPES):
assert len(img) == 16 and all(len(row) == 16 for row in img), name
L.append("#define %s %d" % (name, i))
L.append("#define N_SHAPES %d" % len(SHAPES))
L.append("static const u8 shape_data[N_SHAPES * 128] = {")
for name, img in SHAPES:
b = [int(row[x], 16) << 4 | int(row[x + 1], 16) for row in img for x in range(0, 16, 2)]
L.append(" " + ",".join(str(v) for v in b) + ", /* %s */" % name)
L.append("};")
# 64 steps round the circle, 0 = right, 16 = down; scaled by 64
L.append("static const i8 sin64[64] = {")
L.append(" " + ",".join(str(int(round(64 * math.sin(2 * math.pi * k / 64)))) for k in range(64)))
L.append("};")
open(out, "w").write("\n".join(L) + "\n")
print("data.h: %d shapes" % len(SHAPES))
if __name__ == "__main__":
main(sys.argv[1] if len(sys.argv) > 1 else "data.h")