-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamle_logic.txt
176 lines (156 loc) · 5.19 KB
/
gamle_logic.txt
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
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.NUMERIC_STD.ALL;
entity game_logic is
port ( Clk : in std_logic;
player_input : in std_logic_vector (2 downto 0);
player_location : out std_logic_vector (2 downto 0);
obst_locs_1 : out std_logic_vector (31 downto 0);
obst_locs_2 : out std_logic_vector (31 downto 0);
obst_locs_3 : out std_logic_vector (31 downto 0);
score : out std_logic_vector (7 downto 0);
remaining_life : out std_logic_vector (1 downto 0);
dead_state : out std_logic);
end game_logic;
architecture Behavioral of game_logic is
constant clk_frequency : integer := 25270000;
constant game_frequency : integer := 8;
component shift_register
port ( game_clock : in std_logic;
obst_in : in std_logic;
dead : in std_logic;
reset : in std_logic;
path : out std_logic_vector (31 downto 0));
end component;
component Linear_Feedback_shift_reg
port (game_clock : in std_logic;
dead : in std_logic;
reset : in std_logic;
obst_out : out std_logic);
end component;
component select_lane
port (game_clock : in STD_LOGIC;
obst_in : in STD_LOGIC;
obstacle_1 : out STD_LOGIC;
obstacle_2 : out STD_LOGIC;
obstacle_3 : out STD_LOGIC);
end component;
signal enable_game_update : std_logic := '0';
signal lose_state : std_logic := '0';
signal current_player_location : std_logic_vector (2 downto 0) := "010";
signal path1, path2, path3 : std_logic_vector (31 downto 0);
signal current_obst : std_logic;
signal current_obstacle_1,current_obstacle_2,current_obstacle_3: std_logic;
signal lives_left : std_logic_vector (1 downto 0) := "11";
signal global_reset : std_logic;
signal current_score : std_logic_vector (7 downto 0) := "00000000";
begin
global_reset <= player_input(0);
lane1_sr : shift_register
port map ( game_clock => enable_game_update,
obst_in => current_obstacle_1,
dead => lose_state,
reset => global_reset,
path => path1);
lane2_sr : shift_register
port map ( game_clock => enable_game_update,
obst_in => current_obstacle_2,
dead => lose_state,
reset => global_reset,
path => path2);
lane3_sr : shift_register
port map ( game_clock => enable_game_update,
obst_in => current_obstacle_3,
dead => lose_state,
reset => global_reset,
path => path3);
obst_locs_1 <= path1;
obst_locs_2 <= path2;
obst_locs_3 <= path3;
obst_generator : Linear_Feedback_shift_reg
port map ( game_clock => enable_game_update,
dead => lose_state,
reset => global_reset,
obst_out => current_obst);
lane_selector : select_lane
port map ( game_clock => enable_game_update,
obst_in => current_obst,
obstacle_1 => current_obstacle_1,
obstacle_2 => current_obstacle_2,
obstacle_3 => current_obstacle_3);
process(Clk)
variable count : integer := 0;
begin
if (rising_edge(Clk)) then
enable_game_update <= '0';
count := (count + 1);
if (count = (clk_frequency / game_frequency)) then
enable_game_update <= '1';
count := 0;
end if;
end if;
end process;
process(enable_game_update, player_input)
begin
if (rising_edge(enable_game_update)) then
if ((current_player_location = "100") and (player_input = "010")) then
current_player_location <= "010";
elsif (current_player_location = "010") then
if (player_input = "100") then
current_player_location <= "100";
elsif (player_input = "010") then
current_player_location <= "001";
end if;
elsif (current_player_location = "001") then
if (player_input = "100") then
current_player_location <= "010";
end if;
end if;
player_location <= current_player_location;
end if;
end process player_movement;
process(enable_game_update)
begin
if (rising_edge(enable_game_update)) then
if (global_reset = '1') then
lives_left <= "11";
elsif ((current_player_location = "100" and (path1(0) = '1')) or
(current_player_location = "010" and (path2(0) = '1')) or
(current_player_location = "001" and (path3(0) = '1'))) then
lives_left <= (lives_left - 1);
end if;
end if;
end process;
process(enable_game_update)
variable count : integer := 0;
begin
if (rising_edge(enable_game_update)) then
if (global_reset = '1') then
current_score <= "00000000";
elsif (lives_left /= "00") then
count := count + 1;
if (count = 8) then
current_score <= current_score + 1;
count := 0;
end if;
end if;
end if;
end process;
score <= current_score;
process (enable_game_update)
begin
if (rising_edge(enable_game_update)) then
if (global_reset = '1') then
lose_state <= '0';
elsif (lives_left = "00") then
lose_state <= '1';
elsif (current_score <= "01100100") then
lose_state <= '0';
end if;
end if;
end process;
remaining_life <= lives_left;
dead_state <= lose_state;
end Behavioral;