<: ;#Component and file information :>
<: set ComponentName [getComponentNameString] :>
<: set icap_ext [getIntValue "C_ICAP_EXTERNAL"] :>
<: if { $icap_ext == 0 } { ttcl_return; } :>
<: setOutputDirectory [ttcl_string_concat $ComponentName "/example_design"] :>
<: setFileName [ttcl_string_concat $ComponentName "_arbiter"] :>
<: set c_family [getProjectFamily] :>
<: set enable_async [getBooleanValue "C_ENABLE_ASYNC"] :>
<: set data_width [getStringValue "C_ICAP_WIDTH_S"] :>
<: set startup [getIntValue "C_INCLUDE_STARTUP"] :>
<: set large_data [getIntValue "C_OPERATION"] :>
<: set uc_family [get_property PARAM_VALUE.uc_family] :>
<: set icap_width [get_property PARAM_VALUE.C_ICAP_DWIDTH] :>
<: set dev_id [getIntValue "C_DEVICE_ID"] :>
<: set devi_id [string replace $dev_id 0 1 ""] :>
<: set shared_startup [getIntValue "C_SHARED_STARTUP"] :>
<: set wd [ file dirname [ get_property xml_file_name [ current_ipbom ] ] ] :>
<: setFileExtension ".vhd" :>
-- (c) Copyright 2009 - 2013 Xilinx, Inc. All rights reserved.
-- 
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
-- 
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
-- 
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
-- 
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.


library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.std_logic_misc.all;


library unisim;
use unisim.vcomponents.all;
library work;
entity <=:$ComponentName:>_arbiter is
  generic (
    C_NUM_ICAP_MASTERS  : integer := 2);
   port (
         icap_clk                 : in std_logic;
         resetn                   : in std_logic;
         cap_req                  : in std_logic_vector(C_NUM_ICAP_MASTERS-1 downto 0);
         cap_rel                  : out std_logic_vector(C_NUM_ICAP_MASTERS-1 downto 0); 
         cap_gnt                  : out std_logic_vector(C_NUM_ICAP_MASTERS-1 downto 0);
         cap_avail                : out std_logic;
         icap_i_0                 : out  std_logic_vector(31 downto 0);  
         icap_i_1                 : out  std_logic_vector(31 downto 0);  
         icap_o_0                 : in std_logic_vector(31 downto 0);
         icap_o_1                 : in std_logic_vector(31 downto 0);
         icap_csib                : in std_logic_vector(C_NUM_ICAP_MASTERS-1 downto 0);
         icap_rdwrb               : in std_logic_vector(C_NUM_ICAP_MASTERS-1 downto 0)

);

end entity;

architecture impl of <=:$ComponentName:>_arbiter is
  attribute DowngradeIPIdentifiedWarnings: string;
  attribute DowngradeIPIdentifiedWarnings of impl : architecture is "yes";
signal icap_gnt_int : std_logic_vector(C_NUM_ICAP_MASTERS-1 downto 0);
signal icap_rel_int : std_logic_vector(C_NUM_ICAP_MASTERS-1 downto 0);
signal icap_i : std_logic_vector(31 downto 0);
signal icap_o : std_logic_vector(31 downto 0);
signal icap_csib_int : std_logic;--std_logic_vector(C_NUM_ICAP_MASTERS-1 downto 0));
signal icap_rdwrb_int : std_logic;--std_logic_vector(C_NUM_ICAP_MASTERS-1 downto 0));
signal zero : std_logic_vector(31 downto 0) := (others => '0');
 
begin
cap_gnt <= icap_gnt_int;
cap_rel <= icap_rel_int;
icap_i_0 <= icap_i when(icap_gnt_int(0) = '1' and cap_req(0) = '1') else zero;
icap_i_1 <= icap_i when(icap_gnt_int(1) = '1'and cap_req(1) = '1') else zero;
icap_o <= icap_o_0 when(icap_gnt_int(0) = '1'and cap_req(0) = '1') else icap_o_1;
icap_csib_int <= icap_csib(0) when(icap_gnt_int(0) = '1' and cap_req(0) = '1') else icap_csib(1);
icap_rdwrb_int <= icap_rdwrb(0) when(icap_gnt_int(0) = '1' and cap_req(0) = '1') else icap_rdwrb(1);


