fifo_sync_ram.v 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * fifo_sync_ram.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 fifo_sync_ram #(
  35. parameter integer DEPTH = 256,
  36. parameter integer WIDTH = 16
  37. )(
  38. input wire [WIDTH-1:0] wr_data,
  39. input wire wr_ena,
  40. output wire wr_full,
  41. output wire [WIDTH-1:0] rd_data,
  42. input wire rd_ena,
  43. output wire rd_empty,
  44. input wire clk,
  45. input wire rst
  46. );
  47. localparam AWIDTH = $clog2(DEPTH);
  48. // Signals
  49. // -------
  50. // RAM
  51. reg [AWIDTH-1:0] ram_wr_addr;
  52. wire [ WIDTH-1:0] ram_wr_data;
  53. wire ram_wr_ena;
  54. reg [AWIDTH-1:0] ram_rd_addr;
  55. wire [ WIDTH-1:0] ram_rd_data;
  56. wire ram_rd_ena;
  57. // Fill-level
  58. reg [AWIDTH:0] level;
  59. (* keep="true" *) wire lvl_dec;
  60. (* keep="true" *) wire lvl_mov;
  61. wire lvl_empty;
  62. // Full
  63. wire full_nxt;
  64. reg full;
  65. // Read logic
  66. reg rd_valid;
  67. // Fill level counter
  68. // ------------------
  69. // (counts the number of used words - 1)
  70. always @(posedge clk or posedge rst)
  71. if (rst)
  72. level <= {(AWIDTH+1){1'b1}};
  73. else
  74. level <= level + { {AWIDTH{lvl_dec}}, lvl_mov };
  75. assign lvl_dec = ram_rd_ena & ~ram_wr_ena;
  76. assign lvl_mov = ram_rd_ena ^ ram_wr_ena;
  77. assign lvl_empty = level[AWIDTH];
  78. // Full flag generation
  79. // --------------------
  80. assign full_nxt = level == { 1'b0, {(AWIDTH-2){1'b1}}, 2'b01 };
  81. always @(posedge clk or posedge rst)
  82. if (rst)
  83. full <= 1'b0;
  84. else
  85. full <= (full | (wr_ena & ~rd_ena & full_nxt)) & ~(rd_ena & ~wr_ena);
  86. assign wr_full = full;
  87. // Write
  88. // -----
  89. always @(posedge clk or posedge rst)
  90. if (rst)
  91. ram_wr_addr <= 0;
  92. else if (ram_wr_ena)
  93. ram_wr_addr <= ram_wr_addr + 1;
  94. assign ram_wr_data = wr_data;
  95. assign ram_wr_ena = wr_ena;
  96. // Read
  97. // ----
  98. always @(posedge clk or posedge rst)
  99. if (rst)
  100. ram_rd_addr <= 0;
  101. else if (ram_rd_ena)
  102. ram_rd_addr <= ram_rd_addr + 1;
  103. assign ram_rd_ena = (rd_ena | ~rd_valid) & ~lvl_empty;
  104. always @(posedge clk or posedge rst)
  105. if (rst)
  106. rd_valid <= 1'b0;
  107. else if (rd_ena | ~rd_valid)
  108. rd_valid <= ~lvl_empty;
  109. assign rd_data = ram_rd_data;
  110. assign rd_empty = ~rd_valid;
  111. // RAM
  112. // ---
  113. ram_sdp #(
  114. .AWIDTH(AWIDTH),
  115. .DWIDTH(WIDTH)
  116. ) ram_I (
  117. .wr_addr(ram_wr_addr),
  118. .wr_data(ram_wr_data),
  119. .wr_ena(ram_wr_ena),
  120. .rd_addr(ram_rd_addr),
  121. .rd_data(ram_rd_data),
  122. .rd_ena(ram_rd_ena),
  123. .clk(clk)
  124. );
  125. endmodule // fifo_sync_ram