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:
1 parent
a95062f42d
commit
6e54d75c56
10 files changed
+1173
-350
No files matched your search
+635
-149
@@ -1,15 +1,20 @@
|
||||
`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}.
|
||||
// VT220 terminal engine with ANSI colour. Consumes received bytes and keeps
|
||||
// the 80x30 new_cell buffer (row-major, address = row*80 + col) up to date.
|
||||
//
|
||||
// Cell format: {blink, underline, bg[3:0], fg[3:0], CP437 code}. Reverse,
|
||||
// bold and concealed text are resolved into the colours when a new_cell is written.
|
||||
//
|
||||
// Supported (beyond C0 controls):
|
||||
// ESC 7 8 D E H M Z c = > ESC ( ) <0|A|B> ESC # 8
|
||||
// CSI A B C D E F G H I J K L M P S T X Z @ ` a d e f g h l m n q r s u c
|
||||
// CSI ? J K h l n CSI > c CSI ! p
|
||||
// Modes: IRM(4) LNM(20) DECCKM(?1) DECSCLM(?4) DECOM(?6) DECAWM(?7) DECTCEM(?25)
|
||||
// SGR 0 1 4 5 7 8 21-28 30-49 90-107, 38/48;5;n and 38/48;2;r;g;b (approximated)
|
||||
// DECLL (CSI n q) drives four LEDs.
|
||||
// Scrolling copies rows in memory. With smooth scroll on, a line feed at the
|
||||
// bottom margin first slides the region up a few pixels per frame.
|
||||
|
||||
module term_ctrl (
|
||||
input clk,
|
||||
@@ -17,45 +22,102 @@ module term_ctrl (
|
||||
input [7:0] in_byte,
|
||||
input in_empty,
|
||||
output reg in_rd,
|
||||
input lf_cr, // LF also returns the carriage
|
||||
input lf_cr, // LF also returns the carriage
|
||||
input clear_req,
|
||||
input [3:0] def_fg, // default foreground colour index
|
||||
input smooth_sw, // force smooth scrolling on
|
||||
input smooth_fast,
|
||||
input frame_tick, // start of vertical blanking
|
||||
// character RAM port
|
||||
output reg [11:0] addr,
|
||||
output reg we,
|
||||
output reg [11:0] waddr,
|
||||
output reg [8:0] wdata,
|
||||
output reg [4:0] top,
|
||||
output reg [17:0] wdata,
|
||||
input [17:0] rdata,
|
||||
// to the display
|
||||
output reg [4:0] row,
|
||||
output reg [6:0] col,
|
||||
output reg cur_vis,
|
||||
output reg [4:0] stop, // scroll region, inclusive
|
||||
output reg [4:0] sbot,
|
||||
output reg sm_on,
|
||||
output reg sm_dir, // 0 = scrolling up, 1 = down
|
||||
output reg [4:0] sm_off,
|
||||
// replies to the host (DA, DSR)
|
||||
output reg [7:0] resp_byte,
|
||||
output reg resp_wr,
|
||||
input resp_full,
|
||||
// modes the keyboard needs
|
||||
output reg cursor_app,
|
||||
output reg keypad_app,
|
||||
output reg lnm,
|
||||
output reg [3:0] dec_leds,
|
||||
output reg bell
|
||||
);
|
||||
localparam LAST_ROW = 5'd29, LAST_COL = 7'd79;
|
||||
localparam [4:0] LAST_ROW = 5'd29;
|
||||
localparam [6:0] LAST_COL = 7'd79;
|
||||
localparam [11:0] CELLS = 12'd2400;
|
||||
|
||||
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;
|
||||
localparam [3:0] S_IDLE = 4'd0, S_DISP = 4'd1, S_FILL = 4'd2, S_CRD = 4'd3,
|
||||
S_CWAIT = 4'd4, S_CWR = 4'd5, S_PLOOP = 4'd6, S_RESP = 4'd7,
|
||||
S_PUT = 4'd8, S_TAB = 4'd9, S_SMOOTH = 4'd10, S_SMEND = 4'd11,
|
||||
S_OP = 4'd12;
|
||||
localparam [1:0] P_NORM = 2'd0, P_ESC = 2'd1, P_ESCI = 2'd2, P_CSI = 2'd3;
|
||||
localparam [1:0] K_SGR = 2'd0, K_SET = 2'd1, K_RST = 2'd2, K_LL = 2'd3;
|
||||
|
||||
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;
|
||||
reg [3:0] st, ret_st, sm_ret;
|
||||
reg [1:0] ps;
|
||||
reg [7:0] c;
|
||||
|
||||
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
|
||||
// modes and attributes
|
||||
reg pend, awm, irm, om, sclm;
|
||||
reg bold, ul, blink, rev, conceal;
|
||||
reg [3:0] fg, bg;
|
||||
reg [1:0] g0, g1; // 0 ASCII, 1 DEC special graphics, 2 UK
|
||||
reg gl; // 0 = G0 active, 1 = G1 (SO)
|
||||
reg [79:0] tabs;
|
||||
|
||||
// saved cursor (DECSC)
|
||||
reg [4:0] sv_row;
|
||||
reg [6:0] sv_col;
|
||||
reg sv_pend, sv_om, sv_gl, sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal;
|
||||
reg [3:0] sv_fg, sv_bg;
|
||||
reg [1:0] sv_g0, sv_g1;
|
||||
|
||||
// parser
|
||||
reg [7:0] par [0:15];
|
||||
reg [3:0] pidx;
|
||||
reg priv, gtm, ign;
|
||||
reg [7:0] cint, inter;
|
||||
|
||||
// memory copy / fill engine
|
||||
reg [11:0] c_src, c_dst, c_cnt, f_addr, f_cnt;
|
||||
reg c_dir;
|
||||
reg [17:0] f_cell;
|
||||
|
||||
// parameter loop (SGR, SM/RM, DECLL)
|
||||
reg [1:0] pk;
|
||||
reg [3:0] pi;
|
||||
reg [1:0] ext; // 38/48 extended colour state
|
||||
reg ext_bg;
|
||||
reg [1:0] ext_n;
|
||||
reg [7:0] ext_r, ext_g;
|
||||
|
||||
reg [95:0] rbuf;
|
||||
reg [3:0] rlen;
|
||||
reg [7:0] rep;
|
||||
reg tdir;
|
||||
|
||||
integer i;
|
||||
|
||||
// ------------------------------------------------------------ helpers
|
||||
function [11:0] rc(input [4:0] r, input [6:0] cc);
|
||||
rc = {1'b0, r, 6'b0} + {3'b0, r, 4'b0} + {5'b0, cc};
|
||||
endfunction
|
||||
|
||||
function [11:0] rows80(input [5:0] n);
|
||||
rows80 = {n, 6'b0} + {2'b0, n, 4'b0};
|
||||
endfunction
|
||||
|
||||
// CSI parameter accumulate, saturating at 255
|
||||
function [7:0] acc(input [7:0] p, input [7:0] d);
|
||||
reg [11:0] v;
|
||||
begin
|
||||
@@ -64,172 +126,596 @@ module term_ctrl (
|
||||
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;
|
||||
// DEC special graphics to CP437
|
||||
function [7:0] gfx(input [7:0] ch);
|
||||
case (ch)
|
||||
8'h5F: gfx = 8'h20; 8'h60: gfx = 8'h04; 8'h61: gfx = 8'hB1;
|
||||
8'h66: gfx = 8'hF8; 8'h67: gfx = 8'hF1; 8'h6A: gfx = 8'hD9;
|
||||
8'h6B: gfx = 8'hBF; 8'h6C: gfx = 8'hDA; 8'h6D: gfx = 8'hC0;
|
||||
8'h6E: gfx = 8'hC5; 8'h6F: gfx = 8'hC4; 8'h70: gfx = 8'hC4;
|
||||
8'h71: gfx = 8'hC4; 8'h72: gfx = 8'hC4; 8'h73: gfx = 8'hC4;
|
||||
8'h74: gfx = 8'hC3; 8'h75: gfx = 8'hB4; 8'h76: gfx = 8'hC1;
|
||||
8'h77: gfx = 8'hC2; 8'h78: gfx = 8'hB3; 8'h79: gfx = 8'hF3;
|
||||
8'h7A: gfx = 8'hF2; 8'h7B: gfx = 8'hE3; 8'h7C: gfx = 8'hD8;
|
||||
8'h7D: gfx = 8'h9C; 8'h7E: gfx = 8'hFA;
|
||||
default: gfx = 8'hFE; // control-code pictures
|
||||
endcase
|
||||
endfunction
|
||||
|
||||
task fill(input [4:0] r0, input [6:0] c0, input [4:0] r1, input [6:0] c1, input [1:0] rs);
|
||||
function [1:0] charset(input [7:0] ch);
|
||||
charset = ch == "0" ? 2'd1 : ch == "A" ? 2'd2 : 2'd0;
|
||||
endfunction
|
||||
|
||||
// colour index from levels: bit0 red, bit1 green, bit2 blue, bit3 bright
|
||||
function [3:0] approx(input hr, input hg, input hb, input bright, input dim);
|
||||
approx = (hr | hg | hb) ? {bright, hb, hg, hr} : (dim ? 4'd8 : 4'd0);
|
||||
endfunction
|
||||
|
||||
function [3:0] map256(input [7:0] n);
|
||||
reg [7:0] k;
|
||||
reg [2:0] r, g, b, mx;
|
||||
begin
|
||||
fr <= r0; fc <= c0; er <= r1; ec <= c1;
|
||||
if (n < 16)
|
||||
map256 = n[3:0];
|
||||
else if (n >= 232)
|
||||
map256 = n < 238 ? 4'd0 : n < 244 ? 4'd8 : n < 250 ? 4'd7 : 4'd15;
|
||||
else begin
|
||||
k = n - 8'd16;
|
||||
r = k / 36;
|
||||
g = (k / 6) % 6;
|
||||
b = k % 6;
|
||||
mx = r > g ? (r > b ? r : b) : (g > b ? g : b);
|
||||
map256 = approx(r >= 3, g >= 3, b >= 3, mx >= 4, mx >= 2);
|
||||
end
|
||||
end
|
||||
endfunction
|
||||
|
||||
function [3:0] rgb16(input [7:0] r, input [7:0] g, input [7:0] b);
|
||||
reg [7:0] mx;
|
||||
begin
|
||||
mx = r > g ? (r > b ? r : b) : (g > b ? g : b);
|
||||
rgb16 = approx(r[7], g[7], b[7], mx >= 192, mx >= 64);
|
||||
end
|
||||
endfunction
|
||||
|
||||
function [6:0] next_tab(input [79:0] t, input [6:0] cc);
|
||||
integer k;
|
||||
begin
|
||||
next_tab = LAST_COL;
|
||||
for (k = 79; k >= 0; k = k - 1)
|
||||
if (k > cc && t[k]) next_tab = k;
|
||||
end
|
||||
endfunction
|
||||
|
||||
function [6:0] prev_tab(input [79:0] t, input [6:0] cc);
|
||||
integer k;
|
||||
begin
|
||||
prev_tab = 7'd0;
|
||||
for (k = 0; k < 80; k = k + 1)
|
||||
if (k < cc && t[k]) prev_tab = k;
|
||||
end
|
||||
endfunction
|
||||
|
||||
// --------------------------------------------------- derived values
|
||||
wire [17:0] BLANK = {2'b00, 4'd0, def_fg, 8'h20};
|
||||
wire [7:0] p0 = par[0];
|
||||
wire [7:0] p1 = par[1];
|
||||
wire [7:0] n8 = p0 == 0 ? 8'd1 : p0;
|
||||
wire [6:0] room = 7'd80 - col; // cells from cursor to end of line
|
||||
wire [6:0] nc = n8 > room ? room : n8[6:0];
|
||||
|
||||
wire [3:0] fgb = bold ? (fg | 4'd8) : fg;
|
||||
wire [3:0] dfg = rev ? bg : fgb;
|
||||
wire [3:0] dbg = rev ? fgb : bg;
|
||||
wire [1:0] cs = gl ? g1 : g0;
|
||||
wire [7:0] glyph = (cs == 2'd1 && c >= 8'h5F && c <= 8'h7E) ? gfx(c) :
|
||||
(cs == 2'd2 && c == "#") ? 8'h9C : c;
|
||||
wire [17:0] new_cell = {blink, ul, dbg, conceal ? dbg : dfg, glyph};
|
||||
|
||||
wire smooth = sclm | smooth_sw;
|
||||
wire [4:0] sm_step = smooth_fast ? 5'd4 : 5'd2;
|
||||
|
||||
wire [4:0] up_lim = row >= stop ? stop : 5'd0;
|
||||
wire [4:0] dn_lim = row <= sbot ? sbot : LAST_ROW;
|
||||
wire [4:0] row_up = (row - up_lim > n8) ? row - n8 : up_lim;
|
||||
wire [4:0] row_dn = (dn_lim - row > n8) ? row + n8 : dn_lim;
|
||||
wire [6:0] col_rt = (LAST_COL - col > n8) ? col + n8 : LAST_COL;
|
||||
wire [6:0] col_lt = (col > n8) ? col - n8 : 7'd0;
|
||||
|
||||
wire [7:0] r_rel = p0 == 0 ? 8'd0 : p0 - 1'b1;
|
||||
wire [8:0] r_abs = (om ? stop : 5'd0) + r_rel;
|
||||
wire [4:0] r_max = om ? sbot : LAST_ROW;
|
||||
wire [4:0] row_p0 = r_abs > r_max ? r_max : r_abs[4:0];
|
||||
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;
|
||||
|
||||
wire [11:0] here = rc(row, col);
|
||||
wire [4:0] rep_row = (om ? row - stop : row) + 1'b1;
|
||||
wire [6:0] rep_col = col + 1'b1;
|
||||
reg [7:0] pv; // parameter being processed, registered
|
||||
reg pv_ok;
|
||||
|
||||
// ------------------------------------------------------------ tasks
|
||||
task op(input [11:0] src, input [11:0] dst, input [11:0] ccnt, input dir,
|
||||
input [11:0] fa, input [11:0] fcnt, input [17:0] fcell, input [3:0] rs);
|
||||
begin
|
||||
c_src <= src; c_dst <= dst; c_cnt <= ccnt; c_dir <= dir;
|
||||
f_addr <= fa; f_cnt <= fcnt; f_cell <= fcell;
|
||||
ret_st <= rs;
|
||||
st <= S_FILL;
|
||||
st <= S_OP; // decided next clock, off the critical path
|
||||
end
|
||||
endtask
|
||||
|
||||
// line feed; scrolls by advancing `top` and blanking the new bottom row
|
||||
task line_feed(input [1:0] rs);
|
||||
task fill(input [11:0] fa, input [11:0] fcnt);
|
||||
op(12'd0, 12'd0, 12'd0, 1'b0, fa, fcnt, BLANK, S_IDLE);
|
||||
endtask
|
||||
|
||||
// scroll rows t..b up (dir 0) or down (dir 1) by n lines
|
||||
task scroll(input dir, input [4:0] t, input [4:0] b, input [7:0] n, input [3:0] rs);
|
||||
reg [5:0] h, k;
|
||||
begin
|
||||
if (row == LAST_ROW) begin
|
||||
top <= top == LAST_ROW ? 5'd0 : top + 1'b1;
|
||||
fill(LAST_ROW, 0, LAST_ROW, LAST_COL, rs);
|
||||
h = b - t + 1'b1;
|
||||
k = n > h ? h : n[5:0];
|
||||
if (!dir)
|
||||
op(rc(t + k, 0), rc(t, 0), rows80(h - k), 1'b0,
|
||||
rc(b - k + 1'b1, 0), rows80(k), BLANK, rs);
|
||||
else
|
||||
op(rc(b - k + 1'b1, 0) - 1'b1, rc(b + 1'b1, 0) - 1'b1, rows80(h - k), 1'b1,
|
||||
rc(t, 0), rows80(k), BLANK, rs);
|
||||
end
|
||||
endtask
|
||||
|
||||
task line_feed(input [3:0] rs);
|
||||
begin
|
||||
if (row == sbot) begin
|
||||
if (smooth) begin
|
||||
sm_on <= 1'b1; sm_dir <= 1'b0; sm_off <= 5'd0; sm_ret <= rs;
|
||||
st <= S_SMOOTH;
|
||||
end else
|
||||
scroll(1'b0, stop, sbot, 8'd1, rs);
|
||||
end else begin
|
||||
row <= row + 1'b1;
|
||||
st <= rs;
|
||||
if (row != LAST_ROW) row <= row + 1'b1;
|
||||
st <= rs;
|
||||
end
|
||||
end
|
||||
endtask
|
||||
|
||||
reg r_sgr;
|
||||
task reverse_index;
|
||||
begin
|
||||
if (row == stop) begin
|
||||
if (smooth) begin
|
||||
sm_on <= 1'b1; sm_dir <= 1'b1; sm_off <= 5'd0; sm_ret <= S_IDLE;
|
||||
st <= S_SMOOTH;
|
||||
end else
|
||||
scroll(1'b1, stop, sbot, 8'd1, S_IDLE);
|
||||
end else if (row != 0)
|
||||
row <= row - 1'b1;
|
||||
end
|
||||
endtask
|
||||
|
||||
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;
|
||||
task reset_attrs;
|
||||
begin
|
||||
{bold, ul, blink, rev, conceal} <= 5'b0;
|
||||
fg <= def_fg;
|
||||
bg <= 4'd0;
|
||||
end
|
||||
endtask
|
||||
|
||||
task soft_reset;
|
||||
begin
|
||||
reset_attrs;
|
||||
{pend, irm, om, cursor_app, keypad_app, gl} <= 6'b0;
|
||||
{g0, g1} <= 4'b0;
|
||||
awm <= 1'b1;
|
||||
cur_vis <= 1'b1;
|
||||
fill(0, 0, LAST_ROW, LAST_COL, S_IDLE);
|
||||
stop <= 5'd0;
|
||||
sbot <= LAST_ROW;
|
||||
{sv_row, sv_col, sv_pend, sv_om, sv_gl, sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal} <= 0;
|
||||
{sv_g0, sv_g1, sv_bg} <= 0;
|
||||
sv_fg <= def_fg;
|
||||
end
|
||||
endtask
|
||||
|
||||
task full_reset;
|
||||
begin
|
||||
soft_reset;
|
||||
{row, col} <= 0;
|
||||
{sclm, lnm} <= 2'b0;
|
||||
dec_leds <= 4'd0;
|
||||
sm_on <= 1'b0;
|
||||
sm_off <= 5'd0;
|
||||
ps <= P_NORM;
|
||||
for (i = 0; i < 80; i = i + 1) tabs[i] <= i != 0 && i % 8 == 0;
|
||||
fill(12'd0, CELLS);
|
||||
end
|
||||
endtask
|
||||
|
||||
task respond(input [95:0] s, input [3:0] n);
|
||||
begin
|
||||
rbuf <= s;
|
||||
rlen <= n;
|
||||
st <= S_RESP;
|
||||
end
|
||||
endtask
|
||||
|
||||
function [7:0] tens(input [6:0] v);
|
||||
tens = "0" + v / 7'd10;
|
||||
endfunction
|
||||
|
||||
function [7:0] ones(input [6:0] v);
|
||||
ones = "0" + v % 7'd10;
|
||||
endfunction
|
||||
|
||||
task cursor_report;
|
||||
respond({8'h1B, "[", tens(rep_row), ones(rep_row), ";",
|
||||
tens(rep_col), ones(rep_col), "R", 32'd0}, 4'd8);
|
||||
endtask
|
||||
|
||||
// one SGR parameter
|
||||
task sgr(input [7:0] v);
|
||||
begin
|
||||
case (ext)
|
||||
2'd0:
|
||||
if (v == 0) reset_attrs;
|
||||
else if (v == 1) bold <= 1'b1;
|
||||
else if (v == 4 || v == 21) ul <= 1'b1;
|
||||
else if (v == 5 || v == 6) blink <= 1'b1;
|
||||
else if (v == 7) rev <= 1'b1;
|
||||
else if (v == 8) conceal <= 1'b1;
|
||||
else if (v == 22) bold <= 1'b0;
|
||||
else if (v == 24) ul <= 1'b0;
|
||||
else if (v == 25) blink <= 1'b0;
|
||||
else if (v == 27) rev <= 1'b0;
|
||||
else if (v == 28) conceal <= 1'b0;
|
||||
else if (v >= 30 && v <= 37) fg <= v - 8'd30;
|
||||
else if (v == 38 || v == 48) begin ext <= 2'd1; ext_bg <= v == 48; end
|
||||
else if (v == 39) fg <= def_fg;
|
||||
else if (v >= 40 && v <= 47) bg <= v - 8'd40;
|
||||
else if (v == 49) bg <= 4'd0;
|
||||
else if (v >= 90 && v <= 97) fg <= v - 8'd82;
|
||||
else if (v >= 100 && v <= 107) bg <= v - 8'd92;
|
||||
2'd1: // after 38/48: 5 = indexed, 2 = RGB
|
||||
if (v == 5) ext <= 2'd2;
|
||||
else if (v == 2) begin ext <= 2'd3; ext_n <= 2'd0; end
|
||||
else ext <= 2'd0;
|
||||
2'd2: begin
|
||||
if (ext_bg) bg <= map256(v); else fg <= map256(v);
|
||||
ext <= 2'd0;
|
||||
end
|
||||
2'd3: begin
|
||||
ext_n <= ext_n + 1'b1;
|
||||
if (ext_n == 0) ext_r <= v;
|
||||
else if (ext_n == 1) ext_g <= v;
|
||||
else begin
|
||||
if (ext_bg) bg <= rgb16(ext_r, ext_g, v); else fg <= rgb16(ext_r, ext_g, v);
|
||||
ext <= 2'd0;
|
||||
end
|
||||
end
|
||||
endcase
|
||||
end
|
||||
endtask
|
||||
|
||||
// one SM/RM parameter
|
||||
task set_mode(input [7:0] v, input on);
|
||||
begin
|
||||
if (priv) begin
|
||||
if (v == 1) cursor_app <= on;
|
||||
if (v == 4) sclm <= on;
|
||||
if (v == 6) begin om <= on; row <= on ? stop : 5'd0; col <= 7'd0; pend <= 1'b0; end
|
||||
if (v == 7) awm <= on;
|
||||
if (v == 25) cur_vis <= on;
|
||||
end else begin
|
||||
if (v == 4) irm <= on;
|
||||
if (v == 20) lnm <= on;
|
||||
end
|
||||
end
|
||||
endtask
|
||||
|
||||
task plist(input [1:0] kind);
|
||||
begin
|
||||
pk <= kind;
|
||||
pi <= 4'd0;
|
||||
ext <= 2'd0;
|
||||
pv_ok <= 1'b0;
|
||||
st <= S_PLOOP;
|
||||
end
|
||||
endtask
|
||||
|
||||
// ------------------------------------------------------------ main
|
||||
always @(posedge clk) begin
|
||||
in_rd <= 1'b0;
|
||||
we <= 1'b0;
|
||||
bell <= 1'b0;
|
||||
resp_wr <= 1'b0;
|
||||
if (rst) begin
|
||||
full_reset;
|
||||
cursor_app <= 1'b0;
|
||||
end else case (st)
|
||||
S_IDLE:
|
||||
if (clear_req) begin
|
||||
{row, col, pend} <= 0;
|
||||
fill(0, 0, LAST_ROW, LAST_COL, S_IDLE);
|
||||
fill(12'd0, CELLS);
|
||||
end else if (!in_empty && !in_rd) begin
|
||||
c <= in_byte;
|
||||
in_rd <= 1'b1;
|
||||
st <= S_DISP;
|
||||
end
|
||||
|
||||
S_FILL: begin
|
||||
// ---- copy c_cnt cells, then fill f_cnt cells, then go to ret_st
|
||||
S_OP:
|
||||
st <= c_cnt != 0 ? S_CRD : f_cnt != 0 ? S_FILL : ret_st;
|
||||
S_CRD: begin
|
||||
addr <= c_src;
|
||||
st <= S_CWAIT;
|
||||
end
|
||||
S_CWAIT:
|
||||
st <= S_CWR;
|
||||
S_CWR: begin
|
||||
addr <= c_dst;
|
||||
wdata <= rdata;
|
||||
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;
|
||||
c_src <= c_dir ? c_src - 1'b1 : c_src + 1'b1;
|
||||
c_dst <= c_dir ? c_dst - 1'b1 : c_dst + 1'b1;
|
||||
c_cnt <= c_cnt - 1'b1;
|
||||
st <= c_cnt != 1 ? S_CRD : f_cnt != 0 ? S_FILL : ret_st;
|
||||
end
|
||||
S_FILL: begin
|
||||
addr <= f_addr;
|
||||
wdata <= f_cell;
|
||||
we <= 1'b1;
|
||||
f_addr <= f_addr + 1'b1;
|
||||
f_cnt <= f_cnt - 1'b1;
|
||||
if (f_cnt == 1) st <= ret_st;
|
||||
end
|
||||
|
||||
S_SMOOTH:
|
||||
// the region slides during active video; the copy runs in blanking
|
||||
if (frame_tick) begin
|
||||
if (sm_off + sm_step >= 16)
|
||||
scroll(sm_dir, stop, sbot, 8'd1, S_SMEND);
|
||||
else
|
||||
sm_off <= sm_off + sm_step;
|
||||
end
|
||||
S_SMEND: begin
|
||||
sm_on <= 1'b0;
|
||||
sm_off <= 5'd0;
|
||||
st <= sm_ret;
|
||||
end
|
||||
|
||||
S_RESP:
|
||||
if (!resp_full) begin
|
||||
resp_byte <= rbuf[95:88];
|
||||
resp_wr <= 1'b1;
|
||||
rbuf <= rbuf << 8;
|
||||
rlen <= rlen - 1'b1;
|
||||
if (rlen == 1) st <= S_IDLE;
|
||||
end
|
||||
|
||||
S_TAB: begin
|
||||
col <= tdir ? prev_tab(tabs, col) : next_tab(tabs, col);
|
||||
rep <= rep - 1'b1;
|
||||
if (rep == 1) st <= S_IDLE;
|
||||
end
|
||||
|
||||
S_PLOOP: if (!pv_ok) begin
|
||||
// fetch the parameter one clock ahead, then act on it
|
||||
pv <= par[pi];
|
||||
pv_ok <= 1'b1;
|
||||
end else begin
|
||||
pv_ok <= 1'b0;
|
||||
case (pk)
|
||||
K_SGR: sgr(pv);
|
||||
K_SET: set_mode(pv, 1'b1);
|
||||
K_RST: set_mode(pv, 1'b0);
|
||||
K_LL:
|
||||
if (pv == 0) dec_leds <= 4'd0;
|
||||
else if (pv >= 1 && pv <= 4) dec_leds[pv - 1] <= 1'b1;
|
||||
else if (pv >= 21 && pv <= 24) dec_leds[pv - 21] <= 1'b0;
|
||||
endcase
|
||||
pi <= pi + 1'b1;
|
||||
if (pi == pidx) st <= S_IDLE;
|
||||
end
|
||||
|
||||
S_PUT: begin // write the character at the cursor
|
||||
we <= 1'b1;
|
||||
addr <= here;
|
||||
wdata <= new_cell;
|
||||
if (col == LAST_COL) pend <= awm;
|
||||
else col <= col + 1'b1;
|
||||
st <= S_IDLE;
|
||||
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)
|
||||
if (c == 8'h1B) begin
|
||||
ps <= P_ESC;
|
||||
end else if (c == 8'h18 || c == 8'h1A) begin
|
||||
ps <= P_NORM; // CAN / SUB cancel a sequence
|
||||
end else if (c < 8'h20) begin
|
||||
// C0 controls act immediately, even inside an escape sequence
|
||||
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
|
||||
8'h09: begin pend <= 1'b0; col <= next_tab(tabs, col); end
|
||||
8'h0A, 8'h0B, 8'h0C: begin
|
||||
pend <= 1'b0;
|
||||
if (lf_cr) col <= 7'd0;
|
||||
if (lf_cr || lnm) 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;
|
||||
8'h0E: gl <= 1'b1;
|
||||
8'h0F: gl <= 1'b0;
|
||||
default: ;
|
||||
endcase
|
||||
end else if (c == 8'h7F) begin
|
||||
// DEL is ignored
|
||||
end else case (ps)
|
||||
P_NORM:
|
||||
if (pend && awm) begin
|
||||
pend <= 1'b0;
|
||||
col <= 7'd0;
|
||||
line_feed(S_DISP); // wrap, then print this byte
|
||||
end else if (irm && col != LAST_COL)
|
||||
// insert mode: shift the rest of the line right first
|
||||
op(rc(row, LAST_COL - 1'b1), rc(row, LAST_COL), {5'd0, LAST_COL - col}, 1'b1,
|
||||
12'd0, 12'd0, BLANK, S_PUT);
|
||||
else
|
||||
st <= S_PUT;
|
||||
|
||||
P_ESC: begin
|
||||
ps <= P_NORM;
|
||||
case (c)
|
||||
if (c >= 8'h20 && c <= 8'h2F) begin
|
||||
inter <= c;
|
||||
ps <= P_ESCI;
|
||||
end else 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);
|
||||
for (i = 0; i < 16; i = i + 1) par[i] <= 8'd0;
|
||||
{pidx, priv, gtm, ign} <= 0;
|
||||
cint <= 8'd0;
|
||||
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;
|
||||
"M": begin pend <= 1'b0; reverse_index; end
|
||||
"H": tabs[col] <= 1'b1;
|
||||
"7": begin
|
||||
{sv_row, sv_col, sv_pend, sv_om, sv_gl} <= {row, col, pend, om, gl};
|
||||
{sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal} <= {bold, ul, blink, rev, conceal};
|
||||
{sv_fg, sv_bg, sv_g0, sv_g1} <= {fg, bg, g0, g1};
|
||||
end
|
||||
"8": begin
|
||||
{row, col, pend, om, gl} <= {sv_row, sv_col, sv_pend, sv_om, sv_gl};
|
||||
{bold, ul, blink, rev, conceal} <= {sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal};
|
||||
{fg, bg, g0, g1} <= {sv_fg, sv_bg, sv_g0, sv_g1};
|
||||
end
|
||||
"=": keypad_app <= 1'b1;
|
||||
">": keypad_app <= 1'b0;
|
||||
"Z": respond({8'h1B, "[?62;22c", 24'd0}, 4'd9);
|
||||
"c": begin full_reset; cursor_app <= 1'b0; end
|
||||
default: ;
|
||||
endcase
|
||||
end
|
||||
|
||||
P_ESCI: begin
|
||||
ps <= P_NORM;
|
||||
if (c >= 8'h20 && c <= 8'h2F)
|
||||
ps <= P_ESCI; // further intermediates
|
||||
else case (inter)
|
||||
"(": g0 <= charset(c);
|
||||
")": g1 <= charset(c);
|
||||
"#": if (c == "8") begin // DECALN: fill the screen with E
|
||||
{row, col, pend, stop} <= 0;
|
||||
sbot <= LAST_ROW;
|
||||
op(12'd0, 12'd0, 12'd0, 1'b0, 12'd0, CELLS, {2'b00, 4'd0, def_fg, "E"}, S_IDLE);
|
||||
end
|
||||
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
|
||||
if (c >= "0" && c <= "9")
|
||||
par[pidx] <= acc(par[pidx], c);
|
||||
else if (c == ";" || c == ":") begin
|
||||
if (pidx != 4'd15) pidx <= pidx + 1'b1;
|
||||
end else if (c == "?")
|
||||
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
|
||||
else if (c == ">")
|
||||
gtm <= 1'b1;
|
||||
else if (c == "<" || c == "=")
|
||||
ign <= 1'b1;
|
||||
else if (c >= 8'h20 && c <= 8'h2F)
|
||||
cint <= c;
|
||||
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;
|
||||
if (ign) begin
|
||||
end else if (cint == "!") begin
|
||||
if (c == "p") soft_reset;
|
||||
end else if (cint != 0) begin
|
||||
end else if (gtm) begin
|
||||
if (c == "c") respond({8'h1B, "[>1;10;0c", 16'd0}, 4'd10);
|
||||
end else if (priv) begin
|
||||
case (c)
|
||||
"h": plist(K_SET);
|
||||
"l": plist(K_RST);
|
||||
"J": begin
|
||||
pend <= 1'b0;
|
||||
if (p0 == 0) fill(here, CELLS - here);
|
||||
else if (p0 == 1) fill(12'd0, here + 1'b1);
|
||||
else if (p0 == 2) fill(12'd0, CELLS);
|
||||
end
|
||||
rev <= r_sgr;
|
||||
"K": begin
|
||||
pend <= 1'b0;
|
||||
if (p0 == 0) fill(here, room);
|
||||
else if (p0 == 1) fill(rc(row, 0), col + 1'b1);
|
||||
else if (p0 == 2) fill(rc(row, 0), 12'd80);
|
||||
end
|
||||
"n": if (p0 == 6) cursor_report;
|
||||
default: ;
|
||||
endcase
|
||||
end else case (c)
|
||||
"A": begin pend <= 1'b0; row <= row_up; end
|
||||
"B", "e": begin pend <= 1'b0; row <= row_dn; end
|
||||
"C", "a": begin pend <= 1'b0; col <= col_rt; end
|
||||
"D": begin pend <= 1'b0; col <= col_lt; end
|
||||
"E": begin pend <= 1'b0; row <= row_dn; col <= 7'd0; end
|
||||
"F": begin pend <= 1'b0; row <= row_up; col <= 7'd0; end
|
||||
"G", "`": begin pend <= 1'b0; col <= col_p0; end
|
||||
"d": begin pend <= 1'b0; row <= row_p0; end
|
||||
"H", "f": begin pend <= 1'b0; row <= row_p0; col <= col_p1; end
|
||||
"I": begin pend <= 1'b0; rep <= n8; tdir <= 1'b0; st <= S_TAB; end
|
||||
"Z": begin pend <= 1'b0; rep <= n8; tdir <= 1'b1; st <= S_TAB; end
|
||||
"J": begin
|
||||
pend <= 1'b0;
|
||||
if (p0 == 0) fill(here, CELLS - here);
|
||||
else if (p0 == 1) fill(12'd0, here + 1'b1);
|
||||
else if (p0 == 2 || p0 == 3) fill(12'd0, CELLS);
|
||||
end
|
||||
"K": begin
|
||||
pend <= 1'b0;
|
||||
if (p0 == 0) fill(here, room);
|
||||
else if (p0 == 1) fill(rc(row, 0), col + 1'b1);
|
||||
else if (p0 == 2) fill(rc(row, 0), 12'd80);
|
||||
end
|
||||
"L": if (row >= stop && row <= sbot) begin
|
||||
pend <= 1'b0; col <= 7'd0;
|
||||
scroll(1'b1, row, sbot, n8, S_IDLE);
|
||||
end
|
||||
"M": if (row >= stop && row <= sbot) begin
|
||||
pend <= 1'b0; col <= 7'd0;
|
||||
scroll(1'b0, row, sbot, n8, S_IDLE);
|
||||
end
|
||||
"@": begin
|
||||
pend <= 1'b0;
|
||||
op(rc(row, LAST_COL - nc), rc(row, LAST_COL), room - nc, 1'b1,
|
||||
here, nc, BLANK, S_IDLE);
|
||||
end
|
||||
"P": begin
|
||||
pend <= 1'b0;
|
||||
op(rc(row, col + nc), here, room - nc, 1'b0,
|
||||
rc(row, 7'd80 - nc), nc, BLANK, S_IDLE);
|
||||
end
|
||||
"X": begin pend <= 1'b0; fill(here, nc); end
|
||||
"S": scroll(1'b0, stop, sbot, n8, S_IDLE);
|
||||
"T": scroll(1'b1, stop, sbot, n8, S_IDLE);
|
||||
"g": if (p0 == 0) tabs[col] <= 1'b0; else if (p0 == 3) tabs <= 80'd0;
|
||||
"h": plist(K_SET);
|
||||
"l": plist(K_RST);
|
||||
"m": plist(K_SGR);
|
||||
"q": plist(K_LL);
|
||||
"r": begin
|
||||
if ((p0 == 0 ? 8'd1 : p0) < (p1 == 0 || p1 > 30 ? 8'd30 : p1)) begin
|
||||
stop <= p0 == 0 ? 5'd0 : p0 - 1'b1;
|
||||
sbot <= p1 == 0 || p1 > 30 ? LAST_ROW : p1 - 1'b1;
|
||||
row <= om ? (p0 == 0 ? 5'd0 : p0 - 1'b1) : 5'd0;
|
||||
col <= 7'd0;
|
||||
pend <= 1'b0;
|
||||
end
|
||||
end
|
||||
"s": {sv_row, sv_col, sv_pend} <= {row, col, pend};
|
||||
"u": {row, col, pend} <= {sv_row, sv_col, sv_pend};
|
||||
"n": if (p0 == 5) respond({8'h1B, "[0n", 64'd0}, 4'd4);
|
||||
else if (p0 == 6) cursor_report;
|
||||
"c": if (p0 == 0) respond({8'h1B, "[?62;22c", 24'd0}, 4'd9);
|
||||
default: ;
|
||||
endcase
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user