library IEEE;
use IEEE.std_logic_1164.all;
--use IEEE.std_logic_textio.all;
--use IEEE.numeric_bit.all;      blocks usage of to_integer(unsigned());
use IEEE.numeric_std.all;
use IEEE.std_logic_signed.all;
use IEEE.std_logic_unsigned.all;
use IEEE.math_real.all;
use IEEE.math_complex.all;

entity motor_control_with_start_sequence is
    Port (  --INPUTS
            clk                                       : in  std_logic;
            in_control_state                          : in  std_logic_vector(4  downto 0);
            in_comparators                            : in  std_logic_vector(2  downto 0);
            --speed control
            in_speed_increase                         : in  std_logic;
            in_speed_decrease                         : in  std_logic;
            --duty cycle control
            in_duty_increase                          : in  std_logic;
            in_duty_decrease                          : in  std_logic;
            out_engine_rotaty_pwm_speed               : out std_logic_vector(15 downto 0);
            out_engine_power_pwm_duty                 : out std_logic_vector(7  downto 0); 
            in_engine_power_pwm_speed                 : in  std_logic_vector(7  downto 0);
            --MEASURED VALUES
            in_sense_PVDD                             : in  std_logic_vector(9  downto 0); 
            in_voltage_A                              : in  std_logic_vector(9  downto 0); 
            in_voltage_B                              : in  std_logic_vector(9  downto 0); 
            in_voltage_C                              : in  std_logic_vector(9  downto 0); 
            --OUTPUTS
            out_pwm_driver_output                     : out std_logic_vector(5  downto 0);
            
            out_time_between_commutation_in_pulses    : out std_logic_vector(15 downto 0);
            out_current_state                         : out std_logic_vector(2 downto 0));
end motor_control_with_start_sequence;

architecture Behavioral of motor_control_with_start_sequence is

    type t_array_lut is array(0 to 22) of natural;

    constant c_max_value_30_bit                       : integer := 1073741824;
    constant c_decrease_speed_debug                   : integer := 20;
    
    constant c_preposition_phase_speeds_array         : t_array_lut := ( 
        89      ,    -- c_inc_cnt_120ms     0
        119      ,    -- c_inc_cnt_90ms      1  
        153      ,    -- c_inc_cnt_70ms      2  
        215      ,
        268      ,
        307      ,
        358      ,
        429      ,
        467      ,
        511      ,
        537      ,
        565      ,
        597      ,
        632      ,
        671      ,
        716      ,
        767      ,
        826      ,
        895      ,
        976      ,
        1074     ,
        1193     ,
        1342     );





    --POWER PWM - counter, actual value and 
    signal s_counter_power_pwm                        : natural range 0 to 255  := 0;
    signal s_engine_power_pwm_duty_integer            : natural range 0 to 255 := 0;
    signal s_engine_power_pwm_duty_vector             : std_logic_vector(7 downto 0);
    signal s_pwm                                      : std_logic;
    --PULSE OUTPUTS FROM DIVIDER
    signal s_divider_pulse_power_pwm                  : std_logic;
    signal s_divider_pulse_rotaty_pwm                 : std_logic;
    --SPEED OF COUNTERS
    signal s_engine_rotaty_pwm_speed                  : std_logic_vector(29 downto 0); --potreba pro citac
    signal s_engine_rotaty_pwm_speed_integer          : natural range 0 to 1073741824 := 0;
    signal s_engine_power_pwm_speed                   : std_logic_vector(9 downto 0); --pro citac

    signal s_in_comparators_last                      : std_logic_vector(2 downto 0);
    signal s_30ms_trigger                             : std_logic;
    
    signal s_pwm_to_be_loaded                         : natural range 0 to 255  := 0;
    signal s_pwm_to_be_loaded_second                  : natural range 0 to 255  := 0;
    
    signal s_counter_preposition_phase                : natural range 0 to 63   := 0;
    
    signal time_between_commutation_in_pulses         : integer;
    signal on_time_of_pwm                             : integer;
    signal on_time_of_pwm_counter                     : integer;
    
    signal s_x_pwm                                    : std_logic;
    
    signal s_pulse_for_short_phase                    : std_logic;
    
    --CALCULATED HALD OF MEASURED VDDA VOLTAGE        
    signal s_half_VDDA                                : std_logic_vector(9 downto 0);
    
    type STATE is (Reset, Start, S0, S1, S2, S3, S4, S5,
                before_S0, before_S1, before_S2, before_S3, before_S4, before_S5,
                on_S0, on_S1, on_S2, on_S3, on_S4, on_S5,
                off_S0, off_S1, off_S2, off_S3, off_S4, off_S5);
    signal current_state                              : STATE;
    signal previous_state                             : STATE;

   component divider_with_increase
       generic( bits                                  : positive);
       port(    clk                                   : in  std_logic;
                in_increase                           : in  std_logic_vector(bits-1 downto 0);
                out_pulse                             : out std_logic;
                --pulse out in particullar value of counter
                in_multiplier                         : in std_logic_vector(7 downto 0);
                out_percentage_pulse                  : out std_logic --pulse that is triggered when in particullar percentage of counter
                );
    end component;


begin
    process(clk)
    begin
        if rising_edge(clk) then
            --******************************************************************************************
            --      PWM - do not put in to conditions of cases - this has to work always               *
            --******************************************************************************************
        
            --citac pro duty cycle signalu
            if(s_divider_pulse_power_pwm = '1') then
                if(s_counter_power_pwm < 255) then
                    s_counter_power_pwm <= s_counter_power_pwm + 1;
                else 
                    s_counter_power_pwm <= 0;
                end if; 
            end if;
            
            --power pwm podle stavu citace
            if(s_counter_power_pwm > s_engine_power_pwm_duty_integer) then
                s_pwm <= '0';
            else
                s_pwm <= '1';
            end if;  
            
            --******************************************************************************************
            --      calculation of time between one commutation to another - in pulses                 *
            --******************************************************************************************
