pkt_fifo.v 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * pkt_fifo.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut <tnt@246tNt.com>
  7. * All rights reserved.
  8. *
  9. * BSD 3-clause, see LICENSE.bsd
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of the <organization> nor the
  19. * names of its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. `default_nettype none
  34. module pkt_fifo #(
  35. parameter integer AWIDTH = 9
  36. )(
  37. input wire [7:0] wr_data,
  38. input wire wr_last,
  39. input wire wr_ena,
  40. output wire full,
  41. output wire [7:0] rd_data,
  42. output wire rd_last,
  43. input wire rd_ena,
  44. output wire empty,
  45. input wire clk,
  46. input wire rst
  47. );
  48. // Signals
  49. reg [AWIDTH-1:0] ram_waddr;
  50. wire [7:0] ram_wdata;
  51. wire ram_wen;
  52. reg [AWIDTH-1:0] ram_raddr;
  53. wire [7:0] ram_rdata;
  54. wire ram_ren;
  55. (* keep="true" *) wire [1:0] ln_mod;
  56. wire [AWIDTH:0] ln_mod_ext;
  57. reg [AWIDTH:0] len_nxt; // Length with next packet
  58. reg [AWIDTH:0] len_cur; // Length current - 1
  59. wire rd_ce;
  60. wire valid_nxt;
  61. reg valid_out;
  62. // Storage element
  63. ram_sdp #(
  64. .AWIDTH(AWIDTH),
  65. .DWIDTH(8)
  66. ) ram_I (
  67. .wr_addr(ram_waddr),
  68. .wr_data(ram_wdata),
  69. .wr_ena(ram_wen),
  70. .rd_addr(ram_raddr),
  71. .rd_data(ram_rdata),
  72. .rd_ena(ram_ren),
  73. .clk(clk)
  74. );
  75. // Write pointer
  76. always @(posedge clk or posedge rst)
  77. if (rst)
  78. ram_waddr <= 0;
  79. else if (wr_ena)
  80. ram_waddr <= ram_waddr + 1;
  81. // Read pointer
  82. always @(posedge clk or posedge rst)
  83. if (rst)
  84. ram_raddr <= 0;
  85. else if (rd_ce)
  86. ram_raddr <= ram_raddr + 1;
  87. // Next Length counter
  88. assign ln_mod = { rd_ce & ~wr_ena, rd_ce ^ wr_ena };
  89. assign ln_mod_ext = { {(AWIDTH){ln_mod[1]}}, ln_mod[0] };
  90. always @(posedge clk or posedge rst)
  91. if (rst)
  92. len_nxt <= 0;
  93. else
  94. len_nxt <= len_nxt + ln_mod_ext;
  95. // Length counter (readable length minus 1)
  96. always @(posedge clk)
  97. if (rst)
  98. len_cur <= { (AWIDTH+1){1'b1} };
  99. else
  100. len_cur <= ((wr_ena & wr_last) ? len_nxt : len_cur) - rd_ce;
  101. // Write logic
  102. assign ram_wdata = wr_data;
  103. assign ram_wen = wr_ena;
  104. assign full = len_nxt[AWIDTH];
  105. // Read logic
  106. assign rd_data = ram_rdata;
  107. assign rd_last = ~valid_nxt;
  108. assign empty = ~valid_out;
  109. assign valid_nxt = ~len_cur[AWIDTH];
  110. always @(posedge clk or posedge rst)
  111. if (rst)
  112. valid_out <= 1'b0;
  113. else if (rd_ena | ~valid_out)
  114. valid_out <= valid_nxt;
  115. assign rd_ce = valid_nxt & (rd_ena | ~valid_out);
  116. assign ram_ren = rd_ce;
  117. endmodule // pkt_fifo