You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.8 KiB

  1. `timescale 1 ns/10 ps // time-unit = 1 ns, precision = 10 ps
  2. // simplified CC_PLL model
  3. module CC_PLL #(
  4. parameter REF_CLK = "", // e.g. "10.0"
  5. parameter OUT_CLK = "", // e.g. "50.0"
  6. parameter PERF_MD = "", // LOWPOWER, ECONOMY, SPEED
  7. parameter LOW_JITTER = 1,
  8. parameter CI_FILTER_CONST = 2,
  9. parameter CP_FILTER_CONST = 4
  10. )(
  11. input CLK_REF, CLK_FEEDBACK, USR_CLK_REF,
  12. input USR_LOCKED_STDY_RST, USR_SET_SEL,
  13. output USR_PLL_LOCKED_STDY, USR_PLL_LOCKED,
  14. output CLK270, CLK180, CLK90, CLK0, CLK_REF_OUT
  15. );
  16. reg r_pll_clk;
  17. reg r_user_pll_locked;
  18. // OUT_FREQ = 10 MHz
  19. integer clk_half_period = 50;
  20. initial begin
  21. r_pll_clk = 1'b0;
  22. r_user_pll_locked = 1'b1;
  23. end
  24. always #clk_half_period r_pll_clk = ~r_pll_clk;
  25. assign CLK0 = r_pll_clk;
  26. assign USR_PLL_LOCKED = r_user_pll_locked;
  27. endmodule
  28. // simplified CC_CFG_END model
  29. module CC_CFG_END (
  30. output CFG_END
  31. );
  32. assign CFG_END = 1'b1;
  33. endmodule
  34. module tb_uart_loop;
  35. // DUT in/out
  36. reg clk = 1'b0;
  37. reg rst_n = 1'b1;
  38. reg uart_rx;
  39. wire uart_tx;
  40. // Testbench variables
  41. reg [7:0] tx_data = 8'h0;
  42. reg [7:0] rx_data = 8'h0;
  43. reg [7:0] ref_data [0:15];
  44. // Testbench 1/2 clock period
  45. localparam clk_half_period = 50;
  46. // UART period calculation (9600 baud)
  47. localparam uart_bit_period = 1000000000 / 9600;
  48. localparam uart_bit_half_period = uart_bit_period/2;
  49. uart_loop UUT (.clk_i(clk), .rst_n_i(rst_n), .uart_rx_i(uart_rx), .uart_tx_o(uart_tx));
  50. // set dumpfile
  51. initial begin
  52. $dumpfile ("tb_uart_loop.fst");
  53. $dumpvars (0, tb_uart_loop);
  54. end
  55. // Setup simulation
  56. initial begin
  57. uart_rx = 1'b1;
  58. #1 rst_n = 1'b0;
  59. #120 rst_n = 1'b1;
  60. end
  61. // Generate 10 mhz clock
  62. always #clk_half_period clk = !clk;
  63. // Stimuli generator
  64. initial
  65. forever @(posedge rst_n) begin
  66. uart_rx = 1'b1;
  67. #uart_bit_period;
  68. for (integer i = 0; i < $size(ref_data); i = i + 1) begin
  69. tx_data = {$random} % 255;
  70. ref_data[i] = tx_data;
  71. $display ("UART send: 0x%h", tx_data);
  72. uart_rx = 1'b0;
  73. #uart_bit_period;
  74. for (integer i = 0; i <= 7; i = i + 1) begin
  75. uart_rx = tx_data[i];
  76. #uart_bit_period;
  77. end
  78. uart_rx = 1'b1;
  79. #uart_bit_period;
  80. #uart_bit_period;
  81. #uart_bit_period;
  82. end
  83. end
  84. // Checker
  85. initial begin
  86. @(posedge rst_n)
  87. for (integer i = 0; i < $size(ref_data); i = i + 1) begin
  88. @(negedge uart_tx)
  89. #uart_bit_period;
  90. #uart_bit_half_period;
  91. for (integer i = 0; i <= 7; i = i + 1) begin
  92. rx_data[i] = uart_tx;
  93. #uart_bit_period;
  94. end
  95. assert (rx_data == ref_data[i])
  96. $display("UART recv: 0x%h", rx_data);
  97. else
  98. $error("UART receive error, got 0x%h, expected 0x%h", rx_data, ref_data[i]);
  99. end
  100. $display ("UART tests finished");
  101. $finish;
  102. end
  103. endmodule