Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library IEEE;
- use IEEE.STD_LOGIC_1164.ALL;
- use IEEE.STD_LOGIC_UNSIGNED.ALL;
- entity Zadatak4 is
- Port ( inOK : in STD_LOGIC;
- inHAZ : in STD_LOGIC;
- inRST : in STD_LOGIC;
- iCLK : in STD_LOGIC;
- oRED : out STD_LOGIC_VECTOR (1 downto 0);
- oYELLOW : out STD_LOGIC_VECTOR (1 downto 0);
- oGREEN : out STD_LOGIC_VECTOR (1 downto 0));
- end Zadatak4;
- architecture Behavioral of Zadatak4 is
- type tSTATE is (IDLE, RED, RED_YELLOW, GREEN, YELLOW, HAZARD);
- signal state, state_next : tSTATE;
- signal sTC : STD_LOGIC;
- signal sCNT : STD_LOGIC_VECTOR (3 downto 0); --(23 downto 0)
- signal sGO : STD_LOGIC;
- signal sSTOP : STD_LOGIC;
- begin
- --COUNTER
- process (iCLK, inRST) begin
- if (inRST = '0') then
- sCNT <= (others => '0');
- elsif (iCLK'event and iCLK = '1') then
- if (sCNT = 11) then --11999999
- sCNT <= (others => '0');
- else
- sCNT <= sCNT + 1;
- end if;
- end if;
- end process;
- sTC <= '1' when sCNT = 11 else '0'; --11999999
- --SEMAFOR:
- --FLIPFLOP
- process (iCLK, inRST) begin
- if (inRST = '0') then
- state <= IDLE;
- elsif (iCLK'event and iCLK = '1') then
- state <= state_next;
- end if;
- end process;
- --fp
- process (state, sTC, sGO, sSTOP) begin
- case (state) is
- when IDLE =>
- if (sTC = '1') then
- if (sGO = '1') then
- state_next <= RED;
- elsif (sSTOP = '1') then
- state_next <= HAZARD;
- else
- state_next <= IDLE;
- end if;
- else
- state_next <= IDLE;
- end if;
- when RED =>
- if (sTC = '1') then
- if (sGO = '1') then
- state_next <= RED_YELLOW;
- else
- state_next <= HAZARD;
- end if;
- else
- state_next <= RED;
- end if;
- when RED_YELLOW =>
- if (sTC = '1') then
- if (sGO = '1') then
- state_next <= GREEN;
- else
- state_next <= HAZARD;
- end if;
- else
- state_next <= RED_YELLOW;
- end if;
- when GREEN =>
- if (sTC = '1') then
- if (sGO = '1') then
- state_next <= YELLOW;
- else
- state_next <= HAZARD;
- end if;
- else
- state_next <= GREEN;
- end if;
- when YELLOW =>
- if (sTC = '1') then
- if (sGO = '1') then
- state_next <= RED;
- else
- state_next <= HAZARD;
- end if;
- else
- state_next <= YELLOW;
- end if;
- when others =>
- if (sTC = '1') then
- if (sGO = '1') then
- state_next <= RED;
- else
- state_next <= HAZARD;
- end if;
- else
- state_next <= HAZARD;
- end if;
- end case;
- end process;
- --pomocni signali
- process (iCLK, inRST) begin
- if (inRST = '0') then
- sGO <= '0';
- elsif (iCLK'event and iCLK = '1') then
- if (inOK = '0') then
- sGO <= '1';
- elsif (inHAZ = '0') then
- sGO <= '0';
- else
- sGO <= sGO;
- end if;
- end if;
- end process;
- process (iCLK, inRST) begin
- if (inRST = '0') then
- sSTOP <= '0';
- elsif (iCLK'event and iCLK = '1') then
- if (inOK = '1' and inHAZ = '0') then
- sSTOP <= '1';
- elsif (inOK = '0') then
- sSTOP <= '0';
- else
- sSTOP <= sSTOP;
- end if;
- end if;
- end process;
- --fi
- process (state) begin
- case (state) is
- when IDLE =>
- oRED <= "00";
- oYELLOW <= "00";
- oGREEN <= "00";
- when RED =>
- oRED <= "11";
- oYELLOW <= "00";
- oGREEN <= "00";
- when RED_YELLOW =>
- oRED <= "11";
- oYELLOW <= "11";
- oGREEN <= "00";
- when GREEN =>
- oRED <= "00";
- oYELLOW <= "00";
- oGREEN <= "11";
- when YELLOW =>
- oRED <= "00";
- oYELLOW <= "11";
- oGREEN <= "00";
- when others =>
- oRED <= "11";
- oYELLOW <= "11";
- oGREEN <= "11";
- end case;
- end process;
- end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement