- VGA 640x480@60 text display, 8x16 CP437 font ROM from Linux console fonts - USB HID keyboard (PS/2 via onboard PIC24), scan code set 2 to ASCII with xterm sequences for cursor/editing keys - UART RX/TX with switch-selectable baud, local echo option - Terminal engine: control codes, deferred wrap, hardware scroll, VT100/ANSI subset (cursor moves, erase, reverse video, cursor hide) - Vivado build/program/flash scripts and xsim whole-design testbench Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
243 lines
9.3 KiB
Verilog
243 lines
9.3 KiB
Verilog
`timescale 1ns / 1ps
|
|
|
|
// Terminal engine: consumes received bytes and updates the 80x30 character
|
|
// buffer. Supports CR LF BS TAB BEL FF, deferred auto-wrap, hardware scrolling
|
|
// (the buffer is a ring of rows; `top` is the physical row shown first) and a
|
|
// VT100/ANSI subset:
|
|
// ESC [ n A/B/C/D cursor up/down/right/left ESC [ r;c H / f position
|
|
// ESC [ n d / G row / column absolute ESC [ n J / K erase
|
|
// ESC [ n m 0 normal, 7 reverse, 27 not reverse
|
|
// ESC [ ? 25 h/l show/hide cursor
|
|
// ESC c reset ESC D index ESC E next line ESC M reverse index ESC 7/8 save/restore
|
|
// Cell format in the buffer: {reverse, CP437 code}.
|
|
|
|
module term_ctrl (
|
|
input clk,
|
|
input rst,
|
|
input [7:0] in_byte,
|
|
input in_empty,
|
|
output reg in_rd,
|
|
input lf_cr, // LF also returns the carriage
|
|
input clear_req,
|
|
output reg we,
|
|
output reg [11:0] waddr,
|
|
output reg [8:0] wdata,
|
|
output reg [4:0] top,
|
|
output reg [4:0] row,
|
|
output reg [6:0] col,
|
|
output reg cur_vis,
|
|
output reg bell
|
|
);
|
|
localparam LAST_ROW = 5'd29, LAST_COL = 7'd79;
|
|
|
|
localparam S_IDLE = 2'd0, S_DISP = 2'd1, S_FILL = 2'd2;
|
|
localparam P_NORM = 2'd0, P_ESC = 2'd1, P_CSI = 2'd2;
|
|
|
|
reg [1:0] st, ret_st, ps;
|
|
reg [7:0] c;
|
|
reg pend; // a character went in the last column
|
|
reg rev;
|
|
reg [7:0] p0, p1;
|
|
reg [1:0] pidx;
|
|
reg priv;
|
|
reg [4:0] sv_row;
|
|
reg [6:0] sv_col;
|
|
reg [4:0] fr, er; // fill range, in screen coordinates
|
|
reg [6:0] fc, ec;
|
|
|
|
function [11:0] phys(input [4:0] r, input [6:0] cc, input [4:0] t);
|
|
reg [5:0] s;
|
|
reg [4:0] pr;
|
|
begin
|
|
s = r + t;
|
|
pr = (s >= 6'd30) ? s - 6'd30 : s;
|
|
phys = {1'b0, pr, 6'b0} + {3'b0, pr, 4'b0} + {5'b0, cc}; // pr*80 + cc
|
|
end
|
|
endfunction
|
|
|
|
// CSI parameter accumulate, saturating at 255
|
|
function [7:0] acc(input [7:0] p, input [7:0] d);
|
|
reg [11:0] v;
|
|
begin
|
|
v = p * 10 + (d - "0");
|
|
acc = v > 255 ? 8'd255 : v[7:0];
|
|
end
|
|
endfunction
|
|
|
|
wire [7:0] n0 = p0 == 0 ? 8'd1 : p0;
|
|
wire [8:0] row_dn = row + n0;
|
|
wire [8:0] col_rt = col + n0;
|
|
wire [4:0] row_p0 = p0 == 0 ? 5'd0 : p0 > 30 ? LAST_ROW : p0 - 1'b1;
|
|
wire [6:0] col_p0 = p0 == 0 ? 7'd0 : p0 > 80 ? LAST_COL : p0 - 1'b1;
|
|
wire [6:0] col_p1 = p1 == 0 ? 7'd0 : p1 > 80 ? LAST_COL : p1 - 1'b1;
|
|
|
|
task fill(input [4:0] r0, input [6:0] c0, input [4:0] r1, input [6:0] c1, input [1:0] rs);
|
|
begin
|
|
fr <= r0; fc <= c0; er <= r1; ec <= c1;
|
|
ret_st <= rs;
|
|
st <= S_FILL;
|
|
end
|
|
endtask
|
|
|
|
// line feed; scrolls by advancing `top` and blanking the new bottom row
|
|
task line_feed(input [1:0] rs);
|
|
begin
|
|
if (row == LAST_ROW) begin
|
|
top <= top == LAST_ROW ? 5'd0 : top + 1'b1;
|
|
fill(LAST_ROW, 0, LAST_ROW, LAST_COL, rs);
|
|
end else begin
|
|
row <= row + 1'b1;
|
|
st <= rs;
|
|
end
|
|
end
|
|
endtask
|
|
|
|
reg r_sgr;
|
|
|
|
always @(posedge clk) begin
|
|
in_rd <= 1'b0;
|
|
we <= 1'b0;
|
|
bell <= 1'b0;
|
|
if (rst) begin
|
|
ps <= P_NORM;
|
|
{top, row, col, sv_row, sv_col} <= 0;
|
|
{pend, rev} <= 2'b00;
|
|
cur_vis <= 1'b1;
|
|
fill(0, 0, LAST_ROW, LAST_COL, S_IDLE);
|
|
end else case (st)
|
|
S_IDLE:
|
|
if (clear_req) begin
|
|
{row, col, pend} <= 0;
|
|
fill(0, 0, LAST_ROW, LAST_COL, S_IDLE);
|
|
end else if (!in_empty && !in_rd) begin
|
|
c <= in_byte;
|
|
in_rd <= 1'b1;
|
|
st <= S_DISP;
|
|
end
|
|
|
|
S_FILL: begin
|
|
we <= 1'b1;
|
|
waddr <= phys(fr, fc, top);
|
|
wdata <= 9'h020;
|
|
if (fr == er && fc == ec)
|
|
st <= ret_st;
|
|
else if (fc == LAST_COL) begin
|
|
fc <= 7'd0;
|
|
fr <= fr + 1'b1;
|
|
end else
|
|
fc <= fc + 1'b1;
|
|
end
|
|
|
|
S_DISP: begin
|
|
st <= S_IDLE;
|
|
case (ps)
|
|
P_NORM:
|
|
if (c >= 8'h20 && c != 8'h7F) begin
|
|
if (pend) begin
|
|
// wrap first, then come back to print this byte
|
|
pend <= 1'b0;
|
|
col <= 7'd0;
|
|
line_feed(S_DISP);
|
|
end else begin
|
|
we <= 1'b1;
|
|
waddr <= phys(row, col, top);
|
|
wdata <= {rev, c};
|
|
if (col == LAST_COL) pend <= 1'b1;
|
|
else col <= col + 1'b1;
|
|
end
|
|
end else case (c)
|
|
8'h07: bell <= 1'b1;
|
|
8'h08: begin pend <= 1'b0; if (col != 0) col <= col - 1'b1; end
|
|
8'h09: begin pend <= 1'b0; col <= col >= 7'd72 ? LAST_COL : (col | 7'd7) + 1'b1; end
|
|
8'h0A, 8'h0B: begin
|
|
pend <= 1'b0;
|
|
if (lf_cr) col <= 7'd0;
|
|
line_feed(S_IDLE);
|
|
end
|
|
8'h0C: begin
|
|
{row, col, pend} <= 0;
|
|
fill(0, 0, LAST_ROW, LAST_COL, S_IDLE);
|
|
end
|
|
8'h0D: begin pend <= 1'b0; col <= 7'd0; end
|
|
8'h1B: ps <= P_ESC;
|
|
default: ;
|
|
endcase
|
|
|
|
P_ESC: begin
|
|
ps <= P_NORM;
|
|
case (c)
|
|
"[": begin
|
|
ps <= P_CSI;
|
|
{p0, p1, pidx, priv} <= 0;
|
|
end
|
|
"c": begin
|
|
{row, col, pend, rev} <= 0;
|
|
cur_vis <= 1'b1;
|
|
fill(0, 0, LAST_ROW, LAST_COL, S_IDLE);
|
|
end
|
|
"D": begin pend <= 1'b0; line_feed(S_IDLE); end
|
|
"E": begin pend <= 1'b0; col <= 7'd0; line_feed(S_IDLE); end
|
|
"M": begin pend <= 1'b0; if (row != 0) row <= row - 1'b1; end
|
|
"7": begin sv_row <= row; sv_col <= col; end
|
|
"8": begin pend <= 1'b0; row <= sv_row; col <= sv_col; end
|
|
8'h1B: ps <= P_ESC;
|
|
default: ;
|
|
endcase
|
|
end
|
|
|
|
P_CSI:
|
|
if (c >= "0" && c <= "9") begin
|
|
if (pidx == 0) p0 <= acc(p0, c);
|
|
else if (pidx == 1) p1 <= acc(p1, c);
|
|
end else if (c == ";") begin
|
|
if (pidx != 2'd3) pidx <= pidx + 1'b1;
|
|
end else if (c == "?") begin
|
|
priv <= 1'b1;
|
|
end else if (c == 8'h18 || c == 8'h1A) begin
|
|
ps <= P_NORM; // CAN / SUB abort the sequence
|
|
end else if (c >= 8'h40 && c <= 8'h7E) begin
|
|
ps <= P_NORM;
|
|
if (priv) begin
|
|
if (p0 == 25 && c == "h") cur_vis <= 1'b1;
|
|
if (p0 == 25 && c == "l") cur_vis <= 1'b0;
|
|
end else case (c)
|
|
"A": begin pend <= 1'b0; row <= row > n0 ? row - n0 : 5'd0; end
|
|
"B": begin pend <= 1'b0; row <= row_dn > LAST_ROW ? LAST_ROW : row_dn[4:0]; end
|
|
"C": begin pend <= 1'b0; col <= col_rt > LAST_COL ? LAST_COL : col_rt[6:0]; end
|
|
"D": begin pend <= 1'b0; col <= col > n0 ? col - n0 : 7'd0; end
|
|
"H", "f": begin pend <= 1'b0; row <= row_p0; col <= col_p1; end
|
|
"d": begin pend <= 1'b0; row <= row_p0; end
|
|
"G", "`": begin pend <= 1'b0; col <= col_p0; end
|
|
"J": case (p0)
|
|
8'd0: fill(row, col, LAST_ROW, LAST_COL, S_IDLE);
|
|
8'd1: fill(0, 0, row, col, S_IDLE);
|
|
8'd2, 8'd3: fill(0, 0, LAST_ROW, LAST_COL, S_IDLE);
|
|
default: ;
|
|
endcase
|
|
"K": case (p0)
|
|
8'd0: fill(row, col, row, LAST_COL, S_IDLE);
|
|
8'd1: fill(row, 0, row, col, S_IDLE);
|
|
8'd2: fill(row, 0, row, LAST_COL, S_IDLE);
|
|
default: ;
|
|
endcase
|
|
"m": begin
|
|
r_sgr = rev;
|
|
if (p0 == 0 || p0 == 27) r_sgr = 1'b0;
|
|
else if (p0 == 7) r_sgr = 1'b1;
|
|
if (pidx != 0) begin
|
|
if (p1 == 0 || p1 == 27) r_sgr = 1'b0;
|
|
else if (p1 == 7) r_sgr = 1'b1;
|
|
end
|
|
rev <= r_sgr;
|
|
end
|
|
default: ;
|
|
endcase
|
|
end
|
|
default: ps <= P_NORM;
|
|
endcase
|
|
end
|
|
default: st <= S_IDLE;
|
|
endcase
|
|
end
|
|
endmodule
|