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

+81 -41
View File
@@ -58,8 +58,13 @@ module ps2_rx (
end
endmodule
// Turns scan codes into ASCII bytes. Cursor and editing keys send VT100/xterm
// escape sequences; F1-F4 send ESC O P..S.
// Turns scan codes into ASCII bytes and VT220 key sequences:
// arrows ESC [ A..D, or ESC O A..D in cursor-key application mode (DECCKM)
// Home End ESC [ 1 ~ ESC [ 4 ~ (VT220 Find / Select)
// Ins Del PgUp PgDn ESC [ 2 ~ 3 ~ 5 ~ 6 ~
// F1-F4 ESC O P..S F5-F12 ESC [ 15 17 18 19 20 21 23 24 ~
// keypad digits, or ESC O p..y etc. in keypad application mode (DECKPAM)
// Alt+key ESC prefix
module ps2_keymap (
input clk,
input rst,
@@ -67,17 +72,19 @@ module ps2_keymap (
input code_valid,
input bs_del, // Backspace sends DEL (7F) instead of BS (08)
input enter_crlf, // Enter sends CR LF instead of CR
input cursor_app, // DECCKM
input keypad_app, // DECKPAM
output reg [7:0] ascii,
output reg ascii_valid,
output reg caps
);
localparam [7:0] ESC = 8'h1B;
reg e0, f0;
reg [2:0] skip;
reg lsh, rsh, lct, rct;
reg [7:0] s0, s1, s2, s3;
reg [2:0] cnt;
reg e0, f0;
reg [2:0] skip;
reg lsh, rsh, lct, rct, alt;
reg [47:0] sq;
reg [2:0] cnt;
// {unshifted, shifted}
function [15:0] keymap(input [7:0] c);
@@ -100,7 +107,7 @@ module ps2_keymap (
8'h2A: keymap = "vV"; 8'h32: keymap = "bB"; 8'h31: keymap = "nN";
8'h3A: keymap = "mM"; 8'h41: keymap = ",<"; 8'h49: keymap = ".>";
8'h4A: keymap = "/?"; 8'h29: keymap = " "; 8'h76: keymap = {ESC, ESC};
// keypad, always as if Num Lock were on
// keypad, as if Num Lock were on
8'h70: keymap = "00"; 8'h69: keymap = "11"; 8'h72: keymap = "22";
8'h7A: keymap = "33"; 8'h6B: keymap = "44"; 8'h73: keymap = "55";
8'h74: keymap = "66"; 8'h6C: keymap = "77"; 8'h75: keymap = "88";
@@ -110,46 +117,72 @@ module ps2_keymap (
endcase
endfunction
// final byte of ESC O x for the keypad in application mode (0 = not a keypad key)
function [7:0] kp_app(input [7:0] c);
case (c)
8'h70: kp_app = "p"; 8'h69: kp_app = "q"; 8'h72: kp_app = "r";
8'h7A: kp_app = "s"; 8'h6B: kp_app = "t"; 8'h73: kp_app = "u";
8'h74: kp_app = "v"; 8'h6C: kp_app = "w"; 8'h75: kp_app = "x";
8'h7D: kp_app = "y"; 8'h71: kp_app = "n"; 8'h7B: kp_app = "m";
8'h79: kp_app = "l"; 8'h7C: kp_app = "j";
default: kp_app = 8'h00;
endcase
endfunction
wire shift = lsh | rsh;
wire ctrl = lct | rct;
wire [15:0] km = keymap(code);
wire letter = km[15:8] >= "a" && km[15:8] <= "z";
wire [7:0] ch = (letter ? shift ^ caps : shift) ? km[7:0] : km[15:8];
wire [7:0] ckey = cursor_app ? "O" : "[";
wire [7:0] kpa = kp_app(code);
// bytes a key press produces
reg [2:0] k_len;
reg [7:0] k0, k1, k2, k3;
// bytes a key press produces, first byte in k_seq[47:40]
reg [2:0] k_len;
reg [47:0] k_seq;
always @* begin
{k_len, k0, k1, k2, k3} = 35'd0;
k_len = 3'd0;
k_seq = 48'd0;
if (e0) case (code)
8'h75: {k_len, k0, k1, k2} = {3'd3, ESC, "[A"}; // up
8'h72: {k_len, k0, k1, k2} = {3'd3, ESC, "[B"}; // down
8'h74: {k_len, k0, k1, k2} = {3'd3, ESC, "[C"}; // right
8'h6B: {k_len, k0, k1, k2} = {3'd3, ESC, "[D"}; // left
8'h6C: {k_len, k0, k1, k2} = {3'd3, ESC, "[H"}; // home
8'h69: {k_len, k0, k1, k2} = {3'd3, ESC, "[F"}; // end
8'h70: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[2~"}; // insert
8'h71: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[3~"}; // delete
8'h7D: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[5~"}; // page up
8'h7A: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[6~"}; // page down
8'h4A: {k_len, k0} = {3'd1, "/"}; // keypad /
8'h5A: {k_len, k0, k1} = enter_crlf ? {3'd2, 8'h0D, 8'h0A} : {3'd1, 8'h0D, 8'h00};
8'h75: {k_len, k_seq} = {3'd3, ESC, ckey, "A", 24'd0}; // up
8'h72: {k_len, k_seq} = {3'd3, ESC, ckey, "B", 24'd0}; // down
8'h74: {k_len, k_seq} = {3'd3, ESC, ckey, "C", 24'd0}; // right
8'h6B: {k_len, k_seq} = {3'd3, ESC, ckey, "D", 24'd0}; // left
8'h6C: {k_len, k_seq} = {3'd4, ESC, "[1~", 16'd0}; // home (Find)
8'h70: {k_len, k_seq} = {3'd4, ESC, "[2~", 16'd0}; // insert
8'h71: {k_len, k_seq} = {3'd4, ESC, "[3~", 16'd0}; // delete (Remove)
8'h69: {k_len, k_seq} = {3'd4, ESC, "[4~", 16'd0}; // end (Select)
8'h7D: {k_len, k_seq} = {3'd4, ESC, "[5~", 16'd0}; // page up
8'h7A: {k_len, k_seq} = {3'd4, ESC, "[6~", 16'd0}; // page down
8'h4A: {k_len, k_seq} = keypad_app ? {3'd3, ESC, "Oo", 24'd0} : {3'd1, "/", 40'd0};
8'h5A: {k_len, k_seq} = keypad_app ? {3'd3, ESC, "OM", 24'd0} :
enter_crlf ? {3'd2, 8'h0D, 8'h0A, 32'd0} : {3'd1, 8'h0D, 40'd0};
default: ;
endcase
else case (code)
8'h66: {k_len, k0} = {3'd1, bs_del ? 8'h7F : 8'h08};
8'h5A: {k_len, k0, k1} = enter_crlf ? {3'd2, 8'h0D, 8'h0A} : {3'd1, 8'h0D, 8'h00};
8'h05: {k_len, k0, k1, k2} = {3'd3, ESC, "OP"}; // F1
8'h06: {k_len, k0, k1, k2} = {3'd3, ESC, "OQ"}; // F2
8'h04: {k_len, k0, k1, k2} = {3'd3, ESC, "OR"}; // F3
8'h0C: {k_len, k0, k1, k2} = {3'd3, ESC, "OS"}; // F4
8'h66: {k_len, k_seq} = {3'd1, bs_del ? 8'h7F : 8'h08, 40'd0};
8'h5A: {k_len, k_seq} = enter_crlf ? {3'd2, 8'h0D, 8'h0A, 32'd0} : {3'd1, 8'h0D, 40'd0};
8'h05: {k_len, k_seq} = {3'd3, ESC, "OP", 24'd0}; // F1
8'h06: {k_len, k_seq} = {3'd3, ESC, "OQ", 24'd0}; // F2
8'h04: {k_len, k_seq} = {3'd3, ESC, "OR", 24'd0}; // F3
8'h0C: {k_len, k_seq} = {3'd3, ESC, "OS", 24'd0}; // F4
8'h03: {k_len, k_seq} = {3'd5, ESC, "[15~", 8'd0}; // F5
8'h0B: {k_len, k_seq} = {3'd5, ESC, "[17~", 8'd0}; // F6
8'h83: {k_len, k_seq} = {3'd5, ESC, "[18~", 8'd0}; // F7
8'h0A: {k_len, k_seq} = {3'd5, ESC, "[19~", 8'd0}; // F8
8'h01: {k_len, k_seq} = {3'd5, ESC, "[20~", 8'd0}; // F9
8'h09: {k_len, k_seq} = {3'd5, ESC, "[21~", 8'd0}; // F10
8'h78: {k_len, k_seq} = {3'd5, ESC, "[23~", 8'd0}; // F11
8'h07: {k_len, k_seq} = {3'd5, ESC, "[24~", 8'd0}; // F12
default:
if (ch != 0) begin
if (keypad_app && kpa != 0)
{k_len, k_seq} = {3'd3, ESC, "O", kpa, 24'd0};
else if (ch != 0) begin
k_len = 3'd1;
if (!ctrl) k0 = ch;
else if (ch == " ") k0 = 8'h00;
else if (ch >= 8'h40) k0 = ch & 8'h1F; // ^A..^Z, ^[ ^\ ^] ^^ ^_
else k0 = ch;
if (!ctrl) k_seq[47:40] = ch;
else if (ch == " ") k_seq[47:40] = 8'h00;
else if (ch >= 8'h40) k_seq[47:40] = ch & 8'h1F; // ^A..^Z, ^[ ^\ ^] ^^ ^_
else k_seq[47:40] = ch;
end
endcase
end
@@ -157,14 +190,14 @@ module ps2_keymap (
always @(posedge clk) begin
ascii_valid <= 1'b0;
if (rst) begin
{e0, f0, lsh, rsh, lct, rct, caps} <= 7'd0;
{e0, f0, lsh, rsh, lct, rct, alt, caps} <= 8'd0;
skip <= 3'd0;
cnt <= 3'd0;
end else begin
if (cnt != 0) begin
ascii <= s0;
ascii <= sq[47:40];
ascii_valid <= 1'b1;
{s0, s1, s2} <= {s1, s2, s3};
sq <= sq << 8;
cnt <= cnt - 1'b1;
end
if (code_valid) begin
@@ -183,11 +216,18 @@ module ps2_keymap (
else if (code == 8'h59 && !e0) rsh <= !f0;
else if (code == 8'h14) begin
if (e0) rct <= !f0; else lct <= !f0;
end else if (code == 8'h58 && !e0) begin
end else if (code == 8'h11)
alt <= !f0;
else if (code == 8'h58 && !e0) begin
if (!f0) caps <= !caps;
end else if (!f0 && k_len != 0) begin
{s0, s1, s2, s3} <= {k0, k1, k2, k3};
cnt <= k_len;
if (alt && k_len != 3'd6) begin
sq <= {ESC, k_seq[47:8]};
cnt <= k_len + 1'b1;
end else begin
sq <= k_seq;
cnt <= k_len;
end
end
end
end