先入先出队列(FIFO)是一种可以实现数据先入先出的储存器件,他就像一个单向管道,数据只能按照固定的方向从管道的一头进来,再按照相同的顺序从管道的另一头出去,最先进来的最先出去。FIFO在数字系统设计中有着非常重要的应用,它经常用来接觉时间不能同步情况下的数据操作问题。
FIFO和RAM有很多相同的部分,唯一不同的部分是FIFO没有操作地址,而只是用内部指针来保证数据在FIFO中的先入先出的正确性。
下面是verilog描述的同步fifo,主要包括如下单元 : 储存单元 写指针 读指针 读写控制信号和 慢 空标志。
module fifo(clk,rst,data_in,write,read,data_out,full,empty
);
input clk,rst,write,read; //同步FIFO ,同步复位
input [15:0]data_in;
output reg full,empty;
output reg [15:0]data_out;
parameter depth=2; //储存深度 ,即储存数据的个数
parameter max_count=2'b11;
reg [depth-1:0]tail; //读指针
reg [depth-1:0]head; //写指针
reg [depth-1:0]count;
reg [15:0] memory[0:max_count];
always@(posedge clk)
begin
if(rst)
begin
data_out<=4'b0000;
tail<=2'b00;
end
else if(read==1'b1&&empty==1'b0)
begin
data_out<=memory[tail];
tail<=tail+1;
end
end
always@(posedge clk)
begin
if(rst)
begin
head<=2'b00;
end
else if(write==1'b1&&full==1'b0)
begin
memory[head]<=data_in;
head<=head+1;
end
end
always@(posedge clk)
begin
if(rst)
begin
count<=0;
end
else
begin
case({read,write})
2'b00:count<=count+1;
2'b01:if(count!=2'b11) count<=count+1;
2'b10:if(count!=2'b00) count<=count-1;
2'b11:count<=count;
endcase
end
end
always@(posedge clk)
begin
if(count==2'b11)
begin
full<=1;
end
else
begin
full<=0;
end
end
always@(posedge clk)
begin
if(count==2'b00)
begin
empty<=1;
end
else
begin
empty<=0;
end
end
endmodule
以下是仿真结果
本文介绍了FIFO作为数据先入先出队列的工作原理及其在数字系统设计中的重要应用。通过verilog描述,展示了同步FIFO的设计,包括储存单元、写指针、读指针、读写控制信号和空标志,并提供了仿真结果。

916

被折叠的 条评论
为什么被折叠?



