如何解决在模拟中访问 RAM 内容时看不到任何内容
我在尝试设计 SRAM 存储器时遇到了问题。更具体地说,内存有时钟,有一个写使能 - 当高时,可以写数据,当低时,可以读数据 - 一个地址输入,指定写入/读取数据的内存地址.然后,我创建了一个名为user
的模块,方便写操作;因此,写入数据时无需提供内存地址。
我尝试模拟电路时出现问题,因为访问内存内容时看不到任何内容。在测试台上,我指定了一些要存储在内存中的值,然后我提取了数据,但没有成功。
//stores instructions
module sram_1port_instructions(
input clk,//clocked memory
input wr_en,//when high,data is writeen,otherwise is read
input [15:0] address_in,//suppose timer cannot count more than 13ms
input [2:0] wr_data,//3 bit instructions
output reg [2:0] rd_data
);
reg [2:0] memory [2 ** 15 - 1 : 0];
always @(posedge clk) begin
if(wr_en) memory[address_in] <= wr_data;
else rd_data <= memory[address_in];
end
endmodule
//user interface designed for the first memory
module user(
input clk,input wr_en,input [15:0] address_in,input [2:0] wr_data,output [2:0] rd_data
);
reg [15:0] pointer,address;
initial pointer = 16'd0;
sram_1port_instructions i0(.clk(clk),.wr_en(wr_en),.address_in(address_in),.wr_data(wr_data),.rd_data(rd_data));
always @(posedge clk) begin
if(wr_en) begin
address <= pointer;
pointer <= pointer + 1;
end
else address <= address_in;
end
endmodule
//user tb
module user_tb(
output reg clk,wr_en,output reg [15:0] address_in,output reg [2:0] wr_data,output [2:0] rd_data
);
user cut(.clk(clk),.rd_data(rd_data));
initial $dumpvars(0,user_tb);
initial begin
clk = 1'd1;
repeat (2000)
#100 clk = ~clk;
end
initial begin
wr_en = 1'd1;
#100000 wr_en = 1'd0;
end
integer i;
initial begin
wr_data = 3'd0;
for(i = 1;i < 500;i = i + 1) begin
#200 wr_data = i;
end
end
initial begin
address_in = 16'd0;
#100000 address_in = 16'd0;
for(i = 1;i < 500;i = i + 1) begin
#200 address_in = i;
end
end
endmodule
解决方法
当我运行您的模拟并查看波形时,我看到 rd_data
=3 在时间 100000。那时,您读取地址 0,这是您写入地址 0 的最后一个值。否则,所有您对其他地址的读取返回 X(未知)。当您完成所有写入操作时,您只会写入地址 0。从 time=0 到 time=100000、wr_en
=1 和 address_in
=0(在您的 sram_1port_instructions
模块中)。当您查看 VCD 文件中的波形时,您可以看到这一点。
wr_data
每个时钟周期都会改变,但您每个周期都写入相同的地址。
但是,在 user
中,如果您将 address
信号连接到 address_in
模块的 sram_1port_instructions
端口,您将写入不同的地址。
变化:
sram_1port_instructions i0(.clk(clk),.wr_en(wr_en),.address_in(address_in),.wr_data(wr_data),.rd_data(rd_data));
到:
sram_1port_instructions i0(.clk(clk),.address_in(address),.rd_data(rd_data));
您的代码具有连接到 address_in
端口的恒定 address_in
信号。
当我进行更改时,我看到从 RAM 中读取了所有不同的值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。