usb_tx_ll.v 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * usb_tx_ll.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut
  7. * All rights reserved.
  8. *
  9. * LGPL v3+, see LICENSE.lgpl3
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. `default_nettype none
  26. module usb_tx_ll (
  27. // PHY
  28. output wire phy_tx_dp,
  29. output wire phy_tx_dn,
  30. output wire phy_tx_en,
  31. // Low-Level
  32. input wire ll_start,
  33. input wire ll_bit,
  34. input wire ll_last,
  35. output reg ll_ack,
  36. // Common
  37. input wire clk,
  38. input wire rst
  39. );
  40. `include "usb_defs.vh"
  41. // Signals
  42. // -------
  43. // State
  44. reg [2:0] state;
  45. wire active;
  46. reg [2:0] br_cnt;
  47. wire br_now;
  48. // Bit stuffing
  49. reg [2:0] bs_cnt;
  50. reg bs_now;
  51. wire bs_bit;
  52. // NRZI
  53. reg lvl_prev;
  54. // Output
  55. reg out_active;
  56. wire [1:0] out_sym_nxt;
  57. reg [1:0] out_sym;
  58. // State
  59. // -----
  60. always @(posedge clk or posedge rst)
  61. if (rst)
  62. state <= 3'b000;
  63. else begin
  64. if (ll_start)
  65. state <= 3'b100;
  66. else if (br_now & ~bs_now) begin
  67. if (ll_last)
  68. state <= 3'b101;
  69. else
  70. case (state[1:0])
  71. 2'b00: state <= state;
  72. 2'b01: state <= 3'b110;
  73. 2'b10: state <= 3'b111;
  74. default: state <= 3'b000;
  75. endcase
  76. end
  77. end
  78. assign active = state[2];
  79. always @(posedge clk)
  80. br_cnt <= { 1'b0, active ? br_cnt[1:0] : 2'b10 } + 1;
  81. assign br_now = br_cnt[2];
  82. // Bit Stuffing
  83. // ------------
  84. // Track number of 1s
  85. always @(posedge clk or posedge ll_start)
  86. if (ll_start) begin
  87. bs_cnt <= 3'b000;
  88. bs_now <= 1'b0;
  89. end else if (br_now) begin
  90. bs_cnt <= (ll_bit & ~bs_now) ? (bs_cnt + 1) : 3'b000;
  91. bs_now <= ll_bit & (bs_cnt == 3'b101);
  92. end
  93. // Effective bit
  94. assign bs_bit = ~bs_now & ll_bit;
  95. // Track previous level
  96. always @(posedge clk)
  97. lvl_prev <= active ? (lvl_prev ^ (~bs_bit & br_now)) : 1'b1;
  98. // Output stage
  99. // ------------
  100. // Ack input
  101. always @(posedge clk)
  102. ll_ack <= br_now & ~bs_now & (state[1:0] == 2'b00);
  103. // Output symbol. Must be forced to 'J' outside of active area to
  104. // be ready for the next packet start
  105. assign out_sym_nxt = (bs_bit ^ lvl_prev) ? SYM_K : SYM_J;
  106. always @(posedge clk or posedge rst)
  107. begin
  108. if (rst)
  109. out_sym <= SYM_J;
  110. else if (br_now) begin
  111. case (state[1:0])
  112. 2'b00: out_sym <= out_sym_nxt;
  113. 2'b01: out_sym <= bs_now ? out_sym_nxt : SYM_SE0;
  114. 2'b10: out_sym <= SYM_SE0;
  115. 2'b11: out_sym <= SYM_J;
  116. default: out_sym <= 2'bxx;
  117. endcase
  118. end
  119. end
  120. // The OE is a bit in advance (not aligned with br_now) on purpose
  121. // so that we output a bit of 'J' at the packet beginning
  122. always @(posedge clk)
  123. out_active <= active;
  124. // PHY control
  125. assign phy_tx_dp = out_sym[1];
  126. assign phy_tx_dn = out_sym[0];
  127. assign phy_tx_en = out_active;
  128. endmodule // usb_tx_ll