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>
This commit is contained in:
commit
5930b958b4
103 files changed
+55677
No files matched your search
+4096
File diff suppressed because it is too large.
Load diff
Executable
BIN
Binary file not shown.
+2381
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,40 @@
|
||||
/* BASIC: code and constants below A000h (ROM_TOP: the hardware ignores writes
|
||||
there), the program area at A000h, then data. 12000h-12FFFh is left free for
|
||||
machine code; the stack comes down from 13FF0h; 13FF8h-13FFFh hold the clock
|
||||
(include/basys3.h). */
|
||||
OUTPUT_ARCH("riscv")
|
||||
ENTRY(_start)
|
||||
MEMORY
|
||||
{
|
||||
ROM (rx) : ORIGIN = 0x00000, LENGTH = 0xA000
|
||||
RAM (rw) : ORIGIN = 0x0A000, LENGTH = 0x8000
|
||||
}
|
||||
SECTIONS
|
||||
{
|
||||
.text : {
|
||||
KEEP(*(.text.start))
|
||||
*(.text .text.*)
|
||||
*(.rodata .rodata.* .srodata .srodata.*)
|
||||
. = ALIGN(4);
|
||||
} > ROM
|
||||
.arena (NOLOAD) : { KEEP(*(.arena)) } > RAM
|
||||
.data : ALIGN(4) {
|
||||
__data_start = .;
|
||||
*(.data .data.*)
|
||||
__global_pointer$ = . + 0x800;
|
||||
*(.sdata .sdata.*)
|
||||
. = ALIGN(4);
|
||||
__data_end = .;
|
||||
} > RAM AT > ROM
|
||||
__data_load = LOADADDR(.data);
|
||||
.bss (NOLOAD) : ALIGN(4) {
|
||||
__bss_start = .;
|
||||
*(.sbss .sbss.* .bss .bss.* COMMON)
|
||||
. = ALIGN(4);
|
||||
__bss_end = .;
|
||||
} > RAM
|
||||
ASSERT(__bss_end <= 0x12000, "BASIC's data runs into the machine-code area")
|
||||
__stack_top = 0x13FF0;
|
||||
__rom_end = __data_load + SIZEOF(.data);
|
||||
/DISCARD/ : { *(.comment) *(.riscv.attributes) }
|
||||
}
|
||||
+16384
File diff suppressed because it is too large.
Load diff
Executable
+21
@@ -0,0 +1,21 @@
|
||||
#!/bin/bash
|
||||
# Compile BASIC for RV32IM and write basic.bin and the RAM images basic.mem
|
||||
# (0000-FFFF) and basic-hi.mem (10000-13FFF).
|
||||
set -e
|
||||
cd "$(dirname "$0")"
|
||||
source ../scripts/rv_env.sh
|
||||
mkdir -p out
|
||||
CFLAGS="$RV_ARCH -Os -ffunction-sections -fdata-sections -fno-common -Wall -I../include"
|
||||
${RV_PREFIX}gcc $CFLAGS -c -o out/crt0.o crt0.S
|
||||
${RV_PREFIX}gcc $CFLAGS -c -o out/basic.o basic.c
|
||||
${RV_PREFIX}gcc $CFLAGS -c -o out/string.o ../lib/string.c
|
||||
${RV_PREFIX}gcc $CFLAGS -c -o out/mathf.o ../lib/mathf.c
|
||||
${RV_PREFIX}gcc $CFLAGS -c -o out/setjmp.o ../lib/setjmp.S
|
||||
${RV_PREFIX}gcc $RV_ARCH -nostdlib -nostartfiles -T basic.ld -Wl,--gc-sections -Wl,-Map,out/basic.map \
|
||||
-Wl,--build-id=none -o out/basic.elf out/crt0.o out/basic.o out/string.o out/mathf.o out/setjmp.o -lgcc
|
||||
${RV_PREFIX}objcopy -O binary -j .text -j .data out/basic.elf basic.bin
|
||||
${RV_PREFIX}nm -n out/basic.elf > out/basic.sym
|
||||
python3 ../tools/bin2mem.py basic.bin out/basic-all.mem 20480
|
||||
head -n 16384 out/basic-all.mem > basic.mem
|
||||
tail -n 4096 out/basic-all.mem > basic-hi.mem
|
||||
${RV_PREFIX}size out/basic.elf
|
||||
@@ -0,0 +1,31 @@
|
||||
# Start-up for BASIC on the Basys 3 RISC-V computer: at the reset address 0.
|
||||
# Copy the initialised data from ROM, clear the rest, point traps at the
|
||||
# monitor and run main.
|
||||
#include "basys3.h"
|
||||
.section .text.start, "ax"
|
||||
.globl _start
|
||||
_start:
|
||||
.option push
|
||||
.option norelax
|
||||
la gp, __global_pointer$
|
||||
.option pop
|
||||
la sp, __stack_top
|
||||
li t0, MON_TRAP # illegal instructions etc. stop in the monitor
|
||||
csrw mtvec, t0
|
||||
la a0, __data_start # copy initialised data
|
||||
la a1, __data_load
|
||||
la a2, __data_end
|
||||
1: bgeu a0, a2, 2f
|
||||
lw t0, 0(a1)
|
||||
sw t0, 0(a0)
|
||||
addi a0, a0, 4
|
||||
addi a1, a1, 4
|
||||
j 1b
|
||||
2: la a0, __bss_start # clear uninitialised data
|
||||
la a2, __bss_end
|
||||
3: bgeu a0, a2, 4f
|
||||
sw zero, 0(a0)
|
||||
addi a0, a0, 4
|
||||
j 3b
|
||||
4: call main
|
||||
5: j 5b
|
||||
@@ -0,0 +1,69 @@
|
||||
10 REM GRAPHICS DEMO FOR THE BASYS 3 Z-80
|
||||
20 REM COLOUR BARS, CIRCLES, LINES, CURVES, SHAPES, ANIMATION, MANDELBROT
|
||||
30 S=TIM
|
||||
100 CLS
|
||||
110 INK 15: LOCATE 3,30: PRINT "BASYS 3 Z-80 GRAPHICS"
|
||||
120 INK 7: LOCATE 5,26: PRINT "320 X 240 PIXELS IN 16 COLOURS"
|
||||
130 FOR C=1 TO 15
|
||||
140 FILL C*20-10,100,C*20+7,180,C
|
||||
150 INK C: LOCATE 25,C*5-1: PRINT C;
|
||||
160 NEXT C
|
||||
170 GOSUB 9000
|
||||
200 CLS: INK 15: PRINT "CIRCLE"
|
||||
210 C=0
|
||||
220 FOR R=3 TO 117 STEP 3
|
||||
230 GOSUB 8000: CIRCLE 160,120,R,C
|
||||
240 NEXT R
|
||||
250 GOSUB 9000
|
||||
300 CLS: INK 15: PRINT "LINE"
|
||||
310 C=0
|
||||
320 FOR X=0 TO 319 STEP 8: GOSUB 8000: LINE 160,120,X,0,C: NEXT X
|
||||
330 FOR Y=0 TO 239 STEP 8: GOSUB 8000: LINE 160,120,319,Y,C: NEXT Y
|
||||
340 FOR X=319 TO 0 STEP -8: GOSUB 8000: LINE 160,120,X,239,C: NEXT X
|
||||
350 FOR Y=239 TO 0 STEP -8: GOSUB 8000: LINE 160,120,0,Y,C: NEXT Y
|
||||
360 GOSUB 9000
|
||||
400 CLS: INK 10: PRINT "Y = SIN(X)": INK 12: PRINT "Y = COS(X)"
|
||||
410 LINE 0,120,319,120,8: LINE 160,0,160,239,8
|
||||
420 FOR X=0 TO 319
|
||||
430 PLOT X,120-90*SIN((X-160)/25),10
|
||||
440 PLOT X,120-90*COS((X-160)/25),12
|
||||
450 NEXT X
|
||||
460 GOSUB 9000
|
||||
500 CLS: INK 15: PRINT "FILL AND RECT": RANDOMIZE
|
||||
510 FOR I=1 TO 120
|
||||
520 X=INT(RND*290): Y=16+INT(RND*200): C=1+INT(RND*15)
|
||||
530 IF RND<.5 THEN FILL X,Y,X+5+INT(RND*30),Y+5+INT(RND*20),C: GOTO 550
|
||||
540 RECT X,Y,X+5+INT(RND*30),Y+5+INT(RND*20),C
|
||||
550 NEXT I
|
||||
560 GOSUB 9000
|
||||
600 CLS: INK 15: PRINT "ANIMATION"
|
||||
610 RECT 0,16,319,239,7
|
||||
620 X=40: Y=60: U=3: V=2
|
||||
630 FOR I=1 TO 500
|
||||
640 CIRCLE X,Y,6,0
|
||||
650 X=X+U: Y=Y+V
|
||||
660 IF X<8 OR X>311 THEN U=-U
|
||||
670 IF Y<24 OR Y>231 THEN V=-V
|
||||
680 CIRCLE X,Y,6,11: PLOT X,Y,14
|
||||
690 T=TIM
|
||||
700 IF TIM-T<.01 THEN 700
|
||||
710 NEXT I
|
||||
720 GOSUB 9000
|
||||
800 CLS: INK 15: PRINT "MANDELBROT SET": M=TIM
|
||||
810 FOR J=0 TO 59: FOR I=0 TO 79
|
||||
820 A=I*.04-2.2: B=J*.04-1.2: X=0: Y=0
|
||||
830 FOR K=1 TO 15
|
||||
840 Z=X*X-Y*Y+A: Y=2*X*Y+B: X=Z
|
||||
850 IF X*X+Y*Y>4 THEN 870
|
||||
860 NEXT K
|
||||
870 IF K>15 THEN K=0
|
||||
880 FILL I*4,J*4,I*4+3,J*4+3,K
|
||||
890 NEXT I: NEXT J
|
||||
900 INK 7: LOCATE 30,1: PRINT "MANDELBROT TOOK";INT(TIM-M);"SECONDS.";
|
||||
910 PRINT " WHOLE DEMO";INT(TIM-S);"SECONDS."
|
||||
920 END
|
||||
8000 C=C+1: IF C>15 THEN C=1
|
||||
8010 RETURN
|
||||
9000 T=TIM
|
||||
9010 IF TIM-T<3 THEN 9010
|
||||
9020 RETURN
|
||||
@@ -0,0 +1,26 @@
|
||||
10 REM SOUND DEMO - NEEDS A PMODAMP2 ON THE TOP ROW OF PMOD JA
|
||||
20 REM PITCH = 125000 / FREQUENCY. PORTS: 144-149 PITCHES, 150 NOISE,
|
||||
30 REM 151 MIXER, 152-154 VOLUMES, 155-157 ENVELOPES (ATTACK*16+DECAY)
|
||||
40 PRINT "A SCALE"
|
||||
50 OUT 151, 1: OUT 155, 3
|
||||
60 FOR I = 1 TO 8
|
||||
70 READ P: GOSUB 500
|
||||
80 OUT 152, 15
|
||||
90 IF INP(152) > 0 THEN 90
|
||||
100 NEXT I
|
||||
110 PRINT "A LASER"
|
||||
120 OUT 151, 4: OUT 149, 0: OUT 157, 0: OUT 154, 13
|
||||
130 FOR P = 30 TO 250 STEP 6: OUT 148, P: NEXT P
|
||||
140 OUT 154, 0
|
||||
150 PRINT "AN EXPLOSION"
|
||||
160 OUT 151, 16: OUT 150, 24: OUT 156, 8: OUT 153, 15
|
||||
170 IF INP(153) > 0 THEN 170
|
||||
180 PRINT "A CHORD"
|
||||
190 OUT 151, 7
|
||||
200 OUT 144, 221: OUT 145, 1: OUT 146, 123: OUT 147, 1: OUT 148, 63: OUT 149, 1
|
||||
210 OUT 155, 69: OUT 156, 69: OUT 157, 69
|
||||
220 OUT 152, 12: OUT 153, 12: OUT 154, 12
|
||||
230 IF INP(152) > 0 THEN 230
|
||||
240 END
|
||||
500 OUT 144, P - INT(P / 256) * 256: OUT 145, INT(P / 256): RETURN
|
||||
900 DATA 477, 425, 379, 358, 319, 284, 253, 239
|
||||
@@ -0,0 +1,54 @@
|
||||
10 REM SPRITE DEMO FOR THE BASYS 3 Z-80
|
||||
20 REM W A S D MOVE THE ROBOT, Q QUITS. SIX BALLS BOUNCE AROUND;
|
||||
30 REM THE BORDER CHANGES COLOUR WHILE THE ROBOT TOUCHES ONE.
|
||||
40 DEF FNH(A)=A-48-7*INT(A/65)
|
||||
50 RANDOMIZE
|
||||
100 CLS: INK 15: PRINT "SPRITES W A S D MOVE Q QUITS"
|
||||
110 RECT 0,16,319,239,8
|
||||
120 FOR S=0 TO 1: GOSUB 8000: NEXT S
|
||||
200 N=6: DIM X(6),Y(6),U(6),V(6)
|
||||
210 FOR I=1 TO N
|
||||
220 X(I)=20+INT(RND(1)*270): Y(I)=30+INT(RND(1)*180)
|
||||
230 U(I)=2+INT(RND(1)*3): V(I)=1+INT(RND(1)*3)
|
||||
240 OUT 112,I: OUT 116,1: OUT 117,1
|
||||
250 NEXT I
|
||||
260 P=152: Q=120: OUT 112,0: OUT 116,0: OUT 117,1
|
||||
300 F=INP(120)
|
||||
310 IF INP(0) AND 1 THEN GOSUB 7000
|
||||
320 FOR I=1 TO N
|
||||
330 X(I)=X(I)+U(I): IF X(I)<2 OR X(I)>301 THEN U(I)=-U(I): X(I)=X(I)+2*U(I)
|
||||
340 Y(I)=Y(I)+V(I): IF Y(I)<18 OR Y(I)>221 THEN V(I)=-V(I): Y(I)=Y(I)+2*V(I)
|
||||
350 OUT 112,I: OUT 113,X(I) AND 255: OUT 114,INT(X(I)/256): OUT 115,Y(I)
|
||||
360 NEXT I
|
||||
370 OUT 112,0: OUT 113,P AND 255: OUT 114,INT(P/256): OUT 115,Q
|
||||
380 IF INP(128) AND 1 THEN H=H+1: RECT 0,16,319,239,1+(H AND 7)
|
||||
390 IF INP(120)=F THEN 390
|
||||
400 GOTO 300
|
||||
7000 K=INP(1): IF K>96 THEN K=K-32
|
||||
7010 IF K=87 AND Q>18 THEN Q=Q-4
|
||||
7020 IF K=83 AND Q<220 THEN Q=Q+4
|
||||
7030 IF K=65 AND P>2 THEN P=P-4: OUT 112,0: OUT 117,3
|
||||
7040 IF K=68 AND P<300 THEN P=P+4: OUT 112,0: OUT 117,1
|
||||
7050 IF K=81 THEN CLS: END
|
||||
7060 RETURN
|
||||
8000 REM LOAD SHAPE S FROM 16 ROWS OF 16 HEX DIGITS (0 = SEE-THROUGH)
|
||||
8010 OUT 118,S
|
||||
8020 FOR R=1 TO 16: READ A$
|
||||
8030 FOR C=1 TO 15 STEP 2
|
||||
8040 OUT 119,16*FNH(ASC(MID$(A$,C,1)))+FNH(ASC(MID$(A$,C+1,1)))
|
||||
8050 NEXT C: NEXT R
|
||||
8060 RETURN
|
||||
9000 REM SHAPE 0: THE ROBOT
|
||||
9010 DATA "00000BBBBBB00000","0000BBBBBBBB0000","000BB99BB99BB000"
|
||||
9020 DATA "000BB99BB99BB000","000BBBBBBBBBB000","000BBB0000BBB000"
|
||||
9030 DATA "0000BBBBBBBB0000","0000007777000000","0077777777777700"
|
||||
9040 DATA "0770777777770770","0770777777770770","0770777777770770"
|
||||
9050 DATA "0000777777770000","0000770000770000","0000770000770000"
|
||||
9060 DATA "000FFF0000FFF000"
|
||||
9100 REM SHAPE 1: A BALL
|
||||
9110 DATA "00000EEEEEE00000","000EEEEEEEEEE000","00EEFFEEEEEEEE00"
|
||||
9120 DATA "0EEFFFEEEEEEEEE0","0EEFFEEEEEEEEEE0","EEEEEEEEEEEEEEEE"
|
||||
9130 DATA "EEEEEEEEEEEEEEEE","EEEEEEEEEEEEEEEE","EEEEEEEEEEEEEEEE"
|
||||
9140 DATA "EEEEEEEEEEEEEEEE","0EEEEEEEEEEEEE60","0EEEEEEEEEEEE660"
|
||||
9150 DATA "00EEEEEEEEEE6600","000EEEEEEE666000","0000066666600000"
|
||||
9160 DATA "0000000000000000"
|
||||
@@ -0,0 +1,135 @@
|
||||
#!/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()
|
||||
@@ -0,0 +1,124 @@
|
||||
#!/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()
|
||||
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Try BASIC's string arrays in tools/rv32emu.py, with the screen kept by pyte.
|
||||
|
||||
python3 test-strarr.py
|
||||
"""
|
||||
import importlib.util
|
||||
import os
|
||||
import sys
|
||||
|
||||
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):
|
||||
nonlocal errors
|
||||
print(("PASS " if ok else "FAIL ") + what)
|
||||
errors += not ok
|
||||
|
||||
def run(prog):
|
||||
b.type(b"NEW" + CR)
|
||||
for line in prog.strip().splitlines():
|
||||
b.type(line.encode() + CR)
|
||||
b.type(b"RUN" + CR)
|
||||
return "\n".join(l.rstrip() for l in b.screen_text().display).split("RUN")[-1]
|
||||
|
||||
b.type(b"")
|
||||
out = run('''
|
||||
10 DIM N$(4), A(3)
|
||||
20 FOR I = 0 TO 4: READ N$(I): NEXT I
|
||||
30 A(2) = 7: A$ = "PLAIN"
|
||||
40 FOR I = 4 TO 0 STEP -1: PRINT N$(I);",";: NEXT I
|
||||
50 PRINT: PRINT A$; A(2); LEN(N$(1)); " "; LEFT$(N$(3), 2)
|
||||
60 DATA RED, GREEN, BLUE, "YEL, LOW", WHITE
|
||||
''')
|
||||
check("DIM, READ and PRINT a string array", "WHITE,YEL, LOW,BLUE,GREEN,RED," in out)
|
||||
check("plain A$ and A() kept apart", "PLAIN 7 5 YE" in out)
|
||||
|
||||
out = run('''
|
||||
10 DIM B$(2,3)
|
||||
20 B$(1,2) = "MID": B$(2,3) = B$(1,2) + "DLE"
|
||||
30 PRINT B$(2,3); "/"; B$(0,0); "/"; LEN(B$(0,1))
|
||||
40 C$(10) = "AUTO": PRINT C$(10)
|
||||
50 IF B$(1,2) = "MID" THEN PRINT "EQUAL"
|
||||
''')
|
||||
check("two subscripts and joining", "MIDDLE// 0" in out)
|
||||
check("undeclared string array gets 0..10", "AUTO" in out)
|
||||
check("comparing an element", "EQUAL" in out)
|
||||
|
||||
out = run('''
|
||||
10 INPUT "NAME"; N$(3)
|
||||
20 PRINT "HI "; N$(3)
|
||||
''')
|
||||
b.type(b"ZED" + CR)
|
||||
check("INPUT into an element", "HI ZED" in "\n".join(l.rstrip() for l in b.screen_text().display))
|
||||
|
||||
out = run('10 LINE INPUT L$(1): PRINT "<"; L$(1); ">"')
|
||||
b.type(b"A, B" + CR)
|
||||
check("LINE INPUT into an element", "<A, B>" in "\n".join(l.rstrip() for l in b.screen_text().display))
|
||||
|
||||
out = run('10 DIM Q$(2): Q$(3) = "X"')
|
||||
check("subscript out of range", "SUBSCRIPT" in out.upper())
|
||||
out = run('10 DIM Z$(300)')
|
||||
check("too many elements is an error", "SUBSCRIPT" in out.upper())
|
||||
out = run('10 DIM Z$(200), Y$(200)')
|
||||
check("running out of memory is an error", "MEMORY" in out.upper())
|
||||
out = run('10 DIM Z$(200): Z$(200) = "LAST": PRINT Z$(200)')
|
||||
check("a 201-element string array fits", "LAST" in out)
|
||||
sys.exit(1 if errors else 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user