----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date: 01/04/2023 01:55:04 PM
-- Design Name: 
-- Module Name: internal_can - Behavioral
-- Project Name: 
-- Target Devices: 
-- Tool Versions: 
-- Description: 
-- 
-- Dependencies: 
-- 
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
-- 
----------------------------------------------------------------------------------


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

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

entity external_can is
port(
	-- clock signal
	clk		: in  std_logic;
	-- external reset button
	rst		: in std_logic;
	
	led		: out std_logic_vector(7 downto 0);
	
	btn : in std_logic_vector(4 downto 0);
	sw : in std_logic_vector(15 downto 0);
	
	anodes : out std_logic_vector(7 downto 0);
    cathodes : out std_logic_vector(7 downto 0);

    -- pmod
    pmod_rxd : in std_logic_vector(1 downto 0);
    pmod_txd : out std_logic_vector(1 downto 0);
    pmod_de : out std_logic_vector(1 downto 0);
    pmod_re_n : out std_logic_vector(1 downto 0)
);
end entity;

architecture Behavioral of external_can is
	COMPONENT lt16soc_top IS
		generic(
			programfilename : string := "../../programs/project.ram"
		);
		port(
			clk		: in  std_logic;
			rst		: in std_logic;
			led		: out std_logic_vector(7 downto 0);
			btn : in std_logic_vector(4 downto 0);
	        sw : in std_logic_vector(15 downto 0);
            anodes : out std_logic_vector(7 downto 0);
            cathodes : out std_logic_vector(7 downto 0);
            can_rx_i : in std_logic;
            can_tx_o : out std_logic
		);
	END COMPONENT;
	
    signal btn_cpy : std_logic_vector(4 downto 0) := (others => '0');
	signal sw_cpy : std_logic_vector(15 downto 0) := (others => '0');
	
	signal anodes_cpy : std_logic_vector(7 downto 0);
    signal cathodes_cpy : std_logic_vector(7 downto 0);
    
    signal led_cpy : std_logic_vector(7 downto 0);
    
    signal tx : std_logic_vector(1 downto 0);
    
    signal rst_n : std_logic;
begin

	soc0: lt16soc_top
    generic map(
        programfilename => "../../programs/project.ram"
    )
    port map(
		clk=>clk,
		rst=>rst,
		led=>led,
		btn=>btn,
		sw=>sw,
        anodes=>anodes_cpy,
        cathodes=>cathodes_cpy,
        can_rx_i=>pmod_rxd(0),
        can_tx_o=>tx(0)
	);

    soc1: lt16soc_top
    generic map(
        programfilename => "../../programs/project.ram"
    )
    port map(
		clk=>clk,
		rst=>rst,
		led=>led_cpy,
		btn=>btn_cpy,
		sw=>sw_cpy,
        anodes=>anodes,
        cathodes=>cathodes,
        can_rx_i=>pmod_rxd(1),
        can_tx_o=>tx(1)
	);
    
    -- TODO: für pmod !read enable auf low setzen
    pmod_re_n <= (others => '0');
    -- pmod_de <= (others => '1');
    
    pmod_de <= not tx;
    pmod_txd <= tx;
    
    rst_n <= not rst;

end Behavioral;
