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

133 lines
4.6 KiB
Verilog

`timescale 1ns / 1ps
// 640x480 @ 60 Hz VGA text display: 80x30 cells of 8x16 pixels, 16 colours,
// underline, blink, block cursor and smooth-scroll offset for the scroll region.
// Runs on the 100 MHz clock with a 25 MHz pixel enable. After each pixel tick:
// clock 1 scanline values registered (row base address, glyph line)
// clock 2 character RAM read
// clock 3 font ROM read
// clock 4 pixel out (next tick)
module text_display #(
parameter FONT_FILE = "font.mem"
) (
input clk,
input rst,
input [4:0] cur_row,
input [6:0] cur_col,
input cur_vis,
input [4:0] stop, // scroll region (rows, inclusive)
input [4:0] sbot,
input sm_on, // smooth scroll in progress
input sm_dir, // 0 = up, 1 = down
input [4:0] sm_off, // pixels the region has moved
output [11:0] ram_addr,
input [17:0] ram_data, // {blink, underline, bg, fg, code}
output reg [3:0] red,
output reg [3:0] green,
output reg [3:0] blue,
output reg hsync,
output reg vsync,
output reg frame_tick // one clock at the start of vertical blanking
);
localparam H_VIS = 640, H_FP = 16, H_SYNC = 96, H_TOTAL = 800;
localparam V_VIS = 480, V_FP = 10, V_SYNC = 2, V_TOTAL = 525;
reg [1:0] div = 2'd0;
reg [9:0] h, v;
reg [5:0] frame = 6'd0;
wire tick = div == 2'd3;
always @(posedge clk) begin
div <= rst ? 2'd0 : div + 1'b1;
frame_tick <= tick && h == 0 && v == V_VIS;
if (rst) begin
h <= 0;
v <= 0;
end else if (tick) begin
if (h == H_TOTAL - 1) begin
h <= 0;
if (v == V_TOTAL - 1) begin
v <= 0;
frame <= frame + 1'b1;
end else
v <= v + 1'b1;
end else
h <= h + 1'b1;
end
end
// which buffer row and glyph line this scanline shows
wire in_region = sm_on && v < V_VIS && v[8:4] >= stop && v[8:4] <= sbot;
wire [9:0] ys = !in_region ? v : sm_dir ? v - sm_off : v + sm_off;
wire gap = in_region && (sm_dir ? v < {stop, 4'b0} + sm_off : ys[9:4] > sbot);
reg [11:0] row_base;
reg [4:0] srow;
reg [3:0] line;
reg blank_line;
always @(posedge clk) begin
srow <= ys[8:4];
row_base <= {1'b0, ys[8:4], 6'b0} + {3'b0, ys[8:4], 4'b0};
line <= ys[3:0];
blank_line <= gap;
end
wire [6:0] tcol = h[9:3];
assign ram_addr = row_base + {5'b0, tcol};
reg [7:0] font [0:4095];
reg [7:0] glyph;
initial $readmemh(FONT_FILE, font);
always @(posedge clk) glyph <= font[{ram_data[7:0], line}];
function [11:0] palette(input [3:0] n);
case (n)
4'd0: palette = 12'h000; 4'd1: palette = 12'hA00;
4'd2: palette = 12'h0A0; 4'd3: palette = 12'hA50;
4'd4: palette = 12'h00A; 4'd5: palette = 12'hA0A;
4'd6: palette = 12'h0AA; 4'd7: palette = 12'hAAA;
4'd8: palette = 12'h555; 4'd9: palette = 12'hF55;
4'd10: palette = 12'h5F5; 4'd11: palette = 12'hFF5;
4'd12: palette = 12'h55F; 4'd13: palette = 12'hF5F;
4'd14: palette = 12'h5FF; 4'd15: palette = 12'hFFF;
endcase
endfunction
wire visible = h < H_VIS && v < V_VIS;
wire cursor = cur_vis && frame[5] && tcol == cur_col && srow == cur_row;
wire ink = (glyph[~h[2:0]] | (ram_data[16] && line == 4'd15)) && !(ram_data[17] && frame[5]);
wire [3:0] idx = blank_line ? 4'd0 : (ink ^ cursor) ? ram_data[11:8] : ram_data[15:12];
always @(posedge clk) begin
if (tick) begin
{red, green, blue} <= visible ? palette(idx) : 12'h000;
hsync <= !(h >= H_VIS + H_FP && h < H_VIS + H_FP + H_SYNC);
vsync <= !(v >= V_VIS + V_FP && v < V_VIS + V_FP + V_SYNC);
end
end
endmodule
// 2400-cell character buffer: read/write port for the terminal engine,
// read-only port for video.
module char_ram (
input clk,
input [11:0] a_addr,
input a_we,
input [17:0] a_wdata,
output reg [17:0] a_rdata,
input [11:0] b_addr,
output reg [17:0] b_rdata
);
(* ram_style = "block" *) reg [17:0] mem [0:4095];
integer i;
initial for (i = 0; i < 4096; i = i + 1) mem[i] = {10'h00A, 8'h20};
always @(posedge clk) begin
if (a_we) mem[a_addr] <= a_wdata;
a_rdata <= mem[a_addr];
end
always @(posedge clk) b_rdata <= mem[b_addr];
endmodule