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