-------------------------------------------------------------------------------
-- Title       : palet
-- Project     : 
-------------------------------------------------------------------------------
-- File        : palet.vhd
-- Author      : Frederic Lochon
-- Company     : 
-- Platform    : 
-------------------------------------------------------------------------------
-- Description :
-- detection approximative de zones de couleur 
-------------------------------------------------------------------------------
-- Modification history :
-- 2002/11/28 : created
-- 2006/02/11 : rewritten
-------------------------------------------------------------------------------

LIBRARY ieee, work;
USE ieee.std_logic_1164.ALL;
USE ieee.numeric_std.ALL;
USE ieee.std_logic_unsigned.all;

ENTITY palet IS
  
	PORT (
		cpt_line	: IN  integer range 0 to 511;
		cpt_row	    : IN  integer range 0 to 1023;

		cpt_row_out : OUT integer range 0 to 1023;
		cpt_line_out: OUT integer range 0 to 511;

		code_out    : OUT integer range 0 to 7;
		code_in     : IN  integer range 0 to 7;

		choix       : IN  std_logic_vector(1 downto 0);
		donnee      : OUT std_logic_vector(11 downto 0);
    
		en_win      : IN  std_logic;
		so_win      : OUT std_logic;

		blank_in    : in std_logic;
		sync_in     : in std_logic;
		burst_in	: in std_logic;
		blank_out   : out std_logic;
		sync_out    : out std_logic;
		burst_out	: out std_logic;

		reset_n     : IN  std_logic;
		clock       : IN  std_logic;

		code_detect : in integer range 0 to 7;
		code_ok     : in integer range 0 to 7;

		seuilpixel  : in integer range 0 to 15;
		seuilligne  : in integer range 0 to 15
	);

END palet ;

ARCHITECTURE deux_d OF palet IS 
  
	signal s_droite, s_gauche: integer range 0 to 1023;
	signal s_haut, s_bas : integer range 0 to 511;
	signal match : std_logic;

	BEGIN

	colormatch : process (code_in, code_detect)
	begin
		if code_in = code_detect then
			match <= '1';
		else
			match <= '0';
		end if;
	end process;
 
-------------------------------------------------------------------------------
  
	choix_donnee : process (choix, s_gauche, s_droite, s_haut, s_bas)
	begin
		case choix is
			when "00" => donnee <= std_logic_vector(to_unsigned(s_gauche,12));
			when "01" => donnee <= std_logic_vector(to_unsigned(s_droite,12));
			when "10" => donnee <= std_logic_vector(to_unsigned(s_haut,12));
			when others => donnee <= std_logic_vector(to_unsigned(s_bas,12));
		end case;
	end process;

-------------------------------------------------------------------------------

	process(clock, blank_in, sync_in)
	begin
		if rising_edge(clock) then
			blank_out    <= blank_in;
			sync_out     <= sync_in;
			burst_out    <= burst_in;
			cpt_row_out  <= cpt_row;
			cpt_line_out <= cpt_line;
			so_win       <= en_win;
		end if;
	end process;
  
-------------------------------------------------------------------------------
	findball : process (clock, reset_n, en_win, cpt_line, cpt_row)
	begin
		if reset_n='0' then
			s_droite     <= 0;
			s_gauche     <= 0;
			s_bas        <= 0;
			s_haut       <= 0;

			code_out     <= 0;

		elsif (rising_edge(clock)) then
       		if match = '1' and en_win = '1' then
				-- demarrage du palet ?
				if s_gauche = 0 and s_haut = 0 then
					code_out   <= code_ok;
					s_gauche   <= cpt_row;
					s_droite   <= cpt_row;
					s_bas      <= cpt_line;
					s_haut     <= cpt_line;
				-- pixel pas trop loin en dessous en nombre de lignes ?
				elsif cpt_line-s_bas < seuilligne then
					if (cpt_row > s_gauche - seuilpixel) and (cpt_row < s_droite + seuilpixel) then
						code_out <= code_ok;
						s_bas <= cpt_line;
						-- la zone s'etend sur la droite ?
						if s_droite < cpt_row then
							s_droite <= cpt_row;
						end if;
						-- la zone s'etend sur la gauche ?
						if cpt_row < s_gauche then
							s_gauche <= cpt_row;
						end if;
					-- pixel non retenu (trop sur le coté)
					else
						code_out <= code_in;
					end if;
				else
				-- pixel non retenu (trop bas)
					code_out <= code_in;
-- interet limité
--					if (s_droite - s_gauche) < seuilpixel and (s_bas - s_haut) < seuilligne then
--						s_droite     <= 0;
--						s_gauche     <= 0;
--						s_bas        <= 0;
--						s_haut       <= 0;
--					end if;
				end if;
			else
			-- mauvaise couleur, on transmet le code couleur sans altération
				code_out <= code_in;
			end if;
		end if;
	end process;

end deux_d;

