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

278 lines
11 KiB
Systemverilog

`timescale 1ns/1ps
// Functional ADC pin model. Six conversion pipeline stages are followed by
// nonzero output propagation delay and bit-to-bit skew. These deliberately
// avoid a same-edge RTL stimulus; they are not a characterized PVT/SI model.
module adc_pin_model #(
parameter integer CHANNEL_BASE = 0
) (
input wire clk,
input wire pins_connected,
output reg [9:0] data_a,
output reg [9:0] data_b
);
reg [9:0] pipeline_a [0:5];
reg [9:0] pipeline_b [0:5];
reg [9:0] launched_a, launched_b;
integer conversion = 0;
integer i;
function automatic [9:0] pattern(input integer channel, input integer n);
integer phase, bit_number;
begin
phase = n % 120;
if (phase < 80)
pattern = (n * (2 * channel + 3) + channel * 137) & 1023;
else if (phase < 90) begin
bit_number = (phase - 80 + channel) % 10;
pattern = 10'b1 << bit_number;
end else if (phase < 100) begin
bit_number = (phase - 90 + channel) % 10;
pattern = ~(10'b1 << bit_number);
end else begin
case ((phase + channel) % 6)
0: pattern = 10'd0;
1: pattern = 10'd1023;
2: pattern = 10'd1;
3: pattern = 10'd1022;
4: pattern = 10'd511;
5: pattern = 10'd512;
endcase
end
end
endfunction
initial begin
data_a = 10'bx;
data_b = 10'bx;
for (i = 0; i < 6; i = i + 1) begin
pipeline_a[i] = 10'bx;
pipeline_b[i] = 10'bx;
end
end
always @(posedge clk) begin
launched_a = pins_connected ? pipeline_a[5] : 10'bx;
launched_b = pins_connected ? pipeline_b[5] : 10'bx;
for (i = 5; i > 0; i = i - 1) begin
pipeline_a[i] = pipeline_a[i-1];
pipeline_b[i] = pipeline_b[i-1];
end
pipeline_a[0] = pattern(CHANNEL_BASE, conversion);
pipeline_b[0] = pattern(CHANNEL_BASE + 1, conversion);
conversion = conversion + 1;
for (i = 0; i < 10; i = i + 1) begin
data_a[i] <= #(4.20 + i * 0.06) launched_a[i];
data_b[i] <= #(4.30 + (9-i) * 0.06) launched_b[i];
end
end
endmodule
// External-interface reference: pad sampling followed by one fabric stage;
// two reset-release stages, two READY stages and one READY warmup stage.
// Reference state is independent of DUT hierarchy and is updated before NBA.
module adc_capture_scoreboard #(
parameter integer CHIP = 1
) (
input wire clk, arst_n, ready_async,
input wire [9:0] data_a, data_b,
input wire [31:0] word_value,
input wire valid,
input wire [63:0] sample_index,
output integer checked_samples,
output reg [9:0] walking_one_a, walking_one_b,
output reg [9:0] walking_zero_a, walking_zero_b,
output reg [3:0] boundaries_a, boundaries_b
);
reg [1:0] reset_release = 0, ready_history = 0;
reg ready_warm = 0;
reg [19:0] pad_history = 20'bx;
reg [31:0] expected_word;
reg expected_valid;
reg [63:0] expected_next_index = 0, expected_index = 0;
reg local_reset;
integer bit_number;
initial begin
checked_samples = 0;
walking_one_a = 0; walking_one_b = 0;
walking_zero_a = 0; walking_zero_b = 0;
boundaries_a = 0; boundaries_b = 0;
end
always @(negedge arst_n) begin
reset_release = 0;
ready_history = 0;
end
always @(posedge clk) begin
local_reset = !reset_release[1];
expected_word = local_reset ? 32'd0 :
{6'b0, pad_history[19:10], 6'b0, pad_history[9:0]};
expected_valid = !local_reset && ready_history[1] && ready_warm;
if (local_reset) begin
expected_next_index = 0;
expected_index = 0;
ready_warm = 0;
end else begin
ready_warm = ready_history[1];
if (expected_valid) begin
expected_index = expected_next_index;
expected_next_index = expected_next_index + 1'b1;
end
end
ready_history = (!arst_n || local_reset) ? 2'b00 :
{ready_history[0], ready_async};
reset_release = !arst_n ? 2'b00 : {reset_release[0], 1'b1};
pad_history = {data_b, data_a};
#0.1;
if (valid !== expected_valid)
$fatal(1, "chip%0d valid/reset/READY latency at t=%0t: got %b expected %b",
CHIP, $time, valid, expected_valid);
if (sample_index !== expected_index)
$fatal(1, "chip%0d index/reset/retention at t=%0t: got %0d expected %0d",
CHIP, $time, sample_index, expected_index);
// The data stage is updated even while invalid. Checking every edge
// detects an unintended CE, an extra stage or a stale restart sample.
if (word_value !== expected_word)
$fatal(1, "chip%0d data/pipeline alignment at t=%0t: got %h expected %h",
CHIP, $time, word_value, expected_word);
if (valid) begin
if ($isunknown(word_value))
$fatal(1, "chip%0d unknown data was marked valid", CHIP);
if (word_value[15:10] !== 6'b0 || word_value[31:26] !== 6'b0)
$fatal(1, "chip%0d nonzero sample padding", CHIP);
checked_samples = checked_samples + 1;
for (bit_number = 0; bit_number < 10; bit_number = bit_number + 1) begin
if (word_value[9:0] == (10'b1 << bit_number))
walking_one_a[bit_number] = 1;
if (word_value[25:16] == (10'b1 << bit_number))
walking_one_b[bit_number] = 1;
if (word_value[9:0] == ~(10'b1 << bit_number))
walking_zero_a[bit_number] = 1;
if (word_value[25:16] == ~(10'b1 << bit_number))
walking_zero_b[bit_number] = 1;
end
case (word_value[9:0])
10'd0: boundaries_a[0] = 1;
10'd1023: boundaries_a[1] = 1;
10'd511: boundaries_a[2] = 1;
10'd512: boundaries_a[3] = 1;
endcase
case (word_value[25:16])
10'd0: boundaries_b[0] = 1;
10'd1023: boundaries_b[1] = 1;
10'd511: boundaries_b[2] = 1;
10'd512: boundaries_b[3] = 1;
endcase
end
end
endmodule
module tb_adc_capture;
reg arst_n = 1, mezz_ready = 0;
reg adc1_clk = 0, adc2_clk = 0;
reg pins_connected = 1;
wire [9:0] adc1_a, adc1_b, adc2_a, adc2_b;
wire [31:0] chip1_word, chip2_word;
wire chip1_valid, chip2_valid;
wire [63:0] chip1_index, chip2_index;
wire [31:0] count1, count2;
wire [9:0] ones1a, ones1b, zeros1a, zeros1b;
wire [9:0] ones2a, ones2b, zeros2a, zeros2b;
wire [3:0] bounds1a, bounds1b, bounds2a, bounds2b;
integer stopped_count1, stopped_count2;
reg [63:0] stopped_index1, stopped_index2;
always #5 adc1_clk = ~adc1_clk;
initial begin
#1.37;
forever #5.3 adc2_clk = ~adc2_clk;
end
adc_capture_dual dut(.*);
adc_pin_model #(.CHANNEL_BASE(0)) source1(
.clk(adc1_clk), .pins_connected(pins_connected), .data_a(adc1_a), .data_b(adc1_b));
adc_pin_model #(.CHANNEL_BASE(2)) source2(
.clk(adc2_clk), .pins_connected(pins_connected), .data_a(adc2_a), .data_b(adc2_b));
adc_capture_scoreboard #(.CHIP(1)) check1(
.clk(adc1_clk), .arst_n(arst_n), .ready_async(mezz_ready),
.data_a(adc1_a), .data_b(adc1_b), .word_value(chip1_word),
.valid(chip1_valid), .sample_index(chip1_index), .checked_samples(count1),
.walking_one_a(ones1a), .walking_one_b(ones1b),
.walking_zero_a(zeros1a), .walking_zero_b(zeros1b),
.boundaries_a(bounds1a), .boundaries_b(bounds1b));
adc_capture_scoreboard #(.CHIP(2)) check2(
.clk(adc2_clk), .arst_n(arst_n), .ready_async(mezz_ready),
.data_a(adc2_a), .data_b(adc2_b), .word_value(chip2_word),
.valid(chip2_valid), .sample_index(chip2_index), .checked_samples(count2),
.walking_one_a(ones2a), .walking_one_b(ones2b),
.walking_zero_a(zeros2a), .walking_zero_b(zeros2b),
.boundaries_a(bounds2a), .boundaries_b(bounds2b));
task automatic require_idle;
begin
if (chip1_valid !== 1'b0 || chip2_valid !== 1'b0)
$fatal(1, "valid outside a settled READY interval");
end
endtask
initial begin
// Reset release is deliberately unrelated to either forwarded clock.
#0.2 arst_n = 0;
#31.1 arst_n = 1;
#100 require_idle();
mezz_ready = 1;
#14000;
// READY loss permits only the defined synchronizer tail. Disconnect
// the bus after that tail: high-Z/unpowered data is modeled as X.
mezz_ready = 0;
#100 require_idle();
stopped_count1 = count1; stopped_count2 = count2;
stopped_index1 = chip1_index; stopped_index2 = chip2_index;
pins_connected = 0;
#200 require_idle();
if (count1 != stopped_count1 || count2 != stopped_count2 ||
chip1_index !== stopped_index1 || chip2_index !== stopped_index2)
$fatal(1, "READY-low interval advanced stream/index");
pins_connected = 1;
#140 mezz_ready = 1;
#500;
if (count1 <= stopped_count1 || count2 <= stopped_count2)
$fatal(1, "capture did not resume after READY restoration");
// Reset while disconnected must not release stale IOB contents.
mezz_ready = 0;
#83.7 require_idle();
pins_connected = 0;
#17.2 arst_n = 0;
#31.9 arst_n = 1;
#150 require_idle();
if (chip1_index !== 64'd0 || chip2_index !== 64'd0)
$fatal(1, "reset did not clear sample indices");
pins_connected = 1;
#140 mezz_ready = 1;
#2000;
// Also reset during active capture with READY already asserted.
#13.1 arst_n = 0;
#27.3 arst_n = 1;
#400;
if (chip1_valid !== 1'b1 || chip2_valid !== 1'b1)
$fatal(1, "active-READY reset recovery failed");
if (count1 < 1600 || count2 < 1500)
$fatal(1, "insufficient independent samples: %0d/%0d", count1, count2);
if ({ones1a, ones1b, ones2a, ones2b, zeros1a, zeros1b, zeros2a, zeros2b}
!== {80{1'b1}})
$fatal(1, "incomplete walking-one/zero coverage");
if ({bounds1a, bounds1b, bounds2a, bounds2b} !== 16'hffff)
$fatal(1, "incomplete boundary-code coverage");
$display("PASS tb_adc_capture samples=%0d/%0d delayed/skewed pins, ramps, walking bits, boundaries, reset and READY", count1, count2);
$finish;
end
initial begin
#30000;
$fatal(1, "tb_adc_capture timeout");
end
endmodule