`timescale 1ns/1ps module uart_rx_byte_tb; reg clk = 1'b0; reg rst_n = 1'b0; reg rx_i = 1'b1; wire byte_valid; wire [7:0] byte_data; wire framing_error; integer valid_count; integer framing_count; always #1 clk = ~clk; uart_rx_byte #(.CLKS_PER_BIT(8)) dut ( .clk(clk), .rst_n(rst_n), .rx_i(rx_i), .byte_valid(byte_valid), .byte_data(byte_data), .framing_error(framing_error) ); always @(posedge clk) begin if (byte_valid) begin valid_count = valid_count + 1; if (byte_data !== 8'hA5) $fatal(1, "RX data mismatch: %h", byte_data); end if (framing_error) framing_count = framing_count + 1; end task send_byte; input [7:0] value; input good_stop; integer bit_index; begin rx_i = 1'b0; repeat (8) @(posedge clk); for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin rx_i = value[bit_index]; repeat (8) @(posedge clk); end rx_i = good_stop; repeat (8) @(posedge clk); rx_i = 1'b1; repeat (4) @(posedge clk); end endtask initial begin valid_count = 0; framing_count = 0; repeat (3) @(posedge clk); rst_n = 1'b1; send_byte(8'hA5, 1'b1); send_byte(8'h5A, 1'b0); if (valid_count !== 1) $fatal(1, "invalid stop bit was published"); if (framing_count !== 1) $fatal(1, "framing error was not reported"); $display("uart_rx_byte_tb PASS"); $finish; end endmodule