ice40_serdes_sync.v 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * ice40_serdes_sync.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2020 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 ice40_serdes_sync #(
  35. parameter integer PHASE = 0,
  36. parameter integer NEG_EDGE = 0,
  37. parameter integer GLOBAL_BUF = 0,
  38. parameter BEL_BASE = "X12/Y15"
  39. )(
  40. input wire clk_slow,
  41. input wire clk_fast,
  42. input wire rst,
  43. output wire sync
  44. );
  45. wire [1:0] clk_samp;
  46. wire [1:0] edge_det;
  47. wire [1:0] edge_found;
  48. wire [1:0] cnt_next;
  49. wire [1:0] cnt_val;
  50. wire sync_next;
  51. wire sync_i;
  52. // Double sample of the slow clock
  53. ice40_serdes_dff #(
  54. .NEG(NEG_EDGE),
  55. .RST(1),
  56. .BEL({BEL_BASE, "/lc0"})
  57. ) ff_samp0_I (
  58. .d(clk_slow),
  59. .q(clk_samp[0]),
  60. .c(clk_fast),
  61. .r(rst)
  62. );
  63. ice40_serdes_dff #(
  64. .NEG(NEG_EDGE),
  65. .RST(1),
  66. .BEL({BEL_BASE, "/lc1"})
  67. ) ff_samp1_I (
  68. .d(clk_samp[0]),
  69. .q(clk_samp[1]),
  70. .c(clk_fast),
  71. .r(rst)
  72. );
  73. // Detect falling edge, then rising edge
  74. assign edge_det[0] = edge_found[0] | (clk_samp[1] & ~clk_samp[0]);
  75. assign edge_det[1] = edge_found[1] | (clk_samp[0] & ~clk_samp[1] & edge_found[0]);
  76. ice40_serdes_dff #(
  77. .NEG(NEG_EDGE),
  78. .RST(1),
  79. .BEL({BEL_BASE, "/lc2"})
  80. ) ff_edge0_I (
  81. .d(edge_det[0]),
  82. .q(edge_found[0]),
  83. .c(clk_fast),
  84. .r(rst)
  85. );
  86. ice40_serdes_dff #(
  87. .NEG(NEG_EDGE),
  88. .RST(1),
  89. .BEL({BEL_BASE, "/lc3"})
  90. ) ff_edge1_I (
  91. .d(edge_det[1]),
  92. .q(edge_found[1]),
  93. .c(clk_fast),
  94. .r(rst)
  95. );
  96. // 2 bit upcounter
  97. assign cnt_next[0] = cnt_val[0] ^ edge_found[1];
  98. assign cnt_next[1] = cnt_val[1] ^ (cnt_val[0] & edge_found[1]);
  99. ice40_serdes_dff #(
  100. .NEG(NEG_EDGE),
  101. .RST(1),
  102. .BEL({BEL_BASE, "/lc4"})
  103. ) ff_cnt0_I (
  104. .d(cnt_next[0]),
  105. .q(cnt_val[0]),
  106. .c(clk_fast),
  107. .r(rst)
  108. );
  109. ice40_serdes_dff #(
  110. .NEG(NEG_EDGE),
  111. .RST(1),
  112. .BEL({BEL_BASE, "/lc5"})
  113. ) ff_cnt1_I (
  114. .d(cnt_next[1]),
  115. .q(cnt_val[1]),
  116. .c(clk_fast),
  117. .r(rst)
  118. );
  119. // Final comparator
  120. assign sync_next = edge_found[1] & (cnt_val == PHASE);
  121. ice40_serdes_dff #(
  122. .NEG(NEG_EDGE),
  123. .RST(1),
  124. .BEL({BEL_BASE, "/lc6"})
  125. ) ff_sync_I (
  126. .d(sync_next),
  127. .q(sync_i),
  128. .c(clk_fast),
  129. .r(rst)
  130. );
  131. // Buffer ?
  132. generate
  133. if (GLOBAL_BUF)
  134. SB_GB gbuf_sync_I (
  135. .USER_SIGNAL_TO_GLOBAL_BUFFER(sync_i),
  136. .GLOBAL_BUFFER_OUTPUT(sync)
  137. );
  138. else
  139. assign sync = sync_i;
  140. endgenerate
  141. endmodule