-
Notifications
You must be signed in to change notification settings - Fork 7
/
image_generator.vhd
401 lines (290 loc) · 8.82 KB
/
image_generator.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
library ieee;
use ieee.std_logic_1164.all;
entity image_generator is
generic(
Ha: integer := 96;
Hb: integer := 144;
Hc: integer := 784;
Hd: integer := 800;
Va: integer := 2;
Vb: integer := 35;
Vc: integer := 515;
Vd: integer := 525;
PVsize: integer := 50;
PHsize: integer := 10;
BallSize: integer := 50);
port(
pixel_clk : in std_logic;
paddle_clk : in std_logic;
ball_clk : in std_logic;
reset : in std_logic;
Hactive, Vactive : in std_logic;
Hsync, Vsync : in std_logic;
dena : in std_logic;
direction_switch : in std_logic_vector(3 downto 0);
start_game : in std_logic;
score1 : buffer integer;
score2 : buffer integer;
R,G,B : out std_logic_vector(3 downto 0));
end image_generator;
architecture image_generator_arch of image_generator is
--Pixel counters
signal row_counter : integer range 0 to Vc;
signal col_counter : integer range 0 to Hc;
--Paddle positions
signal paddle1_pos_x : integer range 0 to Hc;
signal paddle2_pos_x : integer range 0 to Hc;
signal paddle1_pos_y : integer range 0 to Vc;
signal paddle2_pos_y : integer range 0 to Vc;
--Position and direction of the ball
signal Ball_pos_x : integer range 0 to Hc;
signal Ball_pos_y : integer range 0 to Vc;
signal Ball_direction : integer range 0 to 5;
--States of the game
type state_type is (S0, S1);
signal state: state_type;
signal move: std_logic;
begin
--Pixel counters to represent the image-----------
process(pixel_clk, Hactive, Vactive, Hsync, Vsync)
begin
if(reset = '0') then
row_counter <= 0;
elsif(Vsync = '0') then
row_counter <= 0;
elsif(Hsync'event and Hsync = '1') then
if(Vactive = '1') then
row_counter <= row_counter + 1;
end if;
end if;
if(reset = '0') then
col_counter <= 0;
elsif(Hsync = '0') then
col_counter <= 0;
elsif(pixel_clk'event and pixel_clk = '1') then
if(Hactive = '1') then
col_counter <= col_counter + 1;
end if;
end if;
end process;
---Paddle movements--------------------------------
process(paddle_clk, reset, direction_switch)
begin
if(reset = '0') then
paddle1_pos_X <= 50;
paddle1_pos_y <= 240;
paddle2_pos_x <= 590;
paddle2_pos_y <= 240;
elsif(paddle_clk'event and paddle_clk = '1') then
paddle1_pos_x <= 50;
paddle2_pos_x <= 590;
--Movement paddle 1
if(direction_switch(0) = '1') then
if(paddle1_pos_y = Vc - Vb) then
paddle1_pos_y <= 0;
else paddle1_pos_y <= paddle1_pos_y + 1;
end if;
end if;
if(direction_switch(1) = '1') then
if(paddle1_pos_y = 0) then
paddle1_pos_y <= Vc - Vb;
else paddle1_pos_y <= paddle1_pos_y - 1;
end if;
end if;
--Movement paddle 2
if(direction_switch(2) = '1') then
if(paddle2_pos_y = Vc - Vb) then
paddle2_pos_y <= 0;
else paddle2_pos_y <= paddle2_pos_y + 1;
end if;
end if;
if(direction_switch(3) = '1') then
if(paddle2_pos_y = 0) then
paddle2_pos_y <= Vc - Vb;
else paddle2_pos_y <= paddle2_pos_y - 1;
end if;
end if;
end if;
end process;
---Position and direction of the ball-----------
process(ball_clk, reset, Ball_direction, move)
begin
if(reset = '0' or move = '0') then
Ball_pos_x <= 320;
Ball_pos_y <= 240;
Ball_direction <= Ball_direction + 1;
if(Ball_direction > 5) then
Ball_direction <= 0;
end if;
elsif(ball_clk'event and ball_clk = '1') then
case Ball_direction is
-- Direcciones, 6 en total, 4 diagonales y 2 horizontales
when 0 => Ball_pos_x <= Ball_pos_x + 1;
Ball_pos_y <= Ball_pos_y - 1;
when 1 => Ball_pos_x <= Ball_pos_x - 1;
Ball_pos_y <= Ball_pos_y - 1;
when 2 => Ball_pos_x <= Ball_pos_x - 1;
Ball_pos_y <= Ball_pos_y + 1;
when 3 => Ball_pos_x <= Ball_pos_x + 1;
Ball_pos_y <= Ball_pos_y + 1;
when 4 => Ball_pos_x <= Ball_pos_x + 1;
when 5 => Ball_pos_x <= Ball_pos_x - 1;
end case;
--Bounce with the board edges
if(Ball_pos_y = 0) then
if(Ball_direction = 0) then
Ball_direction <= 3;
elsif(Ball_direction = 1) then
Ball_direction <= 2;
end if;
end if;
if(Ball_pos_y = 480) then
if(Ball_direction = 2) then
Ball_direction <= 1;
elsif(Ball_direction = 3) then
Ball_direction <= 0;
end if;
end if;
if(Ball_pos_x = 0) then
if(Ball_direction = 1) then
Ball_direction <= 0;
elsif(Ball_direction = 2) then
Ball_direction <= 3;
elsif(Ball_direction = 5) then
Ball_direction <= 4;
end if;
end if;
if(Ball_pos_x = 640) then
if(Ball_direction = 0) then
Ball_direction <= 1;
elsif(Ball_direction = 3) then
Ball_direction <= 2;
elsif(Ball_direction = 4) then
Ball_direction <= 5;
end if;
end if;
--Bounces with the paddles
if(Ball_pos_x + BallSize > paddle2_pos_x - PHsize) then
if(Ball_pos_y - BallSize <= paddle2_pos_y + PVsize and
Ball_pos_y + BallSize >= paddle2_pos_y - PVsize) then
if(Ball_pos_y >= paddle2_pos_y - 10 and
Ball_pos_y <= paddle2_pos_y + 10) then
Ball_direction <= 5;
else
if(Ball_direction = 0) then
Ball_direction <= 1;
elsif(Ball_direction = 3) then
Ball_direction <= 2;
elsif(Ball_direction = 4) then
if(Ball_pos_y > paddle2_pos_y) then
Ball_direction <= 2;
else
Ball_direction <= 1;
end if;
end if;
end if;
end if;
end if;
if(Ball_pos_x - BallSize < paddle1_pos_x + PHsize) then
if(Ball_pos_y - BallSize <= paddle1_pos_y + PVsize and
Ball_pos_y + BallSize >= paddle1_pos_y - PVsize) then
if(Ball_pos_y >= paddle1_pos_y - 10 and
Ball_pos_y <= paddle1_pos_y + 10) then
Ball_direction <= 4;
else
if(Ball_direction = 1) then
Ball_direction <= 0;
elsif(Ball_direction = 2) then
Ball_direction <= 3;
elsif(Ball_direction = 5) then
if(Ball_pos_y > paddle1_pos_y) then
Ball_direction <= 3;
else
Ball_direction <= 0;
end if;
end if;
end if;
end if;
end if;
end if;
end process;
---State Machine of the game-----------------
process(pixel_clk, reset)
begin
if(reset = '0') then
state <= S0;
score1 <= 0;
score2 <= 0;
elsif(pixel_clk'event and pixel_clk = '1') then
case state is
when S0 =>
if(start_game = '0') then
State <= S1;
end if;
when S1 =>
if(Ball_pos_x < 40) then
State <= S0;
if(score2 = 4) then
score2 <= 0;
score1 <= 0;
else
score2 <= score2 + 1;
end if;
end if;
if(Ball_pos_x > 600) then
State <= S0;
if(score1 = 4) then
score1 <= 0;
score2 <= 0;
else
score1 <= score1 + 1;
end if;
end if;
end case;
end if;
end process;
process(State)
begin
case State is
when S0 => move <= '0';
when S1 => move <= '1';
end case;
end process;
---Image generator--------------------
process(paddle1_pos_x, paddle1_pos_y, paddle2_pos_x, paddle2_pos_y, dena, row_counter, col_counter)
begin
--Signal that enables to display data on the screen
if(dena = '1') then
-- Paddle 1 detection
if((paddle1_pos_x <= col_counter + PHsize) and
(paddle1_pos_x + PHsize >= col_counter) and
(paddle1_pos_y <= row_counter + PVsize) and
(paddle1_pos_y + PVsize >= row_counter)) or
-- Paddle 2 detection
((paddle2_pos_x <= col_counter + PHsize) and
(paddle2_pos_x + PHsize >= col_counter) and
(paddle2_pos_y <= row_counter + PVsize) and
(paddle2_pos_y + PVsize >= row_counter)) or
-- Ball detection
((Ball_pos_x <= col_counter + BallSize) and
(Ball_pos_X + BallSize >= col_counter) and
(Ball_pos_y <= row_counter + BallSize) and
(Ball_pos_y + BallSize >= row_counter)) then
-- Paddle and ball color
R <= "1111";
G <= "1111";
B <= "1111";
else
-- Background color
R <= "0000";
G <= "0000";
B <= "0000";
end if;
else
-- If dena = 0, no color has to be displayed
R <= (others => '0');
G <= (others => '0');
B <= (others => '0');
end if;
end process;
end image_generator_arch;