library ieee
use ieee.std_logic_1164.all
use IEEE.STD_LOGIC_ARITH.all
use IEEE.STD_LOGIC_UNSIGNED.all
entity blk_mem is
generic(
data_width : integer := 8 -- used to change the memory data's width
addr_width : integer := 11) -- used to change the memery address' width
port (
clka : in std_logic
dina : in std_logic_vector(data_width - 1 downto 0)
addra : in std_logic_vector(addr_width - 1 downto 0)
ena : in std_logic
wea : in std_logic
douta : out std_logic_vector(data_width - 1 downto 0)
clkb : in std_logic
dinb : in std_logic_vector(data_width - 1 downto 0)
addrb : in std_logic_vector(addr_width - 1 downto 0)
enb : in std_logic
web : in std_logic
doutb : out std_logic_vector(data_width - 1 downto 0))
end blk_mem
architecture blkmem of blk_mem is
type ram_template is array(2 ** addr_width - 1 downto 0) of std_logic_vector(data_width - 1 downto 0)
shared variable ram1 : ram_template
begin -- blkmem
process (clka)
begin -- process
if clka'event and clka = '1' then -- rising clock edge
if ena = '1' then
douta <= ram1(conv_integer(addra))
if wea = '1' then
ram1(conv_integer(addra)) := dina
end if
else
douta <= (others =>'0')
end if
end if
end process
process (clkb)
begin -- process
if clkb'event and clkb = '1' then -- rising clock edge
if enb = '1' then
doutb <= ram1(conv_integer(addrb))
if web = '1' then
ram1(conv_integer(addrb)) := dinb
end if
else
doutb <= (others =>'0')
end if
end if
end process
end blkmem
个人觉得学好VHDL,最主要的还是实践,如果原先有一点C语言基础,VHDL就不难学,多自行练几次就没问题了!!可以先熟悉语法,了解一些逻辑功能,然后再上网或者在书上找一些小项目做做,最好是手头上自已有实验板,多做几次就OK了!!!以下是你要求的程序,至于读写速度,主要就是看读时钟和写时钟,哪个频率快,就哪个快了!!!
library ieee
use ieee.std_logic_1164.all --调用常用的程序包
use ieee.std_logic_unsigned.all
use ieee.std_logic_arith.all
entity ram_test is --定义实体
generic(width:integer :=8length:integer:=8)--根据这个来改变RAM的大小,width为数据长度,length为数据个数
port(r_clk,w_clk:in std_logic---定义写时钟和读时钟
r_add,w_add:in std_logic_vector(2 downto 0)--写地址和读地址
r_en,w_en:in std_logic--读使能和写使能
d_in:in std_logic_vector(width-1 downto 0)--数据输入
d_out:out std_logic_vector(width-1 downto 0))--数据输出
end entity
architecture art of ram_test is
type memory is array (0 to length-1) of std_logic_vector(width-1 downto 0)---定义一数组类型来存储数据
signal data:memory
begin
process(w_clk,w_add,w_en,d_in)--写数据进程
begin
if w_clk'event and w_clk='1' then--在时钟上升沿来时
if w_en='1' then --若使能为1,则写数据
data(conv_integer(w_add))<=d_in
end if
end if
end process
process(r_clk,r_add,r_en,data)--读数据进程
begin
if r_clk'event and r_clk='1' then
if r_en='1' then
d_out<=data(conv_integer(r_add))
end if
end if
end process
end art
定义数组类型type R_256X8D is array (7 downto 0) of std_logic_vector(7 downto 0)
声明变量
signal myRam : R_256X8D;
myRam使用的时候就像二维数组一样使用,例如要寻址地址是15的ram内容给test
signal test : std_logic_vector(7 downto 0)
test = myRam(15);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)