| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- `timescale 1ns/1ps
- module uart_8n1_phy_tb;
- reg clk;
- reg rst_n;
- reg tx_valid;
- reg [7:0] tx_data;
- wire tx_ready;
- wire tx_line;
- wire rx_valid;
- wire [7:0] rx_data;
- wire rx_framing_error;
- wire rx_break_detected;
- wire rx_overflow;
- reg rx_ready;
- reg tx_min_valid;
- reg [7:0] tx_min_data;
- wire tx_min_ready;
- wire tx_min_line;
- wire tx_min_busy;
- reg tx_np_valid;
- reg [7:0] tx_np_data;
- wire tx_np_ready;
- wire tx_np_line;
- wire tx_np_busy;
- wire rx_np_valid;
- wire [7:0] rx_np_data;
- wire rx_np_framing_error;
- wire rx_np_break_detected;
- wire rx_np_overflow;
- reg rx_np_ready;
- integer min_busy_cycles;
- uart_tx_8n1 #(
- .CLKS_PER_BIT(4)
- ) u_tx (
- .clk(clk),
- .rst_n(rst_n),
- .tx_valid(tx_valid),
- .tx_data(tx_data),
- .tx_ready(tx_ready),
- .tx_o(tx_line),
- .busy()
- );
- uart_rx_8n1 #(
- .CLKS_PER_BIT(4)
- ) u_rx (
- .clk(clk),
- .rst_n(rst_n),
- .rx_i(tx_line),
- .rx_valid(rx_valid),
- .rx_data(rx_data),
- .rx_ready(rx_ready),
- .framing_error(rx_framing_error),
- .break_detected(rx_break_detected),
- .overflow(rx_overflow)
- );
- uart_tx_8n1 #(
- .CLKS_PER_BIT(1)
- ) u_tx_min (
- .clk(clk),
- .rst_n(rst_n),
- .tx_valid(tx_min_valid),
- .tx_data(tx_min_data),
- .tx_ready(tx_min_ready),
- .tx_o(tx_min_line),
- .busy(tx_min_busy)
- );
- uart_tx_8n1 #(
- .CLKS_PER_BIT(3)
- ) u_tx_np (
- .clk(clk),
- .rst_n(rst_n),
- .tx_valid(tx_np_valid),
- .tx_data(tx_np_data),
- .tx_ready(tx_np_ready),
- .tx_o(tx_np_line),
- .busy(tx_np_busy)
- );
- uart_rx_8n1 #(
- .CLKS_PER_BIT(3)
- ) u_rx_np (
- .clk(clk),
- .rst_n(rst_n),
- .rx_i(tx_np_line),
- .rx_valid(rx_np_valid),
- .rx_data(rx_np_data),
- .rx_ready(rx_np_ready),
- .framing_error(rx_np_framing_error),
- .break_detected(rx_np_break_detected),
- .overflow(rx_np_overflow)
- );
- initial begin
- clk = 1'b0;
- forever #5 clk = ~clk;
- end
- task send_byte;
- input [7:0] value;
- begin
- while (!tx_ready) @(negedge clk);
- tx_data = value;
- tx_valid = 1'b1;
- @(negedge clk);
- tx_valid = 1'b0;
- end
- endtask
- task send_np_byte;
- input [7:0] value;
- begin
- while (!tx_np_ready) @(negedge clk);
- tx_np_data = value;
- tx_np_valid = 1'b1;
- @(negedge clk);
- tx_np_valid = 1'b0;
- end
- endtask
- initial begin
- rst_n = 1'b0;
- tx_valid = 1'b0;
- tx_data = 8'd0;
- rx_ready = 1'b1;
- tx_min_valid = 1'b0;
- tx_min_data = 8'd0;
- tx_np_valid = 1'b0;
- tx_np_data = 8'd0;
- rx_np_ready = 1'b1;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- if ($bits(u_tx_min.clk_count) != 1 ||
- $bits(u_tx_np.clk_count) != 2 ||
- $bits(u_rx_np.clk_count) != 2) begin
- $display(
- "UART clock counter widths mismatch tx1=%0d tx3=%0d rx3=%0d",
- $bits(u_tx_min.clk_count),
- $bits(u_tx_np.clk_count),
- $bits(u_rx_np.clk_count)
- );
- $fatal;
- end
- send_byte(8'ha5);
- wait (rx_valid === 1'b1);
- if (rx_data !== 8'ha5 || rx_framing_error || rx_break_detected || rx_overflow) begin
- $display("UART 8N1 loopback mismatch data=%02x framing=%0b break=%0b overflow=%0b",
- rx_data, rx_framing_error, rx_break_detected, rx_overflow);
- $fatal;
- end
- @(negedge clk);
- send_np_byte(8'h5a);
- wait (rx_np_valid === 1'b1);
- if (rx_np_data !== 8'h5a || rx_np_framing_error ||
- rx_np_break_detected || rx_np_overflow) begin
- $display(
- "UART non-power-of-two loopback mismatch data=%02x framing=%0b break=%0b overflow=%0b",
- rx_np_data,
- rx_np_framing_error,
- rx_np_break_detected,
- rx_np_overflow
- );
- $fatal;
- end
- @(negedge clk);
- while (!tx_min_ready) @(negedge clk);
- tx_min_data = 8'h96;
- tx_min_valid = 1'b1;
- @(posedge clk);
- #1;
- @(negedge clk);
- tx_min_valid = 1'b0;
- min_busy_cycles = 0;
- while (tx_min_busy && min_busy_cycles < 12) begin
- @(posedge clk);
- #1;
- min_busy_cycles = min_busy_cycles + 1;
- end
- if (min_busy_cycles !== 10 || tx_min_busy || !tx_min_ready || tx_min_line !== 1'b1) begin
- $display(
- "UART one-clock TX mismatch cycles=%0d busy=%0b ready=%0b line=%0b",
- min_busy_cycles,
- tx_min_busy,
- tx_min_ready,
- tx_min_line
- );
- $fatal;
- end
- $display("PASS: uart_8n1_phy_tb");
- $finish;
- end
- endmodule
|