Basys 3 ASCII terminal: VGA 80x30 text, USB keyboard, UART

- VGA 640x480@60 text display, 8x16 CP437 font ROM from Linux console fonts
- USB HID keyboard (PS/2 via onboard PIC24), scan code set 2 to ASCII
  with xterm sequences for cursor/editing keys
- UART RX/TX with switch-selectable baud, local echo option
- Terminal engine: control codes, deferred wrap, hardware scroll,
  VT100/ANSI subset (cursor moves, erase, reverse video, cursor hide)
- Vivado build/program/flash scripts and xsim whole-design testbench

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
barantandClaude Opus 5.5 committed 2026-11-17 10:26:13 -08:00
commit 0e95a14cfe
18 files changed
+5596

No files matched your search

+228
View File
@@ -0,0 +1,228 @@
`timescale 1ns / 1ps
// Basys 3 ASCII terminal.
// Keyboard (USB HID port) -> UART TX (USB-UART port)
// UART RX -> 80x30 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 colour: 00 green, 01 amber, 10 white, 11 cyan
// SW6 Backspace sends DEL (7F) instead of BS (08)
// SW7 Enter sends CR LF instead of CR
// BTNC reset BTNU clear screen
// LED7:0 last byte received LED12 bell LED13 Caps Lock
// LED14 UART RX activity LED15 keyboard activity
// 7-seg last scan code (left), last byte sent (right)
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
);
// ---------------------------------------------------------------- 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 [7:0] sw_r;
reg [1:0] clr_sync;
always @(posedge clk) begin
sw_r <= sw[7: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
// ------------------------------------------------------------- keyboard
wire [7:0] scan;
wire scan_valid;
wire [7:0] key;
wire key_valid;
wire caps;
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]),
.ascii(key), .ascii_valid(key_valid), .caps(caps)
);
// ------------------------------------------------------------- transmit
wire [7:0] kq;
wire kq_empty;
wire tx_busy;
reg tx_start;
reg [7:0] tx_byte;
fifo #(.W(8), .A(4)) key_fifo (
.clk(clk), .rst(rst), .din(key), .wr(key_valid),
.rd(tx_start), .dout(kq), .empty(kq_empty), .full()
);
always @(posedge clk) begin
tx_start <= 1'b0;
if (!kq_empty && !tx_busy && !tx_start && !rst) begin
tx_start <= 1'b1;
tx_byte <= kq;
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*160-1:0] BANNER = {
"Basys 3 ASCII Terminal 80x30\015\012",
"UART RX -> screen, keyboard -> UART TX\015\012",
"SW1:0 baud 00=115200 01=9600 10=19200 11=57600 SW2 echo\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 (tx_start && sw_r[2]) begin
echo_pend <= 1'b1;
echo_byte <= kq;
end
if (rst) begin
echo_pend <= 1'b0;
ban_i <= 8'd159;
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 <= tx_start && 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(6)) term_fifo (
.clk(clk), .rst(rst), .din(tin), .wr(tin_wr),
.rd(tq_rd), .dout(tq), .empty(tq_empty), .full(tin_full)
);
// ------------------------------------------------------------- terminal
wire we;
wire [11:0] waddr;
wire [8:0] wdata;
wire [4:0] top, cur_row;
wire [6:0] cur_col;
wire cur_vis;
wire bell;
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]),
.we(we), .waddr(waddr), .wdata(wdata),
.top(top), .row(cur_row), .col(cur_col), .cur_vis(cur_vis), .bell(bell)
);
wire [11:0] raddr;
wire [8:0] rdata;
char_ram screen (
.clk(clk), .we(we), .waddr(waddr), .wdata(wdata),
.raddr(raddr), .rdata(rdata)
);
reg [11:0] fg;
always @(posedge clk)
case (sw_r[5:4])
2'b00: fg <= 12'h3F3; // green
2'b01: fg <= 12'hFA0; // amber
2'b10: fg <= 12'hDDD; // white
2'b11: fg <= 12'h0EF; // cyan
endcase
text_display #(.FONT_FILE(FONT_FILE)) video (
.clk(clk), .rst(rst), .top(top), .cur_row(cur_row), .cur_col(cur_col),
.cur_vis(cur_vis), .fg(fg), .bg(12'h000),
.ram_addr(raddr), .ram_data(rdata),
.red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync)
);
// ----------------------------------------------------- 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 <= kq;
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, 4'b0000, last_rx};
assign dp = 1'b1;
sevenseg digits (.clk(clk), .value({last_scan, last_tx}), .seg(seg), .an(an));
endmodule