<: set mem_type_0 [getIntValue "C_MEM0_TYPE"] :>
<: set mem_type_1 [getIntValue "C_MEM1_TYPE"] :>
<: set mem_type_2 [getIntValue "C_MEM2_TYPE"] :>
<: set mem_type_3 [getIntValue "C_MEM3_TYPE"] :>
<: set mem_ban [getIntValue "C_NUM_BANKS_MEM"] :>
<: if {$mem_type_0 == 1 || ($mem_type_1 == 1 && $mem_ban > 1) || ($mem_type_2 == 1 && $mem_ban > 2) || ($mem_type_3 == 1 && $mem_ban > 3)} { :>
<: set deliver true :>
<: } else { :>
<: set deliver false :>
<: } :>
<: if {!$deliver} {ttcl_return;} :>
<: set ComponentName [getComponentNameString] :>
<: set en_reg [getIntValue "C_S_AXI_EN_REG"] :>
<: set id_wid [getIntValue "C_S_AXI_MEM_ID_WIDTH"] :>
<: set id_width [expr {$id_wid - 1}] :>
<: set mem_wid [getIntValue "C_S_AXI_MEM_DATA_WIDTH"] :>
<: set mem_width [expr {$mem_wid - 1}] :>
<: set mem_strb [expr {[expr {$mem_wid / 8}] -1}]:>
<: set mem_banks [expr {$mem_ban - 1}] :>
<: set pipedelay_0 [getIntValue "C_SYNCH_PIPEDELAY_0"] :>
<: set parity_0 [getIntValue "C_PARITY_TYPE_MEM_0"] :>
<: set axi_clk_per [getIntValue "C_AXI_CLK_PERIOD_PS"] :>
<: setOutputDirectory [ttcl_string_concat $ComponentName "/example_design"] :>
<: setFileName "asram_model" :>
<: set wd [ file dirname [ get_property xml_file_name [ current_ipbom ] ] ] :>

<: setFileExtension ".vhd" :>
--ASRAM Memory Model
-- file: asram_model.vhd
-- 
-- (c) Copyright 2008 - 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.
-- 
------------------------------------------------------------------------------
-- User entered comments
------------------------------------------------------------------------------
-- This is a self-desigined Memory model developed for EMC Testing purpose
--
------------------------------------------------------------------------------

LIBRARY ieee;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.NUMERIC_STD.ALL;
USE IEEE.STD_LOGIC_MISC.all;

entity asram_model is
  generic (
    data_width     : integer := 32;
    addr_width     : integer := 32;
-----timing parameters------------
    Trc            : integer := 0;  --Read Cycle Time
    Taa            : integer := 0;  --Address to Data valid Time
    Toha           : integer := 0;  --Data Hold from Address Change
    Tace           : integer := 0;  --CE LOW to Data Valid
    Tdoe           : integer := 0;  --OE LOW to Data Valid
    Thzoe          : integer := 0;  --OE HIGH to High Z
    Thzce          : integer := 0;  --CE HIGH to High Z
    Twc            : integer := 0;  --Write Cycle Time
    Tsce           : integer := 0;  --CE LOW to Write End
    Taw            : integer := 0;  --Address Set-Up to Write End
    Tha            : integer := 0;  --Address Hold from Write End
    Tsa            : integer := 0;  --Address Set-Up to Write Start
    Tpwe           : integer := 0;  --WE Pulse Width
    Tsd            : integer := 0;  --Data Set-Up to Write End
    Thd            : integer := 0   --Data Hold from Write End
  );
  port (
    CLK    : IN STD_LOGIC;
    D_IN   : IN STD_LOGIC_VECTOR (data_width-1 downto 0);
    D_OUT  : OUT STD_LOGIC_VECTOR (data_width-1 downto 0);
    A      : IN STD_LOGIC_VECTOR (addr_width-1 downto 0);
    CEN    : IN STD_LOGIC;
    OEN    : IN STD_LOGIC;
    WEN    : IN STD_LOGIC;
    BEN    : IN STD_LOGIC_VECTOR ((data_width/8)-1 downto 0)
);
end entity;

