library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity lab5fsm is
port (X: in STD_LOGIC_VECTOR(2 downto 0);
CLK: in STD_LOGIC;
RESET: in STD_LOGIC;
Y: out STD_LOGIC_VECTOR(3 downto 0));
end lab5fsm;
architecture Behavioral of lab5fsm is
-- define the state of the FSM
type STATE is (A1, A2, A3, A4);
-- signals to manage the current state and next state
signal CURRENT_STATE, NEXT_STATE: STATE;
begin
P1: process (RESET, CLK)
begin
-- Initial state is A1 and the state changes only
-- on rising edge of the clock.
if (RESET = '1') then
CURRENT_STATE <= A1;
elsif (CLK'event and CLK='1') then
CURRENT_STATE <= NEXT_STATE;
end if;
end process P1;
P2: process(CURRENT_STATE, X)
begin
case CURRENT_STATE is
when A1 =>
Y <= "0111";
NEXT_STATE <= A2;
when A2 =>
if (X = "00-" ) then
Y <= "1100";
NEXT_STATE <= A3;
elsif (X = "10-" ) then
Y <= "0010";
NEXT_STATE <= A1;
elsif (X = "-10") then
Y <= "1110";
NEXT_STATE <= A1;
elsif (X = "--1") then
Y <= "0110";
NEXT_STATE <= A1;
end if;
when A3 =>
if (X = "-0-" ) then
Y <= "1110";
NEXT_STATE <= A4;
elsif (X = "-1-") then
Y <= "0111";
NEXT_STATE <= A1;
end if;
when A4 =>
if (X = "1--" ) then
Y <= "0111";
NEXT_STATE <= A1;
elsif (X = "0--") then
NEXT_STATE <= A3;
end if;
when others =>
Y <= "XXXX";
end case;
end process P2;
end Behavioral;