
entity NRZ2MAN is
  port (NRZ, CLOCK2, RESET : in BIT;
                       MAN : out BIT);
end NRZ2MAN;

architecture BEHAVE of NRZ2MAN is
  type STATE_TYPE is (S0, S1, S2, S3);
  signal CURRENT_STATE, NEXT_STATE : STATE_TYPE;
begin
  process (CURRENT_STATE)
  begin
    case CURRENT_STATE is
      when S0 => if (NRZ = '0') then
                   NEXT_STATE <= S1;
                 else
                   NEXT_STATE <= S3;
                 end if;
      when S1 => NEXT_STATE <= S2;
      when S2 => if NRZ = '0' then
                   NEXT_STATE <= S1;
                 else
                   NEXT_STATE <= S3;
                 end if;
      when S3 => NEXT_STATE <= S0;
      when OTHERS => NEXT_STATE <= S0;
    end case;
  end process;

  process (RESET, CLOCK2)
  begin 
    if (RESET = '0') then
      CURRENT_STATE <= S0;
    elsif (CLOCK2'EVENT and CLOCK2 = '1') then
      CURRENT_STATE <= NEXT_STATE;
    end if;
  end process;

  process (CURRENT_STATE)
  begin
    case CURRENT_STATE is
      when S0 | S1 => MAN <= '0';
      when S2 | S3 => MAN <= '1';
      when OTHERS  => MAN <= '0';
    end case;
  end process;

end BEHAVE;


