| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- `timescale 1ns/1ps
- module uart_rx_back_to_back_tb;
- localparam integer CLKS_PER_BIT = 8;
- reg clk;
- reg rst_n;
- reg rx_i;
- reg rx_ready;
- wire rx_valid;
- wire [7:0] rx_data;
- wire framing_error;
- wire break_detected;
- wire overflow;
- reg [7:0] captured [0:3];
- integer capture_count;
- uart_rx_8n1 #(
- .CLKS_PER_BIT(CLKS_PER_BIT),
- .BREAK_BITS(11)
- ) dut (
- .clk(clk),
- .rst_n(rst_n),
- .rx_i(rx_i),
- .rx_valid(rx_valid),
- .rx_data(rx_data),
- .rx_ready(rx_ready),
- .framing_error(framing_error),
- .break_detected(break_detected),
- .overflow(overflow)
- );
- initial begin
- clk = 1'b0;
- forever #5 clk = ~clk;
- end
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- capture_count <= 0;
- end else if (rx_valid) begin
- captured[capture_count] <= rx_data;
- capture_count <= capture_count + 1;
- end
- end
- task drive_uart_byte;
- input [7:0] value;
- integer bit_index;
- begin
- rx_i = 1'b0;
- repeat (CLKS_PER_BIT) @(negedge clk);
- for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
- rx_i = value[bit_index];
- repeat (CLKS_PER_BIT) @(negedge clk);
- end
- rx_i = 1'b1;
- repeat (CLKS_PER_BIT) @(negedge clk);
- end
- endtask
- initial begin
- rst_n = 1'b0;
- rx_i = 1'b1;
- rx_ready = 1'b1;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- drive_uart_byte(8'h33);
- drive_uart_byte(8'h34);
- drive_uart_byte(8'h20);
- drive_uart_byte(8'h0a);
- repeat (16) @(negedge clk);
- if (capture_count !== 4 || captured[0] !== 8'h33 || captured[1] !== 8'h34 ||
- captured[2] !== 8'h20 || captured[3] !== 8'h0a || framing_error || break_detected || overflow) begin
- $display(
- "UART RX back-to-back mismatch count=%0d bytes=%02x %02x %02x %02x framing=%0b break=%0b overflow=%0b",
- capture_count,
- captured[0],
- captured[1],
- captured[2],
- captured[3],
- framing_error,
- break_detected,
- overflow
- );
- $fatal;
- end
- $display("PASS: uart_rx_back_to_back_tb");
- $finish;
- end
- endmodule
|