one place for all my love
[hwmod.git] / demo / src / demo.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4 use work.demo_pkg.all;
5
6 entity demo is
7   
8   port (
9     clk   : in  std_logic;
10     reset : in  std_logic;
11     leds  : out std_logic_vector(7 downto 0));
12
13 end demo;
14
15
16 architecture behav of demo is
17
18   function GetShiftDelay return time is
19   begin
20         if SIMULATION then
21                 return 1 us;
22         else
23                 return 100 ms;
24         end if;
25   end;
26   
27   signal knightlight, knightlight_next : std_logic_vector (7 downto 0);
28   
29   type LEDSTATE_T is (LEFT_S, RIGHT_S);
30   signal ledstate, ledstate_next : LEDSTATE_T;
31
32   subtype counter_t is integer range 0 to (GetShiftDelay / PERIOD);
33   signal counter : counter_t;
34
35   signal syncreset : std_logic;
36   
37 begin  -- behav
38
39   process(clk)
40   begin
41     if rising_edge(clk) then
42       syncreset <= reset;
43     end if;
44   end process;
45
46              
47   sync: process(clk, reset)
48   begin
49     if rising_edge(clk) then
50       if syncreset = RESETVALUE then
51         knightlight <= (others => LED_OFF);
52         knightlight(2 downto 0) <= (others => LED_ON);
53         ledstate <= LEFT_S;
54       else
55         knightlight <= knightlight_next;
56         ledstate <= ledstate_next;
57       end if;
58     end if;
59   end process;
60
61   next_state_logic: process(ledstate, knightlight, counter)
62     variable knightlight_tmp : bit_vector (7 downto 0);
63   begin  -- process  
64     -- default assignments
65     ledstate_next <= ledstate;
66     knightlight_tmp := TO_BITVECTOR(knightlight);
67       
68     case ledstate is
69       when LEFT_S =>
70         if counter = counter_t'high - 1 then
71           knightlight_tmp := knightlight_tmp sll 1;          
72         end if;
73         if knightlight_tmp = "11100000" then
74           ledstate_next <= RIGHT_S;
75         end if;
76       when RIGHT_S =>
77         if counter = counter_t'high - 1 then
78           knightlight_tmp := knightlight_tmp srl 1;
79         end if;
80         if knightlight_tmp = "00000111" then
81           ledstate_next <= LEFT_S;
82         end if;
83       when others => null;
84     end case;
85
86     knightlight_next <= TO_STDLOGICVECTOR(knightlight_tmp);
87   end process;
88
89   counterProcess: process(clk, reset)
90   begin  -- process
91     if rising_edge(clk) then
92       if syncreset = RESETVALUE then
93         counter <= 0;
94       elsif counter < counter_t'high then
95         counter <= counter + 1;
96       else
97         counter <= 0;
98       end if;
99     end if;
100   end process;
101       
102   leds <= not knightlight;
103   
104 end behav;