Files
Basys3Terminal/basic/test-edit.py
T
brad ArantandClaude Opus 5.5 eb72d85834 BASIC line editor: cursor keys, Home/End, Delete, and Right to recall a line
Typing a line number and pressing Right brings that program line up to
edit. Left/Right move, Home/End (Ctrl-A/Ctrl-E) jump, typing inserts,
Backspace and Delete remove, Enter takes the whole line; long lines wrap.
To fit the ROM, the date code is smaller and SDCC gets more allocator
effort; build.sh now fails if the code runs past 8000h. basic/test-edit.py
checks the editor in the emulator.

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

125 lines
4.2 KiB
Python

#!/usr/bin/env python3
"""Try BASIC's line editor in tools/z80emu.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, HOME, END, DEL = b"\x1b[C", b"\x1b[D", 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 = bytearray(open(os.path.join(HERE, "basic.bin"), "rb").read())
mem += bytes(65536 - len(mem))
super().__init__(mem, start=0)
self.pc = 0
self.m[0] = mem[0]
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)
y = cur()[0]
b.type(b"10" + RIGHT)
check("right after 10 brings line 10 in", 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(b"99" + RIGHT)
y = cur()[0]
check("right after a missing line number does nothing", row(y) == "99" and cur() == (y, 2))
b.type(CTRL_U)
check("Ctrl-U clears the line", row(y) == "" and cur() == (y, 0))
b.type(b"20" + RIGHT + HOME + DEL * 2 + b"30" + CR)
b.type(b"LIST" + CR)
lst = lines()
check("a recalled line can be stored as a copy", "20 GOTO 10\n30 GOTO 10" in lst)
# 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" + RIGHT)
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(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()