我一直在尝试用 VHDL 实现一个简单的双端口 Block RAM,但它在模拟中没有产生预期的结果。这是代码:
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
entity rams is
generic ( g_adress_width: integer:= 18;
g_ram_size: integer:= 1000
);
port(
clka : in std_logic;
clkb : in std_logic;
wea : in std_logic;
web : in std_logic;
addra : in std_logic_vector(g_adress_width-1 downto 0);
addrb : in std_logic_vector(g_adress_width-1 downto 0);
dia : in std_logic_vector(15 downto 0);
dib : in std_logic_vector(15 downto 0);
doa : out std_logic_vector(15 downto 0);
dob : out std_logic_vector(15 downto 0));
end rams;
architecture syn of rams is
type ram_type is array (g_ram_size-1 downto 0) of std_logic_vector(15 downto 0);
signal RAM : ram_type;
begin
process (CLKA)
begin
if CLKA'event and CLKA = '1' then
DOA <= RAM(conv_integer(ADDRA));
if WEA = '1' then --always 0
RAM(conv_integer(ADDRA)) <= DIA; --does not execute
end if;
end if;
end process;
process (CLKB)
begin
if CLKB'event and CLKB = '1' then
DOB <= RAM(conv_integer(ADDRB));
if WEB = '1' then
RAM(conv_integer(ADDRB)) <= DIB;
end if;
end if;
end process;
end syn;
Run Code Online (Sandbox Code Playgroud)
这是模拟:
clka和clkb都连接到同一个时钟。我给dib提供了一些任意值(无符号 300 和 355) 。
基本上,doa总是在读取,所以我希望它是未定义的,直到使用dib将某些内容写入这些块 ram 地址,但它始终显示未定义的值。
我期望发生的情况是,当addra再次为 0 时,doa会读取 300 ,而当addra为 15 时,doa 会读取 355。像这样的事情(请原谅我的绘画技巧):
如果有人能指出我做错的事情的正确方向,我将不胜感激。谢谢。
编辑:代码被修改为这个并且它现在可以工作(感谢Paebbels解决方案):
signal RAM : ram_type;
begin
process (CLKA)
begin
if CLKA'event and CLKA = '1' then
DOA <= RAM(to_integer(unsigned(ADDRA)));
if WEA = '1' then --always 0
RAM(to_integer(unsigned(ADDRA))) <= DIA; --does not happen
end if;
end if;
if CLKA'event and CLKA = '1' then
DOB <= RAM(to_integer(unsigned(ADDRB)));
if WEB = '1' then
RAM(to_integer(unsigned(ADDRB))) <= DIB;
end if;
end if;
end process;
end syn;
Run Code Online (Sandbox Code Playgroud)
这种对双时钟 RAM 的描述是错误的。您需要使用:
使用一个信号和两个进程是不正确的。它在一个信号上创建多个驱动器。这反过来又造成了多源问题。虽然您的模拟可以工作,但由于std_logic_vector用户定义的数组类型中已解析的类型,综合将会失败。
此外,为了允许推断 BlockRAM,您需要在 VHDL 代码中表示 BlockRAM 的内部结构。这意味着您需要在地址路径上添加管道寄存器。
您应该阅读UG901 - Vivado Synthesis Guide并搜索“RAM HDL Coding Techniques”。
此外,您应该使用 packagenumeric_std而std_logic_unsigned不是 IEEE 官方包。
可以在PoC 库中找到可用的真双端口 (TDP) BlockRAM 实现:PoC.mem.ocram.tdp。该实现也适用于 Altera/Intel FPGA 和 Lattice FPGA。
-- RAM can be inferred correctly only if '-use_new_parser yes' is enabled in XST options
subtype word_t is std_logic_vector(D_BITS - 1 downto 0);
type ram_t is array(0 to DEPTH - 1) of word_t;
signal ram : ram_t;
signal a1_reg : unsigned(A_BITS-1 downto 0);
signal a2_reg : unsigned(A_BITS-1 downto 0);
begin
process (clk1, clk2)
begin -- process
if rising_edge(clk1) then
if ce1 = '1' then
if we1 = '1' then
ram(to_integer(a1)) <= d1;
end if;
a1_reg <= a1;
end if;
end if;
if rising_edge(clk2) then
if ce2 = '1' then
if we2 = '1' then
ram(to_integer(a2)) <= d2;
end if;
a2_reg <= a2;
end if;
end if;
end process;
q1 <= (others => 'X') when SIMULATION and is_x(std_logic_vector(a1_reg)) else
ram(to_integer(a1_reg)); -- returns new data
q2 <= (others => 'X') when SIMULATION and is_x(std_logic_vector(a2_reg)) else
ram(to_integer(a2_reg)); -- returns new data
Run Code Online (Sandbox Code Playgroud)