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

ENTITY multi8_palet IS
  
	PORT (
		line_in		: IN  integer range 0 to 511;
		row_in	    : IN  integer range 0 to 1023;

		row_out		: OUT integer range 0 to 1023;
		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	: IN  std_logic_vector(7 downto 0);
		donnee      : OUT std_logic_vector(11 downto 0);
    
		win_in		: IN  std_logic;
		win_out		: 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;
		
		write_code  : in std_logic;
		select_code : in std_logic_vector(5 downto 0)
	);

END multi8_palet ;


ARCHITECTURE a OF multi8_palet IS

component palet
	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); -- demuxeur
    
		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; -- a gerer
		code_ok     : in integer range 0 to 7; -- a gerer

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

signal choixdata : std_logic_vector(1 downto 0);
signal choixpalet : std_logic_vector(5 downto 0);

signal p0line_in, p1line_in, p2line_in, p3line_in, p4line_in, p5line_in, p6line_in, p7line_in  : integer range 0 to 511;
signal p0row_in, p1row_in, p2row_in, p3row_in, p4row_in, p5row_in, p6row_in, p7row_in  : integer range 0 to 1023;
signal p0code_in, p1code_in, p2code_in, p3code_in, p4code_in, p5code_in, p6code_in, p7code_in  : integer range 0 to 7;
signal p0donnee, p1donnee, p2donnee, p3donnee, p4donnee, p5donnee, p6donnee, p7donnee : std_logic_vector(11 downto 0);
signal p0en_win, p1en_win, p2en_win, p3en_win, p4en_win, p5en_win, p6en_win, p7en_win : std_logic;
signal p0blank_in, p1blank_in, p2blank_in, p3blank_in, p4blank_in, p5blank_in, p6blank_in, p7blank_in : std_logic;
signal p0sync_in, p1sync_in, p2sync_in, p3sync_in, p4sync_in, p5sync_in, p6sync_in, p7sync_in : std_logic;
signal p0burst_in, p1burst_in, p2burst_in, p3burst_in, p4burst_in, p5burst_in, p6burst_in, p7burst_in : std_logic;
signal p0code_detect, p1code_detect, p2code_detect, p3code_detect, p4code_detect, p5code_detect, p6code_detect, p7code_detect : integer range 0 to 7;
signal p0code_ok, p1code_ok, p2code_ok, p3code_ok, p4code_ok, p5code_ok, p6code_ok, p7code_ok : integer range 0 to 7;

BEGIN

choixdata <= choix_in(1 downto 0);
choixpalet <= choix_in(7 downto 2);

p0line_in <= line_in;
p0row_in <= row_in;
p0code_in <= code_in;
p0en_win <= win_in;
p0blank_in <= blank_in;
p0sync_in <= sync_in;
p0burst_in <= burst_in;

demuxeur : process(choixpalet, p0donnee, p1donnee, p2donnee, p3donnee, p4donnee, p5donnee, p6donnee, p7donnee)
begin
	case choixpalet is
		when "000000" => donnee <= p0donnee;
		when "000001" => donnee <= p1donnee;
		when "000010" => donnee <= p2donnee;
		when "000011" => donnee <= p3donnee;
		when "000100" => donnee <= p4donnee;
		when "000101" => donnee <= p5donnee;
		when "000110" => donnee <= p6donnee;
		when "000111" => donnee <= p7donnee;
		when others => donnee <= "000000000000";
	end case;
end process;

muxeur : process(select_code, code_ok, code_detect, write_code)
begin
	if write_code = '1' then
	case select_code(5 downto 0) is
		when "000000" => p0code_ok <= code_ok; p0code_detect <= code_detect;
		when "000001" => p1code_ok <= code_ok; p1code_detect <= code_detect;
		when "000010" => p2code_ok <= code_ok; p2code_detect <= code_detect;
		when "000011" => p3code_ok <= code_ok; p3code_detect <= code_detect;
		when "000100" => p4code_ok <= code_ok; p4code_detect <= code_detect;
		when "000101" => p5code_ok <= code_ok; p5code_detect <= code_detect;
		when "000110" => p6code_ok <= code_ok; p6code_detect <= code_detect;
		when "000111" => p7code_ok <= code_ok; p7code_detect <= code_detect;
		when others => null;
	end case;
	end if;
end process;

p0_palet : palet
	PORT MAP
	(
		cpt_line => p0line_in,
		cpt_row => p0row_in,
		cpt_row_out => p1row_in,
		cpt_line_out => p1line_in,
		code_out => p1code_in,
		code_in => p0code_in,
		choix => choixdata,
		donnee => p0donnee,
		en_win => p0en_win,
		so_win => p1en_win,
		blank_in => p0blank_in,
		sync_in => p0sync_in,
		burst_in => p0burst_in,
		blank_out => p1blank_in,
		sync_out => p1sync_in,
		burst_out => p1burst_in,
		reset_n => reset_n,
		clock => clock,
		code_detect => p0code_detect,
		code_ok => p0code_ok,
		seuilpixel => seuilpixel,
		seuilligne => seuilligne
	);
