----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;      
use IEEE.STD_LOGIC_ARITH.ALL;     
use IEEE.STD_LOGIC_UNSIGNED.ALL;
----------------------------------------------------------------------------------
entity SPI_bridge_74HC is
  Port (
    
    CLK48 : in  STD_LOGIC;                    --Interface Clock
	 
    dataA : in  STD_LOGIC_VECTOR(7 downto 0); --leds and DC switches
	 dataB : in  STD_LOGIC_VECTOR(7 downto 0); --leds and DC switches
	 d7segA : in  STD_LOGIC_VECTOR(7 downto 0); --7segment
	 d7segB : in  STD_LOGIC_VECTOR(7 downto 0); --7segment
	 
	 wr_en : in  STD_LOGIC; -- write data now
	  
	 CS : out  STD_LOGIC := '1'; 	  
    MOSI : out STD_LOGIC := '0';
	 SPI_SCK	: out STD_LOGIC := '0'
	 
  );
end SPI_bridge_74HC;
----------------------------------------------------------------------------------
architecture rtl of SPI_bridge_74HC is

COMPONENT SPI_core
	PORT(
		CLK48 : IN std_logic;
		data : IN std_logic_vector(7 downto 0);
		wr_en : IN std_logic;          
		MOSI : OUT std_logic;
		SPI_SCK : OUT std_logic	);
	END COMPONENT;
------------------------------

signal data : std_logic_vector(7 downto 0) := X"00"; -- the data wich is sent


signal send_now : std_logic := '0';

signal SPI_state_cntr : std_logic_vector(7 downto 0) := X"FF";

signal s_d7segA: std_logic_vector(7 downto 0) := X"FF";
signal s_d7segB: std_logic_vector(7 downto 0) := X"FF";
----------------------------------------------------------------------------------
begin
 
SPI_core_main: SPI_core PORT MAP(
		CLK48 => CLK48,
		data => data,
		wr_en => send_now,
		MOSI => MOSI,
		SPI_SCK => SPI_SCK	);
------------------------------	
	

process(CLK48)
begin
    if(rising_edge(CLK48)) then
  
	
					 
			if (wr_en = '1') then -- if rising edge of send_now
				SPI_state_cntr <= X"00"; -- startst the sending by nulling this cntr
				CS <= '0';
				
			elsif SPI_state_cntr < X"C0" then -- else if it is the end of counting
				SPI_state_cntr <= SPI_state_cntr + 1;
			end if;
			
			
			
			---------lookup table for 7 segment----------
			
				 -- for the high place on 7seg
	case d7segA is 
      when X"30" => s_d7segA <= "11111111"; -- to draw actual 0 replace with "00011000"
      when X"31" => s_d7segA <= "11111001";
      when X"32" => s_d7segA <= "00101100"; 
      when X"33" => s_d7segA <= "01101000"; 
		when X"34" => s_d7segA <= "11001001";
		when X"35" => s_d7segA <= "01001010"; -- 0 lightning 1 not
		when X"36" => s_d7segA <= "00001010";
		when X"37" => s_d7segA <= "11111000";
		when X"38" => s_d7segA <= "00001000";
		when X"39" => s_d7segA <= "01001000";
		when X"69" => s_d7segA <= "11111011"; -- letter i
      when others => s_d7segA <= "11110111";
   end case;
	 
	
	 -- for the low place on 7seg
	case d7segB is 
      when X"30" => s_d7segB <= "10000001"; 
      when X"31" => s_d7segB <= "11111001";
      when X"32" => s_d7segB <= "01000011"; 
      when X"33" => s_d7segB <= "01100001"; 
		when X"34" => s_d7segB <= "00111001";
		when X"35" => s_d7segB <= "00100101";-- 0 lightning 1 not
		when X"36" => s_d7segB <= "00000101";
		when X"37" => s_d7segB <= "11110001";
		when X"38" => s_d7segB <= "00000001";
		when X"39" => s_d7segB <= "00100001";
		when X"6E" => s_d7segB <= "01011101"; -- letter n
      when others => s_d7segB <= "11111110";
   end case;	
			
			
			
			
			
			---------------sending 7segA-------------------
			
			if (SPI_state_cntr = X"00") then
			 	data <= s_d7segA;
				send_now <= '1';
			end if;
			
			
			if (SPI_state_cntr = X"01") then
				send_now <= '0';
			end if;
			
			---------------sending 7segB-------------------
			
			if (SPI_state_cntr = X"30") then
			 	data <= s_d7segB;
				send_now <= '1';
			end if;
			
			
			if (SPI_state_cntr = X"31") then
				send_now <= '0';
			end if;
			
			
			---------------sending dataA-------------------
			
			if (SPI_state_cntr = X"60") then
			 	data <= dataA;
				send_now <= '1';
			end if;
			
			
			if (SPI_state_cntr = X"61") then
				send_now <= '0';
			end if;
			
			---------------sending dataB-------------------
			
			if (SPI_state_cntr = X"90") then
			 	data <= dataB;
				send_now <= '1';
			end if;
			
			
			if (SPI_state_cntr = X"91") then
				send_now <= '0';
			end if;
			
			----------------- ending transmission -----------------
			if (SPI_state_cntr = X"C0") then
				CS <= '1';
				SPI_state_cntr <= X"FF";
			end if;
			
			
			
	
    end if;
  end process;
end rtl;