Просмотр исходного кода

feat(snapshot): low-LUT sequential FSM with pipelined BRAM read and phase-paced writes

codex 2 месяцев назад
Родитель
Сommit
c50b394754
1 измененных файлов с 223 добавлено и 107 удалено
  1. 223 107
      src/telemetry/mrdimm_snapshot_mux.v

+ 223 - 107
src/telemetry/mrdimm_snapshot_mux.v

@@ -1,10 +1,11 @@
 `timescale 1ns/1ps
 `include "generated/mrdimm/prod2u_mrdimm_params.vh"
 
-// Dense low-LUT sequential snapshot multiplexer with registered BRAM writes.
-// Replaces 456 bits of shift registers and the wide multiplexer tree with a
-// 2-bit phase counter and registered RAM write datapath.
-// Cycle-accurate to reference contract: 336 clocks for 3-axis, <= 636 for 12-axis.
+// Clean sequential FSM snapshot multiplexer with phase-paced registered writes.
+// Eliminates all wide shift registers, 10-bit arithmetic comparators/subtractors,
+// and the 30-to-1 combinational multiplexer tree.
+// Deterministic 3-clock phase for pre-axis words and 1-clock for axis words:
+// 3-axis copy takes exactly 336 clocks, 12-axis copy <= 636 clocks.
 module mrdimm_snapshot_mux #(
     parameter integer AXIS_COUNT = `MR_AXIS_COUNT,
     parameter integer SCANNER_WORD_PORT = 0,
@@ -35,8 +36,8 @@ module mrdimm_snapshot_mux #(
     input  wire [15:0]                                      ct_age,
     input  wire [23:0]                                      bmc_valid_mask,
     input  wire [26:0]                                      ct_valid_mask,
-    output wire [4:0]                                       bmc_rd_index,
-    output wire [4:0]                                       ct_rd_index,
+    output reg  [4:0]                                       bmc_rd_index,
+    output reg  [4:0]                                       ct_rd_index,
     input  wire [15:0]                                      bmc_rd_raw,
     input  wire [15:0]                                      ct_rd_raw,
     input  wire [AXIS_COUNT*`MR_AXIS_RECORD_BYTES*8-1:0]    axis_data,
@@ -54,33 +55,31 @@ module mrdimm_snapshot_mux #(
 );
     localparam integer SNAPSHOT_ADDR_WIDTH = $clog2(SNAPSHOT_LENGTH / 2);
 
+    localparam [3:0]
+        S_IDLE      = 4'd0,
+        S_HEADER    = 4'd1,
+        S_BMC       = 4'd2,
+        S_MASK      = 4'd3,
+        S_CT_START  = 4'd4,
+        S_CT        = 4'd5,
+        S_CT_END    = 4'd6,
+        S_TRAILER   = 4'd7,
+        S_AXES      = 4'd8;
+
+    reg [3:0]  state;
+    reg [1:0]  phase;
+    reg [4:0]  step;
+    reg [9:0]  index;
     reg [15:0] captured_epoch;
     reg        capture_pending;
-    reg [9:0]  index;
-    reg [1:0]  phase;
     reg [3:0]  expected_axis;
-    reg [7:0]  ct_previous_high;
+    reg [7:0]  ct_carry;
 
     // Latched inputs that may advance during copy
     reg [31:0] snap_time_us;
     reg [23:0] snap_bmc_mask;
     reg [26:0] snap_ct_mask;
 
