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

+30 -3
View File
@@ -223,9 +223,15 @@ edit the line being typed.
About 20.5 KB of program space is free; F000h-F3F7h is kept free for machine code.
**Graphics:** the layer is 320×240 pixels in the 16 terminal colours, each
pixel a 2×2 block, with (0,0) at the top left. Colour 0 is transparent. Text is
pixel a 4×4 block, with (0,0) at the top left. Colour 0 is transparent. Text is
drawn on top, and the graphics show through blank text.
**Sprites:** 32 hardware sprites of 16×16 graphics pixels, from 64 shapes, in
front of the graphics and behind the text, with flipping and sprite-sprite and
sprite-graphics collision flags. BASIC moves them with `OUT` (ports 112–119)
and reads the frame counter and collisions with `INP` (120, 128–135); see the
manual. `basic/examples/sprites.bas` is a small example.
**Manual:** [docs/basic-manual.md](docs/basic-manual.md) describes every
statement and function, with examples, error messages and limits.
`docs/build-manual.sh` rebuilds the PDF from it.
@@ -358,10 +364,20 @@ tools/gen-asmtab.py # regenerate monitor/asmtab.h after changing the d
and jumps to 0066h. It also wakes the CPU from HALT.
The 320×240×4-bit graphics layer is a separate dual-clock block RAM: the Z-80
writes it on its own clock, and the video reads it on the 100 MHz clock.
writes it on its own clock, and the video reads it on the 108 MHz pixel clock.
`rtl/text_display.v` overlays it on the text. A hardware fill clears it in
76,800 clocks.
`rtl/sprites.v` draws the sprites. The video asks for each graphics line one
line ahead; the engine clears a 320-pixel line buffer, then walks the sprites
from 31 down to 0 writing their visible pixels (about 900 clocks of the 6,752
a graphics line lasts), while the video reads the other buffer. A pixel
written over another sprite's flags both; a sprite pixel shown over a graphics
pixel flags that sprite. The flags are collected per frame and handed to the
Z-80 at the start of vertical blank, along with a frame counter. Shapes are a
dual-clock block RAM (64 × 16 × 16 × 4 bits); positions and shapes are in
distributed RAM.
Memory is 64 KB of block RAM loaded with BASIC at configuration. Writes below
8000h are ignored, so the interpreter can't be damaged. BASIC's data and the
stack fill the top half.
@@ -374,13 +390,20 @@ stack fill the top half.
| 20h / 21h | | 7-segment display, right / left byte |
| 30h, 31h, 32h | | graphics X low byte, X bit 8, Y |
| 33h | colour at X,Y | plot at X,Y, then X+1 |
| 34h | bit 0: fill busy | fill the whole graphics layer with a colour |
| 34h | bit 0: fill busy | fill the whole graphics layer with a colour; also turns the sprites off unless bit 7 is set |
| 40h | bit 0: SPI byte busy, bit 1: command refused | 1 starts a flash command (chip select low), 0 ends it |
| 41h | byte received during the last transfer | send a byte to the flash |
| 60h | why the monitor was entered (1 Pause, 2 port 61h, 3 step, 4/5 breakpoint) | bit 0: monitor bank in; bit 1: stop after one instruction |
| 61h | | enter the monitor (NMI) |
| 62h, 63h, 64h | 64h: byte of main memory at the debug address, then address+1 | 62h/63h: debug address |
| 65h/66h, 68h/69h, 67h | | breakpoint 0 and 1 addresses; 67h bits 1:0 turn them on |
| 70h | | select sprite 0–31 |
| 71h, 72h, 73h | | sprite X low byte, X bit 8, Y (writing Y moves it) |
| 74h, 75h | | sprite shape 0–63; bit 0 on, bit 1 flip X, bit 2 flip Y |
| 76h, 77h | | start loading shape n; two pixels (left in bits 7:4), then next |
| 78h | frame counter (+1 each vertical blank) | |
| 80h–83h | sprites that touched another sprite last frame (bit per sprite) | |
| 84h–87h | sprites that touched a graphics pixel last frame | |
BASIC is C (`basic/basic.c`) compiled with SDCC (`sudo apt install sdcc`):
@@ -455,6 +478,9 @@ older boards, Macronix `mx25l3273f` on newer ones).
`JP` onto its target and sets a breakpoint that BASIC then hits. It also
enters through the Pause key and checks BASIC's screen comes back.
- It draws graphics from BASIC and checks the pixels.
- It defines sprite shapes and places overlapping, flipped and edge-clipped
sprites from BASIC, then checks the pixels on screen, the collision ports and
that `CLS` turns the sprites off.
- Against a model flash (`sim/spiflash_model.v`) it sets the date, saves,
loads, runs and lists a program (checking the date in `DIR`), tries to erase
address 0 and checks the model saw no write below 240000h, then writes a data
@@ -474,6 +500,7 @@ which Vivado needs on newer distros.
| `rtl/basys3_terminal.v` | Top level: reset, switches, FIFOs, flow control, banner, LEDs, Z-80 clock |
| `rtl/z80_cpu.v` | Z-80 CPU |
| `rtl/z80_system.v` | Z-80 RAM, console, timer, graphics and flash ports, monitor bank, step and breakpoints |
| `rtl/sprites.v` | Hardware sprites: shapes, line buffers, collisions |
| `rtl/flash_spi.v` | SPI port to the configuration flash, with bitstream write protection |
| `monitor/` | Machine-code monitor (C and start-up code), its build script and disassembler test |
| `basic/tramp.s` | The NMI entry and exit bytes shared by BASIC and the monitor |
+54
View File
@@ -0,0 +1,54 @@
10 REM SPRITE DEMO FOR THE BASYS 3 Z-80
20 REM W A S D MOVE THE ROBOT, Q QUITS. SIX BALLS BOUNCE AROUND;
30 REM THE BORDER CHANGES COLOUR WHILE THE ROBOT TOUCHES ONE.
40 DEF FNH(A)=A-48-7*INT(A/65)
50 RANDOMIZE
100 CLS: INK 15: PRINT "SPRITES W A S D MOVE Q QUITS"
110 RECT 0,16,319,239,8
120 FOR S=0 TO 1: GOSUB 8000: NEXT S
200 N=6: DIM X(6),Y(6),U(6),V(6)
210 FOR I=1 TO N
220 X(I)=20+INT(RND(1)*270): Y(I)=30+INT(RND(1)*180)
230 U(I)=2+INT(RND(1)*3): V(I)=1+INT(RND(1)*3)
240 OUT 112,I: OUT 116,1: OUT 117,1
250 NEXT I
260 P=152: Q=120: OUT 112,0: OUT 116,0: OUT 117,1
300 F=INP(120)
310 IF INP(0) AND 1 THEN GOSUB 7000
320 FOR I=1 TO N
330 X(I)=X(I)+U(I): IF X(I)<2 OR X(I)>301 THEN U(I)=-U(I): X(I)=X(I)+2*U(I)
340 Y(I)=Y(I)+V(I): IF Y(I)<18 OR Y(I)>221 THEN V(I)=-V(I): Y(I)=Y(I)+2*V(I)
350 OUT 112,I: OUT 113,X(I) AND 255: OUT 114,INT(X(I)/256): OUT 115,Y(I)
360 NEXT I
370 OUT 112,0: OUT 113,P AND 255: OUT 114,INT(P/256): OUT 115,Q
380 IF INP(128) AND 1 THEN H=H+1: RECT 0,16,319,239,1+(H AND 7)
390 IF INP(120)=F THEN 390
400 GOTO 300
7000 K=INP(1): IF K>96 THEN K=K-32
7010 IF K=87 AND Q>18 THEN Q=Q-4
7020 IF K=83 AND Q<220 THEN Q=Q+4
7030 IF K=65 AND P>2 THEN P=P-4: OUT 112,0: OUT 117,3
7040 IF K=68 AND P<300 THEN P=P+4: OUT 112,0: OUT 117,1
7050 IF K=81 THEN CLS: END
7060 RETURN
8000 REM LOAD SHAPE S FROM 16 ROWS OF 16 HEX DIGITS (0 = SEE-THROUGH)
8010 OUT 118,S
8020 FOR R=1 TO 16: READ A$
8030 FOR C=1 TO 15 STEP 2
8040 OUT 119,16*FNH(ASC(MID$(A$,C,1)))+FNH(ASC(MID$(A$,C+1,1)))
8050 NEXT C: NEXT R
8060 RETURN
9000 REM SHAPE 0: THE ROBOT
9010 DATA "00000BBBBBB00000","0000BBBBBBBB0000","000BB99BB99BB000"
9020 DATA "000BB99BB99BB000","000BBBBBBBBBB000","000BBB0000BBB000"
9030 DATA "0000BBBBBBBB0000","0000007777000000","0077777777777700"
9040 DATA "0770777777770770","0770777777770770","0770777777770770"
9050 DATA "0000777777770000","0000770000770000","0000770000770000"
9060 DATA "000FFF0000FFF000"
9100 REM SHAPE 1: A BALL
9110 DATA "00000EEEEEE00000","000EEEEEEEEEE000","00EEFFEEEEEEEE00"
9120 DATA "0EEFFFEEEEEEEEE0","0EEFFEEEEEEEEEE0","EEEEEEEEEEEEEEEE"
9130 DATA "EEEEEEEEEEEEEEEE","EEEEEEEEEEEEEEEE","EEEEEEEEEEEEEEEE"
9140 DATA "EEEEEEEEEEEEEEEE","0EEEEEEEEEEEEE60","0EEEEEEEEEEEE660"
9150 DATA "00EEEEEEEEEE6600","000EEEEEEE666000","0000066666600000"
9160 DATA "0000000000000000"
+69 -1
View File
@@ -441,6 +441,72 @@ place. Use `TIM` to set the pace:
70 GOTO 20
```
### Sprites
Sprites are small pictures that the hardware draws over the graphics wherever
you put them. To move one, give it a new position: nothing needs erasing or
redrawing, nothing flickers, and the Z-80 does no drawing work. They are what
moving things in a game should be made of.
- **32 sprites**, numbered 0–31, each 16 × 16 graphics pixels.
- **64 shapes**, numbered 0–63, that the sprites show. Several sprites can
show the same shape.
- **Colours:** each pixel of a shape is one of the 16 colours; colour 0 is
see-through.
- **Layers:** sprites are in front of the graphics and behind the text. Where
sprites overlap, the lower number is in front.
- **Collisions:** the hardware notices when a sprite touches another sprite,
or touches a graphics pixel that isn't colour 0.
BASIC drives them with `OUT` and `INP`:
| Port | `OUT` | `INP` |
|---|---|---|
| 112 | Pick sprite *n* (0–31) for ports 113–117 | — |
| 113, 114 | X: `X AND 255`, then `INT(X / 256)` | — |
| 115 | Y; the sprite moves to the new X and Y together | — |
| 116 | Shape (0–63) | — |
| 117 | 1 = show, 0 = hide; add 2 to flip left-right, 4 to flip upside down | — |
| 118 | Start defining shape *n* (0–63) | — |
| 119 | The next two pixels of the shape: 16 × left colour + right colour | — |
| 120 | — | Frame counter: goes up by one 60 times a second, as each picture is finished |
| 128–131 | — | Sprites that touched another sprite in the last frame: 128 has a bit for sprites 0–7 (1, 2, 4 … 128), 129 for 8–15, 130 for 16–23, 131 for 24–31 |
| 132–135 | — | The same for sprites that touched the graphics |
**Defining a shape.** `OUT 118, n` and then 128 `OUT 119`s: the 16 rows from
the top, each as 8 bytes of two pixels. A shape stays defined until the power
goes off, even through `NEW`, so define it once.
**Placing a sprite.** X and Y are its top-left pixel, the same as for `PLOT`.
X can go from 0 to 511 and Y from 0 to 255; values past the edge wrap round, so
X = 500 puts the sprite 12 pixels off the left edge, half showing.
```
10 CLS
20 OUT 118, 0: FOR I = 1 TO 128: OUT 119, 204: NEXT I : REM SHAPE 0: COLOUR 12
30 OUT 112, 0: OUT 116, 0: OUT 117, 1 : REM SPRITE 0 SHOWS IT
40 FOR X = 0 TO 304
50 OUT 112, 0: OUT 113, X AND 255: OUT 114, INT(X / 256): OUT 115, 100
60 F = INP(120)
70 IF INP(120) = F THEN 70 : REM WAIT FOR THE NEXT FRAME
80 NEXT X
```
Waiting for the frame counter to change, as in lines 60–70, keeps movement
smooth: one step per frame, 60 a second.
**Collisions.** `IF INP(128) AND 1 THEN …` asks whether sprite 0 touched
another sprite in the last frame; `INP(132) AND 1` whether it touched the
graphics, such as a wall drawn with `FILL`. A collision only counts where
pixels that aren't colour 0 overlap, so the see-through corners of a round
shape don't count.
**Turning them off.** `CLS` hides every sprite (the shapes are kept), and so
does restarting the Z-80. `OUT 117, 0` hides the picked one.
`basic/examples/sprites.bas` is a complete example: a robot steered with
W A S D among bouncing balls, with shapes written as rows of hex digits.
### Speed
`CLS` clears the graphics in under a millisecond, and a line right across the
@@ -502,12 +568,13 @@ kept free for it.
| 33 | — | left two digits of the 7-segment display |
| 48, 49, 50 | — | graphics X (low byte, high bit), Y |
| 51 | colour at X, Y | plot a pixel at X, Y and move right |
| 52 | bit 0: screen fill in progress | fill the whole graphics layer with a colour |
| 52 | bit 0: screen fill in progress | fill the whole graphics layer with a colour; also hides the sprites, unless 128 is added |
| 64 | bit 0: flash byte in progress; bit 1: last flash command refused | 1 selects the flash and starts a command, 0 ends it |
| 65 | the byte the flash sent back during the last `OUT 65` | a byte to the flash |
| 96 | why the monitor was entered (used by the monitor) | monitor bank and single step (used by the monitor) |
| 97 | — | enter the monitor (what `MONITOR` does) |
| 98–105 | memory read and breakpoint registers (used by the monitor) | |
| 112–120, 128–135 | sprites (section 7) | sprites (section 7) |
```
10 FOR I = 0 TO 255
@@ -984,6 +1051,7 @@ bouncing ball and a Mandelbrot set, then prints how long it all took.
| User functions | FNA–FNZ, one argument each |
| FOR nesting / GOSUB nesting | 16 / 32 |
| Graphics | 320 × 240, 16 colours |
| Sprites | 32 on screen, 64 shapes, 16 × 16 pixels each |
| Text | 80 × 32, or 132 × 64 |
| Files in flash | 64, 1,784 KB in all, 32 KB each |
| Open data files | 3 |
Binary file not shown.
+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
+39
View File
@@ -359,6 +359,45 @@ module tb_terminal;
end
capture_frame("frame_3.ppm");
// ---- sprites: shape 0 solid colour 9, shape 1 solid 2, shape 2 left
// half 14; sprite 0 over sprite 1, sprite 2 flipped at the right edge
// over a filled strip
uart_str({"CLS:OUT 118,0:FOR I=1 TO 128:OUT 119,153:NEXT I:",
"OUT 118,1:FOR I=1 TO 128:OUT 119,34:NEXT I\015"});
uart_str({"OUT 118,2:FOR I=1 TO 16:FOR J=1 TO 4:OUT 119,238:NEXT J:",
"FOR J=1 TO 4:OUT 119,0:NEXT J:NEXT I\015"});
uart_str({"OUT 112,0:OUT 113,100:OUT 114,0:OUT 115,150:OUT 116,0:OUT 117,1:",
"OUT 112,1:OUT 113,108:OUT 114,0:OUT 115,158:OUT 116,1:OUT 117,1\015"});
uart_str({"FILL 0,200,319,239,4:OUT 112,2:OUT 113,54:OUT 114,1:OUT 115,230:",
"OUT 116,2:OUT 117,3\015"});
#40_000_000;
@(posedge vs); @(posedge vs); @(posedge vs);
uart_str("LOCATE 30,1:PRINT INP(128);INP(129);INP(132)\015");
wait_row(29, " 3 0 4");
capture_frame("frame_5.ppm");
// graphics pixel (x, y) is screen pixel (4x+2, 4y+2)
if (fb[(4*151+2)*1280 + 4*101+2] == 12'hF55 && // sprite 0
fb[(4*162+2)*1280 + 4*112+2] == 12'hF55 && // sprite 0 over sprite 1
fb[(4*170+2)*1280 + 4*120+2] == 12'h0A0 && // sprite 1
fb[(4*150+2)*1280 + 4*99+2] == 12'h000 && // beside them
fb[(4*235+2)*1280 + 4*312+2] == 12'h00A && // flipped: clear half
fb[(4*235+2)*1280 + 4*318+2] == 12'h5FF) // flipped: coloured half
$display("PASS sprites on screen");
else begin
$display("FAIL sprites on screen: %h %h %h %h %h %h",
fb[(4*151+2)*1280 + 4*101+2], fb[(4*162+2)*1280 + 4*112+2],
fb[(4*170+2)*1280 + 4*120+2], fb[(4*150+2)*1280 + 4*99+2],
fb[(4*235+2)*1280 + 4*312+2], fb[(4*235+2)*1280 + 4*318+2]);
errors = errors + 1;
end
uart_str("CLS\015");
#5_000_000;
if (dut.z80.spr.en == 32'd0) $display("PASS CLS turns sprites off");
else begin
$display("FAIL CLS left sprites on: %h", dut.z80.spr.en);
errors = errors + 1;
end
// ---- files in the flash, and the bitstream protection
uart_str({"CLS:DATE \"2026-11-28 09:30\":NEW\01510 PRINT \"SAVED\";6*7\015SAVE \"TEST1\"\015NEW\015",
"LOAD \"TEST1\"\015RUN\015DIR\015"});