pkt_mux.v 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * pkt_mux.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_mux #(
  35. parameter integer N = 3
  36. )(
  37. // Multiple packet FIFOs interfaces
  38. input wire [8*N-1:0] pkt_data,
  39. input wire [ N-1:0] pkt_last,
  40. input wire [ N-1:0] pkt_valid,
  41. output wire [ N-1:0] pkt_ack,
  42. // HS PHY interface
  43. output wire [7:0] hs_data,
  44. output wire hs_start,
  45. output wire hs_last,
  46. output wire hs_clk_req,
  47. input wire hs_clk_rdy,
  48. // Clock / Reset
  49. input wire clk,
  50. input wire rst
  51. );
  52. // Signals
  53. // -------
  54. // FSM
  55. localparam
  56. ST_CLK_OFF = 0,
  57. ST_CLK_BOOT = 1,
  58. ST_CLK_RUN = 2,
  59. ST_STREAM = 3,
  60. ST_EOTP = 4;
  61. reg [2:0] fsm_state;
  62. reg [2:0] fsm_state_next;
  63. // EoTp
  64. reg [7:0] eotp_data;
  65. reg [1:0] eotp_cnt;
  66. reg eotp_last;
  67. // HS clock
  68. // FSM
  69. // ---
  70. // State register
  71. always @(posedge clk)
  72. if (rst)
  73. fsm_state <= ST_CLK_OFF;
  74. else
  75. fsm_state <= fsm_state_next;
  76. // Next-State logic
  77. always @(*)
  78. begin
  79. // Default is to not move
  80. fsm_state_next = fsm_state;
  81. // Transitions ?
  82. case (fsm_state)
  83. ST_CLK_OFF:
  84. if ( )
  85. fsm_state_next = ST_STREAM;
  86. ST_CLK_BOOT:
  87. if (hs_clk_rdy)
  88. fsm_state_next = ST_CLK_RUN;
  89. ST_CLK_RUN:
  90. if (hs_clk_timeout)
  91. fsm_state_next = ST_CLK_OFF;
  92. else if ( )
  93. fsm_state_next = ST_STREAM;
  94. ST_STREAM:
  95. if ( )
  96. fsm_state_next = ST_EOTP;
  97. ST_EOTP:
  98. if (hs_ack & eotp_last)
  99. fsm_state_next = ST_CLK_RUN;
  100. endcase
  101. end
  102. // EoTp logic
  103. // ----------
  104. always @(posedge clk)
  105. if (fsm_state != ST_EOTP) begin
  106. eotp_cnt <= 2'b00;
  107. eotp_last <= 1'b0;
  108. end else if (hs_ack) begin
  109. eotp_cnt <= eotp_cnt + 1;
  110. eotp_last <= (eotp_cnt == 2'b10);
  111. end
  112. always @(eotp_cnt)
  113. case (eotp_cnt)
  114. 2'b00: eotp_data = 8'h08;
  115. 2'b01: eotp_data = 8'h0f;
  116. 2'b10: eotp_data = 8'h0f;
  117. 2'b11: eotp_data = 8'h01;
  118. endcase
  119. // HS clock
  120. // --------
  121. reg [15:0] hs_clk_timer;
  122. wire hs_clk_timeout;
  123. // Request
  124. assign hs_clk_req = (fsm_state != ST_CLK_OFF);
  125. // Time-Out
  126. always @(posedge clk)
  127. if (fsm_state != ST_CLK_RUN)
  128. hs_clk_timer <= 0;
  129. else if (~hs_clk_timeout)
  130. hs_clk_timer <= hs_clk_timer + 1
  131. assign hs_clk_timeout <= hs_clk_timer[15];
  132. // Data mux
  133. // --------
  134. // "Any" valid - Is there any channel valid
  135. // "Any Other" valid - Is there any channel valid other than the current one
  136. input wire [8*N-1:0] pkt_data,
  137. input wire [ N-1:0] pkt_last,
  138. input wire [ N-1:0] pkt_valid,
  139. output wire [ N-1:0] pkt_ack,
  140. // Data mux
  141. // --------
  142. endmodule // pkt_mux