Files
templates/hdl/scope-acquisition/rtl/acquisition_buffer.sv

100 lines
4.6 KiB
Systemverilog

`timescale 1ns/1ps
// Bounded BRAM reference acquisition: pre + trigger + post, then frozen drain.
// This is not the later continuously DMA-written DDR ring.
module acquisition_buffer #(
parameter integer ADDR_BITS = 8
) (
input wire clk, rst, arm, stop, session_fault,
input wire [31:0] cfg_pre, cfg_post,
input wire [63:0] sample_data, sample_index,
input wire sample_valid,
input wire trigger_hit,
output wire eligible,
output reg active, done, fault,
output reg [63:0] trigger_index, first_index,
output reg [31:0] record_length,
output reg [63:0] m_data, m_index,
output wire m_valid,
input wire m_ready,
output wire m_last
);
localparam integer DEPTH = 1 << ADDR_BITS;
(* ram_style = "block" *) reg [63:0] ram [0:DEPTH-1];
reg [ADDR_BITS-1:0] write_ptr, read_ptr;
reg [31:0] pre_length, post_length, history_count, post_remaining, fetch_count;
reg [63:0] expected_index;
reg seen_sample, triggered, output_valid, output_last;
wire [32:0] cfg_total = {1'b0,cfg_pre}+{1'b0,cfg_post}+33'd1;
wire bad_index = seen_sample && sample_index != expected_index;
wire take_sample = active && sample_valid && !arm && !stop && !session_fault &&
!fault && !bad_index;
wire fetch = done && !fault && !arm && !stop && !session_fault &&
(!output_valid || m_ready) && fetch_count < record_length;
assign eligible = active && !fault && history_count >= pre_length;
assign m_valid = output_valid;
assign m_last = output_last && output_valid;
initial if (ADDR_BITS < 2 || ADDR_BITS > 20) $error("acquisition_buffer ADDR_BITS 2..20 required");
// RAM has no reset. Valid/state owns initialized payload.
always @(posedge clk) begin
if (take_sample) ram[write_ptr] <= sample_data;
if (fetch) m_data <= ram[read_ptr];
end
always @(posedge clk) begin
if (rst) begin
write_ptr<=0; read_ptr<=0; pre_length<=0; post_length<=0;
history_count<=0; post_remaining<=0; fetch_count<=0;
expected_index<=0; seen_sample<=0; triggered<=0;
active<=0; done<=0; fault<=0;
trigger_index<=0; first_index<=0; record_length<=0;
output_valid<=0; output_last<=0; m_index<=0;
end else begin
if (output_valid && m_ready) output_valid<=0;
// Once stalled, preserve the pending output even through faults.
if (fetch) begin
read_ptr<=read_ptr+1'b1;
m_index<=first_index+fetch_count;
output_last<=fetch_count+1 == record_length;
output_valid<=1;
fetch_count<=fetch_count+1'b1;
end
if (session_fault) begin fault<=1; active<=0; done<=0; end
else if (stop) begin active<=0; done<=0; end
else if (arm) begin
// A new ARM is rejected until the old output has drained.
if (active || output_valid || (done && fetch_count < record_length)) begin
fault<=1; active<=0; done<=0;
end else begin
write_ptr<=0; read_ptr<=0; history_count<=0;
seen_sample<=0; triggered<=0; fetch_count<=0; post_remaining<=0;
trigger_index<=0; first_index<=0;
pre_length<=cfg_pre; post_length<=cfg_post;
record_length<=cfg_total[31:0]; done<=0;
if (cfg_total > DEPTH) begin fault<=1; active<=0; end
else if (!fault) active<=1;
end
end else if (active && sample_valid) begin
if (bad_index || (trigger_hit && (!eligible || sample_index < pre_length))) begin
fault<=1; active<=0; done<=0;
end else begin
write_ptr<=write_ptr+1'b1;
expected_index<=sample_index+1'b1;
seen_sample<=1;
if (history_count < pre_length) history_count<=history_count+1'b1;
if (!triggered && trigger_hit) begin
triggered<=1; trigger_index<=sample_index;
first_index<=sample_index-pre_length;
read_ptr<=write_ptr-pre_length;
post_remaining<=post_length;
if (post_length==0) begin active<=0; done<=1; end
end else if (triggered) begin
post_remaining<=post_remaining-1'b1;
if (post_remaining==1) begin active<=0; done<=1; end
end
end
end
end
end
endmodule