usb_tx_pkt.v 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * usb_tx_pkt.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_pkt (
  27. // Low-Level
  28. output reg ll_start,
  29. output wire ll_bit,
  30. output wire ll_last,
  31. input wire ll_ack,
  32. // Packet interface
  33. input wire pkt_start,
  34. output reg pkt_done,
  35. input wire [3:0] pkt_pid,
  36. input wire [9:0] pkt_len,
  37. input wire [7:0] pkt_data,
  38. output reg pkt_data_ack,
  39. // Common
  40. input wire clk,
  41. input wire rst
  42. );
  43. `include "usb_defs.vh"
  44. // FSM
  45. // ---
  46. localparam
  47. ST_IDLE = 0,
  48. ST_SYNC = 1,
  49. ST_PID = 2,
  50. ST_DATA = 3,
  51. ST_CRC_LSB = 4,
  52. ST_CRC_MSB = 5;
  53. // Signals
  54. // -------
  55. // FSM
  56. reg [3:0] state_nxt;
  57. reg [3:0] state;
  58. // Helper
  59. reg pid_is_handshake;
  60. wire next;
  61. // Shift register
  62. reg [3:0] shift_bit;
  63. reg [7:0] shift_load;
  64. reg [7:0] shift_data;
  65. reg shift_data_crc;
  66. wire shift_last_bit;
  67. reg shift_last_byte;
  68. wire shift_do_load;
  69. wire shift_now;
  70. reg shift_new_bit;
  71. // Packet length
  72. reg [10:0] len;
  73. wire len_last;
  74. wire len_dec;
  75. // CRC
  76. wire crc_in_bit;
  77. reg crc_in_first;
  78. wire crc_in_valid;
  79. wire [15:0] crc;
  80. // Main FSM
  81. // --------
  82. // Next state logic
  83. always @(*)
  84. begin
  85. // Default is to stay put
  86. state_nxt = state;
  87. // Main case
  88. case (state)
  89. ST_IDLE:
  90. if (pkt_start)
  91. state_nxt = ST_SYNC;
  92. ST_SYNC:
  93. state_nxt = ST_PID;
  94. ST_PID:
  95. if (next)
  96. begin
  97. if (pid_is_handshake)
  98. state_nxt = ST_IDLE;
  99. else if (len_last)
  100. state_nxt = ST_CRC_LSB;
  101. else
  102. state_nxt = ST_DATA;
  103. end
  104. ST_DATA:
  105. if (next && len_last)
  106. state_nxt = ST_CRC_LSB;
  107. ST_CRC_LSB:
  108. if (next)
  109. state_nxt = ST_CRC_MSB;
  110. ST_CRC_MSB:
  111. if (next)
  112. state_nxt = ST_IDLE;
  113. endcase
  114. end
  115. // State register
  116. always @(posedge clk or posedge rst)
  117. if (rst)
  118. state <= ST_IDLE;
  119. else
  120. state <= state_nxt;
  121. // Helper
  122. // ------
  123. always @(posedge clk)
  124. pid_is_handshake <= (pkt_pid == PID_ACK) || (pkt_pid == PID_NAK) || (pkt_pid == PID_STALL);
  125. assign next = shift_last_bit & ll_ack;
  126. // Shift register
  127. // --------------
  128. // When to load a new byte
  129. assign shift_do_load = (state == ST_SYNC) | (shift_last_bit & ll_ack);
  130. // When to shift
  131. assign shift_now = (state == ST_SYNC) | ll_ack;
  132. // Bit counter
  133. always @(posedge clk)
  134. if (shift_now)
  135. shift_bit <= (shift_do_load ? 4'b0111 : shift_bit) - 1;
  136. assign shift_last_bit = shift_bit[3];
  137. // Load mux
  138. always @(*)
  139. case (state)
  140. ST_SYNC: shift_load <= 8'h80;
  141. ST_PID: shift_load <= { ~pkt_pid, pkt_pid };
  142. ST_DATA: shift_load <= pkt_data;
  143. ST_CRC_LSB: shift_load <= crc_in_first ? 8'h00 : crc[ 7:0];
  144. ST_CRC_MSB: shift_load <= crc_in_first ? 8'h00 : crc[15:8];
  145. default: shift_load <= 8'hxx;
  146. endcase
  147. // Shift data
  148. always @(posedge clk)
  149. if (shift_now)
  150. shift_data <= shift_do_load ? shift_load : {1'b0, shift_data[7:1]};
  151. // Some flags about the data
  152. always @(posedge clk)
  153. if (shift_now & shift_do_load) begin
  154. shift_data_crc <= (state == ST_DATA);
  155. shift_last_byte <= (state == ST_CRC_MSB) | ((state == ST_PID) & pid_is_handshake);
  156. end
  157. // When a fresh new bit is available
  158. always @(posedge clk)
  159. shift_new_bit <= shift_now;
  160. // Packet length
  161. // -------------
  162. assign len_dec = pkt_start || (shift_do_load && ((state == ST_DATA) || (state == ST_PID)));
  163. always @(posedge clk)
  164. if (len_dec)
  165. len <= (pkt_start ? { 1'b0, pkt_len } : len) - 1;
  166. assign len_last = len[10];
  167. // CRC generation
  168. // --------------
  169. // Keep track of first bit
  170. always @(posedge clk)
  171. crc_in_first <= (crc_in_first & ~crc_in_valid) | (state == ST_IDLE);
  172. // Input all bits once acked
  173. assign crc_in_bit = shift_data[0];
  174. assign crc_in_valid = shift_data_crc & shift_new_bit;
  175. // CRC16 core
  176. usb_crc #(
  177. .WIDTH(16),
  178. .POLY(16'h8005),
  179. .MATCH(16'h800D)
  180. ) crc_16_I (
  181. .in_bit(crc_in_bit),
  182. .in_first(crc_in_first),
  183. .in_valid(crc_in_valid),
  184. .crc(crc),
  185. .crc_match(),
  186. .clk(clk),
  187. .rst(rst)
  188. );
  189. // Low-level control
  190. // -----------------
  191. // Start right after the load of SYNC
  192. always @(posedge clk)
  193. ll_start <= state == ST_SYNC;
  194. // Bit
  195. assign ll_bit = shift_data[0];
  196. assign ll_last = shift_last_bit & shift_last_byte;
  197. // Packet interface feedback
  198. // -------------------------
  199. // We don't care about the delay, better register
  200. always @(posedge clk)
  201. begin
  202. pkt_done <= ll_ack && ll_last;
  203. pkt_data_ack <= (state == ST_DATA) && next;
  204. end
  205. endmodule // usb_tx_pkt