Files
templates/hdl/scope-acquisition/tests/tb_sample_pairer.sv
T

280 lines
11 KiB
Systemverilog

`timescale 1ns/1ps
module tb_sample_pairer;
reg clk = 0;
reg rst = 1;
reg coherent_epoch_valid = 0, session_fault = 0;
reg [95:0] a_data = 0, b_data = 0;
reg a_valid = 0, b_valid = 0;
wire a_ready, b_ready;
wire [63:0] m_data, m_index;
wire m_valid, fault;
reg m_ready = 0;
reg auto_source = 0, full_rate = 0;
integer source_limit = 0;
integer source_a = 0, source_b = 0, source_cycle = 0;
reg a_consumed = 0, b_consumed = 0;
reg [63:0] queue_data [0:511];
reg [63:0] queue_index [0:511];
integer queue_head = 0, queue_tail = 0;
integer transferred = 0, total_transferred = 0;
reg stalled = 0;
reg [63:0] stalled_data, stalled_index;
always #4 clk = ~clk;
sample_pairer dut(.*);
function automatic [31:0] chip_word(input integer chip, input integer n);
reg [9:0] lane_a, lane_b;
begin
lane_a = (n * 13 + chip * 101) & 1023;
lane_b = (n * 29 + chip * 211) & 1023;
chip_word = {6'b0, lane_b, 6'b0, lane_a};
end
endfunction
// Independent FIFO-head models. Valid does not wait for ready, and an
// offered head remains stable until it is consumed. Availability/stalls
// are deterministic and independently exercise both input interfaces.
always @(negedge clk) begin
if (auto_source && !rst) begin
source_cycle = source_cycle + 1;
if (!a_valid || a_consumed) begin
a_valid = source_a < source_limit &&
(full_rate || source_cycle % 5 != 0);
a_data = {64'(source_a), chip_word(0, source_a)};
end
if (!b_valid || b_consumed) begin
b_valid = source_b < source_limit &&
(full_rate || source_cycle % 7 != 1);
b_data = {64'(source_b), chip_word(1, source_b)};
end
m_ready = full_rate ||
(source_cycle % 7 != 2 && source_cycle % 11 != 0);
end
end
// Compare accepted input/output transactions, not DUT internal state.
always @(posedge clk) begin
if (rst) begin
source_a = 0; source_b = 0; source_cycle = 0;
a_consumed = 0; b_consumed = 0;
queue_head = 0; queue_tail = 0; transferred = 0;
stalled = 0;
end else begin
if (stalled && (m_valid !== 1'b1 ||
m_data !== stalled_data || m_index !== stalled_index))
$fatal(1, "stalled output changed, including fault path");
if (a_ready !== b_ready)
$fatal(1, "input pair was not consumed atomically");
if (fault && (a_ready || b_ready))
$fatal(1, "input consumed after sticky fault");
if (auto_source && full_rate && source_a > 0 &&
source_a < source_limit && m_valid !== 1'b1)
$fatal(1, "elastic pairer inserted a full-rate output bubble");
if (m_valid && m_ready) begin
if (queue_head >= queue_tail)
$fatal(1, "unexpected output without an accepted pair");
if (m_data !== queue_data[queue_head] ||
m_index !== queue_index[queue_head])
$fatal(1, "output data/index/order mismatch at transfer %0d", queue_head);
queue_head = queue_head + 1;
transferred = transferred + 1;
total_transferred = total_transferred + 1;
end
a_consumed = a_valid && a_ready;
b_consumed = b_valid && b_ready;
if (a_consumed || b_consumed) begin
if (!a_consumed || !b_consumed || !coherent_epoch_valid ||
session_fault || fault)
$fatal(1, "pair accepted without session/epoch permission");
if (a_data[95:32] !== b_data[95:32] ||
a_data[95:32] !== 64'(queue_tail))
$fatal(1, "mismatched/noncontiguous index accepted");
if (queue_tail >= 512)
$fatal(1, "reference queue overflow");
queue_data[queue_tail] = {b_data[31:0], a_data[31:0]};
queue_index[queue_tail] = a_data[95:32];
queue_tail = queue_tail + 1;
if (auto_source) begin
source_a = source_a + 1;
source_b = source_b + 1;
end
end
stalled = m_valid && !m_ready;
stalled_data = m_data;
stalled_index = m_index;
end
end
task automatic reset_session;
begin
@(negedge clk);
auto_source = 0;
rst = 1;
a_valid = 0; b_valid = 0; m_ready = 0;
coherent_epoch_valid = 0; session_fault = 0;
repeat (2) begin @(posedge clk); #0.2; end
if (m_valid !== 1'b0 || fault !== 1'b0 ||
a_ready !== 1'b0 || b_ready !== 1'b0)
$fatal(1, "reset did not clear/gate the interface");
@(negedge clk); rst = 0;
end
endtask
task automatic offer_heads(input integer ai, input integer bi);
begin
@(negedge clk);
a_valid = 1; b_valid = 1;
a_data = {64'(ai), chip_word(0, ai)};
b_data = {64'(bi), chip_word(1, bi)};
end
endtask
task automatic check_fault;
begin
@(posedge clk); #0.2;
if (fault !== 1'b1 || a_ready !== 1'b0 || b_ready !== 1'b0)
$fatal(1, "expected sticky fault with no new input consumption");
end
endtask
task automatic exercise_stream(input integer count, input reg continuous);
begin
reset_session();
@(negedge clk);
source_limit = count; full_rate = continuous;
coherent_epoch_valid = 1;
auto_source = 1;
while (source_a < count) begin @(posedge clk); #0.2; end
@(negedge clk);
auto_source = 0; a_valid = 0; b_valid = 0; m_ready = 1;
repeat (3) begin @(posedge clk); #0.2; end
if (fault !== 1'b0 || transferred != count ||
queue_head != queue_tail || m_valid !== 1'b0)
$fatal(1, "stream incomplete or faulted: sent=%0d out=%0d", count, transferred);
end
endtask
initial begin
// Equal local index zero must not bypass the external epoch guard.
reset_session();
offer_heads(0, 0);
repeat (4) begin
@(posedge clk); #0.2;
if (a_ready || b_ready || m_valid || fault)
$fatal(1, "unverified physical epoch released a pair");
end
@(negedge clk); coherent_epoch_valid = 1;
@(posedge clk); #0.2;
if (!m_valid || m_index !== 64'd0)
$fatal(1, "epoch permission did not release initial pair");
@(negedge clk); a_valid = 0; b_valid = 0;
repeat (5) begin @(posedge clk); #0.2; end
@(negedge clk); m_ready = 1;
@(posedge clk); #0.2;
if (transferred != 1 || m_valid)
$fatal(1, "stalled initial pair did not drain");
exercise_stream(128, 0);
exercise_stream(64, 1);
// Different FIFO-head indices are rejected without consuming either.
reset_session();
coherent_epoch_valid = 1;
offer_heads(0, 1);
check_fault();
if (queue_tail != 0 || m_valid)
$fatal(1, "index mismatch leaked a pair");
offer_heads(0, 0);
check_fault();
// A simultaneous drop is not detected by equality alone. Expected
// index rejects both heads jumping from 0 to 2; prior good output is
// held and may drain even after the acquisition has been invalidated.
reset_session();
coherent_epoch_valid = 1;
offer_heads(0, 0);
@(posedge clk); #0.2;
offer_heads(2, 2);
check_fault();
repeat (4) begin @(posedge clk); #0.2; end
if (queue_tail != 1 || !m_valid || m_index !== 64'd0)
$fatal(1, "both-stream gap changed prior pending output");
@(negedge clk); m_ready = 1;
@(posedge clk); #0.2;
if (transferred != 1 || m_valid)
$fatal(1, "prior good output failed to drain after mismatch fault");
// An equal duplicate index is also a sequence violation.
reset_session();
coherent_epoch_valid = 1;
offer_heads(0, 0);
@(posedge clk); #0.2;
offer_heads(0, 0);
check_fault();
if (queue_tail != 1)
$fatal(1, "equal duplicate ordinal was consumed");
// ADC valid ordinals remain contiguous across a common READY gap.
// The mandatory session guard, also used for overflow/clock loss,
// must latch the loss even though later offered indices equal one.
reset_session();
coherent_epoch_valid = 1;
offer_heads(0, 0);
@(posedge clk); #0.2;
@(negedge clk);
a_valid = 0; b_valid = 0; session_fault = 1;
check_fault();
@(negedge clk); session_fault = 0;
repeat (3) begin @(posedge clk); #0.2; end
offer_heads(1, 1);
check_fault();
if (queue_tail != 1 || !m_valid || m_index !== 64'd0)
$fatal(1, "session fault consumed resumed contiguous ordinals");
@(negedge clk); m_ready = 1;
@(posedge clk); #0.2;
if (transferred != 1 || m_valid)
$fatal(1, "prior good output failed to drain after session fault");
// Loss of established epoch permission invalidates the session, but
// cannot retract an already offered stalled valid transaction.
reset_session();
coherent_epoch_valid = 1;
offer_heads(0, 0);
@(posedge clk); #0.2;
@(negedge clk);
a_valid = 0; b_valid = 0; coherent_epoch_valid = 0;
check_fault();
repeat (3) begin @(posedge clk); #0.2; end
@(negedge clk); m_ready = 1;
@(posedge clk); #0.2;
if (transferred != 1 || m_valid)
$fatal(1, "pending output failed to drain after epoch loss");
// Reset is the explicit transaction-abort boundary. It clears a
// pending word/fault and permits a new verified epoch starting at zero.
reset_session();
coherent_epoch_valid = 1;
offer_heads(0, 0);
@(posedge clk); #0.2;
reset_session();
coherent_epoch_valid = 1;
offer_heads(0, 0);
@(posedge clk); #0.2;
@(negedge clk); a_valid = 0; b_valid = 0; m_ready = 1;
@(posedge clk); #0.2;
if (fault || transferred != 1 || m_valid)
$fatal(1, "reset/re-arm did not start a clean index-zero session");
$display("PASS tb_sample_pairer transfers=%0d epoch guard, independent availability, stalls, full rate, mismatch, simultaneous gap, session fault and reset", total_transferred);
$finish;
end
initial begin
#30000;
$fatal(1, "tb_sample_pairer timeout");
end
endmodule