---------------------------------------------------------------------------
-- NDT Solutions Ltd. Proprietary Information
-- Company Confidential
-- Copyright 2004
--
-- Filename: SramModel.vhd
-- Language: VHDL '93
-- Project:  Digitiser Card FPGAs
--
-- Design Description:
-- Simple SRAM model - not very clever at all
---------------------------------------------------------------------------

library IEEE;
use IEEE.Std_Logic_1164.all;
use IEEE.Std_Logic_Arith.all;


entity SramModel is
  generic (gDataBusWidth : integer := 32;
           gAddrBusWidth : integer := 18);
  port (pData : inout std_logic_vector(gDataBusWidth-1 downto 0);
        pAddr : in    std_logic_vector(gAddrBusWidth-1 downto 0);
        pnCS  : in    std_ulogic;
        pnOE  : in    std_ulogic;
        pnWE  : in    std_ulogic);
end entity SramModel;

architecture Behave of SramModel is

  signal debug: integer;

begin

  -- SRAM Writes
  GenSramAccess: process(pnCS, pnWE, pnOE, pAddr, pData)
    type tMemoryArray is array (0 to 2 ** gAddrBusWidth) of std_logic_vector(gDataBusWidth-1 downto 0);
    variable vMemoryArray: tMemoryArray;
  begin

    -- Read access
    if (pnCS'event and pnCS = '0' and pnOE = '0')
    or (pnOE'event and pnOE = '0' and pnCS = '0')
    or pAddr'event then
      pData <= transport (others => 'X') after 1 ns;
      pData <= transport vMemoryArray(conv_integer(unsigned(pAddr))) after 10 ns;
    elsif pnCS = '1' or pnOE = '1' then
      pData <= (others => 'Z');
    end if;

    -- Write access
    if pnCS = '0' and pnWE = '0' then
      vMemoryArray(conv_integer(unsigned(pAddr))) := pData;
    end if;

  end process GenSramAccess;


  -- Do some basic SRAM checks, these will be extended as required

  -- Check for nWE being active when nCE goes active or inactive
  Check1: process(pnCS)
  begin
    if falling_edge(pnCS) then
      if (not falling_edge(pnWE)) and pnWE = '0' then
        report "SramModel: Error - Falling edge of nCS seen while nWE is already low"
          severity failure;
      end if;
    elsif rising_edge(pnCS) then
      if pnWE = '0' then
        report "SramModel: Error - Rising edge of nCS seen while nWE is still low"
          severity failure;
      end if;
    end if;
  end process Check1;


end Behave;

