firmware_tools/verification/mem_model/mem_model.vhd

225 lines
8.2 KiB
VHDL

-------------------------------------------------------------------------------
-- Title : Memory model
-- Project : EL-GENERIC-IP
-------------------------------------------------------------------------------
-- File : mem_model.vhd
-- Author : Simon Southwell
-- Company : Global Inkjet Systems
-- Created : 2019-09-27
-- Platform :
-- Standard : VHDL 2008
-------------------------------------------------------------------------------
-- Description: VHDL for the C memory model
-------------------------------------------------------------------------------
-- Copyright (c) 2019 Global Inkjet Systems
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.mem_model_pkg.all;
entity mem_model is
generic (
AVM_TX_WR_DATA_WIDTH : natural range 32 to 128 := 32
);
port (
clk : in std_logic;
rst_n : in std_logic;
-- Avalon CSR slave interface
avs_address : in std_logic_vector(31 downto 0) := (others => '0');
avs_write : in std_logic := '0';
avs_writedata : in std_logic_vector(31 downto 0) := (others => '0');
avs_read : in std_logic := '0';
avs_readdata : out std_logic_vector(31 downto 0);
-- Avalon Master interface
avm_rx_waitrequest : out std_logic;
avm_rx_burstcount : in std_logic_vector(11 downto 0) := (others => '0');
avm_rx_address : in std_logic_vector(31 downto 0) := (others => '0');
avm_rx_read : in std_logic := '0';
avm_rx_readdata : out std_logic_vector(31 downto 0);
avm_rx_readdatavalid : out std_logic;
avm_tx_waitrequest : out std_logic;
avm_tx_burstcount : in std_logic_vector(11 downto 0) := 12x"0";
avm_tx_address : in std_logic_vector(31 downto 0) := 32x"0";
avm_tx_write : in std_logic := '1';
avm_tx_writedata : in std_logic_vector(AVM_TX_WR_DATA_WIDTH-1 downto 0) := (others => '0');
wr_port_valid_0 : in std_logic := '0';
wr_port_data_0 : in std_logic_vector(31 downto 0) := 32x"0";
wr_port_addr_0 : in std_logic_vector(31 downto 0) := 32x"0";
wr_port_valid_1 : in std_logic := '0';
wr_port_data_1 : in std_logic_vector(31 downto 0) := 32x"0";
wr_port_addr_1 : in std_logic_vector(31 downto 0) := 32x"0";
wr_port_valid_2 : in std_logic := '0';
wr_port_data_2 : in std_logic_vector(31 downto 0) := 32x"0";
wr_port_addr_2 : in std_logic_vector(31 downto 0) := 32x"0";
wr_port_valid_3 : in std_logic := '0';
wr_port_data_3 : in std_logic_vector(31 downto 0) := 32x"0";
wr_port_addr_3 : in std_logic_vector(31 downto 0) := 32x"0"
);
end entity;
architecture model of mem_model is
begin
-- Update process
pUPDATE : process
variable avs_readdata_int : integer;
variable avm_rx_readdatavalid_int : std_logic;
variable avm_rx_readdata_int : std_logic_vector(31 downto 0);
variable readdata : integer;
variable rd_addr : integer;
variable wr_addr : integer;
variable rx_count : integer := 0;
variable tx_count : integer := 0;
variable avm_rx_waitrequest_int : std_logic := '0';
variable avm_tx_waitrequest_int : std_logic := '0';
begin
while true loop
-- Can't have a sensitivity list for the process and process delta cycles in VHDL,
-- so emulate the clock edge with a wait here.
wait until clk'event and clk = '1';
if rst_n = '0' then
rx_count := 0;
rd_addr := 0;
tx_count := 0;
wr_addr := 0;
avm_tx_waitrequest <='0';
avm_rx_waitrequest <='0';
avm_rx_readdatavalid <='0';
avm_rx_readdatavalid_int :='0';
avs_readdata <= 32x"00000000";
else
-- Default some of the outputs
avs_readdata_int := 0;
-- Update the output valid signal with that calculated last cycle
avm_rx_readdatavalid <= avm_rx_readdatavalid_int;
-- read data is valid (internally) whenever the RX counter is non-zero
if rx_count /= 0 then
avm_rx_readdatavalid_int := '1';
else
avm_rx_readdatavalid_int := '0';
end if;
-- Update RX bus output with value calculated from last cycle
avm_rx_readdata <= avm_rx_readdata_int;
-- Internal read data is updated from that fetched from model last cycle.
avm_rx_readdata_int := std_logic_vector(to_signed(readdata, 32));
-- If a slave read, return memory contents
if avs_read = '1' then
MemRead(to_integer(signed(avs_address(29 downto 0))) * 4, avs_readdata_int);
end if;
avs_readdata <= std_logic_vector(to_signed(avs_readdata_int, 32));
-- wait until the negative edge of the clock
wait until Clk'event and Clk = '0';
-- If a slave write, update memory
if avs_write = '1' then
MemWrite(to_integer(signed(avs_address(29 downto 0))) * 4, to_integer(signed(avs_writedata)));
end if;
if tx_count = 0 and rx_count = 0 then
avm_rx_waitrequest_int := '0';
else
avm_rx_waitrequest_int := '1';
end if;
avm_rx_waitrequest <= avm_rx_waitrequest_int;
if rx_count = 0 or tx_count /= 0 then
avm_tx_waitrequest_int := '0';
else
avm_tx_waitrequest_int := '1';
end if;
avm_tx_waitrequest <= avm_tx_waitrequest_int;
-- If a new master read request comes in (and not active), latch the rx_count and address values
if avm_rx_read = '1' and avm_rx_waitrequest_int = '0' then
-- Load RX count with burst + 1 as we are going to decrement it immediately
rx_count := to_integer(signed(avm_rx_burstcount) + 1);
rd_addr := to_integer(signed(avm_rx_address));
end if;
-- If a new master write request comes in (and not active), latch the tx_count and address values
if avm_tx_write = '1' and avm_tx_waitrequest_int = '0' and tx_count = 0 then
tx_count := to_integer(signed(avm_tx_burstcount));
wr_addr := to_integer(signed(avm_tx_address));
end if;
-- If an active read transfer in progress, transfer data
if rx_count /= 0 then
MemRead(rd_addr, readdata);
-- Decrement the word count
rx_count := rx_count - 1;
-- Increment the read address
rd_addr := rd_addr + 4;
-- Set the outputs with a valid word
-- avm_rx_readdatavalid_int = 1'b1;
end if;
-- If an active write transfer in progress, transfer data
if avm_tx_write = '1' and avm_tx_waitrequest_int = '0' and tx_count /= 0 then
for i in AVM_TX_WR_DATA_WIDTH/32-1 downto 0 loop
MemWrite(wr_addr, to_integer(signed(avm_tx_writedata(i*32+31 downto i*32))));
-- Increment the write address
wr_addr := wr_addr + 4;
end loop;
-- Decrement the word count
tx_count := tx_count - 1;
end if;
-- If a write port access valid, write data
if wr_port_valid_0 = '1' then
MemWrite(to_integer(signed(wr_port_addr_0)), to_integer(signed(wr_port_data_0)));
end if;
if wr_port_valid_1 = '1' then
MemWrite(to_integer(signed(wr_port_addr_1)), to_integer(signed(wr_port_data_1)));
end if;
if wr_port_valid_2 = '1' then
MemWrite(to_integer(signed(wr_port_addr_2)), to_integer(signed(wr_port_data_2)));
end if;
if wr_port_valid_3 = '1' then
MemWrite(to_integer(signed(wr_port_addr_3)), to_integer(signed(wr_port_data_3)));
end if;
end if;
end loop;
end process;
end architecture;