Cover image for Formal vs Functional Verification: simulation samples, formal proves. Random simulation of 100,000 cycles missed the deep bug, a formal check found it at step 9, and the trace was replayed in UVM as a directed test. The Art of Verification, lesson 8.
| | |

Formal vs Functional Verification: What Each One Proves, With a Real Example

In short: Functional verification, usually simulation, checks the cases you actually run. Formal verification uses a solver to check every possible input sequence, either up to a set number of cycles or, with a full proof, for all time. Simulation scales to a whole chip and tests real behaviour end to end. Formal is exhaustive on small blocks and finds deep corner cases that random stimulus almost never reaches. In this lesson the same rule, never grant two requesters at once, is checked both ways on one arbiter. Random simulation misses a bug hidden behind a six-step unlock sequence. A formal tool finds it in nine cycles, and the bench then replays that trace in UVM.

What is the difference between formal and functional verification?

Both ask the same question: does the design follow the spec? They answer it in different ways.

Functional verification drives stimulus into the design, watches what comes out, and compares it with a model. Constrained random UVM tests are the usual form. It can only say something about the cycles it actually simulated. When a regression passes, it means no failure showed up in the traffic you ran. It does not mean no failure exists.

Formal verification does not run stimulus. A tool turns the design and a property, such as never grant both, into a maths problem, and a solver searches every possible input on every cycle. If the property can break, it hands you the exact input sequence that breaks it, called a counterexample. If it cannot, it can prove the property holds, up to a depth or for all time.

Neither replaces the other. Formal struggles as designs grow, because the number of reachable states explodes. Simulation scales, but it only ever samples the space. Good teams use both and let each cover the other’s weak spot.

The picture: a combination lock

You want to know if a six-digit combination lock can be opened without the code.

One way is to try random combinations. Try 100,000 and you have covered a tenth of the space. If none opened it, you know a little, not a lot. That is simulation.

The other way is a locksmith who studies the mechanism and works out, from how it is built, which combinations open it, if any. On a big safe that could take forever, but on a small lock they give you a certain answer: either the exact combination, or a proof that none works. That is formal.

On a whole building full of doors, you send people to try the handles. On the one lock that guards the vault, you call the locksmith.

What will you learn in this lesson?

By the end of this lesson you will be able to:

  • Explain what simulation shows and what formal proves, and why they are different claims.
  • Use the basic formal words: property, assumption, counterexample, bounded check, proof.
  • Pick formal, simulation or both for a given block.
  • Write one SVA property and check it both in a UVM bench and with an open-source formal tool.
  • Replay a formal counterexample as a directed UVM test.

How do formal and simulation compare?

Functional (simulation)Formal
How it worksDrives stimulus, compares outputs with a modelA solver searches every input on every cycle
What a pass meansNo failure in the cycles you ranNo failure in any input sequence, up to a depth or for all time
What a fail gives youA failing test and a seedA counterexample: the shortest input sequence that breaks the rule
Scales toWhole chips, with softwareBlocks, mostly control logic of modest size
Best atData paths, end-to-end flows, performance, real softwareControl logic, arbiters, FIFO pointers, protocol rules, deep corners
Weak atRare sequences random stimulus almost never hitsBig designs, wide arithmetic, long latencies
What you writeSequences, a model, checkers, coverageProperties and assumptions

What do the formal words mean?

  • Property: a rule the design must follow, written in SVA, for example gnt != 2'b11.
  • Assumption: a rule about the inputs the tool must respect, such as a protocol the other block always follows. It plays the role constraints play in random stimulus. Too few and you get fake failures; too many and you hide real ones.
  • Counterexample: the exact input sequence, cycle by cycle, that breaks a property. It is a ready-made directed test.
  • Bounded check (BMC): the tool tries every input sequence up to N cycles after reset. A pass means nothing breaks the rule within N cycles.
  • Proof: a pass for all time, not just N cycles. One common method is induction: show the rule holds after reset, and that if it holds on one cycle it must hold on the next.
  • State explosion: every extra flop doubles the number of states the tool may need to consider, which is why formal works best on smaller blocks.

Why does random simulation miss some bugs?

Because some states are hard to reach by chance. In this lesson’s design, a debug mode turns on only after six configuration writes in a row with exactly the right values: A, 5, C, 3, F, 0. Each write is one of 16 values, so a random run hits the full sequence with odds of about 1 in 16 to the power of 6, roughly 1 in 17 million attempts. Coverage would tell you the state was never reached, if you thought to cover it. A formal tool does not need luck. It treats the inputs as unknowns and solves for the sequence directly.

Diagram: how far each method got into the six-step unlock code A, 5, C, 3, F, 0. Random simulation with 100,000 UVM cycles on VCS reached step 4 of 6 and never turned debug mode on, so +BUG=1 stayed hidden. The formal bounded check with Yosys and Z3 solved all six writes and failed the never-grant-both rule at step 9. Odds of random stimulus hitting all six writes are about 1 in 17 million.
Random stimulus stalls at step 4 of the unlock code; the solver walks straight in.

How do they work together?

  • Share the properties. The same SVA runs as an assertion in simulation and as a target in formal. Write it once.
  • Turn counterexamples into tests. A formal trace is a short directed test. Replay it in the UVM bench so the bug stays fixed for good.
  • Close coverage holes. When a cover point is never hit in simulation, formal can either reach it, which gives you a test, or prove it unreachable, which lets you remove it from the goal with a reason.
  • Split the work. Formal on the control-heavy blocks where corners hide. Simulation for the data paths, the integration and the full system.

