Files
Basys3RiscV/basic/test-renum.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

125 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""Try BASIC's RENUM in tools/rv32emu.py, with the screen kept by pyte.
python3 test-renum.py
Also checks number printing and reading, which were rewritten to make room.
"""
import importlib.util
import os
import sys
import functools
print = functools.partial(print, flush=True)
HERE = os.path.dirname(os.path.abspath(__file__))
spec = importlib.util.spec_from_file_location("te", os.path.join(HERE, "test-edit.py"))
te = importlib.util.module_from_spec(spec)
spec.loader.exec_module(te)
CR = te.CR
def main():
b = te.Basic()
errors = 0
def check(what, ok, got=None):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
if not ok and got is not None:
print(" got: " + repr(got))
errors += not ok
def screen():
return "\n".join(l.rstrip() for l in b.screen_text().display)
def cmd(text):
"""Type a command and return what it printed."""
b.type(b"CLS" + CR)
b.type(text.encode() + CR)
out = screen().split(text, 1)[-1]
return out.replace("\nREADY", "").strip()
def load(prog):
b.type(b"NEW" + CR)
for line in prog.strip().splitlines():
b.type(line.strip().encode() + CR)
def listing():
return [l for l in cmd("LIST").splitlines() if l]
b.type(b"")
load('''
5 REM GOTO 5 STAYS AS TYPED
7 GOSUB 200
12 IF X < 3 THEN 7
13 IF X = 3 GOTO 40
40 ON X - 2 GOTO 41, 13, 999
41 PRINT "GOTO 12"; X: END
200 X = X + 1: RETURN
300 END
''')
before = cmd("RUN")
cmd("RENUM")
check("RENUM numbers from 10 in steps of 10, fixing GOTO, GOSUB, THEN and ON",
listing() == ['10 REM GOTO 5 STAYS AS TYPED',
'20 GOSUB 70',
'30 IF X < 3 THEN 20',
'40 IF X = 3 GOTO 50',
'50 ON X - 2 GOTO 60, 40, 999',
'60 PRINT "GOTO 12"; X: END',
'70 X = X + 1: RETURN',
'80 END'], listing())
check("a reference to a missing line is left alone", "GOTO 60, 40, 999" in "\n".join(listing()))
check("the program runs the same afterwards", cmd("RUN") == before and before == "GOTO 12 3", before)
cmd("RENUM 1000, 5")
l = listing()
check("RENUM start, step", l[0].startswith("1000 ") and l[-1] == "1035 END" and
"1010 IF X < 3 THEN 1005" in l and "1020 ON X - 2 GOTO 1025, 1015, 999" in l, l)
cmd("RENUM 1, 1")
l = listing()
check("line numbers can get shorter", "3 IF X < 3 THEN 2" in l and "5 ON X - 2 GOTO 6, 4, 999" in l, l)
check("still runs", cmd("RUN") == "GOTO 12 3")
load('''
1 GOTO 2
2 RUN 3
3 GOSUB 1
''')
cmd("RENUM 10000, 10000")
check("RUN n is renumbered too", listing() == ["10000 GOTO 20000", "20000 RUN 30000", "30000 GOSUB 10000"], listing())
out = cmd("RENUM 60000, 3000")
check("numbers past 65535 are refused", "ERROR" in out.upper() or "ILLEGAL" in out.upper(), out)
check("and the program is unchanged", listing() == ["10000 GOTO 20000", "20000 RUN 30000", "30000 GOSUB 10000"], listing())
out = cmd("RENUM 10, 0")
check("step 0 is refused", listing()[0].startswith("10000"), out)
load("10 PRINT 1\n20 GOTO 10")
load("")
check("RENUM on an empty program is fine", cmd("RENUM") == "" and listing() == [])
# a line that would grow past 255 bytes
refs = ",".join(["1"] * 115) # typed lines stop at 250 characters
load("1 ON X GOTO " + refs)
out = cmd("RENUM 10000")
check("a line that would grow too long is refused", "LONG" in out.upper(), out)
check("and is left as it was", ("1 ON X GOTO " + refs) in cmd("LIST").replace("\n", ""))
# number printing and reading (rewritten to make room); the screen wraps
# at 80 columns, so compare the words only
def nums(text):
b.type(b"CLS" + CR)
b.type(text.encode() + CR)
return " ".join("".join(b.screen_text().display).split()).split(text, 1)[-1].replace("READY", "").strip()
out = nums('PRINT 0; 1; -1; .5; 1/3; .001; .0001; 123.456; 999999; 1000000; 1E30; -2.5E-12; 65535; 12345678')
check("numbers print as before",
out == "0 1 -1 .5 .333333 .001 1E-04 123.456 999999 1E+06 1E+30 -2.5E-12 65535 1.23457E+07", out)
out = nums("PRINT 1.5E3; 12.5E-1; 1234567890123; .000123; 3.; 00042.50")
check("numbers are read as before", out == "1500 1.25 1.23457E+12 1.23E-04 3 42.5", out)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()