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:
1 parent
c78073320b
commit
13373244e8
9 files changed
+480
-7
No files matched your search
+237
@@ -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
|
||||
Reference in new issue
Block a user