p1_palet : palet
	PORT MAP
	(
		cpt_line => p1line_in,
		cpt_row => p1row_in,
		cpt_row_out => p2row_in,
		cpt_line_out => p2line_in,
		code_out => p2code_in,
		code_in => p1code_in,
		choix => choixdata,
		donnee => p1donnee,
		en_win => p1en_win,
		so_win => p2en_win,
		blank_in => p1blank_in,
		sync_in => p1sync_in,
		burst_in => p1burst_in,
		blank_out => p2blank_in,
		sync_out => p2sync_in,
		burst_out => p2burst_in,
		reset_n => reset_n,
		clock => clock,
		code_detect => p1code_detect,
		code_ok => p1code_ok,
		seuilpixel => seuilpixel,
		seuilligne => seuilligne
	);
p2_palet : palet
	PORT MAP
	(
		cpt_line => p2line_in,
		cpt_row => p2row_in,
		cpt_row_out => p3row_in,
		cpt_line_out => p3line_in,
		code_out => p3code_in,
		code_in => p2code_in,
		choix => choixdata,
		donnee => p2donnee,
		en_win => p2en_win,
		so_win => p3en_win,
		blank_in => p2blank_in,
		sync_in => p2sync_in,
		burst_in => p2burst_in,
		blank_out => p3blank_in,
		sync_out => p3sync_in,
		burst_out => p3burst_in,
		reset_n => reset_n,
		clock => clock,
		code_detect => p2code_detect,
		code_ok => p2code_ok,
		seuilpixel => seuilpixel,
		seuilligne => seuilligne
	);
p3_palet : palet
	PORT MAP
	(
		cpt_line => p3line_in,
		cpt_row => p3row_in,
		cpt_row_out => p4row_in,
		cpt_line_out => p4line_in,
		code_out => p4code_in,
		code_in => p3code_in,
		choix => choixdata,
		donnee => p3donnee,
		en_win => p3en_win,
		so_win => p4en_win,
		blank_in => p3blank_in,
		sync_in => p3sync_in,
		burst_in => p3burst_in,
		blank_out => p4blank_in,
		sync_out => p4sync_in,
		burst_out => p4burst_in,
		reset_n => reset_n,
		clock => clock,
		code_detect => p3code_detect,
		code_ok => p3code_ok,
		seuilpixel => seuilpixel,
		seuilligne => seuilligne
	);
p4_palet : palet
	PORT MAP
	(
		cpt_line => p4line_in,
		cpt_row => p4row_in,
		cpt_row_out => p5row_in,
		cpt_line_out => p5line_in,
		code_out => p5code_in,
		code_in => p4code_in,
		choix => choixdata,
		donnee => p4donnee,
		en_win => p4en_win,
		so_win => p5en_win,
		blank_in => p4blank_in,
		sync_in => p4sync_in,
		burst_in => p4burst_in,
		blank_out => p5blank_in,
		sync_out => p5sync_in,
		burst_out => p5burst_in,
		reset_n => reset_n,
		clock => clock,
		code_detect => p4code_detect,
		code_ok => p4code_ok,
		seuilpixel => seuilpixel,
		seuilligne => seuilligne
	);
p5_palet : palet
	PORT MAP
	(
		cpt_line => p5line_in,
		cpt_row => p5row_in,
		cpt_row_out => p6row_in,
		cpt_line_out => p6line_in,
		code_out => p6code_in,
		code_in => p5code_in,
		choix => choixdata,
		donnee => p5donnee,
		en_win => p5en_win,
		so_win => p6en_win,
		blank_in => p5blank_in,
		sync_in => p5sync_in,
		burst_in => p5burst_in,
		blank_out => p6blank_in,
		sync_out => p6sync_in,
		burst_out => p6burst_in,
		reset_n => reset_n,
		clock => clock,
		code_detect => p5code_detect,
		code_ok => p5code_ok,
		seuilpixel => seuilpixel,
		seuilligne => seuilligne
	);
p6_palet : palet
	PORT MAP
	(
		cpt_line => p6line_in,
		cpt_row => p6row_in,
		cpt_row_out => p7row_in,
		cpt_line_out => p7line_in,
		code_out => p7code_in,
		code_in => p6code_in,
		choix => choixdata,
		donnee => p6donnee,
		en_win => p6en_win,
		so_win => p7en_win,
		blank_in => p6blank_in,
		sync_in => p6sync_in,
		burst_in => p6burst_in,
		blank_out => p7blank_in,
		sync_out => p7sync_in,
		burst_out => p7burst_in,
		reset_n => reset_n,
		clock => clock,
		code_detect => p6code_detect,
		code_ok => p6code_ok,
		seuilpixel => seuilpixel,
		seuilligne => seuilligne
	);
p7_palet : palet
	PORT MAP
	(
		cpt_line => p7line_in,
		cpt_row => p7row_in,
		cpt_row_out => row_out,
		cpt_line_out => line_out,
		code_out => code_out,
		code_in => p7code_in,
		choix => choixdata,
		donnee => p7donnee,
		en_win => p7en_win,
		so_win => win_out,
		blank_in => p7blank_in,
		sync_in => p7sync_in,
		burst_in => p7burst_in,
		blank_out => blank_out,
		sync_out => sync_out,
		burst_out => burst_out,
		reset_n => reset_n,
		clock => clock,
		code_detect => p7code_detect,
		code_ok => p7code_ok,
		seuilpixel => seuilpixel,
		seuilligne => seuilligne
	);

END a;
