2018-08-13 22:17:36 +00:00
|
|
|
|
2017-11-17 21:01:07 +00:00
|
|
|
`include "hvsync_generator.v"
|
2017-11-18 19:51:25 +00:00
|
|
|
`include "digits10.v"
|
2018-02-07 00:07:40 +00:00
|
|
|
`include "scoreboard.v"
|
2017-11-17 21:01:07 +00:00
|
|
|
|
2018-10-01 16:30:47 +00:00
|
|
|
/*
|
|
|
|
A brick-smashing ball-and-paddle game.
|
|
|
|
*/
|
|
|
|
|
2017-11-18 00:40:44 +00:00
|
|
|
module ball_paddle_top(clk, reset, hpaddle, hsync, vsync, rgb);
|
2017-11-17 21:01:07 +00:00
|
|
|
|
|
|
|
input clk;
|
2017-11-18 00:40:44 +00:00
|
|
|
input reset;
|
2017-11-17 21:01:07 +00:00
|
|
|
input hpaddle;
|
|
|
|
output hsync, vsync;
|
|
|
|
output [2:0] rgb;
|
|
|
|
wire display_on;
|
|
|
|
wire [8:0] hpos;
|
|
|
|
wire [8:0] vpos;
|
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
reg [8:0] paddle_pos; // paddle X position
|
2017-11-17 21:01:07 +00:00
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
reg [8:0] ball_x; // ball X position
|
|
|
|
reg [8:0] ball_y; // ball Y position
|
|
|
|
reg ball_dir_x; // ball X direction (0=left, 1=right)
|
|
|
|
reg ball_speed_x; // ball speed (0=1 pixel/frame, 1=2 pixels/frame)
|
|
|
|
reg ball_dir_y; // ball Y direction (0=up, 1=down)
|
2017-11-17 21:01:07 +00:00
|
|
|
|
2018-07-22 12:31:42 +00:00
|
|
|
reg brick_array [0:BRICKS_H*BRICKS_V-1]; // 16*8 = 128 bits
|
2017-11-18 19:51:25 +00:00
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
wire [3:0] score0; // score right digit
|
|
|
|
wire [3:0] score1; // score left digit
|
|
|
|
wire [3:0] lives; // # lives remaining
|
|
|
|
reg incscore; // incscore signal
|
2017-11-20 01:32:58 +00:00
|
|
|
reg declives = 0; // TODO
|
2017-11-18 00:40:44 +00:00
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
localparam BRICKS_H = 16; // # of bricks across
|
|
|
|
localparam BRICKS_V = 8; // # of bricks down
|
2017-11-18 00:40:44 +00:00
|
|
|
|
|
|
|
localparam BALL_DIR_LEFT = 0;
|
|
|
|
localparam BALL_DIR_RIGHT = 1;
|
|
|
|
localparam BALL_DIR_DOWN = 1;
|
|
|
|
localparam BALL_DIR_UP = 0;
|
2017-11-17 21:01:07 +00:00
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
localparam PADDLE_WIDTH = 31; // horizontal paddle size
|
|
|
|
localparam BALL_SIZE = 6; // square ball size
|
|
|
|
|
|
|
|
// video sync generator
|
2017-11-17 21:01:07 +00:00
|
|
|
hvsync_generator hvsync_gen(
|
|
|
|
.clk(clk),
|
2017-11-29 01:48:27 +00:00
|
|
|
.reset(reset),
|
2017-11-17 21:01:07 +00:00
|
|
|
.hsync(hsync),
|
|
|
|
.vsync(vsync),
|
|
|
|
.display_on(display_on),
|
|
|
|
.hpos(hpos),
|
|
|
|
.vpos(vpos)
|
|
|
|
);
|
2017-11-18 19:51:25 +00:00
|
|
|
|
|
|
|
// scoreboard
|
2018-07-31 18:41:27 +00:00
|
|
|
wire score_gfx; // output from score generator
|
2018-10-09 23:37:38 +00:00
|
|
|
player_stats stats(
|
|
|
|
.reset(reset),
|
|
|
|
.score0(score0),
|
|
|
|
.score1(score1),
|
|
|
|
.incscore(incscore),
|
|
|
|
.lives(lives),
|
|
|
|
.declives(declives)
|
|
|
|
);
|
|
|
|
|
2018-02-07 00:07:40 +00:00
|
|
|
scoreboard_generator score_gen(
|
2018-10-09 23:37:38 +00:00
|
|
|
.score0(score0),
|
|
|
|
.score1(score1),
|
|
|
|
.lives(lives),
|
|
|
|
.vpos(vpos),
|
|
|
|
.hpos(hpos),
|
|
|
|
.board_gfx(score_gfx)
|
|
|
|
);
|
2017-11-17 21:01:07 +00:00
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
wire [5:0] hcell = hpos[8:3]; // horizontal brick index
|
|
|
|
wire [5:0] vcell = vpos[8:3]; // vertical brick index
|
|
|
|
wire lr_border = hcell==0 || hcell==31; // along horizontal border?
|
2017-11-18 00:40:44 +00:00
|
|
|
|
2017-11-17 21:01:07 +00:00
|
|
|
// TODO: unsigned compare doesn't work in JS
|
|
|
|
wire [8:0] paddle_rel_x = ((hpos-paddle_pos) & 9'h1ff);
|
2018-07-31 18:41:27 +00:00
|
|
|
|
|
|
|
// player paddle graphics signal
|
2017-11-18 00:40:44 +00:00
|
|
|
wire paddle_gfx = (vcell == 28) && (paddle_rel_x < PADDLE_WIDTH);
|
2017-11-18 19:51:25 +00:00
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
// difference between ball position and video beam
|
|
|
|
wire [8:0] ball_rel_x = (hpos - ball_x);
|
|
|
|
wire [8:0] ball_rel_y = (vpos - ball_y);
|
|
|
|
|
|
|
|
// ball graphics signal
|
2017-11-17 21:01:07 +00:00
|
|
|
wire ball_gfx = ball_rel_x < BALL_SIZE
|
|
|
|
&& ball_rel_y < BALL_SIZE;
|
2017-11-18 19:51:25 +00:00
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
reg main_gfx; // main graphics signal (bricks and borders)
|
|
|
|
reg brick_present; // 1 when we are drawing a brick
|
|
|
|
reg [6:0] brick_index;// index into array of current brick
|
|
|
|
// brick graphics signal
|
2017-11-29 01:48:27 +00:00
|
|
|
wire brick_gfx = lr_border || (brick_present && vpos[2:0] != 0 && hpos[3:1] != 4);
|
2017-11-20 01:32:58 +00:00
|
|
|
|
2017-11-29 01:48:27 +00:00
|
|
|
// scan bricks: compute brick_index and brick_present flag
|
2018-02-09 16:59:52 +00:00
|
|
|
always @(posedge clk)
|
2017-11-18 19:51:25 +00:00
|
|
|
// see if we are scanning brick area
|
|
|
|
if (vpos[8:6] == 1 && !lr_border)
|
2017-11-18 00:40:44 +00:00
|
|
|
begin
|
2017-11-18 19:51:25 +00:00
|
|
|
// every 16th pixel, starting at 8
|
2017-11-18 00:40:44 +00:00
|
|
|
if (hpos[3:0] == 8) begin
|
2017-11-18 19:51:25 +00:00
|
|
|
// compute brick index
|
2017-11-18 00:40:44 +00:00
|
|
|
brick_index <= {vpos[5:3], hpos[7:4]};
|
2017-11-18 19:51:25 +00:00
|
|
|
end
|
|
|
|
// every 17th pixel
|
|
|
|
else if (hpos[3:0] == 9) begin
|
|
|
|
// load brick bit from array
|
2018-10-09 23:37:38 +00:00
|
|
|
brick_present <= !brick_array[brick_index];
|
2017-11-18 00:40:44 +00:00
|
|
|
end
|
|
|
|
end else begin
|
|
|
|
brick_present <= 0;
|
|
|
|
end
|
2017-11-29 01:48:27 +00:00
|
|
|
|
2017-11-20 01:32:58 +00:00
|
|
|
// only works when paddle at bottom of screen!
|
|
|
|
// (we don't want to mess w/ paddle position during visible portion)
|
|
|
|
always @(posedge hsync)
|
|
|
|
if (!hpaddle)
|
|
|
|
paddle_pos <= vpos;
|
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
// 1 when ball signal intersects main (brick + border) signal
|
2017-11-17 21:01:07 +00:00
|
|
|
wire ball_pixel_collide = main_gfx & ball_gfx;
|
|
|
|
|
2017-11-29 01:48:27 +00:00
|
|
|
reg ball_collide_paddle = 0;
|
|
|
|
reg [3:0] ball_collide_bits = 0;
|
2017-11-17 21:01:07 +00:00
|
|
|
|
2017-11-18 19:51:25 +00:00
|
|
|
// compute ball collisions with paddle and playfield
|
2018-02-09 16:59:52 +00:00
|
|
|
always @(posedge clk)
|
2018-10-09 23:37:38 +00:00
|
|
|
// clear all collide bits for frame
|
|
|
|
if (vsync) begin
|
|
|
|
ball_collide_bits <= 0;
|
|
|
|
ball_collide_paddle <= 0;
|
|
|
|
end else begin
|
|
|
|
if (ball_pixel_collide) begin
|
|
|
|
// did we collide w/ paddle?
|
|
|
|
if (paddle_gfx) begin
|
|
|
|
ball_collide_paddle <= 1;
|
|
|
|
end
|
|
|
|
// ball has 4 collision quadrants
|
|
|
|
if (!ball_rel_x[2] & !ball_rel_y[2]) ball_collide_bits[0] <= 1;
|
|
|
|
if (ball_rel_x[2] & !ball_rel_y[2]) ball_collide_bits[1] <= 1;
|
|
|
|
if (!ball_rel_x[2] & ball_rel_y[2]) ball_collide_bits[2] <= 1;
|
|
|
|
if (ball_rel_x[2] & ball_rel_y[2]) ball_collide_bits[3] <= 1;
|
2017-11-18 19:51:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-29 01:48:27 +00:00
|
|
|
// compute ball collisions with brick and increment score
|
2018-02-09 16:59:52 +00:00
|
|
|
always @(posedge clk)
|
2017-11-18 19:51:25 +00:00
|
|
|
if (ball_pixel_collide && brick_present) begin
|
2018-10-09 23:37:38 +00:00
|
|
|
brick_array[brick_index] <= 1;
|
2017-11-18 19:51:25 +00:00
|
|
|
incscore <= 1; // increment score
|
|
|
|
end else begin
|
|
|
|
incscore <= 0; // reset incscore
|
2017-11-17 21:01:07 +00:00
|
|
|
end
|
2017-11-18 00:40:44 +00:00
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
// computes position of ball in relation to center of paddle
|
2017-11-18 19:51:25 +00:00
|
|
|
wire signed [8:0] ball_paddle_dx = ball_x - paddle_pos + 8;
|
|
|
|
|
2017-11-29 01:48:27 +00:00
|
|
|
// ball bounce: determine new velocity/direction
|
2017-11-18 00:40:44 +00:00
|
|
|
always @(posedge vsync or posedge reset)
|
2017-11-17 21:01:07 +00:00
|
|
|
begin
|
2017-11-18 00:40:44 +00:00
|
|
|
if (reset) begin
|
|
|
|
ball_dir_y <= BALL_DIR_DOWN;
|
|
|
|
end else
|
2017-11-18 19:51:25 +00:00
|
|
|
// ball collided with paddle?
|
2017-11-29 01:48:27 +00:00
|
|
|
if (ball_collide_paddle) begin
|
2017-11-18 00:40:44 +00:00
|
|
|
// bounces upward off of paddle
|
|
|
|
ball_dir_y <= BALL_DIR_UP;
|
|
|
|
// which side of paddle, left/right?
|
2017-11-29 01:48:27 +00:00
|
|
|
ball_dir_x <= (ball_paddle_dx < 20) ? BALL_DIR_LEFT : BALL_DIR_RIGHT;
|
2017-11-18 00:40:44 +00:00
|
|
|
// hitting with edge of paddle makes it fast
|
2017-11-24 19:14:22 +00:00
|
|
|
ball_speed_x <= ball_collide_bits[3:0] != 4'b1100;
|
2017-11-18 00:40:44 +00:00
|
|
|
end else begin
|
|
|
|
// collided with playfield
|
|
|
|
// TODO: can still slip through corners
|
2017-11-18 19:51:25 +00:00
|
|
|
// compute left/right bounce
|
2017-11-18 00:40:44 +00:00
|
|
|
casez (ball_collide_bits[3:0])
|
|
|
|
4'b01?1: ball_dir_x <= BALL_DIR_RIGHT; // left edge/corner
|
|
|
|
4'b1101: ball_dir_x <= BALL_DIR_RIGHT; // left corner
|
|
|
|
4'b101?: ball_dir_x <= BALL_DIR_LEFT; // right edge/corner
|
|
|
|
4'b1110: ball_dir_x <= BALL_DIR_LEFT; // right corner
|
|
|
|
default: ;
|
|
|
|
endcase
|
2017-11-18 19:51:25 +00:00
|
|
|
// compute top/bottom bounce
|
2017-11-18 00:40:44 +00:00
|
|
|
casez (ball_collide_bits[3:0])
|
|
|
|
4'b1011: ball_dir_y <= BALL_DIR_DOWN;
|
|
|
|
4'b0111: ball_dir_y <= BALL_DIR_DOWN;
|
|
|
|
4'b001?: ball_dir_y <= BALL_DIR_DOWN;
|
|
|
|
4'b0001: ball_dir_y <= BALL_DIR_DOWN;
|
|
|
|
4'b0100: ball_dir_y <= BALL_DIR_UP;
|
|
|
|
4'b1?00: ball_dir_y <= BALL_DIR_UP;
|
|
|
|
4'b1101: ball_dir_y <= BALL_DIR_UP;
|
|
|
|
4'b1110: ball_dir_y <= BALL_DIR_UP;
|
|
|
|
default: ;
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-29 01:48:27 +00:00
|
|
|
// ball motion: update ball position
|
2017-11-18 00:40:44 +00:00
|
|
|
always @(negedge vsync or posedge reset)
|
|
|
|
begin
|
|
|
|
if (reset) begin
|
2018-07-31 18:41:27 +00:00
|
|
|
// reset ball position to top center
|
2017-11-18 00:40:44 +00:00
|
|
|
ball_x <= 128;
|
|
|
|
ball_y <= 180;
|
|
|
|
end else begin
|
2018-07-31 18:41:27 +00:00
|
|
|
// move ball horizontal and vertical position
|
2017-11-18 00:40:44 +00:00
|
|
|
if (ball_dir_x == BALL_DIR_RIGHT)
|
|
|
|
ball_x <= ball_x + (ball_speed_x?1:0) + 1;
|
|
|
|
else
|
|
|
|
ball_x <= ball_x - (ball_speed_x?1:0) - 1;
|
|
|
|
ball_y <= ball_y + (ball_dir_y==BALL_DIR_DOWN?1:-1);
|
|
|
|
end
|
|
|
|
end
|
2017-11-17 21:01:07 +00:00
|
|
|
|
2017-11-29 01:48:27 +00:00
|
|
|
// compute main_gfx
|
|
|
|
always @(*)
|
|
|
|
begin
|
|
|
|
case (vpos[8:3])
|
2018-10-09 23:37:38 +00:00
|
|
|
0,1,2: main_gfx = score_gfx; // scoreboard
|
2017-11-29 01:48:27 +00:00
|
|
|
3: main_gfx = 0;
|
|
|
|
4: main_gfx = 1; // top border
|
2018-10-09 23:37:38 +00:00
|
|
|
8,9,10,11,12,13,14,15: main_gfx = brick_gfx; // brick rows 1-8
|
2017-11-29 01:48:27 +00:00
|
|
|
28: main_gfx = paddle_gfx | lr_border; // paddle
|
|
|
|
29: main_gfx = hpos[0] ^ vpos[0]; // bottom border
|
|
|
|
default: main_gfx = lr_border; // left/right borders
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
// combine signals to RGB output
|
2017-11-17 21:01:07 +00:00
|
|
|
wire grid_gfx = (((hpos&7)==0) || ((vpos&7)==0));
|
2017-11-18 19:51:25 +00:00
|
|
|
wire r = display_on && (ball_gfx | paddle_gfx);
|
2017-11-17 21:01:07 +00:00
|
|
|
wire g = display_on && (main_gfx | ball_gfx);
|
2017-11-18 19:51:25 +00:00
|
|
|
wire b = display_on && (grid_gfx | ball_gfx | brick_present);
|
2017-11-17 21:01:07 +00:00
|
|
|
assign rgb = {b,g,r};
|
|
|
|
|
|
|
|
endmodule
|