【FPGA】单端口RAM的设计(同步读、同步写)
Single Port RAM Synchronous Read/Write
这篇博文介绍单端口同步读写RAM,在之前的博文中,也介绍过类似的设计:【Verilog HDL 训练】第 13 天(存储器、SRAM)
在这篇博文中,与知识星球里的伙伴们交流,真是让我受益匪浅呀。
单端口同步读写RAM的设计没什么可描述的,代码不麻烦,看起来最为清晰:
输入输出:
//--------------Input Ports-----------------------
input clk ;
input [ADDR_WIDTH-1:0] address ;
input cs ;
input we ;
input oe ;//--------------Inout Ports-----------------------
inout [DATA_WIDTH-1:0] data ;
对RAM的读写相对于时钟clk同步,当输入条件(cs、we、oe)满足的情况下,对inout端口 data同步读写。
给出Verilog HDL描述文件:
-
`timescale 1ns / 1ps
-
//
-
// Engineer: Jiashan Lee
-
// Create Date: 2019/05/21 15:46:55
-
// Design Name: ram_sp_sr_sw
-
// Module Name: ram_sp_sr_sw
-
// Function : Synchronous read write RAM
-
// Revision 0.01 - File Created
-
// Additional Comments:
-
//
-
-
module ram_sp_sr_sw (
-
clk , // Clock Input
-
address , // Address Input
-
data , // Data bi-directional
-
cs , // Chip Select
-
we , // Write Enable/Read Enable
-
oe // Output Enable
-
);
-
-
parameter DATA_WIDTH = 8 ;
-
parameter ADDR_WIDTH = 8 ;
-
parameter RAM_DEPTH = 1 << ADDR_WIDTH;
-
-
//--------------Input Ports-----------------------
-
input clk ;
-
input [ADDR_WIDTH-1:0] address ;
-
input cs ;
-
input we ;
-
input oe ;
-
-
//--------------Inout Ports-----------------------
-
inout [DATA_WIDTH-1:0] data ;
-
-
//--------------Internal variables----------------
-
reg [DATA_WIDTH-1:0] data_out ;
-
reg [DATA_WIDTH-1:0] mem [0:RAM_DEPTH-1];
-
reg oe_r;
-
-
-
//initialization
-
-
// synopsys_translate_off
-
integer i;
-
initial begin
-
for(i=0; i < RAM_DEPTH; i = i + 1) begin
-
mem[i] = 8'h00;
-
end
-
end
-
// synopsys_translate_on
-
-
-
-
-
-
//--------------Code Starts Here------------------
-
-
// Tri-State Buffer control
-
// output : When we = 0, oe = 1, cs = 1
-
assign data = (cs && oe && !we) ? data_out : 8'bz;
-
-
// Memory Write Block
-
// Write Operation : When we = 1, cs = 1
-
always @ (posedge clk)
-
begin : MEM_WRITE
-
if ( cs && we ) begin
-
mem[address] <= data;
-
end
-
end
-
-
// Memory Read Block
-
// Read Operation : When we = 0, oe = 1, cs = 1
-
always @ (posedge clk)
-
begin : MEM_READ
-
if (cs && !we && oe) begin
-
data_out <= mem[address];
-
/* oe_r <= 1;
-
end else begin
-
oe_r <= 0; */
-
end
-
end
-
-
endmodule // End of Module ram_sp_sr_sw
且对这个RAM设计进行功能仿真,验证功能是否正确:
测试代码:
-
`timescale 1ns / 1ps
-
//
-
// Create Date: 2019/05/21 16:00:12
-
// Design Name:
-
// Module Name: ram_sp_sr_sw_tb
-
//
-
-
-
module ram_sp_sr_sw_tb(
-
-
);
-
-
reg clk; // Clock Input
-
reg [7 : 0] address; // address Input
-
wire [7 : 0] data; // Data bi-directional
-
reg cs; // Chip Select
-
reg we; // Write Enable/Read Enable
-
reg oe; // Output Enable
-
-
reg [7 : 0] data_in;
-
assign data = (cs && we && !oe) ? data_in : 8'dz;
-
-
integer i;
-
-
initial begin
-
clk = 0;
-
forever
-
#2 clk = ~clk;
-
end
-
-
-
initial begin
-
-
cs = 1'b0;
-
we = 1'b0;
-
oe = 1'b0;
-
address = 8'd0;
-
data_in = 8'h00;
-
#20
-
@(negedge clk) begin//read
-
cs = 1'b1;
-
oe = 1'b1;
-
end
-
-
for (i = 0; i < 256; i = i + 1) begin
-
@(negedge clk)
-
address = i;
-
end
-
-
@(negedge clk) begin//write
-
we = 1'b1;
-
oe = 1'b0;
-
end
-
-
for (i = 0; i < 256; i = i + 1) begin
-
@(negedge clk) begin
-
address = i;
-
//此处如何给输入数据?
-
data_in = data_in + 1;
-
end
-
end
-
-
@(negedge clk) begin//read
-
we = 1'b0;
-
oe = 1'b1;
-
end
-
-
for (i = 0; i < 256; i = i + 1) begin
-
@(negedge clk)
-
address = i;
-
end
-
-
@(negedge clk)
-
cs = 1'b0;
-
//#100 $finish;
-
#100 $stop;
-
-
-
-
-
-
-
end
-
-
-
ram_sp_sr_sw u_ram(
-
.clk(clk) , // Clock Input
-
.address(address) , // address Input
-
.data(data) , // Data bi-directional
-
.cs(cs) , // Chip Select
-
.we(we) , // Write Enable/Read Enable
-
.oe(oe) // Output Enable
-
);
-
-
-
endmodule
由于内存初始化为0,所以刚开始读出来的数据肯定为0:
读完256个初识数据后,开始对此RAM写入数据(地址0出写1,地址1出写2,类推,最后在地址255处写0):
写完数据之后,我们又开始读数据,看看写入的数据是否正确:
可见,在地址0处读出的数据为1,地址1处读出数据为2,类推,最后再地址255处读出数据为0.
读出数据与写入数据一致,我们的设计没有问题。
最后给出RTL原理图:
参考链接:
Single Port RAM Synchronous Read/Write
【Verilog HDL 训练】第 13 天(存储器、SRAM)
文章来源: reborn.blog.csdn.net,作者:李锐博恩,版权归原作者所有,如需转载,请联系作者。
原文链接:reborn.blog.csdn.net/article/details/90639422
- 点赞
- 收藏
- 关注作者
评论(0)