<: if {$uc_family == 0} { :>	

       ICAP_VIRTEX7_I : ICAPE2
         generic map (
           DEVICE_ID         => X"<=: devi_id :>",
           ICAP_WIDTH        => "X<=:icap_width :>"
         )
         port map (
           clk               => icap_clk,
           csib               => icap_csib_int,
           rdwrb             => icap_rdwrb_int,
           i                 => icap_o,
       --    busy              => icap_busy,
           o                 => icap_i);

       cap_avail <= '0';


<: } else { :>
    ICAP_VIRTEXU_I : ICAPE3
         generic map ( 
           DEVICE_ID         => X"<=: devi_id :>" 
           --ICAP_WIDTH        => C_ICAP_WIDTH
         )
         port map (
           clk               => icap_clk,
           csib              => icap_csib_int,
           rdwrb             => icap_rdwrb_int,
           i                 => icap_o,
           o                 => icap_i,
           prerror           => open,
           prdone            => open,
           avail             => cap_avail);



<: } :>


ARBITER_GNT_GEN : process (icap_clk) is
begin
          if (icap_clk'event and icap_clk = '1') then
             if (resetn = '0') then
               icap_gnt_int <= (others => '0');
               icap_rel_int <= (others => '0');
             --  icap_i_0 <= (others => '0');
             --  icap_i_1 <= (others => '0');
             --  icap_o <= (others => '0');
              -- icap_csib_int <= '0';
             --  icap_rdwrb_int <= '0';
             elsif (cap_req(0) = '1' and icap_gnt_int(0) = '0' and icap_gnt_int(1) = '0') then
               icap_rel_int <= (others => '0');
               icap_gnt_int(0) <= '1';
               icap_gnt_int(1) <= '0';
             --  icap_i_0 <= icap_i;
            --   icap_i_1 <= (others => '0');
             --  icap_o <= icap_o_0;
             --  icap_csib_int <= icap_csib(0);
             --  icap_rdwrb_int <= icap_rdwrb(0);
             elsif (cap_req(0) = '1' and icap_gnt_int(0) = '1' and icap_gnt_int(1) = '0') then
               icap_rel_int <= (others => '0');
               icap_gnt_int(0) <= '1';
               icap_gnt_int(1) <= '0';
            ----   icap_i_0 <= icap_i;
             --  icap_i_1 <= (others => '0');
            --   icap_o <= icap_o_0;
             --  icap_csib_int <= icap_csib(0);
             --  icap_rdwrb_int <= icap_rdwrb(0);
             elsif (cap_req(0) = '1' and icap_gnt_int(0) = '0' and icap_gnt_int(1) = '1' and cap_req(1) = '1') then
               icap_rel_int(1) <= '1';
               icap_rel_int(0) <= '0';
               icap_gnt_int <= "10";
            --   icap_i_1 <= icap_i;
            --   icap_i_0 <= (others => '0');
             --  icap_o <= icap_o_1;
             --  icap_csib_int <= icap_csib(1);
             --  icap_rdwrb_int <= icap_rdwrb(1);
             elsif (cap_req(0) = '1' and icap_gnt_int(0) = '0' and icap_gnt_int(1) = '1' and cap_req(1) = '0' and icap_rel_int(1) = '1') then
               icap_rel_int <= (others => '0');
               icap_gnt_int <= "01"; -- Granting to first master
            --   icap_i_0 <= icap_i;
            --   icap_i_1 <= (others => '0');
            --   icap_o <= icap_o_0;
             --  icap_csib_int <= icap_csib(0);
             --  icap_rdwrb_int <= icap_rdwrb(0);
             elsif (cap_req(0) = '1' and icap_gnt_int(0) = '0' and icap_gnt_int(1) = '1' and cap_req(1) = '0' and icap_rel_int(1) = '0') then
               icap_rel_int <= "10";
               icap_gnt_int <= "10"; -- Second master
            --   icap_i_1 <= icap_i;
            --   icap_i_0 <= (others => '0');
            ---   icap_o <= icap_o_1;
            --   icap_csib_int <= icap_csib(1);
             --  icap_rdwrb_int <= icap_rdwrb(1);
             elsif (cap_req(0) = '0' and cap_req(1) = '1' and icap_gnt_int(0) = '0' and icap_gnt_int(1) = '0') then
               icap_rel_int <= (others => '0');
               icap_gnt_int <= "10"; -- Granting to Second master
             --  icap_i_1 <= icap_i;
            --   icap_i_0 <= (others => '0');
             --  icap_o <= icap_o_1;
             --  icap_csib_int <= icap_csib(1);
             --  icap_rdwrb_int <= icap_rdwrb(1);
             elsif (cap_req(0) = '0' and cap_req(1) = '1' and icap_gnt_int(0) = '0' and icap_gnt_int(1) = '1') then
               icap_rel_int <= (others => '0');
               icap_gnt_int <= "10"; -- Second master
            --   icap_i_1 <= icap_i;
            --   icap_i_0 <= (others => '0');
            --   icap_o <= icap_o_1;
             ---  icap_csib_int <= icap_csib(1);
             --  icap_rdwrb_int <= icap_rdwrb(1);
             elsif (cap_req(0) = '0' and cap_req(1) = '1' and icap_gnt_int(0) = '1' and icap_rel_int(0) = '0' and icap_gnt_int(1) = '0') then
               icap_rel_int <= "01";
               icap_gnt_int <= "01"; -- First master
             --  icap_i_0 <= icap_i;
             --  icap_i_1 <= (others => '0');
             ----  icap_o <= icap_o_0;
             ---  icap_csib_int <= icap_csib(0);
            --   icap_rdwrb_int <= icap_rdwrb(0);
             elsif (cap_req(0) = '0' and cap_req(1) = '1' and icap_gnt_int(0) = '1' and icap_rel_int(0) = '1'  and icap_gnt_int(1) = '0') then
               icap_rel_int <= (others => '0');
               icap_gnt_int <= "10"; -- Granting to Second master
            --   icap_i_1 <= icap_i;
            --   icap_i_0 <= (others => '0');
            --   icap_o <= icap_o_1;
             ----  icap_csib_int <= icap_csib(1);
           --    icap_rdwrb_int <= icap_rdwrb(1);
             elsif (cap_req(0) = '0' and cap_req(1) = '0' and icap_gnt_int(0) = '1' and icap_rel_int(0) = '0') then
               icap_rel_int <= (others => '0');
               icap_gnt_int <= "00"; -- de-asserting grant
             --  icap_i_0 <= (others => '0');--icap_i;
             --  icap_i_0 <= (others => '0');
             --  icap_o <= (others => '0');--icap_o_1;
             --  icap_csib_int <= '0';--icap_csib(0);
             --  icap_rdwrb_int <= '0';--icap_rdwrb(0);
             elsif (cap_req(0) = '0' and cap_req(1) = '0' and icap_gnt_int(0) = '0' and icap_gnt_int(1) = '1' and icap_rel_int(0) = '0') then
               icap_rel_int <= (others => '0');
               icap_gnt_int <= "00"; -- de-asserting grant
            --   icap_i_0 <= (others => '0');--icap_i;
            --   icap_i_0 <= (others => '0');
            --   icap_o <= (others => '0');--icap_o_1;
            --   icap_csib_int <= '0';--icap_csib(0);
            --   icap_rdwrb_int <= '0';--icap_rdwrb(0);
             end if;
          end if; 

end process ARBITER_GNT_GEN;

end impl;

