Facebook
From Blush Lemur, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 61
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3.  
  4. --brojilo broji (3 bita), sklopka1 zaustavi i freeza brojilo, sklopka2 obrne redoslijed brojanja gore-dolje
  5.  
  6. entity moving_light is
  7.         --TO DO 1 -> deklarirati ulaz "clk" (STD_LOGIC) i izlaz "output" (STD_LOGIC_VECTOR) veličine 8 bita
  8.    Port (
  9.            clk : IN STD_LOGIC;
  10.            output : OUT STD_LOGIC_VECTOR (2 downto 0);
  11.            sw1, sw2 : IN STD_LOGIC
  12.                                
  13.          );
  14. end moving_light;
  15.  
  16. architecture Behavioral of moving_light is
  17.  
  18.         --TO DO 2 -> definirati korisnički tip podataka naziva "state" koji može imati vrijednosti "state0", ... "state7"
  19.    type state is (state0, state1, state2, state3, state4, state5, state6, state7);
  20.         --TO DO 3 -> deklarirati signale "current_state" i "next_state" koji su tipa "state"
  21.    SIGNAL current_state, next_state : state;
  22.         --TO DO 4 -> deklarirati signal "clk_div" koji je tipa STD_LOGIC
  23.         SIGNAL temp : STD_LOGIC_VECTOR (2 downto 0);
  24.    SIGNAL clk_div : STD_LOGIC;
  25.  
  26. begin
  27.  
  28.         --TO DO 5 -> instancirati generički djelitelj frekvencije na način da od ulaznog singala takta kreira signal takta frekvencije 2 Hz, 0.5 s
  29.     clk_2Hz : entity work.generic_divider generic map (25000000) port map (clk, clk_div);
  30.        
  31.         ----Lower section of FSM----
  32.     process(clk_div)
  33.     begin
  34.                 --TO DO 6 -> na rastući brid signala takta "clk_div" signalu "current_state" pridružiti vrijednost signala "next_state"
  35.        if(rising_edge(clk_div)) then
  36.            current_state <= next_state;
  37.        end if;
  38.     end process;
  39.        
  40.         ----Upper section of FSM----
  41.     process(current_state)
  42.     begin
  43.                 if(sw2 = '0') then
  44.                          if(sw1 = '1') then
  45.                                  output <= temp;
  46.                          end if;
  47.                          
  48.                          if (sw1 = '0') then
  49.                                  case current_state is
  50.                                         when state0 =>
  51.                                                 output <= "000";
  52.                                                 temp <=  "000";
  53.                                                 next_state <= state1;
  54.                                         when  state1 =>
  55.                                                 output <= "001";
  56.                                                 temp <= "001";
  57.                                                 next_state <= state2;
  58.                                         when  state2 =>
  59.                                                 output <= "010";
  60.                                                 temp <= "010";
  61.                                                 next_state <= state3;
  62.                                         when  state3 =>
  63.                                                 output <= "011";
  64.                                                 temp <= "011";
  65.                                                 next_state <= state4;
  66.                                         when  state4 =>
  67.                                                 output <= "100";
  68.                                                 temp <= "100";
  69.                                                 next_state <= state5;
  70.                                         when  state5 =>
  71.                                                 output <= "101";
  72.                                                 temp <= "101";
  73.                                                 next_state <= state6;
  74.                                         when  state6 =>
  75.                                                 output <= "110";
  76.                                                 temp <= "110";
  77.                                                 next_state <= state7;
  78.                                         when  state7 =>
  79.                                                 output <= "111";
  80.                                                 temp <= "111";
  81.                                                 next_state <= state0;
  82.                                  end case;
  83.                           end if;
  84.                   end if;
  85.                        
  86.                   if(sw2 = '1') then
  87.                                 if(sw1 = '1') then
  88.                                  output <= temp;
  89.                            end if;
  90.                          
  91.                          if (sw1 = '0') then
  92.                                  case current_state is
  93.                                         when state0 =>
  94.                                                 output <= "000";
  95.                                                 temp <= "000";
  96.                                                 next_state <= state7;
  97.                                         when  state1 =>
  98.                                                 output <= "001";
  99.                                                 temp <= "001";
  100.                                                 next_state <= state0;
  101.                                         when  state2 =>
  102.                                                 output <= "010";
  103.                                                 temp <= "010";
  104.                                                 next_state <= state1;
  105.                                         when  state3 =>
  106.                                                 output <= "011";
  107.                                                 temp <= "011";
  108.                                                 next_state <= state2;
  109.                                         when  state4 =>
  110.                                                 output <= "100";
  111.                                                 temp <= "100";
  112.                                                 next_state <= state3;
  113.                                         when  state5 =>
  114.                                                 output <= "101";
  115.                                                 temp <= "101";
  116.                                                 next_state <= state4;
  117.                                         when  state6 =>
  118.                                                 output <= "110";
  119.                                                 temp <= "110";
  120.                                                 next_state <= state5;
  121.                                         when  state7 =>
  122.                                                 output <= "111";
  123.                                                 temp <= "111";
  124.                                                 next_state <= state6;
  125.                                  end case;
  126.                           end if;
  127.                   end if;
  128.       end process;  
  129.  
  130. end Behavioral;