VT220 emulation with ANSI colour, underline and smooth scrolling

- Terminal engine rewritten for VT220: scroll regions, IL/DL/ICH/DCH/ECH,
  SU/SD, origin mode, insert mode, tab stops, DEC special graphics with
  G0/G1 and SO/SI, DECSC/DECRC, DECSTR, RIS, DECALN, DECLL LEDs
- Replies to DA, secondary DA, DSR and CPR over the UART
- 16-colour SGR (256-colour and RGB approximated), bold, underline,
  blink, reverse and conceal; cells are now 18 bits
- Smooth scrolling (SW8 or DECSCLM): the region slides per frame and
  rows are moved during vertical blanking; XON/XOFF holds the host back
- Keyboard: DECCKM, keypad application mode, F5-F12, VT220 Home/End,
  Alt as ESC prefix
- basys3 terminfo entry (vt220 + 16 colours, 30 lines)
- Flash script reboots the FPGA from flash after programming
- Testbench covers replies, modes, attributes, editing and smooth scroll

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
barantandClaude Opus 5.5 committed 2026-11-17 11:18:36 -08:00
1 parent a95062f42d
commit 6e54d75c56
10 files changed
+1173 -350

No files matched your search

+138 -46
View File
@@ -1,12 +1,13 @@
`timescale 1ns / 1ps
// Whole-design test: keystrokes on PS/2 -> UART TX bytes, UART RX text ->
// screen buffer (printed as text) and one VGA frame (written as frame.ppm).
// 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'h0004; // 115200 baud, local echo on
reg [15:0] sw = 16'h0200; // 115200 baud, fast smooth scroll when enabled
reg btnC = 0, btnU = 0;
reg rsrx = 1;
reg ps2c = 1, ps2d = 1;
@@ -21,6 +22,7 @@ module tb_terminal;
);
localparam BIT_NS = 1_000_000_000 / 115200;
integer errors = 0;
task uart_send(input [7:0] d);
integer i;
@@ -31,10 +33,10 @@ module tb_terminal;
end
endtask
task uart_str(input [8*160-1:0] s);
task uart_str(input [8*200-1:0] s);
integer i;
begin
for (i = 159; i >= 0; i = i - 1)
for (i = 199; i >= 0; i = i - 1)
if (s[i*8 +: 8] != 0) uart_send(s[i*8 +: 8]);
end
endtask
@@ -53,8 +55,16 @@ module tb_terminal;
end
endtask
// decode what the terminal transmits
reg [8*16-1:0] tx_log = 0;
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;
@@ -64,27 +74,71 @@ module tb_terminal;
for (i = 0; i < 8; i = i + 1) begin #(BIT_NS); d[i] = rstx; end
#(BIT_NS);
if (!rstx) $display("TX framing error");
$display("%t TX %02h", $time, d);
tx_log = {tx_log[8*15-1:0], d};
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, p;
integer rr, cc;
reg [8*80-1:0] line;
reg [8:0] cv;
begin
$display("+--------------------------------------------------------------------------------+");
for (rr = 0; rr < 30; rr = rr + 1) begin
p = (rr + dut.term.top) % 30;
for (cc = 0; cc < 80; cc = cc + 1) begin
cv = dut.screen.mem[p * 80 + cc];
line[(79 - cc) * 8 +: 8] = cv[8] ? (cv[7:0] == " " ? "#" : cv[7:0]) : cv[7:0];
end
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, top %0d", dut.term.row, dut.term.col, dut.term.top);
$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
@@ -98,20 +152,19 @@ module tb_terminal;
smp = 0;
end
task capture_frame;
task capture_frame(input [8*16-1:0] name);
integer fd, i;
begin
@(negedge vs);
@(posedge vs);
cap = 1;
@(negedge vs);
cap = 0;
fd = $fopen("frame.ppm", "w");
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 frame.ppm");
$display("wrote %0s", name);
end
endtask
@@ -126,36 +179,75 @@ module tb_terminal;
t_vs = $realtime;
end
integer k;
initial begin
#2_000_000; // reset, clear, banner
// keyboard: a, shift+b, Enter, up arrow, ctrl+c
ps2_send(8'h1C); ps2_send(8'hF0); ps2_send(8'h1C);
ps2_send(8'h12); ps2_send(8'h32); ps2_send(8'hF0); ps2_send(8'h32); ps2_send(8'hF0); ps2_send(8'h12);
ps2_send(8'h5A); ps2_send(8'hF0); ps2_send(8'h5A);
ps2_send(8'hE0); ps2_send(8'h75); ps2_send(8'hE0); ps2_send(8'hF0); ps2_send(8'h75);
ps2_send(8'h14); ps2_send(8'h21); ps2_send(8'hF0); ps2_send(8'h21); ps2_send(8'hF0); ps2_send(8'h14);
#200_000;
if (tx_n == 7 && tx_log[8*7-1:0] == {8'h61, 8'h42, 8'h0D, 8'h1B, "[A", 8'h03})
$display("PASS keyboard -> UART: 61 42 0D 1B 5B 41 03");
else
$display("FAIL keyboard -> UART: %0d bytes %h", tx_n, tx_log);
// ---- 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");
uart_str("\033[2J\033[HHello, world!\015\012");
uart_str("tab\tstop\tX\015\012abc\010Z \033[7m reverse \033[0m normal\015\012");
uart_str("erase-to-end: KEEP\033[4DXXXX\033[4D\033[K\015\012");
for (k = 0; k < 20; k = k + 1) begin
uart_str("line ");
uart_send("0" + k / 10);
uart_send("0" + k % 10);
uart_str("\015\012");
end
uart_str("\033[12;30H\033[7m+------------------+\033[13;30H| ANSI positioning |\033[14;30H+------------------+\033[0m");
uart_str("\033[30;1H\033[?25lwrap test: 0123456789012345678901234567890123456789012345678901234567890123456789");
#100_000;
// ---- 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("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;
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;
if (errors == 0) $display("ALL TESTS PASSED");
else $display("%0d TESTS FAILED", errors);
$finish;
end
endmodule