-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_fsm.vhd
90 lines (80 loc) · 1.98 KB
/
game_fsm.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
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_signed.all;
entity game_fsm is
port (
clk, reset, PB1, PB2, SW_pause : in std_logic;
hit : in std_logic;
game_reset, game_pause : out std_logic;
selected_mode : out std_logic_vector(1 downto 0)
);
end entity;
architecture fsm of game_fsm is
-- FSM signals
type state_type is (s_menu, s_regular, s_training, s_over);
signal state, next_state : state_type := s_menu;
begin
sync_proc : process(clk, reset) --synchronously move to next state
begin
if (reset = '1') then
state <= s_menu;
elsif (rising_edge(clk)) then
state <= next_state;
end if;
end process;
next_states_fn: process(state, PB1, PB2, SW_pause, hit) --asynchronously decide next state based only on current state and inputs
begin
case(state) is
when s_menu =>
if (PB1 = '0') then
next_state <= s_training;
elsif (PB2 = '0') then
next_state <= s_regular;
else
next_state <= s_menu;
end if;
when s_training =>
if (hit = '1') then
next_state <= s_over;
elsif (reset = '1') then -- Check if to remove
next_state <= s_menu;
else
next_state <= s_training;
end if;
when s_regular =>
if (hit = '1') then
next_state <= s_over;
elsif (reset = '1') then -- Check if to remove
next_state <= s_menu;
else
next_state <= s_regular;
end if;
when s_over =>
if (reset = '1') then
next_state <= s_menu;
else
next_state <= s_over;
end if;
end case;
game_pause <= SW_pause;
end process;
outputs:process(state)
begin
case state is
when s_menu =>
game_reset <= '1';
selected_mode <= "00";
when s_training =>
game_reset <= '0';
selected_mode <= "01";
when s_regular =>
game_reset <= '0';
selected_mode <= "10";
when s_over =>
game_reset <= '0';
selected_mode <= "11";
end case;
end process;
end architecture fsm;