LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.std_logic_unsigned.ALL;
USE IEEE.std_logic_arith.ALL;
ENTITY step_motor IS
     PORT(
         f, p, d: IN   STD_LOGIC:='0';
         speed : in  STD_LOGIC_VECTOR(1 downto 0);
         coil : OUT    STD_LOGIC_VECTOR(3 downto 0)
         );

END step_motor;
ARCHITECTURE behavior OF step_motor IS
     SIGNAL ind_coil: STD_LOGIC_VECTOR(3 downto 0) := "0001";
     SIGNAL clk_scan: STD_LOGIC;
    SIGNAL PHASE,DIRECTION:STD_LOGIC;
    signal t:std_logic_vector(3 downto 0);
     signal comp:integer range 0 to 2500 ;
    SIGNAL osc:STD_LOGIC;
     BEGIN
coil <= t;

process(f,osc)
variable delay:integer range 0 to 50;
begin
if (f'event and f='1') then
if delay>=50 then delay:=0;osc<=not osc;
else delay:=delay+1;
end if;
end if;

if (osc'event and osc='1') then
case speed is

   when "10" => if comp<2500 then comp<=comp+1;
                else comp<=comp;
                end if;
   when "01" => if comp>2 then comp<=comp-1;
                else comp<=comp;
                end if;
   when others => if comp<2 then comp<=2;
                else comp<=comp;
                end if;
end case;
end if;
end process;

PROCESS
     VARIABLE d_ff: integer range 0 to 2500;

BEGIN

     WAIT UNTIL f = '1';
     if d_ff >= comp then
         d_ff :=0; clk_scan <= not CLK_SCAN;
     else
         d_ff := d_ff + 1;
     end if;
          END PROCESS ;

PROCESS(F)
VARIABLE B:STD_LOGIC;
BEGIN
IF (F'EVENT AND F='1') THEN B:=(P and (b and P) );
IF B='1' THEN PHASE<=NOT PHASE ;B:='0';
ELSIF P='0' THEN PHASE<=PHASE;B:='1';
END IF;
END IF;
END PROCESS;

PROCESS(F)
VARIABLE B:STD_LOGIC;
BEGIN
IF (F'EVENT AND F='1') THEN B:=(D and (b and D) );
IF B='1' THEN DIRECTION<=NOT DIRECTION ;B:='0';
ELSIF D='0' THEN DIRECTION<=DIRECTION;B:='1';
END IF;
END IF;
END PROCESS;
motor:
process
begin
--if (clk_scan'event and clk_scan='1') then
WAIT UNTIL clk_scan= '0';
     CASE phase IS
         WHEN '1' =>
             IF direction = '0' THEN
                 IF ((ind_coil = "1001") or (ind_coil = "0000")) THEN
                  ind_coil <= "0001";
                 ELSE
                  ind_coil <= (ind_coil(2 downto 0) & ind_coil(3));
             END IF;
             ELSE
                 IF ((ind_coil = "1001") or (ind_coil = "0000")) THEN
                 ind_coil <= "1000";
                 ELSE
                  ind_coil <= (ind_coil(0) & ind_coil(3 downto 1));
             END IF;
             END IF;

           WHEN OTHERS =>
                         ind_coil<=IND_COIL;
           END CASE;
t<=not ind_coil;
END PROCESS motor;

END behavior;