如何在VHDL测试平台上模拟内存?

时间:2014-01-03 21:06:25

标签: vhdl modelsim

我正在为我的设计编写一个通用测试平台,通过一个非常标准的总线与RAM通信。我查阅了一些例子并写成了这样的:

signal memory: mem_array;
signal mem_address: std_logic_vector(31 downto 0);
signal mem_data: std_logic_vector(31 downto 0);
signal mem_read: std_logic;
signal mem_write: std_logic;

cpu_mem_data <= transport memory(to_integer(unsigned(mem_address))) after DELAY when mem_read = '1' else (others => 'Z');

always : PROCESS
    file in_file: text open read_mode is "in.txt";
    variable line_str: line;
    variable address: std_logic_vector(31 downto 0);
    variable data: std_logic_vector(31 downto 0);
BEGIN
    reset <= '1';

    readline(in_file, line_str);
    hread(line_str, address);
    starting_pc <= address;
    while not endfile(in_file) loop
        readline(in_file, line_str);
        hread(line_str, address);
        read(line_str, data);
        memory(to_integer(unsigned(address))) <= data;
        report "Initialized " & integer'image(to_integer(unsigned(address))) & " to " & integer'image(to_integer(unsigned(data)));
    end loop;

    wait for 30 ns;

    reset <= '0';
WAIT;                                                        
END PROCESS always; 

process (mem_write)
begin
    if (rising_edge(mem_write)) then
        memory(to_integer(unsigned(mem_address))) <= transport mem_data after DELAY;
        report "Will write " & integer'image(to_integer(unsigned(mem_data))) & " to " & integer'image(to_integer(unsigned(mem_address)));
    end if;
end process;

我遇到了两个问题。

在ModelSim Altera SE中编译和模拟时,内存显示所有位置的所有X.报告显示从in.txt文件中读取了正确的值,但没有任何内容写入内存。我也尝试初始化所有位置或只是一个位置到所有'0',它也不起作用。相反,信号starting_pc被设置为正确的值。

另一个问题是延迟分配似乎不起作用。我的设计试图从内存中读取并且mem_read和mem_address被正确设置,但在DELAY时间后mem_data保持高阻抗。

我做错了什么?

1 个答案:

答案 0 :(得分:5)

信号存储器上有多个驱动程序。它由标记为“always”的过程和未标记的过程编写。而是使用如下结构的单个流程:

process
    file in_file: text open read_mode is "in.txt";
    variable line_str: line;
    variable address: std_logic_vector(31 downto 0);
    variable data: std_logic_vector(31 downto 0);
begin
    -- Initialize memory by reading file
    reset <= '1';

    readline(in_file, line_str);
    hread(line_str, address);
    starting_pc <= address;
    while not endfile(in_file) loop
        readline(in_file, line_str);
        hread(line_str, address);
        read(line_str, data);
        memory(to_integer(unsigned(address))) <= data;
        report "Initialized " & integer'image(to_integer(unsigned(address))) & " to " & 
              integer'image(to_integer(unsigned(data)));
    end loop;

    wait for 30 ns;

    reset <= '0';

    -- Write to Memory
    Write_loop : loop 
        wait until rising_edge(mem_write) ;
        memory(to_integer(unsigned(mem_address))) <= transport mem_data after DELAY;
    end loop ;
end process ; 

请注意,在单独的过程中进行代码重置是合适的,但不是必需的。

你在使用整个记忆吗?如果要实现内存的2 ** 32个存储位置,使用信号作为存储元素并使用std_logic_vector作为数据类型,则会占用大量内存并导致模拟器运行速度变慢。

对于某些想法,请参阅http://www.freemodelfoundry.com/

上的内存模型

同样在我们的VHDL测试平台类http://www.synthworks.com/vhdl_testbench_verification.htm中,我们提供了一个实现稀疏内存数据结构的包来简化编码。这些软件包的一个好处是它们使用共享变量,允许多个进程在您尝试处理信号时访问数据结构。