uart_rx_back_to_back_tb.v 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. `timescale 1ns/1ps
  2. module uart_rx_back_to_back_tb;
  3. localparam integer CLKS_PER_BIT = 8;
  4. reg clk;
  5. reg rst_n;
  6. reg rx_i;
  7. reg rx_ready;
  8. wire rx_valid;
  9. wire [7:0] rx_data;
  10. wire framing_error;
  11. wire break_detected;
  12. wire overflow;
  13. reg [7:0] captured [0:3];
  14. integer capture_count;
  15. uart_rx_8n1 #(
  16. .CLKS_PER_BIT(CLKS_PER_BIT),
  17. .BREAK_BITS(11)
  18. ) dut (
  19. .clk(clk),
  20. .rst_n(rst_n),
  21. .rx_i(rx_i),
  22. .rx_valid(rx_valid),
  23. .rx_data(rx_data),
  24. .rx_ready(rx_ready),
  25. .framing_error(framing_error),
  26. .break_detected(break_detected),
  27. .overflow(overflow)
  28. );
  29. initial begin
  30. clk = 1'b0;
  31. forever #5 clk = ~clk;
  32. end
  33. always @(posedge clk or negedge rst_n) begin
  34. if (!rst_n) begin
  35. capture_count <= 0;
  36. end else if (rx_valid) begin
  37. captured[capture_count] <= rx_data;
  38. capture_count <= capture_count + 1;
  39. end
  40. end
  41. task drive_uart_byte;
  42. input [7:0] value;
  43. integer bit_index;
  44. begin
  45. rx_i = 1'b0;
  46. repeat (CLKS_PER_BIT) @(negedge clk);
  47. for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
  48. rx_i = value[bit_index];
  49. repeat (CLKS_PER_BIT) @(negedge clk);
  50. end
  51. rx_i = 1'b1;
  52. repeat (CLKS_PER_BIT) @(negedge clk);
  53. end
  54. endtask
  55. initial begin
  56. rst_n = 1'b0;
  57. rx_i = 1'b1;
  58. rx_ready = 1'b1;
  59. repeat (4) @(negedge clk);
  60. rst_n = 1'b1;
  61. drive_uart_byte(8'h33);
  62. drive_uart_byte(8'h34);
  63. drive_uart_byte(8'h20);
  64. drive_uart_byte(8'h0a);
  65. repeat (16) @(negedge clk);
  66. if (capture_count !== 4 || captured[0] !== 8'h33 || captured[1] !== 8'h34 ||
  67. captured[2] !== 8'h20 || captured[3] !== 8'h0a || framing_error || break_detected || overflow) begin
  68. $display(
  69. "UART RX back-to-back mismatch count=%0d bytes=%02x %02x %02x %02x framing=%0b break=%0b overflow=%0b",
  70. capture_count,
  71. captured[0],
  72. captured[1],
  73. captured[2],
  74. captured[3],
  75. framing_error,
  76. break_detected,
  77. overflow
  78. );
  79. $fatal;
  80. end
  81. $display("PASS: uart_rx_back_to_back_tb");
  82. $finish;
  83. end
  84. endmodule