日常工作实用同步FIFO(已Compile)

日常工作中实用的同步FIFO


在这里插入图片描述
代码和学生时代所学的有很大不同,重点是FIFO深度&宽度的可配置,分块思想,#1思想,以及同步FIFO的相关性质的处理。
(有任何问题可关注微信公众号:数字IC小宝贝儿,留言call我)

/***************************************************************
// File   : sync_fifo.v
// Auther : HoneyIC
// Time   : 2020.8.30
// Version: #1
// All rights reserved.
A synchronous fifo used in daily work, which is different from what we learned in the school.
The idea of this classification is very important, that is, separate coding of different modules.
What's more, please note that if-else in the sequential circuit is not written in full (eg. line 85). 
The advantage is that a gated control circuit can be generated, which can reduce the power consumption of the circuit.
"#1" can solve many timing problems in the synthesized circuit.
***************************************************************/

`timescale 1ns / 100ps

//`include "para.v"
//`define DLY 1

  
module sync_fifo #(
  parameter DEPTH = 'd24,
  parameter WIDTH = 'd8
)(
  // input
  clk,
  rst_n,
  wr,
  rd,
  data_in,  
  
  // output
  full,
  empty,
  overun,
  underun,
  data_out
);

  parameter DLY = 1;
  parameter RPT_WIDTH = DEPTH <= 'd2    ? 'd1 :
                        DEPTH <= 'd4    ? 'd2 :
                        DEPTH <= 'd8    ? 'd3 :
                        DEPTH <= 'd16   ? 'd4 :
                        DEPTH <= 'd32   ? 'd5 :
                        DEPTH <= 'd64   ? 'd6 :
                        DEPTH <= 'd128  ? 'd7 :
                        DEPTH <= 'd256  ? 'd8 :
                        DEPTH <= 'd512  ? 'd9 :
                        DEPTH <= 'd1024 ? 'd10: 'd11;
  // input
  input               clk;
  input               rst_n;
  input               wr;
  input               rd;
  input  [WIDTH-1:0]  data_in;
  
  // output
  output [WIDTH-1:0]  data_out;
  output              full;
  output              empty;
  output              overun;
  output              underun;
  
  // registers
  reg [    WIDTH-1:0] dataout;
  integer             i;
  reg [    DEPTH-1:0] fifo [WIDTH-1:0];
  reg [RPT_WIDTH-1:0] wptr;
  reg [RPT_WIDTH-1:0] rptr;
  reg [  RPT_WIDTH:0] gap_cnt;                           

  
  // 1. full, empty, overun, underun
  assign full    = (gap_cnt == DEPTH);
  assign empty   = (gap_cnt == 0);
  assign overun  = wr && (!rd) && full;
  assign underun = (!wr) && rd && empty;
  
  
  // 2. Updata gap_cnt
  // The advantage of this writing type is that it is easy to modify, especially in large projects. 
  assign gap_cnt_full = (gap_cnt == DEPTH);
  assign gap_cnt_add  = (wr && (!rd) && (!full));
  assign gap_cnt_sub  = ((!wr) && rd && (!empty));
  always @(posedge clk or negedge rst_n)
    if (!rst_n)
      gap_cnt <= #DLY 'd0;
    else if (gap_cnt_full)
      gap_cnt <= #DLY 'd0;
    else if (gap_cnt_add)
      gap_cnt <= #DLY gap_cnt + 1'b1;
    else if (gap_cnt_sub)
      gap_cnt <= #DLY gap_cnt - 1'b1;
    // The advantage of not writing 'else' here is that the Synthesis tool will generate gating clock.
    
  
  // 3. Updata wptr & rptr
  assign wptr_full = (wptr == (DEPTH - 1));
  always @(posedge clk or negedge rst_n)
    if (!rst_n)
      wptr <= 'd0;
    else if (wr)
      if (wptr_full)
        wptr <= #DLY 'd0;
      else
        wptr <= #DLY wptr + 1'b1;
  
  assign rptr_full = (rptr == (DEPTH - 1)); 
  always @(posedge clk or negedge rst_n)
    if (!rst_n)
      rptr <= #DLY 'd0;
    else if (rd)
      if (rptr_full)
        rptr <= #DLY 'd0;
      else
        rptr <= #DLY rptr + 1'b1;
  
  
  // 4. Write/Read data to/from fifo      
  always @(posedge clk or negedge rst_n)
    if (!rst_n)
      for (i=0; i<DEPTH; i=i+1)
        fifo[i] <= #DLY 'd0;
    else if (wr)
      fifo[wptr] <= #DLY data_in;
  
  assign data_out = fifo[rptr];


endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数字IC小宝贝儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值