Commit 1fca1517 authored by ktsiam's avatar ktsiam
Browse files

pixel prototype -- not compiled

parent f61443f2
...@@ -4,33 +4,33 @@ ...@@ -4,33 +4,33 @@
/* 32 x 8 */ /* 32 x 8 */
struct State { struct State {
static const int PIXEL = 512; // size of pixel static const int PIXEL = 32; // size of pixel
static const int BLOCK = 32 * PIXEL; static const int BRICK = 32 * PIXEL;
static const int WIDTH = 512 * PIXEL; // 16 blocks static const int WIDTH = 512 * PIXEL; // 16 blocks
static const int HEIGHT = 480 * PIXEL; // 15 blocks static const int HEIGHT = 480 * PIXEL; // 15 blocks
static const int PADDLE_WIDTH = 50 * PIXEL; static const int PADDLE_WIDTH = 80 * PIXEL;
static const int PADDLE_SPEED = 50 * PIXEL; // CHANGE THIS static const int PADDLE_SPEED = 50 * PIXEL; // CHANGE THIS
static const int BLOCK_ROWS = 5; static const int BRICK_ROWS = 5;
static const int BLOCK_COLS = 16; static const int BRICK_COLS = 16;
static const int BLOCK_NUM = BLOCK_ROWS * BLOCK_COLS; static const int BRICK_NUM = BRICK_ROWS * BRICK_COLS;
static_assert(WIDTH/BLOCK_COLS == BLOCK, "INVARIANT INVALID"); static_assert(WIDTH/BRICK_COLS == BRICK, "INVARIANT INVALID");
bool bricks[BLOCK_NUM]; bool bricks[BRICK_NUM];
int paddle; int paddle;
int ball_p[2]; int ball_p[2];
int ball_v[2]; int ball_v[2];
void reset() { void reset() {
std::fill_n(bricks, BLOCK_NUM, false); std::fill_n(bricks, BRICK_NUM, false);
paddle = WIDTH/2; paddle = WIDTH/2;
ball_p[0] = 3*WIDTH/4; ball_p[0] = 3*WIDTH/4;
ball_p[1] = HEIGHT-1; ball_p[1] = HEIGHT-1;
ball_v[0] = BLOCK; // CHANGE THIS ball_v[0] = BRICK; // CHANGE THIS
ball_v[1] = -BLOCK; // CHANGE THIS ball_v[1] = -BRICK; // CHANGE THIS
} }
State() { reset(); } State() { reset(); }
...@@ -72,19 +72,19 @@ struct State { ...@@ -72,19 +72,19 @@ struct State {
} }
} }
// for (int i = 0; i < BLOCK_ROWS * BLOCK_COLS; ++i) { // for (int i = 0; i < BRICK_ROWS * BRICK_COLS; ++i) {
// // constructing brick intervals // // constructing brick intervals
// int x_interval[2]; // int x_interval[2];
// int y_interval[2]; // int y_interval[2];
// x_interval[0] = (i % BLOCK_COLS) * BLOCK_SIZE; // x_interval[0] = (i % BRICK_COLS) * BRICK_SIZE;
// x_interval[1] = x_interval[0] + BLOCK_SIZE; // x_interval[1] = x_interval[0] + BRICK_SIZE;
// y_interval[0] = (i / BLOCK_COLS) * BLOCK_SIZE; // y_interval[0] = (i / BRICK_COLS) * BRICK_SIZE;
// y_interval[1] = y_interval[0] + BLOCK_SIZE; // y_interval[1] = y_interval[0] + BRICK_SIZE;
// std::cerr << "CURRENT BLOCK : (" << x_interval[0]/UNIT << '-' << x_interval[1]/UNIT // std::cerr << "CURRENT BRICK : (" << x_interval[0]/UNIT << '-' << x_interval[1]/UNIT
// << ", " << y_interval[0]/UNIT << '-' << y_interval[1]/UNIT << ")\n"; // << ", " << y_interval[0]/UNIT << '-' << y_interval[1]/UNIT << ")\n";
...@@ -95,7 +95,7 @@ struct State { ...@@ -95,7 +95,7 @@ struct State {
// continue; // no collision // continue; // no collision
// else { // else {
// std::cerr << "BALL IS AT (" << ball_p[0]/UNIT << ", " << ball_p[1]/UNIT << ")\n"; // std::cerr << "BALL IS AT (" << ball_p[0]/UNIT << ", " << ball_p[1]/UNIT << ")\n";
// std::cerr << "BLOCK COLLISION AT " << x_interval[0]/UNIT << " and " << y_interval[0]/UNIT << std::endl; // std::cerr << "BRICK COLLISION AT " << x_interval[0]/UNIT << " and " << y_interval[0]/UNIT << std::endl;
// bricks[i] = false; // bricks[i] = false;
// // center of block // // center of block
...@@ -116,19 +116,19 @@ struct State { ...@@ -116,19 +116,19 @@ struct State {
void render() { // DISCARD THIS -- WILL BE REPLACED BY VGA void render() { // DISCARD THIS -- WILL BE REPLACED BY VGA
std::cout << "POSITION : " << ball_p[0]/PIXEL << ", " << ball_p[1]/PIXEL << std::endl; std::cout << "POSITION : " << ball_p[0]/PIXEL << ", " << ball_p[1]/PIXEL << std::endl;
for (int i = 0; i < WIDTH/BLOCK; ++i) for (int i = 0; i < WIDTH/BRICK; ++i)
std::cout << "--"; std::cout << "--";
std::cout << '\n'; std::cout << '\n';
for (int r = 0; r < HEIGHT/BLOCK; ++r) { for (int r = 0; r < HEIGHT/BRICK; ++r) {
std::cout << '|'; std::cout << '|';
for (int c = 0; c < WIDTH/BLOCK; ++c) { for (int c = 0; c < WIDTH/BRICK; ++c) {
if (r < BLOCK_ROWS and bricks[(BLOCK_ROWS-r-1)*BLOCK_COLS+c]) { if (r < BRICK_ROWS and bricks[(BRICK_ROWS-r-1)*BRICK_COLS+c]) {
std::cout << "x "; std::cout << "x ";
} else if (ball_p[0] >= c*BLOCK and } else if (ball_p[0] >= c*BRICK and
(ball_p[0] < (c+1)*BLOCK) and (ball_p[0] < (c+1)*BRICK) and
ball_p[1] >= r*BLOCK and ball_p[1] >= r*BRICK and
ball_p[1] < (r+1)*BLOCK) ball_p[1] < (r+1)*BRICK)
std::cout << "o "; std::cout << "o ";
else { else {
std::cout << " "; std::cout << " ";
...@@ -137,14 +137,14 @@ struct State { ...@@ -137,14 +137,14 @@ struct State {
std::cout << "|\n"; std::cout << "|\n";
} }
for (int i = 0; i < WIDTH; i += BLOCK) { for (int i = 0; i < WIDTH; i += BRICK) {
if (i > paddle and i < paddle+PADDLE_WIDTH) if (i > paddle and i < paddle+PADDLE_WIDTH)
std::cout << "--"; std::cout << "--";
else std::cout << " "; else std::cout << " ";
} }
std::cout << '\n'; std::cout << '\n';
std::cout << "PADDLE : " << (float)paddle/BLOCK << std::endl; std::cout << "PADDLE : " << (float)paddle/BRICK << std::endl;
} }
}; };
......
...@@ -7,9 +7,9 @@ entity GAME is ...@@ -7,9 +7,9 @@ entity GAME is
L : in std_logic; L : in std_logic;
R : in std_logic; R : in std_logic;
clk : in std_logic; clk : in std_logic;
ball_x_position : out integer (16 downto 0); ball_x_position : out unsigned (10 downto 0);
ball_y_position : out integer (16 downto 0); ball_y_position : out unsigned (10 downto 0);
paddle_position : out integer (16 downto 0); paddle_position : out unsigned (10 downto 0);
game_over : out std_logic; game_over : out std_logic;
--ball_array : out unsigned(99 downto 0); --ball_array : out unsigned(99 downto 0);
...@@ -22,27 +22,33 @@ architecture GAME_ARCH of GAME is ...@@ -22,27 +22,33 @@ architecture GAME_ARCH of GAME is
-- to avoid unsigned underflow -- to avoid unsigned underflow
-- Every constant must be power of 2 constant PIXEL : unsigned (16 downto 0) := 17d"32";
constant UNIT : integer (16 downto 0) := 17d"1024"; constant BRICK : unsigned (16 downto 0) := 16d"32" * PIXEL;
constant PIXEL_SHIFT : unsigned (16 downto 0) := 17d"5";
constant WIDTH : integer (16 downto 0) := 17d"32" * UNIT;
constant HEIGHT : integer (16 downto 0) := 17d"16" * UNIT;
constant PADDLE_WIDTH : integer (16 downto 0) := 17d"4" * UNIT; -- constant WIDTH : unsigned (16 downto 0) := 17d"512" * PIXEL;
constant PADDLE_SPEED : integer (16 downto 0) := 1; -- -- constant HEIGHT : unsigned (16 downto 0) := 17d"480" * PIXEL;
constant MIN_X : unsigned (16 downto 0) := 17d"63" * PIXEL;
constant MAX_X : unsigned (16 downto 0) := 17d"575" * PIXEL;
constant MIN_Y : unsigned (16 downto 0) := 17d"9" * PIXEL;
constant MAX_Y : unsigned (16 downto 0) := 17d"480" * PIXEL;
constant MAX_SPEED : unsigned (16 downto 0) := 17d"8" * PIXEL; -- (< 9)!
constant PADDLE_WIDTH : unsigned (16 downto 0) := 17d"80" * PIXEL;
constant PADDLE_SPEED : unsigned (16 downto 0) := MAX_SPEED;
constant BRICK_ROWS : integer (16 downto 0) := 8; constant BRICK_ROWS : unsigned (16 downto 0) := 17d"5";
constant BRICK_COLS : integer (16 downto 0) := 32; constant BRICK_COLS : unsigned (16 downto 0) := 16d"16";
constant BRICK_SIZE : integer (16 downto 0) := WIDTH/BRICK_COLS; constant BRICK_SIZE : unsigned (16 downto 0) := BRICK_ROWS * BRICK_COLS;
signal L : std_logic := '0';
signal R : std_logic := '1';
signal paddle : integer (16 downto 0) := 0; signal paddle : unsigned (16 downto 0) := MIN_X;
signal ball_x : integer (16 downto 0) := 0; signal ball_x : unsigned (16 downto 0) := MIN_X;
signal ball_y : integer (16 downto 0) := HEIGHT; -- y=0 is top signal ball_y : unsigned (16 downto 0) := MAX_Y;
signal ball_vx : integer (16 downto 0) := UNIT; -- MUST LOWER SIGNIFICANTLY signal ball_vx : unsigned (16 downto 0) := MAX_SPEED;
signal ball_vy : integer (16 downto 0) := -UNIT; -- MUST LOWER SIGNIFICANTLY signal ball_vy : unsigned (16 downto 0) := -MAX_SPEED;
begin begin
process (clk) is process (clk) is
...@@ -50,9 +56,9 @@ begin ...@@ -50,9 +56,9 @@ begin
-- PADDLE (changes 'paddle') -- PADDLE (changes 'paddle')
if L = '1' and R = '0' then if L = '1' and R = '0' then
paddle <= max (17d"0", paddle-PADDLE_SPEED); paddle <= max (MIN_X, paddle-PADDLE_SPEED);
elsif L = '0' and R = '1' then elsif L = '0' and R = '1' then
paddle <= min (WIDTH-PADDLE_WIDTH-1, paddle+PADDLE_SPEED); paddle <= min (MAX_X-PADDLE_WIDTH, paddle+PADDLE_SPEED);
else else
paddle <= paddle; paddle <= paddle;
end if; end if;
...@@ -62,22 +68,22 @@ begin ...@@ -62,22 +68,22 @@ begin
ball_y <= ball_y + ball_vy; ball_y <= ball_y + ball_vy;
-- BALL COLLISION (changes 'ball_vx', 'ball_vy') -- BALL COLLISION (changes 'ball_vx', 'ball_vy')
if (ball_x <= 17d"0" or ball_x >= WIDTH) then if (ball_x <= MIN_X or ball_x >= MAX_X) then
ball_vx <= -ball_vx; ball_vx <= -ball_vx;
end if; end if;
if (ball_y <= "0" or ball_y >= HEIGHT) then if (ball_y <= MIN_Y or ball_y >= MAX_Y) then
ball_vy <= -ball_vy; ball_vy <= -ball_vy;
end if; end if;
-- LOST CONDITION -- LOST CONDITION
game_over <= '1' when ball_y >= HEIGHT and game_over <= '1' when ball_y >= MAX_Y and
(ball_x < paddle or ball_x > paddle+PADDLE_WIDTH) else '0'; (ball_x < paddle or ball_x > paddle+PADDLE_WIDTH) else '0';
-- MAPPING TO OUTPUTS -- MAPPING TO OUTPUTS (shifting for pixel)
ball_x_position <= ball_x; ball_x_position <= ball_x (16 downto 5);
ball_y_position <= ball_y; ball_y_position <= ball_y (16 downto 5);
paddle_position <= paddle; paddle_position <= paddle (16 downto 5);
end if; end if;
end process; end process;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment