"""Build alewman-probe: font and data, assemble in WSL, patch size and checksum. python build.py -> out/alewman-probe.bin python build.py --hang -> out/alewman-probe-hang.bin, with a test that never returns python build.py --late -> out/alewman-probe-late.bin, only the last three tests, for a die run that ran short python build.py --reset -> out/alewman-probe-reset.bin, no tests, every boot logged: for emulators, which hold no A python build.py --qr-cycle -> out/alewman-probe-qrcycle.bin, the QR pages turn by themselves: to check them on a board python build.py --wedge -> out/alewman-probe-wedge.bin, with WEDG, which hangs the console and the die: for emulators python build.py --soak N -> out/alewman-probe-soak.bin, a long run of N cycles with no menu: for emulators python build.py --audio -> out/alewman-probe-audio.bin, straight into the audio test: for emulators """ import json import struct import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parent GEN, OUT = ROOT / "gen", ROOT / "out" SRC = ROOT / "src" # 5x7, columns left to right, bit 0 the top row GLYPHS = { " ": "00 00 00 00 00", "0": "3E 51 49 45 3E", "1": "00 42 7F 40 00", "2": "42 61 51 49 46", "3": "21 41 45 4B 31", "4": "18 14 12 7F 10", "5": "27 45 45 45 39", "6": "3C 4A 49 49 30", "7": "01 71 09 05 03", "8": "36 49 49 49 36", "9": "06 49 49 29 1E", "A": "7E 11 11 11 7E", "B": "7F 49 49 49 36", "C": "3E 41 41 41 22", "D": "7F 41 41 22 1C", "E": "7F 49 49 49 41", "F": "7F 09 09 09 01", "G": "3E 41 49 49 7A", "H": "7F 08 08 08 7F", "I": "00 41 7F 41 00", "J": "20 40 41 3F 01", "K": "7F 08 14 22 41", "L": "7F 40 40 40 40", "M": "7F 02 0C 02 7F", "N": "7F 04 08 10 7F", "O": "3E 41 41 41 3E", "P": "7F 09 09 09 06", "Q": "3E 41 51 21 5E", "R": "7F 09 19 29 46", "S": "46 49 49 49 31", "T": "01 01 7F 01 01", "U": "3F 40 40 40 3F", "V": "1F 20 40 20 1F", "W": "3F 40 38 40 3F", "X": "63 14 08 14 63", "Y": "07 08 70 08 07", "Z": "61 51 49 45 43", ":": "00 36 36 00 00", "<": "08 14 22 41 00", ">": "00 41 22 14 08", "-": "08 08 08 08 08", ".": "00 60 60 00 00", ",": "00 50 30 00 00", "/": "20 10 08 04 02", "(": "00 1C 22 41 00", ")": "00 41 22 1C 00", "=": "14 14 14 14 14", "!": "00 00 5F 00 00", "%": "23 13 08 64 62", "\\": "7F 7F 7F 7F 7F", } def tile(columns): rows = [] for y in range(8): v = 0 for x, c in enumerate(columns): if c >> y & 1: v |= 1 << (28 - 4 * (x + 1)) rows.append(v) return rows def font(): lines = [] for code in range(0x20, 0x60): columns = [int(h, 16) for h in GLYPHS.get(chr(code), GLYPHS[" "]).split()] lines.append("\t.long " + ", ".join(f"0x{v:08X}" for v in tile(columns))) (GEN / "font.inc").write_text("\n".join(lines) + "\n") def data(): # 8 KiB of a fixed pattern: the source of every DMA (GEN / "data.bin").write_bytes(bytes((i * 37 + 11) & 0xFF for i in range(0x2000))) def sine(): # one period of a sine, 256 steps, -127..127: the audio test's waves import math vals = [round(127 * math.sin(2 * math.pi * i / 256)) for i in range(256)] (GEN / "sine.h").write_text("static const signed char sine[256] = {\n" + ",\n".join( ", ".join(str(v) for v in vals[r:r + 16]) for r in range(0, 256, 16)) + "\n};\n") def wsl_path(p): return "/mnt/" + str(p)[0].lower() + str(p)[2:].replace("\\", "/") def wsl(*args): subprocess.run(["wsl", "-e", *args], check=True, cwd=ROOT) #: The save, one setting: bytes kept, and how many past runs it holds. SAVE_BYTES = 32768 RUN_SLOTS = 1 DSRC = 0x2000 #: Frames per VINT the cart counted, x256, and VINTs counted in one cycle: from board `genesis` #: (tests mask interrupts, so a cycle's VINTs undercount its frames). For the clock on screen. TICK_SCALE = 347 CYCLE_TICKS = 3837 CFLAGS = ["-m68000", "-O2", "-fno-store-merging", "-std=gnu11", "-Wall", "-Wextra", "-ffreestanding", "-fno-pie", "-fno-pic", "-fno-stack-protector", "-fno-asynchronous-unwind-tables", "-fomit-frame-pointer", "-fno-jump-tables"] SYMBOLS = {"SAVE_BYTES": SAVE_BYTES, "DSRC": DSRC} def git_id(): r = subprocess.run(["git", "rev-parse", "--short=7", "HEAD"], cwd=ROOT, capture_output=True, text=True) return (r.stdout.strip() if r.returncode == 0 else "nogit").ljust(8)[:8].encode() def predictions(): """Each test's first value on board `genesis`, by position: see tools/predict.py.""" p = ROOT / "predict.json" return json.loads(p.read_text()) if p.exists() else [] EXTRA = [a[2:] for a in sys.argv if a.startswith("-D")] # e.g. -DDIE_CHECK, for one-off check ROMs def main(): hang = "--hang" in sys.argv late = "--late" in sys.argv rst = "--reset" in sys.argv qrc = "--qr-cycle" in sys.argv wedge = "--wedge" in sys.argv soak = int(sys.argv[sys.argv.index("--soak") + 1]) if "--soak" in sys.argv else 0 audio = "--audio" in sys.argv GEN.mkdir(exist_ok=True) OUT.mkdir(exist_ok=True) font() data() sine() (GEN / "settings.h").write_text(f"#define SAVE_BYTES {SAVE_BYTES}\n#define RUN_SLOTS {RUN_SLOTS}\n#define DSRC {DSRC}\n#define TICK_SCALE {TICK_SCALE}\n#define CYCLE_TICKS {CYCLE_TICKS}\n") objs = [] for s_ in ("boot.s", "tests.s", "tests2.s", "tests3.s", "tests4.s", "lib68k.s"): o = OUT / (s_[:-2] + ".o") wsl("m68k-linux-gnu-as", "-m68000", "--register-prefix-optional", "-I", wsl_path(ROOT), *[f"--defsym={k}={v}" for k, v in SYMBOLS.items()], "-o", wsl_path(o), wsl_path(SRC / s_)) objs.append(o) for c in ("main.c", "qr.c", "libc.c"): o = OUT / (c[:-2] + ".o") wsl("m68k-linux-gnu-gcc", *CFLAGS, *(["-DHANG_TEST"] if hang else []), *(["-DLATE_ONLY"] if late else []), *(["-DRESET_ONLY"] if rst else []), *(["-DQR_CYCLE"] if qrc else []), *(["-DWEDGE_TEST"] if wedge else []), *([f"-DSOAK_AUTO={soak}"] if soak else []), *(["-DAUDIO_AUTO"] if audio else []), *[f"-D{d}" for d in EXTRA], "-I", wsl_path(SRC), "-I", wsl_path(GEN), "-c", wsl_path(SRC / c), "-o", wsl_path(o)) objs.append(o) elf, raw = OUT / "probe.elf", OUT / ("alewman-probe-hang.bin" if hang else "alewman-probe-late.bin" if late else "alewman-probe-reset.bin" if rst else "alewman-probe-qrcycle.bin" if qrc else "alewman-probe-wedge.bin" if wedge else "alewman-probe-soak.bin" if soak else "alewman-probe-audio.bin" if audio else "alewman-probe.bin") wsl("m68k-linux-gnu-ld", "-z", "noexecstack", "-T", wsl_path(SRC / "link.ld"), "-Map", wsl_path(OUT / "probe.map"), "-o", wsl_path(elf), *[wsl_path(o) for o in objs]) wsl("m68k-linux-gnu-objcopy", "-O", "binary", wsl_path(elf), wsl_path(raw)) rom = bytearray(raw.read_bytes()) i = rom.index(b"BUILDID0") rom[i:i + 8] = git_id() i = rom.index(b"PRED") table = predictions() rom[i:i + 4] = b"\xFF\xFF\xFF\xFF" for n, v in enumerate(table): struct.pack_into(">I", rom, i + 4 * n, 0xFFFFFFFF if v is None else v) rom += bytes(-len(rom) % 0x10000) struct.pack_into(">I", rom, 0x1A4, len(rom) - 1) total = sum(struct.unpack_from(">H", rom, n)[0] for n in range(0x200, len(rom), 2)) & 0xFFFF struct.pack_into(">H", rom, 0x18E, total) raw.write_bytes(rom) print(raw, len(rom), "bytes, checksum", hex(total), "predictions", len(table)) if __name__ == "__main__": main()