blob: 2a5c187376493753478ffb2ec086cd62d57d71cc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity bounce is
-- bounce motion
port (
clk, reset: in std_logic;
x, y: out std_logic_vector(9 downto 0));
end bounce;
architecture Behavioral of bounce is
-- direction[1]: 0 = right, 1 = left
-- direction[0]: 0 = down, 1 = up
signal direction: std_logic_vector(1 downto 0) := "00";
constant velocity: natural := 1;
-- current square position
signal temp_x, temp_y: std_logic_vector(9 downto 0) := (others => '0');
begin
x <= temp_x;
y <= temp_y;
process(clk, reset)
begin
if reset = '1' then
-- async reset
direction <= "00"; -- reset direction to bottom right
temp_x <= (others => '0');
temp_y <= (others => '0');
elsif rising_edge(clk) then
if direction(0) = '0' then
temp_x <= temp_x + velocity; -- move left
if (temp_x + velocity) > 630 then
direction(0) <= '1';
end if;
else
temp_x <= temp_x - velocity; -- move right
if (temp_x - velocity) <= 0 then
direction(0) <= '0';
end if;
end if;
if direction(1) = '0' then
temp_y <= temp_y + 1; -- move down
if (temp_y + velocity) > 470 then
direction(1) <= '1';
end if;
else
temp_y <= temp_y - 1; -- move up
if (temp_y - velocity) <= 0 then
direction(1) <= '0';
end if;
end if;
end if;
end process;
end Behavioral;
|