【FPGA】FIFO的Verilog设计之同步FIFO的设计

举报
李锐博恩 发表于 2021/07/15 04:45:21 2021/07/15
【摘要】 这个同步FIFO的设计方法是调用异步读写双端口RAM来实现的。 关于异步读写双端口RAM的设计,前面博文已经讲到过了:【FPGA】双端口RAM的设计(异步读写) 此时使用双端口RAM来设计FIFO,可以使用一套端口进行写操作,一套端口进行读操作的方式来实现,例如例化方式大概是这样的: ram_dp_ar_aw #(DATA_WIDTH,ADDR_WIDTH) DP...

这个同步FIFO的设计方法是调用异步读写双端口RAM来实现的。

关于异步读写双端口RAM的设计,前面博文已经讲到过了:【FPGA】双端口RAM的设计(异步读写)

此时使用双端口RAM来设计FIFO,可以使用一套端口进行写操作,一套端口进行读操作的方式来实现,例如例化方式大概是这样的:

ram_dp_ar_aw #(DATA_WIDTH,ADDR_WIDTH) DP_RAM (
.address_0 (wr_pointer) , // address_0 input 
.data_0    (data_in)    , // data_0 bi-directional
.cs_0      (wr_cs)      , // chip select
.we_0      (wr_en)      , // write enable
.oe_0      (1'b0)       , // output enable
.address_1 (rd_pointer) , // address_q input
.data_1    (data_ram)   , // data_1 bi-directional
.cs_1      (rd_cs)      , // chip select
.we_1      (1'b0)       , // Read enable
.oe_1      (rd_en)        // output enable
);  

这样就可以同时读写。


其原理大概如此:

 FIFO uses a dual port memory and there will be two pointers to point read and write addresses. Here is a generalized block diagram of FIFO. 

 

Generally fifos are implementedusing rotating pointers. We can call write and read pointers of a FIFO as headand tail of data area. Initially read and write pointers of the FIFO will pointto the same location

Here is an example to explain howFIFO uses the memory. This is a fifo of length 8, WP and RP are the locationswhere write pointer and read pointer points. Shaded area in the diagram isfilled with data.

 

When ever FIFO counter becomes zeroor BUF_LENGTH, empty or full flags will be set.

使用fifo_counter记录FIFO RAM中的数据个数,等于0时,给出empty信号,等于BUF_LENGTH时,给出full信号

fifo_counter is incremented ifwrite takes place and buffer is not full and will be decremented id read takesplace and buffer is not empty. If both read and write takes place, counter willremain the same.

fifo_counter写而未满时增加1,读而未空时减1。同时发生读写操作时,fifo_counter不变。

rd_ptr and wr_ptr are read andwrite pointers. Since we selected the bits in these registers same as addresswidth of buffer, when buffer overflows, values will overflow and become 0.

读写指针宽度与地址宽度相当,地址增加而溢出后,自动变成0。

给出同步FIFO的Verilog描述:

 


  
  1. `timescale 1ns / 1ps
  2. //-----------------------------------------------------
  3. // Design Name : syn_fifo
  4. // File Name : syn_fifo.v
  5. // Function : Synchronous (single clock) FIFO
  6. //-----------------------------------------------------
  7. module syn_fifo (
  8. clk , // Clock input
  9. rst , // Active high reset
  10. wr_cs , // Write chip select
  11. rd_cs , // Read chipe select
  12. data_in , // Data input
  13. rd_en , // Read enable
  14. wr_en , // Write Enable
  15. data_out , // Data Output
  16. empty , // FIFO empty
  17. full // FIFO full
  18. );
  19. // FIFO constants
  20. parameter DATA_WIDTH = 8;
  21. parameter ADDR_WIDTH = 8;
  22. parameter RAM_DEPTH = (1 << ADDR_WIDTH);
  23. // Port Declarations
  24. input clk ;
  25. input rst ;
  26. input wr_cs ;
  27. input rd_cs ;
  28. input rd_en ;
  29. input wr_en ;
  30. input [DATA_WIDTH-1:0] data_in ;
  31. output full ;
  32. output empty ;
  33. output [DATA_WIDTH-1:0] data_out ;
  34. //-----------Internal variables-------------------
  35. reg [ADDR_WIDTH-1:0] wr_pointer;
  36. reg [ADDR_WIDTH-1:0] rd_pointer;
  37. reg [ADDR_WIDTH :0] status_cnt;
  38. reg [DATA_WIDTH-1:0] data_out ;
  39. wire [DATA_WIDTH-1:0] data_ram ;
  40. //-----------Variable assignments---------------
  41. assign full = (status_cnt == (RAM_DEPTH));
  42. assign empty = (status_cnt == 0);
  43. //-----------Code Start---------------------------
  44. always @ (posedge clk or posedge rst)
  45. begin : WRITE_POINTER
  46. if (rst) begin
  47. wr_pointer <= 0;
  48. end
  49. else if (wr_cs && wr_en ) begin
  50. wr_pointer <= wr_pointer + 1;
  51. end
  52. end
  53. always @ (posedge clk or posedge rst)
  54. begin : READ_POINTER
  55. if (rst) begin
  56. rd_pointer <= 0;
  57. end else if (rd_cs && rd_en ) begin
  58. rd_pointer <= rd_pointer + 1;
  59. end
  60. end
  61. always @ (posedge clk or posedge rst)
  62. begin : READ_DATA
  63. if (rst) begin
  64. data_out <= 0;
  65. end
  66. else if (rd_cs && rd_en ) begin
  67. data_out <= data_ram;
  68. end
  69. end
  70. always @ (posedge clk or posedge rst)
  71. begin : STATUS_COUNTER
  72. if (rst) begin
  73. status_cnt <= 0;
  74. // Read but no write.
  75. end
  76. else if ((rd_cs && rd_en) && !(wr_cs && wr_en)
  77. && (status_cnt != 0)) begin
  78. status_cnt <= status_cnt - 1;
  79. // Write but no read.
  80. end
  81. else if ((wr_cs && wr_en) && !(rd_cs && rd_en)
  82. && (status_cnt != RAM_DEPTH)) begin
  83. status_cnt <= status_cnt + 1;
  84. end
  85. end
  86. ram_dp_ar_aw #(DATA_WIDTH,ADDR_WIDTH) DP_RAM (
  87. .address_0 (wr_pointer) , // address_0 input
  88. .data_0 (data_in) , // data_0 bi-directional
  89. .cs_0 (wr_cs) , // chip select
  90. .we_0 (wr_en) , // write enable
  91. .oe_0 (1'b0) , // output enable
  92. .address_1 (rd_pointer) , // address_q input
  93. .data_1 (data_ram) , // data_1 bi-directional
  94. .cs_1 (rd_cs) , // chip select
  95. .we_1 (1'b0) , // Read enable
  96. .oe_1 (rd_en) // output enable
  97. );
  98. endmodule

给出异步读写双端口RAM的Verilog描述代码:


  
  1. `timescale 1ns / 1ps
  2. //
  3. // Create Date: 2019/05/29 21:11:08
  4. // Design Name:
  5. // Module Name: ram_dp_ar_aw
  6. //
  7. module ram_dp_ar_aw #(
  8. parameter DATA_WIDTH = 8,
  9. parameter ADDR_WIDTH = 8,
  10. parameter RAM_DEPTH = 1 << ADDR_WIDTH
  11. )(
  12. input [ADDR_WIDTH - 1 : 0] address_0,
  13. inout [DATA_WIDTH - 1 : 0] data_0,
  14. input cs_0,
  15. input we_0,
  16. input oe_0,
  17. input [ADDR_WIDTH - 1 : 0] address_1,
  18. inout [DATA_WIDTH - 1 : 0] data_1,
  19. input cs_1,
  20. input we_1,
  21. input oe_1
  22. );
  23. reg [DATA_WIDTH - 1 : 0] mem [RAM_DEPTH - 1 : 0];
  24. integer i;
  25. // initialization
  26. initial begin
  27. for(i = 0; i < RAM_DEPTH; i = i + 1) begin
  28. mem[i] = 0;
  29. end
  30. end
  31. //write
  32. always@(*) begin
  33. if(cs_0 && we_0) begin
  34. mem[address_0] = data_0;
  35. end
  36. else if(cs_1 && we_0) begin
  37. mem[address_1] = data_1;
  38. end
  39. else;
  40. end
  41. //read
  42. reg [DATA_WIDTH - 1 : 0] data_out0;
  43. assign data_0 = (cs_0 && !we_0 && oe_0) ? data_out0 : 'hz;
  44. always@(*) begin
  45. if(cs_0 && !we_0 && oe_0) begin
  46. data_out0 = mem[address_0];
  47. end
  48. else begin
  49. data_out0 = 0;
  50. end
  51. end
  52. reg [DATA_WIDTH - 1 : 0] data_out1;
  53. assign data_1 = (cs_1 && !we_1 && oe_1) ? data_out1 : 'hz;
  54. always@(*) begin
  55. if(cs_1 && !we_1 && oe_1) begin
  56. data_out1 = mem[address_1];
  57. end
  58. else begin
  59. data_out1 = 0;
  60. end
  61. end
  62. endmodule

