Verilog笔记.6.FIFO
FIFO,First In First Out ,是一种先进先出的数据缓存器。
没有外部读写地址线,只能顺序写入数据,顺序的读出数据, 其数据地址由内部读写指针自动加1完成。
不能像普通存储器那样可以由地址线决定读取或写入某个指定的地址。
FIFO一般用于不同时钟域之间的数据传输,根据工作的时钟域,分为同步FIFO和异步FIFO。
同步FIFO是指读时钟和写时钟为同一个时钟。在时钟沿来临时同时发生读写操作。
异步FIFO是指读写时钟不一致,读写时钟是互相独立的。
同步FIFO
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2018/05/02 21:34:02
// Design Name:
// Module Name: FIFO_16bits_16
// Project Name:
// Target Devices:
// Tool Versions:
// Description: synchronous fifo 16bits * 16
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
`define ADDR_WIDTH //ADDR WIDTH = 4,
`define FIFO_DEPTH //FIFO DEPTH
`define FIFO_WIDTH //FIFO WIDTH 16 BITS module myFIFO(
input wire clk,
input wire rst_n,
input wire wr_en,
input wire rd_en, //wire write/read enable
input wire [`FIFO_WIDTH:] buf_in, // data input to be pushed to buffer
output reg [`FIFO_WIDTH:] buf_out, // port to output the data using pop.
output wire buf_empty,
output wire buf_full, // fifo buffer empty/full indication
output reg [`ADDR_WIDTH:] fifo_cnt // number of data pushed in to buffer,16-> FULL;0-> EMPTY
); reg [`ADDR_WIDTH-:] rd_ptr,wr_ptr; //ADDR PTR .INDEX ,CYCLE 0->15->0->15
reg [`FIFO_WIDTH:] buf_mem[:`FIFO_DEPTH-];
reg rst_nr;
//juge full/empty
assign buf_empty = (fifo_cnt == )?:;
assign buf_full = (fifo_cnt == `FIFO_DEPTH)?:;
//Asynchronous reset,synch release
// always @(posedge clk)begin
// rst_nr <= rst_n;
// end
//FIFO_CNT
always @(posedge clk or negedge rst_n)begin
if(!rst_n)
fifo_cnt <= ;
else if((!buf_full&&wr_en)&&(!buf_empty&&rd_en)) //WRTITE & READ ,HOLD
fifo_cnt <= fifo_cnt;
else if(!buf_full && wr_en) //WRITE-> +1
fifo_cnt <= fifo_cnt + ;
else if(!buf_empty && rd_en) //READ -> -1
fifo_cnt <= fifo_cnt-;
else
fifo_cnt <= fifo_cnt;
end
//READ
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
buf_out <= ;
if(rd_en && !buf_empty)
buf_out <= buf_mem[rd_ptr];
end
//WRITE
always @(posedge clk) begin
if(wr_en && !buf_full)
buf_mem[wr_ptr] <= buf_in;
end
//wr_ptr & rd_ptr ,ADDR PTR
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
wr_ptr <= ;
rd_ptr <= ;
end
else begin
if(!buf_full && wr_en)
wr_ptr <= wr_ptr + ;
if(!buf_empty && rd_en)
rd_ptr <= rd_ptr + ;
end
end endmodule
testbench
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2018/05/02 21:42:06
// Design Name:
// Module Name: tb
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
`define ADDR_WIDTH //ADDR WIDTH = 4,
`define BUF_DEPTH //FIFO DEPTH
`define FIFO_WIDTH //FIFO WIDTH 16 BITS module tb;
reg clk,rst_n;
reg wr_en,rd_en;
reg [:] buf_in; // data input to be pushed to buffer
wire [:] buf_out; // port to output the data using pop.
wire buf_empty,buf_full; // buffer empty and full indication
wire [`ADDR_WIDTH-:] fifo_cnt; // number of data pushed in to buffer myFIFO dut(
.clk(clk),
.rst_n(rst_n),
.buf_in(buf_in),
.buf_out(buf_out),
.wr_en(wr_en),
.rd_en(rd_en),
.buf_empty(buf_empty),
.buf_full(buf_full),
.fifo_cnt(fifo_cnt)
); always # clk = ~clk; reg [:] tempdata = ;
initial begin
clk = ;
rst_n = ;
wr_en = ;
rd_en = ;
buf_in = ;
#;
rst_n = ; push();
fork
push();
pop(tempdata);
join //push and pop together
push();
push();
push();
push();
push();
push();
push();
push();
push();
push();
push();
push();
push();
push();
push();
push();
push();
push();
push(); pop(tempdata);
push(tempdata); pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
push();
pop(tempdata);
push(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
push();
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata);
pop(tempdata); #;
rst_n = ;
#;
rst_n = ;
push();
fork
push();
pop(tempdata);
join //push and pop together
push();
push();
push();
pop(tempdata);
pop(tempdata);
pop(tempdata);
end task push (input [:] data);
if(buf_full)
$display("---Cannot push %d: Buffer Full---",data);
else begin
$display("Push:%d",data);
buf_in = data;
wr_en = ;
@(posedge clk);
# wr_en = ;
end
endtask task pop(output[:] data);
if(buf_empty)
$display("---Cannot Pop: Buffer Empty---");
else begin
rd_en = ;
@(posedge clk);
# rd_en = ;
data = buf_out;
$display("------Poped:%d",data);
end
endtask endmodule
在综合是出现错误
[Synth 8-91] ambiguous clock in event control [:61]
是因为该段没有else语句,于是将该段改为
//READ
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
buf_out <= ;
else begin
if(rd_en && !buf_empty)
buf_out <= buf_mem[rd_ptr];
end
end
通过。
//********************************************************************************
异步FIFO
参考了很多文章。
直接上代码
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2018/05/03 13:56:26
// Design Name:
// Module Name: AsyncFIFO
// Project Name:
// Target Devices:
// Tool Versions:
// Description: asynchronous fifo
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
////////////////////////////////////////////////////////////////////////////////// module AsyncFIFO
#(
parameter fifo_width = ,
parameter fifo_depth = , // = ((1<<addr_width) -1)
parameter addr_width =
)
(
input wire wclk,
input wire rclk,
input wire wrst_n,
input wire rrst_n,
input wire winc,
input wire rinc,
input wire [fifo_width-:] wdata,
output wire [fifo_width-:] rdata,
output reg wfull,
output reg rempty
//output reg [addr_width:0] fifo_cnt
); reg [addr_width:] wptr,rptr;
reg [addr_width:] rbin,wbin;
reg [addr_width:] wq1_rptr,rq1_wptr,wq2_rptr,rq2_wptr;
reg [fifo_width-:] fifo_mem [:fifo_depth-];
wire [addr_width-:] waddr,raddr;
wire [addr_width:] rgraynext,rbinnext,wgraynext,wbinnext;
wire rempty_val,wfull_val;
//snyc wptr
always @(posedge wclk or negedge wrst_n)
if(!wrst_n) begin
{wq2_rptr,wq1_rptr} <= ;
end
else begin
{wq2_rptr,wq1_rptr} <= {wq1_rptr,rptr};
end
//snyc rptr
always @(posedge wclk or negedge wrst_n)
if (!wrst_n) begin
{rq2_wptr,rq1_wptr} <= ;
end
else begin
{rq2_wptr,rq1_wptr} <= {rq1_wptr,wptr};
end
//dualRAM
assign rdata = fifo_mem[raddr];
always@(posedge wclk)
if (winc && !wfull) fifo_mem[waddr] <= wdata;
//------------------------rempty & raddr----------------------------
always @(posedge rclk or negedge rrst_n) // GRAYSTYLE-2 pointer
if (!rrst_n) {rbin, rptr} <= ;
else {rbin, rptr} <= {rbinnext, rgraynext};
// Memory read-address pointer (okay to use binary to address memory)
assign raddr = rbin[addr_width-:];
assign rbinnext = rbin + (rinc & ~rempty);
assign rgraynext = (rbinnext>>) ^ rbinnext;//binary to gray
// FIFO empty when the next rptr == synchronized wptr or on reset
assign rempty_val = (rgraynext == rq2_wptr)?:;
always @(posedge rclk or negedge rrst_n)
if (!rrst_n) rempty <= 'b1;
else rempty <= rempty_val;
//-----------------------wfull & waddr------------------------------
always @(posedge wclk or negedge wrst_n) // GRAYSTYLE2 pointer
if (!wrst_n) {wbin, wptr} <= ;
else {wbin, wptr} <= {wbinnext, wgraynext};
// Memory write-address pointer (okay to use binary to address memory)
assign waddr = wbin[addr_width-:];
assign wbinnext = wbin + (winc & ~wfull);
assign wgraynext = (wbinnext>>) ^ wbinnext;
//------------------------------------------------------------------
// Simplified version of the three necessary full-tests:
// assign wfull_val=((wgnext[ADDRSIZE] !=wq2_rptr[ADDRSIZE] ) &&
// (wgnext[ADDRSIZE-1] !=wq2_rptr[ADDRSIZE-1]) &&
// (wgnext[ADDRSIZE-2:0]==wq2_rptr[ADDRSIZE-2:0]));
//------------------------------------------------------------------
assign wfull_val = (wgraynext=={~wq2_rptr[addr_width:addr_width-], wq2_rptr[addr_width-:]});
always @(posedge wclk or negedge wrst_n)
if (!wrst_n) wfull <= 'b0;
else wfull <= wfull_val; endmodule
测试程序
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2018/05/03 16:53:39
// Design Name:
// Module Name: tb
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
////////////////////////////////////////////////////////////////////////////////// module tb;
// parameter fifo_width = 16;
// parameter fifo_depth = 16;
// parameter addr_width = 4; reg wclk,rclk,wrst_n,rrst_n;
reg winc,rinc;
reg [:] wdata;
wire [:] rdata;
wire rempty,wfull; AsyncFIFO i1(
.wclk(wclk),
.rclk(rclk),
.wrst_n(wrst_n),
.rrst_n(rrst_n),
.winc(winc),
.rinc(rinc),
.wdata(wdata),
.rdata(rdata),
.wfull(wfull),
.rempty(rempty)
);
always # wclk = ~wclk;
always # rclk = ~rclk; reg [:] tempdata;
reg [:] data1;
reg [:] data2;
initial begin
wclk = ;
rclk = ;
wrst_n = ;
rrst_n = ;
winc = ;
rinc = ;
wdata = ;
data1 = ; #
fork
wrst_n = ;
rrst_n = ;
join
#
push();
push();
push();
push();
#
//pop(tempdata);
//push and pop together
while()begin
fork
wr;
rd;
join
end
push();
push();
push();
push();
push(); end task wr;begin
while(!wfull)begin
push(data1);
data1 =data1+;
end
end
endtask task rd;begin
while(!rempty)
# pop(tempdata); end
endtask task push (input [:] data);
if(wfull)
$display("---Cannot push %d: Buffer Full---",data);
else begin
$display("Push:%d",data);
wdata = data;
winc = ;
@(posedge wclk);
# winc = ;
end
endtask task pop(output[:] data);
if(rempty)
$display("---Cannot Pop: Buffer Empty---");
else begin
rinc = ;
@(posedge rclk);
# rinc = ;
data = rdata;
$display("------Poped:%d",data);
end
endtask endmodule
测试算是通过,但还是有点问题。相同再弄了。
Verilog笔记.6.FIFO的更多相关文章
- Verilog笔记——Verilog数字系统设计(第二版)夏宇闻
本片记录Verilog学习笔记,主要是和以往用的C语言的不同之处,以例子.代码的形式记录.学习以<Verilog数字系统设计>(第二版)为参考资料,援助作者夏宇闻. 1. C语言和Veri ...
- 基于Verilog的带FIFO写入缓冲的串口发送接口封装
一.模块框图及基本思路 tx_module:串口发送的核心模块,详细介绍请参照前面的“基于Verilog的串口发送实验” fifo2tx_module:当fifo不为空时,读取fifo中的数据并使能发 ...
- 基于Verilog的带FIFO输出缓冲的串口接收接口封装
一.模块框图及基本思路 rx_module:串口接收的核心模块,详细介绍请见“基于Verilog的串口接收实验” rx2fifo_module:rx_module与rx_fifo之间的控制模块,其功能 ...
- 基于Verilog的简单FIFO读写实验
一.模块框图及基本思路 fifo_ip:ISE生成的IP fifo_control:在fifo未满情况下不断写入递增的四位数,每隔1s读出一个数据驱动Led显示 fifo_top:前两个模块的组合 二 ...
- Verilog设计异步FIFO
转自http://ninghechuan.com 异步FIFO有两个异步时钟,一个端口写入数据,一个端口读出数据.通常被用于数据的跨时钟域的传输. 同步FIFO的设计.一个时钟控制一个计数器,计数器增 ...
- Verilog笔记.三段式状态机
之前都是用的一段式状态机,逻辑与输出混在一起,复杂点的就比较吃力了. 所以就开始着手三段式状态机. 组合逻辑与时序逻辑分开,这样就能简单许多了. 但是两者在思考方式上也有着很大的区别. 三段式,分作: ...
- Verilog笔记.1.基本语法
0.前 抽象模型分级: • 系统级(system):用高级语言结构实现设计模块的外部性能的模型.• 算法级(algorithm):用高级语言结构实现设计算法的模型.• RTL级(Register Tr ...
- Verilog笔记——YUV2RGB的模块测试
1 YUV2RGB的模块如下: module yuv2rgb( clk, //时钟输入 rstn, //复位输入,低电平复位 y_in, //变换前Y分量输出 cb_in, //变换前Cb分量输出 c ...
- Verilog笔记.5.同步、异步
在数字电路中经常有同步synchronism.异步asynchronism的概念.异步指输入信号和时钟无关:同步指输入信号和时钟信号有关,实际上就是输入信号和时钟信号进行了与运算或者与非运算.实际开发 ...
随机推荐
- Windows连接Linux服务器中MySQL数据库-权限配置
问题描述 在Windows系统中安装了监控MySQL数据库服务器性能的工具Spotlight on MySQL,利用Spotlight连接Linux服务器中的MySQL,进行相关配置如下: 点击& ...
- 详解Python闭包,装饰器及类装饰器
在项目开发中,总会遇到在原代码的基础上添加额外的功能模块,原有的代码也许是很久以前所写,为了添加新功能的代码块,您一般还得重新熟悉源代码,稍微搞清楚一点它的逻辑,这无疑是一件特别头疼的事情.今天我们介 ...
- bootstrap 中的静态模式的控制按钮上的一个坑
在使用modal时发现,代码:<button class="btn btn-danger" data-toggle="modal" data-target ...
- 转---秒杀多线程第十二篇 多线程同步内功心法——PV操作上 (续)
PV操作的核心就是 PV操作可以同时起到同步与互斥的作用. 1.同步就是通过P操作获取信号量,V操作释放信号量来进行. 2.互斥其实就是,同时操作P操作,结束后进行V操作即可做到. Java上实现PV ...
- lxm --- ans lb config
lxm --- ans lb config #ANS2.2 Build 160.006 # Last modified by `save config`, Fri Oct 12 17:15:42 20 ...
- [POI2012]OKR-A Horrible Poem hash
题面:洛谷 题解: 首先我们需要知道一个性质,串s的最小循环节 = len - next[len].其中next[len]表示串s的一个最长长度使得s[1] ~ s[next[len]] == s[l ...
- Spring Boot系列教程六:日志输出配置log4j2
一.前言 spring boot支持的日志框架有,logback,Log4j2,Log4j和Java Util Logging,默认使用的是logback日志框架,笔者一直在使用log4j2,并且 ...
- 常州day1p4
给定一棵 n 个点的树,树上每个节点代表一个小写字母,询问一个字符串 S 是否在树上出现过? 字符串 S 出现过即表示存在两个点 u,v,u 到 v 的最短路径上依次连接所有点上的字母恰好是 S N ...
- 【BZOJ1566】【NOI2009】管道取珠(动态规划)
[BZOJ1566][NOI2009]管道取珠(动态规划) 题面 BZOJ 题解 蛤?只有两档部分分.一脸不爽.jpg 第一档?爆搜,这么显然,爆搜+状压最后统计一下就好了 #include<i ...
- 解题:POI 2009 TAB
题面 这也算是个套路题(算吗)?发现换来换去每行每列数的组成是不变的,那么就把每行每列拎出来哈希一下,复杂度$O(Tn^2log$ $n)$有点卡时=.=. 然而正解似乎不需要哈希,就像这样↓ ;i& ...