uart_rx_byte_tb.v 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. `timescale 1ns/1ps
  2. module uart_rx_byte_tb;
  3. reg clk = 1'b0;
  4. reg rst_n = 1'b0;
  5. reg rx_i = 1'b1;
  6. wire byte_valid;
  7. wire [7:0] byte_data;
  8. wire framing_error;
  9. integer valid_count;
  10. integer framing_count;
  11. always #1 clk = ~clk;
  12. uart_rx_byte #(.CLKS_PER_BIT(8)) dut (
  13. .clk(clk), .rst_n(rst_n), .rx_i(rx_i),
  14. .byte_valid(byte_valid), .byte_data(byte_data),
  15. .framing_error(framing_error)
  16. );
  17. always @(posedge clk) begin
  18. if (byte_valid) begin
  19. valid_count = valid_count + 1;
  20. if (byte_data !== 8'hA5) $fatal(1, "RX data mismatch: %h", byte_data);
  21. end
  22. if (framing_error) framing_count = framing_count + 1;
  23. end
  24. task send_byte;
  25. input [7:0] value;
  26. input good_stop;
  27. integer bit_index;
  28. begin
  29. rx_i = 1'b0;
  30. repeat (8) @(posedge clk);
  31. for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
  32. rx_i = value[bit_index];
  33. repeat (8) @(posedge clk);
  34. end
  35. rx_i = good_stop;
  36. repeat (8) @(posedge clk);
  37. rx_i = 1'b1;
  38. repeat (4) @(posedge clk);
  39. end
  40. endtask
  41. initial begin
  42. valid_count = 0;
  43. framing_count = 0;
  44. repeat (3) @(posedge clk);
  45. rst_n = 1'b1;
  46. send_byte(8'hA5, 1'b1);
  47. send_byte(8'h5A, 1'b0);
  48. if (valid_count !== 1) $fatal(1, "invalid stop bit was published");
  49. if (framing_count !== 1) $fatal(1, "framing error was not reported");
  50. $display("uart_rx_byte_tb PASS");
  51. $finish;
  52. end
  53. endmodule