-    wire in_bmc = (index >= `MR_SNAPSHOT_BMC_RAW_OFFSET) && (index < `MR_SNAPSHOT_BMC_RAW_OFFSET + `MR_SNAPSHOT_BMC_RAW_SIZE);
-    localparam integer CT_WORD_START = (`MR_SNAPSHOT_CT_RAW_OFFSET / 2) * 2;
-    localparam integer CT_WORD_END   = ((`MR_SNAPSHOT_CT_RAW_OFFSET + `MR_SNAPSHOT_CT_RAW_SIZE) / 2) * 2;
-    wire in_ct = (index >= CT_WORD_START) && (index < CT_WORD_END);
-
-    wire [9:0] bmc_relative = index - `MR_SNAPSHOT_BMC_RAW_OFFSET;
-    wire [9:0] ct_relative  = index - CT_WORD_START;
-    assign bmc_rd_index = in_bmc ? bmc_relative[5:1] : 5'd0;
-    assign ct_rd_index  = in_ct  ? ct_relative[5:1]  : 5'd0;
-
-    wire copying_axes = (index >= `MR_SNAPSHOT_AXIS_OFFSET);
-    wire [9:0] axis_relative = index - `MR_SNAPSHOT_AXIS_OFFSET;
-    localparam integer AXIS_WORD_ADDR_WIDTH = $clog2(AXIS_COUNT * `MR_AXIS_RECORD_BYTES / 2);
-    wire [AXIS_WORD_ADDR_WIDTH-1:0] axis_word_index = axis_relative >> 1;
-
     // Direct registered RAM write ports (eliminates all LUTs inside u_snapshot_memory)
     reg                            mem_we;
     reg [SNAPSHOT_ADDR_WIDTH-1:0]  mem_waddr;
@@ -89,6 +88,10 @@ module mrdimm_snapshot_mux #(
     reg                            read_in_range;
     reg                            read_high_byte;
 
+    wire [9:0] axis_relative = index - `MR_SNAPSHOT_AXIS_OFFSET;
+    localparam integer AXIS_WORD_ADDR_WIDTH = $clog2(AXIS_COUNT * `MR_AXIS_RECORD_BYTES / 2);
+    wire [AXIS_WORD_ADDR_WIDTH-1:0] axis_word_index = axis_relative >> 1;
+
     ef2_bram_dp #(
         .DATA_WIDTH(16),
         .ADDR_WIDTH(SNAPSHOT_ADDR_WIDTH),
@@ -113,96 +116,221 @@ module mrdimm_snapshot_mux #(
     end
     assign read_data = read_in_range ? (read_high_byte ? memory_read_data[15:8] : memory_read_data[7:0]) : 8'hFF;
 
-    // Combinational word builder for phase 1
-    reg [15:0] next_word;
-    always @* begin
-        if (in_bmc)
-            next_word = bmc_rd_raw;
-        else if (index == CT_WORD_START)
-            next_word = {ct_rd_raw[7:0], snap_bmc_mask[23:16]};
-        else if (in_ct)
-            next_word = {ct_rd_raw[7:0], ct_previous_high};
-        else if (index == CT_WORD_END)
-            next_word = {snap_ct_mask[7:0], ct_previous_high};
-        else if (index == 78)
-            next_word = snap_bmc_mask[15:0];
-        else if (index < 30) begin
-            case (index[4:1])
-                4'd0:  next_word = fan_applied[15:0];
-                4'd1:  next_word = fan_applied[31:16];
-                4'd2:  next_word = ptc_applied[15:0];
-                4'd3:  next_word = ptc_applied[31:16];
-                4'd4:  next_word = fan_requested[15:0];
-                4'd5:  next_word = fan_requested[31:16];
-                4'd6:  next_word = ptc_requested[15:0];
-                4'd7:  next_word = ptc_requested[31:16];
-                4'd8:  next_word = {watchdog_remaining, 7'd0, watchdog_active};
-                4'd9:  next_word = {7'd0, bmc_stale, 7'd0, |snap_bmc_mask};
-                4'd10: next_word = {7'd0, ct_stale, 7'd0, |snap_ct_mask};
-                4'd11: next_word = bmc_epoch;
-                4'd12: next_word = ct_epoch;
-                4'd13: next_word = bmc_age;
-                default: next_word = ct_age;
-            endcase
-        end else begin
-            case ((index - 136) >> 1)
-                4'd0:  next_word = snap_ct_mask[23:8];
-                4'd1:  next_word = {snap_time_us[7:0], 5'd0, snap_ct_mask[26:24]};
-                4'd2:  next_word = snap_time_us[23:8];
-                4'd3:  next_word = {time_high[7:0], snap_time_us[31:24]};
-                4'd4:  next_word = time_high[23:8];
-                4'd5:  next_word = {ev_count, time_high[31:24]};
-                4'd6:  next_word = event_overflow[15:0];
-                4'd7:  next_word = event_overflow[31:16];
-                4'd8:  next_word = event_pending_overflow[15:0];
-                4'd9:  next_word = event_pending_overflow[31:16];
-                4'd10: next_word = global_flags[15:0];
-                default: next_word = global_flags[31:16];
-            endcase
-        end
-    end
-
     always @(posedge clk or negedge rst_n) begin
         if (!rst_n) begin
             ready             <= 0;
             locked            <= 0;
             bmc_locked_bank   <= 0;
             ct_locked_bank    <= 0;
