Files
Basys3Terminal/rtl/basys3_terminal.v
T
barantandClaude Opus 5.5 6e54d75c56 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>
2026-11-17 11:18:36 -08:00

289 lines
9.7 KiB
Verilog

`timescale 1ns / 1ps
// Basys 3 VT220 terminal with ANSI colour.
// Keyboard (USB HID port) -> UART TX (USB-UART port)
// UART RX -> 80x30 colour text on VGA
//
// SW1:0 baud rate: 00 115200, 01 9600, 10 19200, 11 57600 (8N1)
// SW2 local echo
// SW3 received LF also does CR
// SW5:4 default text colour: 00 green, 01 yellow, 10 white, 11 cyan
// SW6 Backspace sends DEL (7F) instead of BS (08)
// SW7 Enter sends CR LF instead of CR
// SW8 smooth scrolling (also turned on by ESC [ ? 4 h)
// SW9 fast smooth scrolling
// BTNC reset BTNU clear screen
// LED7:0 last byte received LED11:8 VT220 LEDs L4..L1 (DECLL)
// LED12 bell LED13 Caps Lock LED14 UART RX activity LED15 keyboard activity
// 7-seg last scan code (left), last byte sent (right)
//
// The terminal sends XOFF when its input buffer passes 256 bytes and XON when
// it drains below 32, so smooth scrolling can hold the host back.
module basys3_terminal #(
parameter FONT_FILE = "font.mem",
parameter CLK_HZ = 100_000_000
) (
input clk,
input btnC,
input btnU,
input [15:0] sw,
output [15:0] led,
output [6:0] seg,
output dp,
output [3:0] an,
output [3:0] vgaRed,
output [3:0] vgaGreen,
output [3:0] vgaBlue,
output Hsync,
output Vsync,
input RsRx,
output RsTx,
input PS2Clk,
input PS2Data
);
localparam [7:0] XON = 8'h11, XOFF = 8'h13;
// ---------------------------------------------------------------- reset
reg [1:0] btn_sync;
reg [15:0] por = 16'd0;
reg rst = 1'b1;
always @(posedge clk) begin
btn_sync <= {btn_sync[0], btnC};
if (btn_sync[1]) por <= 16'd0;
else if (!por[15]) por <= por + 1'b1;
rst <= !por[15];
end
reg [9:0] sw_r;
reg [1:0] clr_sync;
always @(posedge clk) begin
sw_r <= sw[9:0];
clr_sync <= {clr_sync[0], btnU};
end
reg [13:0] baud_div;
always @(posedge clk)
case (sw_r[1:0])
2'b00: baud_div <= CLK_HZ / 115200;
2'b01: baud_div <= CLK_HZ / 9600;
2'b10: baud_div <= CLK_HZ / 19200;
2'b11: baud_div <= CLK_HZ / 57600;
endcase
reg [3:0] def_fg;
always @(posedge clk)
case (sw_r[5:4])
2'b00: def_fg <= 4'd10; // bright green
2'b01: def_fg <= 4'd11; // yellow
2'b10: def_fg <= 4'd7; // white
2'b11: def_fg <= 4'd14; // bright cyan
endcase
// ------------------------------------------------------------- keyboard
wire [7:0] scan;
wire scan_valid;
wire [7:0] key;
wire key_valid;
wire caps;
wire cursor_app, keypad_app, lnm;
ps2_rx kbd_rx (
.clk(clk), .rst(rst), .ps2_clk(PS2Clk), .ps2_data(PS2Data),
.code(scan), .valid(scan_valid)
);
ps2_keymap kbd_map (
.clk(clk), .rst(rst), .code(scan), .code_valid(scan_valid),
.bs_del(sw_r[6]), .enter_crlf(sw_r[7] | lnm),
.cursor_app(cursor_app), .keypad_app(keypad_app),
.ascii(key), .ascii_valid(key_valid), .caps(caps)
);
// ------------------------------------------------------------- transmit
// Sources: flow control (first), then keyboard and terminal replies. The
// keyboard/reply choice sticks until that queue is empty, so multi-byte
// sequences are never interleaved.
wire [7:0] kq, rq;
wire kq_empty, rq_empty, rq_full;
wire [7:0] resp_byte;
wire resp_wr;
wire tx_busy;
reg tx_start, kq_rd, rq_rd, from_resp;
reg [7:0] tx_byte;
reg flow_pend;
reg [7:0] flow_byte;
fifo #(.W(8), .A(4)) key_fifo (
.clk(clk), .rst(rst), .din(key), .wr(key_valid),
.rd(kq_rd), .dout(kq), .empty(kq_empty), .full(), .count()
);
fifo #(.W(8), .A(4)) resp_fifo (
.clk(clk), .rst(rst), .din(resp_byte), .wr(resp_wr),
.rd(rq_rd), .dout(rq), .empty(rq_empty), .full(rq_full), .count()
);
wire [10:0] tq_count;
reg xoff_sent;
always @(posedge clk) begin
tx_start <= 1'b0;
kq_rd <= 1'b0;
rq_rd <= 1'b0;
if (rst) begin
from_resp <= 1'b0;
flow_pend <= 1'b0;
xoff_sent <= 1'b0;
end else begin
if (!flow_pend && !xoff_sent && tq_count >= 256) begin
flow_pend <= 1'b1; flow_byte <= XOFF; xoff_sent <= 1'b1;
end else if (!flow_pend && xoff_sent && tq_count < 32) begin
flow_pend <= 1'b1; flow_byte <= XON; xoff_sent <= 1'b0;
end
if (!tx_busy && !tx_start) begin
if (flow_pend) begin
tx_byte <= flow_byte;
tx_start <= 1'b1;
flow_pend <= 1'b0;
end else if (!from_resp && !kq_empty) begin
tx_byte <= kq;
tx_start <= 1'b1;
kq_rd <= 1'b1;
end else if (from_resp && !rq_empty) begin
tx_byte <= rq;
tx_start <= 1'b1;
rq_rd <= 1'b1;
end else
from_resp <= !from_resp;
end
end
end
uart_tx utx (
.clk(clk), .rst(rst), .div(baud_div), .data(tx_byte),
.start(tx_start), .tx(RsTx), .busy(tx_busy)
);
// -------------------------------------------------------------- receive
wire [7:0] rx_byte;
wire rx_valid;
uart_rx urx (
.clk(clk), .rst(rst), .div(baud_div), .rx(RsRx),
.data(rx_byte), .valid(rx_valid)
);
// Start-up banner, fed in like received text. Bytes are read from the
// high end of the string; the unused high bytes are zero and skipped.
localparam [8*200-1:0] BANNER = {
"\033[1mBasys 3 VT220 Terminal\033[0m 80x30 ",
"\033[31mc\033[33mo\033[32ml\033[36mo\033[34mu\033[35mr\033[0m ",
"\033[4munderline\033[0m\015\012",
"UART RX -> screen, keyboard -> UART TX TERM=basys3 or vt220\015\012\012"};
reg [7:0] ban_i;
wire [7:0] ban_c = BANNER[ban_i*8 +: 8];
// terminal input: received bytes, local echo, banner (in that priority)
reg echo_pend;
reg [7:0] echo_byte;
reg [7:0] tin;
reg tin_wr;
wire tin_full;
always @(posedge clk) begin
tin_wr <= 1'b0;
if (kq_rd && sw_r[2]) begin
echo_pend <= 1'b1;
echo_byte <= kq;
end
if (rst) begin
echo_pend <= 1'b0;
ban_i <= 8'd199;
end else if (rx_valid) begin
tin <= rx_byte;
tin_wr <= 1'b1;
end else if (echo_pend && !tin_full) begin
tin <= echo_byte;
tin_wr <= 1'b1;
echo_pend <= kq_rd && sw_r[2];
end else if (ban_i != 8'hFF && !tin_full && !tin_wr) begin
ban_i <= ban_i - 1'b1;
if (ban_c != 0) begin
tin <= ban_c;
tin_wr <= 1'b1;
end
end
end
wire [7:0] tq;
wire tq_empty;
wire tq_rd;
fifo #(.W(8), .A(10)) term_fifo (
.clk(clk), .rst(rst), .din(tin), .wr(tin_wr),
.rd(tq_rd), .dout(tq), .empty(tq_empty), .full(tin_full), .count(tq_count)
);
// ------------------------------------------------------------- terminal
wire [11:0] t_addr;
wire t_we;
wire [17:0] t_wdata, t_rdata;
wire [4:0] cur_row, stop, sbot, sm_off;
wire [6:0] cur_col;
wire cur_vis, sm_on, sm_dir;
wire [3:0] dec_leds;
wire bell;
wire frame_tick;
term_ctrl term (
.clk(clk), .rst(rst), .in_byte(tq), .in_empty(tq_empty), .in_rd(tq_rd),
.lf_cr(sw_r[3]), .clear_req(clr_sync[1]), .def_fg(def_fg),
.smooth_sw(sw_r[8]), .smooth_fast(sw_r[9]), .frame_tick(frame_tick),
.addr(t_addr), .we(t_we), .wdata(t_wdata), .rdata(t_rdata),
.row(cur_row), .col(cur_col), .cur_vis(cur_vis),
.stop(stop), .sbot(sbot), .sm_on(sm_on), .sm_dir(sm_dir), .sm_off(sm_off),
.resp_byte(resp_byte), .resp_wr(resp_wr), .resp_full(rq_full),
.cursor_app(cursor_app), .keypad_app(keypad_app), .lnm(lnm),
.dec_leds(dec_leds), .bell(bell)
);
wire [11:0] v_addr;
wire [17:0] v_data;
char_ram screen (
.clk(clk),
.a_addr(t_addr), .a_we(t_we), .a_wdata(t_wdata), .a_rdata(t_rdata),
.b_addr(v_addr), .b_rdata(v_data)
);
text_display #(.FONT_FILE(FONT_FILE)) video (
.clk(clk), .rst(rst), .cur_row(cur_row), .cur_col(cur_col), .cur_vis(cur_vis),
.stop(stop), .sbot(sbot), .sm_on(sm_on), .sm_dir(sm_dir), .sm_off(sm_off),
.ram_addr(v_addr), .ram_data(v_data),
.red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync),
.frame_tick(frame_tick)
);
// ----------------------------------------------------- status displays
reg [7:0] last_rx;
reg [7:0] last_scan;
reg [7:0] last_tx;
reg [22:0] rx_led, kb_led, bell_led; // ~84 ms pulse stretchers
always @(posedge clk) begin
if (rx_valid) last_rx <= rx_byte;
if (scan_valid) last_scan <= scan;
if (tx_start) last_tx <= tx_byte;
rx_led <= rx_valid ? 23'h7FFFFF : rx_led - (rx_led != 0);
kb_led <= scan_valid ? 23'h7FFFFF : kb_led - (kb_led != 0);
bell_led <= bell ? 23'h7FFFFF : bell_led - (bell_led != 0);
end
assign led = {kb_led != 0, rx_led != 0, caps, bell_led != 0, dec_leds[3:0], last_rx};
assign dp = 1'b1;
sevenseg digits (.clk(clk), .value({last_scan, last_tx}), .seg(seg), .an(an));
endmodule