Files
Basys3Terminal/rtl/text_display.v
T
brad ArantandClaude Opus 5.5 634e314010 Machine-code monitor, fast serial, memory files, Ctrl-Alt-Del
Monitor: the CPU gets an NMI (taken at an instruction start, or from
HALT) raised by the Pause key, BASIC's MONITOR, two hardware breakpoint
comparators or a step counter. It enters through a trampoline at 0066h
(the same bytes in BASIC's ROM and the monitor's, basic/tramp.s) that
switches in a 32 KB monitor bank over 0000-7FFF. The monitor (monitor/,
C) runs on the terminal's new alternate screen (CSI ?47 h/l, a second
screen page; graphics hidden) and shows registers, flags, a disassembly
from PC, the stack and memory. Commands: Enter/S n step, O step over,
G/X go, B breakpoint, D/M windows, E edit, R registers, and SAVE/LOAD/
DIR for memory files. The disassembler covers every opcode;
monitor/test-disasm.py round-trips all forms through sdasz80. Lockstep
CPU tests still match the reference emulator.

BASIC: MONITOR, CALL addr, BSAVE "N",start,len and BLOAD "N"[,addr]
(memory files: load address then bytes). The DATE clock moves to
F3F8h so the monitor can date files. F000-F3F7 is kept free for
machine code (program area 21076 bytes).

Serial: SW1:0 = 10 is now 921600 and 11 is 3 Mbaud. A 17 KB burst to
BASIC lists back identical at both on the board. send-bas.py reads
the echo while sending; unread, it could stall the PC's flow control.

Ctrl-Alt-Del in Z-80 mode restarts the Z-80 like BTND.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 11:17:54 -08:00

141 lines
5.1 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
// Two 2400-cell character buffers (the normal and the alternate screen; the
// top address bit picks one): read/write port for the terminal engine,
// read-only port for video.
module char_ram (
input clk,
input [12:0] a_addr,
input a_we,
input [17:0] a_wdata,
output reg [17:0] a_rdata,
input [12:0] b_addr,
output reg [17:0] b_rdata
);
(* ram_style = "block" *) reg [17:0] mem [0:8191];
integer i;
initial for (i = 0; i < 8192; 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