Files
Basys3Terminal/rtl/basys3_terminal.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

476 lines
18 KiB
Verilog

`timescale 1ns / 1ps
// Basys 3 VT220 terminal with ANSI colour, and a Z-80 computer running BASIC.
// SW15 down: keyboard (USB HID port) -> UART TX (USB-UART port),
// UART RX -> 80x30 colour text on VGA
// SW15 up: the terminal is the Z-80's console
//
// SW1:0 baud rate: 00 115200, 01 9600, 10 921600, 11 3000000 (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
// SW14 in Z-80 mode, link the PC's UART too: bytes from the PC also go to the
// Z-80, and the Z-80's output is copied to the PC
// SW15 Z-80 mode
// BTNC reset BTNU clear screen BTND reset the Z-80 only
// 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); in Z-80 mode, ports 21h/20h
//
// 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. In Z-80 mode
// it does the same for the Z-80's input buffer, for text pasted over the UART.
module basys3_terminal #(
parameter FONT_FILE = "font.mem",
parameter MEM_FILE = "basic.mem",
parameter MON_FILE = "monitor.mem",
parameter CLK_HZ = 100_000_000,
parameter real CPU_MULT = 9.0, // Z-80 clock = 100 MHz * CPU_MULT / CPU_DIV
parameter real CPU_DIV = 10.0,
parameter CPU_HZ = 90_000_000
) (
input clk,
input btnC,
input btnU,
input btnD,
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,
output QspiCSn,
inout [3:0] QspiDB
);
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 [15:0] sw_r;
reg [1:0] clr_sync, zrst_sync;
always @(posedge clk) begin
sw_r <= sw;
clr_sync <= {clr_sync[0], btnU};
zrst_sync <= {zrst_sync[0], btnD};
end
wire z80_mode = sw_r[15];
wire z80_link = sw_r[14];
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 / 921600; // 108: 0.5% fast
2'b11: baud_div <= CLK_HZ / 3000000; // 33: 1% fast
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)
);
wire cad;
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), .cad(cad)
);
// Ctrl-Alt-Del in Z-80 mode restarts the Z-80, like BTND (held 1 us)
reg [6:0] cad_t = 7'd0;
always @(posedge clk) cad_t <= cad && z80_mode ? 7'd100 : cad_t - (cad_t != 0);
// ----------------------------------------------------------- host input
// Keyboard bytes and terminal replies go to the host: the PC's UART, or in
// Z-80 mode the Z-80's input FIFO. The keyboard/reply choice sticks until
// that queue is empty, so multi-byte sequences are never interleaved.
// Flow control bytes for the PC go first.
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 [7:0] h_byte = from_resp ? rq : kq;
wire h_avail = from_resp ? !rq_empty : !kq_empty;
wire pop_pend = kq_rd | rq_rd; // last byte taken pops this clock
// Z-80 input FIFO: keyboard/replies, and the PC's UART when linked
wire [7:0] rx_byte;
wire rx_valid;
wire [7:0] zin;
wire zin_empty, zin_full;
wire [10:0] zin_count;
reg zin_rd;
wire rx_to_z = z80_mode && z80_link && rx_valid;
wire take_z = z80_mode && h_avail && !zin_full && !rx_to_z && !pop_pend;
fifo #(.W(8), .A(10)) z80_in_fifo (
.clk(clk), .rst(rst), .din(rx_to_z ? rx_byte : h_byte), .wr(rx_to_z || take_z),
.rd(zin_rd), .dout(zin), .empty(zin_empty), .full(zin_full), .count(zin_count)
);
// Z-80 output copied to the PC when linked
wire [7:0] mq;
wire mq_empty;
wire [10:0] mq_count;
reg mq_rd;
wire z80_out; // a Z-80 byte is going to the screen
wire [7:0] z80_out_byte;
fifo #(.W(8), .A(10)) mirror_fifo (
.clk(clk), .rst(rst), .din(z80_out_byte), .wr(z80_out && z80_link),
.rd(mq_rd), .dout(mq), .empty(mq_empty), .full(), .count(mq_count)
);
wire [10:0] tq_count;
wire [10:0] flow_count = z80_mode ? zin_count : tq_count;
reg xoff_sent;
always @(posedge clk) begin
tx_start <= 1'b0;
kq_rd <= 1'b0;
rq_rd <= 1'b0;
mq_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 && flow_count >= 256) begin
flow_pend <= 1'b1; flow_byte <= XOFF; xoff_sent <= 1'b1;
end else if (!flow_pend && xoff_sent && flow_count < 32) begin
flow_pend <= 1'b1; flow_byte <= XON; xoff_sent <= 1'b0;
end
if (take_z) begin
kq_rd <= !from_resp;
rq_rd <= from_resp;
end else if (!h_avail)
from_resp <= !from_resp;
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 (!z80_mode && h_avail && !pop_pend) begin
tx_byte <= h_byte;
tx_start <= 1'b1;
kq_rd <= !from_resp;
rq_rd <= from_resp;
end else if (z80_mode && !mq_empty && !mq_rd) begin
tx_byte <= mq;
tx_start <= 1'b1;
mq_rd <= 1'b1;
end
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
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 80x32 ",
"\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"};
// (not shown in Z-80 mode, where BASIC prints its own)
reg [7:0] ban_i;
wire [7:0] ban_c = BANNER[ban_i*8 +: 8];
// ---------------------------------------------------------------- Z-80
wire cpu_clk, locked;
`ifdef SIMULATION
assign cpu_clk = clk;
assign locked = 1'b1;
`else
wire cpu_fb, cpu_clk_mmcm;
MMCME2_BASE #(
.CLKIN1_PERIOD(10.0), .DIVCLK_DIVIDE(1),
.CLKFBOUT_MULT_F(CPU_MULT), .CLKOUT0_DIVIDE_F(CPU_DIV)
) cpu_mmcm (
.CLKIN1(clk), .CLKFBIN(cpu_fb), .CLKFBOUT(cpu_fb), .CLKFBOUTB(),
.CLKOUT0(cpu_clk_mmcm), .CLKOUT0B(), .CLKOUT1(), .CLKOUT1B(), .CLKOUT2(),
.CLKOUT2B(), .CLKOUT3(), .CLKOUT3B(), .CLKOUT4(), .CLKOUT5(), .CLKOUT6(),
.LOCKED(locked), .PWRDWN(1'b0), .RST(1'b0)
);
BUFG cpu_bufg (.I(cpu_clk_mmcm), .O(cpu_clk));
`endif
// ------------------------------------------------- pixel clock, 108 MHz
// 1280x1024 at 60 Hz: 100 MHz x 10.125 / 9.375
wire pix_clk;
`ifdef SIMULATION
assign pix_clk = clk;
`else
wire pix_fb, pix_clk_mmcm;
MMCME2_BASE #(
.CLKIN1_PERIOD(10.0), .DIVCLK_DIVIDE(1),
.CLKFBOUT_MULT_F(10.125), .CLKOUT0_DIVIDE_F(9.375)
) pix_mmcm (
.CLKIN1(clk), .CLKFBIN(pix_fb), .CLKFBOUT(pix_fb), .CLKFBOUTB(),
.CLKOUT0(pix_clk_mmcm), .CLKOUT0B(), .CLKOUT1(), .CLKOUT1B(), .CLKOUT2(),
.CLKOUT2B(), .CLKOUT3(), .CLKOUT3B(), .CLKOUT4(), .CLKOUT5(), .CLKOUT6(),
.LOCKED(), .PWRDWN(1'b0), .RST(1'b0)
);
BUFG pix_bufg (.I(pix_clk_mmcm), .O(pix_clk));
`endif
(* ASYNC_REG = "TRUE" *) reg [1:0] pix_rst_s;
always @(posedge pix_clk) pix_rst_s <= {pix_rst_s[0], rst};
(* ASYNC_REG = "TRUE" *) reg [2:0] cpu_rst_s;
always @(posedge cpu_clk) cpu_rst_s <= {cpu_rst_s[1:0], rst | zrst_sync[1] | (cad_t != 0) | !locked};
// Z-80 input: one byte at a time from z80_in_fifo
reg zrx_req, zrx_pend;
reg [7:0] zrx_data;
wire zrx_ack;
(* ASYNC_REG = "TRUE" *) reg [1:0] zrx_ack_s;
always @(posedge clk) begin
zin_rd <= 1'b0;
zrx_ack_s <= {zrx_ack_s[0], zrx_ack};
if (rst)
zrx_pend <= 1'b0;
else if (!zrx_pend && !zin_empty && !zin_rd) begin
zrx_data <= zin;
zin_rd <= 1'b1;
zrx_req <= ~zrx_req;
zrx_pend <= 1'b1;
end else if (zrx_pend && zrx_ack_s[1] == zrx_req)
zrx_pend <= 1'b0;
end
initial zrx_req = 1'b0;
// Z-80 output
wire ztx_req;
wire [7:0] ztx_data;
reg ztx_ack = 1'b0;
(* ASYNC_REG = "TRUE" *) reg [1:0] ztx_req_s;
always @(posedge clk) ztx_req_s <= {ztx_req_s[0], ztx_req};
wire [15:0] z80_seg;
wire fl_sck;
wire [16:0] gfx_addr;
wire [3:0] gfx_pix;
// The Pause key (its scan codes start E1, used by nothing else) enters the
// Z-80 monitor: a toggle crosses to the CPU clock.
reg brk_t = 1'b0;
always @(posedge clk) if (scan_valid && scan == 8'hE1 && z80_mode) brk_t <= ~brk_t;
(* ASYNC_REG = "TRUE" *) reg [2:0] brk_s = 3'd0;
always @(posedge cpu_clk) brk_s <= {brk_s[1:0], brk_t};
z80_system #(.MEM_FILE(MEM_FILE), .MON_FILE(MON_FILE), .CPU_HZ(CPU_HZ)) z80 (
.clk(cpu_clk), .rst(cpu_rst_s[2]),
.rx_req(zrx_req), .rx_data(zrx_data), .rx_ack(zrx_ack),
.tx_req(ztx_req), .tx_data(ztx_data), .tx_ack(ztx_ack),
.seg_val(z80_seg), .halted(), .brk_key(brk_s[2] ^ brk_s[1]),
.vclk(pix_clk), .gfx_vaddr(gfx_addr), .gfx_vpix(gfx_pix),
.fl_cs_n(QspiCSn), .fl_sck(fl_sck), .fl_mosi(QspiDB[0]), .fl_miso(QspiDB[1])
);
// The flash clock is the configuration clock pin, reached through STARTUPE2.
// DQ2/DQ3 (write protect, hold) stay high.
assign QspiDB[1] = 1'bz;
assign QspiDB[3:2] = 2'b11;
`ifndef SIMULATION
STARTUPE2 #(.PROG_USR("FALSE"), .SIM_CCLK_FREQ(0.0)) startup (
.CFGCLK(), .CFGMCLK(), .EOS(), .PREQ(),
.CLK(1'b0), .GSR(1'b0), .GTS(1'b0), .KEYCLEARB(1'b1), .PACK(1'b0),
.USRCCLKO(fl_sck), .USRCCLKTS(1'b0), .USRDONEO(1'b1), .USRDONETS(1'b1)
);
`endif
// terminal input: Z-80 output or received bytes, local echo, banner
reg echo_pend;
reg [7:0] echo_byte;
reg [7:0] tin;
reg tin_wr;
wire tin_full;
assign z80_out = z80_mode && ztx_req_s[1] != ztx_ack && !tin_wr && tq_count < 1000 &&
(!z80_link || mq_count < 1000);
assign z80_out_byte = ztx_data;
always @(posedge clk) begin
tin_wr <= 1'b0;
if (kq_rd && sw_r[2] && !z80_mode) begin
echo_pend <= 1'b1;
echo_byte <= kq;
end
if (rst) begin
echo_pend <= 1'b0;
ban_i <= z80_mode ? 8'hFF : 8'd199;
end else if (z80_mode) begin
if (z80_out) begin
tin <= ztx_data;
tin_wr <= 1'b1;
ztx_ack <= ~ztx_ack;
end
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] && !z80_mode;
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 [13:0] t_addr;
wire alt_scr, wide;
wire t_we;
wire [17:0] t_wdata, t_rdata;
wire [5:0] cur_row, stop, sbot;
wire [4:0] sm_off;
wire [7:0] cur_col;
wire cur_vis, sm_on, sm_dir;
wire [3:0] dec_leds;
wire bell;
wire frame_tick, pix_frame;
// start of vertical blanking, from the pixel clock to this one
reg frame_t = 1'b0;
always @(posedge pix_clk) if (pix_frame) frame_t <= ~frame_t;
(* ASYNC_REG = "TRUE" *) reg [2:0] frame_s = 3'd0;
always @(posedge clk) frame_s <= {frame_s[1:0], frame_t};
assign frame_tick = frame_s[2] ^ frame_s[1];
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), .alt_scr(alt_scr), .wide(wide)
);
wire [13:0] v_addr;
wire [17:0] v_data;
// The display reads the terminal's cursor and modes across clocks: they
// change rarely and a stale value lasts one pixel.
// The alternate screen (the Z-80 monitor uses it) has no graphics layer.
char_ram screen (
.clk(clk), .a_alt(alt_scr),
.a_addr(t_addr), .a_we(t_we), .a_wdata(t_wdata), .a_rdata(t_rdata),
.vclk(pix_clk), .b_alt(alt_scr), .b_addr(v_addr), .b_rdata(v_data)
);
text_display #(.FONT_FILE(FONT_FILE)) video (
.clk(pix_clk), .rst(pix_rst_s[1]), .wide(wide),
.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), .gfx_addr(gfx_addr), .gfx_pix(alt_scr ? 4'd0 : gfx_pix),
.red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync),
.frame_tick(pix_frame)
);
// ----------------------------------------------------- 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 (z80_mode ? tin_wr : rx_valid) last_rx <= z80_mode ? tin : rx_byte;
if (scan_valid) last_scan <= scan;
if (tx_start) last_tx <= tx_byte;
rx_led <= (z80_mode ? tin_wr : 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(z80_mode ? z80_seg : {last_scan, last_tx}),
.seg(seg), .an(an));
endmodule