简单测试下:

测试代码:


  
  1. `timescale 1ns / 1ps
  2. //
  3. // Create Date: 2019/05/29 22:01:38
  4. // Design Name:
  5. // Module Name: syn_fifo_tb
  6. //
  7. module syn_fifo_tb(
  8. );
  9. reg clk ;
  10. reg rst ;
  11. reg wr_cs ;
  12. reg rd_cs ;
  13. reg rd_en ;
  14. reg wr_en ;
  15. reg [7:0] data_in ;
  16. wire full ;
  17. wire empty ;
  18. wire [7:0] data_out ;
  19. initial begin
  20. clk = 0;
  21. forever
  22. #2 clk = ~clk;
  23. end
  24. integer i=0;
  25. initial begin
  26. rst = 1;
  27. wr_cs = 0;
  28. rd_cs = 0;
  29. wr_en = 0;
  30. rd_en = 0;
  31. data_in = 0;
  32. #8
  33. rst = 0;
  34. //write
  35. wr_cs = 1;
  36. wr_en = 1;
  37. for(i = 0; i < 256; i = i + 1) begin
  38. @(negedge clk) begin
  39. data_in = data_in + 1;
  40. end
  41. end
  42. //read
  43. //#8
  44. wr_cs = 0;
  45. wr_en = 0;
  46. rd_cs = 1;
  47. rd_en = 1;
  48. end
  49. syn_fifo u_syn_fifo(
  50. .clk(clk),
  51. .rst(rst),
  52. .wr_cs(wr_cs),
  53. .rd_cs(rd_cs),
  54. .rd_en(rd_en),
  55. .wr_en(wr_en),
  56. .data_in(data_in),
  57. .full(full),
  58. .empty(empty),
  59. .data_out(data_out)
  60. );
  61. endmodule

当然测试代码并没有测试同时读写的情况,但至少验证了,先进先出的事实,且读写数据都符合预期。

下一篇:异步FIFO的设计

 

 

文章来源: reborn.blog.csdn.net,作者:李锐博恩,版权归原作者所有,如需转载,请联系作者。

原文链接:reborn.blog.csdn.net/article/details/90708011

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。