-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_pong.vhd
221 lines (196 loc) · 6.23 KB
/
led_pong.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
--
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
--
entity led_pong is
generic(
NUM_LEDS: integer := 10;
WIN_SCORE: integer := 5; -- max 9
FCLK: integer := 50_000_000
);
port(
-- ins
buttonLeftPlayer, buttonRightPlayer: in std_logic;
speedSwitches: in std_logic_vector(3 downto 0);
autoSpeed: in std_logic;
clock, reset: in std_logic;
-- outs
leds: out std_logic_vector(0 to NUM_LEDS - 1);
scoreLeftPlayerSSD, scoreRightPlayerSSD: out std_logic_vector(6 downto 0)
);
end entity;
--
architecture arch of led_pong is
signal buttonLeftPlayerDebounced, buttonRightPlayerDebounced: std_logic;
-- Get ball position and reset positions
signal resetBall: std_logic;
signal resetPosition: integer range 0 to NUM_LEDS - 1 := 0;
signal resetDirection: std_logic;
signal currentPosition: integer range resetPosition'range;
signal enableBall: std_logic := '0';
-- Scores
signal scoreLeft, scoreRight: integer range 0 to WIN_SCORE;
signal isGameOver: std_logic;
signal hideLeftScore, hideRightScore: std_logic;
-- Game logic states
type state is (ServeLeft, ServeRight, MovingLeft, MovingRight,
BallOnLeft, BallOnRight, LoseLeft, LoseRight);
signal presState, nextState: state;
-- Game logic "inner outputs"
signal leftLose, rightLose: std_logic := '0';
constant BTN_PRESSED: std_logic := '1';
begin
-- debounce buttons
dbleft: entity work.debouncer generic map (DEBOUNCE_TIME => FCLK / 10)
port map(button => not buttonLeftPlayer, clock => clock, buttonDebouncedPulse => buttonLeftPlayerDebounced);
dbright: entity work.debouncer generic map (DEBOUNCE_TIME => FCLK / 10)
port map(button => not buttonRightPlayer, clock => clock, buttonDebouncedPulse => buttonRightPlayerDebounced);
-- Connect signals to LED RUNNER (used to display ball/led moviment)
ledrunner: entity work.led_runner generic map (FCLK => FCLK, NUM_LEDS => NUM_LEDS)
port map(
-- ins
speed => speedSwitches,
autoSpeed => autoSpeed,
enable => enableBall,-- and not gameOver,
reset => resetBall,
resetPosition => resetPosition,
resetDirection => resetDirection,
clock => clock,
-- outs
leds => leds,
position => currentPosition
);
-- Wire scoreboards
sbleft: entity work.bin2ssd port map(conv_std_logic_vector(scoreLeft, 4), hideLeftScore, scoreLeftPlayerSSD);
sbright: entity work.bin2ssd port map(conv_std_logic_vector(scoreRight, 4), hideRightScore, scoreRightPlayerSSD);
-- Game logic FSM
process(clock, reset)
begin
if reset = '1' or isGameOver = '1' then
presState <= ServeLeft;
elsif rising_edge(clock) then
presState <= nextState;
end if;
end process;
-- State logic
process(all)
begin
case presState is
when ServeLeft =>
leftLose <= '0';
rightLose <= '0';
resetBall <= '1'; resetPosition <= 0; resetDirection <= '0';
enableBall <= '0';
if buttonLeftPlayerDebounced = BTN_PRESSED then
nextState <= MovingRight;
else
nextState <= ServeLeft;
end if;
when ServeRight =>
leftLose <= '0';
rightLose <= '0';
resetBall <= '1'; resetPosition <= NUM_LEDS - 1; resetDirection <= '1';
enableBall <= '0';
if buttonRightPlayerDebounced = BTN_PRESSED then
nextState <= MovingLeft;
else
nextState <= ServeRight;
end if;
when MovingLeft =>
leftLose <= '0';
rightLose <= '0';
resetBall <= '0'; resetPosition <= 0; resetDirection <= '0';
enableBall <= '1';
if buttonLeftPlayerDebounced = BTN_PRESSED then
nextState <= LoseLeft;
elsif buttonRightPlayerDebounced = BTN_PRESSED then
nextState <= LoseRight;
elsif currentPosition = 0 then
nextState <= BallOnLeft;
else
nextState <= MovingLeft;
end if;
when MovingRight =>
leftLose <= '0';
rightLose <= '0';
resetBall <= '0'; resetPosition <= 0; resetDirection <= '0';
enableBall <= '1';
if buttonLeftPlayerDebounced = BTN_PRESSED then
nextState <= LoseLeft;
elsif buttonRightPlayerDebounced = BTN_PRESSED then
nextState <= LoseRight;
elsif currentPosition = NUM_LEDS - 1 then
nextState <= BallOnRight;
else
nextState <= MovingRight;
end if;
when BallOnLeft =>
leftLose <= '0';
rightLose <= '0';
resetBall <= '0'; resetPosition <= 0; resetDirection <= '0';
enableBall <= '1';
if buttonLeftPlayerDebounced = BTN_PRESSED then
nextState <= MovingRight;
elsif buttonRightPlayerDebounced = BTN_PRESSED then
nextState <= LoseRight;
elsif currentPosition = 1 then
nextState <= LoseLeft;
else
nextState <= BallOnLeft;
end if;
when BallOnRight =>
leftLose <= '0';
rightLose <= '0';
resetBall <= '0'; resetPosition <= 0; resetDirection <= '0';
enableBall <= '1';
if buttonLeftPlayerDebounced = BTN_PRESSED then
nextState <= LoseLeft;
elsif buttonRightPlayerDebounced = BTN_PRESSED then
nextState <= MovingLeft;
elsif currentPosition = NUM_LEDS - 2 then
nextState <= LoseRight;
else
nextState <= BallOnRight;
end if;
when LoseLeft =>
leftLose <= '1';
rightLose <= '0';
resetBall <= '1'; resetPosition <= 0; resetDirection <= '0';
enableBall <= '0';
nextState <= ServeLeft;
when LoseRight =>
leftLose <= '0';
rightLose <= '1';
resetBall <= '1'; resetPosition <= NUM_LEDS - 1; resetDirection <= '1';
enableBall <= '0';
nextState <= ServeRight;
end case;
end process;
-- Process user losing signals
process(clock, reset, leftLose, rightLose)
begin
if reset = '1' then
scoreLeft <= 0;
scoreRight <= 0;
isGameOver <= '0';
hideLeftScore <= '0';
hideRightScore <= '0';
elsif rising_edge(clock) then
if leftLose = '1' then
scoreRight <= scoreRight + 1;
if scoreRight = WIN_SCORE - 1 then
hideLeftScore <= '1';
isGameOver <= '1';
end if;
elsif rightLose = '1' then
scoreLeft <= scoreLeft + 1;
if scoreLeft = WIN_SCORE - 1 then
hideRightScore <= '1';
isGameOver <= '1';
end if;
end if;
end if;
end process;
end architecture;
--