1 | library IEEE;
|
2 | use IEEE.STD_LOGIC_1164.ALL;
|
3 | use IEEE.NUMERIC_STD.ALL;
|
4 |
|
5 |
|
6 | entity ledmatrix is
|
7 | GENERIC (
|
8 | delay : natural := 63
|
9 | );
|
10 | PORT
|
11 | (
|
12 |
|
13 | clk : IN std_logic; -- 50 MHz Quartz
|
14 | ledout : OUT std_logic_vector (0 to 23) := "000000000000000000000000";
|
15 | inp : IN std_logic_vector (0 to 7); -- 0-6 = Spaltentaster, 7 = Reset
|
16 | segments : out std_logic_vector (7 downto 0) := "00000000"
|
17 |
|
18 | );
|
19 |
|
20 | end ledmatrix;
|
21 |
|
22 |
|
23 | architecture Behavioral of ledmatrix is
|
24 |
|
25 | --Matrixmultiplex
|
26 | type matrixB is array (1 to 7, 1 to 6) of integer range 0 to 3;
|
27 | signal matP : matrixB := (others => (others => 0)); --0 = None, 1=Rot, 2=Grün, 3=Gelb
|
28 | signal f_multiplex: std_logic := '0'; -- 800Hz
|
29 | signal f_mult_f: std_logic := '0';
|
30 | -------------------
|
31 |
|
32 | --Logik
|
33 | signal win : integer range 0 to 2 := 0;
|
34 | signal xls : integer range 1 to 7 := 1;
|
35 | signal yls : integer range 1 to 6 := 1;
|
36 | signal state : integer range 0 to 11 := 0;
|
37 |
|
38 |
|
39 | signal checkF : integer range 0 to 2 := 1;
|
40 |
|
41 | signal currentPlayer : std_logic := '0';
|
42 |
|
43 | type intA is array(0 to 7) of integer range 0 to 8;
|
44 | signal CountS : intA := (others => 0);
|
45 | -------------------
|
46 |
|
47 | --Taster entprellen
|
48 | signal inp_entp : std_logic_vector (0 to 7) := "00000000";
|
49 | -------------------
|
50 |
|
51 | begin
|
52 |
|
53 |
|
54 | EntprellenNeu:
|
55 | process
|
56 | type intB is array(0 to 7) of integer range 0 to 150000;
|
57 | variable inpcnt : intB := (others => 0);
|
58 | variable inp_active : std_logic_vector(0 to 7) := (others => '0');
|
59 | begin
|
60 | wait until rising_edge(clk);
|
61 |
|
62 | for i in 0 to 7 loop
|
63 | inp_entp(i) <= '0';
|
64 |
|
65 | if inp(i) = '0' then
|
66 | inp_active(i) := '1';
|
67 | end if;
|
68 |
|
69 | if inp_active(i) = '1' then
|
70 | if inpcnt(i) = 150000 then
|
71 | inpcnt(i) := 0;
|
72 | if inp(i) = '1' then
|
73 | inp_entp(i) <= '1'; --Steigende flanke
|
74 | inp_active(i) := '0';
|
75 | end if;
|
76 | else
|
77 |
|
78 | inpcnt(i) := inpcnt(i) + 1;
|
79 | end if;
|
80 | end if;
|
81 |
|
82 | end loop;
|
83 |
|
84 |
|
85 | end process;
|
86 |
|
87 |
|
88 |
|
89 | --Schalterauswertung--
|
90 | Schalterauswertung:
|
91 | process
|
92 | variable checkC : integer range 0 to 6 := 0;
|
93 | variable checkV : integer range 0 to 6 := 0;
|
94 | variable checkH : integer range 0 to 6 := 0;
|
95 | variable checkDR : integer range 0 to 6 := 0;
|
96 | variable checkDL : integer range 0 to 6 := 0;
|
97 | begin
|
98 |
|
99 | wait until rising_edge(clk);
|
100 |
|
101 |
|
102 | case state is
|
103 | when 0 =>
|
104 |
|
105 | --Reset
|
106 | if inp_entp(7) = '1' then
|
107 |
|
108 | currentPlayer <= '0';
|
109 | win <= 0;
|
110 | xls <= 1;
|
111 |
|
112 | --Matrix zurücksetzen
|
113 | for x in 1 to 7 loop
|
114 |
|
115 | CountS(x) <= 0;
|
116 |
|
117 | for y in 1 to 6 loop
|
118 | matP(x, y) <= 0;
|
119 | end loop;
|
120 |
|
121 | end loop;
|
122 |
|
123 | ---Kein Reset
|
124 | elsif (win = 0) then
|
125 |
|
126 | if inp_entp(0) = '1' then
|
127 | xls <= 1;
|
128 | state <= state + 1;
|
129 | elsif inp_entp(1) = '1' then
|
130 | xls <= 2;
|
131 | state <= state + 1;
|
132 | elsif inp_entp(2) = '1' then
|
133 | xls <= 3;
|
134 | state <= state + 1;
|
135 | elsif inp_entp(3) = '1' then
|
136 | xls <= 4;
|
137 | state <= state + 1;
|
138 | elsif inp_entp(4) = '1' then
|
139 | xls <= 5;
|
140 | state <= state + 1;
|
141 | elsif inp_entp(5) = '1' then
|
142 | xls <= 6;
|
143 | state <= state + 1;
|
144 | elsif inp_entp(6) = '1' then
|
145 | xls <= 7;
|
146 | state <= state + 1;
|
147 | end if;
|
148 |
|
149 |
|
150 | end if;
|
151 |
|
152 |
|
153 | when 1 =>
|
154 | --Auswertung Stufe 1
|
155 |
|
156 | CountS(xls) <= CountS(xls) + 1;
|
157 |
|
158 | if currentPlayer = '0' then
|
159 | checkF <= 1;
|
160 | else
|
161 | checkF <= 2;
|
162 | end if;
|
163 |
|
164 | state <= state + 1;
|
165 |
|
166 | when 2 =>
|
167 |
|
168 |
|
169 | yls <= CountS(xls);
|
170 | checkH := 1;
|
171 | checkV := 1;
|
172 | checkDL := 1;
|
173 | checkDR := 1;
|
174 |
|
175 | state <= state + 1;
|
176 |
|
177 | when 3 =>
|
178 |
|
179 |
|
180 | --Rechts
|
181 |
|
182 |
|
183 | for j in 1 to 3 loop
|
184 | if (matP(xls+j, yls) = checkF) then
|
185 | checkH := checkH + 1;
|
186 | end if;
|
187 | end loop;
|
188 |
|
189 |
|
190 | state <= state + 1;
|
191 |
|
192 | when 4 =>
|
193 |
|
194 | --Links
|
195 | if (win = 0) then
|
196 |
|
197 | for j in 1 to 3 loop
|
198 | if (matP(xls-j, yls) = checkF) then
|
199 | checkH := checkH + 1;
|
200 | end if;
|
201 | end loop;
|
202 | end if;
|
203 |
|
204 | state <= state + 1;
|
205 |
|
206 |
|
207 | when 5 =>
|
208 |
|
209 | --Unten
|
210 | if (win = 0) then
|
211 |
|
212 | for j in 1 to 3 loop
|
213 | if (matP(xls, yls-j) = checkF) then
|
214 | checkV := checkV + 1;
|
215 | end if;
|
216 | end loop;
|
217 |
|
218 | end if;
|
219 |
|
220 | state <= state + 1;
|
221 |
|
222 |
|
223 | when 6 =>
|
224 |
|
225 | --Links Unten
|
226 | if (win = 0) then
|
227 |
|
228 | for j in 1 to 3 loop
|
229 | if (matP(xls-j, yls-j) = checkF) then
|
230 | checkDR := checkDR + 1;
|
231 | end if;
|
232 | end loop;
|
233 |
|
234 | end if;
|
235 |
|
236 | state <= state + 1;
|
237 |
|
238 | when 7 =>
|
239 |
|
240 | --Rechts Unten
|
241 |
|
242 |
|
243 | if (win = 0) then
|
244 |
|
245 | for j in 1 to 3 loop
|
246 | if (matP(xls+j, yls-j) = checkF) then
|
247 | checkDL := checkDL + 1;
|
248 | end if;
|
249 | end loop;
|
250 | end if;
|
251 |
|
252 | state <= state + 1;
|
253 |
|
254 | when 8 =>
|
255 |
|
256 | --Rechts Oben
|
257 |
|
258 | if (win = 0) then
|
259 |
|
260 | for j in 1 to 3 loop
|
261 | if (matP(xls+j, yls+j) = checkF) then
|
262 | checkDR := checkDR + 1;
|
263 | end if;
|
264 | end loop;
|
265 | end if;
|
266 |
|
267 | state <= state + 1;
|
268 |
|
269 | when 9 =>
|
270 |
|
271 | --Links Oben
|
272 | if (win = 0) then
|
273 | for j in 1 to 3 loop
|
274 | if (matP(xls-j, yls+j) = checkF) then
|
275 | checkDL := checkDL + 1;
|
276 | end if;
|
277 | end loop;
|
278 |
|
279 | end if;
|
280 |
|
281 | state <= state + 1;
|
282 |
|
283 | when 10 =>
|
284 |
|
285 | if (checkH >= 4) or (checkV >= 4) or (checkDR >= 4) or (checkDL >= 4) then
|
286 | win <= checkF;
|
287 | end if;
|
288 |
|
289 | state <= state + 1;
|
290 |
|
291 | when 11 =>
|
292 |
|
293 | --Spielstein erzeugen
|
294 |
|
295 | if currentPlayer = '0' then
|
296 | matP(xls, CountS(xls)) <= 1;
|
297 | else
|
298 | matP(xls, CountS(xls)) <= 2;
|
299 | end if;
|
300 |
|
301 | currentPlayer <= not currentPlayer;
|
302 |
|
303 | state <= 0;
|
304 |
|
305 | end case;
|
306 |
|
307 |
|
308 | end process;
|
309 |
|
310 |
|
311 |
|
312 | -------------------------------------------------------------------------------------------------------
|
313 |
|
314 | ---Gewinnanzeige provisorisch
|
315 | Gewinnanzeige:
|
316 | process
|
317 | begin
|
318 | wait until rising_edge(clk);
|
319 |
|
320 | case win is
|
321 | when 0 =>
|
322 | segments <= "00000000";
|
323 | when 1 =>
|
324 | --R
|
325 | segments <= "00000110";
|
326 | when 2 =>
|
327 | --G
|
328 | segments <= "01011011";
|
329 | end case;
|
330 |
|
331 | end process;
|
332 |
|
333 |
|
334 |
|
335 |
|
336 | --clk -> f_multiplex
|
337 | Multiplexfreq:
|
338 | process (clk)
|
339 | constant max_cnt : integer := 62500; --ergibt ca 114 Hz Bildaufbau pro Led
|
340 | variable cnt : integer range 0 to max_cnt + 1;
|
341 | begin
|
342 | if rising_edge(clk) then
|
343 | cnt := cnt + 1;
|
344 | if cnt = max_cnt +1 then
|
345 | cnt := 0;
|
346 | f_multiplex <= not f_multiplex;
|
347 | end if;
|
348 | end if;
|
349 | end process;
|
350 |
|
351 |
|
352 |
|
353 | --////////Matrix Ausgabe Anfang//////////
|
354 |
|
355 | FlankeFMult:
|
356 | process
|
357 | variable sr : std_logic_vector (3 downto 0) := "0000";
|
358 | begin
|
359 | wait until rising_edge(clk);
|
360 | -- Flanken erkennen
|
361 | f_mult_f <= not sr(3) and sr(2);
|
362 |
|
363 | -- Eingang in Schieberegister einlesen
|
364 | sr := sr(2 downto 0) & f_multiplex;
|
365 | end process;
|
366 |
|
367 |
|
368 |
|
369 | Matrixausgabe: process
|
370 | variable x : integer range 0 to 7 := 1;
|
371 | constant spaltenAnz : integer range 0 to 10 := 7;
|
372 | begin
|
373 |
|
374 | wait until rising_edge(clk);
|
375 | if (f_mult_f = '1') then
|
376 |
|
377 |
|
378 | --unbenutzte pins
|
379 | for i in 13 to 17 loop
|
380 | ledout(i) <= '0';
|
381 | end loop;
|
382 |
|
383 |
|
384 | -- 1 Spalte auf High/Kathode durchschalten
|
385 | ledout(x-1) <= '1';
|
386 |
|
387 | -- die letzte Spalte wieder auf 0
|
388 | if (x>1) then
|
389 | ledout(x-2) <= '0';
|
390 | else
|
391 | ledout(spaltenAnz-1) <= '0';
|
392 | end if;
|
393 |
|
394 |
|
395 | --Code für die zu leuchtenden LEDs in der aktuellen Spalte:
|
396 | --ledout[0-6] = Kathoden // werden gemultiplext // Header A - PIN[2-8]
|
397 | --ledout[7-12] = Anoden Rot // Header A - PIN[9-14]
|
398 | --ledout[18-23] = Anoden Grün // Header B - PIN[2-7]
|
399 |
|
400 |
|
401 |
|
402 | for i in 0 to 5 loop
|
403 |
|
404 | case (matP(x, i+1)) is
|
405 | when 1 =>
|
406 | ledout(7+i) <= '1';
|
407 | ledout(18+i) <= '0';
|
408 |
|
409 | when 2 =>
|
410 | ledout(18+i) <= '1';
|
411 | ledout(7+i) <= '0';
|
412 |
|
413 | when 3 =>
|
414 | ledout(7+i) <= '1';
|
415 | ledout(18+i) <= '1';
|
416 |
|
417 | when others =>
|
418 | ledout(7+i) <= '0';
|
419 | ledout(18+i) <= '0';
|
420 | end case;
|
421 |
|
422 | end loop;
|
423 |
|
424 |
|
425 | if (x = spaltenAnz) then
|
426 | x := 1;
|
427 | else
|
428 | x := x + 1;
|
429 | end if;
|
430 |
|
431 | end if;
|
432 |
|
433 |
|
434 |
|
435 | end process;
|
436 |
|
437 |
|
438 | --////////Matrix Ausgabe Ende//////////
|
439 |
|
440 |
|
441 | end Behavioral;
|