Lab: verifying a tag allocator
A tag allocator hands out free transaction tags to a requester and takes them back when the matching response
returns. It is common in bus masters, DMA engines and caches — and a classic source of "two transactions with the same tag"
bugs. Our version has N tags and always hands out the lowest free one.
Specification
- When
alloc_reqis high and at least one tag is free,alloc_gntis high andalloc_tagis a free tag, which becomes busy. - When no tag is free,
alloc_gntstays low. - When
free_vldis high, tagfree_tagbecomes free again. (The environment only frees busy tags.) free_maskshows which tags are free.
// tag_alloc.sv — hands out the lowest free tag; BUG=1 injects a real-world mistake
module tag_alloc #(
parameter int N = 4,
parameter bit BUG = 1'b0
) (
input logic clk,
input logic rst,
input logic alloc_req,
output logic alloc_gnt,
output logic [$clog2(N)-1:0] alloc_tag,
input logic free_vld,
input logic [$clog2(N)-1:0] free_tag,
output logic [N-1:0] free_mask
);
localparam int TW = $clog2(N);
// lowest free tag (tag 0 when none is free)
always_comb begin
alloc_tag = '0;
for (int i = N - 1; i >= 0; i--)
if (free_mask[i]) alloc_tag = TW'(i);
end
// the bug: forgetting to check that a tag is actually free
assign alloc_gnt = BUG ? alloc_req : (alloc_req && (free_mask != '0));
always_ff @(posedge clk) begin
if (rst) free_mask <= '1;
else free_mask <= (free_mask & ~({N{alloc_gnt}} & (N'(1) << alloc_tag)))
| ({N{free_vld}} & (N'(1) << free_tag));
end
endmoduleThe formal testbench
We track one symbolic tag. A single flag, sym_busy, says whether that tag is currently handed out.
The same symbol is used for the environment assumption, so the environment never frees the tracked tag while it is free.
// tag_alloc_fv.sv — formal wrapper with a symbolic tag
module tag_alloc_fv #(
parameter int N = 4,
parameter bit BUG = 1'b0
) (
input logic clk,
input logic rst,
input logic alloc_req,
input logic free_vld,
input logic [$clog2(N)-1:0] free_tag,
input logic [$clog2(N)-1:0] sym_tag // symbolic: chosen once by the tool
);
localparam int TW = $clog2(N);
logic alloc_gnt;
logic [TW-1:0] alloc_tag;
logic [N-1:0] free_mask;
tag_alloc #(.N(N), .BUG(BUG)) dut (.*);
initial assume (rst);
default clocking cb @(posedge clk); endclocking
default disable iff (rst);
// ---------------------------------------------------- auxiliary model
am_sym_stable: assume property ($stable(sym_tag));
logic sym_busy; // is the symbolic tag handed out?
always_ff @(posedge clk)
if (rst) sym_busy <= 1'b0;
else if (alloc_gnt && alloc_tag == sym_tag) sym_busy <= 1'b1;
else if (free_vld && free_tag == sym_tag) sym_busy <= 1'b0;
// ---------------------------------------------------- environment
am_free_busy_only: assume property (free_vld && free_tag == sym_tag |-> sym_busy);
// ---------------------------------------------------- checks
a_no_double_alloc: assert property (alloc_gnt && alloc_tag == sym_tag |-> !sym_busy);
a_mask_matches: assert property (free_mask[sym_tag] == !sym_busy);
a_gnt_when_free: assert property (alloc_req && free_mask != '0 |-> alloc_gnt);
a_gnt_free_tag: assert property (alloc_gnt |-> free_mask[alloc_tag]);
// ---------------------------------------------------- covers
c_all_busy: cover property (free_mask == '0);
c_sym_reused: cover property (sym_busy ##1 !sym_busy ##[1:$] sym_busy);
c_alloc_free: cover property (alloc_gnt && free_vld);
endmodule# tag_alloc.sby
[tasks]
prove
cover
bug
[options]
prove: mode prove
bug: mode prove
cover: mode cover
depth 12
[engines]
prove: abc pdr
bug: smtbmc
cover: smtbmc
[script]
read -formal tag_alloc.sv tag_alloc_fv.sv
bug: chparam -set BUG 1 tag_alloc_fv
prep -top tag_alloc_fv
[files]
tag_alloc.sv
tag_alloc_fv.svWhat to expect
sby -f tag_alloc.sby prove— all assertions pass for every tag, because the tag is symbolic.sby -f tag_alloc.sby cover— all covers are reached, so the environment is not over-constrained.sby -f tag_alloc.sby bug— fails. The trace allocates all four tags, then requests again: withBUG=1the allocator grants tag 0 a second time. Botha_no_double_alloc(withsym_tag = 0) anda_gnt_free_tagcatch it.