fifo_sync_shift.v 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * fifo_sync_shift.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_shift #(
  35. parameter integer DEPTH = 4,
  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. // Signals
  48. // -------
  49. wire [DEPTH+1:0] ce;
  50. wire [DEPTH+1:0] valid;
  51. wire [WIDTH-1:0] data [DEPTH+1:0];
  52. // Stages
  53. // ------
  54. // Generate loop
  55. genvar i;
  56. generate
  57. for (i=1; i<=DEPTH; i=i+1)
  58. begin : stage
  59. // Local signals
  60. reg [WIDTH-1:0] l_data;
  61. reg l_valid;
  62. // Data register
  63. always @(posedge clk)
  64. if (ce[i])
  65. l_data <= valid[i+1] ? data[i+1] : wr_data;
  66. // Valid flag
  67. always @(posedge clk or posedge rst)
  68. if (rst)
  69. l_valid <= 1'b0;
  70. else if (ce[i])
  71. l_valid <= ~rd_ena | valid[i+1] | (wr_ena & valid[i]);
  72. // CE for this stage
  73. assign ce[i] = rd_ena | (wr_ena & ~valid[i] & valid[i-1]);
  74. // Map
  75. assign data[i] = l_data;
  76. assign valid[i] = l_valid;
  77. end
  78. endgenerate
  79. // Boundary conditions
  80. assign data[DEPTH+1] = wr_data;
  81. assign data[0] = { WIDTH{1'bx} };
  82. assign valid[DEPTH+1] = 1'b0;
  83. assign valid[0] = 1'b1;
  84. assign ce[DEPTH+1] = 1'bx;
  85. assign ce[0] = 1'bx;
  86. // User IF
  87. // -------
  88. assign wr_full = valid[DEPTH];
  89. assign rd_empty = ~valid[1];
  90. assign rd_data = data[1];
  91. endmodule // fifo_sync_shift