Skip to content

Instantly share code, notes, and snippets.

@suarezvictor
Created October 27, 2022 03:17
Show Gist options
  • Save suarezvictor/2367c35ccd2281b530045b10136374d4 to your computer and use it in GitHub Desktop.
Save suarezvictor/2367c35ccd2281b530045b10136374d4 to your computer and use it in GitHub Desktop.
/*
A bouncing ball using absolute coordinates.
*/
//inspired on https://8bitworkshop.com/v3.10.0/?platform=verilog&file=ball_absolute.v
module frame_display(pix_vblank, pix_y, pix_x, pix_r, pix_g, pix_b, sys_clk, sys_rst);
input sys_clk;
input sys_rst;
input pix_vblank;
input [9:0] pix_x;
input [9:0] pix_y;
output [7:0] pix_r, pix_g, pix_b;
reg [9:0] ball_hpos; // ball current X position
reg [9:0] ball_vpos; // ball current Y position
//FIXME: support for sign extensions in move
reg signed [9:0] ball_horiz_move = 1; // ball current X velocity
reg signed [9:0] ball_vert_move = 1; // ball current Y velocity
localparam ball_horiz_initial = 640/2; // ball initial X position
localparam ball_vert_initial = 480/2; // ball initial Y position
localparam BALL_SIZE = 8; // ball size (in pixels)
// update horizontal timer
always @(posedge pix_vblank or posedge sys_rst)
begin
if (sys_rst) begin
// sys_rst ball position to center
ball_vpos <= ball_vert_initial;
ball_hpos <= ball_horiz_initial;
end else begin
// add velocity vector to ball position
ball_hpos <= ball_hpos + ball_horiz_move;
ball_vpos <= ball_vpos + ball_vert_move;
end
end
// vertical bounce
always @(posedge ball_vert_collide)
begin
ball_vert_move <= -ball_vert_move;
end
// horizontal bounce
always @(posedge ball_horiz_collide)
begin
ball_horiz_move <= -ball_horiz_move;
end
// offset of ball position from video beam
wire [9:0] ball_hdiff = pix_x - ball_hpos;
wire [9:0] ball_vdiff = pix_y - ball_vpos;
// ball graphics output
wire ball_hgfx = ball_hdiff < BALL_SIZE;
wire ball_vgfx = ball_vdiff < BALL_SIZE;
wire ball_gfx = ball_hgfx && ball_vgfx;
// collide with vertical and horizontal boundaries
// these are set when the ball touches a border
wire ball_vert_collide = ball_vpos >= 480 - BALL_SIZE;
wire ball_horiz_collide = ball_hpos >= 640 - BALL_SIZE;
// combine signals to RGB output
wire grid_gfx = (!(pix_x&28)) && (!(pix_y&28));
wire r = ball_hgfx | ball_gfx;
wire g = grid_gfx | ball_gfx;
wire b = ball_vgfx | ball_gfx;
assign pix_r = r << 7;
assign pix_g = g << 7;
assign pix_b = b << 7;
endmodule
MODULE frame_display
( uint1 const & pix_vblank //1
,uint10 const & pix_y //2
,uint10 const & pix_x //3
,uint8 & pix_r //4
,uint8 & pix_g //5
,uint8 & pix_b //6
,uint1 const & sys_clk //7
,uint1 const & sys_rst //8
)
{
uint10 var_0_ball_horiz_move_9_0_;
uint10 var_0_ball_hpos_9_0_;
uint10 var_0_ball_vert_move_9_0_;
uint10 var_0_ball_vpos_9_0_;
uint10 var_1_ball_horiz_move_9_0_;
uint10 var_1_ball_vert_move_9_0_;
uint10 var_add48_2_Y;
uint10 var_add49_3_Y;
uint32 var_and80_15_Y;
uint32 var_and80_17_Y;
uint1 var_ge76_13_Y;
uint1 var_ge77_14_Y;
uint1 var_logic_and72_12_Y;
uint1 var_logic_and80_19_Y;
uint1 var_logic_not80_16_Y;
uint1 var_logic_not80_18_Y;
uint1 var_lt70_10_Y;
uint1 var_lt71_11_Y;
int10 var_neg56_5_Y;
int10 var_neg62_7_Y;
uint1 var_or81_20_Y;
uint1 var_or82_21_Y;
uint1 var_or83_22_Y;
uint8 var_shl85_23_Y;
uint8 var_shl86_24_Y;
uint8 var_shl87_25_Y;
uint10 var_sub66_8_Y;
uint10 var_sub67_9_Y;
uint1 b;
uint1 ball_gfx;
uint10 ball_hdiff;
uint1 ball_hgfx;
uint1 ball_horiz_collide;
int10 ball_horiz_move;
uint10 ball_hpos;
uint10 ball_vdiff;
uint1 ball_vert_collide;
int10 ball_vert_move;
uint1 ball_vgfx;
uint10 ball_vpos;
uint1 g;
uint1 grid_gfx;
uint1 r;
ball_horiz_move = 1; // rhs from root action
ball_vert_move = 1; // rhs from root action
//-------LOOP-----------
while(always())
{
ball_hdiff = var_sub66_8_Y;
ball_vdiff = var_sub67_9_Y;
ball_hgfx = var_lt70_10_Y;
ball_vgfx = var_lt71_11_Y;
ball_gfx = var_logic_and72_12_Y;
ball_vert_collide = var_ge76_13_Y;
ball_horiz_collide = var_ge77_14_Y;
grid_gfx = var_logic_and80_19_Y;
r = var_or81_20_Y;
g = var_or82_21_Y;
b = var_or83_22_Y;
pix_r = var_shl85_23_Y;
pix_g = var_shl86_24_Y;
pix_b = var_shl87_25_Y;
var_add48_2_Y = ball_hpos + ball_horiz_move;
var_add49_3_Y = ball_vpos + ball_vert_move;
var_and80_15_Y = pix_x & 28;
var_and80_17_Y = pix_y & 28;
var_ge76_13_Y = ball_vpos >= 472;
ball_vert_collide = var_ge76_13_Y; //reconnection
var_ge77_14_Y = ball_hpos >= 632;
ball_horiz_collide = var_ge77_14_Y; //reconnection
var_logic_and72_12_Y = ball_hgfx && ball_vgfx;
ball_gfx = var_logic_and72_12_Y; //reconnection
var_logic_and80_19_Y = var_logic_not80_16_Y && var_logic_not80_18_Y;
grid_gfx = var_logic_and80_19_Y; //reconnection
var_logic_not80_16_Y = !var_and80_15_Y;
var_logic_not80_18_Y = !var_and80_17_Y;
var_lt70_10_Y = ball_hdiff < 8;
ball_hgfx = var_lt70_10_Y; //reconnection
var_lt71_11_Y = ball_vdiff < 8;
ball_vgfx = var_lt71_11_Y; //reconnection
var_neg56_5_Y = -ball_vert_move;
var_neg62_7_Y = -ball_horiz_move;
var_or81_20_Y = ball_hgfx | ball_gfx;
r = var_or81_20_Y; //reconnection
var_or82_21_Y = grid_gfx | ball_gfx;
g = var_or82_21_Y; //reconnection
var_or83_22_Y = ball_vgfx | ball_gfx;
b = var_or83_22_Y; //reconnection
var_shl85_23_Y = r << 7;
pix_r = var_shl85_23_Y; //reconnection
var_shl86_24_Y = g << 7;
pix_g = var_shl86_24_Y; //reconnection
var_shl87_25_Y = b << 7;
pix_b = var_shl87_25_Y; //reconnection
var_sub66_8_Y = pix_x - ball_hpos;
ball_hdiff = var_sub66_8_Y; //reconnection
var_sub67_9_Y = pix_y - ball_vpos;
ball_vdiff = var_sub67_9_Y; //reconnection
var_1_ball_horiz_move_9_0_ = 1;
var_1_ball_vert_move_9_0_ = 1;
var_0_ball_hpos_9_0_ = ball_hpos;
var_0_ball_vpos_9_0_ = ball_vpos;
if(sys_rst)
{
var_0_ball_vpos_9_0_ = 240;
var_0_ball_hpos_9_0_ = 320;
}
else
{
var_0_ball_hpos_9_0_ = var_add48_2_Y;
var_0_ball_vpos_9_0_ = var_add49_3_Y;
}
var_0_ball_vert_move_9_0_ = var_neg56_5_Y;
var_0_ball_horiz_move_9_0_ = var_neg62_7_Y;
if(posedge(pix_vblank))
ball_hpos = var_0_ball_hpos_9_0_;
if(posedge(pix_vblank))
ball_vpos = var_0_ball_vpos_9_0_;
if(posedge(sys_rst))
ball_hpos = var_0_ball_hpos_9_0_;
if(posedge(sys_rst))
ball_vpos = var_0_ball_vpos_9_0_;
if(posedge(ball_vert_collide))
ball_vert_move = var_0_ball_vert_move_9_0_;
if(posedge(ball_horiz_collide))
ball_horiz_move = var_0_ball_horiz_move_9_0_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment