在模拟中访问 RAM 内容时看不到任何内容

Can't see anything when accessing RAM contents in simulation

我在尝试设计 SRAM 内存时遇到了问题。更具体地说,内存是有时钟的,有一个写使能 - 当高时,可以写入数据,当低时,可以读取数据 - 一个地址输入,它指定内存地址 to/from 哪些数据是 written/read。然后,我创建了一个名为user的模块,方便写操作;因此,写入数据时无需提供内存地址。

我在尝试模拟电路时出现问题,因为访问内存内容时看不到任何东西。在test bench里面,我指定了一些值存入内存,然后,我提取数据,但是没有成功。

我在这里附上了代码。

//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),.wr_en(wr_en),.address_in(address_in),.wr_data(wr_data),.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。否则,您对其他地址的所有读取 return X(未知)。当您执行所有写入操作时,您只写入地址 0。从时间 = 0 到时间 = 100000,wr_en=1 和 address_in=0(在您的 sram_1port_instructions 模块内)。当您查看 VCD 文件中的波形时,您可以看到这一点。 wr_data 每个时钟周期都会改变,但你每个周期都写入相同的地址。

但是,在user中,如果你将address信号连接到sram_1port_instructions模块的to address_in端口,你将写入不同的地址。

变化:

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),.wr_en(wr_en),.address_in(address),.wr_data(wr_data),.rd_data(rd_data));

您的代码将常量 address_in 信号连接到 address_in 端口。

当我进行更改时,我看到所有不同的值都从 RAM 中读取。