-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_control.vhd
257 lines (225 loc) · 5.54 KB
/
game_control.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
-- 21/05 -> Checked keybord functionality
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.game_package.all;
entity game_control is
port (
CLOCK_50 : in std_logic;
PS2_DAT : inout STD_LOGIC;
PS2_CLK : inout STD_LOGIC;
SW : in std_logic_vector(9 downto 0);
HEX5 : out std_logic_vector(6 downto 0);
HEX4 : out std_logic_vector(6 downto 0);
HEX3 : out std_logic_vector(6 downto 0);
HEX2 : out std_logic_vector(6 downto 0);
HEX1 : out std_logic_vector(6 downto 0);
HEX0 : out std_logic_vector(6 downto 0);
KEY : in std_logic_vector(3 downto 0);
VGA_R, VGA_G, VGA_B : out std_logic_vector(7 downto 0);
VGA_HS, VGA_VS : out std_logic;
VGA_BLANK_N, VGA_SYNC_N : out std_logic;
VGA_CLK : out std_logic
);
end game_control ;
architecture rtl of game_control is
signal state, next_state : std_logic_vector (3 downto 0) := "0000";
signal enter_on : std_logic;
signal key_on, key_on_prev : std_logic_vector (2 downto 0);
signal key_pressed, key_number : std_logic_vector (7 downto 0);
signal key_code : std_logic_vector (47 downto 0);
signal n_players, t_cards, n_pairs : integer range 0 to 9;
signal n_cards : integer range 0 to 79;
signal game_table : vetor;
signal table_map_out: std_logic_vector (79 downto 0);
signal linha, coluna : integer range 0 to 9;
signal p1, p2, p3, p4, p5, p6 : std_logic_vector (3 downto 0);
signal pa, pb, pc, pd, pe, pf : std_logic_vector (3 downto 0);
-- Signals for block control.
signal reset_game : std_logic;
signal configure : std_logic := '0';
signal config_ready : std_logic;
signal seed_in : integer range 0 to 50000000;
signal set_table : std_logic := '0';
signal table_ready : std_logic;
signal play_game : std_logic := '0';
signal game_over : std_logic;
begin
reset_game <= not KEY(2);
vga : vga_ball
port map (
CLOCK_50,
KEY(1 downto 0),
VGA_R, VGA_G, VGA_B,
VGA_HS, VGA_VS,
VGA_BLANK_N, VGA_SYNC_N,
VGA_CLK,
game_table,
table_map_out,
linha, coluna,
n_cards
);
kbdex_ctrl_inst : kbdex_ctrl
generic map (
clkfreq => 50000
)
port map (
ps2_data => PS2_DAT,
ps2_clk => PS2_CLK,
clk => CLOCK_50,
en => '1',
resetn => '1',
lights => "000",
key_on => key_on,
key_code => key_code
);
kbd_alphanum_inst : kbd_alphanum
port map (
clk => CLOCK_50,
key_on => key_on,
key_code => key_code,
HEX1 => key_pressed(7 downto 4),
HEX0 => key_pressed(3 downto 0)
);
translate : ascii_2_num
port map (
key_pressed,
key_number,
enter_on
);
-- Bloco para configuracoes.
settings : config_table
port map (
CLOCK_50,
configure,
key_on,
key_number,
config_ready,
n_players, t_cards, n_pairs,
n_cards,
seed_in
);
-- Bloco para o preparo da mesa.
randomize : ready_table
port map (
reset_game,
CLOCK_50,
set_table,
table_ready,
t_cards,
n_cards,
seed_in,
game_table
);
-- Block para execucao do gameplay.
gameplay : play_table
port map (
reset_game,
CLOCK_50,
play_game,
enter_on,
key_on,
key_number,
game_over,
n_players,
n_cards,
game_table,
pa, pb, pc, pd, pe, pf,
table_map_out,
linha, coluna
);
process
variable l, c : integer range 0 to 100;
begin
wait until CLOCK_50'event and CLOCK_50 = '1';
if (reset_game = '1') then
set_table <= '0';
play_game <= '0';
next_state <= "0011";
else
case state is
when "0000" =>
-- Sinaliza "config_table" para executar.
configure <= '1';
next_state <= "0001";
when "0001" =>
-- Imprime opcoes de configuracao.
p1 <= std_logic_vector(to_unsigned(n_players mod 10, 4));
p2 <= "1111";
p3 <= std_logic_vector(to_unsigned(t_cards mod 10, 4));
p4 <= "1110";
p5 <= std_logic_vector(to_unsigned(n_cards mod 10, 4));
p6 <= std_logic_vector(to_unsigned(n_cards/10 mod 10, 4));
-- Aguarda "config_table" terminar de executar.
if (config_ready = '1') then
configure <= '0'; -- Sinaliza "config_table" que recebeu a resposta.
next_state <= "0010";
end if;
when "0010" =>
-- Pausa para o usuario ver as opcoes selecionadas.
if (enter_on = '1') then next_state <= "0011";
end if;
when "0011" =>
-- Sinaliza "ready_table" para executar.
set_table <= '1';
next_state <= "0100";
when "0100" =>
-- Aguarda "ready_table" terminar de executar.
if (table_ready = '1') then
set_table <= '0'; -- Sinaliza "ready_table" que recebeu a resposta.
next_state <= "0101";
end if;
when "0101" =>
-- Sinaliza "play_table" para executar.
play_game <= '1';
next_state <= "0110";
when "0110" =>
-- Recebe valores de "play_table" para o display.
p1 <= pa;
p2 <= pb;
p3 <= pc;
p4 <= pd;
p5 <= pe;
p6 <= pf;
-- Aguarda "play_table" terminar de executar.
if (game_over = '1') then
play_game <= '0'; -- Sinaliza "play_table" que recebeu a resposta.
next_state <= "1111";
end if;
when others => next_state <= "0000";
end case;
end if;
state <= next_state; -- Atualiza estado;
end process;
-- DISPLAYS PARA MOSTRAR AS OPCOES SELECIONADAS
print0 : bin2dec
port map (
p1,
HEX0
) ;
print1 : bin2dec
port map (
p2,
HEX1
) ;
print2 : bin2dec
port map (
p3,
HEX2
) ;
print3 : bin2dec
port map (
p4,
HEX3
) ;
print4 : bin2dec
port map (
p5,
HEX4
) ;
print5 : bin2dec
port map (
p6,
HEX5
) ;
end rtl;