----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date:    15:53:16 05/31/2010 
-- Design Name: 
-- Module Name:    a2 - 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 primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity address is
port (
			ecu_addr: in std_ulogic_vector (15 downto 0);
			ecu_data:out std_ulogic_vector (7 downto 0):="HHHHHHHH";
			ecu_psen: in std_logic;
			flash_addr: out std_ulogic_vector (15 downto 0):="HHHHHHHHHHHHHHHH";
			flash_data: inout std_ulogic_vector (7 downto 0):="HHHHHHHH";
			flash_rd: out std_logic:='1';
			flash_wr: out std_logic:='1';
			pic_addr:inout std_ulogic_vector (15 downto 0):="HHHHHHHHHHHHHHHH";
			pic_data:inout std_ulogic_vector (7 downto 0):="HHHHHHHH";
			pic_rd:in std_logic;
			pic_wr:in std_logic;
			pic_mode:in std_logic;
			pic_int:out std_logic:='0'
		);
end address;

architecture Behavioral of address is
	shared variable buff0, buff1:std_ulogic_vector (15 downto 0);
begin
	--	cimvezetekeket kezelo process
	a1:process (ecu_addr, pic_addr, pic_mode)
	begin
		if (pic_mode='1') then	--	ha a pic kezeli a flasht
			flash_addr<=pic_addr;	-- akkor a pic hajtja meg a flash cimbuszat
		else
			flash_addr<=ecu_addr;	-- ellenkezo esetben a 8051 hajtja meg a flash cimbuszat
		end if;
	end process;
	-- flash rd es wr vezetekeit kezelo process
	a4:process(pic_rd, pic_wr, pic_mode)
	begin			
		if (pic_mode='1') then		-- ha a pic kezeli a flasht
			if (pic_rd='0') then		-- ha a pic olvasni akar a flashbol
				flash_rd<='0';			-- akkor a flash RD labat lehuzzuk
			elsif (pic_wr='0') then	-- ha a flash irni akar a flashbe
				flash_wr<='0';			-- akkor a flash WR labat lehuzzuk
			else							-- ha a pic nem akar semmit a flashtol
				flash_rd<='1';			-- akkor mind az RD, mind WR magas lesz
				flash_wr<='1';
			end if;
		else								-- ha a 8051 kezeli a flasht
			flash_wr<='1';				-- akkor a WR fixen 1-be kerul
			flash_rd<='0';				-- RD fixen 0-ba (ugyis a CPLD-nek kell megoldani a nagyimpedancias kimenetet)
		end if;
	end process;
	--a pic_addr vezetekekre teszi a tarolt cimeket
	a5:process (pic_rd, pic_mode)
	begin
		if (pic_mode='0') then		-- ha a 8051 kezeli a flasht
			if (pic_rd='0') then		-- ha a pic_rd nulla
				pic_addr<=buff0;		-- akkor a nulladik buffer kerul ki
			else
				pic_addr<=buff1;
			end if;
		end if;
		
	end process;
	--az adatvezetekek kozotti kapcsolatot kezeli
	a2:process (ecu_addr, flash_data, pic_data, pic_mode, pic_rd, pic_wr, ecu_psen)
	begin
		if (pic_mode='1') then			-- ha a pic kezeli a flasht
			ecu_data<="LLLLLLLL";		-- akkor a 8051 fele lefele huzzuk az adatbuszt
			if (pic_rd='0') then			-- ha a pic olvasni akar a flashbol
				flash_data<="HHHHHHHH";	-- akkor a flash kimeneti buszat nagyimpedanciaba rakjuk
				pic_data<=flash_data;	-- osszekapcsoljuk, hogy a flash kimenete eljusson a pic bemenetere
			else								-- ha a pic irni akar, vagy pedig alapeset van
				pic_data<="HHHHHHHH";	-- akkor a pic felol nagyimpedancias kimenet, hogy a pic meg tudja hajtani
				flash_data<=pic_data;	-- osszekapcsoljuk, hogy a pic kimenete eljusson a flash bemenetere
			end if;
		else									-- ha a 8051 vezerli a flasht
			if (ecu_psen='0') then		-- ha a PSEN alacsony
				ecu_data<=flash_data;	-- akkor a flash adatbusza tovabbjut a 8051 adatbuszara
			else								-- ha a PSEN magas
				ecu_data<="HHHHHHHH";	-- akkor a 8051 adatbuszan nagyimpedancias allapotba lep a CPLD
			end if;
		end if;
	end process;
	a3:process (ecu_psen, ecu_addr, pic_mode)
	variable tmp:std_logic:='0';
	begin
		if (pic_mode='0') then
			if (ecu_psen'event and ecu_psen='0') then	-- ha epp most lett a PSEN alacsony
				case tmp is										-- akkor eltaroljuk a pillanatnyi cimbuszt a megfelelo valtozoba
					when '0' =>
						tmp:='1';
						buff0:=ecu_addr;
					when '1' =>
						tmp:='0';
						buff1:=ecu_addr;
					when others =>
						tmp:='0';
				end case;
				pic_int<=tmp;									-- ebbol fogja tudni a pic, hogy mikor van meg mindket cim (amikor ez alacsonyra valt)
			end if;
		else
			tmp:='0';
		end if;
	end process;
			
end Behavioral;

