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

fix(events): freeze a bounded read copy and preserve exact record fields

codex 2 месяцев назад
Родитель
Сommit
a83dbf29f7

+ 3 - 0
src/motor/motor_engine.v

@@ -56,6 +56,8 @@ module motor_engine #(
     output wire [127:0] ev_rd_data,
     output wire [7:0]  ev_count,
     output wire [7:0]  ev_overflow,
+    output wire ev_view_ready,
+    output wire [7:0] ev_view_count,
     output wire [31:0] ev_time_us,
     input  wire        ev_clear_n,
     input  wire [7:0]  ev_clear_count,
@@ -291,6 +293,7 @@ module motor_engine #(
         .wr_tag(ev_tag), .wr_position(ev_pos), .wr_total(ev_total), .time_us(time_us),
         .rd_index(ev_rd_index), .rd_hold(ev_hold), .rd_data(ev_rd_data), .record_count(ev_count),
         .overflow_lsb(ev_overflow),
+        .view_ready(ev_view_ready), .view_count(ev_view_count), .overflow_count(),
         .clear_n(ev_clear_n), .clear_count(ev_clear_count)
     );
 

+ 55 - 66
src/motor/motor_event_ring.v

@@ -1,77 +1,66 @@
 `timescale 1ns/1ps
-
-// Event ring for the MRDIMM motor engine (contract 10.6).
-//
-// 64 records x 16 bytes in BRAM.  When full, the oldest record is
-// overwritten and the overflow counter increments.  Reads are by index
-// from the oldest record and never delete; a read snapshot is stable
-// while no new write lands (the caller freezes the view).
-//
-// Record layout (16 bytes, little endian):
-//   [3:0]  timestamp_us_low32   [4] axis (0=X,1=Y,2=Z,FF=global)
-//   [5]    event code           [7:6] command_tag
-//   [11:8] position s32         [15:12] total_steps
-module motor_event_ring #(
-    parameter integer DEPTH = 64
-) (
-    input  wire        clk,
-    input  wire        rst_n,
-    // write side (request/ack so same-cycle multi-axis events serialize
-    // without stalling the STEP datapath)
-    input  wire        wr_req,
-    output reg         wr_ack,
-    input  wire [3:0]  wr_axis,
-    input  wire [7:0]  wr_event,
-    input  wire [15:0] wr_tag,
-    input  wire [31:0] wr_position,
-    input  wire [31:0] wr_total,
-    input  wire [31:0] time_us,
-    // read side (index 0 = oldest)
-    input  wire [7:0]  rd_index,     // 0..63, >= count reads as garbage-free 0
-    input  wire        rd_hold,      // freeze the view: writes wait outside
+// 64 x 16-byte records plus an eight-record host view. Capture pauses the
+// writer for at most ten clocks, never for the duration of a Host transfer.
+module motor_event_ring #(parameter integer DEPTH=64) (
+    input wire clk,rst_n,wr_req,
+    output reg wr_ack,
+    input wire [3:0] wr_axis,
+    input wire [7:0] wr_event,
+    input wire [15:0] wr_tag,
+    input wire [31:0] wr_position,wr_total,time_us,
+    input wire [7:0] rd_index,
+    input wire rd_hold,
     output reg [127:0] rd_data,
-    output reg  [7:0]  record_count,
-    output reg  [7:0]  overflow_lsb,
-    // clear
-    input  wire        clear_n,      // 1 = clear N oldest, FF = clear all
-    input  wire [7:0]  clear_count
+    output wire [7:0] record_count,overflow_lsb,
+    output wire [31:0] overflow_count,
+    output reg view_ready,
+    output reg [7:0] view_count,
+    input wire clear_n,
+    input wire [7:0] clear_count
 );
+    localparam integer AW=$clog2(DEPTH);
     reg [127:0] mem [0:DEPTH-1];
-    reg [5:0]  head;        // next write slot
-    reg [7:0]  count;
+    reg [127:0] view_mem [0:7];
+    reg [AW-1:0] head,view_base;
+    reg [7:0] count;
     reg [31:0] overflow;
-
-    wire [5:0] rd_addr = head - count[5:0] + rd_index[5:0];
-    wire overflowing = (count == DEPTH[7:0]);
-
+    reg hold_q,copy_active;
+    reg [3:0] copy_index;
+    wire freeze_start=rd_hold && !hold_q;
+    wire write_fire=wr_req && !wr_ack && !freeze_start && !copy_active;
+    wire [7:0] after_clear=!clear_n ? count :
+        ((clear_count==8'hFF || clear_count>=count) ? 8'd0 : count-clear_count);
+    wire [AW-1:0] read_address=head-count[AW-1:0]+rd_index[AW-1:0];
+    wire [AW-1:0] copy_address=view_base+copy_index;
+    wire [7:0] axis_byte=(wr_axis==4'hF) ? 8'hFF : {4'd0,wr_axis};
+    assign record_count=count;
+    assign overflow_lsb=overflow[7:0];
+    assign overflow_count=overflow;
+    always @(posedge clk) begin
+        if(write_fire) mem[head]<={wr_total,wr_position,wr_tag,wr_event,axis_byte,time_us};
+        if(copy_active && copy_index<view_count) view_mem[copy_index[2:0]]<=mem[copy_address];
+    end
     always @(posedge clk or negedge rst_n) begin
-        if (!rst_n) begin
-            head <= 6'd0; count <= 8'd0; overflow <= 32'd0;
-            wr_ack <= 1'b0; rd_data <= 128'd0; record_count <= 8'd0;
-            overflow_lsb <= 8'd0;
+        if(!rst_n) begin
+            head<=0;count<=0;overflow<=0;wr_ack<=0;rd_data<=0;
+            hold_q<=0;copy_active<=0;copy_index<=0;view_base<=0;
+            view_ready<=0;view_count<=0;
         end else begin
-            wr_ack <= 1'b0;
-            if (wr_req && !wr_ack && !rd_hold) begin
-                // rd_hold defers writes so a frozen read window cannot shift
-                // underneath the host; the writer simply waits (STEP is not
-                // on this path).
-                mem[head] <= {wr_total, wr_position, 16'd0, wr_tag, 8'd0, wr_event, wr_axis, time_us};
-                head <= head + 6'd1;
-                if (overflowing) overflow <= overflow + 32'd1;
-                else count <= count + 8'd1;
-                wr_ack <= 1'b1;
-            end
-            if (clear_n) begin
-                if (clear_count == 8'hFF) begin
-                    count <= 8'd0;
-                    head <= 6'd0;
-                end else begin
-                    count <= (clear_count >= count) ? 8'd0 : count - clear_count;
-                end
+            hold_q<=rd_hold;wr_ack<=0;count<=after_clear;
+            if(write_fire) begin
+                head<=head+1'b1;wr_ack<=1;
+                if(after_clear==DEPTH) overflow<=overflow+1'b1;
+                else count<=after_clear+1'b1;
             end
-            record_count <= count;
-            overflow_lsb <= overflow[7:0];
-            rd_data <= (rd_index < count) ? mem[rd_addr] : 128'd0;
+            if(freeze_start) begin
+                view_base<=head-count[AW-1:0];view_count<=count>8 ? 8'd8 : count;
+                copy_index<=0;copy_active<=1;view_ready<=0;
+            end else if(copy_active) begin
+                if(copy_index>=view_count) begin copy_active<=0;view_ready<=1;end
+                else copy_index<=copy_index+1'b1;
+            end else if(!rd_hold) view_ready<=0;
+            if(rd_hold) rd_data<=(view_ready && rd_index<view_count) ? view_mem[rd_index[2:0]] : 128'd0;
+            else rd_data<=rd_index<count ? mem[read_address] : 128'd0;
         end
     end
 endmodule

+ 5 - 0
src/top/prod2u_mrdimm_top.v

@@ -138,6 +138,8 @@ module prod2u_mrdimm_top (
     wire [7:0] ev_rd_index, ev_count, ev_overflow, ev_clear_count;
     wire ev_hold, ev_clear_n;
     wire [127:0] ev_rd_data;
+    wire ev_view_ready;
+    wire [7:0] ev_view_count;
     wire [7:0] status_byte_in;
     wire tx_start, tx_done, frame_tx_busy, frame_tx_byte_valid;
     wire [7:0] tx_frame_type, tx_payload_data, frame_tx_byte_data;
@@ -164,6 +166,7 @@ module prod2u_mrdimm_top (
         .ctl_accept(ctl_accept), .ctl_status(ctl_status), .ctl_detail(ctl_detail),
         .ev_rd_index(ev_rd_index), .ev_hold(ev_hold),
         .ev_rd_data(ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
+        .ev_view_ready(ev_view_ready), .ev_view_count(ev_view_count),
         .ev_clear_n(ev_clear_n), .ev_clear_count(ev_clear_count),
         .status_byte_in(status_byte_in),
         .tx_payload_addr(frame_tx_payload_addr),
@@ -213,6 +216,7 @@ module prod2u_mrdimm_top (
         .startup_seek(boot_seek),
         .ev_rd_index(ev_rd_index), .ev_hold(ev_hold),
         .ev_rd_data(ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
+        .ev_view_ready(ev_view_ready), .ev_view_count(ev_view_count),
         .ev_time_us(ev_time_us),
         .ev_clear_n(ev_clear_n), .ev_clear_count(ev_clear_count),
         .axis_telemetry(axis_telemetry),
@@ -372,6 +376,7 @@ module prod2u_mrdimm_top (
         .ctl_status(i2c_ctl_status), .ctl_detail(i2c_ctl_detail),
         .ev_rd_index(i2c_ev_rd_index), .ev_hold(i2c_ev_hold),
         .ev_rd_data(i2c_ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
+        .ev_view_ready(ev_view_ready), .ev_view_count(ev_view_count),
         .ev_clear_n(i2c_ev_clear_n), .ev_clear_count(i2c_ev_clear_count),
         .status_byte_in(status_byte_in), .cmd_tx_payload_data()
     );

+ 3 - 0
src/transport/host_i2c_register_bank.v

@@ -86,6 +86,8 @@ module host_i2c_register_bank #(
     input  wire [127:0] ev_rd_data,
     input  wire [7:0]  ev_count,
     input  wire [7:0]  ev_overflow,
+    input wire ev_view_ready,
+    input wire [7:0] ev_view_count,
     output wire        ev_clear_n,
     output wire [7:0]  ev_clear_count,
     input  wire [7:0]  status_byte_in,
@@ -176,6 +178,7 @@ module host_i2c_register_bank #(
         .ctl_accept(ctl_accept), .ctl_status(ctl_status), .ctl_detail(ctl_detail),
         .ev_rd_index(ev_rd_index), .ev_hold(ev_hold),
         .ev_rd_data(ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
+        .ev_view_ready(ev_view_ready), .ev_view_count(ev_view_count),
         .ev_clear_n(ev_clear_n), .ev_clear_count(ev_clear_count),
         .status_byte_in(status_byte_in),
         .tx_payload_addr(i2c_tx_payload_addr),

+ 9 - 4
src/transport/mrdimm_host_command.v

@@ -60,6 +60,8 @@ module mrdimm_host_command #(
     input  wire [127:0] ev_rd_data,
     input  wire [7:0]  ev_count,
     input  wire [7:0]  ev_overflow,
+    input wire ev_view_ready,
+    input wire [7:0] ev_view_count,
     output reg        ev_clear_n,
     output reg [7:0]  ev_clear_count,
     // ALL_STATUS payload: byte window from the snapshot mux, addressed by
@@ -86,7 +88,7 @@ module mrdimm_host_command #(
         ST_QW_STREAM = 5'd6, ST_QW_SUBMIT = 5'd7, ST_QW_WAIT = 5'd8,
         ST_CTL_SEND = 5'd9, ST_CTL_WAIT = 5'd10, ST_EVCLEAR_SEND = 5'd11,
         ST_EV_FREEZE = 5'd12, ST_SNAP_WAIT = 5'd13, ST_ACK_TX = 5'd14,
-        ST_DATA_TX = 5'd15, ST_DROP = 5'd16, ST_EP_WAIT = 5'd17;
+        ST_DATA_TX = 5'd15, ST_DROP = 5'd16, ST_EP_WAIT = 5'd17, ST_EV_WAIT=5'd18;
 
     reg [4:0] state;
     reg [5:0] index;
@@ -136,7 +138,7 @@ module mrdimm_host_command #(
     // Ring view freeze: while the EVENT payload streams, the engine holds
     // the ring so the frozen record window cannot shift under the reader.
     always @* begin
-        ev_hold = event_valid && tx_start_or_running;
+        ev_hold = event_valid;
         // record index for the byte at tx_payload_addr (payload byte 0 is
         // the count byte); the ring registers rd_data one cycle later,
         // matching frame_tx PAYLOAD_READ_LATENCY=1.
@@ -422,15 +424,18 @@ module mrdimm_host_command #(
                     end else begin
                         ev_max_records <= payload_read_data;
                         // freeze: min(requested, available), saturated at 8
-                        ev_freeze_count <= (ev_count < payload_read_data) ? ev_count : payload_read_data;
                         event_valid <= 1'b1; status_valid <= 1'b0;
-                        start_ack(1'b1, T_EVREAD, S_OK, DET_NONE);
+                        state<=ST_EV_WAIT;
                     end
                 end
 
                 ST_SNAP_WAIT: if (snapshot_ready) begin
                     start_ack(1'b1, T_GET_ALL, S_OK, DET_NONE);
                 end
+                ST_EV_WAIT: if(ev_view_ready) begin
+                    ev_freeze_count<=(ev_view_count<ev_max_records) ? ev_view_count : ev_max_records;
+                    start_ack(1'b1,T_EVREAD,S_OK,DET_NONE);
+                end
 
                 // ---- response phases ----
                 ST_ACK_TX: if (tx_done) begin

+ 7 - 3
tests/sim/prod2u_mrdimm_uart_e2e_tb.v

@@ -35,13 +35,15 @@ module prod2u_mrdimm_uart_e2e_tb;
     wire [15:0] ctl_axis_mask;
     wire [7:0] ctl_flags;
     wire [31:0] ctl_value;
-    wire [7:0] ev_rd_index_w = 8'd0;
+    wire [7:0] ev_rd_index_w;
     wire ev_hold;
     wire [127:0] ev_rd_data;
+    wire ev_view_ready;
+    wire [7:0] ev_view_count;
     wire [7:0] ev_count, ev_overflow;
     wire [31:0] ev_time_us;
-    wire ev_clear_n_w = 1'b0;
-    wire [7:0] ev_clear_count_w = 8'd0;
+    wire ev_clear_n_w;
+    wire [7:0] ev_clear_count_w;
     wire [7:0] status_byte_in;
     wire tx_start, tx_done;
     wire [7:0] tx_frame_type, tx_payload_data;
@@ -124,6 +126,7 @@ module prod2u_mrdimm_uart_e2e_tb;
         .ctl_accept(ctl_accept), .ctl_status(ctl_status), .ctl_detail(ctl_detail),
         .ev_rd_index(ev_rd_index_w), .ev_hold(ev_hold),
         .ev_rd_data(ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
+        .ev_view_ready(ev_view_ready), .ev_view_count(ev_view_count),
         .ev_clear_n(ev_clear_n_w), .ev_clear_count(ev_clear_count_w),
         .status_byte_in(status_byte_in),
         .tx_payload_addr(u_frame_tx.payload_addr),
@@ -169,6 +172,7 @@ module prod2u_mrdimm_uart_e2e_tb;
         .startup_seek(1'b0),
         .ev_rd_index(ev_rd_index_w), .ev_hold(ev_hold),
         .ev_rd_data(ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
+        .ev_view_ready(ev_view_ready), .ev_view_count(ev_view_count),
         .ev_time_us(unused_time),
         .ev_clear_n(ev_clear_n_w), .ev_clear_count(ev_clear_count_w),
         .axis_telemetry(axis_telemetry),