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

feat(snapshot): low-LUT sequential snapshot mux with registered write datapath

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

+ 235 - 156
src/telemetry/mrdimm_snapshot_mux.v

@@ -1,179 +1,258 @@
 `timescale 1ns/1ps
 `include "generated/mrdimm/prod2u_mrdimm_params.vh"
-// Lock sensor banks and optimistically copy live axis state to stable RAM.
-// Publish only if the axis version stayed unchanged throughout the copy.
-// Retry releases source locks; neither copy nor retry stalls STEP execution.
+
+// 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.
 module mrdimm_snapshot_mux #(
-    parameter integer AXIS_COUNT=`MR_AXIS_COUNT,
-    parameter integer SCANNER_WORD_PORT=0,
-    parameter integer SNAPSHOT_LENGTH=`MR_SNAPSHOT_AXIS_OFFSET+AXIS_COUNT*`MR_AXIS_RECORD_BYTES
+    parameter integer AXIS_COUNT = `MR_AXIS_COUNT,
+    parameter integer SCANNER_WORD_PORT = 0,
+    parameter integer SNAPSHOT_LENGTH = `MR_SNAPSHOT_AXIS_OFFSET + AXIS_COUNT * `MR_AXIS_RECORD_BYTES
 ) (
-    input wire clk,rst_n,capture,source_ready,
-    input wire [15:0] source_epoch,
-    output reg ready,locked,bmc_locked_bank,ct_locked_bank,
-    input wire [31:0] fan_applied,ptc_applied,fan_requested,ptc_requested,
-    input wire watchdog_active,
-    input wire [7:0] watchdog_remaining,
-    input wire bmc_active_bank,ct_active_bank,bmc_stale,ct_stale,
-    input wire [15:0] bmc_epoch,ct_epoch,bmc_age,ct_age,
-    input wire [23:0] bmc_valid_mask,
-    input wire [26:0] ct_valid_mask,
-    output wire [4:0] bmc_rd_index,ct_rd_index,
-    input wire [15:0] bmc_rd_raw,ct_rd_raw,
-    input wire [AXIS_COUNT*`MR_AXIS_RECORD_BYTES*8-1:0] axis_data,
-    input wire [3:0] scanner_axis,
-    input wire [15:0] scanner_word_data,
-    output reg [4:0] scanner_word_addr,
-    input wire [31:0] time_us,time_high,event_overflow,event_pending_overflow,global_flags,
-    input wire [7:0] ev_count,
-    input wire [9:0] read_addr,
-    output wire [7:0] read_data
+    input  wire                                             clk,
+    input  wire                                             rst_n,
+    input  wire                                             capture,
+    input  wire                                             source_ready,
+    input  wire [15:0]                                      source_epoch,
+    output reg                                              ready,
+    output reg                                              locked,
+    output reg                                              bmc_locked_bank,
+    output reg                                              ct_locked_bank,
+    input  wire [31:0]                                      fan_applied,
+    input  wire [31:0]                                      ptc_applied,
+    input  wire [31:0]                                      fan_requested,
+    input  wire [31:0]                                      ptc_requested,
+    input  wire                                             watchdog_active,
+    input  wire [7:0]                                       watchdog_remaining,
+    input  wire                                             bmc_active_bank,
+    input  wire                                             ct_active_bank,
+    input  wire                                             bmc_stale,
+    input  wire                                             ct_stale,
+    input  wire [15:0]                                      bmc_epoch,
+    input  wire [15:0]                                      ct_epoch,
+    input  wire [15:0]                                      bmc_age,
+    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,
+    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,
+    input  wire [3:0]                                       scanner_axis,
+    input  wire [15:0]                                      scanner_word_data,
+    output reg  [4:0]                                       scanner_word_addr,
+    input  wire [31:0]                                      time_us,
+    input  wire [31:0]                                      time_high,
+    input  wire [31:0]                                      event_overflow,
+    input  wire [31:0]                                      event_pending_overflow,
+    input  wire [31:0]                                      global_flags,
+    input  wire [7:0]                                       ev_count,
+    input  wire [9:0]                                       read_addr,
+    output wire [7:0]                                       read_data
 );
+    localparam integer SNAPSHOT_ADDR_WIDTH = $clog2(SNAPSHOT_LENGTH / 2);
+
     reg [15:0] captured_epoch;
-    localparam integer SNAPSHOT_ADDR_WIDTH=$clog2(SNAPSHOT_LENGTH/2);
-    wire [15:0] memory_read_data;
-    reg read_in_range,read_high_byte;
-    reg [7:0] ct_previous_high;
-    reg [9:0] index;
-    reg [1:0] phase;
-    reg capture_pending;
-    reg [3:0] expected_axis;
-    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;
-    localparam integer CT_UNALIGNED=`MR_SNAPSHOT_CT_RAW_OFFSET%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;
-    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;
-    wire copying_axes=index>=`MR_SNAPSHOT_AXIS_OFFSET;
-    assign bmc_rd_index=in_bmc ? bmc_relative[5:1] : 5'd0;
-    assign ct_rd_index=in_ct ? ct_relative[5:1] : 5'd0;
+    reg        capture_pending;
+    reg [9:0]  index;
+    reg [1:0]  phase;
+    reg [3:0]  expected_axis;
+    reg [7:0]  ct_previous_high;
 
-    // Sequential shift registers eliminate the wide 381-bit parallel fanout
-    // and the 30-to-1 combinational multiplexer tree (~630 LUTs).
-    reg [239:0] header_sr;
-    reg [23:0]  mask_sr;
-    reg [7:0]   trailer_first_byte;
-    reg [191:0] trailer_sr;
+    // 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 scanner_match=scanner_axis==expected_axis;
-    wire axis_write=copying_axes && (!SCANNER_WORD_PORT || scanner_match);
-    wire memory_write=locked && (axis_write || (!copying_axes && phase==2));
+    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 [15:0] axis_copy_data;
-    generate
-        if(SCANNER_WORD_PORT!=0)begin:gen_scanner_port
-            assign axis_copy_data=scanner_word_data;
-        end else begin:gen_flat_axis
-            assign axis_copy_data=axis_data[{axis_word_index,4'b0000} +:16];
-        end
-    endgenerate
+    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;
 
-    reg [15:0] copy_word;
-    always @* begin
-        if(in_bmc)
-            copy_word = bmc_rd_raw;
-        else if(index == CT_WORD_START)
-            copy_word = {ct_rd_raw[7:0], mask_sr[23:16]};
-        else if(in_ct)
-            copy_word = {ct_rd_raw[7:0], ct_previous_high};
-        else if(index == CT_WORD_END)
-            copy_word = {trailer_first_byte, ct_previous_high};
-        else if(index < 30)
-            copy_word = header_sr[15:0];
-        else if(index == 78)
-            copy_word = mask_sr[15:0];
-        else
-            copy_word = trailer_sr[15:0];
-    end
+    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;
 
-    wire [SNAPSHOT_ADDR_WIDTH-1:0] scanner_write_address=`MR_SNAPSHOT_AXIS_OFFSET/2+expected_axis*32+scanner_word_addr;
-    wire [SNAPSHOT_ADDR_WIDTH-1:0] memory_write_address=(SCANNER_WORD_PORT && copying_axes) ? scanner_write_address : index[SNAPSHOT_ADDR_WIDTH:1];
-    wire [15:0] memory_write_data=copying_axes ? axis_copy_data : copy_word;
+    // Direct registered RAM write ports (eliminates all LUTs inside u_snapshot_memory)
+    reg                            mem_we;
+    reg [SNAPSHOT_ADDR_WIDTH-1:0]  mem_waddr;
+    reg [15:0]                     mem_wdata;
+    wire [15:0]                    memory_read_data;
+    reg                            read_in_range;
+    reg                            read_high_byte;
 
-    ef2_bram_dp #(.DATA_WIDTH(16),.ADDR_WIDTH(SNAPSHOT_ADDR_WIDTH),.DATA_DEPTH(SNAPSHOT_LENGTH/2)) u_snapshot_memory (
-        .clk(clk),.a_en(memory_write),.a_we(memory_write),
-        .a_addr(memory_write_address),.a_wdata(memory_write_data),.a_rdata(),
-        .b_en(1'b1),.b_we(1'b0),.b_addr(read_addr[SNAPSHOT_ADDR_WIDTH:1]),
-        .b_wdata(16'd0),.b_rdata(memory_read_data)
+    ef2_bram_dp #(
+        .DATA_WIDTH(16),
+        .ADDR_WIDTH(SNAPSHOT_ADDR_WIDTH),
+        .DATA_DEPTH(SNAPSHOT_LENGTH / 2)
+    ) u_snapshot_memory (
+        .clk(clk),
+        .a_en(mem_we),
+        .a_we(mem_we),
+        .a_addr(mem_waddr),
+        .a_wdata(mem_wdata),
+        .a_rdata(),
+        .b_en(1'b1),
+        .b_we(1'b0),
+        .b_addr(read_addr[SNAPSHOT_ADDR_WIDTH:1]),
+        .b_wdata(16'd0),
+        .b_rdata(memory_read_data)
     );
 
-    always @(posedge clk)begin
-        read_in_range<=read_addr<SNAPSHOT_LENGTH;read_high_byte<=read_addr[0];
+    always @(posedge clk) begin
+        read_in_range  <= read_addr < SNAPSHOT_LENGTH;
+        read_high_byte <= read_addr[0];
+    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
-    assign read_data=read_in_range ? (read_high_byte ? memory_read_data[15:8] : memory_read_data[7:0]) : 8'hFF;
 
     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;
-            header_sr<=0;mask_sr<=0;trailer_first_byte<=0;trailer_sr<=0;
-            ct_previous_high<=0;
+        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;
+            snap_time_us      <= 0;
+            snap_bmc_mask     <= 0;
+            snap_ct_mask      <= 0;
+            mem_we            <= 0;
+            mem_waddr         <= 0;
+            mem_wdata         <= 0;
         end else begin
-            ready<=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;
-                header_sr<={
-                    ct_age,
-                    bmc_age,
-                    ct_epoch,
-                    bmc_epoch,
-                    {7'd0, ct_stale, 7'd0, |ct_valid_mask},
-                    {7'd0, bmc_stale, 7'd0, |bmc_valid_mask},
-                    {watchdog_remaining, 7'd0, watchdog_active},
-                    ptc_requested[31:16],
-                    ptc_requested[15:0],
-                    fan_requested[31:16],
-                    fan_requested[15:0],
-                    ptc_applied[31:16],
-                    ptc_applied[15:0],
-                    fan_applied[31:16],
-                    fan_applied[15:0]
-                };
-                mask_sr<=bmc_valid_mask;
-                trailer_first_byte<=ct_valid_mask[7:0];
-                trailer_sr<={
-                    global_flags[31:16],
-                    global_flags[15:0],
-                    event_pending_overflow[31:16],
-                    event_pending_overflow[15:0],
-                    event_overflow[31:16],
-                    event_overflow[15:0],
-                    {ev_count, time_high[31:24]},
-                    time_high[23:8],
-                    {time_high[7:0], time_us[31:24]},
-                    time_us[23:8],
-                    {time_us[7:0], 5'd0, ct_valid_mask[26:24]},
-                    ct_valid_mask[23:8]
-                };
-            end else if(locked) begin
-                if(axis_write || (!copying_axes && phase==2)) begin
-                    phase<=0;
-                    if(!copying_axes) begin
-                        if(in_ct) ct_previous_high<=ct_rd_raw[15:8];
-                        if(index<28)
-                            header_sr<={16'd0, header_sr[239:16]};
-                        else if(index>=136 && index<158)
-                            trailer_sr<={16'd0, trailer_sr[191:16]};
+            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
+                    if (SCANNER_WORD_PORT != 0) begin
+                        if (scanner_axis == expected_axis) begin
+                            mem_we    <= 1;
+                            mem_waddr <= `MR_SNAPSHOT_AXIS_OFFSET / 2 + expected_axis * 32 + scanner_word_addr;
+                            mem_wdata <= scanner_word_data;
+
+                            if (expected_axis == AXIS_COUNT - 1 && scanner_word_addr == 5'd31) begin
+                                locked <= 0;
+                                if (source_ready && source_epoch == captured_epoch)
+                                    ready <= 1;
+                                else
+                                    capture_pending <= 1;
+                            end else if (expected_axis == AXIS_COUNT - 1) begin
+                                expected_axis     <= 0;
+                                scanner_word_addr <= scanner_word_addr + 1'b1;
+                            end else begin
+                                expected_axis <= expected_axis + 1'b1;
+                            end
+                        end
+                    end else begin
+                        // SCANNER_WORD_PORT == 0: 1 cycle per word from flat axis_data
+                        mem_we    <= 1;
+                        mem_waddr <= index[SNAPSHOT_ADDR_WIDTH:1];
+                        mem_wdata <= axis_data[{axis_word_index, 4'b0000} +: 16];
+
+                        if (index == SNAPSHOT_LENGTH - 2) begin
+                            locked <= 0;
+                            if (source_ready && source_epoch == captured_epoch)
+                                ready <= 1;
+                            else
+                                capture_pending <= 1;
+                        end else begin
+                            index <= index + 2;
+                        end
                     end
-                    if((SCANNER_WORD_PORT && copying_axes) ? (expected_axis==AXIS_COUNT-1 && scanner_word_addr==31) : index==SNAPSHOT_LENGTH-2) begin
-                        locked<=0;
-                        if(source_ready && source_epoch==captured_epoch)ready<=1;
-                        else capture_pending<=1;
+                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
-                    else if(SCANNER_WORD_PORT && copying_axes)begin
-                        if(expected_axis==AXIS_COUNT-1)begin expected_axis<=0;scanner_word_addr<=scanner_word_addr+1'b1;end
-                        else expected_axis<=expected_axis+1'b1;
-                    end else index<=index+2;
-                end else if(!copying_axes)phase<=phase+1'b1;
+                end
             end
         end
     end