2018-08-13 22:17:36 +00:00
|
|
|
|
2017-11-21 21:36:38 +00:00
|
|
|
`include "hvsync_generator.v"
|
|
|
|
|
2018-10-01 16:30:47 +00:00
|
|
|
/*
|
|
|
|
A bouncing ball using the "slipping counter" method, as
|
|
|
|
used in Pong, Computer Space, and other early arcade games.
|
|
|
|
*/
|
|
|
|
|
2018-02-15 18:31:32 +00:00
|
|
|
module ball_slip_counter_top(clk, reset, hsync, vsync, rgb);
|
2017-11-21 21:36:38 +00:00
|
|
|
|
|
|
|
input clk;
|
|
|
|
input reset;
|
|
|
|
output hsync, vsync;
|
|
|
|
output [2:0] rgb;
|
|
|
|
wire display_on;
|
|
|
|
wire [8:0] hpos;
|
|
|
|
wire [8:0] vpos;
|
|
|
|
|
2018-07-12 04:59:35 +00:00
|
|
|
// 9-bit ball timers
|
2017-11-29 01:48:27 +00:00
|
|
|
reg [8:0] ball_htimer;
|
|
|
|
reg [8:0] ball_vtimer;
|
2017-11-21 21:36:38 +00:00
|
|
|
|
2018-07-12 04:59:35 +00:00
|
|
|
// 4-bit motion codes
|
2018-07-10 01:46:45 +00:00
|
|
|
reg [3:0] ball_horiz_move;
|
|
|
|
reg [3:0] ball_vert_move;
|
|
|
|
|
2018-07-12 04:59:35 +00:00
|
|
|
// 4-bit stop codes
|
|
|
|
localparam ball_horiz_stop = 4'd11;
|
|
|
|
localparam ball_vert_stop = 4'd10;
|
2018-07-10 01:46:45 +00:00
|
|
|
|
|
|
|
// 5-bit constants to load into counters
|
|
|
|
localparam ball_horiz_prefix = 5'b01100; // 192
|
|
|
|
localparam ball_vert_prefix = 5'b01111; // 240
|
2018-07-31 18:41:27 +00:00
|
|
|
|
|
|
|
// reset ball; will be unset when video beam reaches center position
|
|
|
|
reg ball_reset;
|
2018-02-03 20:20:56 +00:00
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
// video sync generator
|
2017-11-21 21:36:38 +00:00
|
|
|
hvsync_generator hvsync_gen(
|
|
|
|
.clk(clk),
|
2017-11-29 01:48:27 +00:00
|
|
|
.reset(reset),
|
2017-11-21 21:36:38 +00:00
|
|
|
.hsync(hsync),
|
|
|
|
.vsync(vsync),
|
|
|
|
.display_on(display_on),
|
|
|
|
.hpos(hpos),
|
|
|
|
.vpos(vpos)
|
|
|
|
);
|
|
|
|
|
2017-11-29 01:48:27 +00:00
|
|
|
// update horizontal timer
|
2018-07-12 04:59:35 +00:00
|
|
|
always @(posedge clk or posedge ball_reset)
|
|
|
|
begin
|
|
|
|
if (ball_reset || &ball_htimer) begin
|
|
|
|
if (ball_reset || &ball_vtimer) // nudge ball in horiz. dir
|
2018-07-10 01:46:45 +00:00
|
|
|
ball_htimer <= {ball_horiz_prefix, ball_horiz_move};
|
|
|
|
else // reset timer but don't move ball horizontally
|
|
|
|
ball_htimer <= {ball_horiz_prefix, ball_horiz_stop};
|
2017-11-29 01:48:27 +00:00
|
|
|
end else
|
2017-11-21 21:36:38 +00:00
|
|
|
ball_htimer <= ball_htimer + 1;
|
2018-02-03 20:20:56 +00:00
|
|
|
end
|
2017-11-21 21:36:38 +00:00
|
|
|
|
2017-11-29 01:48:27 +00:00
|
|
|
// update vertical timer
|
2018-07-12 04:59:35 +00:00
|
|
|
always @(posedge hsync or posedge ball_reset)
|
2017-11-21 21:36:38 +00:00
|
|
|
begin
|
2018-07-12 04:59:35 +00:00
|
|
|
if (ball_reset || &ball_vtimer) // reset timer
|
2018-07-10 01:46:45 +00:00
|
|
|
ball_vtimer <= {ball_vert_prefix, ball_vert_move};
|
2017-11-29 01:48:27 +00:00
|
|
|
else
|
2017-11-21 21:36:38 +00:00
|
|
|
ball_vtimer <= ball_vtimer + 1;
|
2018-02-03 20:20:56 +00:00
|
|
|
end
|
2018-07-12 04:59:35 +00:00
|
|
|
|
|
|
|
// reset ball position
|
|
|
|
always @(posedge clk or posedge reset)
|
|
|
|
begin
|
|
|
|
if (reset)
|
|
|
|
ball_reset <= 1;
|
|
|
|
else if (hpos == 128 && vpos == 128)
|
2018-07-31 18:41:27 +00:00
|
|
|
ball_reset <= 0; // un-reset when beam reaches center position
|
2018-07-12 04:59:35 +00:00
|
|
|
end
|
2018-02-03 20:20:56 +00:00
|
|
|
|
2018-07-31 18:41:27 +00:00
|
|
|
// collide with vertical and horizontal boundaries?
|
2018-02-03 20:20:56 +00:00
|
|
|
wire ball_vert_collide = ball_vgfx && vpos >= 240;
|
|
|
|
wire ball_horiz_collide = ball_hgfx && hpos >= 256 && vpos == 255;
|
2017-11-29 01:48:27 +00:00
|
|
|
|
|
|
|
// vertical bounce
|
2018-07-10 01:46:45 +00:00
|
|
|
always @(posedge ball_vert_collide or posedge reset)
|
2017-11-29 01:48:27 +00:00
|
|
|
begin
|
2018-07-10 01:46:45 +00:00
|
|
|
if (reset)
|
2018-07-31 18:41:27 +00:00
|
|
|
ball_vert_move <= 4'd9; // initial vertical velocity
|
2018-07-10 01:46:45 +00:00
|
|
|
else
|
2018-07-12 04:59:35 +00:00
|
|
|
ball_vert_move <= (4'd9 ^ 4'd11) ^ ball_vert_move; // change dir.
|
2018-02-03 20:20:56 +00:00
|
|
|
end
|
2017-11-29 01:48:27 +00:00
|
|
|
|
|
|
|
// horizontal bounce
|
2018-07-10 01:46:45 +00:00
|
|
|
always @(posedge ball_horiz_collide or posedge reset)
|
2017-11-29 01:48:27 +00:00
|
|
|
begin
|
2018-07-10 01:46:45 +00:00
|
|
|
if (reset)
|
2018-07-31 18:41:27 +00:00
|
|
|
ball_horiz_move <= 4'd10; // initial horizontal velocity
|
2018-07-10 01:46:45 +00:00
|
|
|
else
|
2018-07-12 04:59:35 +00:00
|
|
|
ball_horiz_move <= (4'd10 ^ 4'd12) ^ ball_horiz_move; // change dir.
|
2018-02-03 20:20:56 +00:00
|
|
|
end
|
2017-11-29 01:48:27 +00:00
|
|
|
|
2018-02-03 20:20:56 +00:00
|
|
|
// compute ball display
|
2018-07-31 18:41:27 +00:00
|
|
|
wire ball_hgfx = ball_htimer >= 508; // 512-508 = 4 pixel ball
|
2017-11-29 01:48:27 +00:00
|
|
|
wire ball_vgfx = ball_vtimer >= 508;
|
|
|
|
wire ball_gfx = ball_hgfx && ball_vgfx;
|
2017-11-21 21:36:38 +00:00
|
|
|
|
2018-02-03 20:20:56 +00:00
|
|
|
// compute grid display
|
2017-11-29 01:48:27 +00:00
|
|
|
wire grid_gfx = (((hpos&7)==0) && ((vpos&7)==0));
|
2017-11-21 21:36:38 +00:00
|
|
|
|
2018-02-03 20:20:56 +00:00
|
|
|
// combine into RGB signals
|
2017-11-29 01:48:27 +00:00
|
|
|
wire r = display_on && (ball_hgfx | ball_gfx);
|
|
|
|
wire g = display_on && (grid_gfx | ball_gfx);
|
|
|
|
wire b = display_on && (ball_vgfx | ball_gfx);
|
2017-11-21 21:36:38 +00:00
|
|
|
assign rgb = {b,g,r};
|
|
|
|
|
|
|
|
endmodule
|