-- Note: Record is a keyword, so am using RecordX

library ieee;
use ieee.std_logic_1164.all;
entity ANS_MACH3 is port(
	Clk, DialTone:					in std_logic;
	Ring, EndGreeting, EndMessage:	in std_logic;
	Answer, Play, RecordX:			out std_logic);
end ANS_MACH3;

architecture behavior of ANS_MACH3 is
	type States is (Waiting, Answering);
attribute enum_encoding of States: type is "0 1";
	signal PresentState, NextState:	States;
	signal Default, BeginPlay, RunPlayer, BeginRecord:	std_logic;
	signal Outs: std_logic_vector(2 downto 0);
begin

Default <= DialTone and not Ring;
BeginPlay <= Ring;
RunPlayer <= not DialTone and not EndGreeting and not EndMessage;
BeginRecord <= not DialTone and EndGreeting and not EndMessage;

Answer <= Outs(2); Play <= Outs(1); RecordX <= Outs(0);

NEW_STATE: process (Clk, DialTone, Ring, EndGreeting, EndMessage) begin
	case PresentState is
		when Waiting =>	if BeginPlay='1' then
					NextState<=Answering; Outs <= "110";
				else NextState<=Waiting; Outs <= "000";
			end if;
		when Answering => if RunPlayer='1' then
					NextState<=Answering; Outs <= "110";
				elsif BeginRecord='1' then 
					NextState<=Answering; Outs <= "101";
				else NextState<=Waiting; Outs <= "000";
			end if;
		when others =>	NextState<=Waiting; Outs <= "000";
	end case;
end process NEW_STATE;

UPDATE_STATE: process (Clk) begin
	if RISING_EDGE(Clk) then
		PresentState <= NextState;
	end if;
end process UPDATE_STATE;

end behavior;