architecture asram_model_arch of asram_model is

--------------------------------------------------------------------------------------
-- below attributes are added to reduce the synth warnings in Vivado tool
  attribute DowngradeIPIdentifiedWarnings: string;
  attribute DowngradeIPIdentifiedWarnings of asram_model_arch : architecture is "yes";
--------------------------------------------------------------------------------------

   function max2 (num1, num2 : integer) return integer is
   begin
       if num1 >= num2 then
           return num1;
       else
           return num2;
       end if;
   end function max2;

  constant axi_clk_per : integer := <=: $axi_clk_per :>; --Obtained from EMC settings
  constant TRD_CLKS : integer range 0 to 31
                 := ((max2(1,max2(Tace,Taa))-1)/axi_clk_per);
  constant THZ_CLKS : integer range 0 to 31
                  := ((max2(1,max2(Thzce,Thzoe))-1)/axi_clk_per);

  --TYPE mem_array IS ARRAY ((2 ** addr_width) -1 DOWNTO 0) OF STD_LOGIC_VECTOR(data_width - 1 DOWNTO 0);
  attribute ram_style: string;
  TYPE mem_array IS ARRAY ((2 ** 9)-1 DOWNTO 0) OF STD_LOGIC_VECTOR(data_width - 1 DOWNTO 0);
  SIGNAL ram : mem_array;
  attribute ram_style of ram : SIGNAL is "distributed";

  SIGNAL ce,rd_en,wr_en,rd_en_o : STD_LOGIC := '0';
  SIGNAL a_tmp : STD_LOGIC_VECTOR (addr_width-1 downto 0);
  SIGNAL addr_event : STD_LOGIC;
  SIGNAL dout : STD_LOGIC_VECTOR (data_width - 1 DOWNTO 0) := (OTHERS => 'Z');
  SIGNAL counter : INTEGER := 0;
  SIGNAL en_counter: STD_LOGIC := '0';

begin
-------Internal Signal Assignments-------------
  ce <= not(CEN);
  rd_en_o <= ce and WEN and not(OEN);
  wr_en <= ce and not(WEN);
  addr_event <= '0' when A = a_tmp else '1';

----This Process is to delay Icoming Address ---
PROCESS_ADDR_DLY:process (CLK) begin
  if CLK'event and CLK = '1' then
    a_tmp <= A;
  end if;
end process PROCESS_ADDR_DLY;
  
----Counter Enable Generation Process ----------
PROCESS_EN_CNT:process (CLK) begin
 if CLK'event and CLK = '1' then
  if (rd_en_o = '1' and addr_event = '1') then
     en_counter <= '1';
  elsif counter = TRD_CLKS then
     en_counter <= '0';
  else
     en_counter <= '0';
  end if;
 end if;
end process PROCESS_EN_CNT;

----Counter for Delay Generation ---------------
PROCESS_DLY_CNT:process (CLK) begin
  if CLK = '1' and CLK'event then
    if addr_event = '1' then
      counter <= counter + 1;
    else
      counter <= 0;
    end if;
  end if;
end process PROCESS_DLY_CNT;

----Read Enable Generation process -------------
PROCESS_RD_EN:process (CLK) begin
  if CLK = '1' and CLK'event then
    if counter = TRD_CLKS then
     rd_en <= '1';
    else 
     rd_en <= '0';
    end if;
  end if;
end process PROCESS_RD_EN;

----Writing Incoming Data to RAM ---------------
PROCESS_RD:process (CLK) begin
 if CLK'event and CLK = '1' then
  if wr_en = '1' then
    ram(TO_INTEGER(UNSIGNED(A(8 downto 0)))) <= D_IN;
  end if;
 end if;
end process PROCESS_RD;

 D_OUT <= ram(TO_INTEGER(UNSIGNED(A(8 downto 0)))) when (rd_en = '1' and rd_en_o = '1') else (OTHERS => 'Z');

end asram_model_arch;
