--------------------------------------------------------------------------------
-- Company: 
-- Engineer:
--
-- Create Date:    15:03:04 11/10/09
-- Design Name:    
-- Module Name:    PWM - Behavioral
-- Project Name:   
-- Target Device:  
-- Tool versions:  
-- Description:
--
-- Dependencies:
-- 
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
-- 
--------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

---- Uncomment the following library declaration if instantiating
---- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity PWM is
PORT (CLK: 		in std_logic;		
		CO: 		in std_logic;			-- povoleni citani od preddelice
		kod:		in std_logic_vector(5 downto 0);
		blik:		in std_logic;
		button:	out std_logic	--	vystup 
		);
end PWM;

architecture Behavioral of PWM is

	signal temp : std_logic_vector(6 downto 0) := (OTHERS => '0');	--pri implementaci inicializuj
	signal butt : std_logic := '0';
	signal smer : std_logic := '0';

	signal x: std_logic_vector(5 downto 0) := (OTHERS => '0');

begin
	process(CLK, CO, blik)

		begin
			
			if rising_edge(CLK) then
				if blik = '1' then
					if CO = '1'	then
						temp <= temp + 1;
						butt <= '1';
					end if;
					
					if temp = "1000000" then
						temp <= (OTHERS => '0');
						if smer = '1' then
							x <= x + 1;
						else x <= x - 1;
						end if;
					end if;
					
					if x = X"3F" then
						smer <= '0';
					elsif x = 0 then
						smer <= '1';
					end if;

					if temp >= X"00" and x = X"00" then butt <= '0'; 
					elsif temp >= X"01" and x = X"01" then butt <= '0';
					elsif temp >= X"02" and x = X"02" then butt <= '0';
					elsif temp >= X"03" and x = X"03" then butt <= '0';
					elsif temp >= X"04" and x = X"04" then butt <= '0';
					elsif temp >= X"05" and x = X"05" then butt <= '0';
					elsif temp >= X"06" and x = X"06" then butt <= '0';
					elsif temp >= X"07" and x = X"07" then butt <= '0';
					elsif temp >= X"08" and x = X"08" then butt <= '0';
					elsif temp >= X"09" and x = X"09" then butt <= '0';
					elsif temp >= X"0A" and x = X"0A" then butt <= '0';
					elsif temp >= X"0B" and x = X"0B" then butt <= '0';
					elsif temp >= X"0C" and x = X"0C" then butt <= '0';
					elsif temp >= X"0D" and x = X"0D" then butt <= '0';
					elsif temp >= X"0E" and x = X"0E" then butt <= '0';
					elsif temp >= X"0F" and x = X"0F" then butt <= '0';
					elsif temp >= X"10" and x = X"10" then butt <= '0'; 
					elsif temp >= X"11" and x = X"11" then butt <= '0'; 
					elsif temp >= X"12" and x = X"12" then butt <= '0';
					elsif temp >= X"13" and x = X"13" then butt <= '0';
					elsif temp >= X"14" and x = X"14" then butt <= '0';
					elsif temp >= X"15" and x = X"15" then butt <= '0';
					elsif temp >= X"16" and x = X"16" then butt <= '0';
					elsif temp >= X"17" and x = X"17" then butt <= '0';
					elsif temp >= X"18" and x = X"18" then butt <= '0';
					elsif temp >= X"19" and x = X"19" then butt <= '0';
					elsif temp >= X"1A" and x = X"1A" then butt <= '0';
					elsif temp >= X"1B" and x = X"1B" then butt <= '0';
					elsif temp >= X"1C" and x = X"1C" then butt <= '0';
					elsif temp >= X"1D" and x = X"1D" then butt <= '0';
					elsif temp >= X"1E" and x = X"1E" then butt <= '0';
					elsif temp >= X"1F" and x = X"1F" then butt <= '0';
					elsif temp >= X"20" and x = X"20" then butt <= '0';
					elsif temp >= X"21" and x = X"21" then butt <= '0';
					elsif temp >= X"22" and x = X"22" then butt <= '0';
					elsif temp >= X"23" and x = X"23" then butt <= '0';
					elsif temp >= X"24" and x = X"24" then butt <= '0';
					elsif temp >= X"25" and x = X"25" then butt <= '0';
					elsif temp >= X"26" and x = X"26" then butt <= '0';
					elsif temp >= X"27" and x = X"27" then butt <= '0';
					elsif temp >= X"28" and x = X"28" then butt <= '0';
					elsif temp >= X"29" and x = X"29" then butt <= '0';
					elsif temp >= X"2A" and x = X"2A" then butt <= '0';
					elsif temp >= X"2B" and x = X"2B" then butt <= '0';
					elsif temp >= X"2C" and x = X"2C" then butt <= '0';
					elsif temp >= X"2D" and x = X"2D" then butt <= '0';
					elsif temp >= X"2E" and x = X"2E" then butt <= '0';
					elsif temp >= X"2F" and x = X"2F" then butt <= '0';
					elsif temp >= X"30" and x = X"30" then butt <= '0'; 
					elsif temp >= X"31" and x = X"31" then butt <= '0'; 
					elsif temp >= X"32" and x = X"32" then butt <= '0';
					elsif temp >= X"33" and x = X"33" then butt <= '0';
					elsif temp >= X"34" and x = X"34" then butt <= '0';
					elsif temp >= X"35" and x = X"35" then butt <= '0';
					elsif temp >= X"36" and x = X"36" then butt <= '0';
					elsif temp >= X"37" and x = X"37" then butt <= '0';
					elsif temp >= X"38" and x = X"38" then butt <= '0';
					elsif temp >= X"39" and x = X"39" then butt <= '0';
					elsif temp >= X"3A" and x = X"3A" then butt <= '0';
					elsif temp >= X"3B" and x = X"3B" then butt <= '0';
					elsif temp >= X"3C" and x = X"3C" then butt <= '0';
					elsif temp >= X"3D" and x = X"3D" then butt <= '0';
					elsif temp >= X"3E" and x = X"3E" then butt <= '0';
					end if;
					
				else 
					if CO = '1'	then
						temp <= temp + 1;
						butt <= '1';
					end if;
					
					if temp = "1000000" then
						temp <= (OTHERS => '0');
					end if;
					
					if temp >= kod then
						butt <= '0';
						if kod = X"3F" then
							butt <= '1';
						end if;
					end if;
					
				end if;
			end if;
	end process;
	
			button <= butt;

end Behavioral;