--            out_time_between_commutation_in_pulses <= std_logic_vector(to_unsigned(on_time_of_pwm, out_time_between_commutation_in_pulses'length));
            
--            previous_state <= current_state;
            
--            --commutation occured in last cycle
--            if(previous_state /= current_state) then
--                time_between_commutation_in_pulses <= 0;
--                --calculation of pwm ON duration - number of pulses / 255 * required value (in range from 0 to 255)
--                on_time_of_pwm <= (c_max_value_30_bit / to_integer(unsigned(s_engine_rotaty_pwm_speed))) * s_engine_power_pwm_duty_integer / 255 ;
--                --change output to high when commutation occurs
--                s_pwm                  <= '1';    
--            else
--                time_between_commutation_in_pulses <= time_between_commutation_in_pulses + 1;
--            end if;
            
--            --we hit calculated ON value, so we switch the output to low
--            if(on_time_of_pwm_counter >= on_time_of_pwm) then
--                on_time_of_pwm_counter <=  0;
--                s_pwm                  <= '0';
--            else
--                on_time_of_pwm_counter <= on_time_of_pwm_counter + 1;
--            end if;
            

            --*****************************************************************************************************
            --      SEND VALUES TO UPPER LEVEL COMPONENT - DISPLAY                                                *
            --*****************************************************************************************************
            --SEND SPEED TO DIPLAY
            out_engine_rotaty_pwm_speed <= std_logic_vector(to_unsigned(s_engine_rotaty_pwm_speed_integer, out_engine_rotaty_pwm_speed'length));
            --out_engine_power_pwm_duty   <= std_logic_vector(to_unsigned(s_pwm_to_be_loaded, out_engine_power_pwm_duty'length)); -- s_engine_rotaty_pwm_speed_integer
            --CONVERT INCREASE VALUE FROM INTEGER TO STD_LOGIC_VECTOR
            s_engine_rotaty_pwm_speed <= std_logic_vector(to_unsigned(s_engine_rotaty_pwm_speed_integer, s_engine_rotaty_pwm_speed'length)); 

--            --THIS IS KIND OF RESET (EBABLE IS = 0 IN RESET AND PREPOSITION PHASE)
--            if(in_enable_commutation = '0') then
--                --CHANGE SPEED - POSITION IN ARRAY IS GIVEN BY s_counter_preposition_phase
--                --s_engine_rotaty_pwm_speed <= std_logic_vector(to_unsigned(c_preposition_phase_speeds_array(s_counter_preposition_phase), s_engine_rotaty_pwm_speed'length)); 
--                  s_engine_rotaty_pwm_speed_integer <= c_preposition_phase_speeds_array(s_counter_preposition_phase);
                
--                s_counter_preposition_phase <= 0; -- zbytek z pristupu pres pole
            
--            --SPEED IS RAISED AUTOMATICALLY BY CONSTANT VALUE OR VALUES ARE LOADED FROM ARRAY (TWO SEPERATE APPROACHES)
--            elsif(s_divider_pulse_rotaty_pwm = '1' AND in_enable_commutation = '1' AND s_engine_rotaty_pwm_speed_integer < 8000) then  --s_counter_preposition_phase < 97 -- pouzit v pripade nacitani hodnot z pole
--                --CHANGE SPEED - POSITION IN ARRAY IS GIVEN BY s_counter_preposition_phase
--                --s_engine_rotaty_pwm_speed <= std_logic_vector(to_unsigned(c_preposition_phase_speeds_array(s_counter_preposition_phase), s_engine_rotaty_pwm_speed'length)); --prvni pristup nacitaci hodnot z pole
                
--                s_engine_rotaty_pwm_speed_integer <= s_engine_rotaty_pwm_speed_integer + 80; --Zvysovani o konstantni hodnotu
--                s_counter_preposition_phase <= s_counter_preposition_phase + 1; -- zbytek z pristupu pres pole
                
--            --AFTER SPEED RISES TO GIVEN SPEED, WE CAN CONTROL SPEED USING BUTTONS
--            elsif(s_divider_pulse_rotaty_pwm = '1' AND in_enable_commutation = '1' AND s_engine_rotaty_pwm_speed_integer > 8000) then
--                if (in_speed_increase = '1') then
--                    s_engine_rotaty_pwm_speed_integer <= s_engine_rotaty_pwm_speed_integer + 80;
--                elsif (in_speed_decrease = '1') then
--                    s_engine_rotaty_pwm_speed_integer <= s_engine_rotaty_pwm_speed_integer + 80;
--                end if;

            case in_control_state is
              --RESET STATE
              when "00000" =>
                  s_engine_rotaty_pwm_speed_integer <= 80;    --reset of speed in reset state
                  s_engine_power_pwm_duty_integer   <= 0;     --reset of pwm duty in reset state
                  s_counter_preposition_phase <= 0;
                  current_state <= Reset; 
          
              --PREPOSITION STATE -- automatic raise of pwm duty in order to set motor in known state
              when "00001" =>
                  --need to synchronize so that state machine that controls these states advances to next state
                  
                  --UNCOMMENT LATER FOR GRADUAL CURRENT RISE
--                  if (s_engine_power_pwm_duty_integer < 3 AND s_30ms_trigger = '1') then
--                         s_engine_power_pwm_duty_integer <= s_engine_power_pwm_duty_integer + 1;       
--                  end if;
 
                  s_engine_power_pwm_duty_integer <= 25; 
 
                  if (in_duty_increase = '1') then
                      s_pwm_to_be_loaded <= s_pwm_to_be_loaded + 1;    --s_pwm_to_be_loaded -- NEWLY VALULUE THAT DUTY CYCLE INCREASES
                  elsif (in_duty_decrease = '1') then
                      s_pwm_to_be_loaded <= s_pwm_to_be_loaded - 1;
                  end if;
                  current_state <= Start;
                  
                  --JUST A INFORMATION TO DISPLAY - NEED THIS
                  out_engine_power_pwm_duty   <= std_logic_vector(to_unsigned(s_pwm_to_be_loaded, out_engine_power_pwm_duty'length)); -- s_engine_rotaty_pwm_speed_integer
                  
                  --SETS REAL PWM DUTY CYCLE FROM INTEGER VALUE
                  --s_engine_power_pwm_duty_vector <= std_logic_vector(to_unsigned(s_engine_power_pwm_duty_integer, s_engine_power_pwm_duty_vector'length));
          
                                                 
              --SETTING SPEED INCREASE
              when "00111" =>
               s_engine_power_pwm_duty_integer <= 10;--s_pwm_to_be_loaded;      
                     
                  --mechanismus nastavovani
                  if (in_duty_increase = '1') then
                      s_pwm_to_be_loaded_second <= s_pwm_to_be_loaded_second + 1;
                  elsif (in_duty_decrease = '1') then
                      s_pwm_to_be_loaded_second <= s_pwm_to_be_loaded_second - 1;
                  end if;
                  
                  --JUST A INFORMATION TO DISPLAY - NEED THIS
                  out_engine_power_pwm_duty   <= std_logic_vector(to_unsigned(s_pwm_to_be_loaded_second, out_engine_power_pwm_duty'length)); -- s_engine_rotaty_pwm_speed_integer
                  
                  --SETS REAL PWM DUTY CYCLE FROM INTEGER VALUE
                  s_engine_power_pwm_duty_vector <= std_logic_vector(to_unsigned(s_engine_power_pwm_duty_integer, s_engine_power_pwm_duty_vector'length));
                     
                     
--                   --mechanicmus cteni z pole
                   if(s_divider_pulse_rotaty_pwm = '1' AND s_counter_preposition_phase < 22) then  --s_counter_preposition_phase < 97 -- pouzit v pripade nacitani hodnot z pole
                        --CHANGE SPEED - POSITION IN ARRAY IS GIVEN BY s_counter_preposition_phase
                        s_engine_rotaty_pwm_speed_integer <= c_preposition_phase_speeds_array(s_counter_preposition_phase); --prvni pristup nacitaci hodnot z pole
                
                        s_counter_preposition_phase <= s_counter_preposition_phase + 1;
                    end if;
                   
                    if(s_divider_pulse_rotaty_pwm = '1') then
                        if (current_state = Start) then
                            current_state <= S0;   
                        elsif (current_state = S0) then
                            current_state <= S1;
                        elsif (current_state = S1) then
                            current_state <= S2; 
                        elsif (current_state = S2) then
                            current_state <= S3; 
                        elsif (current_state = S3) then
                            current_state <= S4; 
                        elsif (current_state = S4) then
                            current_state <= S5; 
                        elsif (current_state = S5) then
                            current_state <= S0; 
                        end if;
                    end if;
              
              
              
              
              
              --FORCES SYNCHRONOUS MODE -- automatic raise of speed in order to start motor spinning
               when "00010" =>   
                  out_engine_power_pwm_duty   <= std_logic_vector(to_unsigned(s_pwm_to_be_loaded_second, out_engine_power_pwm_duty'length)); -- s_engine_rotaty_pwm_speed_integer
                  s_engine_power_pwm_duty_vector <= std_logic_vector(to_unsigned(s_engine_power_pwm_duty_integer, s_engine_power_pwm_duty_vector'length));
                    
                        if(s_engine_rotaty_pwm_speed_integer < 10000) then
                            s_engine_power_pwm_duty_integer <= 60 - s_pwm_to_be_loaded; --45
                        -- 11 500    
                        elsif(s_engine_rotaty_pwm_speed_integer < 11500) then
                            s_engine_power_pwm_duty_integer <= 65 - s_pwm_to_be_loaded; --55
                        -- 13 000    
                        elsif(s_engine_rotaty_pwm_speed_integer < 13000) then
                            s_engine_power_pwm_duty_integer <= 70 - s_pwm_to_be_loaded; --55 
                        -- 14 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 14500) then
                            s_engine_power_pwm_duty_integer <= 75 - s_pwm_to_be_loaded; --60
                        -- 16 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 16000) then
                            s_engine_power_pwm_duty_integer <= 75 - s_pwm_to_be_loaded; --65
                        -- 17 500    
                        elsif(s_engine_rotaty_pwm_speed_integer < 17500) then
                            s_engine_power_pwm_duty_integer <= 75 - s_pwm_to_be_loaded; --70
                        -- 19 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 19000) then
                            s_engine_power_pwm_duty_integer <= 80 - s_pwm_to_be_loaded; --80
                        -- 20 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 20500) then
                            s_engine_power_pwm_duty_integer <= 80 - s_pwm_to_be_loaded; --90
                        -- 22 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 22000) then
                            s_engine_power_pwm_duty_integer <= 85 - s_pwm_to_be_loaded; --85
                        -- 23 500   
                        elsif(s_engine_rotaty_pwm_speed_integer < 23500) then
                            s_engine_power_pwm_duty_integer <= 90 - s_pwm_to_be_loaded; --90
                        -- 25 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 25000) then
                            s_engine_power_pwm_duty_integer <= 100 - s_pwm_to_be_loaded; --100
                        -- 26 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 26500) then
                            s_engine_power_pwm_duty_integer <= 105 - s_pwm_to_be_loaded; --105
                        -- 28 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 28000) then
                            s_engine_power_pwm_duty_integer <= 115 - s_pwm_to_be_loaded; --115
                        -- 29 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 29500) then
                            s_engine_power_pwm_duty_integer <= 120 - s_pwm_to_be_loaded; --120
                        -- 31 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 31000) then
                            s_engine_power_pwm_duty_integer <= 125 - s_pwm_to_be_loaded; --125
                        -- 32 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 32500) then
                            s_engine_power_pwm_duty_integer <= 130 - s_pwm_to_be_loaded; --115
                        -- 34 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 34000) then
                            s_engine_power_pwm_duty_integer <= 140 - s_pwm_to_be_loaded; --120
                        -- 35 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 35500) then
                            s_engine_power_pwm_duty_integer <= 150 - s_pwm_to_be_loaded; --125
                        -- 37 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 37000) then
                            s_engine_power_pwm_duty_integer <= 155 - s_pwm_to_be_loaded; --125
                        -- 38 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 38500) then
                            s_engine_power_pwm_duty_integer <= 165 - s_pwm_to_be_loaded; --125
                        -- 40 000
--                        elsif(s_engine_rotaty_pwm_speed_integer < 40000) then
--                            s_engine_power_pwm_duty_integer <= 34 * s_pwm_to_be_loaded; --125
--                        else
--                            s_engine_power_pwm_duty_integer <= 13 * s_pwm_to_be_loaded;
                        end if;
                     
                     --mechanicmus zvysovani o konstantni hodnotu
                   if(s_x_pwm = '1' AND s_engine_rotaty_pwm_speed_integer < 16000) then  --s_counter_preposition_phase < 97 -- pouzit v pripade nacitani hodnot z pole
                        --CHANGE SPEED - POSITION IN ARRAY IS GIVEN BY s_counter_preposition_phase
                        --s_engine_rotaty_pwm_speed <= std_logic_vector(to_unsigned(c_preposition_phase_speeds_array(s_counter_preposition_phase), s_engine_rotaty_pwm_speed'length)); --prvni pristup nacitaci hodnot z pole
                
                        s_engine_rotaty_pwm_speed_integer <= s_engine_rotaty_pwm_speed_integer + (s_pwm_to_be_loaded_second * 7); --Zvysovani o konstantni hodnotu
                        --s_counter_preposition_phase <= s_counter_preposition_phase + 1; -- zbytek z pristupu pres pole
                    end if;
                   
--                    if(s_divider_pulse_rotaty_pwm = '1') then -- and s_counter_preposition_phase < 98
--                        if (current_state = Start) then
--                            current_state <= S0;   
--                        elsif (current_state = S0) then
--                            current_state <= S1;
--                        elsif (current_state = S1) then
--                            current_state <= S2; 
--                        elsif (current_state = S2) then
--                            current_state <= S3; 
--                        elsif (current_state = S3) then
--                            current_state <= S4; 
--                        elsif (current_state = S4) then
--                            current_state <= S5; 
--                        elsif (current_state = S5) then
--                            current_state <= S0; 
--                        end if;
--                    end if;

                --SOLUTION 2
                  --UNCOMMENT THIS FOR SITUATION WHEN PWM IS USED ON TOP TRANZISTOR
                  --AND BOTTOM TRANZISTOR STAYS CLOSED DURING OFF TIME OF TOP TRANZISTOR            
                  if (current_state = S0 OR
                      current_state = S1 OR
                      current_state = S2 OR
                      current_state = S4 OR
                      current_state = S5) then
                      current_state <= on_S0;   
                  
                  --THESE FIRST 6 CONDITION MUST HAVE HIGHER PRIORITY
                  elsif ((current_state = on_S0 OR current_state = off_S0) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S1;
                  elsif ((current_state = on_S1 OR current_state = off_S1) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S2; 
                  elsif ((current_state = on_S2 OR current_state = off_S2) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S3;
                  elsif ((current_state = on_S3 OR current_state = off_S3) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S4; 
                  elsif ((current_state = on_S4 OR current_state = off_S4) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S5; 
                  elsif ((current_state = on_S5 OR current_state = off_S5) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S0;
                  
                  --JUST PWMing USING STATES TO SWITCH OFF TOP TRANZISTOR AND ALSO TURN ON BOTTOM TRANZISTOR
                  elsif ((current_state = on_S0 OR current_state = off_S0) AND s_pwm = '1') then
                      current_state <= on_S0;
                  elsif ((current_state = on_S0 OR current_state = off_S0) AND s_pwm = '0') then
                      current_state <= off_S0;
                      
                  elsif ((current_state = on_S1 OR current_state = off_S1) AND s_pwm = '1') then
                      current_state <= on_S1;
                  elsif ((current_state = on_S1 OR current_state = off_S1) AND s_pwm = '0') then
                      current_state <= off_S1;
                      
                  elsif ((current_state = on_S2 OR current_state = off_S2) AND s_pwm = '1') then
                      current_state <= on_S2;
                  elsif ((current_state = on_S2 OR current_state = off_S2) AND s_pwm = '0') then
                      current_state <= off_S2;
                      
                  elsif ((current_state = on_S3 OR current_state = off_S3) AND s_pwm = '1') then
                      current_state <= on_S3;
                  elsif ((current_state = on_S3 OR current_state = off_S3) AND s_pwm = '0') then
                      current_state <= off_S3;
                      
                  elsif ((current_state = on_S4 OR current_state = off_S4) AND s_pwm = '1') then
                      current_state <= on_S4;
                  elsif ((current_state = on_S4 OR current_state = off_S4) AND s_pwm = '0') then
                      current_state <= off_S4;
                      
                  elsif ((current_state = on_S5 OR current_state = off_S5) AND s_pwm = '1') then
                      current_state <= on_S5;
                  elsif ((current_state = on_S5 OR current_state = off_S5) AND s_pwm = '0') then
                      current_state <= off_S5;
                  end if;
              --******************************************************************************************
              --      SENSORED                                                                           *
              --******************************************************************************************           
--                   when "01000" =>  
--                        --Musime pocitat polovinu napajeciho napeti
--                        s_half_VDDA <= std_logic_vector(to_unsigned(to_integer(unsigned(in_sense_PVDD))/21, s_half_VDDA'length)); 
                        
--                         case current_state is
--                            when S0 =>
                            
--                            when S1 =>
                           
--                            when S2 =>
                            
--                            when S3 =>
                            
--                            when S4 =>
                            
--                            when S5 =>

                        
                        
                        
                        
                        
                        
                        
                        
                 
                        
                        
                        
              --MOTOR IS RUNNING
              when others => 
              
                --s_engine_power_pwm_duty_integer <= 60;
                  out_engine_power_pwm_duty   <= std_logic_vector(to_unsigned(s_engine_power_pwm_duty_integer, out_engine_power_pwm_duty'length));
                  s_engine_power_pwm_duty_vector <= std_logic_vector(to_unsigned(s_engine_power_pwm_duty_integer, s_engine_power_pwm_duty_vector'length));
                  
                  if (in_speed_increase = '1' AND s_engine_rotaty_pwm_speed_integer <= 38400 ) then
                      s_engine_rotaty_pwm_speed_integer <= s_engine_rotaty_pwm_speed_integer + 5;
                  elsif (in_speed_decrease = '1') then
                      s_engine_rotaty_pwm_speed_integer <= s_engine_rotaty_pwm_speed_integer - 5;
                  end if;
                  
--                  if (in_duty_increase = '1') then
--                      s_engine_power_pwm_duty_integer <= s_engine_power_pwm_duty_integer + 1;
--                  elsif (in_duty_decrease = '1') then
--                      s_engine_power_pwm_duty_integer <= s_engine_power_pwm_duty_integer - 1;
--                  end if;
                  
                  
                        if(s_engine_rotaty_pwm_speed_integer < 10000) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 30; -- pwm while at staying at constant speed
                            else 
                                s_engine_power_pwm_duty_integer <= 60 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 11 500    
                        elsif(s_engine_rotaty_pwm_speed_integer < 11500) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 30; -- pwm while at staying at constant speed
                            else 
                                s_engine_power_pwm_duty_integer <= 65 - s_pwm_to_be_loaded; --55
                            end if;
                        -- 13 000    
                        elsif(s_engine_rotaty_pwm_speed_integer < 13000) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 30; -- pwm while at staying at constant speed
                            else 
                                s_engine_power_pwm_duty_integer <= 70 - s_pwm_to_be_loaded; --55
                            end if; 
                        -- 14 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 14500) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 40;
                            else 
                                s_engine_power_pwm_duty_integer <= 75 - s_pwm_to_be_loaded; --60
                            end if;
                        -- 16 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 16000) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 40;
                            else 
                                s_engine_power_pwm_duty_integer <= 75 - s_pwm_to_be_loaded; --65
                            end if;
                        -- 17 500    
                        elsif(s_engine_rotaty_pwm_speed_integer < 17500) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 40;
                            else 
                                s_engine_power_pwm_duty_integer <= 75 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 19 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 19000) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 40;
                            else 
                                s_engine_power_pwm_duty_integer <= 80 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 20 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 20500) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 40;
                            else 
                                s_engine_power_pwm_duty_integer <= 80 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 22 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 22000) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 40;
                            else 
                                s_engine_power_pwm_duty_integer <= 85 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 23 500   
                        elsif(s_engine_rotaty_pwm_speed_integer < 23500) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 40;
                            else 
                                s_engine_power_pwm_duty_integer <= 90 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 25 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 25000) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 40;
                            else 
                                s_engine_power_pwm_duty_integer <= 100 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 26 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 26500) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 50;
                            else 
                                s_engine_power_pwm_duty_integer <= 105 - s_pwm_to_be_loaded; --105
                            end if;
                        -- 28 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 28000) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 50;
                            else 
                                s_engine_power_pwm_duty_integer <= 115 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 29 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 29500) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 60;
                            else 
                                s_engine_power_pwm_duty_integer <= 120 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 31 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 31000) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 60;
                            else 
                                s_engine_power_pwm_duty_integer <= 125 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 32 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 32500) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 65;
                            else 
                                s_engine_power_pwm_duty_integer <= 130 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 34 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 34000) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 65 ; --FINAL
                            else 
                                s_engine_power_pwm_duty_integer <= 140 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 35 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 35500) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 65; --45
                            else 
                                s_engine_power_pwm_duty_integer <= 150 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 37 000
                        elsif(s_engine_rotaty_pwm_speed_integer < 37000) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 65; --45
                            else 
                                s_engine_power_pwm_duty_integer <= 155 - s_pwm_to_be_loaded; --45
                            end if;
                        -- 38 500
                        elsif(s_engine_rotaty_pwm_speed_integer < 38500) then
                            if (in_speed_increase = '0' AND in_speed_decrease = '0') then
                                s_engine_power_pwm_duty_integer <= 65; --45
                            else 
                                s_engine_power_pwm_duty_integer <= 165 - s_pwm_to_be_loaded; --45
                            end if;
                        -- Make current lower when speed reached
--                        else
--                            s_engine_power_pwm_duty_integer <= 135;
                        end if;
              
               
----------------------------------------------------------------------------------------------------------------------------------
                  --SOLUTION 1
--                  --UNCOMMENT THIS FOR SITUATION WHEN PWM IS USED ON TOP TRANZISTOR
--                  --AND BOTTOM TRANZISTOR STAYS CLOSED DURING OFF TIME OF TOP TRANZISTOR            
--                  if (current_state = Start) then
--                      current_state <= S0;   
--                  -- COMMUTATION TO NEXT PREPARATORY STEP IS DEPENDENT ON PULSE FROM COUNTER SIGNALING 90 PERCENT
--                  elsif (current_state = S0 AND s_pulse_for_short_phase = '1') then
--                      current_state <= before_S1;
--                  elsif (current_state = S1 AND s_pulse_for_short_phase = '1') then
--                      current_state <= before_S2; 
--                  elsif (current_state = S2 AND s_pulse_for_short_phase = '1') then
--                      current_state <= before_S3; 
--                  elsif (current_state = S3 AND s_pulse_for_short_phase = '1') then
--                      current_state <= before_S4; 
--                  elsif (current_state = S4 AND s_pulse_for_short_phase = '1') then
--                      current_state <= before_S5; 
--                  elsif (current_state = S5 AND s_pulse_for_short_phase = '1') then
--                      current_state <= before_S0; 
--                  -- COMMUTATION TO NEXT STEP IS DEPENDENT OF SPEED PWM
--                  elsif (current_state = before_S1 AND s_divider_pulse_rotaty_pwm = '1') then
--                      current_state <= S1;
--                  elsif (current_state = before_S2 AND s_divider_pulse_rotaty_pwm = '1') then
--                      current_state <= S2; 
--                  elsif (current_state = before_S3 AND s_divider_pulse_rotaty_pwm = '1') then
--                      current_state <= S3; 
--                  elsif (current_state = before_S4 AND s_divider_pulse_rotaty_pwm = '1') then
--                      current_state <= S4; 
--                  elsif (current_state = before_S5 AND s_divider_pulse_rotaty_pwm = '1') then
--                      current_state <= S5; 
--                  elsif (current_state = before_S0 AND s_divider_pulse_rotaty_pwm = '1') then
--                      current_state <= S0; 
--                  end if;


                --SOLUTION 2
                  --UNCOMMENT THIS FOR SITUATION WHEN PWM IS USED ON TOP TRANZISTOR
                  --AND BOTTOM TRANZISTOR STAYS CLOSED DURING OFF TIME OF TOP TRANZISTOR            
                  
                  --THESE FIRST 6 CONDITION MUST HAVE HIGHER PRIORITY
                  if ((current_state = on_S0 OR current_state = off_S0) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S1;
                  elsif ((current_state = on_S1 OR current_state = off_S1) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S2; 
                  elsif ((current_state = on_S2 OR current_state = off_S2) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S3;
                  elsif ((current_state = on_S3 OR current_state = off_S3) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S4; 
                  elsif ((current_state = on_S4 OR current_state = off_S4) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S5; 
                  elsif ((current_state = on_S5 OR current_state = off_S5) AND s_divider_pulse_rotaty_pwm = '1') then
                      current_state <= on_S0;
                  
                  --JUST PWMing USING STATES TO SWITCH OFF TOP TRANZISTOR AND ALSO TURN ON BOTTOM TRANZISTOR
                  elsif ((current_state = on_S0 OR current_state = off_S0) AND s_pwm = '1') then
                      current_state <= on_S0;
                  elsif ((current_state = on_S0 OR current_state = off_S0) AND s_pwm = '0') then
                      current_state <= off_S0;
                      
                  elsif ((current_state = on_S1 OR current_state = off_S1) AND s_pwm = '1') then
                      current_state <= on_S1;
                  elsif ((current_state = on_S1 OR current_state = off_S1) AND s_pwm = '0') then
                      current_state <= off_S1;
                      
                  elsif ((current_state = on_S2 OR current_state = off_S2) AND s_pwm = '1') then
                      current_state <= on_S2;
                  elsif ((current_state = on_S2 OR current_state = off_S2) AND s_pwm = '0') then
                      current_state <= off_S2;
                      
                  elsif ((current_state = on_S3 OR current_state = off_S3) AND s_pwm = '1') then
                      current_state <= on_S3;
                  elsif ((current_state = on_S3 OR current_state = off_S3) AND s_pwm = '0') then
                      current_state <= off_S3;
                      
                  elsif ((current_state = on_S4 OR current_state = off_S4) AND s_pwm = '1') then
                      current_state <= on_S4;
                  elsif ((current_state = on_S4 OR current_state = off_S4) AND s_pwm = '0') then
                      current_state <= off_S4;
                      
                  elsif ((current_state = on_S5 OR current_state = off_S5) AND s_pwm = '1') then
                      current_state <= on_S5;
                  elsif ((current_state = on_S5 OR current_state = off_S5) AND s_pwm = '0') then
                      current_state <= off_S5;
                  end if;
                  
                  
----------------------------------------------------------------------------------------------------------------------------------
                  
                  --CODE FOR WORKING WITH COMPARATORS
                  --            if(s_counter_preposition_phase >= 99) then
--                --Comparator output 1 rising edge
--                if (s_in_comparators_last(0) = '0' and in_comparators(0) = '1') then
--                    current_state <= S0;
                    
--                --Comparator output 1 falling edge
--                elsif (s_in_comparators_last(0) = '1' and in_comparators(0) = '0') then
--                    current_state <= S1;
                    
--               --Comparator output 2 rising edge
--                elsif (s_in_comparators_last(1) = '0' and in_comparators(1) = '1') then
--                    current_state <= S2;
                    
--                --Comparator output 2 falling edge
--                elsif (s_in_comparators_last(1) = '1' and in_comparators(1) = '0') then
--                    current_state <= S3;
                     
--                --Comparator output 3 rising edge
--                elsif (s_in_comparators_last(2) = '0' and in_comparators(2) = '1') then
--                    current_state <= S4;
                      
--                --Comparator output 3 falling edge
--                elsif (s_in_comparators_last(2) = '1' and in_comparators(2) = '0') then
--                    current_state <= S5;    
--                end if;
--                --Store actual value of comparator outputs
--                s_in_comparators_last <= in_comparators;

              end case;

            --PWM is applied only on top tranzistors
            
            --Start - T1, T6, T4
            --S0    - T1, T4
            --S1    - T1, T6
            --S2    - T3, T6
            --S3    - T3, T2
            --S4    - T5, T2
            --S5    - T5, T4
            
            case current_state is
              when Reset =>
                  out_pwm_driver_output(0) <= '0';                          --horni 
                  out_pwm_driver_output(1) <= '0';                          --spodni
                  out_pwm_driver_output(2) <= '0';                          --horni
                  out_pwm_driver_output(3) <= '0';                          --spodni
                  out_pwm_driver_output(4) <= '0';                          --horni
                  out_pwm_driver_output(5) <= '0';                          --spodni
              when Start =>
                  out_pwm_driver_output(0) <= s_pwm;                        --horni   T1
                  out_pwm_driver_output(1) <= '0';                          --spodni  T2
                  out_pwm_driver_output(2) <= '0';                          --horni   T3
                  out_pwm_driver_output(3) <= s_pwm;                        --spodni  T4
                  out_pwm_driver_output(4) <= '0';                          --horni   T5
                  out_pwm_driver_output(5) <= s_pwm;                        --spodni  T6
              
              
              
              when S0 => -- Merime fazi C - AH-BL
                  out_current_state <= "000";
                  --PHASE A
                  out_pwm_driver_output(0) <= s_pwm;                        --horni 
                  out_pwm_driver_output(1) <= '0';                          --spodni
                  --PHASE B
                  out_pwm_driver_output(2) <= '0';                          --horni 
                  out_pwm_driver_output(3) <= '1';                          --spodni
                  --PHASE C
                  out_pwm_driver_output(4) <= '0';                          --horni 
                  out_pwm_driver_output(5) <= '0';                          --spodni
              when S1 => -- Merime fazi B - AH-CL
                 out_current_state <= "001";
                 out_pwm_driver_output(0) <= s_pwm;                         --horni 
                 out_pwm_driver_output(1) <= '0';                           --spodni
                 out_pwm_driver_output(2) <= '0';                           --horni 
                 out_pwm_driver_output(3) <= '0';                           --spodni
                 out_pwm_driver_output(4) <= '0';                           --horni 
                 out_pwm_driver_output(5) <= '1';                           --spodni                
              when S2 => --Merime fazi A - BH-CL
                 out_current_state <= "010";
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '0';                           --spodni
                 out_pwm_driver_output(2) <= s_pwm;                         --horni 
                 out_pwm_driver_output(3) <= '0';                           --spodni
                 out_pwm_driver_output(4) <= '0';                           --horni 
                 out_pwm_driver_output(5) <= '1';                           --spodni                 
              when S3 => -- Merime fazi C - BH-AL
                 out_current_state <= "011";
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '1';                           --spodni
                 out_pwm_driver_output(2) <= s_pwm;                         --horni 
                 out_pwm_driver_output(3) <= '0';                           --spodni
                 out_pwm_driver_output(4) <= '0';                           --horni 
                 out_pwm_driver_output(5) <= '0';                           --spodni                             
              when S4 => -- Merime fazi B - CH-AL
                 out_current_state <= "100";
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '1';                           --spodni
                 out_pwm_driver_output(2) <= '0';                           --horni 
                 out_pwm_driver_output(3) <= '0';                           --spodni
                 out_pwm_driver_output(4) <= s_pwm;                         --horni 
                 out_pwm_driver_output(5) <= '0';                           --spodni                 
              when S5 => --Merime fazi A - CH-BL
                 out_current_state <= "101";
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '0';                           --spodni
                 out_pwm_driver_output(2) <= '0';                           --horni 
                 out_pwm_driver_output(3) <= '1';                           --spodni
                 out_pwm_driver_output(4) <= s_pwm;                         --horni 
                 out_pwm_driver_output(5) <= '0';                           --spodni
              
              
              
              
              
              when on_S0 => -- Merime fazi C - AH-BL
                  out_current_state <= "000";
                  --PHASE A
                  out_pwm_driver_output(0) <= '1';                        --horni 
                  out_pwm_driver_output(1) <= '0';                          --spodni
                  --PHASE B
                  out_pwm_driver_output(2) <= '0';                          --horni 
                  out_pwm_driver_output(3) <= '1';                          --spodni
                  --PHASE C
                  out_pwm_driver_output(4) <= '0';                          --horni 
                  out_pwm_driver_output(5) <= '0';                          --spodni
              when on_S1 => -- Merime fazi B - AH-CL
                 out_current_state <= "001";
                 out_pwm_driver_output(0) <= '1';                         --horni 
                 out_pwm_driver_output(1) <= '0';                           --spodni
                 out_pwm_driver_output(2) <= '0';                           --horni 
                 out_pwm_driver_output(3) <= '0';                           --spodni
                 out_pwm_driver_output(4) <= '0';                           --horni 
                 out_pwm_driver_output(5) <= '1';                           --spodni                
              when on_S2 => --Merime fazi A - BH-CL
                 out_current_state <= "010";
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '0';                           --spodni
                 out_pwm_driver_output(2) <= '1';                         --horni 
                 out_pwm_driver_output(3) <= '0';                           --spodni
                 out_pwm_driver_output(4) <= '0';                           --horni 
                 out_pwm_driver_output(5) <= '1';                           --spodni                 
              when on_S3 => -- Merime fazi C - BH-AL
                 out_current_state <= "011";
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '1';                           --spodni
                 out_pwm_driver_output(2) <= '1';                         --horni 
                 out_pwm_driver_output(3) <= '0';                           --spodni
                 out_pwm_driver_output(4) <= '0';                           --horni 
                 out_pwm_driver_output(5) <= '0';                           --spodni                             
              when on_S4 => -- Merime fazi B - CH-AL
                 out_current_state <= "100";
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '1';                           --spodni
                 out_pwm_driver_output(2) <= '0';                           --horni 
                 out_pwm_driver_output(3) <= '0';                           --spodni
                 out_pwm_driver_output(4) <= '1';                         --horni 
                 out_pwm_driver_output(5) <= '0';                           --spodni                 
              when on_S5 => --Merime fazi A - CH-BL
                 out_current_state <= "101";
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '0';                           --spodni
                 out_pwm_driver_output(2) <= '0';                           --horni 
                 out_pwm_driver_output(3) <= '1';                           --spodni
                 out_pwm_driver_output(4) <= '1';                         --horni 
                 out_pwm_driver_output(5) <= '0';                           --spodni
 
               --POKUD UZEMNENI FAZE, KTERA BUDE MERENA
              when off_S0 => -- Merime fazi C - AH-BL
                  out_current_state <= "000";
                  --PHASE A -- DONE
                  out_pwm_driver_output(0) <= '0';                        --horni 
                  out_pwm_driver_output(1) <= '1';                          --spodni
                  --PHASE B
                  out_pwm_driver_output(2) <= '0';                          --horni 
                  out_pwm_driver_output(3) <= '1';                          --spodni
                  --PHASE C
                  out_pwm_driver_output(4) <= '0';                          --horni 
                  out_pwm_driver_output(5) <= '0';                          --spodni
              when off_S1 => -- Merime fazi B - AH-CL
                 out_current_state <= "001";
                 --PHASE A -- DONE
                 out_pwm_driver_output(0) <= '0';                         --horni 
                 out_pwm_driver_output(1) <= '1';                           --spodni
                 --PHASE B
                 out_pwm_driver_output(2) <= '0';                           --horni 
                 out_pwm_driver_output(3) <= '0';                           --spodni
                 --PHASE C
                 out_pwm_driver_output(4) <= '0';                           --horni 
                 out_pwm_driver_output(5) <= '1';                           --spodni                
              when off_S2 => --Merime fazi A - BH-CL
                 out_current_state <= "010";
                 --PHASE A -- DONE
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '0';                           --spodni
                 --PHASE B
                 out_pwm_driver_output(2) <= '0';                         --horni 
                 out_pwm_driver_output(3) <= '1';                           --spodni
                 --PHASE C
                 out_pwm_driver_output(4) <= '0';                           --horni 
                 out_pwm_driver_output(5) <= '1';                           --spodni                 
              when off_S3 => -- Merime fazi C - BH-AL
                 out_current_state <= "011";
                 --PHASE A -- DONE
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '1';                           --spodni
                 --PHASE B
                 out_pwm_driver_output(2) <= '0';                         --horni 
                 out_pwm_driver_output(3) <= '1';                           --spodni
                 --PHASE C
                 out_pwm_driver_output(4) <= '0';                           --horni 
                 out_pwm_driver_output(5) <= '0';                           --spodni                             
              when off_S4 => -- Merime fazi B - CH-AL
                 out_current_state <= "100";
                 --PHASE A -- DONE
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '1';                           --spodni
                 --PHASE B
                 out_pwm_driver_output(2) <= '0';                           --horni 
                 out_pwm_driver_output(3) <= '0';                           --spodni
                 --PHASE C
                 out_pwm_driver_output(4) <= '0';                         --horni 
                 out_pwm_driver_output(5) <= '1';                           --spodni                 
              when off_S5 => --Merime fazi A - CH-BL
                 out_current_state <= "101";
                 --PHASE A
                 out_pwm_driver_output(0) <= '0';                           --horni 
                 out_pwm_driver_output(1) <= '0';                           --spodni
                 --PHASE B
                 out_pwm_driver_output(2) <= '0';                           --horni 
                 out_pwm_driver_output(3) <= '1';                           --spodni
                 --PHASE C
                 out_pwm_driver_output(4) <= '0';                         --horni 
                 out_pwm_driver_output(5) <= '1';                           --spodni
              
              
              
--              --POKUD UZEMNENI FAZE, KTERA BUDE MERENA
--              when before_S0 => -- Merime fazi C - AH-BL
--                  out_current_state <= "000";
--                  --PHASE A -- DONE
--                  out_pwm_driver_output(0) <= '0';                        --horni 
--                  out_pwm_driver_output(1) <= '0';                          --spodni
--                  --PHASE B
--                  out_pwm_driver_output(2) <= '0';                          --horni 
--                  out_pwm_driver_output(3) <= '1';                          --spodni
--                  --PHASE C
--                  out_pwm_driver_output(4) <= '0';                          --horni 
--                  out_pwm_driver_output(5) <= '1';                          --spodni
--              when before_S1 => -- Merime fazi B - AH-CL
--                 out_current_state <= "001";
--                 --PHASE A -- DONE
--                 out_pwm_driver_output(0) <= '0';                         --horni 
--                 out_pwm_driver_output(1) <= '1';                           --spodni
--                 --PHASE B
--                 out_pwm_driver_output(2) <= '0';                           --horni 
--                 out_pwm_driver_output(3) <= '1';                           --spodni
--                 --PHASE C
--                 out_pwm_driver_output(4) <= '0';                           --horni 
--                 out_pwm_driver_output(5) <= '0';                           --spodni                
--              when before_S2 => --Merime fazi A - BH-CL
--                 out_current_state <= "010";
--                 --PHASE A -- DONE
--                 out_pwm_driver_output(0) <= '0';                           --horni 
--                 out_pwm_driver_output(1) <= '1';                           --spodni
--                 --PHASE B
--                 out_pwm_driver_output(2) <= '0';                         --horni 
--                 out_pwm_driver_output(3) <= '0';                           --spodni
--                 --PHASE C
--                 out_pwm_driver_output(4) <= '0';                           --horni 
--                 out_pwm_driver_output(5) <= '1';                           --spodni                 
--              when before_S3 => -- Merime fazi C - BH-AL
--                 out_current_state <= "011";
--                 --PHASE A -- DONE
--                 out_pwm_driver_output(0) <= '0';                           --horni 
--                 out_pwm_driver_output(1) <= '0';                           --spodni
--                 --PHASE B
--                 out_pwm_driver_output(2) <= '0';                         --horni 
--                 out_pwm_driver_output(3) <= '1';                           --spodni
--                 --PHASE C
--                 out_pwm_driver_output(4) <= '0';                           --horni 
--                 out_pwm_driver_output(5) <= '1';                           --spodni                             
--              when before_S4 => -- Merime fazi B - CH-AL
--                 out_current_state <= "100";
--                 --PHASE A -- DONE
--                 out_pwm_driver_output(0) <= '0';                           --horni 
--                 out_pwm_driver_output(1) <= '1';                           --spodni
--                 --PHASE B
--                 out_pwm_driver_output(2) <= '0';                           --horni 
--                 out_pwm_driver_output(3) <= '1';                           --spodni
--                 --PHASE C
--                 out_pwm_driver_output(4) <= '0';                         --horni 
--                 out_pwm_driver_output(5) <= '0';                           --spodni                 
--              when before_S5 => --Merime fazi A - CH-BL
--                 out_current_state <= "101";
--                 --PHASE A
--                 out_pwm_driver_output(0) <= '0';                           --horni 
--                 out_pwm_driver_output(1) <= '1';                           --spodni
--                 --PHASE B
--                 out_pwm_driver_output(2) <= '0';                           --horni 
--                 out_pwm_driver_output(3) <= '0';                           --spodni
--                 --PHASE C
--                 out_pwm_driver_output(4) <= '0';                         --horni 
--                 out_pwm_driver_output(5) <= '1';                           --spodni
  
              when others => null;
           end case;     
        end if;
    end process;
    
--                case current_state is
--              when Reset =>
--                  out_pwm_driver_output(0) <= '0';                          --horni 
--                  out_pwm_driver_output(1) <= '0';                          --spodni
--                  out_pwm_driver_output(2) <= '0';                          --horni
--                  out_pwm_driver_output(3) <= '0';                          --spodni
--                  out_pwm_driver_output(4) <= '0';                          --horni
--                  out_pwm_driver_output(5) <= '0';                          --spodni
--              when Start =>
--                  out_pwm_driver_output(0) <= s_pwm;                        --horni  
--                  out_pwm_driver_output(1) <= '0';                          --spodni
--                  out_pwm_driver_output(2) <= '0';                          --horni
--                  out_pwm_driver_output(3) <= '1';                          --spodni
--                  out_pwm_driver_output(4) <= '0';                          --horni
--                  out_pwm_driver_output(5) <= '1';                          --spodni
--              when S0 =>
--                  out_pwm_driver_output(0) <= '0';                          --horni 
--                  out_pwm_driver_output(1) <= '0';                          --spodni
--                  out_pwm_driver_output(2) <= '0';                          --horni 
--                  out_pwm_driver_output(3) <= '1';                          --spodni
--                  out_pwm_driver_output(4) <= s_pwm;                        --horni 
--                  out_pwm_driver_output(5) <= '0';                          --spodni
--              when S1 =>
--                 out_pwm_driver_output(0) <= s_pwm;                         --horni 
--                 out_pwm_driver_output(1) <= '0';                           --spodni
--                 out_pwm_driver_output(2) <= '0';                           --horni 
--                 out_pwm_driver_output(3) <= '1';                           --spodni
--                 out_pwm_driver_output(4) <= '0';                           --horni 
--                 out_pwm_driver_output(5) <= '0';                           --spodni                
--              when S2 =>
--                 out_pwm_driver_output(0) <= s_pwm;                         --horni 
--                 out_pwm_driver_output(1) <= '0';                           --spodni
--                 out_pwm_driver_output(2) <= '0';                           --horni 
--                 out_pwm_driver_output(3) <= '0';                           --spodni
--                 out_pwm_driver_output(4) <= '0';                           --horni 
--                 out_pwm_driver_output(5) <= '1';                           --spodni                 
--              when S3 =>
--                 out_pwm_driver_output(0) <= '0';                           --horni 
--                 out_pwm_driver_output(1) <= '0';                           --spodni
--                 out_pwm_driver_output(2) <= s_pwm;                         --horni 
--                 out_pwm_driver_output(3) <= '0';                           --spodni
--                 out_pwm_driver_output(4) <= '0';                           --horni 
--                 out_pwm_driver_output(5) <= '1';                           --spodni                             
--              when S4 =>
--                 out_pwm_driver_output(0) <= '0';                           --horni 
--                 out_pwm_driver_output(1) <= '1';                           --spodni
--                 out_pwm_driver_output(2) <= s_pwm;                         --horni 
--                 out_pwm_driver_output(3) <= '0';                           --spodni
--                 out_pwm_driver_output(4) <= '0';                           --horni 
--                 out_pwm_driver_output(5) <= '0';                           --spodni                 
--              when S5 =>
--                 out_pwm_driver_output(0) <= '0';                           --horni 
--                 out_pwm_driver_output(1) <= '1';                           --spodni
--                 out_pwm_driver_output(2) <= '0';                           --horni 
--                 out_pwm_driver_output(3) <= '0';                           --spodni
--                 out_pwm_driver_output(4) <= s_pwm;                         --horni 
--                 out_pwm_driver_output(5) <= '0';                           --spodni
    
    --******************************************************************************************
    --      INSTANTIATION OF SUBCOMPONENTS                                                     *
    --******************************************************************************************
     
    divider_with_increase_instance_30ms : divider_with_increase
    --inc_value = (required pulse rate / (100*(10^6)) ) * 2^number_of_bits
    --bottom frequency    = (100*(10^6)) / 2^number_of_bits
    --any frequency       = inc_value * (100*(10^6)) / 2^number_of_bits
    --maximal frequency   = 50*(10^6)
        GENERIC MAP( bits                            => 30)
        PORT MAP( clk                                => clk,
                  in_increase                        => "000000000000000000010000000000",
                  out_pulse                          => s_x_pwm,
                  in_multiplier                      => "00000000",
                  out_percentage_pulse               => open);
                  
    s_engine_power_pwm_speed( 7 downto 0) <= in_engine_power_pwm_speed;
    s_engine_power_pwm_speed( 9 downto 8) <= "00";

    divider_with_increase_instance_rotaty_pwm : divider_with_increase
    --inc_value = (required pulse rate / (100*(10^6)) ) * 2^number_of_bits
    --bottom frequency    =              (100*(10^6)) / 2^number_of_bits
    --any frequency       = inc_value * (100*(10^6)) / 2^number_of_bits
    --maximal frequency   = 50*(10^6)
        GENERIC MAP( bits                             => 30)
        PORT MAP( clk                                 => clk,
                  in_increase                         => s_engine_rotaty_pwm_speed,
                  out_pulse                           => s_divider_pulse_rotaty_pwm,
                  in_multiplier                       => s_engine_power_pwm_duty_vector,
                  out_percentage_pulse                => s_pulse_for_short_phase);

    divider_with_increase_instance_power_pwm : divider_with_increase
    --inc_value = (required pulse rate / (100*(10^6)) ) * 2^number_of_bits
    --bottom frequency    = (100*(10^6)) / 2^number_of_bits
    --any frequency       = inc_value * (100*(10^6)) / 2^number_of_bits
    --maximal frequency   = 50*(10^6)
    
    --inc value of 52 sets final pwm clock at 20kHz - required clock needs to be multiplied by 255,
    --because pwm period has 255 steps
        GENERIC MAP( bits                            => 10)
        PORT MAP( clk                                => clk,
                  in_increase                        => s_engine_power_pwm_speed,
                  out_pulse                          => s_divider_pulse_power_pwm,
                  in_multiplier                      => "00000000",
                  out_percentage_pulse               => open);
     
     
     
end Behavioral;

