Forráskód Böngészése

test(uart): compare fixed and fractional host baud

root 3 hónapja
szülő
commit
6f60c600f4

+ 10 - 5
src/common/uart_rx_byte.v

@@ -11,6 +11,11 @@ module uart_rx_byte #(
     output reg        framing_error
 );
     localparam integer HALF_BIT = (CLKS_PER_BIT < 2) ? 1 : (CLKS_PER_BIT / 2);
+    localparam integer COUNTER_BITS = (CLKS_PER_BIT <= 2) ? 1 : $clog2(CLKS_PER_BIT);
+    /* verilator lint_off WIDTH */
+    localparam [COUNTER_BITS-1:0] BIT_LAST = CLKS_PER_BIT - 1;
+    localparam [COUNTER_BITS-1:0] HALF_LAST = HALF_BIT - 1;
+    /* verilator lint_on WIDTH */
     localparam [1:0] ST_IDLE = 2'd0;
     localparam [1:0] ST_START = 2'd1;
     localparam [1:0] ST_DATA = 2'd2;
@@ -18,7 +23,7 @@ module uart_rx_byte #(
 
     (* async_reg = "true" *) reg [1:0] rx_sync_pipe;
     reg [1:0] state;
-    reg [31:0] bit_counter;
+    reg [COUNTER_BITS-1:0] bit_counter;
     reg [2:0] bit_index;
     reg [7:0] data_reg;
 
@@ -26,7 +31,7 @@ module uart_rx_byte #(
         if (!rst_n) begin
             rx_sync_pipe <= 2'b11;
             state <= ST_IDLE;
