-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalProject.vhd
353 lines (302 loc) · 10.1 KB
/
finalProject.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
-- Quartus II VHDL Template
-- Four-State Moore State Machine
-- A Moore machine's outputs are dependent only on the current state.
-- The output is written only when the state changes. (State
-- transitions are synchronous.)
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity finalProject is
port(
clkT : in std_logic;
resetT : in std_logic;
shiftLeftBtn : in std_logic;
shiftRightBtn : in std_logic;
up : in std_logic;
redOut : out std_logic_vector(3 downto 0);
greenOut : out std_logic_vector(3 downto 0);
blueOut : out std_logic_vector(3 downto 0);
hsyncT : out std_logic;
vsyncT : out std_logic;
stateAdd : out std_logic_vector(3 downto 0)
);
end entity;
architecture rtl of finalProject is
component vgaDriver
port
(
clk : in std_logic;
reset : in std_logic;
hsync : out std_logic;
vsync : out std_logic;
hcounter : out integer;
vcounter : out integer
);
end component;
signal hcounterT : integer;
signal vcounterT : integer;
signal column : integer;
signal row : integer;
signal counter : unsigned(26 downto 0);
signal sCounter : unsigned(27 downto 0);
signal sWaitCounter : unsigned(27 downto 0);
signal vgaClock : std_logic;
signal slowclock : std_logic;
constant blockSize : integer := 30;
constant zeros : std_logic_vector(11 downto 0) := (others=>'0');
constant redVec : std_logic_vector(11 downto 0) := "100100000000";
constant greenVec : std_logic_vector(11 downto 0) := "000010010000";
constant blueVec : std_logic_vector(11 downto 0) := "000000001001";
constant blockWidth : integer := 640/blockSize-1;
constant blockHeight : integer := 480/blockSize-1;
--A 2-d array declaration, from http://vhdlguru.blogspot.com/2010/02/arrays-and-records-in-vhdl.html
type rowBlocks is array (0 to blockHeight) of std_logic_vector(11 downto 0); -- 12 bit vector in each cell, 4 bits per color
type columnBlocks is array (0 to blockWidth) of rowBlocks;
signal blockGrid : columnBlocks;-- := --blockGrid is a row*column two dimensional array.
-- ((others=>(others=>'0')),(others=>(others=>'0')),(others=>(others=>'0')),(others=>(others=>'0')),
-- (others=>(others=>'0')),(others=>(others=>'0')),(others=>(others=>'0')),(others=>(others=>'0')),
-- (others=>(others=>'0')),(others=>(others=>'0')),(others=>(others=>'0')));
--game variables for recording player actions
type intArray is array(0 to 1) of integer;
signal activeBlock : intArray := (0 => blockWidth/2, 1 => 0);
signal prevBlock : intArray := (others=>0);
signal moveFlag : std_logic;
signal stateAddress : std_logic_vector(3 downto 0);
signal fallSpeed : integer := 26;
-- Build an enumerated type for the state machine
type state_type is (sStart, sCount, sStepDown,
sMoveLeft, sMoveRight, sMoveUp,
sWaitLeftUp, sWaitRightUp, sWaitUp,
sStepDownLeft, sStepDownRight, sStepDownUp);
-- Register to hold the current state
signal state : state_type;
begin
VGA: vgaDriver
port map(clk => vgaClock, reset => resetT,
hsync => hsyncT, vsync => vsyncT,
hcounter => hcounterT, vcounter => vcounterT);
column <= hcounterT - 138;
row <= vcounterT - 35;
--process to slow 50MHz clock to 25MHz
process (clkT)
begin
if (rising_edge(clkT)) then
counter <= counter + 1;
end if;
end process;
vgaClock <= std_logic(counter(0));
stateAdd <= stateAddress;
--process to slow set player actions
process (clkT, resetT)
begin
if resetT = '0' then
--reset all blocks to zeros.
for i in 0 to blockWidth loop
for j in 0 to blockHeight loop
blockGrid(i)(j) <= zeros;
end loop;
end loop;
activeBlock(0) <= 0;
activeBlock(1) <= 0;
sCounter <= "0000000000000000000000000000";
sWaitCounter <= "0000000000000000000000000000";
stateAddress <= "0000";
fallSpeed <= 26;
activeBlock(0) <= blockWidth/2;
activeBlock(1) <= 0;
state <= sStart;
elsif rising_edge(clkT) then
case state is
when sStart =>
stateAddress <= "0000";
blockGrid(activeBlock(0))(activeBlock(1)) <= redVec;
sCounter <= "0000000000000000000000000000";
if sWaitCounter(26) = '1' then
state <= sCount;
else
sWaitCounter <= sWaitCounter + 1;
state <= sStart;
end if;
when sCount =>
stateAddress <= "0001";
if sCounter(fallSpeed) = '1' then
state <= sStepDown;
elsif up = '0' then
state <= sMoveUp;
elsif shiftLeftBtn = '0' and activeBlock(0) /= 0 then
state <= sMoveLeft;
elsif shiftRightBtn = '0' and activeBlock(0) /= blockWidth then
state <= sMoveRight;
else
sCounter <= sCounter + 1;
state <= sCount;
end if;
when sStepDown =>
stateAddress <= "0010";
sCounter <= "0000000000000000000000000000";
if blockGrid(activeBlock(0))(activeBlock(1) + 1) /= zeros then
blockGrid(activeBlock(0))(activeBlock(1) + 1) <= blueVec;
else
blockGrid(activeBlock(0))(activeBlock(1) + 1) <= redVec;
end if;
blockGrid(activeBlock(0))(activeBlock(1)) <= zeros;
activeBlock(1) <= activeBlock(1) + 1;
state <= sCount;
when sMoveLeft =>
stateAddress <= "0011";
if blockGrid(activeBlock(0) - 1)(activeBlock(1)) /= zeros then
blockGrid(activeBlock(0) - 1)(activeBlock(1)) <= blueVec;
else
blockGrid(activeBlock(0) - 1)(activeBlock(1)) <= redVec;
end if;
blockGrid(activeBlock(0))(activeBlock(1)) <= zeros;
activeBlock(0) <= activeBlock(0) - 1;
sCounter <= sCounter + 1;
state <= sWaitLeftUp;
when sMoveRight =>
stateAddress <= "0100";
if blockGrid(activeBlock(0) + 1)(activeBlock(1)) /= zeros then
blockGrid(activeBlock(0) + 1)(activeBlock(1)) <= blueVec;
else
blockGrid(activeBlock(0) + 1)(activeBlock(1)) <= redVec;
end if;
blockGrid(activeBlock(0))(activeBlock(1)) <= zeros;
activeBlock(0) <= activeBlock(0) + 1;
sCounter <= sCounter + 1;
state <= sWaitRightUp;
when sMoveUp =>
stateAddress <= "0101";
if blockGrid(activeBlock(0))(activeBlock(1) - 1) /= zeros then
blockGrid(activeBlock(0))(activeBlock(1) - 1) <= blueVec;
else
blockGrid(activeBlock(0))(activeBlock(1) - 1) <= redVec;
end if;
blockGrid(activeBlock(0))(activeBlock(1)) <= zeros;
activeBlock(1) <= activeBlock(1) - 1;
sCounter <= sCounter + 1;
state <= sWaitUp;
when sWaitLeftUp =>
stateAddress <= "0110";
if sCounter(fallSpeed) = '1' then
state <= sStepDownLeft;
elsif shiftLeftBtn = '1' then
sCounter <= sCounter + 1;
state <= sCount;
else
sCounter <= sCounter + 1;
state <= sWaitLeftUp;
end if;
when sWaitRightUp =>
stateAddress <= "0111";
if sCounter(fallSpeed) = '1' then
state <= sStepDownRight;
elsif shiftRightBtn = '1' then
sCounter <= sCounter + 1;
state <= sCount;
else
sCounter <= sCounter + 1;
state <= sWaitRightUp;
end if;
when sWaitUp =>
stateAddress <= "1000";
if sCounter(fallSpeed) = '1' then
state <= sStepDownUp;
elsif up = '1' then
sCounter <= sCounter + 1;
state <= sCount;
else
sCounter <= sCounter + 1;
state <= sWaitUp;
end if;
when sStepDownLeft =>
stateAddress <= "1001";
sCounter <= "0000000000000000000000000000";
if blockGrid(activeBlock(0))(activeBlock(1) + 1) /= zeros then
blockGrid(activeBlock(0))(activeBlock(1) + 1) <= blueVec;
else
blockGrid(activeBlock(0))(activeBlock(1) + 1) <= redVec;
end if;
blockGrid(activeBlock(0))(activeBlock(1)) <= zeros;
activeBlock(1) <= activeBlock(1) + 1;
if shiftLeftBtn = '1' then
state <= sCount;
else
state <= sWaitLeftUp;
end if;
when sStepDownRight =>
stateAddress <= "1010";
sCounter <= "0000000000000000000000000000";
if blockGrid(activeBlock(0))(activeBlock(1) + 1) /= zeros then
blockGrid(activeBlock(0))(activeBlock(1) + 1) <= blueVec;
else
blockGrid(activeBlock(0))(activeBlock(1) + 1) <= redVec;
end if;
blockGrid(activeBlock(0))(activeBlock(1)) <= zeros;
activeBlock(1) <= activeBlock(1) + 1;
if shiftRightBtn = '1' then
state <= sCount;
else
state <= sWaitRightUp;
end if;
when sStepDownUp =>
stateAddress <= "1011";
sCounter <= "0000000000000000000000000000";
if blockGrid(activeBlock(0))(activeBlock(1) + 1) /= zeros then
blockGrid(activeBlock(0))(activeBlock(1) + 1) <= blueVec;
else
blockGrid(activeBlock(0))(activeBlock(1) + 1) <= redVec;
end if;
blockGrid(activeBlock(0))(activeBlock(1)) <= zeros;
activeBlock(1) <= activeBlock(1) + 1;
if up = '1' then
state <= sCount;
else
state <= sWaitUp;
end if;
when others =>
stateAddress <= "1111";
sWaitCounter <= "0000000000000000000000000000";
--state <= sStart;
end case;
end if;
end process;
--sets the pixels to be turned on, blocks in a grid
process (column, row)
begin
--useable area on screen, all pixels will be turned on from here
if column >= 0 and column < 640 then
if row >= 0 and row < 480 then
--initially off
blueOut <= "0000";
redOut <= "0000";
greenOut <= "0000";
-- on for border
--if column = 0 or column = 639 or row = 0 or row = 479 then
-- blueOut <= "1001";
--else
--end if;
-- turn valid blocks on
for i in 0 to blockWidth loop
for j in 0 to blockHeight loop
if column > 1+(i*blockSize) and column < ((i+1)*blockSize) and row > 1+(j*blockSize) and row < ((j+1)*blockSize) then
-- blueOut <= "1001";
blueOut <= blockGrid(i)(j)(3 downto 0);
greenOut <= blockGrid(i)(j)(7 downto 4);
redOut <= blockGrid(i)(j)(11 downto 8);
end if;
end loop;
end loop;
--not useable area
else
redOut <= "0000";
greenOut <= "0000";
blueOut <= "0000";
end if;
else
redOut <= "0000";
greenOut <= "0000";
blueOut <= "0000";
end if;
end process;
end rtl;