rtl/sprites.v draws sprites between the graphics and the text from a pair of line buffers filled one graphics line ahead. Ports 70h-77h set position, shape, flip and on/off and load shapes; 78h is a frame counter; 80h-87h report sprite-sprite and sprite-graphics collisions per frame. The graphics fill (CLS) turns sprites off unless bit 7 is set. Testbench checks pixels, priority, flip, edge clipping, collisions and CLS. Manual, README and basic/examples/sprites.bas demo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
196 lines
7.8 KiB
Verilog
196 lines
7.8 KiB
Verilog
`timescale 1ns / 1ps
|
|
|
|
// 1280x1024 @ 60 Hz VGA text display (108 MHz pixel clock), two layouts:
|
|
// 80 columns: 80x32 cells of 16x32 pixels (the 8x16 font doubled)
|
|
// 132 columns: 132x64 cells of 8x16 pixels, centred (112-pixel side borders)
|
|
// 16 colours, underline, blink, block cursor and smooth-scroll offset for the
|
|
// scroll region. The character buffer has a 132-cell stride in both layouts.
|
|
// Hardware sprites (sprites.v) sit between the graphics and the text.
|
|
// A 320x240 graphics layer (each pixel 4x4, the top 960 lines, so 80-column
|
|
// text row r still starts at graphics line 8(r-1)) shows through wherever the
|
|
// text is background; colour 0 is transparent.
|
|
// One pixel per clock, pipelined:
|
|
// clock 0 position -> row, column, glyph line (registered)
|
|
// clock 1 character RAM and graphics reads
|
|
// clock 2 font ROM read
|
|
// clock 3 colour out
|
|
module text_display #(
|
|
parameter FONT_FILE = "font.mem"
|
|
) (
|
|
input clk, // pixel clock, 108 MHz
|
|
input rst,
|
|
input wide, // 132-column layout
|
|
input [5:0] cur_row,
|
|
input [7:0] cur_col,
|
|
input cur_vis,
|
|
input [5:0] stop, // scroll region (rows, inclusive)
|
|
input [5:0] sbot,
|
|
input sm_on, // smooth scroll in progress
|
|
input sm_dir, // 0 = up, 1 = down
|
|
input [4:0] sm_off, // font lines the region has moved
|
|
output [13:0] ram_addr, // row * 132 + column
|
|
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,
|
|
// sprites: the line to prepare next, and the pixel on screen
|
|
output reg sp_go,
|
|
output reg [7:0] sp_line,
|
|
output sp_buf,
|
|
output [8:0] sp_x,
|
|
output sp_on,
|
|
input [3:0] sp_pix, // read a clock after sp_x, like 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 = 1280, H_FP = 48, H_SYNC = 112, H_TOTAL = 1688;
|
|
localparam V_VIS = 1024, V_FP = 1, V_SYNC = 3, V_TOTAL = 1066;
|
|
localparam WIDE_X0 = 112; // 132 x 8 = 1056 pixels, centred
|
|
|
|
reg [10:0] h, v;
|
|
reg [5:0] frame = 6'd0;
|
|
|
|
always @(posedge clk) begin
|
|
frame_tick <= h == 0 && v == V_VIS;
|
|
// each graphics line is 4 scan lines; ask for the next one as it starts
|
|
sp_go <= h == 0 && v[1:0] == 2'd0 && (v < 11'd956 || v == 11'd1064);
|
|
sp_line <= v == 11'd1064 ? 8'd0 : v[9:2] + 1'b1;
|
|
if (rst) begin
|
|
h <= 0;
|
|
v <= 0;
|
|
end else 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
|
|
|
|
// ---- clock 0: where this pixel is in the text, in font units
|
|
// yl counts font lines (a 16-line font: 32 pixels a row in 80 columns)
|
|
wire [9:0] yl = wide ? v[9:0] : {1'b0, v[9:1]};
|
|
wire [10:0] xw = h - WIDE_X0;
|
|
wire [10:0] xl = wide ? xw : {1'b0, h[10:1]}; // font pixels across
|
|
wire text_on = wide ? h >= WIDE_X0 && h < WIDE_X0 + 1056 : 1'b1;
|
|
wire in_region = sm_on && v < V_VIS && yl[9:4] >= stop && yl[9:4] <= sbot;
|
|
wire [9:0] ys = !in_region ? yl : sm_dir ? yl - sm_off : yl + sm_off;
|
|
wire gap = in_region && (sm_dir ? yl < {stop, 4'b0} + sm_off : ys[9:4] > sbot);
|
|
wire [7:0] tcol = xl[10:3];
|
|
wire [5:0] trow = ys[9:4];
|
|
|
|
reg [13:0] a_base; // row * 132
|
|
reg [7:0] a_col;
|
|
reg [3:0] a_line;
|
|
reg [2:0] a_bit;
|
|
reg a_blank, a_text, a_vis, a_cur, a_hs, a_vs, a_gon;
|
|
reg [16:0] a_gfx;
|
|
reg [8:0] a_spx;
|
|
reg a_spbuf;
|
|
always @(posedge clk) begin
|
|
a_base <= {trow, 7'd0} + {6'd0, trow, 2'd0};
|
|
a_col <= tcol;
|
|
a_line <= ys[3:0];
|
|
a_bit <= xl[2:0];
|
|
a_blank <= gap;
|
|
a_text <= text_on;
|
|
a_vis <= h < H_VIS && v < V_VIS;
|
|
a_cur <= cur_vis && frame[5] && tcol == cur_col && ys[9:4] == cur_row && !gap;
|
|
a_hs <= h >= H_VIS + H_FP && h < H_VIS + H_FP + H_SYNC; // positive sync
|
|
a_vs <= v >= V_VIS + V_FP && v < V_VIS + V_FP + V_SYNC;
|
|
a_gon <= v < 960;
|
|
a_gfx <= {v[9:2], 8'd0} + {2'd0, v[9:2], 6'd0} + {8'd0, h[10:2]};
|
|
a_spx <= h[10:2];
|
|
a_spbuf <= v[2];
|
|
end
|
|
|
|
// ---- clock 1: character RAM and graphics reads
|
|
assign ram_addr = a_base + {6'd0, a_col};
|
|
assign gfx_addr = a_gfx;
|
|
assign sp_x = a_spx;
|
|
assign sp_buf = a_spbuf;
|
|
assign sp_on = a_gon;
|
|
reg [3:0] b_line;
|
|
reg [2:0] b_bit;
|
|
reg b_blank, b_text, b_vis, b_cur, b_hs, b_vs, b_gon;
|
|
always @(posedge clk)
|
|
{b_line, b_bit, b_blank, b_text, b_vis, b_cur, b_hs, b_vs, b_gon} <=
|
|
{a_line, a_bit, a_blank, a_text, a_vis, a_cur, a_hs, a_vs, a_gon};
|
|
|
|
// ---- clock 2: font ROM
|
|
reg [7:0] font [0:4095];
|
|
initial $readmemh(FONT_FILE, font);
|
|
reg [7:0] glyph;
|
|
reg [17:0] c_cell;
|
|
reg [3:0] c_gfx, c_spr, c_line;
|
|
reg [2:0] c_bit;
|
|
reg c_blank, c_text, c_vis, c_cur, c_hs, c_vs;
|
|
always @(posedge clk) begin
|
|
glyph <= font[{ram_data[7:0], b_line}];
|
|
c_cell <= ram_data;
|
|
c_gfx <= b_gon ? gfx_pix : 4'd0;
|
|
c_spr <= sp_pix;
|
|
{c_line, c_bit, c_blank, c_text, c_vis, c_cur, c_hs, c_vs} <=
|
|
{b_line, b_bit, b_blank, b_text, b_vis, b_cur, b_hs, b_vs};
|
|
end
|
|
|
|
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
|
|
|
|
// ---- clock 3: colour
|
|
wire ink = (glyph[~c_bit] | (c_cell[16] && c_line == 4'd15)) && !(c_cell[17] && frame[5]);
|
|
wire fg = c_text && !c_blank && (ink ^ c_cur);
|
|
wire [3:0] idx = fg ? c_cell[11:8] : c_spr != 4'd0 ? c_spr : c_gfx != 4'd0 ? c_gfx :
|
|
c_blank || !c_text ? 4'd0 : c_cell[15:12];
|
|
always @(posedge clk) begin
|
|
{red, green, blue} <= c_vis ? palette(idx) : 12'h000;
|
|
hsync <= c_hs;
|
|
vsync <= c_vs;
|
|
end
|
|
endmodule
|
|
|
|
// Two character buffers of 132x64 cells (the normal and the alternate screen,
|
|
// picked by a_alt/b_alt): read/write port for the terminal engine, read-only
|
|
// port for video on the pixel clock.
|
|
module char_ram (
|
|
input clk,
|
|
input a_alt,
|
|
input [13:0] a_addr,
|
|
input a_we,
|
|
input [17:0] a_wdata,
|
|
output reg [17:0] a_rdata,
|
|
input vclk,
|
|
input b_alt,
|
|
input [13:0] b_addr,
|
|
output reg [17:0] b_rdata
|
|
);
|
|
localparam PAGE = 132 * 64;
|
|
(* ram_style = "block" *) reg [17:0] mem [0:2 * PAGE - 1];
|
|
integer i;
|
|
initial for (i = 0; i < 2 * PAGE; i = i + 1) mem[i] = {10'h00A, 8'h20};
|
|
|
|
wire [14:0] aa = {1'b0, a_addr} + (a_alt ? PAGE : 0);
|
|
wire [14:0] ba = {1'b0, b_addr} + (b_alt ? PAGE : 0);
|
|
always @(posedge clk) begin
|
|
if (a_we) mem[aa] <= a_wdata;
|
|
a_rdata <= mem[aa];
|
|
end
|
|
|
|
always @(posedge vclk) b_rdata <= mem[ba];
|
|
endmodule
|