How do simulation and formal compare on a real block?

One small arbiter, one rule, both methods.

The IP: a 2-requester arbiter with a debug mode

  • Two requesters raise req[0] and req[1]. The arbiter answers on the next cycle with gnt.
  • A requester alone gets the grant. When both request, the grant goes to the requester that did not win most recently, and a grant to a requester alone counts as a win. After reset, requester 0 goes first.
  • The rule: gnt is never 2’b11. Two requesters must never own the resource at once.
  • A debug mode turns on after six config writes in a row: A, 5, C, 3, F, 0. Any wrong value starts the sequence over. The spec says debug mode must not change how requests are granted.

Hidden unlock sequences like this are common in real chips: test modes, debug ports, firmware backdoors. They are also a classic place for bugs to hide, because normal traffic never opens them.

The two bugs

  • +BUG=1, the deep one: in debug mode, the arbiter grants every request as it comes, so both requesters can be granted together. Only reachable after the six-write unlock.
  • +BUG=2, the shallow one: when both request right after an idle cycle and requester 1 won last, both are granted.

How is the rule checked in each method?

In the UVM bench it is an assertion in the interface:

A_MUTEX: assert property (@(posedge clk) disable iff (!rst_n) gnt != 2'b11)
  else `uvm_error("SVA", $sformatf("A_MUTEX: both requesters granted (req=%b)", $sampled(req)));

In the formal harness it is the same rule, as an immediate assertion every cycle after reset:

always @(posedge clk) if (!init) assert (gnt != 2'b11);

The bench runs two phases, each after a reset:

PhaseWhat it does
RANDOM100,000 cycles of random requests and random config writes, cfg_wr 50% of cycles
REPLAYthe 8 cycles of the counterexample the formal tool found for +BUG=1, driven as a directed test

The scoreboard checks every grant against a model written from the spec, counts assertion fails, and reports how far the config writes got into the unlock sequence, from 0 to 6.

Diagram: UVM testbench architecture for the arbiter. tav_formal_vs_sim_test runs 100,000 random cycles, then replays the formal counterexample. The agent drives req, cfg_wr and cfg_data and samples gnt plus two probes through tav_arb_if, which carries the A_MUTEX assertion. The scoreboard holds a spec model, a grant checker, the unlock depth, whether debug mode was reached, and the phase report.
One UVM bench for simulation; the same rule also goes to the formal harness.

What did the formal tool find?

I ran the formal harness with open-source tools: Yosys 0.33, yosys-smtbmc and the Z3 solver, on 24 Nov 2026. They are free, so you can repeat every result below. The tool chose the inputs on every cycle by itself.

DesignBounded check, 20 cyclesInduction
CleanPASSEDPASSED: the rule holds for all time
+BUG=1FAILED at step 9not needed
+BUG=2FAILED at step 3not needed

This is the +BUG=1 run (trimmed):

##   0:00:00  Solver: z3
##   0:00:00  Checking assertions in step 0..
...
##   0:00:00  Checking assertions in step 9..
##   0:00:00  BMC failed!
##   0:00:00  Assert failed in tav_fv_top: tav_formal_vs_sim_fv.sv:61.35-61.57
##   0:00:00  Status: FAILED

And this is the counterexample it wrote out, cycle by cycle after reset:

Cyclereqcfg_wrcfg_data
1011A
20015
3001C
40013
5011F
60110
7110A
80000

Nobody told the tool the unlock code. It found A, 5, C, 3, F, 0 on its own, because those are the only writes that lead to a state where the rule can break. Then both request on cycle 7, and on the next cycle both are granted.

The code

This file, with its expected output, is also on GitHub in the course repo: tav-verification-basics, 08-formal-vs-functional.

Two files. The UVM bench, every name starting with tav_: paste it into EDA Playground, pick Synopsys VCS with UVM 1.2, set the top module to tav_top, and run. Add +BUG=1 or +BUG=2 to switch a bug on.

// tav_formal_vs_sim_uvm.sv : what random simulation reaches, and replaying a formal counterexample (UVM 1.2)
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 24 Nov 2026.
// The Art of Verification | theartofverification.com
//
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
// Paste this whole file into testbench.sv and leave design.sv empty.
//
// The design is a 2-requester arbiter with a debug mode. Debug mode turns on after six
// configuration writes in a row: A, 5, C, 3, F, 0. The safety rule is: never grant both.
// One run does two phases, with a reset before each:
//   RANDOM  100,000 cycles of random requests and random config writes (cfg_wr 50%)
//   REPLAY  the 8-cycle counterexample a formal tool found for +BUG=1, driven as a directed test
// After each phase the scoreboard prints mismatches against the spec model, cycles where both
// were granted, assertion fails reported (A_MUTEX stops after 3 per phase), how far the random config writes got into the unlock sequence, and whether debug mode was reached.
//
// Run options (one per run):
//   (none)    the clean design
//   +BUG=1    a deep bug: in debug mode the arbiter grants every request, so both can be granted
//   +BUG=2    a shallow bug: when both request right after an idle cycle and requester 1 won last,
//             both are granted
//
// Spec:
//   gnt is one-hot or zero: never both. A requester alone gets the grant on the next cycle.
//   When both request, the grant goes to the requester that did not win most recently
//   (a grant to a requester alone counts as a win). After reset, requester 0 goes first.
//   Debug mode must not change how requests are granted.
//   rst_n (async, active low) clears everything, including debug mode.

`timescale 1ns/1ns
`include "uvm_macros.svh"
import uvm_pkg::*;

// ---------------------------------------------------------------------------
// The design
// ---------------------------------------------------------------------------
module tav_mutex_arb (input  logic       clk, rst_n,
                      input  logic [1:0] req,
                      input  logic       cfg_wr,
                      input  logic [3:0] cfg_data,
                      output logic [1:0] gnt,
                      input  int         bug);
  // Debug mode unlocks after six config writes in a row: A 5 C 3 F 0
  logic [2:0] step;
  logic       debug_mode;
  logic       last;          // which requester won last time
  logic [3:0] magic;
  always_comb case (step)
    3'd0: magic = 4'hA; 3'd1: magic = 4'h5; 3'd2: magic = 4'hC;
    3'd3: magic = 4'h3; 3'd4: magic = 4'hF; default: magic = 4'h0;
  endcase
  always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
      step <= 0; debug_mode <= 0; last <= 1; gnt <= 0;
    end else begin
      if (cfg_wr && !debug_mode) begin
        if (cfg_data == magic) begin
          if (step == 3'd5) debug_mode <= 1; else step <= step + 1;
        end else step <= (cfg_data == 4'hA) ? 3'd1 : 3'd0;
      end
      if (bug == 1 && debug_mode) gnt <= req;                       // BUG=1
      else if (req == 2'b11) begin
        if (bug == 2 && gnt == 2'b00 && last == 1'b1) gnt <= 2'b11;   // BUG=2
        else begin gnt <= last ? 2'b01 : 2'b10; last <= ~last; end
      end
      else if (req[0]) begin gnt <= 2'b01; last <= 0; end
      else if (req[1]) begin gnt <= 2'b10; last <= 1; end
      else gnt <= 2'b00;
    end
  end
endmodule

// ---------------------------------------------------------------------------
// Interface: pins plus two probes for coverage; the same safety property formal proves
// ---------------------------------------------------------------------------
interface tav_arb_if (input logic clk);
  logic       rst_n, cfg_wr;
  logic [1:0] req, gnt;
  logic [3:0] cfg_data;
  logic [2:0] step;          // probe: how far into the unlock sequence
  logic       debug_mode;    // probe
  clocking mon_cb @(posedge clk);
    default input #1step;
    input rst_n, cfg_wr, req, gnt, cfg_data, step, debug_mode;
  endclocking

  // A bug that fires every few cycles would flood the log (EDA Playground stops at 5000 lines),
  // so A_MUTEX reports its first 3 fails in a phase and then switches itself off.
  // The scoreboard still counts every cycle where both are granted.
  int tav_sva_fails;
  A_MUTEX: assert property (@(posedge clk) disable iff (!rst_n) gnt != 2'b11)
    else begin
      tav_sva_fails++;
      `uvm_error("SVA", $sformatf("A_MUTEX: both requesters granted (req=%b)", $sampled(req)))
      if (tav_sva_fails == 3) begin
        `uvm_info("SVA_OFF", "A_MUTEX switched off for the rest of this phase after 3 fails", UVM_NONE)
        $assertoff(0, A_MUTEX);
      end
    end

  function void tav_sva_rearm();   // called at the start of each phase
    tav_sva_fails = 0;
    $asserton(0, A_MUTEX);
  endfunction
endinterface

// ---------------------------------------------------------------------------
// Transaction: one clock cycle at the pins
// ---------------------------------------------------------------------------
class tav_arb_item extends uvm_sequence_item;
  rand bit [1:0] req;
  rand bit       cfg_wr;
  rand bit [3:0] cfg_data;
  bit            rst;
  constraint c_wr { cfg_wr dist {1 := 50, 0 := 50}; }
  `uvm_object_utils(tav_arb_item)
  function new(string name = "tav_arb_item");
    super.new(name);
  endfunction
endclass

class tav_arb_cycle extends uvm_object;
  bit       rst, cfg_wr, debug_mode;
  bit [1:0] req;
  logic [1:0] gnt;
  bit [3:0] cfg_data;
  bit [2:0] step;
  `uvm_object_utils(tav_arb_cycle)
  function new(string name = "tav_arb_cycle");
    super.new(name);
  endfunction
endclass

// ---------------------------------------------------------------------------
// Sequences
// ---------------------------------------------------------------------------
class tav_arb_base_seq extends uvm_sequence #(tav_arb_item);
  `uvm_object_utils(tav_arb_base_seq)
  function new(string name = "tav_arb_base_seq");
    super.new(name);
  endfunction
  task cyc(bit [1:0] r, bit w, bit [3:0] d);
    req = tav_arb_item::type_id::create("req");
    start_item(req);
    req.req = r; req.cfg_wr = w; req.cfg_data = d;
    finish_item(req);
  endtask
endclass

class tav_reset_seq extends tav_arb_base_seq;
  `uvm_object_utils(tav_reset_seq)
  function new(string name = "tav_reset_seq");
    super.new(name);
  endfunction
  task body();
    req = tav_arb_item::type_id::create("req");
    start_item(req);
    req.rst = 1;
    finish_item(req);
  endtask
endclass

class tav_random_seq extends tav_arb_base_seq;
  `uvm_object_utils(tav_random_seq)
  int unsigned n = 100000;
  function new(string name = "tav_random_seq");
    super.new(name);
  endfunction
  task body();
    repeat (n) begin
      req = tav_arb_item::type_id::create("req");
      start_item(req);
      if (!req.randomize()) `uvm_fatal("RAND", "randomize failed")
      finish_item(req);
    end
  endtask
endclass

// The counterexample from the formal run on +BUG=1, cycle by cycle after reset
class tav_replay_seq extends tav_arb_base_seq;
  `uvm_object_utils(tav_replay_seq)
  function new(string name = "tav_replay_seq");
    super.new(name);
  endfunction
  task body();
    //  req     cfg_wr cfg_data
    cyc(2'b01, 1, 4'hA);
    cyc(2'b00, 1, 4'h5);
    cyc(2'b00, 1, 4'hC);
    cyc(2'b00, 1, 4'h3);
    cyc(2'b01, 1, 4'hF);
    cyc(2'b01, 1, 4'h0);    // sixth write: debug mode turns on
    cyc(2'b11, 0, 4'hA);    // both request
    cyc(2'b00, 0, 4'h0);
  endtask
endclass

// ---------------------------------------------------------------------------
// Driver: drives on the falling edge; idles when there is no item
// ---------------------------------------------------------------------------
class tav_arb_driver extends uvm_driver #(tav_arb_item);
  `uvm_component_utils(tav_arb_driver)
  virtual tav_arb_if vif;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  function void build_phase(uvm_phase phase);
    if (!uvm_config_db#(virtual tav_arb_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no virtual interface")
  endfunction
  task run_phase(uvm_phase phase);
    vif.rst_n <= 0; vif.req <= 0; vif.cfg_wr <= 0; vif.cfg_data <= 0;
    forever begin
      seq_item_port.try_next_item(req);
      if (req == null) begin
        @(negedge vif.clk);
        vif.req <= 0; vif.cfg_wr <= 0;
      end else begin
        if (req.rst) begin
          @(negedge vif.clk);
          vif.rst_n <= 0; vif.req <= 0; vif.cfg_wr <= 0;
          @(negedge vif.clk);
          vif.rst_n <= 1;
        end else begin
          @(negedge vif.clk);
          vif.req <= req.req; vif.cfg_wr <= req.cfg_wr; vif.cfg_data <= req.cfg_data;
        end
        seq_item_port.item_done();
      end
    end
  endtask
endclass

// ---------------------------------------------------------------------------
// Monitor: one snapshot per clock
// ---------------------------------------------------------------------------
class tav_arb_monitor extends uvm_monitor;
  `uvm_component_utils(tav_arb_monitor)
  virtual tav_arb_if vif;
  uvm_analysis_port #(tav_arb_cycle) ap;
  function new(string name, uvm_component parent);
    super.new(name, parent);
    ap = new("ap", this);
  endfunction
  function void build_phase(uvm_phase phase);
    if (!uvm_config_db#(virtual tav_arb_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no virtual interface")
  endfunction
  task run_phase(uvm_phase phase);
    forever begin
      tav_arb_cycle t;
      @(vif.mon_cb);
      t = tav_arb_cycle::type_id::create("t");
      t.rst        = (vif.mon_cb.rst_n !== 1'b1);
      t.req        = vif.mon_cb.req;
      t.cfg_wr     = vif.mon_cb.cfg_wr;
      t.cfg_data   = vif.mon_cb.cfg_data;
      t.gnt        = vif.mon_cb.gnt;
      t.step       = vif.mon_cb.step;
      t.debug_mode = vif.mon_cb.debug_mode;
      ap.write(t);
    end
  endtask
endclass

// ---------------------------------------------------------------------------
// Scoreboard: spec model of the grants, and how deep the stimulus got
// ---------------------------------------------------------------------------
class tav_arb_scoreboard extends uvm_scoreboard;
  `uvm_component_utils(tav_arb_scoreboard)
  uvm_analysis_imp #(tav_arb_cycle, tav_arb_scoreboard) imp;

  bit [1:0] m_gnt;
  bit       m_last;
  int       cycles, mismatches, shown, both, max_step;
  bit       debug_seen;

  function new(string name, uvm_component parent);
    super.new(name, parent);
    imp = new("imp", this);
  endfunction

  function void start_phase(string name);
    `uvm_info("PHASE", name, UVM_NONE)
    cycles = 0; mismatches = 0; shown = 0; both = 0; max_step = 0; debug_seen = 0;
  endfunction

  function void write(tav_arb_cycle t);
    if (t.rst) begin m_gnt = 0; m_last = 1; return; end
    cycles++;
    // how far the config writes have got into the unlock sequence (6 = debug mode)
    if (t.debug_mode) begin debug_seen = 1; max_step = 6; end
    else if (t.step > max_step) max_step = t.step;
    if (t.gnt == 2'b11) both++;
    // check this cycle's grant against the spec model
    if (t.gnt !== m_gnt) begin
      mismatches++;
      if (shown < 3) begin
        shown++;
        `uvm_error("MISMATCH", $sformatf("cycle %0d: gnt=%b, expected %b", cycles, t.gnt, m_gnt))
      end
    end
    // spec model: the grant for the next cycle, from this cycle's requests
    if (t.req == 2'b11) begin m_gnt = m_last ? 2'b01 : 2'b10; m_last = ~m_last; end
    else if (t.req[0])  begin m_gnt = 2'b01; m_last = 0; end
    else if (t.req[1])  begin m_gnt = 2'b10; m_last = 1; end
    else                      m_gnt = 2'b00;
  endfunction

  function void end_phase(string name, int sva);
    `uvm_info("RESULT", $sformatf("%s: cycles=%0d mismatches=%0d both_granted=%0d assertion_fails=%0d%s -> %s",
              name, cycles, mismatches, both, sva, (sva >= 3) ? " (then switched off)" : "",
              (mismatches == 0 && both == 0 && sva == 0) ? "PASS" : "FAIL"), UVM_NONE)
    `uvm_info("DEPTH", $sformatf("%s: deepest unlock step reached=%0d of 6, debug mode reached=%s",
              name, max_step, debug_seen ? "yes" : "no"), UVM_NONE)
  endfunction
endclass

// ---------------------------------------------------------------------------
// Agent, environment, test
// ---------------------------------------------------------------------------
class tav_arb_agent extends uvm_agent;
  `uvm_component_utils(tav_arb_agent)
  uvm_sequencer #(tav_arb_item) sqr;
  tav_arb_driver  drv;
  tav_arb_monitor mon;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  function void build_phase(uvm_phase phase);
    sqr = uvm_sequencer#(tav_arb_item)::type_id::create("sqr", this);
    drv = tav_arb_driver::type_id::create("drv", this);
    mon = tav_arb_monitor::type_id::create("mon", this);
  endfunction
  function void connect_phase(uvm_phase phase);
    drv.seq_item_port.connect(sqr.seq_item_export);
  endfunction
endclass

class tav_arb_env extends uvm_env;
  `uvm_component_utils(tav_arb_env)
  tav_arb_agent      agt;
  tav_arb_scoreboard scb;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  function void build_phase(uvm_phase phase);
    agt = tav_arb_agent::type_id::create("agt", this);
    scb = tav_arb_scoreboard::type_id::create("scb", this);
  endfunction
  function void connect_phase(uvm_phase phase);
    agt.mon.ap.connect(scb.imp);
  endfunction
endclass

class tav_formal_vs_sim_test extends uvm_test;
  `uvm_component_utils(tav_formal_vs_sim_test)
  tav_arb_env env;
  virtual tav_arb_if vif;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  function void build_phase(uvm_phase phase);
    env = tav_arb_env::type_id::create("env", this);
    if (!uvm_config_db#(virtual tav_arb_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no virtual interface")
  endfunction

  task run_one(string name, tav_arb_base_seq s);
    tav_reset_seq r = tav_reset_seq::type_id::create("r");
    r.start(env.agt.sqr);
    vif.tav_sva_rearm();
    env.scb.start_phase(name);
    s.start(env.agt.sqr);
    repeat (2) @(vif.mon_cb);
    #1;
    env.scb.end_phase(name, vif.tav_sva_fails);
  endtask

  task run_phase(uvm_phase phase);
    phase.raise_objection(this);
    run_one("RANDOM 100k cycles", tav_random_seq::type_id::create("rnd"));
    run_one("REPLAY formal trace", tav_replay_seq::type_id::create("rep"));
    phase.drop_objection(this);
  endtask
endclass

// ---------------------------------------------------------------------------
// Top
// ---------------------------------------------------------------------------
module tav_top;
  logic clk = 0;
  int   BUG = 0;
  always #5 clk = ~clk;

  tav_arb_if vif (clk);
  tav_mutex_arb dut (.clk(clk), .rst_n(vif.rst_n), .req(vif.req), .cfg_wr(vif.cfg_wr),
                     .cfg_data(vif.cfg_data), .gnt(vif.gnt), .bug(BUG));
  assign vif.step       = dut.step;
  assign vif.debug_mode = dut.debug_mode;

  initial begin
    void'($value$plusargs("BUG=%d", BUG));
    `uvm_info("TOP", $sformatf("Running with BUG=%0d", BUG), UVM_NONE)
    uvm_config_db#(virtual tav_arb_if)::set(null, "uvm_test_top", "vif", vif);
    uvm_config_db#(virtual tav_arb_if)::set(null, "uvm_test_top.env.agt.*", "vif", vif);
    run_test("tav_formal_vs_sim_test");
  end
endmodule

The formal harness, for Yosys and yosys-smtbmc. It carries its own copy of the arbiter, so it is one file. It is not a simulation testbench, so do not run it in VCS:

// tav_formal_vs_sim_fv.sv : formal harness for tav_mutex_arb (open-source Yosys + yosys-smtbmc)
// Run on Yosys 0.33, yosys-smtbmc and Z3 4.8.12, 24 Nov 2026.
// The Art of Verification | theartofverification.com
//
// This is a formal harness, not a simulation testbench: run it with Yosys, not VCS.
// It carries its own copy of tav_mutex_arb, identical to the one in tav_formal_vs_sim_uvm.sv.
// Run (Yosys 0.33, Z3), with BUG = 0, 1 or 2:
//   yosys -q -p "read_verilog -sv -formal tav_formal_vs_sim_fv.sv; \
//                chparam -set BUG 1 tav_fv_top; prep -top tav_fv_top; async2sync; dffunmap; \
//                write_smt2 -wires arb.smt2"
//   yosys-smtbmc -s z3 -t 20 --dump-vcd cex.vcd arb.smt2      (bounded check, 20 cycles)
//   yosys-smtbmc -s z3 -i -t 20 arb.smt2                      (induction: a full proof)
// The tool chooses req, cfg_wr and cfg_data freely on every cycle. Reset is held for cycle 0.

module tav_mutex_arb (input  logic       clk, rst_n,
                      input  logic [1:0] req,
                      input  logic       cfg_wr,
                      input  logic [3:0] cfg_data,
                      output logic [1:0] gnt,
                      input  int         bug);
  // Debug mode unlocks after six config writes in a row: A 5 C 3 F 0
  logic [2:0] step;
  logic       debug_mode;
  logic       last;          // which requester won last time
  logic [3:0] magic;
  always_comb case (step)
    3'd0: magic = 4'hA; 3'd1: magic = 4'h5; 3'd2: magic = 4'hC;
    3'd3: magic = 4'h3; 3'd4: magic = 4'hF; default: magic = 4'h0;
  endcase
  always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
      step <= 0; debug_mode <= 0; last <= 1; gnt <= 0;
    end else begin
      if (cfg_wr && !debug_mode) begin
        if (cfg_data == magic) begin
          if (step == 3'd5) debug_mode <= 1; else step <= step + 1;
        end else step <= (cfg_data == 4'hA) ? 3'd1 : 3'd0;
      end
      if (bug == 1 && debug_mode) gnt <= req;                       // BUG=1
      else if (req == 2'b11) begin
        if (bug == 2 && gnt == 2'b00 && last == 1'b1) gnt <= 2'b11;   // BUG=2
        else begin gnt <= last ? 2'b01 : 2'b10; last <= ~last; end
      end
      else if (req[0]) begin gnt <= 2'b01; last <= 0; end
      else if (req[1]) begin gnt <= 2'b10; last <= 1; end
      else gnt <= 2'b00;
    end
  end
endmodule

module tav_fv_top #(parameter int BUG = 0)
  (input logic clk, input logic [1:0] req, input logic cfg_wr, input logic [3:0] cfg_data);
  logic rst_n;
  logic [1:0] gnt;
  reg init = 1;
  always @(posedge clk) init <= 0;
  assign rst_n = !init;
  tav_mutex_arb dut (.clk(clk), .rst_n(rst_n), .req(req), .cfg_wr(cfg_wr), .cfg_data(cfg_data),
                     .gnt(gnt), .bug(BUG));
  // the same rule as A_MUTEX in the UVM bench: never grant both
  always @(posedge clk) if (!init) assert (gnt != 2'b11);

`ifndef FORMAL
  // Yosys defines FORMAL for read_verilog -formal. Any simulator lands here instead.
  initial $fatal(1, "tav_formal_vs_sim_fv.sv is a formal harness: run it with Yosys and yosys-smtbmc (see the header), not in a simulator. For VCS, use tav_formal_vs_sim_uvm.sv.");
`endif
endmodule

What output should you expect from simulation?

UVM_INFO testbench.sv(309) @ 1000036: uvm_test_top.env.scb [RESULT] RANDOM 100k cycles: cycles=100002 mismatches=0 both_granted=0 assertion_fails=0                     -> PASS
UVM_INFO testbench.sv(312) @ 1000036: uvm_test_top.env.scb [DEPTH] RANDOM 100k cycles: deepest unlock step reached=4 of 6, debug mode reached= no
UVM_INFO testbench.sv(309) @ 1000156: uvm_test_top.env.scb [RESULT] REPLAY formal trace: cycles=10 mismatches=0 both_granted=0 assertion_fails=0                     -> PASS
UVM_INFO testbench.sv(312) @ 1000156: uvm_test_top.env.scb [DEPTH] REPLAY formal trace: deepest unlock step reached=6 of 6, debug mode reached=yes

With +BUG=1, the random phase still passes. The REPLAY phase prints:

UVM_ERROR testbench.sv(298) @ 1000145: uvm_test_top.env.scb [MISMATCH] cycle 9: gnt=11, expected 10
"testbench.sv", 89: tav_top.vif.A_MUTEX: started at 1000145ns failed at 1000145ns
	Offending '(gnt != 2'b11)'
UVM_ERROR testbench.sv(92) @ 1000145: reporter [SVA] A_MUTEX: both requesters granted (req=00)
UVM_INFO testbench.sv(309) @ 1000156: uvm_test_top.env.scb [RESULT] REPLAY formal trace: cycles=10 mismatches=1 both_granted=1 assertion_fails=1                     -> FAIL
UVM_INFO testbench.sv(312) @ 1000156: uvm_test_top.env.scb [DEPTH] REPLAY formal trace: deepest unlock step reached=6 of 6, debug mode reached=yes

With +BUG=2, the random phase fails early, and the assertion switches itself off after three fails:

UVM_ERROR testbench.sv(298) @ 305: uvm_test_top.env.scb [MISMATCH] cycle 29: gnt=11, expected 01
...
UVM_INFO testbench.sv(94) @ 465: reporter [SVA_OFF] A_MUTEX switched off for the rest of this phase after 3 fails
UVM_INFO testbench.sv(309) @ 1000036: uvm_test_top.env.scb [RESULT] RANDOM 100k cycles: cycles=100002 mismatches=4366 both_granted=3277 assertion_fails=3 (then switched off) -> FAIL
UVM_INFO testbench.sv(312) @ 1000036: uvm_test_top.env.scb [DEPTH] RANDOM 100k cycles: deepest unlock step reached=4 of 6, debug mode reached= no
UVM_INFO testbench.sv(309) @ 1000156: uvm_test_top.env.scb [RESULT] REPLAY formal trace: cycles=10 mismatches=0 both_granted=0 assertion_fails=0                     -> PASS
RunRANDOM, 100k cyclesDeepest unlock stepREPLAY formal trace
No bugPASS4 of 6, debug mode not reachedPASS
+BUG=1 (deep)PASS: the bug never fired4 of 6, debug mode not reachedFAIL at cycle 9: gnt=11, expected 10
+BUG=2 (shallow)FAIL from cycle 29: both granted on 3,277 cycles4 of 6, debug mode not reachedPASS: this trace was built for +BUG=1

Read the table one row at a time. With no bug, both phases pass. That row also shows the limit of random stimulus. Half of the 100,000 cycles carried a random config write, and they still got only to step 4 of the 6-step code. Debug mode was never reached.

With +BUG=1, the random phase passes. The bug is real, but it sits behind debug mode, and random writes never got there. So the regression is green on a broken design. The replayed formal trace turns debug mode on with its sixth write and fails on cycle 9, the same depth formal reported.

With +BUG=2, random simulation finds the bug fast: the first failure is on cycle 29, and both requesters were granted on 3,277 cycles of the run. Mismatches are higher, 4,366, because after each bad grant the design’s turn bit no longer matches the model, so some later tie grants differ too. The replayed trace passes here, because it was built to hit +BUG=1. A counterexample is a test for one failure, not a general test.

So the two methods failed in opposite places. Random simulation caught the shallow bug within 29 cycles and was blind to the deep one. Formal found both, at steps 3 and 9, and proved the clean design safe for all time. The UVM bench then turned the formal trace into a directed test that will catch +BUG=1 in every regression from now on.

Results grid for the arbiter. No bug: random simulation, formal and replay all pass, with formal proving the rule for all time. +BUG=1: random simulation passes and misses the bug, formal fails at step 9, the UVM replay fails at cycle 9 with gnt=11. +BUG=2: random simulation fails from cycle 29 with both granted on 3,277 cycles, formal fails at step 3, and the replay passes because its trace was built for +BUG=1.
What each method reported, from the VCS logs and the Yosys runs.

What are the most common mistakes with formal and simulation?

MistakeWhat it looks likeFix
Treating a passing regression as a proof“All tests pass, so the arbiter is safe”Simulation shows no failure in what ran. Ask what it never reached, and cover it
Running formal on the whole chipHours of run time and no answerUse formal on small, control-heavy blocks. Simulate the rest
Too few assumptionsFormal reports failures with inputs the real system never sendsAdd assumptions for the input protocol, and review each one
Too many assumptionsFormal proves everything, because the assumptions ruled out the bugCheck every assumption against the spec. Cover the scenarios you care about to show they are still reachable
A bounded pass read as a proof“Formal passed” after a 10-cycle check on a bug that needs 12Know the depth. Use induction, or a depth longer than the deepest path you care about
Writing properties from the RTLThe property copies the code, bugs includedWrite properties from the spec, before you read the RTL
Throwing the counterexample awayThe bug is fixed and the trace is lostReplay it as a directed simulation test and keep it in regression
Separate rules in each worldOne assertion in simulation, a different one in formalShare the same SVA between both
An assertion that reports every failA bug that fires every few cycles fills the log. My first +BUG=2 run hit EDA Playground’s 5,000-line limit and never printed its summaryReport the first few fails, then switch the assertion off with $assertoff, and keep counting in the scoreboard

Can AI write formal properties?

Yes, and formal is the best place to check them, because a formal tool tells you at once whether a property holds on the correct design and fails on a broken one. That is a much harder test than reading the property and nodding.

Task: give an assistant only the spec, and ask for properties in a form open-source Yosys accepts. Then run them through formal against the clean design and both bugs.

Tool: Claude, in Anthropic’s Cowork app, 24 Nov 2026, in a fresh session that had never seen the RTL, the bench or the bug list.

Prompt (shortened):

You are a senior formal verification engineer. Write the properties for this block from its spec.
Block: a 2-requester arbiter. Ports: clk, rst_n, req[1:0], cfg_wr, cfg_data[3:0], gnt[1:0] (registered).
Spec:
- gnt is one-hot or zero: never both.
- gnt is registered: it answers the requests from the previous cycle.
- A requester alone gets the grant on the next cycle.
- When both request, the grant alternates between them, starting with requester 0 after reset.
- With no request, gnt is 0 on the next cycle.
- Debug mode turns on after six config writes in a row: A, 5, C, 3, F, 0.
  Debug mode must not change how requests are granted.
Write plain immediate assertions inside always @(posedge clk) blocks, in one module
tav_arb_props, keeping previous-cycle values in your own registers (no $past, no |->).
Then say which spec line was hardest to turn into a property, and why.

Output (excerpt):

// reference model: predicts gnt from the previous cycle's req and a tie-break bit
if (req == 2'b11) turn <= ~turn;   // alternate only on ties
...
wire [1:0] exp_gnt = (prev_req == 2'b11) ? (prev_turn ? 2'b10 : 2'b01) : prev_req;
...
// Spec: "gnt is one-hot or zero: never both"
assert (gnt != 2'b11);
// Spec: "When both request, the grant alternates, starting with requester 0 after reset"
if (prev_req == 2'b11) assert (gnt == (prev_turn ? 2'b10 : 2'b01));
// Spec: "Debug mode must not change how requests are granted"
assert (gnt == exp_gnt);

It also added a note: “I read ‘alternates’ as alternating between tie cycles only. If your RTL is a classic round-robin that remembers the last grant of any kind, the turn update has to change to follow the last grant.”

How I validated it: I ran its module through the same formal flow, Yosys 0.33 with yosys-smtbmc and Z3, 20 cycles.

PropertiesClean design+BUG=1+BUG=2
As writtenFAILED at step 4 (the tie rule)FAILEDFAILED
With its suggested one-line change to the tie rulePASSEDFAILED at step 9FAILED at step 3

The failure on the clean design was not a design bug. My spec line was ambiguous. My RTL counts a grant to a requester alone as a win, so it decides the next tie; the assistant read the line as only ties counting. Formal found the disagreement in four cycles, and the assistant had already named it in its note. I have since rewritten that spec line to say exactly which reading is meant.

With the tie rule fixed, its properties pass on the clean design and catch both bugs, at the same depths as my own harness: 9 cycles for the deep one and 3 for the shallow one.

Where it fell short:

  • It picked one reading of an ambiguous line and built on it. It did warn about it, which is the right habit, but the properties as written would have failed a correct design. Always run AI properties against a known-good design first.
  • Its debug-mode check needed its own copy of the unlock logic. That copy is a second implementation that could be wrong in the same way as the RTL. Its catch-all model check, gnt == exp_gnt in every state, is the stronger property, because it does not care how debug mode is entered.
  • It wrote what it was asked for and nothing more. No cover properties to show debug mode is reachable. Without one, a property guarded by dbg_mode could pass simply because the tool never reached it.

Use an assistant to draft properties fast, then let a formal tool and a known-good design decide which ones are right.

Interview questions on formal and functional verification

  • What is the difference between formal and functional verification? Functional verification simulates chosen stimulus and checks the results, so it covers only what was run. Formal uses a solver to check a property against every possible input sequence, up to a depth or for all time.
  • What is a counterexample? The exact input sequence, cycle by cycle, that makes a property fail. A formal tool gives you the shortest one it finds, and it makes a good directed test.
  • What does a bounded model check prove? That no input sequence breaks the property within N cycles of reset. A bug that needs more than N cycles is not covered.
  • How do you get a full proof? With an unbounded method such as induction: show the property holds after reset, and that holding on one cycle forces it to hold on the next. Sometimes extra helper properties are needed for induction to succeed.
  • Why can’t simulation prove a design is bug-free? It only explores the cycles it ran. In a state space with millions of reachable states, random stimulus samples a small part.
  • What is an over-constrained formal setup? One where the assumptions are so strong they rule out real input sequences, including the ones that would show a bug. The proof passes, but it proves nothing useful.
  • Which blocks are good formal candidates? Arbiters, FIFO and pointer logic, state machines, protocol interfaces, clock-domain handshakes, and anything with a hidden mode or a deep corner.
  • How do formal and simulation work together on a project? They share SVA properties, formal counterexamples become directed simulation tests, and formal helps close coverage by reaching or proving unreachable the holes simulation left.

Practice challenge

Try these with the demo files:

  1. Run the formal harness on +BUG=1 with a depth of 8 instead of 20 (-t 8). What does the tool report, and why is that result misleading?
  2. Add a cover property to the formal harness that is true when debug mode is on. How many cycles does the tool need to reach it?
  3. Add an assumption to the harness that cfg_data is never 0. Does +BUG=1 still fail? What has the assumption hidden?
  4. Raise the random phase to 1,000,000 cycles. How deep into the unlock sequence does simulation get now?

If the third challenge made the bug disappear, you have just seen an over-constrained proof, the most dangerous kind of pass.

Frequently asked questions

What is the difference between formal and functional verification?

Functional verification runs simulations with chosen stimulus and checks the results, so it covers only the cases you ran. Formal verification uses mathematics and a solver to check a property against every possible input, either up to a number of cycles or for all time.

Is formal verification better than simulation?

Neither is better in general. Formal is exhaustive but works best on small, control-heavy blocks. Simulation scales to whole chips and real software but only samples the possible behaviour. Most projects use both.

What is bounded model checking?

A formal method that checks every possible input sequence up to a fixed number of cycles after reset. If the property can fail within that bound, it returns a counterexample. A pass says nothing about deeper bugs.

Can I try formal verification for free?

Yes. Open-source Yosys with yosys-smtbmc and a solver such as Z3 can run bounded checks and induction on SystemVerilog designs with simple immediate assertions. Commercial tools support full SVA and much larger designs.

Do formal and simulation use the same properties?

They can, and they should. The same SVA assertion can run as a checker in simulation and as a proof target in formal, so you write each rule once.

References and related lessons

References

Related lessons