library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity Semafor is
Port (
iCLK : in std_logic;
iRST : in std_logic;
iOK : in std_logic;
iHAZ : in std_logic;
oRED : out std_logic;
oYELLOW : out std_logic;
oGREEN : out std_logic
);
end entity;
architecture Behavioral of Semafor is
type tSTATE is (IDLE, RED, RED_YELLOW, GREEN, YELLOW, HAZARD);
signal sSTATE, sNEXT_STATE : tSTATE;
signal sLED : std_logic_vector(7 downto 0);
-- signal sNEXT_LED : std_logic_vector(7 downto 0);
signal sCNT : std_logic_vector(23 downto 0);
signal sEN : std_logic;
-- konstanta za maksimalnu vrednost brojača jedne sekunde - za MAX 10
constant cMAX : std_logic_vector (23 downto 0) := conv_std_logic_vector(12000000, 24); -- 12Mhz
-- konstanta za maksimalnu vrednost brojača jedne sekunde - za simulaciju
--constant cMAX : std_logic_vector (23 downto 0) := conv_std_logic_vector(10, 24);
begin
-- brojac jedne sekunde
process (iCLK, iRST) begin
if (iRST = '1') then
sCNT <= (others=>'0');
elsif (iCLK'event and iCLK = '1') then
if (sCNT = cMAX) then
sCNT <= (others=>'0');
else
sCNT <= sCNT + 1;
end if;
end if;
end process;
sEN <= '1' when sCNT = cMAX else '0';
-- registar za pamcenje stanja
process (iCLK, iRST) begin
if (iRST = '1') then
sSTATE <= IDLE;
elsif (iCLK'event and iCLK = '1') then
if(sEN = '1') then
sSTATE <= sNEXT_STATE;
end if;
end if;
end process;
--funkcija prelaza stanja (kombinaciona mreza)
process (iOK, iHAZ, sSTATE) begin
case sSTATE is
when IDLE =>
if (iOK = '1') then
sNEXT_STATE <= RED;
else
sNEXT_STATE <= IDLE;
end if;
when RED =>
if (iHAZ = '1') then
sNEXT_STATE <= HAZARD;
else
sNEXT_STATE <= RED_YELLOW;
end if;
when RED_YELLOW =>
if (iOK = '1') then
sNEXT_STATE <= HAZARD;
else
sNEXT_STATE <= GREEN;
end if;
when GREEN =>
if (iOK = '1') then
sNEXT_STATE <= HAZARD;
else
sNEXT_STATE <= YELLOW;
end if;
when YELLOW =>
if (iOK = '1') then
sNEXT_STATE <= HAZARD;
else
sNEXT_STATE <= RED;
end if;
when HAZARD =>
if (iOK = '1') then
sNEXT_STATE <= RED;
else
sNEXT_STATE <= HAZARD;
end if;
when others =>
sNEXT_STATE <= sSTATE;
end case;
end process;
--registar za izlazno stanje LED diode
oRED <= '1'when (sSTATE = RED or sSTATE = RED_YELLOW or sSTATE = HAZARD) else '0';
oYELLOW <= '1'when (sSTATE = YELLOW or sSTATE = RED_YELLOW or sSTATE = HAZARD) else '0';
oGREEN <= '1'when (sSTATE = GREEN or sSTATE = HAZARD) else '0';
end Behavioral;
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}