1

我在尝试设计 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),.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
4

1 回答 1

0

当我运行您的仿真并查看波形时,我rd_data在时间 100000 看到 =3。那时,您读取地址 0,这是您写入地址 0 的最后一个值。否则,您对其他地址的所有读取都返回 X (未知)。当您执行所有写入操作时,您只写入地址 0。从 time=0 到 time=100000、wr_en=1 和address_in=0(在您的sram_1port_instructions模块内)。当您查看 VCD 文件中的波形时,您可以看到这一点。 wr_data每个时钟周期都会改变,但每个周期都写入相同的地址。

但是,在 中user,如果您将address信号连接到模块的 toaddress_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),.wr_en(wr_en),.address_in(address),.wr_data(wr_data),.rd_data(rd_data));

您的代码将恒定address_in信号连接到address_in端口。

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

于 2021-01-02T15:38:09.077 回答