Ignore OSC/DCS/APC/PM/SOS strings; ease timing in the terminal engine

Ubuntu's prompt emits OSC 3008 context markers (ESC ] 3008;... ESC \),
which were printed as text. String sequences are now swallowed up to
BEL or ST, like a real terminal.

CSI commands now run one clock after their final byte with registered
counts, and scroll ranges are computed over two clocks, so the design
no longer scrapes by on timing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
barantandClaude Opus 5.5 committed 2026-11-17 11:32:04 -08:00
1 parent 6e54d75c56
commit 1e4e2101b8
2 files changed
+145 -107

No files matched your search

+143 -106
View File
@@ -13,6 +13,7 @@
// 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.
// 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.
@@ -60,12 +61,12 @@ module term_ctrl (
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;
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 [1:0] ps;
reg [2:0] ps;
reg [7:0] c;
// modes and attributes
@@ -89,6 +90,12 @@ module term_ctrl (
reg priv, gtm, ign;
reg [7:0] cint, inter;
// scroll request
reg sc_dir;
reg [4:0] sc_t, sc_b;
reg [7:0] sc_n;
reg [5:0] sc_h, sc_k; // region height, lines to move
// memory copy / fill engine
reg [11:0] c_src, c_dst, c_cnt, f_addr, f_cnt;
reg c_dir;
@@ -200,9 +207,14 @@ module term_ctrl (
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;
reg [7:0] n8; // count parameter (default 1), registered
reg [6:0] nc; // ... clamped to the rest of the line
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 [7:0] n8_c = p0 == 0 ? 8'd1 : p0;
always @(posedge clk) begin
n8 <= n8_c;
nc <= n8_c > room ? room : n8_c[6:0];
end
wire [3:0] fgb = bold ? (fg | 4'd8) : fg;
wire [3:0] dfg = rev ? bg : fgb;
@@ -251,17 +263,16 @@ module term_ctrl (
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 [4:0] t, input [4:0] b, input [7:0] n, input [3:0] rs);
reg [5:0] h, k;
begin
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);
sc_dir <= dir;
sc_t <= t;
sc_b <= b;
sc_n <= n;
ret_st <= rs;
st <= S_SCR1;
end
endtask
@@ -441,6 +452,20 @@ module term_ctrl (
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[5:0];
st <= S_SCR2;
end
S_SCR2:
if (!sc_dir)
op(rc(sc_t + sc_k, 0), rc(sc_t, 0), rows80(sc_h - sc_k), 1'b0,
rc(sc_b - sc_k + 1'b1, 0), rows80(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, rows80(sc_h - sc_k), 1'b1,
rc(sc_t, 0), rows80(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;
@@ -527,7 +552,11 @@ module term_ctrl (
S_DISP: begin
st <= S_IDLE;
if (c == 8'h1B) begin
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
@@ -568,6 +597,8 @@ module term_ctrl (
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;
@@ -627,101 +658,107 @@ module term_ctrl (
cint <= c;
else if (c >= 8'h40 && c <= 8'h7E) begin
ps <= P_NORM;
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
"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
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(12'd0, here + 1'b1);
else if (p0 == 2) 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
"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
default: st <= S_IDLE;
endcase
end
+2 -1
View File
@@ -215,7 +215,8 @@ module tb_terminal;
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]3008;start=1f2e;user=brad;type=shell\033\\\033]0;title\007",
"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");