Files
Basys3Terminal/rtl/text_display.v
T
brad ArantandClaude Opus 5.5 dc7c7c22c5 BASIC graphics: 320x240 16-colour layer, manual and demo
- z80_system: dual-clock graphics RAM behind ports 30h-34h (X/Y, plot
  with auto-increment, read-back, hardware fill); text_display overlays it
  under the text, colour 0 transparent.
- BASIC: CLS COLOR PLOT LINE RECT FILL CIRCLE POINT, LOCATE and INK;
  faster keyword tokenizing.
- docs/basic-manual.md (+ PDF via docs/build-manual.sh).
- basic/examples/graphics-demo.bas and tools/send-bas.py to load programs
  over the serial port.
- Simulation checks drawing and saves a frame; the emulator models the
  graphics and timer ports and can stop when BASIC waits for input.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 21:09:43 -08:00

140 lines
5.0 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.
// A 320x240 graphics layer (each pixel 2x2) shows through wherever the text is
// background; graphics colour 0 is transparent.
// 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 [16:0] gfx_addr, // graphics pixel, read a clock later
input [3:0] gfx_pix,
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};
assign gfx_addr = {v[8:1], 8'd0} + {2'd0, v[8:1], 6'd0} + {8'd0, h[9:1]};
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 fg = !blank_line && (ink ^ cursor);
wire [3:0] idx = fg ? ram_data[11:8] : gfx_pix != 4'd0 ? gfx_pix :
blank_line ? 4'd0 : 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