Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 3.73 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  4.  
  5. entity Zadatak4 is
  6.     Port ( inOK : in  STD_LOGIC;
  7.            inHAZ : in  STD_LOGIC;
  8.            inRST : in  STD_LOGIC;
  9.            iCLK : in  STD_LOGIC;
  10.            oRED : out  STD_LOGIC_VECTOR (1 downto 0);
  11.            oYELLOW : out  STD_LOGIC_VECTOR (1 downto 0);
  12.            oGREEN : out  STD_LOGIC_VECTOR (1 downto 0));
  13. end Zadatak4;
  14.  
  15. architecture Behavioral of Zadatak4 is
  16.  
  17. type tSTATE is (IDLE, RED, RED_YELLOW, GREEN, YELLOW, HAZARD);
  18. signal state, state_next : tSTATE;
  19. signal sTC : STD_LOGIC;
  20. signal sCNT : STD_LOGIC_VECTOR (3 downto 0); --(23 downto 0)
  21. signal sGO : STD_LOGIC;
  22. signal sSTOP : STD_LOGIC;
  23.  
  24. begin
  25.  
  26.     --COUNTER
  27.     process (iCLK, inRST) begin
  28.         if (inRST = '0') then
  29.             sCNT <= (others => '0');
  30.         elsif (iCLK'event and iCLK = '1') then
  31.             if (sCNT = 11) then --11999999
  32.                 sCNT <= (others => '0');
  33.             else
  34.                 sCNT <= sCNT + 1;
  35.             end if;
  36.         end if;
  37.     end process;
  38.    
  39.     sTC <= '1' when sCNT = 11 else '0'; --11999999
  40.    
  41.     --SEMAFOR:
  42.     --FLIPFLOP
  43.     process (iCLK, inRST) begin
  44.         if (inRST = '0') then
  45.             state <= IDLE;
  46.         elsif (iCLK'event and iCLK = '1') then
  47.             state <= state_next;
  48.         end if;
  49.     end process;
  50.    
  51.     --fp
  52.     process (state, sTC, sGO, sSTOP) begin
  53.         case (state) is
  54.             when IDLE =>
  55.                 if (sTC = '1') then
  56.                     if (sGO = '1') then
  57.                         state_next <= RED;
  58.                     elsif (sSTOP = '1') then
  59.                         state_next <= HAZARD;
  60.                     else
  61.                         state_next <= IDLE;
  62.                     end if;
  63.                 else
  64.                     state_next <= IDLE;
  65.                 end if;
  66.             when RED =>
  67.                 if (sTC = '1') then
  68.                     if (sGO = '1') then
  69.                         state_next <= RED_YELLOW;
  70.                     else
  71.                         state_next <= HAZARD;
  72.                     end if;
  73.                 else
  74.                     state_next <= RED;
  75.                 end if;
  76.             when RED_YELLOW =>
  77.                 if (sTC = '1') then
  78.                     if (sGO = '1') then
  79.                         state_next <= GREEN;
  80.                     else
  81.                         state_next <= HAZARD;
  82.                     end if;
  83.                 else
  84.                     state_next <= RED_YELLOW;
  85.                 end if;
  86.             when GREEN =>
  87.                 if (sTC = '1') then
  88.                     if (sGO = '1') then
  89.                         state_next <= YELLOW;
  90.                     else
  91.                         state_next <= HAZARD;
  92.                     end if;
  93.                 else
  94.                     state_next <= GREEN;
  95.                 end if;
  96.             when YELLOW =>
  97.                 if (sTC = '1') then
  98.                     if (sGO = '1') then
  99.                         state_next <= RED;
  100.                     else
  101.                         state_next <= HAZARD;
  102.                     end if;
  103.                 else
  104.                     state_next <= YELLOW;
  105.                 end if;
  106.             when others =>
  107.                 if (sTC = '1') then
  108.                     if (sGO = '1') then
  109.                         state_next <= RED;
  110.                     else
  111.                         state_next <= HAZARD;
  112.                     end if;
  113.                 else
  114.                     state_next <= HAZARD;
  115.                 end if;
  116.         end case;
  117.     end process;
  118.                        
  119.     --pomocni signali
  120.     process (iCLK, inRST) begin
  121.         if (inRST = '0') then
  122.             sGO <= '0';
  123.         elsif (iCLK'event and iCLK = '1') then
  124.             if (inOK = '0') then
  125.                 sGO <= '1';
  126.             elsif (inHAZ = '0') then
  127.                 sGO <= '0';
  128.             else
  129.                 sGO <= sGO;
  130.             end if;
  131.         end if;
  132.     end process;
  133.    
  134.     process (iCLK, inRST) begin
  135.         if (inRST = '0') then
  136.             sSTOP <= '0';
  137.         elsif (iCLK'event and iCLK = '1') then
  138.             if (inOK = '1' and inHAZ = '0') then
  139.                 sSTOP <= '1';
  140.             elsif (inOK = '0') then
  141.                 sSTOP <= '0';
  142.             else
  143.                 sSTOP <= sSTOP;
  144.             end if;
  145.         end if;
  146.     end process;
  147.    
  148.     --fi
  149.     process (state) begin
  150.         case (state) is
  151.             when IDLE =>
  152.                 oRED <= "00";
  153.                 oYELLOW <= "00";
  154.                 oGREEN <= "00";
  155.             when RED  =>
  156.                 oRED <= "11";
  157.                 oYELLOW <= "00";
  158.                 oGREEN <= "00";
  159.             when RED_YELLOW  =>
  160.                 oRED <= "11";
  161.                 oYELLOW <= "11";
  162.                 oGREEN <= "00";
  163.             when GREEN  =>
  164.                 oRED <= "00";
  165.                 oYELLOW <= "00";
  166.                 oGREEN <= "11";
  167.             when YELLOW  =>
  168.                 oRED <= "00";
  169.                 oYELLOW <= "11";
  170.                 oGREEN <= "00";
  171.             when others =>
  172.                 oRED <= "11";
  173.                 oYELLOW <= "11";
  174.                 oGREEN <= "11";
  175.         end case;      
  176.     end process;
  177.    
  178.  
  179. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement