Files
Basys3Terminal/rtl/term_ctrl.v
T
brad ArantandClaude Opus 5.5 88b5942da5 1280x1024 display: 80x32 big font or 132x64 (DECCOLM)
The display runs at 1280x1024 60 Hz from a second MMCM (108 MHz pixel
clock), one pixel per clock through a four-stage pipeline. 80 columns
show 80x32 cells of 16x32 pixels (the 8x16 font doubled); CSI ?3 h
switches to 132x64 cells of 8x16 pixels, centred, and CSI ?3 l back,
both clearing the screen and margins as a VT100 does. The 320x240
graphics layer is drawn 4x4 in the top 960 lines, so 80-column row r
still meets graphics line 8(r-1).

The terminal engine has 6-bit rows and 8-bit columns, a 132-cell row
stride in both layouts (so the copy engine is unchanged), 132 tab stops
and 3-digit cursor reports. The character RAM holds two 132x64 pages
(normal and alternate) and is read on the pixel clock.

BASIC's LOCATE takes rows up to 64 and columns up to 132. terminfo has
basys3 (80x32) and basys3-w (132x64). The testbench reads the 132-cell
layout, checks 1280x1024 sync timing, captures 1280x1024 frames and
tests DECCOLM both ways.

Timing met (0.044 ns at 90 MHz CPU, 108 MHz pixel); 46 of 50 BRAMs.

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

790 lines
30 KiB
Verilog

