-
Notifications
You must be signed in to change notification settings - Fork 6
/
Game.vhd
175 lines (150 loc) · 4.49 KB
/
Game.vhd
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.VgaUtils.all;
entity Game is
port (
clk : in std_logic; -- Pin 23, 50MHz from the onboard oscilator.
rgb : out std_logic_vector (2 downto 0); -- Pins 106, 105 and 104
hsync : out std_logic; -- Pin 101
vsync : out std_logic; -- Pin 103
up : in std_logic;
down : in std_logic;
left : in std_logic;
right : in std_logic
);
end entity Game;
architecture rtl of Game is
constant SQUARE_SIZE : integer := 30; -- In pixels
constant SQUARE_SPEED : integer := 100_000;
-- VGA Clock - 25 MHz clock derived from the 50MHz built-in clock
signal vga_clk : std_logic;
signal rgb_input, rgb_output : std_logic_vector(2 downto 0);
signal vga_hsync, vga_vsync : std_logic;
signal hpos, vpos : integer;
signal square_x : integer range HDATA_BEGIN to HDATA_END := HDATA_BEGIN + H_HALF - SQUARE_SIZE/2;
signal square_y : integer range VDATA_BEGIN to VDATA_END := VDATA_BEGIN + V_HALF - SQUARE_SIZE/2;
signal square_speed_count : integer range 0 to SQUARE_SPEED := 0;
signal up_debounced : std_logic;
signal down_debounced : std_logic;
signal left_debounced : std_logic;
signal right_debounced : std_logic;
signal move_square_en : std_logic;
signal should_move_square : boolean;
signal should_draw_square : boolean;
component VgaController is
port (
clk : in std_logic;
rgb_in : in std_logic_vector (2 downto 0);
rgb_out : out std_logic_vector (2 downto 0);
hsync : out std_logic;
vsync : out std_logic;
hpos : out integer;
vpos : out integer
);
end component;
component Debounce is
port (
i_Clk : in std_logic;
i_Switch : in std_logic;
o_Switch : out std_logic
);
end component;
begin
controller : VgaController port map(
clk => vga_clk,
rgb_in => rgb_input,
rgb_out => rgb_output,
hsync => vga_hsync,
vsync => vga_vsync,
hpos => hpos,
vpos => vpos
);
debounce_up_switch : Debounce port map(
i_Clk => vga_clk,
i_Switch => up,
o_Switch => up_debounced
);
debounce_down_switch : Debounce port map(
i_Clk => vga_clk,
i_Switch => down,
o_Switch => down_debounced
);
debounce_left_switch : Debounce port map(
i_Clk => vga_clk,
i_Switch => left,
o_Switch => left_debounced
);
debounce_right_switch : Debounce port map(
i_Clk => vga_clk,
i_Switch => right,
o_Switch => right_debounced
);
rgb <= rgb_output;
hsync <= vga_hsync;
vsync <= vga_vsync;
move_square_en <= up_debounced xor down_debounced xor left_debounced xor right_debounced;
should_move_square <= square_speed_count = SQUARE_SPEED;
Square(hpos, vpos, square_x, square_y, SQUARE_SIZE, should_draw_square);
-- We need 25MHz for the VGA so we divide the input clock by 2
process (clk)
begin
if (rising_edge(clk)) then
vga_clk <= not vga_clk;
end if;
end process;
process (vga_clk)
begin
if (rising_edge(vga_clk)) then
if (should_draw_square) then
rgb_input <= COLOR_GREEN;
else
rgb_input <= COLOR_BLACK;
end if;
end if;
end process;
process (vga_clk)
begin
if (rising_edge(vga_clk)) then
if (move_square_en = '1') then
if should_move_square then
square_speed_count <= 0;
else
square_speed_count <= square_speed_count + 1;
end if;
else
square_speed_count <= 0;
end if;
if (should_move_square) then
if (up_debounced = '0') then
if (square_y <= VDATA_BEGIN) then
square_y <= VDATA_BEGIN;
else
square_y <= square_y - 1;
end if;
end if;
if (down_debounced = '0') then
if (square_y >= VDATA_END - SQUARE_SIZE) then
square_y <= VDATA_END - SQUARE_SIZE;
else
square_y <= square_y + 1;
end if;
end if;
if (left_debounced = '0') then
if (square_x <= HDATA_BEGIN) then
square_x <= HDATA_BEGIN;
else
square_x <= square_x - 1;
end if;
end if;
if (right_debounced = '0') then
if (square_x >= HDATA_END - SQUARE_SIZE) then
square_x <= HDATA_END - SQUARE_SIZE;
else
square_x <= square_x + 1;
end if;
end if;
end if;
end if;
end process;
end architecture;