Files
Basys3Terminal/sim/tb_terminal.v
T
brad ArantandClaude Opus 5.5 fe1daf180d Monitor: Z-80 assembler (ASM "file", A addr)
Two-pass table-driven assembler in the monitor: assembles BASIC-saved
source files from the file system, or one line at a time with A addr.
Labels, EQU/ORG/DB/DW/DS/END, expressions. Table generated from the
disassembler (tools/gen-asmtab.py); round-trip and sdasz80 tests, plus a
Z-80 build run in z80emu. Monitor RAM grows to 6000-7FFF for the symbol
table. Works around an SDCC 4.5 inline strcpy bug with s_copy().

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

489 lines
20 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), .MON_FILE(`MON_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]));
real BIT_NS = 1_000_000_000.0 / 115200; // changed for the 3 Mbaud test
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[(dut.alt_scr ? 8448 : 0) + rr * 132 + cc]; // the page on show
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
// wait (up to 100 ms) for a row to read want, then check it
task wait_row(input integer rr, input [8*40-1:0] want);
integer cc, n;
reg [8*40-1:0] got;
realtime t0;
begin
t0 = $realtime;
n = 0;
for (cc = 0; cc < 40; cc = cc + 1) if (want[cc*8 +: 8] != 0) n = cc + 1;
got = 0;
while (got != want && $realtime - t0 < 100_000_000) begin
#100_000;
for (cc = 0; cc < n; cc = cc + 1) got[(n - 1 - cc) * 8 +: 8] = cellraw(rr, cc);
end
expect_row(rr, want);
end
endtask
// wait (up to 300 ms) for text at row rr from column c0
task wait_at(input integer rr, input integer c0, input [8*40-1:0] want);
integer cc, n;
reg [8*40-1:0] got;
realtime t0;
begin
t0 = $realtime;
n = 0;
for (cc = 0; cc < 40; cc = cc + 1) if (want[cc*8 +: 8] != 0) n = cc + 1;
got = 0;
while (got != want && $realtime - t0 < 300_000_000) begin
#100_000;
for (cc = 0; cc < n; cc = cc + 1) got[(n - 1 - cc) * 8 +: 8] = cellraw(rr, c0 + cc);
end
if (got == want) $display("PASS row %0d col %0d: %0s", rr, c0, want);
else begin
$display("FAIL row %0d col %0d: want \"%0s\" got \"%0s\"", rr, c0, want, got);
errors = errors + 1;
end
end
endtask
reg [31:0] pc_a, pc_b, jp_to;
reg [23:0] jp_txt;
function [31:0] screen_pc(input integer dummy);
screen_pc = {cellraw(2, 4), cellraw(2, 5), cellraw(2, 6), cellraw(2, 7)};
endfunction
// capture one frame (1280x1024; the colour on the pins belongs to the
// position the display's counters had four clocks earlier)
reg [11:0] fb [0:1280*1024-1];
reg cap = 0;
reg [21:0] pd1, pd2, pd3, pd4;
always @(posedge dut.pix_clk) begin
pd1 <= {dut.video.h, dut.video.v};
pd2 <= pd1; pd3 <= pd2; pd4 <= pd3;
end
always @(negedge dut.pix_clk)
if (cap && pd4[21:11] < 1280 && pd4[10:0] < 1024) fb[pd4[10:0] * 1280 + pd4[21:11]] = {r, g, b};
task capture_frame(input [8*16-1:0] name);
integer fd, i;
begin
@(posedge vs);
cap = 1;
@(negedge vs);
@(posedge vs);
cap = 0;
fd = $fopen(name, "w");
$fwrite(fd, "P3\n1280 1024\n15\n");
for (i = 0; i < 1280 * 1024; 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: 1688 x 1066 pixel clocks (10 ns here)
realtime t_hs = 0, t_vs = 0, t_cmd;
integer hs_bad = 0, vs_bad = 0;
always @(posedge hs) begin
if (t_hs != 0 && ($realtime - t_hs) != 16880) hs_bad = hs_bad + 1;
t_hs = $realtime;
end
always @(posedge vs) begin
if (t_vs != 0 && ($realtime - t_vs) != 16880 * 1066) begin
vs_bad = vs_bad + 1;
$display("VSYNC period %0t", $realtime - t_vs);
end
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
#100_000_000;
expect_row(14, "r4");
expect_row(17, "r7");
expect_row(18, "below region");
uart_str("\033[?4l\033[r");
dump_screen;
// ---- 132 columns (DECCOLM): 132x64, cleared, then back to 80x32
uart_str("\033[?3h");
#1_000_000;
uart_str("132 columns\033[64;124Hcorner!\033[10;120H\033[6n");
expect_tx({8'h1B, "[10;120R"}, 9, "cursor report past column 99");
#200_000;
expect_row(0, "132 columns");
if ({cellraw(63, 123), cellraw(63, 129)} == {"c", "!"} && dut.wide)
$display("PASS 132x64: last row and column");
else begin $display("FAIL 132x64 corner: %c%c", cellraw(63, 123), cellraw(63, 129)); errors = errors + 1; end
capture_frame("frame_4.ppm");
uart_str("\033[?3l");
#1_000_000;
uart_str("\033[32;75Hend");
#200_000;
if (!dut.wide && cellraw(31, 74) == "e" && cellraw(0, 0) == " ")
$display("PASS back to 80x32, cleared");
else begin $display("FAIL back to 80x32"); errors = errors + 1; end
if (hs_bad == 0 && vs_bad == 0) $display("PASS 1280x1024 sync timing");
else begin $display("FAIL sync timing: %0d hsync, %0d vsync periods off", hs_bad, vs_bad); errors = errors + 1; end
// ---- 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:DATE \"2026-11-28 09:30\":NEW\01510 PRINT \"SAVED\";6*7\015SAVE \"TEST1\"\015NEW\015",
"LOAD \"TEST1\"\015RUN\015DIR\015"});
#20_000_000;
expect_row(13, "SAVED 42");
wait_row(17, "TEST1 21 2026-11-28 09:30");
wait_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
// ---- a data file: write it, read it back
uart_str({"CLS:OPEN \"D\" FOR OUTPUT AS 1:PRINT #1,\"XY\";\",\";7:CLOSE\015",
"OPEN \"D\" FOR INPUT AS 1:INPUT #1,A$,B:PRINT A$;B*6;EOF(1):CLOSE\015"});
wait_row(3, "XY 42 -1");
// ---- the machine-code monitor
uart_str("MONITOR\015");
wait_at(0, 50, "STOPPED BY MONITOR COMMAND");
wait_at(2, 0, " PC ");
wait_at(29, 0, "> ");
pc_a = screen_pc(0);
wait_at(5, 0, ">");
jp_txt = {cellraw(5, 21), cellraw(5, 22), cellraw(5, 23)};
jp_to = {cellraw(5, 24), cellraw(5, 25), cellraw(5, 26), cellraw(5, 27)};
dump_screen;
uart_str("\015"); // Enter: one step
wait_at(0, 50, "STOPPED BY STEP");
wait_at(29, 0, "> ");
pc_b = screen_pc(0);
// MONITOR stops on a JP (to the end of BASIC's statement routine): one
// step must land exactly on its target
if (pc_b == jp_to) $display("PASS step: PC %0s -> %0s (%0s)", pc_a, pc_b, jp_txt);
else begin $display("FAIL step: PC %0s -> %0s, wanted %0s (%0s)", pc_a, pc_b, jp_to, jp_txt); errors = errors + 1; end
// a breakpoint where the step stopped (the end of BASIC's statement
// routine), hit again by OUT; leaving from it steps off it first
uart_str("B "); uart_send(pc_b[31:24]); uart_send(pc_b[23:16]);
uart_send(pc_b[15:8]); uart_send(pc_b[7:0]); uart_send(8'h0D);
wait_at(29, 40, "BREAKPOINT SET");
uart_str("X\015");
wait_row(3, "XY 42 -1"); // BASIC's screen is back as it was
uart_str("OUT 32,7\015");
wait_at(0, 50, "STOPPED BY BREAKPOINT");
wait_at(29, 0, "> ");
if (screen_pc(0) == pc_b) $display("PASS breakpoint at %0s", pc_b);
else begin $display("FAIL breakpoint: PC %0s, wanted %0s", screen_pc(0), pc_b); errors = errors + 1; end
uart_str("B\015");
wait_at(29, 40, "BREAKPOINT CLEARED");
uart_str("X\015");
#5_000_000;
// the Pause key
ps2_send(8'hE1); ps2_send(8'h14); ps2_send(8'h77);
ps2_send(8'hE1); ps2_send(8'hF0); ps2_send(8'h14); ps2_send(8'hF0); ps2_send(8'h77);
wait_at(0, 50, "STOPPED BY PAUSE KEY");
wait_at(29, 0, "> ");
uart_str("X\015");
#2_000_000;
uart_str("CLS:PRINT 3*4\015");
wait_row(0, " 12");
// ---- 3 Mbaud (SW1:0 = 11): a program in, its output on screen and back
sw[1:0] = 2'b11;
BIT_NS = 1_000_000_000.0 / 3_000_000;
#200_000;
tx_n = 0; tx_log = 0;
uart_str({"CLS:NEW\01510 FOR I=1 TO 3:PRINT I*111;:NEXT I\01520 PRINT \"FAST\"\015",
"RUN\015"});
wait_row(5, " 111 222 333 FAST");
#2_000_000;
if (tx_log[8*7-1:0] == "READY\015\012") $display("PASS 3 Mbaud: %0d bytes back, ending READY", tx_n);
else begin $display("FAIL 3 Mbaud: %0d bytes back, last %h", tx_n, tx_log[8*7-1:0]); errors = errors + 1; end
// ---- memory files: the monitor's SAVE/LOAD/DIR, BASIC's BSAVE/BLOAD
uart_str("CLS:POKE 36864,72:POKE 36865,73:MONITOR\015");
wait_at(29, 0, "> ");
uart_str("SAVE \"MEM1\" 9000 9001\015");
wait_at(29, 40, "SAVED");
uart_str("E 9000 00 00\015");
wait_at(29, 0, "> ");
uart_str("LOAD \"MEM1\"\015");
wait_at(29, 40, "LOADED 9000-9001");
wait_at(22, 0, " 9000 48 49 ");
uart_str("DIR\015");
wait_at(29, 40, "M TO SHOW MEMORY AGAIN");
uart_str("X\015");
#3_000_000;
uart_str("CLS:POKE 36864,0:BLOAD \"MEM1\":BSAVE \"MEM2\",36864,2:BLOAD \"MEM2\",36866:PRINT PEEK(36866);PEEK(36867)\015");
wait_row(0, " 72 73");
// ---- the assembler: source typed into BASIC, saved, assembled by the monitor
uart_str({"NEW\01510 ORG 0F000H\01520 START: LD HL,MSG\01530 LOOP: LD A,(HL)\01540 OR A\015",
"50 RET Z\01560 LD C,A\01570 WAIT: IN A,(0)\01580 AND 2\01590 JR Z,WAIT\015"});
uart_str({"100 LD A,C\015110 OUT (1),A\015120 INC HL\015130 JR LOOP\015",
"140 MSG: DB \"ASM OK\",0 ; THE MESSAGE\015SAVE \"HELLO\"\015MONITOR\015"});
wait_at(29, 0, "> ");
uart_str("ASM \"HELLO\"\015");
wait_at(29, 40, "ASSEMBLED F000-F019");
uart_str("A F100\015LD A,42H\015\015");
wait_at(5, 2, "F100 3E 42");
uart_str("X\015");
#3_000_000;
uart_str("CLS:CALL 61440\015");
wait_row(0, "ASM OK");
// ---- Ctrl-Alt-Del restarts the Z-80
ps2_send(8'h14); ps2_send(8'h11); ps2_send(8'hE0); ps2_send(8'h71);
ps2_send(8'hE0); ps2_send(8'hF0); ps2_send(8'h71); ps2_send(8'hF0); ps2_send(8'h11);
ps2_send(8'hF0); ps2_send(8'h14);
// the restarted BASIC's banner, on whichever row it lands
t_cmd = $realtime;
while ({cellraw(2, 0), cellraw(2, 1)} != "DA" && {cellraw(3, 0), cellraw(3, 1)} != "DA" &&
{cellraw(4, 0), cellraw(4, 1)} != "DA" && $realtime - t_cmd < 50_000_000) #100_000;
if ($realtime - t_cmd < 50_000_000) $display("PASS Ctrl-Alt-Del restarted BASIC");
else begin $display("FAIL Ctrl-Alt-Del: no banner"); errors = errors + 1; end
dump_screen;
if (errors == 0) $display("ALL TESTS PASSED");
else $display("%0d TESTS FAILED", errors);
$finish;
end
endmodule