usb_tb.v 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * usb_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_tb;
  28. // Signals
  29. reg rst = 1;
  30. reg clk_48m = 0; // USB clock
  31. reg clk_samp = 0; // Capture samplerate
  32. reg [7:0] in_file_data;
  33. reg in_file_valid;
  34. reg in_file_done;
  35. wire usb_dp;
  36. wire usb_dn;
  37. wire usb_pu;
  38. wire [ 8:0] ep_tx_addr_0;
  39. wire [31:0] ep_tx_data_0;
  40. wire ep_tx_we_0;
  41. wire [ 8:0] ep_rx_addr_0;
  42. wire [31:0] ep_rx_data_1;
  43. wire ep_rx_re_0;
  44. wire [11:0] bus_addr;
  45. wire [15:0] bus_din;
  46. wire [15:0] bus_dout;
  47. wire bus_cyc;
  48. wire bus_we;
  49. wire bus_ack;
  50. // Setup recording
  51. initial begin
  52. $dumpfile("usb_tb.vcd");
  53. $dumpvars(0,usb_tb);
  54. end
  55. // Reset pulse
  56. initial begin
  57. # 200 rst = 0;
  58. # 1000000 $finish;
  59. end
  60. // Clocks
  61. always #10.416 clk_48m = !clk_48m;
  62. always #3.247 clk_samp = !clk_samp;
  63. // DUT
  64. usb #(
  65. .TARGET("ICE40"),
  66. .EPDW(32)
  67. ) dut_I (
  68. .pad_dp(usb_dp),
  69. .pad_dn(usb_dn),
  70. .pad_pu(usb_pu),
  71. .ep_tx_addr_0(ep_tx_addr_0),
  72. .ep_tx_data_0(ep_tx_data_0),
  73. .ep_tx_we_0(ep_tx_we_0),
  74. .ep_rx_addr_0(ep_rx_addr_0),
  75. .ep_rx_data_1(ep_rx_data_1),
  76. .ep_rx_re_0(ep_rx_re_0),
  77. .ep_clk(clk_48m),
  78. .bus_addr(bus_addr),
  79. .bus_din(bus_din),
  80. .bus_dout(bus_dout),
  81. .bus_cyc(bus_cyc),
  82. .bus_we(bus_we),
  83. .bus_ack(bus_ack),
  84. .clk(clk_48m),
  85. .rst(rst)
  86. );
  87. reg [7:0] cnt;
  88. always @(posedge clk_48m)
  89. if (bus_ack)
  90. cnt <= 0;
  91. else if (~cnt[7])
  92. cnt <= cnt + 1;
  93. assign bus_addr = 12'h000;
  94. assign bus_din = 16'h8001;
  95. assign bus_cyc = cnt[7];
  96. assign bus_we = 1'b1;
  97. assign ep_rx_addr_0 = 9'h000;
  98. assign ep_rx_re_0 = 1'b1;
  99. assign ep_tx_addr_0 = 9'h000;
  100. assign ep_tx_data_0 = 32'h02000112;
  101. assign ep_tx_we_0 = 1'b1;
  102. // Read file
  103. integer fh_in, rv;
  104. initial
  105. fh_in = $fopen("../data/capture_usb_raw_short.bin", "rb");
  106. always @(posedge clk_samp)
  107. begin
  108. if (rst) begin
  109. in_file_data <= 8'h00;
  110. in_file_valid <= 1'b0;
  111. in_file_done <= 1'b0;
  112. end else begin
  113. if (!in_file_done) begin
  114. rv = $fread(in_file_data, fh_in);
  115. in_file_valid <= (rv == 1);
  116. in_file_done <= (rv != 1);
  117. end else begin
  118. in_file_data <= 8'h00;
  119. in_file_valid <= 1'b0;
  120. in_file_done <= 1'b1;
  121. end
  122. end
  123. end
  124. // Input
  125. assign usb_dp = in_file_data[1] & in_file_valid;
  126. assign usb_dn = in_file_data[0] & in_file_valid;
  127. endmodule // usb_tb