`timescale 1ns / 1ps
// VT220 terminal engine with ANSI colour. Consumes received bytes and keeps
// the character buffer up to date: 80x32, or 132x64 after DECCOLM (CSI ?3h).
// The buffer is row-major with a 132-cell stride in both (address = row*132 +
// col), so scrolling copies whole 132-cell rows either way.
//
// 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) DECCOLM(?3) DECSCLM(?4) DECOM(?6) DECAWM(?7)
// DECTCEM(?25)
// alternate screen ?47 (parameters stop at 255, so not ?1047/?1049)
// 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.
// OSC, DCS, APC, PM and SOS strings (e.g. titles, OSC 3008) are discarded.
// 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,
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,
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 [13:0] addr,
output reg we,
output reg [17:0] wdata,
input [17:0] rdata,
// to the display
output reg [5:0] row,
output reg [7:0] col,
output reg cur_vis,
output reg [5:0] stop, // scroll region, inclusive
output reg [5: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,
output reg alt_scr, // showing and writing the alternate screen
output reg wide // 132 columns (DECCOLM)
);
localparam [13:0] STRIDE = 14'd132;
localparam [13:0] ALL = 14'd8448; // 132 x 64: clears either layout
wire [5:0] LAST_ROW = wide ? 6'd63 : 6'd31;
wire [7:0] LAST_COL = wide ? 8'd131 : 8'd79;
wire [7:0] NCOLS = wide ? 8'd132 : 8'd80;
wire [6:0] NROWS = wide ? 7'd64 : 7'd32;
wire [13:0] CELLS = wide ? 14'd8448 : 14'd4224;
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, S_EXEC = 4'd13, S_SCR1 = 4'd14, S_SCR2 = 4'd15;
localparam [2:0] P_NORM = 3'd0, P_ESC = 3'd1, P_ESCI = 3'd2, P_CSI = 3'd3, P_STR = 3'd4;
localparam [1:0] K_SGR = 2'd0, K_SET = 2'd1, K_RST = 2'd2, K_LL = 2'd3;
reg [3:0] st, ret_st, sm_ret;
reg [2:0] ps;
reg [7:0] c;
// 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 [131:0] tabs;
reg colm_clr; // DECCOLM: clear the screen when idle
// saved cursor (DECSC)
reg [5:0] sv_row;
reg [7: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;
// scroll request
reg sc_dir;
reg [5:0] sc_t, sc_b;
reg [7:0] sc_n;
reg [6:0] sc_h, sc_k; // region height, lines to move
// memory copy / fill engine
reg [13: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 [13:0] rc(input [5:0] r, input [7:0] cc);
rc = {1'b0, r, 7'b0} + {6'b0, r, 2'b0} + {6'b0, cc};
endfunction
function [13:0] rows(input [6:0] n); // n whole rows of cells
rows = {n, 7'b0} + {5'b0, n, 2'b0};
endfunction
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
// 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
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
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 [7:0] next_tab(input [131:0] t, input [7:0] cc, input [7:0] last);
integer k;
begin
next_tab = last;
for (k = 131; k >= 0; k = k - 1)
if (k > cc && k < last && t[k]) next_tab = k;
end
endfunction
function [7:0] prev_tab(input [131:0] t, input [7:0] cc);
integer k;
begin
prev_tab = 8'd0;
for (k = 0; k < 132; 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];
reg [7:0] n8; // count parameter (default 1), registered
reg [7:0] nc; // ... clamped to the rest of the line
wire [7:0] room = NCOLS - col; // cells from cursor to end of line
wire [7:0] n8_c = p0 == 0 ? 8'd1 : p0;
always @(posedge clk) begin
n8 <= n8_c;
nc <= n8_c > room ? room : n8_c;
end
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 [5:0] up_lim = row >= stop ? stop : 6'd0;
wire [5:0] dn_lim = row <= sbot ? sbot : LAST_ROW;
wire [5:0] row_up = (row - up_lim > n8) ? row - n8 : up_lim;
wire [5:0] row_dn = (dn_lim - row > n8) ? row + n8 : dn_lim;
wire [7:0] col_rt = (LAST_COL - col > n8) ? col + n8 : LAST_COL;
wire [7:0] col_lt = (col > n8) ? col - n8 : 8'd0;
wire [7:0] r_rel = p0 == 0 ? 8'd0 : p0 - 1'b1;
wire [8:0] r_abs = (om ? stop : 6'd0) + r_rel;
wire [5:0] r_max = om ? sbot : LAST_ROW;
wire [5:0] row_p0 = r_abs > r_max ? r_max : r_abs[5:0];
wire [7:0] col_p0 = p0 == 0 ? 8'd0 : p0 > NCOLS ? LAST_COL : p0 - 1'b1;
wire [7:0] col_p1 = p1 == 0 ? 8'd0 : p1 > NCOLS ? LAST_COL : p1 - 1'b1;
wire [13:0] here = rc(row, col);
wire [6:0] rep_row = (om ? row - stop : row) + 1'b1;
wire [7:0] rep_col = col + 1'b1;
reg [7:0] pv; // parameter being processed, registered
reg pv_ok;
// ------------------------------------------------------------ tasks
task op(input [13:0] src, input [13:0] dst, input [13:0] ccnt, input dir,
input [13:0] fa, input [13: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_OP; // decided next clock, off the critical path
end
endtask
task fill(input [13:0] fa, input [13:0] fcnt);
op(14'd0, 14'd0, 14'd0, 1'b0, fa, fcnt, BLANK, S_IDLE);
endtask
// scroll rows t..b up (dir 0) or down (dir 1) by n lines
// scroll rows t..b up (dir 0) or down (dir 1) by n lines; the copy and fill
// ranges are worked out over the next two clocks (S_SCR1, S_SCR2)
task scroll(input dir, input [5:0] t, input [5:0] b, input [7:0] n, input [3:0] rs);
begin
sc_dir <= dir;
sc_t <= t;
sc_b <= b;
sc_n <= n;
ret_st <= rs;
st <= S_SCR1;
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
if (row != LAST_ROW) row <= row + 1'b1;
st <= rs;
end
end
endtask
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
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;
alt_scr <= 1'b0;
stop <= 6'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, wide, colm_clr} <= 4'b0;
sbot <= 6'd31;
dec_leds <= 4'd0;
sm_on <= 1'b0;
sm_off <= 5'd0;
ps <= P_NORM;
for (i = 0; i < 132; i = i + 1) tabs[i] <= i != 0 && i % 8 == 0;
fill(14'd0, ALL);
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 [7:0] v);
tens = "0" + (v / 8'd10) % 8'd10;
endfunction
function [7:0] ones(input [7:0] v);
ones = "0" + v % 8'd10;
endfunction
// ESC [ rr ; cc R, or ESC [ rr ; 1cc R past column 99
task cursor_report;
if (rep_col >= 100)
respond({8'h1B, "[", tens(rep_row), ones(rep_row), ";", "1",
tens(rep_col), ones(rep_col), "R", 24'd0}, 4'd9);
else
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 == 3) begin // DECCOLM: 80x32 / 132x64, cleared
wide <= on; colm_clr <= 1'b1;
{row, col, pend, om, stop} <= 0;
sbot <= on ? 6'd63 : 6'd31;
end
if (v == 4) sclm <= on;
if (v == 6) begin om <= on; row <= on ? stop : 6'd0; col <= 8'd0; pend <= 1'b0; end
if (v == 7) awm <= on;
if (v == 25) cur_vis <= on;
if (v == 47) alt_scr <= 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 || colm_clr) begin
{row, col, pend, colm_clr} <= 0;
fill(14'd0, ALL);
end else if (!in_empty && !in_rd) begin
c <= in_byte;
in_rd <= 1'b1;
st <= S_DISP;
end
// ---- scroll: size the move, then set up the copy and fill
S_SCR1: begin
sc_h <= sc_b - sc_t + 1'b1;
sc_k <= sc_n > sc_b - sc_t ? sc_b - sc_t + 1'b1 : sc_n[6:0];
st <= S_SCR2;
end
S_SCR2:
if (!sc_dir)
op(rc(sc_t + sc_k, 0), rc(sc_t, 0), rows(sc_h - sc_k), 1'b0,
rc(sc_b - sc_k + 1'b1, 0), rows(sc_k), BLANK, ret_st);
else
op(rc(sc_b - sc_k + 1'b1, 0) - 1'b1, rc(sc_b + 1'b1, 0) - 1'b1, rows(sc_h - sc_k), 1'b1,
rc(sc_t, 0), rows(sc_k), BLANK, ret_st);
// ---- 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;
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, LAST_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;
if (ps == P_STR) begin
// inside OSC/DCS/APC/PM/SOS: discard until BEL or ESC \ (ST)
if (c == 8'h07 || c == 8'h18 || c == 8'h1A) ps <= P_NORM;
else if (c == 8'h1B) ps <= P_ESC;
end else 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 <= next_tab(tabs, col, LAST_COL); end
8'h0A, 8'h0B, 8'h0C: begin
pend <= 1'b0;
if (lf_cr || lnm) col <= 8'd0;
line_feed(S_IDLE);
end
8'h0D: begin pend <= 1'b0; col <= 8'd0; end
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 <= 8'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), {6'd0, LAST_COL - col}, 1'b1,
14'd0, 14'd0, BLANK, S_PUT);
else
st <= S_PUT;
P_ESC: begin
ps <= P_NORM;
if (c >= 8'h20 && c <= 8'h2F) begin
inter <= c;
ps <= P_ESCI;
end else case (c)
"]", "P", "_", "^", "X":
ps <= P_STR; // string sequences are ignored
"[": begin
ps <= P_CSI;
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 <= 8'd0; line_feed(S_IDLE); end
"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(14'd0, 14'd0, 14'd0, 1'b0, 14'd0, CELLS, {2'b00, 4'd0, def_fg, "E"}, S_IDLE);
end
default: ;
endcase
end
P_CSI:
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;
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;
st <= S_EXEC; // run it next clock, off the parser's path
end
default: ps <= P_NORM;
endcase
end
// ---- CSI final byte (in c): parameters and cursor are unchanged
S_EXEC: begin
st <= S_IDLE;
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(14'd0, here + 1'b1);
else if (p0 == 2) fill(14'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), STRIDE);
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 <= 8'd0; end
"F": begin pend <= 1'b0; row <= row_up; col <= 8'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(14'd0, here + 1'b1);
else if (p0 == 2 || p0 == 3) fill(14'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), STRIDE);
end
"L": if (row >= stop && row <= sbot) begin
pend <= 1'b0; col <= 8'd0;
scroll(1'b1, row, sbot, n8, S_IDLE);
end
"M": if (row >= stop && row <= sbot) begin
pend <= 1'b0; col <= 8'd0;
scroll(1'b0, row, sbot, n8, S_IDLE);
end
"@": begin
pend <= 1'b0;
op(rc(row, LAST_COL - nc), rc(row, LAST_COL), {6'd0, room - nc}, 1'b1,
here, {6'd0, nc}, BLANK, S_IDLE);
end
"P": begin
pend <= 1'b0;
op(rc(row, col + nc), here, {6'd0, room - nc}, 1'b0,
rc(row, NCOLS - nc), {6'd0, nc}, BLANK, S_IDLE);
end
"X": begin pend <= 1'b0; fill(here, {6'd0, 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 <= 132'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 > NROWS ? {1'b0, NROWS} : p1)) begin
stop <= p0 == 0 ? 6'd0 : p0 - 1'b1;
sbot <= p1 == 0 || p1 > NROWS ? LAST_ROW : p1 - 1'b1;
row <= om ? (p0 == 0 ? 6'd0 : p0 - 1'b1) : 6'd0;
col <= 8'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
default: st <= S_IDLE;
endcase
end
endmodule