library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity kierroslulkulaskuri is
		Port ( anturit : in std_logic_vector(2 downto 0);
				 clock : in std_logic;
				 reset : in std_logic;
				 valmis : out std_logic;
				 arvo : out std_logic_vector(3 downto 0) := "0000");
end kierroslulkulaskuri;

architecture Behavioral of kierroslulkulaskuri is

signal kierros : integer := 0;
signal apu1	: std_logic := '0';
signal apu2	: std_logic := '0';
signal apu3	: std_logic := '0';

begin

Process(clock)

	begin

	if clock='1' and clock'event then

		if reset = '1' then
			
			kierros <= 0;

		else

			if kierros = 3 then

				valmis <= '1';

			else 
			
				valmis <= '0';

				if (anturit = "100") then
					apu1 <= '1';
				elsif (anturit = "010" and apu1 = '1') then
					apu2 <= '1';
				elsif (anturit = "001" and apu1 = '1' and apu2 = '1') then
					apu3 <= '1';
				elsif (anturit = "111" and apu1 = '1' and apu2 = '1' and apu3 = '1') then
					kierros <= kierros + 1;
					apu1 <= '0';
					apu2 <= '0';
					apu3 <= '0';	

				end if;

			end if;

		end if;

	end if;

end Process;

with kierros Select
arvo <= 	"0000" when 0,
			"0001" when 1,  
			"0010" when 2,  
			"0011" when 3,  
			"0100" when 4,  
			"0101" when 5,  
			"0110" when 6,  
			"0111" when 7,
			"1000" when 8,
			"1001" when 9,  
			"1111" when others;

end Behavioral;