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>
136 lines
4.9 KiB
Python
136 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Try BASIC's line editor in tools/rv32emu.py, with the screen kept by pyte.
|
|
|
|
python3 test-edit.py
|
|
|
|
Types lines the way the keyboard sends them (cursor keys as ESC [ C and so
|
|
on), checks what the screen shows while a line is edited, and checks the
|
|
program LIST prints afterwards. Needs pyte.
|
|
"""
|
|
import os
|
|
import sys
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
sys.path.insert(0, os.path.join(HERE, "..", "tools"))
|
|
from boardemu import Board # noqa: E402
|
|
|
|
RIGHT, LEFT, UP = b"\x1b[C", b"\x1b[D", b"\x1b[A"
|
|
HOME, END, DEL = b"\x1b[1~", b"\x1b[4~", b"\x1b[3~"
|
|
BS, CTRL_U, CR = b"\x7f", b"\x15", b"\r"
|
|
|
|
|
|
class Basic(Board):
|
|
def __init__(self):
|
|
mem = open(os.path.join(HERE, "basic.bin"), "rb").read()
|
|
super().__init__(mem, start=0, rom_top=0xA000)
|
|
self.idle = 0
|
|
|
|
def io_in(self, port):
|
|
if port & 0xFF == 0 and not self.keys_in:
|
|
self.idle += 1
|
|
return super().io_in(port)
|
|
|
|
def io_out(self, port, v):
|
|
self.idle = 0 # printing reads the status port too
|
|
super().io_out(port, v)
|
|
|
|
def type(self, keys):
|
|
"""Send keys one at a time and run until BASIC waits for the next."""
|
|
for k in [keys[i:i + 1] for i in range(len(keys))]:
|
|
self.keys_in += k
|
|
self.idle = 0
|
|
while self.idle < 50:
|
|
self.step()
|
|
|
|
|
|
def main():
|
|
b = Basic()
|
|
errors = 0
|
|
|
|
def check(what, ok):
|
|
nonlocal errors
|
|
print(("PASS " if ok else "FAIL ") + what)
|
|
errors += not ok
|
|
|
|
def screen():
|
|
return b.screen_text()
|
|
|
|
def lines():
|
|
return "\n".join(l.rstrip() for l in screen().display)
|
|
|
|
def row(r):
|
|
return screen().display[r].rstrip()
|
|
|
|
def cur():
|
|
s = screen()
|
|
return s.cursor.y, s.cursor.x
|
|
|
|
b.type(b"")
|
|
b.type(b'10 PRINT "HELLO"' + CR + b"20 GOTO 10" + CR)
|
|
b.type(b"PRINT PEEK(40961)" + CR) # line 10's number, low byte
|
|
check("the program still starts at A000h", lines().rstrip().endswith(" 10\n\nREADY"))
|
|
y = cur()[0]
|
|
b.type(b"10" + RIGHT)
|
|
check("right after 10 copies one character of line 10", row(y) == "10" and cur() == (y, 3))
|
|
b.type(RIGHT * 2)
|
|
check("each right copies one more", row(y) == "10 PR" and cur() == (y, 5))
|
|
b.type(END)
|
|
check("End copies the rest", row(y) == '10 PRINT "HELLO"' and cur() == (y, 16))
|
|
b.type(LEFT * 6) # cursor onto the H
|
|
check("left moves the cursor back", cur() == (y, 10))
|
|
b.type(b"BIG ")
|
|
check("typing inserts in the middle", row(y) == '10 PRINT "BIG HELLO"' and cur() == (y, 14))
|
|
b.type(HOME + RIGHT * 3 + DEL * 6)
|
|
check("Home and Delete", row(y) == '10 "BIG HELLO"' and cur() == (y, 3))
|
|
b.type(b"PRINT " + END + BS + b'!"')
|
|
check("End and Backspace", row(y) == '10 PRINT "BIG HELLO!"')
|
|
b.type(CR + b"LIST" + CR)
|
|
lst = lines()
|
|
check("the edited line is stored", '10 PRINT "BIG HELLO!"\n20 GOTO 10' in lst)
|
|
|
|
b.type(UP)
|
|
y = cur()[0]
|
|
check("up brings back the last line, cursor at the end", row(y) == "LIST" and cur() == (y, 4))
|
|
b.type(CTRL_U)
|
|
check("Ctrl-U clears the line", row(y) == "" and cur() == (y, 0))
|
|
b.type(b"99" + RIGHT)
|
|
check("right after a missing line number does nothing", row(y) == "99" and cur() == (y, 2))
|
|
b.type(CTRL_U + b"20" + RIGHT * 6 + DEL * 2 + b"30" + CR)
|
|
b.type(b"LIST" + CR)
|
|
lst = lines().split("LIST")[-1]
|
|
check("Delete skips old characters while copying", "20 GOTO 30" in lst and "20 GOTO 10" not in lst)
|
|
b.type(b"20" + END + HOME + DEL * 2 + b"40" + CR + b"LIST" + CR)
|
|
check("a copied line can be stored under a new number", "20 GOTO 30\n40 GOTO 30" in lines())
|
|
|
|
# a line that wraps onto three screen rows
|
|
text = "REM " + "".join(chr(65 + i % 26) for i in range(170))
|
|
b.type(b"40 " + text.encode() + CR)
|
|
b.type(b"40" + END)
|
|
y, x = cur()
|
|
full = "40 " + text
|
|
y0 = y - (len(full) // 80)
|
|
shown = "".join(screen().display[y0 + i] for i in range(3))
|
|
check("a long line comes in over three rows", shown.rstrip() == full and (y, x) == (y0 + len(full) // 80, len(full) % 80))
|
|
b.type(HOME + RIGHT * 79)
|
|
check("right walks across a row end", cur() == (y0, 79))
|
|
b.type(RIGHT)
|
|
check("and onto the next row", cur() == (y0 + 1, 0))
|
|
b.type(b"*" + LEFT * 2 + DEL)
|
|
full = full[:79] + "*" + full[80:]
|
|
shown = "".join(screen().display[y0 + i] for i in range(3))
|
|
check("inserting and deleting across rows redraws them", shown.rstrip() == full and cur() == (y0, 79))
|
|
b.type(CR + b"LIST 40" + CR)
|
|
check("the long line is stored", full in "".join(screen().display))
|
|
b.type(UP + UP)
|
|
check("up again gives the same last line", lines().rstrip().endswith("LIST 40"))
|
|
b.type(CTRL_U)
|
|
|
|
b.type(b"NEW" + CR + b'INPUT A$: PRINT "GOT ";A$' + CR)
|
|
b.type(b"ABD" + LEFT + b"C" + CR)
|
|
check("INPUT uses the editor too", "GOT ABCD" in lines())
|
|
sys.exit(1 if errors else 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|