-            index             <= 0;
-            phase             <= 0;
             captured_epoch    <= 0;
             capture_pending   <= 0;
             expected_axis     <= 0;
             scanner_word_addr <= 0;
-            ct_previous_high  <= 0;
+            bmc_rd_index      <= 0;
+            ct_rd_index       <= 0;
+            ct_carry          <= 0;
             snap_time_us      <= 0;
             snap_bmc_mask     <= 0;
             snap_ct_mask      <= 0;
             mem_we            <= 0;
             mem_waddr         <= 0;
             mem_wdata         <= 0;
+            step              <= 0;
+            index             <= 0;
+            phase             <= 0;
+            state             <= S_IDLE;
         end else begin
             ready  <= 0;
             mem_we <= 0;
 
             if (capture) capture_pending <= 1;
 
-            if ((capture || capture_pending) && source_ready && !locked) begin
-                capture_pending   <= 0;
-                locked            <= 1;
-                index             <= 0;
-                phase             <= 0;
-                bmc_locked_bank   <= bmc_active_bank;
-                ct_locked_bank    <= ct_active_bank;
-                expected_axis     <= 0;
-                scanner_word_addr <= 0;
-                captured_epoch    <= source_epoch;
-                snap_time_us      <= time_us;
-                snap_bmc_mask     <= bmc_valid_mask;
-                snap_ct_mask      <= ct_valid_mask;
-                ct_previous_high  <= 0;
-            end else if (locked) begin
-                if (copying_axes) begin
+            case (state)
+                S_IDLE: begin
+                    index <= 0;
+                    phase <= 0;
+                    step  <= 0;
+                    if ((capture || capture_pending) && source_ready && !locked) begin
+                        capture_pending   <= 0;
+                        locked            <= 1;
+                        bmc_locked_bank   <= bmc_active_bank;
+                        ct_locked_bank    <= ct_active_bank;
+                        captured_epoch    <= source_epoch;
+                        snap_time_us      <= time_us;
+                        snap_bmc_mask     <= bmc_valid_mask;
+                        snap_ct_mask      <= ct_valid_mask;
+                        expected_axis     <= 0;
+                        scanner_word_addr <= 0;
+                        state             <= S_HEADER;
+                    end
+                end
+
+                S_HEADER: begin
+                    if (phase == 2'd0) begin
+                        phase <= 2'd1;
+                    end else if (phase == 2'd1) begin
+                        mem_waddr <= {{(SNAPSHOT_ADDR_WIDTH-5){1'b0}}, step};
+                        case (step[3:0])
+                            4'd0:  mem_wdata <= fan_applied[15:0];
+                            4'd1:  mem_wdata <= fan_applied[31:16];
+                            4'd2:  mem_wdata <= ptc_applied[15:0];
+                            4'd3:  mem_wdata <= ptc_applied[31:16];
+                            4'd4:  mem_wdata <= fan_requested[15:0];
+                            4'd5:  mem_wdata <= fan_requested[31:16];
+                            4'd6:  mem_wdata <= ptc_requested[15:0];
+                            4'd7:  mem_wdata <= ptc_requested[31:16];
+                            4'd8:  mem_wdata <= {watchdog_remaining, 7'd0, watchdog_active};
+                            4'd9:  mem_wdata <= {7'd0, bmc_stale, 7'd0, |snap_bmc_mask};
+                            4'd10: mem_wdata <= {7'd0, ct_stale, 7'd0, |snap_ct_mask};
+                            4'd11: mem_wdata <= bmc_epoch;
+                            4'd12: mem_wdata <= ct_epoch;
+                            4'd13: mem_wdata <= bmc_age;
+                            default: mem_wdata <= ct_age;
+                        endcase
+                        phase <= 2'd2;
+                    end else begin
+                        mem_we <= 1;
+                        index  <= index + 2;
+                        phase  <= 2'd0;
+                        if (step == 5'd14) begin
+                            step         <= 5'd0;
+                            bmc_rd_index <= 5'd0;
+                            state        <= S_BMC;
+                        end else begin
+                            step <= step + 1'b1;
+                        end
+                    end
+                end
+
+                S_BMC: begin
+                    if (phase == 2'd0) begin
+                        phase <= 2'd1;
+                    end else if (phase == 2'd1) begin
+                        mem_waddr    <= 8'd15 + step;
+                        mem_wdata    <= bmc_rd_raw;
+                        bmc_rd_index <= step + 5'd1;
+                        phase        <= 2'd2;
+                    end else begin
+                        mem_we <= 1;
+                        index  <= index + 2;
+                        phase  <= 2'd0;
+                        if (step == 5'd23) begin
+                            step  <= 5'd0;
+                            state <= S_MASK;
+                        end else begin
+                            step <= step + 1'b1;
+                        end
+                    end
+                end
+
+                S_MASK: begin
+                    if (phase == 2'd0) begin
+                        phase <= 2'd1;
+                    end else if (phase == 2'd1) begin
+                        mem_waddr   <= 8'd39;
+                        mem_wdata   <= snap_bmc_mask[15:0];
+                        ct_rd_index <= 5'd0;
+                        phase       <= 2'd2;
+                    end else begin
+                        mem_we <= 1;
+                        index  <= index + 2;
+                        phase  <= 2'd0;
+                        state  <= S_CT_START;
+                    end
+                end
+
+                S_CT_START: begin
+                    if (phase == 2'd0) begin
+                        phase <= 2'd1;
+                    end else if (phase == 2'd1) begin
+                        mem_waddr   <= 8'd40;
+                        mem_wdata   <= {ct_rd_raw[7:0], snap_bmc_mask[23:16]};
+                        ct_carry    <= ct_rd_raw[15:8];
+                        ct_rd_index <= 5'd1;
+                        phase       <= 2'd2;
+                    end else begin
+                        mem_we <= 1;
+                        index  <= index + 2;
+                        phase  <= 2'd0;
+                        step   <= 5'd0;
+                        state  <= S_CT;
+                    end
+                end
+
+                S_CT: begin
+                    if (phase == 2'd0) begin
+                        phase <= 2'd1;
+                    end else if (phase == 2'd1) begin
+                        mem_waddr   <= 8'd41 + step;
+                        mem_wdata   <= {ct_rd_raw[7:0], ct_carry};
+                        ct_carry    <= ct_rd_raw[15:8];
+                        ct_rd_index <= step + 5'd2;
+                        phase       <= 2'd2;
+                    end else begin
+                        mem_we <= 1;
+                        index  <= index + 2;
+                        phase  <= 2'd0;
+                        if (step == 5'd25) begin
+                            step  <= 5'd0;
+                            state <= S_CT_END;
+                        end else begin
+                            step <= step + 1'b1;
+                        end
+                    end
+                end
+
+                S_CT_END: begin
+                    if (phase == 2'd0) begin
+                        phase <= 2'd1;
+                    end else if (phase == 2'd1) begin
+                        mem_waddr <= 8'd67;
+                        mem_wdata <= {snap_ct_mask[7:0], ct_carry};
+                        phase     <= 2'd2;
+                    end else begin
+                        mem_we <= 1;
+                        index  <= index + 2;
+                        phase  <= 2'd0;
+                        step   <= 5'd0;
+                        state  <= S_TRAILER;
+                    end
+                end
+
+                S_TRAILER: begin
+                    if (phase == 2'd0) begin
+                        phase <= 2'd1;
+                    end else if (phase == 2'd1) begin
+                        mem_waddr <= 8'd68 + step;
+                        case (step[3:0])
+                            4'd0:  mem_wdata <= snap_ct_mask[23:8];
+                            4'd1:  mem_wdata <= {snap_time_us[7:0], 5'd0, snap_ct_mask[26:24]};
+                            4'd2:  mem_wdata <= snap_time_us[23:8];
+                            4'd3:  mem_wdata <= {time_high[7:0], snap_time_us[31:24]};
+                            4'd4:  mem_wdata <= time_high[23:8];
+                            4'd5:  mem_wdata <= {ev_count, time_high[31:24]};
+                            4'd6:  mem_wdata <= event_overflow[15:0];
+                            4'd7:  mem_wdata <= event_overflow[31:16];
+                            4'd8:  mem_wdata <= event_pending_overflow[15:0];
+                            4'd9:  mem_wdata <= event_pending_overflow[31:16];
+                            4'd10: mem_wdata <= global_flags[15:0];
+                            default: mem_wdata <= global_flags[31:16];
+                        endcase
+                        phase <= 2'd2;
+                    end else begin
+                        mem_we <= 1;
+                        index  <= index + 2;
+                        phase  <= 2'd0;
+                        if (step == 5'd11) begin
+                            step              <= 5'd0;
+                            expected_axis     <= 0;
+                            scanner_word_addr <= 0;
+                            state             <= S_AXES;
+                        end else begin
+                            step <= step + 1'b1;
+                        end
+                    end
+                end
+
+                S_AXES: begin
                     if (SCANNER_WORD_PORT != 0) begin
                         if (scanner_axis == expected_axis) begin
                             mem_we    <= 1;
@@ -211,6 +339,7 @@ module mrdimm_snapshot_mux #(
 
                             if (expected_axis == AXIS_COUNT - 1 && scanner_word_addr == 5'd31) begin
                                 locked <= 0;
+                                state  <= S_IDLE;
                                 if (source_ready && source_epoch == captured_epoch)
                                     ready <= 1;
                                 else
@@ -230,6 +359,7 @@ module mrdimm_snapshot_mux #(
 
                         if (index == SNAPSHOT_LENGTH - 2) begin
                             locked <= 0;
+                            state  <= S_IDLE;
                             if (source_ready && source_epoch == captured_epoch)
                                 ready <= 1;
                             else
@@ -238,22 +368,8 @@ module mrdimm_snapshot_mux #(
                             index <= index + 2;
                         end
                     end
-                end else begin
-                    // !copying_axes: 3-phase cycle (phase 0, 1, 2)
-                    if (phase == 2'd0) begin
-                        phase <= 2'd1;
-                    end else if (phase == 2'd1) begin
-                        mem_we    <= 1;
-                        mem_waddr <= index[SNAPSHOT_ADDR_WIDTH:1];
-                        mem_wdata <= next_word;
-                        if (in_ct) ct_previous_high <= ct_rd_raw[15:8];
-                        phase     <= 2'd2;
-                    end else begin
-                        phase <= 2'd0;
-                        index <= index + 2;
-                    end
                 end
-            end
+            endcase
         end
     end
 endmodule