|
@@ -0,0 +1,221 @@
|
|
|
|
|
+`timescale 1ns/1ps
|
|
|
|
|
+
|
|
|
|
|
+module uart_baud_candidates_tb;
|
|
|
|
|
+ reg clk = 0;
|
|
|
|
|
+ always #20 clk = ~clk;
|
|
|
|
|
+
|
|
|
|
|
+ reg rst_n = 0;
|
|
|
|
|
+ reg rx_fixed = 1;
|
|
|
|
|
+ reg rx_fractional = 1;
|
|
|
|
|
+ wire valid_fixed;
|
|
|
|
|
+ wire [7:0] data_fixed;
|
|
|
|
|
+ wire error_fixed;
|
|
|
|
|
+ wire valid_fractional;
|
|
|
|
|
+ wire [7:0] data_fractional;
|
|
|
|
|
+ wire error_fractional;
|
|
|
|
|
+
|
|
|
|
|
+ reg tx_fixed_valid = 0;
|
|
|
|
|
+ reg [7:0] tx_fixed_data = 0;
|
|
|
|
|
+ wire tx_fixed_line;
|
|
|
|
|
+ wire tx_fixed_busy;
|
|
|
|
|
+ wire tx_fixed_done;
|
|
|
|
|
+ reg tx_fractional_valid = 0;
|
|
|
|
|
+ reg [7:0] tx_fractional_data = 0;
|
|
|
|
|
+ wire tx_fractional_line;
|
|
|
|
|
+ wire tx_fractional_busy;
|
|
|
|
|
+ wire tx_fractional_done;
|
|
|
|
|
+ wire loop_fixed_valid;
|
|
|
|
|
+ wire [7:0] loop_fixed_data;
|
|
|
|
|
+ wire loop_fractional_valid;
|
|
|
|
|
+ wire [7:0] loop_fractional_data;
|
|
|
|
|
+
|
|
|
|
|
+ reg [7:0] expected_fixed [0:255];
|
|
|
|
|
+ reg [7:0] expected_fractional [0:255];
|
|
|
|
|
+ reg [7:0] expected_loop_fixed [0:31];
|
|
|
|
|
+ reg [7:0] expected_loop_fractional [0:31];
|
|
|
|
|
+ integer expected_fixed_count = 0;
|
|
|
|
|
+ integer expected_fractional_count = 0;
|
|
|
|
|
+ integer expected_loop_fixed_count = 0;
|
|
|
|
|
+ integer expected_loop_fractional_count = 0;
|
|
|
|
|
+ integer got_fixed_count = 0;
|
|
|
|
|
+ integer got_fractional_count = 0;
|
|
|
|
|
+ integer got_loop_fixed_count = 0;
|
|
|
|
|
+ integer got_loop_fractional_count = 0;
|
|
|
|
|
+ integer fixed_errors = 0;
|
|
|
|
|
+ integer fractional_errors = 0;
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ integer cycles;
|
|
|
|
|
+
|
|
|
|
|
+ uart_rx_byte #(.CLKS_PER_BIT(27)) u_fixed_rx (
|
|
|
|
|
+ .clk(clk), .rst_n(rst_n), .rx_i(rx_fixed),
|
|
|
|
|
+ .byte_valid(valid_fixed), .byte_data(data_fixed), .framing_error(error_fixed)
|
|
|
|
|
+ );
|
|
|
|
|
+ uart_rx_byte_fractional u_fractional_rx (
|
|
|
|
|
+ .clk(clk), .rst_n(rst_n), .rx_i(rx_fractional),
|
|
|
|
|
+ .byte_valid(valid_fractional), .byte_data(data_fractional), .framing_error(error_fractional)
|
|
|
|
|
+ );
|
|
|
|
|
+ uart_tx_byte #(.CLKS_PER_BIT(27)) u_fixed_tx (
|
|
|
|
|
+ .clk(clk), .rst_n(rst_n), .byte_valid(tx_fixed_valid), .byte_data(tx_fixed_data),
|
|
|
|
|
+ .tx_o(tx_fixed_line), .busy(tx_fixed_busy), .byte_done(tx_fixed_done)
|
|
|
|
|
+ );
|
|
|
|
|
+ uart_tx_byte_fractional u_fractional_tx (
|
|
|
|
|
+ .clk(clk), .rst_n(rst_n), .byte_valid(tx_fractional_valid), .byte_data(tx_fractional_data),
|
|
|
|
|
+ .tx_o(tx_fractional_line), .busy(tx_fractional_busy), .byte_done(tx_fractional_done)
|
|
|
|
|
+ );
|
|
|
|
|
+ uart_rx_byte #(.CLKS_PER_BIT(27)) u_fixed_loop_rx (
|
|
|
|
|
+ .clk(clk), .rst_n(rst_n), .rx_i(tx_fixed_line),
|
|
|
|
|
+ .byte_valid(loop_fixed_valid), .byte_data(loop_fixed_data), .framing_error()
|
|
|
|
|
+ );
|
|
|
|
|
+ uart_rx_byte_fractional u_fractional_loop_rx (
|
|
|
|
|
+ .clk(clk), .rst_n(rst_n), .rx_i(tx_fractional_line),
|
|
|
|
|
+ .byte_valid(loop_fractional_valid), .byte_data(loop_fractional_data), .framing_error()
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ always @(posedge clk) begin
|
|
|
|
|
+ if (valid_fixed) begin
|
|
|
|
|
+ if (got_fixed_count >= expected_fixed_count || data_fixed !== expected_fixed[got_fixed_count]) begin
|
|
|
|
|
+ $display("fixed RX mismatch index=%0d data=%02x", got_fixed_count, data_fixed);
|
|
|
|
|
+ $fatal(1);
|
|
|
|
|
+ end
|
|
|
|
|
+ got_fixed_count = got_fixed_count + 1;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (valid_fractional) begin
|
|
|
|
|
+ if (got_fractional_count >= expected_fractional_count || data_fractional !== expected_fractional[got_fractional_count]) begin
|
|
|
|
|
+ $display("fractional RX mismatch index=%0d data=%02x", got_fractional_count, data_fractional);
|
|
|
|
|
+ $fatal(1);
|
|
|
|
|
+ end
|
|
|
|
|
+ got_fractional_count = got_fractional_count + 1;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (error_fixed) fixed_errors = fixed_errors + 1;
|
|
|
|
|
+ if (error_fractional) fractional_errors = fractional_errors + 1;
|
|
|
|
|
+ if (loop_fixed_valid) begin
|
|
|
|
|
+ if (loop_fixed_data !== expected_loop_fixed[got_loop_fixed_count]) $fatal(1, "fixed TX loopback mismatch");
|
|
|
|
|
+ got_loop_fixed_count = got_loop_fixed_count + 1;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (loop_fractional_valid) begin
|
|
|
|
|
+ if (loop_fractional_data !== expected_loop_fractional[got_loop_fractional_count]) $fatal(1, "fractional TX loopback mismatch");
|
|
|
|
|
+ got_loop_fractional_count = got_loop_fractional_count + 1;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ task hold_clocks;
|
|
|
|
|
+ input integer count;
|
|
|
|
|
+ integer n;
|
|
|
|
|
+ begin
|
|
|
|
|
+ for (n = 0; n < count; n = n + 1) @(posedge clk);
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task send_fixed_peer;
|
|
|
|
|
+ input [7:0] value;
|
|
|
|
|
+ input integer nominal;
|
|
|
|
|
+ input integer jitter;
|
|
|
|
|
+ input integer tag;
|
|
|
|
|
+ integer bit_number;
|
|
|
|
|
+ integer period;
|
|
|
|
|
+ begin
|
|
|
|
|
+ expected_fixed[expected_fixed_count] = value;
|
|
|
|
|
+ expected_fixed_count = expected_fixed_count + 1;
|
|
|
|
|
+ rx_fixed = 0;
|
|
|
|
|
+ period = nominal + ((((tag + 0) & 1) != 0) ? jitter : -jitter);
|
|
|
|
|
+ hold_clocks(period);
|
|
|
|
|
+ for (bit_number = 0; bit_number < 8; bit_number = bit_number + 1) begin
|
|
|
|
|
+ rx_fixed = value[bit_number];
|
|
|
|
|
+ period = nominal + ((((tag + bit_number + 1) & 1) != 0) ? jitter : -jitter);
|
|
|
|
|
+ hold_clocks(period);
|
|
|
|
|
+ end
|
|
|
|
|
+ rx_fixed = 1;
|
|
|
|
|
+ period = nominal + ((((tag + 9) & 1) != 0) ? jitter : -jitter);
|
|
|
|
|
+ hold_clocks(period);
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task send_fractional_peer;
|
|
|
|
|
+ input [7:0] value;
|
|
|
|
|
+ input integer nominal;
|
|
|
|
|
+ input integer jitter;
|
|
|
|
|
+ input integer tag;
|
|
|
|
|
+ integer bit_number;
|
|
|
|
|
+ integer period;
|
|
|
|
|
+ begin
|
|
|
|
|
+ expected_fractional[expected_fractional_count] = value;
|
|
|
|
|
+ expected_fractional_count = expected_fractional_count + 1;
|
|
|
|
|
+ rx_fractional = 0;
|
|
|
|
|
+ period = nominal + ((((tag + 0) & 1) != 0) ? jitter : -jitter);
|
|
|
|
|
+ hold_clocks(period);
|
|
|
|
|
+ for (bit_number = 0; bit_number < 8; bit_number = bit_number + 1) begin
|
|
|
|
|
+ rx_fractional = value[bit_number];
|
|
|
|
|
+ period = nominal + ((((tag + bit_number + 1) & 1) != 0) ? jitter : -jitter);
|
|
|
|
|
+ hold_clocks(period);
|
|
|
|
|
+ end
|
|
|
|
|
+ rx_fractional = 1;
|
|
|
|
|
+ period = nominal + ((((tag + 9) & 1) != 0) ? jitter : -jitter);
|
|
|
|
|
+ hold_clocks(period);
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task transmit_fixed;
|
|
|
|
|
+ input [7:0] value;
|
|
|
|
|
+ begin
|
|
|
|
|
+ expected_loop_fixed[expected_loop_fixed_count] = value;
|
|
|
|
|
+ expected_loop_fixed_count = expected_loop_fixed_count + 1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ tx_fixed_data = value;
|
|
|
|
|
+ tx_fixed_valid = 1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ tx_fixed_valid = 0;
|
|
|
|
|
+ cycles = 0;
|
|
|
|
|
+ while (tx_fixed_busy) begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ cycles = cycles + 1;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (cycles != 270) $fatal(1, "fixed TX duration %0d, expected 270", cycles);
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task transmit_fractional;
|
|
|
|
|
+ input [7:0] value;
|
|
|
|
|
+ begin
|
|
|
|
|
+ expected_loop_fractional[expected_loop_fractional_count] = value;
|
|
|
|
|
+ expected_loop_fractional_count = expected_loop_fractional_count + 1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ tx_fractional_data = value;
|
|
|
|
|
+ tx_fractional_valid = 1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ tx_fractional_valid = 0;
|
|
|
|
|
+ cycles = 0;
|
|
|
|
|
+ while (tx_fractional_busy) begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ cycles = cycles + 1;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (cycles != 272) $fatal(1, "fractional TX duration %0d, expected 272", cycles);
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ initial begin
|
|
|
|
|
+ repeat (8) @(posedge clk);
|
|
|
|
|
+ rst_n = 1;
|
|
|
|
|
+ #7;
|
|
|
|
|
+ for (i = 0; i < 16; i = i + 1) send_fixed_peer(8'h20 + i, 26, 0, i);
|
|
|
|
|
+ for (i = 0; i < 16; i = i + 1) send_fixed_peer(8'h40 + i, 28, 0, i);
|
|
|
|
|
+ for (i = 0; i < 64; i = i + 1) send_fixed_peer((i * 37) ^ 8'ha5, 27, 1, i);
|
|
|
|
|
+ for (i = 0; i < 16; i = i + 1) send_fractional_peer(8'h60 + i, 26, 0, i);
|
|
|
|
|
+ for (i = 0; i < 16; i = i + 1) send_fractional_peer(8'h80 + i, 28, 0, i);
|
|
|
|
|
+ for (i = 0; i < 64; i = i + 1) send_fractional_peer((i * 53) ^ 8'h5a, 27, 1, i);
|
|
|
|
|
+ hold_clocks(20);
|
|
|
|
|
+ if (got_fixed_count != expected_fixed_count || got_fractional_count != expected_fractional_count) begin
|
|
|
|
|
+ $fatal(1, "RX count mismatch fixed=%0d/%0d frac=%0d/%0d", got_fixed_count, expected_fixed_count, got_fractional_count, expected_fractional_count);
|
|
|
|
|
+ end
|
|
|
|
|
+ if (fixed_errors != 0 || fractional_errors != 0) $fatal(1, "unexpected framing errors");
|
|
|
|
|
+ for (i = 0; i < 16; i = i + 1) begin
|
|
|
|
|
+ transmit_fixed(8'h55 ^ i);
|
|
|
|
|
+ transmit_fractional(8'haa ^ i);
|
|
|
|
|
+ end
|
|
|
|
|
+ hold_clocks(20);
|
|
|
|
|
+ if (got_loop_fixed_count != expected_loop_fixed_count || got_loop_fractional_count != expected_loop_fractional_count) begin
|
|
|
|
|
+ $fatal(1, "TX loopback count mismatch");
|
|
|
|
|
+ end
|
|
|
|
|
+ $display("PASS: uart_baud_candidates_tb fixed=27 fractional=27/28 peer=26..28 jitter=+/-1 long=64");
|
|
|
|
|
+ $finish;
|
|
|
|
|
+ end
|
|
|
|
|
+endmodule
|