FVO — Formal Verification Online
Lesson 13.5 · Auxiliary Code and Modelling

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

  1. When alloc_req is high and at least one tag is free, alloc_gnt is high and alloc_tag is a free tag, which becomes busy.
  2. When no tag is free, alloc_gnt stays low.
  3. When free_vld is high, tag free_tag becomes free again. (The environment only frees busy tags.)
  4. free_mask shows which tags are free.
tag_alloc.sv
// 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
endmodule

The 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
// 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
# 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.sv

What 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: with BUG=1 the allocator grants tag 0 a second time. Both a_no_double_alloc (with sym_tag = 0) and a_gnt_free_tag catch it.

Chapter takeaways

Discussion

No comments yet.

Was this page helpful?

Tip: select any text on the page before clicking Report and it will be quoted in your report.