-            bit_counter <= 32'd0;
+            bit_counter <= {COUNTER_BITS{1'b0}};
             bit_index <= 3'd0;
             data_reg <= 8'd0;
             byte_valid <= 1'b0;
@@ -40,7 +45,7 @@ module uart_rx_byte #(
             case (state)
                 ST_IDLE: begin
                     if (!rx_sync_pipe[1]) begin
-                        bit_counter <= HALF_BIT - 1;
+                        bit_counter <= HALF_LAST;
                         state <= ST_START;
                     end
                 end
@@ -49,7 +54,7 @@ module uart_rx_byte #(
                     if (bit_counter == 0) begin
                         if (!rx_sync_pipe[1]) begin
                             bit_index <= 3'd0;
-                            bit_counter <= CLKS_PER_BIT - 1;
+                            bit_counter <= BIT_LAST;
                             state <= ST_DATA;
                         end else begin
                             state <= ST_IDLE;
@@ -62,7 +67,7 @@ module uart_rx_byte #(
                 ST_DATA: begin
                     if (bit_counter == 0) begin
                         data_reg[bit_index] <= rx_sync_pipe[1];
-                        bit_counter <= CLKS_PER_BIT - 1;
+                        bit_counter <= BIT_LAST;
                         if (bit_index == 3'd7) begin
                             state <= ST_STOP;
                         end else begin

+ 84 - 0
src/common/uart_rx_byte_fractional.v

@@ -0,0 +1,84 @@
+`timescale 1ns/1ps
+
+// Candidate-B Host receiver. A phase accumulator schedules 27/28-clock bit
+// intervals for 25 MHz / 921600 without a PLL.
+module uart_rx_byte_fractional #(
+    parameter integer CLOCK_HZ = 25000000,
+    parameter integer BAUD = 921600
+) (
+    input  wire       clk,
+    input  wire       rst_n,
+    input  wire       rx_i,
+    output reg        byte_valid,
+    output reg [7:0]  byte_data,
+    output reg        framing_error
+);
+    localparam [1:0] ST_IDLE = 2'd0;
+    localparam [1:0] ST_START = 2'd1;
+    localparam [1:0] ST_DATA = 2'd2;
+    localparam [1:0] ST_STOP = 2'd3;
+
+    (* async_reg = "true" *) reg [1:0] rx_sync_pipe;
+    reg [1:0] state;
+    reg [31:0] phase_accumulator;
+    reg [2:0] bit_index;
+    reg [7:0] data_reg;
+    wire [32:0] phase_sum = {1'b0, phase_accumulator} + BAUD;
+    wire [32:0] clock_hz_extended = {1'b0, CLOCK_HZ[31:0]};
+    wire [31:0] phase_remainder = phase_sum[31:0] - CLOCK_HZ[31:0];
+    wire phase_tick = phase_sum >= clock_hz_extended;
+
+    always @(posedge clk or negedge rst_n) begin
+        if (!rst_n) begin
+            rx_sync_pipe <= 2'b11;
+            state <= ST_IDLE;
+            phase_accumulator <= 0;
+            bit_index <= 0;
+            data_reg <= 0;
+            byte_valid <= 0;
+            byte_data <= 0;
+            framing_error <= 0;
+        end else begin
+            rx_sync_pipe <= {rx_sync_pipe[0], rx_i};
+            byte_valid <= 0;
+            framing_error <= 0;
+            case (state)
+                ST_IDLE: begin
+                    phase_accumulator <= 0;
+                    if (!rx_sync_pipe[1]) begin
+                        phase_accumulator <= CLOCK_HZ / 2;
+                        state <= ST_START;
+                    end
+                end
+                ST_START: begin
+                    if (phase_tick) begin
+                        phase_accumulator <= phase_remainder;
+                        if (!rx_sync_pipe[1]) begin
+                            bit_index <= 0;
+                            state <= ST_DATA;
+                        end else state <= ST_IDLE;
+                    end else phase_accumulator <= phase_sum[31:0];
+                end
+                ST_DATA: begin
+                    if (phase_tick) begin
+                        phase_accumulator <= phase_remainder;
+                        data_reg[bit_index] <= rx_sync_pipe[1];
+                        if (bit_index == 7) state <= ST_STOP;
+                        else bit_index <= bit_index + 1'b1;
+                    end else phase_accumulator <= phase_sum[31:0];
+                end
+                ST_STOP: begin
+                    if (phase_tick) begin
+                        phase_accumulator <= phase_remainder;
+                        state <= ST_IDLE;
+                        if (rx_sync_pipe[1]) begin
+                            byte_data <= data_reg;
+                            byte_valid <= 1;
+                        end else framing_error <= 1;
+                    end else phase_accumulator <= phase_sum[31:0];
+                end
+                default: state <= ST_IDLE;
+            endcase
+        end
+    end
+endmodule

+ 8 - 4
src/common/uart_tx_byte.v

@@ -11,15 +11,19 @@ module uart_tx_byte #(
     output reg        busy,
     output reg        byte_done
 );
+    localparam integer COUNTER_BITS = (CLKS_PER_BIT <= 2) ? 1 : $clog2(CLKS_PER_BIT);
+    /* verilator lint_off WIDTH */
+    localparam [COUNTER_BITS-1:0] BIT_LAST = CLKS_PER_BIT - 1;
+    /* verilator lint_on WIDTH */
     reg [9:0] frame_bits;
     reg [3:0] bit_index;
-    reg [31:0] bit_counter;
+    reg [COUNTER_BITS-1:0] bit_counter;
 
     always @(posedge clk or negedge rst_n) begin
         if (!rst_n) begin
             frame_bits <= 10'b1111111111;
             bit_index <= 4'd0;
-            bit_counter <= 32'd0;
+            bit_counter <= {COUNTER_BITS{1'b0}};
             tx_o <= 1'b1;
             busy <= 1'b0;
             byte_done <= 1'b0;
@@ -30,7 +34,7 @@ module uart_tx_byte #(
                 if (byte_valid) begin
                     frame_bits <= {1'b1, byte_data, 1'b0};
                     bit_index <= 4'd0;
-                    bit_counter <= CLKS_PER_BIT - 1;
+                    bit_counter <= BIT_LAST;
                     tx_o <= 1'b0;
                     busy <= 1'b1;
                 end
@@ -41,7 +45,7 @@ module uart_tx_byte #(
                     byte_done <= 1'b1;
                 end else begin
                     bit_index <= bit_index + 1'b1;
-                    bit_counter <= CLKS_PER_BIT - 1;
+                    bit_counter <= BIT_LAST;
                     tx_o <= frame_bits[bit_index + 1'b1];
                 end
             end else begin

+ 57 - 0
src/common/uart_tx_byte_fractional.v

@@ -0,0 +1,57 @@
+`timescale 1ns/1ps
+
+// Candidate-B Host transmitter. A phase accumulator schedules 27/28-clock bit
+// intervals for 25 MHz / 921600 without a PLL.
+module uart_tx_byte_fractional #(
+    parameter integer CLOCK_HZ = 25000000,
+    parameter integer BAUD = 921600
+) (
+    input  wire       clk,
+    input  wire       rst_n,
+    input  wire       byte_valid,
+    input  wire [7:0] byte_data,
+    output reg        tx_o,
+    output reg        busy,
+    output reg        byte_done
+);
+    reg [9:0] frame_bits;
+    reg [3:0] bit_index;
+    reg [31:0] phase_accumulator;
+    wire [32:0] phase_sum = {1'b0, phase_accumulator} + BAUD;
+    wire [32:0] clock_hz_extended = {1'b0, CLOCK_HZ[31:0]};
+    wire [31:0] phase_remainder = phase_sum[31:0] - CLOCK_HZ[31:0];
+    wire phase_tick = phase_sum >= clock_hz_extended;
+
+    always @(posedge clk or negedge rst_n) begin
+        if (!rst_n) begin
+            frame_bits <= 10'h3ff;
+            bit_index <= 0;
+            phase_accumulator <= 0;
+            tx_o <= 1;
+            busy <= 0;
+            byte_done <= 0;
+        end else begin
+            byte_done <= 0;
+            if (!busy) begin
+                tx_o <= 1;
+                phase_accumulator <= 0;
+                if (byte_valid) begin
+                    frame_bits <= {1'b1, byte_data, 1'b0};
+                    bit_index <= 0;
+                    tx_o <= 0;
+                    busy <= 1;
+                end
+            end else if (phase_tick) begin
+                phase_accumulator <= phase_remainder;
+                if (bit_index == 9) begin
+                    tx_o <= 1;
+                    busy <= 0;
+                    byte_done <= 1;
+                end else begin
+                    bit_index <= bit_index + 1'b1;
+                    tx_o <= frame_bits[bit_index + 1'b1];
+                end
+            end else phase_accumulator <= phase_sum[31:0];
+        end
+    end
+endmodule

+ 22 - 0
tests/resource/uart_fixed_candidate_top.v

@@ -0,0 +1,22 @@
+module uart_fixed_candidate_top (
+    input wire clk,
+    input wire rst_n,
+    input wire rx,
+    input wire tx_valid,
+    input wire [7:0] tx_data,
+    output wire rx_valid,
+    output wire [7:0] rx_data,
+    output wire framing_error,
+    output wire tx,
+    output wire tx_busy,
+    output wire tx_done
+);
+    uart_rx_byte #(.CLKS_PER_BIT(27)) u_rx (
+        .clk(clk), .rst_n(rst_n), .rx_i(rx),
+        .byte_valid(rx_valid), .byte_data(rx_data), .framing_error(framing_error)
+    );
+    uart_tx_byte #(.CLKS_PER_BIT(27)) u_tx (
+        .clk(clk), .rst_n(rst_n), .byte_valid(tx_valid), .byte_data(tx_data),
+        .tx_o(tx), .busy(tx_busy), .byte_done(tx_done)
+    );
+endmodule

+ 22 - 0
tests/resource/uart_fractional_candidate_top.v

@@ -0,0 +1,22 @@
+module uart_fractional_candidate_top (
+    input wire clk,
+    input wire rst_n,
+    input wire rx,
+    input wire tx_valid,
+    input wire [7:0] tx_data,
+    output wire rx_valid,
+    output wire [7:0] rx_data,
+    output wire framing_error,
+    output wire tx,
+    output wire tx_busy,
+    output wire tx_done
+);
+    uart_rx_byte_fractional u_rx (
+        .clk(clk), .rst_n(rst_n), .rx_i(rx),
+        .byte_valid(rx_valid), .byte_data(rx_data), .framing_error(framing_error)
+    );
+    uart_tx_byte_fractional u_tx (
+        .clk(clk), .rst_n(rst_n), .byte_valid(tx_valid), .byte_data(tx_data),
+        .tx_o(tx), .busy(tx_busy), .byte_done(tx_done)
+    );
+endmodule

+ 221 - 0
tests/sim/uart_baud_candidates_tb.v

@@ -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

+ 12 - 0
tools/run_6400_v1.py

@@ -19,6 +19,12 @@ TESTBENCHES = (
     "bmc_ascii_snapshot_tb",
     "thermal_control_core_tb",
 )
+UART_CANDIDATE_SOURCES = (
+    "src/common/uart_rx_byte.v",
+    "src/common/uart_tx_byte.v",
+    "src/common/uart_rx_byte_fractional.v",
+    "src/common/uart_tx_byte_fractional.v",
+)
 
 
 def sources() -> list[str]:
@@ -61,6 +67,12 @@ def simulation() -> None:
             "iverilog", "-g2012", "-Wall", "-I.", "-s", "prod2u_6400_v1_top",
             "-o", str(temp / "top.vvp"), *sources(),
         ])
+        output = temp / "uart_baud_candidates_tb.vvp"
+        run([
+            "iverilog", "-g2012", "-Wall", "-I.", "-s", "uart_baud_candidates_tb",
+            "-o", str(output), *UART_CANDIDATE_SOURCES, "tests/sim/uart_baud_candidates_tb.v",
+        ])
+        run(["vvp", str(output)])
 
 
 def unit() -> None: