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.

80 lines
1.8 KiB

  1. `timescale 1 ns/10 ps // time-unit = 1 ns, precision = 10 ps
  2. module tb_uart_reg;
  3. reg clk = 0;
  4. reg rst_n;
  5. reg uart_rx;
  6. wire uart_tx;
  7. reg [7:0] tx_data = 0;
  8. reg [7:0] rx_data = 0;
  9. wire [3:0] led_n;
  10. localparam clk_half_period = 50;
  11. localparam uart_bit_period = 1000000000 / 9600;
  12. localparam uart_bit_half_period = uart_bit_period/2;
  13. uart_reg UUT (.clk_i(clk), .rst_n_i(rst_n), .uart_rx_i(uart_rx), .uart_tx_o(uart_tx), .led_n_o(led_n));
  14. // set dumpfile
  15. initial begin
  16. $dumpfile ("tb_uart_reg.fst");
  17. $dumpvars (0, tb_uart_reg);
  18. end
  19. // setup simulation
  20. initial begin
  21. rst_n = 1;
  22. #1 rst_n = 0;
  23. #20 rst_n = 1;
  24. end
  25. // generate clock with 100 mhz
  26. always #clk_half_period clk = !clk;
  27. initial begin
  28. uart_rx = 1'b1;
  29. end
  30. initial
  31. forever @(posedge rst_n) begin
  32. uart_rx = 1'b1;
  33. #uart_bit_period;
  34. for (integer tx = 0; tx < 16; tx = tx + 1) begin
  35. tx_data = tx;
  36. $display ("UART send: 0x%h", tx_data);
  37. uart_rx = 1'b0;
  38. #uart_bit_period;
  39. for (integer i = 0; i < 7; i = i + 1) begin
  40. uart_rx = tx_data[i];
  41. #uart_bit_period;
  42. end
  43. uart_rx = 1'b1;
  44. #uart_bit_period;
  45. #uart_bit_period
  46. #uart_bit_period;
  47. end
  48. end
  49. // Checker
  50. always begin
  51. wait (rst_n)
  52. for (reg [7:0] rx = 0; rx < 16; rx = rx + 1) begin
  53. @(negedge uart_tx)
  54. #uart_bit_period;
  55. #uart_bit_half_period;
  56. for (integer i = 0; i < 7; i = i + 1) begin
  57. rx_data[i] = uart_tx;
  58. #uart_bit_period;
  59. end
  60. assert (rx_data == rx)
  61. $display("UART recv: 0x%h", rx_data);
  62. else
  63. $warning("UART receive error, got 0x%h, expected 0x%h", rx_data, rx);
  64. end
  65. $display ("UART tests finished");
  66. $finish;
  67. end
  68. endmodule