------------------------------------------------------------------------------
-- Affaire :   ROBOT
-- Carte :		Cam-ass-ultra
-- Fonction : 	génération des signaux pour la tv
-- Author :		Eirbot 2006 / crazyfred
-- Date :		12.05.2006
------------------------------------------------------------------------------

library IEEE, work;
use IEEE.std_logic_1164.all;
use ieee.std_logic_unsigned.all; 
use ieee.numeric_std.all; 
use work.all;

entity display is
	port (
		sync_a   : in std_logic;
		blank_a  : in std_logic;
		burst_a  : in std_logic;
		code_a : in std_logic_vector(2 downto 0);
		hue_a : in std_logic_vector(8 downto 0);
		sat_a : in std_logic_vector(6 downto 0);
		light_a : in std_logic_vector(5 downto 0);
		cvbs_a : in std_logic_vector(7 downto 0);
		row_a : in std_logic_vector(9 downto 0);
		line_a : in std_logic_vector(8 downto 0);

		sync_b   : in std_logic;
		blank_b  : in std_logic;
		burst_b  : in std_logic;
		code_b : in std_logic_vector(2 downto 0);
		hue_b : in std_logic_vector(8 downto 0);
		sat_b : in std_logic_vector(6 downto 0);
		light_b : in std_logic_vector(5 downto 0);
		cvbs_b : in std_logic_vector(7 downto 0);
		row_b : in std_logic_vector(9 downto 0);
		line_b : in std_logic_vector(8 downto 0);

		test : in std_logic_vector(7 downto 0);
		sync_val : in std_logic_vector(7 downto 0);
		blank_val : in std_logic_vector(7 downto 0);
		sw : in std_logic_vector(7 downto 0);
		pos_x : in std_logic_vector(7 downto 0);
		pos_y : in std_logic_vector(7 downto 0);

		bit7  : out std_logic;
		bit6  : out std_logic;
		bit5  : out std_logic;
		bit4  : out std_logic;
		bit3  : out std_logic;
		bit2  : out std_logic;
		bit1  : out std_logic;
		bit0  : out std_logic
	);

end display;


architecture structure of display is

signal outbits : std_logic_vector(7 downto 0);
signal selected : std_logic_vector(7 downto 0);
signal overlayed : std_logic_vector(7 downto 0);
signal row : std_logic_vector(9 downto 0);
signal line : std_logic_vector(8 downto 0);

	begin

	bit0 <= outbits(0);
	bit1 <= outbits(1);
	bit2 <= outbits(2);
	bit3 <= outbits(3);
	bit4 <= outbits(4);
	bit5 <= outbits(5);
	bit6 <= outbits(6);
	bit7 <= outbits(7);

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

	process (sync_a, sync_b, blank_a, blank_b, sync_val, blank_val, selected, sw, overlayed, burst_a, burst_b)
	begin
		if sw /= "11111111" then
			if (sync_a = '1' and sw(7) = '0') or (sync_b = '1' and sw(7) = '1') then
				outbits <= "00000000";
			elsif (blank_a = '1' and sw(7) = '0' and not(sw(3) = '1' and burst_a = '1')) or (blank_b = '1' and sw(7) = '1' and not(sw(3) = '1' and burst_b = '1')) then
				outbits <= "01001101";
			else
				outbits <= ("0"&overlayed(7 downto 1)) + ("000"&overlayed(7 downto 3)) + ("0000"&overlayed(7 downto 4)) + "01001101"; -- mise a l'echelle entre 0.375 et 1
			end if;
		else
			outbits <= selected;
		end if;
	end process;

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

	process (sw, test, code_a, hue_a, sat_a, light_a, code_b, hue_b, sat_b, light_b, cvbs_a, cvbs_b)
	begin
		case sw(3 downto 0) is
			when "0000" => selected <= code_a&"00000";
			when "0001" => selected <= hue_a(8 downto 1);
			when "0010" => selected <= sat_a&"0";
			when "0011" => selected <= light_a&"00";
			when "1000" => selected <= cvbs_a;

			when "0100" => selected <= code_b&"00000";
			when "0101" => selected <= hue_b(8 downto 1);
			when "0110" => selected <= sat_b&"0";
			when "0111" => selected <= light_b&"00";
			when "1100" => selected <= cvbs_b;

			when "1111" => selected <= test;
			when others => selected <= "00000000";
		end case;
	end process;

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

	process(sw, line_a,line_b, row_a,row_b,sw)
	begin
		if sw(2) = '0' then
			line <= line_a;
			row  <= row_a;
		else
			line <= line_b;
			row  <= row_b;
		end if;
	end process;

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

	overlay : process(selected, row, line, pos_x,pos_y,sw)
	begin
		if sw(6) = '1' and ((row(9 downto 2) = pos_x) or (line(8 downto 1) = pos_y)) then
			overlayed <= not(selected);
		else
			overlayed <= selected;
		end if;
	end process;

-------------------------------------------------------------------------------
	end structure;
