Hardware sprites: 32 x 16x16 from 64 shapes, collisions, frame counter

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>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-29 08:36:41 -08:00
1 parent c78073320b
commit 13373244e8
9 files changed
+480 -7

No files matched your search

+9 -1
View File
@@ -319,6 +319,10 @@ module basys3_terminal #(
wire fl_sck;
wire [16:0] gfx_addr;
wire [3:0] gfx_pix;
wire sp_go, sp_buf, sp_on, pix_frame;
wire [7:0] sp_line;
wire [8:0] sp_x;
wire [3:0] sp_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.
@@ -333,6 +337,8 @@ module basys3_terminal #(
.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),
.sp_go(sp_go), .sp_line(sp_line), .sp_buf(sp_buf), .sp_x(sp_x), .sp_on(sp_on),
.sp_pix(sp_pix), .sp_ftick(pix_frame),
.fl_cs_n(QspiCSn), .fl_sck(fl_sck), .fl_mosi(QspiDB[0]), .fl_miso(QspiDB[1])
);
@@ -410,7 +416,7 @@ module basys3_terminal #(
wire cur_vis, sm_on, sm_dir;
wire [3:0] dec_leds;
wire bell;
wire frame_tick, pix_frame;
wire frame_tick;
// start of vertical blanking, from the pixel clock to this one
reg frame_t = 1'b0;
@@ -448,6 +454,8 @@ module basys3_terminal #(
.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),
.sp_go(sp_go), .sp_line(sp_line), .sp_buf(sp_buf), .sp_x(sp_x), .sp_on(sp_on),
.sp_pix(alt_scr ? 4'd0 : sp_pix),
.red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync),
.frame_tick(pix_frame)
);
+237
View File
@@ -0,0 +1,237 @@
`timescale 1ns / 1ps
// Hardware sprites over the 320x240 graphics layer.
//
// 32 sprites of 16x16 graphics pixels in the 16 colours (0 = transparent),
// drawn from 64 shapes. Sprite 0 is on top of sprite 1 and so on; all sprites
// are over the graphics and under the text. Each sprite has a position, a
// shape, X and Y flip and an on/off bit.
//
// Z-80 ports (low address byte):
// 70 out select a sprite, 0-31
// 71 out X bits 7:0 72 out X bit 8
// 73 out Y; moves the selected sprite to the X and Y given (both at once)
// 74 out shape, 0-63
// 75 out bit 0 = on, bit 1 = flip left-right, bit 2 = flip upside down
// 76 out start loading shape n (0-63) at its top left
// 77 out two pixels of the shape, left one in bits 7:4, then move on;
// 128 bytes make a shape, a row of 16 pixels at a time
// 78 in frame counter, +1 at the start of each vertical blank
// 80-83 in sprites 0-7, 8-15, 16-23, 24-31 that touched another sprite
// in the last frame
// 84-87 in sprites that touched a graphics pixel (not colour 0)
// 34 out (the graphics fill) also turns every sprite off unless bit 7 is set
//
// X and Y are the top-left pixel and wrap at 512 and 256, so a sprite can go
// partly off the left or top edge: X = 500 is 12 pixels left of the screen.
//
// Drawing: the video asks for each graphics line one line ahead (go, line).
// The engine clears that line's buffer, then walks the sprites from 31 down
// to 0, writing each visible pixel, so lower numbers end up on top. A pixel
// written over another sprite's records a collision for both. The video reads
// the other buffer, one pixel per clock. A line takes at most about 900 of the
// 6,752 pixel clocks that a graphics line lasts.
module sprites (
// Z-80 side
input clk,
input rst,
input io_wr,
input [7:0] io_port,
input [7:0] io_wd,
output reg [7:0] io_q,
// video side (pixel clock)
input vclk,
input go, // prepare graphics line `line`
input [7:0] line,
input rd_buf, // buffer of the line on screen
input [8:0] rd_x, // graphics x on screen, registered
input rd_on, // inside the graphics area
output reg [3:0] pix, // sprite colour there, a clock later
input [3:0] gfx_pix, // graphics colour there, same clock as pix
input ftick // start of vertical blank
);
integer i;
// ------------------------------------------------------------ Z-80 side
reg [4:0] sel = 5'd0;
reg [8:0] x_stage = 9'd0;
reg [31:0] en = 32'd0;
reg [12:0] pat_a = 13'd0;
(* ram_style = "distributed" *) reg [16:0] pos [0:31]; // {y, x}
(* ram_style = "distributed" *) reg [7:0] shp [0:31]; // {flip y, flip x, shape}
(* ram_style = "block" *) reg [7:0] pat [0:8191]; // shape, row, pixel pair
initial begin
for (i = 0; i < 32; i = i + 1) begin pos[i] = 17'd0; shp[i] = 8'd0; end
for (i = 0; i < 8192; i = i + 1) pat[i] = 8'd0;
end
wire [7:0] shp_sel = shp[sel];
always @(posedge clk) begin
if (io_wr) case (io_port)
8'h73: pos[sel] <= {io_wd, x_stage};
8'h74: shp[sel] <= {shp_sel[7:6], io_wd[5:0]};
8'h75: shp[sel] <= {io_wd[2:1], shp_sel[5:0]};
default: ;
endcase
end
always @(posedge clk) if (io_wr && io_port == 8'h77) pat[pat_a] <= io_wd;
always @(posedge clk)
if (rst) begin
en <= 32'd0;
sel <= 5'd0;
x_stage <= 9'd0;
pat_a <= 13'd0;
end else if (io_wr) case (io_port)
8'h34: if (!io_wd[7]) en <= 32'd0;
8'h70: sel <= io_wd[4:0];
8'h71: x_stage[7:0] <= io_wd;
8'h72: x_stage[8] <= io_wd[0];
8'h75: en[sel] <= io_wd[0];
8'h76: pat_a <= {io_wd[5:0], 7'd0};
8'h77: pat_a <= pat_a + 1'b1;
default: ;
endcase
// ----------------------------------------------------------- line fill
// line buffers: {buffer, x} -> {colour, sprite}
(* ram_style = "distributed" *) reg [8:0] lb [0:1023];
initial for (i = 0; i < 1024; i = i + 1) lb[i] = 9'd0;
localparam S_IDLE = 2'd0, S_CLEAR = 2'd1, S_SPR = 2'd2;
reg [1:0] st = S_IDLE;
reg [8:0] cx;
reg wb;
reg [7:0] ln;
reg [4:0] s;
reg drawing = 1'b0;
reg [3:0] col;
reg [8:0] sx;
reg [3:0] srow;
reg sfx;
reg [5:0] sshape;
// the sprite being considered
wire [16:0] s_pos = pos[s];
wire [7:0] s_shp = shp[s];
wire [7:0] s_row = ln - s_pos[16:9];
wire s_vis = en[s] && s_row < 8'd16;
// pixel pipeline: address, pattern read, write
reg [12:0] pat_ra;
reg [7:0] pat_q;
reg p0_v = 1'b0, p1_v = 1'b0, p0_n, p1_n;
reg [8:0] p0_x, p1_x;
reg [4:0] p0_s, p1_s;
wire [3:0] cc = sfx ? ~col : col;
always @(posedge vclk) pat_q <= pat[pat_ra];
always @(posedge vclk) begin
p0_v <= 1'b0;
if (go) begin
st <= S_CLEAR;
cx <= 9'd0;
wb <= line[0];
ln <= line;
drawing <= 1'b0;
end else case (st)
S_CLEAR: begin
cx <= cx + 1'b1;
if (cx == 9'd319) begin
st <= S_SPR;
s <= 5'd31;
end
end
S_SPR:
if (!drawing) begin
if (s_vis) begin
drawing <= 1'b1;
col <= 4'd0;
sx <= s_pos[8:0];
srow <= s_shp[7] ? ~s_row[3:0] : s_row[3:0];
sfx <= s_shp[6];
sshape <= s_shp[5:0];
end else if (s == 5'd0)
st <= S_IDLE;
else
s <= s - 1'b1;
end else begin
pat_ra <= {sshape, srow, cc[3:1]};
p0_n <= cc[0];
p0_x <= sx + col;
p0_s <= s;
p0_v <= 1'b1;
col <= col + 1'b1;
if (col == 4'd15) begin
drawing <= 1'b0;
if (s == 5'd0) st <= S_IDLE;
else s <= s - 1'b1;
end
end
default: ;
endcase
{p1_v, p1_n, p1_x, p1_s} <= {p0_v, p0_n, p0_x, p0_s};
end
wire [3:0] w_col = p1_n ? pat_q[3:0] : pat_q[7:4];
wire w_spr = p1_v && w_col != 4'd0 && p1_x < 9'd320;
wire w_clr = st == S_CLEAR;
wire [9:0] wa = {wb, w_clr ? cx : p1_x};
wire [8:0] old = lb[wa];
always @(posedge vclk)
if (w_clr || w_spr) lb[wa] <= w_clr ? 9'd0 : {w_col, p1_s};
// ----------------------------------------------------------- video read
wire [8:0] rd = lb[{rd_buf, rd_x}];
reg [4:0] pix_id;
always @(posedge vclk) begin
pix <= rd_on ? rd[8:5] : 4'd0;
pix_id <= rd[4:0];
end
// ----------------------------------------------------------- collisions
reg [31:0] ss_acc = 32'd0, bg_acc = 32'd0, ss_frame = 32'd0, bg_frame = 32'd0;
reg ft = 1'b0;
always @(posedge vclk)
if (ftick) begin
ss_frame <= ss_acc;
bg_frame <= bg_acc;
ss_acc <= 32'd0;
bg_acc <= 32'd0;
ft <= ~ft;
end else begin
if (pix != 4'd0 && gfx_pix != 4'd0) bg_acc[pix_id] <= 1'b1;
if (w_spr && old[8:5] != 4'd0) begin
ss_acc[p1_s] <= 1'b1;
ss_acc[old[4:0]] <= 1'b1;
end
end
// A frame's results are steady for a whole frame after ft flips, so the
// Z-80 side copies them once it has seen the flip.
(* ASYNC_REG = "TRUE" *) reg [2:0] ft_s = 3'd0;
reg [7:0] frames = 8'd0;
reg [31:0] ss_cpu = 32'd0, bg_cpu = 32'd0;
always @(posedge clk) begin
ft_s <= {ft_s[1:0], ft};
if (ft_s[2] != ft_s[1]) begin
frames <= frames + 1'b1;
ss_cpu <= ss_frame;
bg_cpu <= bg_frame;
end
end
always @* case (io_port)
8'h78: io_q = frames;
8'h80: io_q = ss_cpu[7:0];
8'h81: io_q = ss_cpu[15:8];
8'h82: io_q = ss_cpu[23:16];
8'h83: io_q = ss_cpu[31:24];
8'h84: io_q = bg_cpu[7:0];
8'h85: io_q = bg_cpu[15:8];
8'h86: io_q = bg_cpu[23:16];
8'h87: io_q = bg_cpu[31:24];
default: io_q = 8'hFF;
endcase
endmodule
+21 -2
View File
@@ -5,6 +5,7 @@
// 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.
@@ -31,6 +32,13 @@ module text_display #(
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,
@@ -47,6 +55,9 @@ module text_display #(
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;
@@ -79,6 +90,8 @@ module text_display #(
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;
@@ -92,11 +105,16 @@ module text_display #(
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;
@@ -109,13 +127,14 @@ module text_display #(
initial $readmemh(FONT_FILE, font);
reg [7:0] glyph;
reg [17:0] c_cell;
reg [3:0] c_gfx, c_line;
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
@@ -136,7 +155,7 @@ module text_display #(
// ---- 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_gfx != 4'd0 ? c_gfx :
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;
+21
View File
@@ -12,6 +12,7 @@
// 30 out graphics X bits 7:0 31 out X bit 8 32 out Y
// 33 out plot colour at (X,Y), then X+1 in colour at (X,Y)
// 34 out fill the whole graphics screen in bit 0 = fill busy
// (also turns the sprites off unless bit 7 is set)
// 40 SPI flash control, 41 SPI flash data (see flash_spi.v)
// 60 out bit 0 = monitor bank in; bit 1 (with bit 0 = 0) = stop again after
// one instruction of the program
@@ -22,6 +23,7 @@
// 64 in main-memory byte at the debug address, then address + 1
// 65 out breakpoint 0 low 66 high 68 out breakpoint 1 low 69 high
// 67 out bit 0 = breakpoint 0 on, bit 1 = breakpoint 1 on
// 70-78, 80-87 sprites (see sprites.v)
//
// The monitor: an NMI (Pause key, port 61, a breakpoint or the step counter)
// pushes the program's PC and jumps to 0066h, where both BASIC and the monitor
@@ -56,6 +58,14 @@ module z80_system #(
input vclk,
input [16:0] gfx_vaddr,
output reg [3:0] gfx_vpix,
// sprites, on the video clock (see sprites.v)
input sp_go,
input [7:0] sp_line,
input sp_buf,
input [8:0] sp_x,
input sp_on,
output [3:0] sp_pix,
input sp_ftick,
// configuration flash (SPI)
output fl_cs_n,
output fl_sck,
@@ -145,6 +155,15 @@ module z80_system #(
end
end
// ------------------------------------------------------------- sprites
wire [7:0] spr_q;
sprites spr (
.clk(clk), .rst(rst),
.io_wr(io_wr), .io_port(io_addr[7:0]), .io_wd(io_wd), .io_q(spr_q),
.vclk(vclk), .go(sp_go), .line(sp_line), .rd_buf(sp_buf), .rd_x(sp_x),
.rd_on(sp_on), .pix(sp_pix), .gfx_pix(gfx_vpix), .ftick(sp_ftick)
);
// --------------------------------------------------------------- flash
wire [7:0] fl_ctl_q, fl_dat_q;
flash_spi flash (
@@ -235,6 +254,8 @@ module z80_system #(
8'h41: io_din = fl_dat_q;
8'h60: io_din = {5'd0, why};
8'h64: io_din = dbg_q;
8'h78, 8'h80, 8'h81, 8'h82, 8'h83, 8'h84, 8'h85, 8'h86, 8'h87:
io_din = spr_q;
default: io_din = 8'hFF;
endcase