e1_tb.v 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * e1_tb.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut <tnt@246tNt.com>
  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 e1_tb;
  28. // Signals
  29. reg rst = 1;
  30. reg clk_16m = 0;
  31. reg clk_30m72 = 0;
  32. reg [7:0] in_file_data;
  33. reg in_file_valid;
  34. reg in_file_done;
  35. wire e1_in_tip;
  36. wire e1_in_ring;
  37. wire e1_bit;
  38. wire e1_valid;
  39. wire e1_out_tip;
  40. wire e1_out_ring;
  41. wire df_valid;
  42. wire [7:0] df_data;
  43. wire [4:0] df_ts;
  44. // Setup recording
  45. initial begin
  46. $dumpfile("e1_tb.vcd");
  47. $dumpvars(0,e1_tb);
  48. end
  49. // Reset pulse
  50. initial begin
  51. # 200 rst = 0;
  52. # 10000000 $finish;
  53. end
  54. // Clocks
  55. always #31.25 clk_16m = !clk_16m;
  56. always #16.276 clk_30m72 = !clk_30m72;
  57. // DUT
  58. e1_rx rx_I (
  59. .pad_rx_hi_p( e1_in_ring),
  60. .pad_rx_hi_n(~e1_in_ring),
  61. .pad_rx_lo_p( e1_in_tip),
  62. .pad_rx_lo_n(~e1_in_tip),
  63. .out_bit(e1_bit),
  64. .out_valid(e1_valid),
  65. .clk(clk_30m72),
  66. .rst(rst)
  67. );
  68. e1_tx tx_I (
  69. .pad_tx_hi(e1_out_ring),
  70. .pad_tx_lo(e1_out_tip),
  71. .in_bit(e1_bit),
  72. .in_valid(e1_valid),
  73. .clk(clk_30m72),
  74. .rst(rst)
  75. );
  76. e1_rx_deframer rx_deframer_I (
  77. .in_bit(e1_bit),
  78. .in_valid(e1_valid),
  79. .out_data(df_data),
  80. .out_valid(df_valid),
  81. .out_ts(df_ts),
  82. .clk(clk_30m72),
  83. .rst(rst)
  84. );
  85. // Read file
  86. integer fh_in, rv;
  87. initial
  88. fh_in = $fopen("../data/capture_e1_raw.bin", "rb");
  89. always @(posedge clk_16m)
  90. begin
  91. if (rst) begin
  92. in_file_data <= 8'h00;
  93. in_file_valid <= 1'b0;
  94. in_file_done <= 1'b0;
  95. end else begin
  96. if (!in_file_done) begin
  97. rv = $fread(in_file_data, fh_in);
  98. in_file_valid <= (rv == 1);
  99. in_file_done <= (rv != 1);
  100. end else begin
  101. in_file_data <= 8'h00;
  102. in_file_valid <= 1'b0;
  103. in_file_done <= 1'b1;
  104. end
  105. end
  106. end
  107. // Write file
  108. integer fh_out;
  109. initial
  110. fh_out = $fopen("/tmp/e1.txt", "w");
  111. always @(posedge clk_30m72)
  112. begin
  113. if (e1_valid) begin
  114. $fwrite(fh_out, "%d", e1_bit);
  115. end
  116. end
  117. // Input
  118. assign e1_in_tip = in_file_data[0] & in_file_valid;
  119. assign e1_in_ring = in_file_data[1] & in_file_valid;
  120. endmodule // e1_tb