The previous lesson gave the two rules: record issue times, and classify what is left against the last progress, not against now. This lesson runs them against an engine with both faults at once.
The engine has eight tags, a latency of 2 to 9 cycles from the data, and answers the lowest ready tag first. The bench drives two copies: a healthy one, and one with two planted faults. On the bad copy, the third request on tag 5 is accepted and never answered, and from cycle 100 no response is sent at all. The drain scoreboard extends the ID scoreboard from the first lesson with issue times and classification.
interface tag_if (input logic clk);
logic req_valid = 0;
logic [2:0] req_tag = 0;
logic [7:0] req_data = 0;
logic rsp_valid;
logic [2:0] rsp_tag;
logic [7:0] rsp_data;
endinterface
// Tagged engine: 8 tags, latency 2..9 cycles from the data, lowest ready tag answers first.
// LEAK_TAG >= 0: the 3rd request on that tag is accepted and never answered.
// HANG_AT >= 0: from that cycle on, no response is ever sent.
module tag_engine #(parameter int LEAK_TAG = -1, parameter int HANG_AT = -1) (
input logic clk, rst_n,
tag_if bus
);
logic busy [8];
logic [3:0] timer[8];
logic [7:0] data [8];
logic [1:0] uses [8];
int cycle;
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
for (int t = 0; t < 8; t++) begin busy[t] <= 0; timer[t] <= 0; uses[t] <= 0; end
bus.rsp_valid <= 0; bus.rsp_tag <= 0; bus.rsp_data <= 0; cycle <= 0;
end else begin
automatic logic found = 0;
cycle <= cycle + 1;
bus.rsp_valid <= 0;
for (int t = 0; t < 8; t++) if (busy[t] && timer[t] != 0) timer[t] <= timer[t] - 1;
if (HANG_AT < 0 || cycle < HANG_AT)
for (int t = 0; t < 8; t++)
if (!found && busy[t] && timer[t] == 0) begin
found = 1;
busy[t] <= 0;
bus.rsp_valid <= 1; bus.rsp_tag <= 3'(t); bus.rsp_data <= ~data[t];
end
if (bus.req_valid) begin
uses[bus.req_tag] <= uses[bus.req_tag] + 1;
data[bus.req_tag] <= bus.req_data;
timer[bus.req_tag] <= 4'(bus.req_data[2:0]) + 2;
busy[bus.req_tag] <= !(int'(bus.req_tag) == LEAK_TAG && uses[bus.req_tag] == 2);
end
end
end
endmodule
class id_scoreboard #(type T = logic [7:0]);
string name;
protected T exp_q[int][$];
int n_added, n_matched, n_errors;
function new(string name); this.name = name; endfunction
virtual function void add_expected(int id, T item);
exp_q[id].push_back(item);
n_added++;
endfunction
virtual function void check_actual(int id, T item);
T exp;
if (!exp_q.exists(id) || exp_q[id].size() == 0) begin
n_errors++;
$display("%s: UNEXPECTED response for ID %0d with nothing outstanding", name, id);
return;
end
exp = exp_q[id].pop_front();
if (exp_q[id].size() == 0) exp_q.delete(id);
if (item !== exp) begin
n_errors++;
$display("%s: MISMATCH ID %0d expected %h got %h", name, id, exp, item);
end else n_matched++;
endfunction
function int outstanding();
int n = 0;
foreach (exp_q[id]) n += exp_q[id].size();
return n;
endfunction
endclass
// Adds issue times, progress tracking and end-of-test classification.
class drain_scoreboard #(type T = logic [7:0]) extends id_scoreboard #(T);
protected longint issued_at[int][$]; // same shape as exp_q
longint last_progress;
int max_lat, stall_limit;
int leaked[$], stuck[$]; // filled by classify()
bit stream_stuck;
function new(string name, int max_lat, int stall_limit);
super.new(name);
this.max_lat = max_lat;
this.stall_limit = stall_limit;
endfunction
function void add_at(longint now, int id, T item);
add_expected(id, item);
issued_at[id].push_back(now);
endfunction
function void check_at(longint now, int id, T item);
check_actual(id, item);
if (issued_at.exists(id)) begin
void'(issued_at[id].pop_front());
if (issued_at[id].size() == 0) issued_at.delete(id);
end
last_progress = now;
endfunction
// Called every cycle: report an item the first time it passes max_lat.
function void watchdog(longint now);
foreach (issued_at[id])
if (now - issued_at[id][0] == max_lat)
$display("%s: WARN ID %0d has waited %0d cycles (issued at %0d)", name, id, max_lat, issued_at[id][0]);
endfunction
// End of test: a leak is an item the DUT outlived by more than max_lat while
// still answering others; everything else left behind is stuck in a hung stream.
function void classify(longint now);
foreach (issued_at[id])
foreach (issued_at[id][k])
if (last_progress - issued_at[id][k] > max_lat) leaked.push_back(id);
else stuck.push_back(id);
stream_stuck = stuck.size() != 0 && (now - last_progress) > stall_limit;
foreach (leaked[i]) $display("%s: LEAK ID %0d never answered; DUT kept working for %0d+ cycles after it", name, leaked[i], max_lat);
if (stream_stuck)
$display("%s: STUCK no response for %0d cycles; IDs waiting: %p", name, now - last_progress, stuck);
if (outstanding() == 0) $display("%s: drained clean, %0d matched", name, n_matched);
endfunction
endclass
class tag_agent;
virtual tag_if vif;
drain_scoreboard #(logic [7:0]) sb;
int free_tags[$];
longint cycle;
bit starved;
function new(virtual tag_if vif, string name);
this.vif = vif;
sb = new(name, 40, 60);
for (int t = 0; t < 8; t++) free_tags.push_back(t);
endfunction
// Monitor: one process per agent, runs for the whole test.
task monitor();
forever begin
@(posedge vif.clk);
cycle++;
if (vif.rsp_valid) begin
sb.check_at(cycle, vif.rsp_tag, ~vif.rsp_data);
free_tags.push_back(vif.rsp_tag);
end
sb.watchdog(cycle);
end
endtask
// Driver: decides on the falling edge, so the monitor has already freed
// this cycle's tag. It never waits forever for a free tag.
task drive(int n_req);
for (int n = 0; n < n_req; n++) begin
int waited = 0;
int t;
logic [7:0] d;
@(negedge vif.clk);
vif.req_valid <= 0;
while (free_tags.size() == 0 && waited < 100) begin @(negedge vif.clk); waited++; end
if (free_tags.size() == 0) begin
starved = 1;
$display("%s: driver gave up after %0d cycles with no free tag", sb.name, waited);
return;
end
t = free_tags.pop_front();
d = 8'($urandom);
vif.req_valid <= 1; vif.req_tag <= 3'(t); vif.req_data <= d;
sb.add_at(cycle + 1, t, d); // the DUT samples it on the next edge
end
@(negedge vif.clk) vif.req_valid <= 0;
endtask
// Drain: wait for the last response, but never longer than the limit.
task drain(int limit);
int waited = 0;
while (sb.outstanding() != 0 && waited < limit) begin @(posedge vif.clk); waited++; end
sb.classify(cycle);
endtask
endclass
The bench passes. The good engine drains clean with 100 matches. On the bad one, the watchdog warns about tag 5 at cycle 63, long before the end, then the driver gives up, and the drain report says:
bad: LEAK ID 5 never answered; DUT kept working for 40+ cycles after it
bad: STUCK no response for 301 cycles; IDs waiting: '{'h0, 'h1, 'h2, 'h3, 'h4, 'h6, 'h7}Two mutants were run and both fail the bench:
Classify against now: change last_progress - issued_at[id][k] to now - issued_at[id][k] in classify(). Every outstanding item then counts as a leak: the report names eight leaked tags and no stuck stream.
Plant a leak in the good engine (LEAK_TAG(2) on u_good). Its drain is no longer clean and the bench fails.
In UVM, this logic belongs in check_phase or report_phase, and the bounded drain maps to the objection drain time and phase_ready_to_end. The classification rule is the same.
It was issued at cycle 23, and the DUT kept answering until about cycle 100. That is more than 40 cycles of progress after it, so the rule classifies it as leaked, not stuck.
The next response on that ID mismatches, because the scoreboard compares it with the dropped item. The end-of-test count is one short on that ID. Unique tags turn the same fault into a clean, named leak.