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
+57 -19

No files matched your search

+55 -18
View File
@@ -13,6 +13,7 @@
// Modes: IRM(4) LNM(20) DECCKM(?1) DECSCLM(?4) DECOM(?6) DECAWM(?7) DECTCEM(?25) // 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) // 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. // 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 // 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. // 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, 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_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_PUT = 4'd8, S_TAB = 4'd9, S_SMOOTH = 4'd10, S_SMEND = 4'd11,
S_OP = 4'd12; S_OP = 4'd12, S_EXEC = 4'd13, S_SCR1 = 4'd14, S_SCR2 = 4'd15;
localparam [1:0] P_NORM = 2'd0, P_ESC = 2'd1, P_ESCI = 2'd2, P_CSI = 2'd3; 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; 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 [3:0] st, ret_st, sm_ret;
reg [1:0] ps; reg [2:0] ps;
reg [7:0] c; reg [7:0] c;
// modes and attributes // modes and attributes
@@ -89,6 +90,12 @@ module term_ctrl (
reg priv, gtm, ign; reg priv, gtm, ign;
reg [7:0] cint, inter; 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 // memory copy / fill engine
reg [11:0] c_src, c_dst, c_cnt, f_addr, f_cnt; reg [11:0] c_src, c_dst, c_cnt, f_addr, f_cnt;
reg c_dir; reg c_dir;
@@ -200,9 +207,14 @@ module term_ctrl (
wire [17:0] BLANK = {2'b00, 4'd0, def_fg, 8'h20}; wire [17:0] BLANK = {2'b00, 4'd0, def_fg, 8'h20};
wire [7:0] p0 = par[0]; wire [7:0] p0 = par[0];
wire [7:0] p1 = par[1]; 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] 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] fgb = bold ? (fg | 4'd8) : fg;
wire [3:0] dfg = rev ? bg : fgb; wire [3:0] dfg = rev ? bg : fgb;
@@ -251,17 +263,16 @@ module term_ctrl (
endtask 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
// 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); 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 begin
h = b - t + 1'b1; sc_dir <= dir;
k = n > h ? h : n[5:0]; sc_t <= t;
if (!dir) sc_b <= b;
op(rc(t + k, 0), rc(t, 0), rows80(h - k), 1'b0, sc_n <= n;
rc(b - k + 1'b1, 0), rows80(k), BLANK, rs); ret_st <= rs;
else st <= S_SCR1;
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 end
endtask endtask
@@ -441,6 +452,20 @@ module term_ctrl (
st <= S_DISP; st <= S_DISP;
end 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 // ---- copy c_cnt cells, then fill f_cnt cells, then go to ret_st
S_OP: S_OP:
st <= c_cnt != 0 ? S_CRD : f_cnt != 0 ? S_FILL : ret_st; st <= c_cnt != 0 ? S_CRD : f_cnt != 0 ? S_FILL : ret_st;
@@ -527,7 +552,11 @@ module term_ctrl (
S_DISP: begin S_DISP: begin
st <= S_IDLE; 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; ps <= P_ESC;
end else if (c == 8'h18 || c == 8'h1A) begin end else if (c == 8'h18 || c == 8'h1A) begin
ps <= P_NORM; // CAN / SUB cancel a sequence ps <= P_NORM; // CAN / SUB cancel a sequence
@@ -568,6 +597,8 @@ module term_ctrl (
inter <= c; inter <= c;
ps <= P_ESCI; ps <= P_ESCI;
end else case (c) end else case (c)
"]", "P", "_", "^", "X":
ps <= P_STR; // string sequences are ignored
"[": begin "[": begin
ps <= P_CSI; ps <= P_CSI;
for (i = 0; i < 16; i = i + 1) par[i] <= 8'd0; for (i = 0; i < 16; i = i + 1) par[i] <= 8'd0;
@@ -627,6 +658,15 @@ module term_ctrl (
cint <= c; cint <= c;
else if (c >= 8'h40 && c <= 8'h7E) begin else if (c >= 8'h40 && c <= 8'h7E) begin
ps <= P_NORM; 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 if (ign) begin
end else if (cint == "!") begin end else if (cint == "!") begin
if (c == "p") soft_reset; if (c == "p") soft_reset;
@@ -719,9 +759,6 @@ module term_ctrl (
default: ; default: ;
endcase endcase
end end
default: ps <= P_NORM;
endcase
end
default: st <= S_IDLE; default: st <= S_IDLE;
endcase endcase
end 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("\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("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("\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[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("\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"); uart_str("r1\015\012r2\015\012r3\015\012r4\015\012r5\015\012r6");