-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtop_level.vhd
More file actions
362 lines (319 loc) · 10.7 KB
/
top_level.vhd
File metadata and controls
362 lines (319 loc) · 10.7 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
LIBRARY ieee;
USE ieee.STD_LOGIC_1164.ALL;
USE ieee.numeric_std.ALL;
USE wORk.declarations.ALL;
ENTITY top_level IS
PORT(
CLOCK_50, rst : IN STD_LOGIC;
SW : IN STD_LOGIC_VECTOR(9 DOWNTO 0);
GPIO_1 : IN STD_LOGIC_VECTOR(35 DOWNTO 0);
VGA_R, VGA_B, VGA_G : OUT STD_LOGIC_VECTOR(7 DOWNTO 0);
VGA_CLK, VGA_SYNC_N, VGA_BLANK_N : OUT STD_LOGIC;
VGA_VS, VGA_HS : OUT STD_LOGIC;
-- FOR testINg
LEDR : OUT STD_LOGIC_VECTOR(9 DOWNTO 0)
);
END top_level;
ARCHITECTURE impl OF top_level IS
SIGNAL hpos, vpos : INTEGER;
SIGNAL hsync, vsync, clock25, ff1_OUT, ff2_OUT, pixel_on_field, pixel_on_text, END_game : STD_LOGIC;
SIGNAL mode, mode_temp : STD_LOGIC_VECTOR(1 DOWNTO 0) := "00";
SIGNAL tank1_x, tank1_y : INTEGER;
SIGNAL tank2_x, tank2_y : INTEGER;
SIGNAL cpu1_x, cpu1_y : INTEGER;
SIGNAL cpu2_x, cpu2_y : INTEGER;
SIGNAL pixel_on_tank1 : STD_LOGIC;
SIGNAL pixel_on_tank2 : STD_LOGIC;
SIGNAL pixel_on_cpu_tank1 : STD_LOGIC;
SIGNAL pixel_on_cpu_tank2 : STD_LOGIC;
SIGNAL pixel_on_bullet : STD_LOGIC;
-- Following signals are for the mode logic
SIGNAL pixel_on_field_s : STD_LOGIC;
SIGNAL pixel_on_tank1_s, pixel_on_tank2_s, pixel_on_cpu_tank1_s, pixel_on_cpu_tank2_s, pixel_on_bullet_s : STD_LOGIC;
-- Tank stuff
SIGNAL tank1_dir, tank2_dir : INTEGER := 0;
SIGNAL hit_tank : STD_LOGIC := '0';
signal tank1_bullet1_x : integer := 0;
signal tank1_bullet1_y : integer := 0;
signal tank1_bullet2_x : integer := 0;
signal tank1_bullet2_y : integer := 0;
signal tank1_bullet3_x : integer := 0;
signal tank1_bullet3_y : integer := 0;
signal tank2_bullet1_x : integer := 0;
signal tank2_bullet1_y : integer := 0;
signal tank2_bullet2_x : integer := 0;
signal tank2_bullet2_y : integer := 0;
signal tank2_bullet3_x : integer := 0;
signal tank2_bullet3_y : integer := 0;
signal cpu1_bullet1_x : integer := 0;
signal cpu1_bullet1_y : integer := 0;
signal cpu1_bullet2_x : integer := 0;
signal cpu1_bullet2_y : integer := 0;
signal cpu1_bullet3_x : integer := 0;
signal cpu1_bullet3_y : integer := 0;
signal cpu2_bullet1_x : integer := 0;
signal cpu2_bullet1_y : integer := 0;
signal cpu2_bullet2_x : integer := 0;
signal cpu2_bullet2_y : integer := 0;
signal cpu2_bullet3_x : integer := 0;
signal cpu2_bullet3_y : integer := 0;
SIGNAL cpu1_tank_x_start : INTEGER;
SIGNAL cpu1_tank_y_start : INTEGER;
SIGNAL cpu2_tank_x_start : INTEGER;
SIGNAL cpu2_tank_y_start : INTEGER;
SIGNAL tank1_x_start : INTEGER;
SIGNAL tank1_y_start : INTEGER;
SIGNAL tank2_x_start : INTEGER;
SIGNAL tank2_y_start : INTEGER;
BEGIN
END_game <= hit_tank;
LEDR <= SW; -- debug
-- Clock
clk_div:PROCESS(CLOCK_50) BEGIN
IF (rst = '0') THEN
clock25 <= '0';
ELSIF (CLOCK_50'event AND CLOCK_50 = '1')then
clock25 <= not clock25;
END IF;
END PROCESS;
--VGA SIGNAL that must be active
VGA_SYNC_N <= '1';
VGA_BLANK_N <= '1';
VGA_CLK <= clock25;
ff1 : vDFF PORT MAP(hsync, clock25, '1', '0', rst, ff1_OUT);
ff2 : vDFF PORT MAP(vsync, clock25, '1', '0', rst, ff2_OUT);
ff3 : vDFF PORT MAP(ff1_OUT, clock25, '1', '0', rst, VGA_HS);
ff4 : vDFF PORT MAP(ff2_OUT, clock25, '1', '0', rst, VGA_VS);
--COMPONENTs PORT MAP
vga_SIGNALs : vga_management PORT MAP(
clk => clock25, rst => rst,
hs => hsync, vs => vsync,
hpos => hpos, vpos => vpos
);
dISplay_colORs : vgacolOR PORT MAP(
clk => clock25, rst => rst,
pixel_on_field => pixel_on_field,
pixel_on_text => pixel_on_text,
pixel_on_tank1 => pixel_on_tank1,
pixel_on_tank2 => pixel_on_tank2,
pixel_on_cpu_tank1 => pixel_on_cpu_tank1,
pixel_on_cpu_tank2 => pixel_on_cpu_tank2,
pixel_on_bullet => pixel_on_bullet,
red => VGA_R, blue => VGA_B, green => VGA_G
);
menu : text_management PORT MAP(
clk => clock25, rst => rst,
hpos => hpos, vpos => vpos,
mode => mode,
pixel_on_text => pixel_on_text
);
modes : ModeFSM PORT MAP(
clk => clock25, rstn => rst,
END_game => END_game, SW => SW,
GPIO_1 => GPIO_1,
mode => mode_temp
);
game_field : field PORT MAP(
clk => clock25, rstn => rst,
xscan => hpos, yscan => vpos,
mode => mode,
flag => pixel_on_field_s
);
tank1 : tank port map(
clk => clock25,
rstn => rst,
xscan => hpos,
yscan => vpos,
x_pixel_ref => tank1_x,
y_pixel_ref => tank1_y,
x_start => tank1_x_start,
y_start => tank1_y_start,
SW_LEFT => not GPIO_1(13),
SW_RIGHT => not GPIO_1(17),
SW_FORWARD => not GPIO_1(15),
SW_SHOOT => not GPIO_1(11),
mode => mode,
flag => pixel_on_tank1_s,
dir_out => tank1_dir,
bullet1_x => tank1_bullet1_x,
bullet1_y => tank1_bullet1_y,
bullet2_x => tank1_bullet2_x,
bullet2_y => tank1_bullet2_y,
bullet3_x => tank1_bullet3_x,
bullet3_y => tank1_bullet3_y
);
tank2 : tank port map(
clk => clock25,
rstn => rst,
xscan => hpos,
yscan => vpos,
x_pixel_ref => tank2_x,
y_pixel_ref => tank2_y,
x_start => tank2_x_start,
y_start => tank2_y_start,
SW_LEFT => not GPIO_1(3),
SW_RIGHT => not GPIO_1(7),
SW_FORWARD => not GPIO_1(5),
SW_SHOOT => not GPIO_1(1),
mode => mode,
flag => pixel_on_tank2_s,
dir_out => tank2_dir,
bullet1_x => tank2_bullet1_x,
bullet1_y => tank2_bullet1_y,
bullet2_x => tank2_bullet2_x,
bullet2_y => tank2_bullet2_y,
bullet3_x => tank2_bullet3_x,
bullet3_y => tank2_bullet3_y
);
cpu_tank1 : cpu_tank PORT MAP(
clk => clock25,
rstn => rst,
xscan => hpos,
yscan => vpos,
x_pixel_ref => cpu1_x,
y_pixel_ref => cpu1_y,
player1_x_pixel_ref => tank1_x,
player1_y_pixel_ref => tank1_y,
player2_x_pixel_ref => tank2_x,
player2_y_pixel_ref => tank2_y,
x_start => cpu1_tank_x_start,
y_start => cpu1_tank_y_start,
mode => mode,
flag => pixel_on_cpu_tank1_s,
bullet1_x => cpu1_bullet1_x,
bullet1_y => cpu1_bullet1_y,
bullet2_x => cpu1_bullet2_x,
bullet2_y => cpu1_bullet2_y,
bullet3_x => cpu1_bullet3_x,
bullet3_y => cpu1_bullet3_y
);
cpu_tank2 : cpu_tank PORT MAP(
clk => clock25,
rstn => rst,
xscan => hpos,
yscan => vpos,
x_pixel_ref => cpu2_x,
y_pixel_ref => cpu2_y,
player1_x_pixel_ref => tank1_x,
player1_y_pixel_ref => tank1_y,
player2_x_pixel_ref => tank2_x,
player2_y_pixel_ref => tank2_y,
x_start => cpu2_tank_x_start,
y_start => cpu2_tank_y_start,
mode => mode,
flag => pixel_on_cpu_tank2_s,
bullet1_x => cpu2_bullet1_x,
bullet1_y => cpu2_bullet1_y,
bullet2_x => cpu2_bullet2_x,
bullet2_y => cpu2_bullet2_y,
bullet3_x => cpu2_bullet3_x,
bullet3_y => cpu2_bullet3_y
);
-- Other Logic
PROCESS(clock25, mode) BEGIN
IF rising_edge(clock25) THEN
mode <= mode_temp;
IF (mode_temp = ONE_CPU_GAME) THEN
tank1_x_start <= 80;
tank1_y_start <= 80;
tank2_x_start <= 80;
tank2_y_start <= 400;
cpu1_tank_x_start <= 500;
cpu1_tank_y_start <= 200;
cpu2_tank_x_start <= -1000;
cpu2_tank_y_start <= -1000;
pixel_on_field <= pixel_on_field_s;
pixel_on_tank1 <= pixel_on_tank1_s;
pixel_on_tank2 <= pixel_on_tank2_s;
pixel_on_cpu_tank1 <= pixel_on_cpu_tank1_s;
pixel_on_cpu_tank2 <= '0';
pixel_on_bullet <= pixel_on_bullet_s;
ELSIF (mode_temp = TWO_CPU_GAME) THEN
tank1_x_start <= 80;
tank1_y_start <= 80;
tank2_x_start <= 80;
tank2_y_start <= 400;
cpu1_tank_x_start <= 500;
cpu1_tank_y_start <= 80;
cpu2_tank_x_start <= 500;
cpu2_tank_y_start <= 400;
pixel_on_field <= pixel_on_field_s;
pixel_on_tank1 <= pixel_on_tank1_s;
pixel_on_tank2 <= pixel_on_tank2_s;
pixel_on_cpu_tank1 <= pixel_on_cpu_tank1_s;
pixel_on_cpu_tank2 <= pixel_on_cpu_tank2_s;
pixel_on_bullet <= pixel_on_bullet_s;
ELSE
tank1_x_start <= -10000;
tank1_y_start <= -10000;
tank2_x_start <= 10000;
tank2_y_start <= 10000;
cpu1_tank_x_start <= -100000;
cpu1_tank_y_start <= -100000;
cpu2_tank_x_start <= 100000;
cpu2_tank_y_start <= 100000;
pixel_on_field <= '0';
pixel_on_tank1 <= '0';
pixel_on_tank2 <= '0';
pixel_on_cpu_tank1 <= '0';
pixel_on_cpu_tank2 <= '0';
pixel_on_bullet <= '0';
END IF;
END IF;
END PROCESS;
-- Check if bullet hits tanks
process(clock25) begin
-- Reset hit_tank signal
hit_tank <= '0';
-- Check if tank1's bullet hits tank2
if (tank1_bullet1_x >= tank2_x and tank1_bullet1_x <= tank2_x + 30 and
tank1_bullet1_y >= tank2_y and tank1_bullet1_y <= tank2_y + 30) then
hit_tank <= '1';
end if;
-- Check if tank1's bullet hits CPU1
if (tank1_bullet1_x >= cpu1_x and tank1_bullet1_x <= cpu1_x + 30 and
tank1_bullet1_y >= cpu1_y and tank1_bullet1_y <= cpu1_y + 30) then
hit_tank <= '1';
end if;
-- Check if tank1's bullet hits CPU2
if (tank1_bullet1_x >= cpu2_x and tank1_bullet1_x <= cpu2_x + 30 and
tank1_bullet1_y >= cpu2_y and tank1_bullet1_y <= cpu2_y + 30) then
hit_tank <= '1';
end if;
-- Check if tank2's bullet hits tank1
if (tank2_bullet1_x >= tank1_x and tank2_bullet1_x <= tank1_x + 30 and
tank2_bullet1_y >= tank1_y and tank2_bullet1_y <= tank1_y + 30) then
hit_tank <= '1';
end if;
-- Check if tank2's bullet hits CPU1
if (tank2_bullet1_x >= cpu1_x and tank2_bullet1_x <= cpu1_x + 30 and
tank2_bullet1_y >= cpu1_y and tank2_bullet1_y <= cpu1_y + 30) then
hit_tank <= '1';
end if;
-- Check if tank2's bullet hits CPU2
if (tank2_bullet1_x >= cpu2_x and tank2_bullet1_x <= cpu2_x + 30 and
tank2_bullet1_y >= cpu2_y and tank2_bullet1_y <= cpu2_y + 30) then
hit_tank <= '1';
end if;
-- Check if CPU1's bullet hits tank1
if (cpu1_bullet1_x >= tank1_x and cpu1_bullet1_x <= tank1_x + 30 and
cpu1_bullet1_y >= tank1_y and cpu1_bullet1_y <= tank1_y + 30) then
hit_tank <= '1';
end if;
-- Check if CPU1's bullet hits tank2
if (cpu1_bullet1_x >= tank2_x and cpu1_bullet1_x <= tank2_x + 30 and
cpu1_bullet1_y >= tank2_y and cpu1_bullet1_y <= tank2_y + 30) then
hit_tank <= '1';
end if;
-- Check if CPU2's bullet hits tank1
if (cpu2_bullet1_x >= tank1_x and cpu2_bullet1_x <= tank1_x + 30 and
cpu2_bullet1_y >= tank1_y and cpu2_bullet1_y <= tank1_y + 30) then
hit_tank <= '1';
end if;
-- Check if CPU2's bullet hits tank2
if (cpu2_bullet1_x >= tank2_x and cpu2_bullet1_x <= tank2_x + 30 and
cpu2_bullet1_y >= tank2_y and cpu2_bullet1_y <= tank2_y + 30) then
hit_tank <= '1';
end if;
end process;
END impl;