|
@@ -1,6 +1,7 @@
|
|
|
`timescale 1ns/1ps
|
|
`timescale 1ns/1ps
|
|
|
-// 64 x 16-byte records plus an eight-record host view. Capture pauses the
|
|
|
|
|
-// writer for at most eighteen clocks, never for the duration of a Host transfer.
|
|
|
|
|
|
|
+// 64 x 16-byte records in a 512 x 16-bit BRAM. A record commits only after
|
|
|
|
|
+// all eight words are written. A frozen host view copies at most eight
|
|
|
|
|
+// records in 128 read clocks; only this bounded copy pauses the writer.
|
|
|
module motor_event_ring #(parameter integer DEPTH=64) (
|
|
module motor_event_ring #(parameter integer DEPTH=64) (
|
|
|
input wire clk,rst_n,wr_req,
|
|
input wire clk,rst_n,wr_req,
|
|
|
output reg wr_ack,
|
|
output reg wr_ack,
|
|
@@ -20,59 +21,80 @@ module motor_event_ring #(parameter integer DEPTH=64) (
|
|
|
);
|
|
);
|
|
|
localparam integer AW=$clog2(DEPTH);
|
|
localparam integer AW=$clog2(DEPTH);
|
|
|
reg [127:0] view_mem [0:7];
|
|
reg [127:0] view_mem [0:7];
|
|
|
- reg [AW-1:0] head,view_base;
|
|
|
|
|
- reg [7:0] count;
|
|
|
|
|
|
|
+ reg [AW-1:0] head,view_base,live_base;
|
|
|
|
|
+ reg [7:0] count,live_requested_index,live_published_index;
|
|
|
reg [31:0] overflow;
|
|
reg [31:0] overflow;
|
|
|
- reg hold_q,copy_active;
|
|
|
|
|
|
|
+ reg hold_q,freeze_pending,copy_active,read_phase,write_active;
|
|
|
reg [3:0] copy_index;
|
|
reg [3:0] copy_index;
|
|
|
- reg copy_phase;
|
|
|
|
|
- wire [127:0] memory_data;
|
|
|
|
|
|
|
+ reg [2:0] read_word,write_word;
|
|
|
|
|
+ reg [127:0] write_shift,read_shift,live_data;
|
|
|
|
|
+ wire [15:0] memory_data;
|
|
|
wire freeze_start=rd_hold && !hold_q;
|
|
wire freeze_start=rd_hold && !hold_q;
|
|
|
- wire write_fire=wr_req && !wr_ack && !freeze_start && !copy_active;
|
|
|
|
|
|
|
+ wire write_start=wr_req && !wr_ack && !write_active && !freeze_start && !freeze_pending && !copy_active;
|
|
|
wire [7:0] after_clear=!clear_n ? count :
|
|
wire [7:0] after_clear=!clear_n ? count :
|
|
|
((clear_count==8'hFF || clear_count>=count) ? 8'd0 : count-clear_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 [AW-1:0] memory_address=copy_active ? copy_address : read_address;
|
|
|
|
|
|
|
+ wire [AW-1:0] memory_record=copy_active ? view_base+copy_index : live_base;
|
|
|
wire [7:0] axis_byte=(wr_axis==4'hF) ? 8'hFF : {4'd0,wr_axis};
|
|
wire [7:0] axis_byte=(wr_axis==4'hF) ? 8'hFF : {4'd0,wr_axis};
|
|
|
- wire [127:0] write_data={wr_total,wr_position,wr_tag,wr_event,axis_byte,time_us};
|
|
|
|
|
- genvar word_index;
|
|
|
|
|
- generate for(word_index=0;word_index<4;word_index=word_index+1) begin:g_record_word
|
|
|
|
|
- ef2_bram_dp #(.DATA_WIDTH(32),.ADDR_WIDTH(AW),.DATA_DEPTH(DEPTH)) u_memory (
|
|
|
|
|
- .clk(clk),.a_en(write_fire),.a_we(write_fire),.a_addr(head),
|
|
|
|
|
- .a_wdata(write_data[word_index*32+:32]),.a_rdata(),
|
|
|
|
|
- .b_en(1'b1),.b_we(1'b0),.b_addr(memory_address),.b_wdata(32'd0),
|
|
|
|
|
- .b_rdata(memory_data[word_index*32+:32])
|
|
|
|
|
- );
|
|
|
|
|
- end endgenerate
|
|
|
|
|
|
|
+ wire [127:0] assembled={memory_data,read_shift[127:16]};
|
|
|
|
|
+ ef2_bram_dp #(.DATA_WIDTH(16),.ADDR_WIDTH(AW+3),.DATA_DEPTH(DEPTH*8)) u_memory (
|
|
|
|
|
+ .clk(clk),.a_en(write_active),.a_we(write_active),.a_addr({head,write_word}),
|
|
|
|
|
+ .a_wdata(write_shift[15:0]),.a_rdata(),.b_en(1'b1),.b_we(1'b0),
|
|
|
|
|
+ .b_addr({memory_record,read_word}),.b_wdata(16'd0),.b_rdata(memory_data)
|
|
|
|
|
+ );
|
|
|
assign record_count=count;
|
|
assign record_count=count;
|
|
|
assign overflow_lsb=overflow[7:0];
|
|
assign overflow_lsb=overflow[7:0];
|
|
|
assign overflow_count=overflow;
|
|
assign overflow_count=overflow;
|
|
|
always @(posedge clk) begin
|
|
always @(posedge clk) begin
|
|
|
- if(copy_active && copy_phase && copy_index<view_count) view_mem[copy_index[2:0]]<=memory_data;
|
|
|
|
|
|
|
+ if(copy_active && read_phase && read_word==7 && copy_index<view_count)
|
|
|
|
|
+ view_mem[copy_index[2:0]]<=assembled;
|
|
|
end
|
|
end
|
|
|
always @(posedge clk or negedge rst_n) begin
|
|
always @(posedge clk or negedge rst_n) begin
|
|
|
if(!rst_n) begin
|
|
if(!rst_n) begin
|
|
|
head<=0;count<=0;overflow<=0;wr_ack<=0;rd_data<=0;
|
|
head<=0;count<=0;overflow<=0;wr_ack<=0;rd_data<=0;
|
|
|
- hold_q<=0;copy_active<=0;copy_index<=0;view_base<=0;copy_phase<=0;
|
|
|
|
|
- view_ready<=0;view_count<=0;
|
|
|
|
|
|
|
+ hold_q<=0;freeze_pending<=0;copy_active<=0;copy_index<=0;view_base<=0;
|
|
|
|
|
+ view_ready<=0;view_count<=0;read_phase<=0;read_word<=0;
|
|
|
|
|
+ write_active<=0;write_word<=0;write_shift<=0;read_shift<=0;
|
|
|
|
|
+ live_base<=0;live_requested_index<=0;live_published_index<=0;live_data<=0;
|
|
|
end else begin
|
|
end else begin
|
|
|
hold_q<=rd_hold;wr_ack<=0;count<=after_clear;
|
|
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;
|
|
|
|
|
|
|
+ if(freeze_start)freeze_pending<=1;
|
|
|
|
|
+ if(write_start)begin
|
|
|
|
|
+ write_shift<={wr_total,wr_position,wr_tag,wr_event,axis_byte,time_us};
|
|
|
|
|
+ write_word<=0;write_active<=1;
|
|
|
|
|
+ end else if(write_active)begin
|
|
|
|
|
+ write_shift<=write_shift>>16;
|
|
|
|
|
+ if(write_word==7)begin
|
|
|
|
|
+ write_active<=0;head<=head+1'b1;wr_ack<=1;
|
|
|
|
|
+ if(after_clear==DEPTH)overflow<=overflow+1'b1;
|
|
|
|
|
+ else count<=after_clear+1'b1;
|
|
|
|
|
+ end else write_word<=write_word+1'b1;
|
|
|
end
|
|
end
|
|
|
- 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;copy_phase<=0;
|
|
|
|
|
- end else if(copy_active) begin
|
|
|
|
|
- if(copy_index>=view_count) begin copy_active<=0;view_ready<=1;end
|
|
|
|
|
- else if(copy_phase) begin copy_index<=copy_index+1'b1;copy_phase<=0;end
|
|
|
|
|
- else copy_phase<=1;
|
|
|
|
|
- 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 ? memory_data : 128'd0;
|
|
|
|
|
|
|
+ // Finish any in-flight write before selecting the oldest record:
|
|
|
|
|
+ // a full ring must never expose a partially overwritten record.
|
|
|
|
|
+ if((freeze_pending || freeze_start) && !write_active)begin
|
|
|
|
|
+ freeze_pending<=0;view_base<=head-count[AW-1:0];
|
|
|
|
|
+ view_count<=count>8 ? 8'd8 : count;view_ready<=count==0;
|
|
|
|
|
+ copy_active<=count!=0;copy_index<=0;read_word<=0;read_phase<=0;
|
|
|
|
|
+ end else if(!read_phase)read_phase<=1;
|
|
|
|
|
+ else begin
|
|
|
|
|
+ read_phase<=0;read_shift<=assembled;
|
|
|
|
|
+ if(read_word==7)begin
|
|
|
|
|
+ read_word<=0;
|
|
|
|
|
+ if(copy_active)begin
|
|
|
|
|
+ if(copy_index+1>=view_count)begin copy_active<=0;view_ready<=1;end
|
|
|
|
|
+ else copy_index<=copy_index+1'b1;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ live_data<=assembled;live_published_index<=live_requested_index;
|
|
|
|
|
+ live_requested_index<=rd_index;
|
|
|
|
|
+ live_base<=head-count[AW-1:0]+rd_index[AW-1:0];
|
|
|
|
|
+ end
|
|
|
|
|
+ end else read_word<=read_word+1'b1;
|
|
|
|
|
+ end
|
|
|
|
|
+ if(!rd_hold)view_ready<=0;
|
|
|
|
|
+ // Ready frozen views retain the one-clock production read port.
|
|
|
|
|
+ // The diagnostic live view settles within two eight-word reads.
|
|
|
|
|
+ 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 && live_published_index==rd_index) ? live_data : 128'd0;
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
|
|
endmodule
|
|
endmodule
|