Files
Basys3Terminal/sim/tb_terminal.v
T
brad ArantandClaude Opus 5.5 29a15bf7c7 Flash file system for BASIC, with bitstream write protection
BASIC gets SAVE, LOAD, RUN "NAME", DIR and DEL, storing program listings
in the configuration flash from 240000h (448 x 4 KB blocks, 64 files,
32 KB each). The directory is kept in two alternating blocks with a
sequence number and checksum, new data is written before the directory
switches to it, and every page is verified after programming.

rtl/flash_spi.v drives the flash through STARTUPE2 on ports 40h/41h. It
only passes read, status, WREN/WRDI, RDID, page program and 4 KB erase,
and abandons any program or erase below 240000h before the address
reaches the chip. Tested on the board: an erase at 0 and a write at
200000h were refused and left the flash unchanged.

With SW14 up, the Z-80's output is also copied to the PC's serial port,
and tools/get-bas.py uses that to LIST a program into a file or run
commands and print the replies.

Also fixes port 10h returning the previous millisecond latch (the first
read after reset was undefined). Simulation adds a model flash and a
save/load/run/protection test. Manual and README updated.

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

312 lines
11 KiB
Verilog

`timescale 1ns / 1ps
// Whole-design test: keystrokes on PS/2 -> UART TX bytes, host queries ->
// replies, UART RX text -> screen buffer (printed as text) and VGA frames
// (written as frame_*.ppm).
module tb_terminal;
reg clk = 0;
always #5 clk = ~clk;
reg [15:0] sw = 16'h0200; // 115200 baud, fast smooth scroll when enabled
reg btnC = 0, btnU = 0, btnD = 0;
reg rsrx = 1;
reg ps2c = 1, ps2d = 1;
wire rstx, hs, vs;
wire [3:0] r, g, b;
wire [15:0] led;
wire qcs;
wire [3:0] qdb;
basys3_terminal #(.FONT_FILE(`FONT_FILE), .MEM_FILE(`MEM_FILE)) dut (
.clk(clk), .btnC(btnC), .btnU(btnU), .btnD(btnD), .sw(sw), .led(led), .seg(), .dp(), .an(),
.vgaRed(r), .vgaGreen(g), .vgaBlue(b), .Hsync(hs), .Vsync(vs),
.RsRx(rsrx), .RsTx(rstx), .PS2Clk(ps2c), .PS2Data(ps2d),
.QspiCSn(qcs), .QspiDB(qdb)
);
spiflash_model flash (.cs_n(qcs), .sck(dut.fl_sck), .mosi(qdb[0]), .miso(qdb[1]));
localparam BIT_NS = 1_000_000_000 / 115200;
integer errors = 0;
task uart_send(input [7:0] d);
integer i;
begin
rsrx = 0; #(BIT_NS);
for (i = 0; i < 8; i = i + 1) begin rsrx = d[i]; #(BIT_NS); end
rsrx = 1; #(BIT_NS);
end
endtask
task uart_str(input [8*200-1:0] s);
integer i;
begin
for (i = 199; i >= 0; i = i - 1)
if (s[i*8 +: 8] != 0) uart_send(s[i*8 +: 8]);
end
endtask
task ps2_send(input [7:0] d);
integer i;
reg [10:0] f;
begin
f = {1'b1, ~^d, d, 1'b0};
for (i = 0; i < 11; i = i + 1) begin
ps2d = f[i]; #10000;
ps2c = 0; #20000;
ps2c = 1; #10000;
end
#100000;
end
endtask
task key(input [7:0] code); // press and release
begin ps2_send(code); ps2_send(8'hF0); ps2_send(code); end
endtask
task ext_key(input [7:0] code);
begin ps2_send(8'hE0); ps2_send(code); ps2_send(8'hE0); ps2_send(8'hF0); ps2_send(code); end
endtask
// everything the terminal transmits
reg [8*32-1:0] tx_log = 0;
integer tx_n = 0;
initial forever begin : txmon
integer i;
reg [7:0] d;
@(negedge rstx);
#(BIT_NS / 2);
for (i = 0; i < 8; i = i + 1) begin #(BIT_NS); d[i] = rstx; end
#(BIT_NS);
if (!rstx) $display("TX framing error");
tx_log = {tx_log[8*31-1:0], d};
tx_n = tx_n + 1;
end
task expect_tx(input [8*32-1:0] want, input integer n, input [8*40-1:0] what);
reg [8*32-1:0] mask;
begin
#1_200_000; // a 10-byte reply takes ~0.9 ms
mask = {256{1'b1}} >> (256 - 8 * n);
if (tx_n == n && (tx_log & mask) == (want & mask))
$display("PASS %0s", what);
else begin
$display("FAIL %0s: got %0d bytes %h", what, tx_n, tx_log & mask);
errors = errors + 1;
end
tx_n = 0;
tx_log = 0;
end
endtask
function [7:0] cellraw(input integer rr, input integer cc);
reg [17:0] v;
begin
v = dut.screen.mem[rr * 80 + cc];
cellraw = v[7:0];
end
endfunction
function [7:0] cellch(input integer rr, input integer cc);
reg [7:0] v;
begin
v = cellraw(rr, cc);
cellch = v < 8'h20 || v >= 8'h7F ? "~" : v;
end
endfunction
task dump_screen;
integer rr, cc;
reg [8*80-1:0] line;
begin
$display("+--------------------------------------------------------------------------------+");
for (rr = 0; rr < 30; rr = rr + 1) begin
for (cc = 0; cc < 80; cc = cc + 1)
line[(79 - cc) * 8 +: 8] = cellch(rr, cc);
$display("|%s| %0d", line, rr);
end
$display("+--------------------------------------------------------------------------------+");
$display("cursor row %0d col %0d, region %0d-%0d", dut.term.row, dut.term.col,
dut.term.stop, dut.term.sbot);
end
endtask
task expect_row(input integer rr, input [8*40-1:0] want);
integer cc, n;
reg [8*40-1:0] got;
begin
n = 0;
for (cc = 0; cc < 40; cc = cc + 1) if (want[cc*8 +: 8] != 0) n = cc + 1;
got = 0;
for (cc = 0; cc < n; cc = cc + 1) got[(n - 1 - cc) * 8 +: 8] = cellraw(rr, cc);
if (got == want) $display("PASS row %0d", rr);
else begin
$display("FAIL row %0d: want %h got %h", rr, want, got);
errors = errors + 1;
end
end
endtask
// capture one frame
reg [11:0] fb [0:640*480-1];
reg cap = 0, smp = 0;
reg [9:0] hh, vv;
always @(posedge clk) if (dut.video.tick) begin hh <= dut.video.h; vv <= dut.video.v; smp <= cap; end
always @(negedge clk) if (smp && hh < 640 && vv < 480) begin
fb[vv * 640 + hh] = {r, g, b};
smp = 0;
end
task capture_frame(input [8*16-1:0] name);
integer fd, i;
begin
@(posedge vs);
cap = 1;
@(negedge vs);
cap = 0;
fd = $fopen(name, "w");
$fwrite(fd, "P3\n640 480\n15\n");
for (i = 0; i < 640 * 480; i = i + 1)
$fwrite(fd, "%0d %0d %0d\n", fb[i][11:8], fb[i][7:4], fb[i][3:0]);
$fclose(fd);
$display("wrote %0s", name);
end
endtask
// check VGA timing on the outputs
realtime t_hs = 0, t_vs = 0, t_cmd;
always @(negedge hs) begin
if (t_hs != 0 && ($realtime - t_hs) != 32000) $display("HSYNC period %0t", $realtime - t_hs);
t_hs = $realtime;
end
always @(negedge vs) begin
if (t_vs != 0 && ($realtime - t_vs) != 16800000) $display("VSYNC period %0t", $realtime - t_vs);
t_vs = $realtime;
end
initial begin
#2_000_000; // reset, clear, banner
// ---- keyboard: a, shift+b, Enter, up, ctrl+c, F5
key(8'h1C);
ps2_send(8'h12); key(8'h32); ps2_send(8'hF0); ps2_send(8'h12);
key(8'h5A);
ext_key(8'h75);
ps2_send(8'h14); key(8'h21); ps2_send(8'hF0); ps2_send(8'h14);
key(8'h03);
expect_tx({8'h61, 8'h42, 8'h0D, 8'h1B, "[A", 8'h03, 8'h1B, "[15~"}, 12, "keyboard -> UART");
// ---- host queries and modes
uart_str("\033[c");
expect_tx({8'h1B, "[?62;22c"}, 9, "DA reply");
uart_str("\033[>c");
expect_tx({8'h1B, "[>1;10;0c"}, 10, "secondary DA reply");
uart_str("\033[5;10H\033[6n");
expect_tx({8'h1B, "[05;10R"}, 8, "cursor position report");
uart_str("\033[?1h");
ext_key(8'h75);
expect_tx({8'h1B, "OA"}, 3, "DECCKM up arrow");
uart_str("\033[?1l\033=");
key(8'h69);
expect_tx({8'h1B, "Oq"}, 3, "DECKPAM keypad 1");
uart_str("\033>\033[1;3q");
#50_000;
if (led[11:8] == 4'b0101) $display("PASS DECLL LEDs");
else begin $display("FAIL DECLL LEDs %b", led[11:8]); errors = errors + 1; end
// ---- screen
uart_str({"\033[2J\033[H\033[1mBold\033[0m \033[31mred \033[32mgreen \033[44;33myellow-on-blue\033[0m ",
"\033[4munderline\033[0m \033[7mreverse\033[0m \033[5mblink\033[0m\015\012"});
uart_str("\033)0\016lqqqqk\015\012x x\015\012mqqqqj\017\015\012");
uart_str("abcdefgh\033[4D\033[2@XY\033[3P\015\012");
uart_str("\033[38;5;196m256-red \033[38;2;0;0;255mrgb-blue \033[90mgrey \033[103;30mon-yellow\033[0m\015\012");
uart_str({"\033]3008;start=1f2e;user=brad;type=shell\033\\\033]0;title\007",
"line A\015\012line B\015\012line C\015\012line D\015\012"});
uart_str("\033[8;1H\033[Linserted\033[10;1H\033[M");
uart_str("\033[13;1Habove region\033[19;1Hbelow region\033[15;18r\033[15;1H");
uart_str("r1\015\012r2\015\012r3\015\012r4\015\012r5\015\012r6");
uart_str("\033[1;1H\033[4h>\033[4l\033[30;70Hcorner\033[1;1H");
#200_000;
expect_row(0, ">Bold red green yellow-on-blue");
expect_row(1, {8'hDA, 8'hC4, 8'hC4, 8'hC4, 8'hC4, 8'hBF});
expect_row(2, {8'hB3, " ", 8'hB3});
expect_row(4, "abcdXYh");
expect_row(6, "line A");
expect_row(7, "inserted");
expect_row(8, "line B");
expect_row(9, "line D");
expect_row(12, "above region");
expect_row(14, "r3");
expect_row(17, "r6");
expect_row(18, "below region");
dump_screen;
capture_frame("frame_1.ppm");
// ---- smooth scroll inside the region
uart_str("\033[18;1H\033[?4h\015\012r7");
capture_frame("frame_2.ppm"); // mid-scroll
#40_000_000;
expect_row(14, "r4");
expect_row(17, "r7");
expect_row(18, "below region");
uart_str("\033[?4l\033[r");
dump_screen;
// ---- Z-80 BASIC as the console, input pasted over the UART
uart_str("\033[2J\033[H");
#100_000;
sw[15] = 1; sw[14] = 1; // the Z-80 has been waiting to print its banner
#3_000_000;
uart_str("PRINT 2+2\015");
#2_000_000;
uart_str("10 FOR I=1 TO 3: PRINT I*I;: NEXT I\015RUN\015");
#5_000_000;
expect_row(1, "DARTMOUTH-STYLE BASIC");
expect_row(3, "READY");
expect_row(4, "PRINT 2+2");
expect_row(5, " 4");
expect_row(7, "READY");
expect_row(10, " 1 4 9");
expect_row(11, "READY");
// ---- graphics
uart_str({"CLS:INK 11:CIRCLE 160,120,50,12:FILL 10,10,40,30,4:",
"LINE 0,0,319,239,14:PRINT POINT(160,70)\015"});
t_cmd = $realtime;
while (cellraw(0, 2) != "2" && $realtime - t_cmd < 20_000_000) #10_000;
$display("graphics line ran in %0.2f ms", ($realtime - t_cmd) / 1e6);
expect_row(0, " 12");
if (dut.z80.gfx[70 * 320 + 160] == 12 && dut.z80.gfx[20 * 320 + 20] == 4 &&
dut.z80.gfx[120 * 320 + 160] == 14 && dut.z80.gfx[5 * 320 + 100] == 0)
$display("PASS graphics memory");
else begin
$display("FAIL graphics memory");
errors = errors + 1;
end
capture_frame("frame_3.ppm");
// ---- files in the flash, and the bitstream protection
uart_str({"CLS:NEW\01510 PRINT \"SAVED\";6*7\015SAVE \"TEST1\"\015NEW\015",
"LOAD \"TEST1\"\015RUN\015DIR\015"});
#20_000_000;
expect_row(13, "SAVED 42");
expect_row(17, "TEST1 21");
expect_row(18, "1 FILE, 1780K FREE");
uart_str("OUT 64,1:OUT 65,6:OUT 64,0:OUT 64,1:OUT 65,32:OUT 65,0:OUT 65,0:OUT 65,0:OUT 64,0\015");
#3_000_000;
if (flash.programs > 0 && flash.erases > 0 && flash.violations == 0)
$display("PASS flash: %0d programs, %0d erases, bitstream untouched",
flash.programs, flash.erases);
else begin
$display("FAIL flash: %0d programs, %0d erases, %0d violations",
flash.programs, flash.erases, flash.violations);
errors = errors + 1;
end
dump_screen;
if (errors == 0) $display("ALL TESTS PASSED");
else $display("%0d TESTS FAILED", errors);
$finish;
end
endmodule