usb_tx_tb.v 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * usb_tx_tb.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. `timescale 1ns/100ps
  27. module usb_tx_tb;
  28. // Signals
  29. reg rst = 1;
  30. reg clk_48m = 0; // USB clock
  31. wire phy_tx_dp;
  32. wire phy_tx_dn;
  33. wire phy_tx_en;
  34. wire ll_start;
  35. wire ll_bit;
  36. wire ll_last;
  37. wire ll_ack;
  38. wire pkt_start;
  39. wire pkt_done;
  40. wire [3:0] pkt_pid;
  41. wire [9:0] pkt_len;
  42. reg [11:0] pkt_data_addr;
  43. reg [7:0] pkt_data;
  44. wire pkt_data_ack;
  45. // Setup recording
  46. initial begin
  47. $dumpfile("usb_tx_tb.vcd");
  48. $dumpvars(0,usb_tx_tb);
  49. end
  50. // Reset pulse
  51. initial begin
  52. # 200 rst = 0;
  53. # 400000 $finish;
  54. end
  55. // Clocks
  56. always #10.416 clk_48m = !clk_48m;
  57. // DUT
  58. usb_tx_ll tx_ll_I (
  59. .phy_tx_dp(phy_tx_dp),
  60. .phy_tx_dn(phy_tx_dn),
  61. .phy_tx_en(phy_tx_en),
  62. .ll_start(ll_start),
  63. .ll_bit(ll_bit),
  64. .ll_last(ll_last),
  65. .ll_ack(ll_ack),
  66. .clk(clk_48m),
  67. .rst(rst)
  68. );
  69. `ifndef NO_PKT
  70. usb_tx_pkt tx_pkt_I (
  71. .ll_start(ll_start),
  72. .ll_bit(ll_bit),
  73. .ll_last(ll_last),
  74. .ll_ack(ll_ack),
  75. .pkt_start(pkt_start),
  76. .pkt_done(pkt_done),
  77. .pkt_pid(pkt_pid),
  78. .pkt_len(pkt_len),
  79. .pkt_data(pkt_data),
  80. .pkt_data_ack(pkt_data_ack),
  81. .clk(clk_48m),
  82. .rst(rst)
  83. );
  84. // Start signal
  85. reg [7:0] cnt;
  86. reg ready;
  87. always @(posedge clk_48m)
  88. if (rst)
  89. ready <= 1'b1;
  90. else
  91. if (pkt_start)
  92. ready <= 1'b0;
  93. else if (pkt_done)
  94. ready <= 1'b1;
  95. always @(posedge clk_48m)
  96. if (rst)
  97. cnt <= 0;
  98. else
  99. cnt <= cnt + 1;
  100. assign pkt_start = (cnt == 8'hff) & ready;
  101. // Packet
  102. assign pkt_len = 10'h100; // 256 bytes payload
  103. assign pkt_pid = 4'b0011; // DATA0
  104. // assign pkt_pid = 4'b0010; // ACK
  105. // Fake data source
  106. always @(posedge clk_48m)
  107. if (rst)
  108. pkt_data_addr <= 8'h00;
  109. else
  110. pkt_data_addr <= pkt_data_addr + pkt_data_ack;
  111. always @(*)
  112. case (pkt_data_addr)
  113. 12'h000: pkt_data = 8'h8c;
  114. 12'h001: pkt_data = 8'h1a;
  115. 12'h002: pkt_data = 8'hf2;
  116. 12'h003: pkt_data = 8'hf0;
  117. 12'h100: pkt_data = 8'ha0;
  118. 12'h101: pkt_data = 8'h28;
  119. 12'h102: pkt_data = 8'hf2;
  120. 12'h103: pkt_data = 8'hf0;
  121. default: pkt_data = 8'h00;
  122. endcase
  123. `endif
  124. `ifdef NO_PKT
  125. wire [31:0] bit_seq = 32'b00000001_10100101_11111111_11100000;
  126. reg [7:0] cnt;
  127. reg started;
  128. always @(posedge clk_48m)
  129. if (rst)
  130. cnt <= 0;
  131. else if (ll_ack | ~started)
  132. cnt <= cnt + 1;
  133. always @(posedge clk_48m)
  134. if (rst)
  135. started <= 1'b0;
  136. else
  137. if (ll_start)
  138. started <= 1'b1;
  139. else if (ll_last & ll_ack)
  140. started <= 1'b0;
  141. assign ll_start = (cnt == 8'h1f);
  142. assign ll_bit = bit_seq[31 - cnt[4:0]];
  143. assign ll_last = cnt[4:0] == 31;
  144. `endif
  145. wire trig = tx_ll_I.ll_last & tx_ll_I.br_now & tx_ll_I.bs_now;
  146. endmodule // usb_tx_tb