同步 FIFO 实列及仿真结果

来源:互联网 发布:mfc编程图书 编辑:程序博客网 时间:2024/05/29 17:21

        先入先出队列(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;  endendalways@(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;  endendalways@(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;endendendmodule

 

以下是仿真结果