Files
Basys3Terminal/tools/cputest.py
T
brad ArantandClaude Opus 5.5 59d5e617f2 Z-80 computer with Dartmouth-style BASIC as the terminal's console
SW15 switches the terminal from the PC's serial line to a Z-80 running at
90 MHz from its own clock, with 64 KB of block RAM preloaded with BASIC.

- rtl/z80_cpu.v: Z-80 built for speed (one clock per memory access, next
  byte read during decode), full documented instruction set plus the
  common undocumented forms; no interrupts.
- rtl/z80_system.v: RAM, console/timer/7-segment ports, toggle handshakes
  to the terminal clock.
- basic/: BASIC interpreter in C for SDCC, start-up code, prebuilt image.
- tools/z80emu.py, tools/cputest.py, scripts/cpusim.sh: reference emulator
  and random tests comparing the CPU after every instruction.
- SW14 feeds the PC's UART into the Z-80 for pasting programs; BTND
  resets the Z-80 only.

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

104 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Random instruction test for the CPU: writes cputest.bin/.mem.
Each block loads random values into the registers (pointers aim at a scratch
area), then runs one random instruction. Control flow instructions branch to
the next block either way, so the program always runs straight through.
"""
import random
import sys
rnd = random.Random(int(sys.argv[1]) if len(sys.argv) > 1 else 1)
blocks = int(sys.argv[2]) if len(sys.argv) > 2 else 3000
code = bytearray()
def w16(v):
return [v & 0xFF, v >> 8 & 0xFF]
def ptr():
return 0x8000 + rnd.randrange(0x40, 0x1C0)
def setup(block_op):
c = [0x31] + w16(0xF000) # LD SP,F000
c += [0x01] + w16(rnd.randrange(65536)) + [0xC5, 0xF1] # random AF
c += [0x01] + w16(rnd.randrange(1, 4) if block_op else ptr())
c += [0x11] + w16(ptr()) + [0x21] + w16(ptr())
c += [0xDD, 0x21] + w16(ptr()) + [0xFD, 0x21] + w16(ptr())
return c
def main_op():
"""A random non-prefixed opcode with its operands, or None to retry."""
op = rnd.randrange(256)
x, y, z = op >> 6, op >> 3 & 7, op & 7
if op in (0x76, 0xCB, 0xDD, 0xED, 0xFD):
return None
if op in (0xC9, 0xE9, 0xD3, 0xDB) or (x == 3 and z in (0, 7)): # RET, JP (HL), I/O, RST
return None
if op in (0x10, 0x18) or (x == 0 and z == 0 and y >= 4):
return [op, 0] # JR/DJNZ to the next byte
if op == 0x22 or op == 0x2A or op == 0x32 or op == 0x3A:
return [op] + w16(ptr())
if x == 0 and z == 1 and op & 8 == 0:
return [op] + w16(rnd.randrange(65536))
if x == 0 and z == 6:
return [op, rnd.randrange(256)]
if x == 3 and z == 6:
return [op, rnd.randrange(256)]
if x == 3 and z in (2, 4) or op in (0xC3, 0xCD):
return [op, None, None] # target filled in later
return [op]
def instruction():
kind = rnd.randrange(10)
if kind < 5:
while (c := main_op()) is None:
pass
return c, False
if kind < 7:
while (c := main_op()) is None:
pass
if c[0] == 0xF9: # LD SP,IX is fine too
pass
c = [rnd.choice((0xDD, 0xFD))] + c
op = c[1]
if op >> 6 in (1, 2) and (op & 7 == 6 or (op >> 6 == 1 and op >> 3 & 7 == 6)) \
or op in (0x34, 0x35):
c.insert(2, rnd.randrange(256)) # (IX+d) displacement
elif op == 0x36:
c[2:3] = [rnd.randrange(256), rnd.randrange(256)]
return c, False
if kind == 7:
return [0xCB, rnd.randrange(256)], False
if kind == 8:
return [rnd.choice((0xDD, 0xFD)), 0xCB, rnd.randrange(256), rnd.randrange(256)], False
while True:
op = rnd.randrange(0x40, 0xC0)
x, y, z = op >> 6, op >> 3 & 7, op & 7
if x == 1 and z in (0, 1) or (x == 1 and z == 5) or (x == 1 and z == 7 and y == 1):
continue # I/O, RETN, LD R,A
if x == 2 and not (y >= 4 and z <= 1):
continue # only LDI/LDD/CPI/CPD (+R)
if x == 1 and z == 3:
return [0xED, op] + w16(ptr()), False
return [0xED, op], x == 2
for _ in range(blocks):
ins, blk = instruction()
code += bytes(setup(blk))
if None in ins:
end = len(code) + len(ins)
ins = [v if v is not None else 0 for v in ins]
ins[-2:] = w16(end)
code += bytes(ins)
code += b"\x76"
assert len(code) < 0x8000
open("cputest.bin", "wb").write(code)
open("cputest.mem", "w").write("".join("%02x\n" % b for b in code))
print("%d bytes" % len(code))