如何制作一个内存模块,其中DATA
总线宽度作为参数传递给每个实例,我的设计根据参数重新配置自己?例如,假设我有字节可寻址存储器,DATA-IN
总线宽度为 32 位(每个周期写入 4 个字节)并且DATA-OUT
为 16 位(每个周期读取 2 个字节)。对于其他实例DATA-IN
是 64 位和DATA-OUT
16 位。对于所有这些情况,我的设计都应该有效。
我尝试的是根据设计参数生成写指针值,例如DATA-IN
32位,写指针在写时每个周期都会增加4。对于 64 位,增量将是 8,依此类推。
问题是:如何根据传递给实例的参数,在单个周期内写入 4 个或 8 个或 16 个字节?
//Something as following I want to implement. This memory instance can be considered as internal memory of FIFO having different datawidth for reading and writing in case you think of an application of such memory
module mem#(parameter DIN=16, parameter DOUT=8, parameter ADDR=4,parameter BYTE=8)
(
input [DIN-1:0] din,
output [DOUT-1:0] dout,
input wen,ren,clk
);
localparam DEPTH = (1<<ADDR);
reg [BYTE-1:0] mem [0:DEPTH-1];
reg wpointer=5'b00000;
reg rpointer=5'b00000;
reg [BYTE-1:0] tmp [0:DIN/BYTE-1];
function [ADDR:0] ptr;
input [4:0] index;
integer i;
begin
for(i=0;i<DIN/BYTE;i=i+1) begin
mem[index] = din[(BYTE*(i+1)-1):BYTE*(i)]; // something like this I want to implement, I know this line is not allowed in verilog, but is there any alternative to this?
index=index+1;
end
ptr=index;
end
endfunction
always @(posedge clk) begin
if(wen==1)
wpointer <= wptr(wpointer);
end
always @(posedge clk) begin
if(ren==1)
rpointer <= ptr(rpointer);
end
endmodule