Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 37 ยท Synthesis and Verification

Assertions (SVA) and Functional Coverage

Write SystemVerilog assertions with immediate and concurrent assert, property, sequence, implication and delays, and measure test quality with cover and covergroups.

A testbench checks outputs at the points where you remembered to look. Assertions turn design rules into permanent, always-on checks: "grant is never given to two requesters", "every request is acknowledged within 4 cycles", "FIFO never underflows". They fire at the exact cycle the rule breaks, even deep inside the design, which makes bugs far easier to locate than a wrong value found a thousand cycles later at an output.

This lesson covers immediate and concurrent assertions in SystemVerilog Assertions (SVA), sequences and properties, the most common operators, cover and coverage. SVA runs in a SystemVerilog simulator or formal tool, neither of which is available on this site, so the SVA code is read-only. The C programs are labelled stand-ins that check the same temporal rules over a recorded trace.

Immediate assertions

An immediate assertion is a boolean check evaluated when the statement executes, like an if with a built-in failure message:

SYSTEMVERILOG
always_comb begin
  assert (!(grant_a && grant_b))
    else $error("both grants active at %0t", $time);
end

Variants: assert (check), assume (constraint for formal tools), cover (record that a case occurred), plus $fatal, $error, $warning and $info for severity. Immediate assertions are good inside procedural code but see only the current values; they cannot talk about "later".

Concurrent assertions

A concurrent assertion describes behaviour over time and is checked at every clock tick by the simulator, independently of any procedural code:

SYSTEMVERILOG
property req_gets_ack;
  @(posedge clk) disable iff (!rst_n)
    req |-> ##[1:4] ack;
endproperty

assert property (req_gets_ack)
  else $error("req was not acknowledged within 4 cycles");

Reading it:

  • @(posedge clk): sample signals at each rising edge.
  • disable iff (!rst_n): switch the check off during reset.
  • req |-> ##[1:4] ack: whenever req is true (the antecedent), ack must become true between 1 and 4 cycles later (the consequent).

If the antecedent is false the property passes vacuously; this matters when you read coverage: a property that never triggers proves nothing.

Sequences and operators

A sequence is a series of boolean events over time:

SYSTEMVERILOG
sequence handshake;
  valid && !ready ##1 valid && ready;     // valid stays high, ready arrives next cycle
endsequence

The operators you will use most:

Operator Meaning
##n exactly n cycles later; ##[m:n] between m and n cycles
a |-> b overlapped implication: if a now then b starts in the same cycle
a |=> b non-overlapped implication: b starts in the next cycle (same as a |-> ##1 b)
s[*n] consecutive repetition: s for n cycles; [*m:n] a range
s[->n] go-to repetition: n occurrences, not necessarily consecutive, ending on the last
$rose(x), $fell(x) x just went 0 to 1 or 1 to 0
$stable(x) x unchanged since last clock
$past(x, n) the value of x n cycles ago
a throughout s a is true for the whole of sequence s
first_match(s) stop at the first completion of s

Common useful properties:

SYSTEMVERILOG
// data must hold while valid is waiting for ready
assert property (@(posedge clk) disable iff (!rst_n)
  valid && !ready |=> valid && $stable(data));

// grants are mutually exclusive
assert property (@(posedge clk) $onehot0({grant_a, grant_b, grant_c}));

// no read when empty and no write when full
assert property (@(posedge clk) !(rd_en && empty));
assert property (@(posedge clk) !(wr_en && full));

// after reset is released, state is IDLE
assert property (@(posedge clk) $rose(rst_n) |-> state == IDLE);

What a checker really does

Simulators implement a concurrent assertion as a little state machine that watches the signals each clock. Whenever the antecedent matches, a new thread starts and waits for the consequent; the property fails if its time window expires first. This C stand-in does the same over a recorded trace for req |-> ##[1:3] ack:

C Stand-in: checking req |-> ##[1:3] ack over a trace

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Three requests (cycles 1, 9 and 10) are acknowledged in time; one ack at cycle 11 serves both of the last two requests, because each request has its own window. The request at cycle 4 gets no ack in cycles 5 to 7 and fails. The vacuous count is the number of cycles with no request, the number you read from an assertion coverage report.

Assertion tips

  • Reset gating. Always add disable iff (reset) so reset does not cause spurious failures.
  • Binding. Put assertions in a separate module and attach them without editing the design file:
SYSTEMVERILOG
bind fifo fifo_checker chk (.*);       // instantiates chk inside every fifo
  • Formal verification. The same properties can be proven for all possible inputs by a formal tool (SymbiYosys, JasperGold, VC Formal), not just the ones a simulation tried. Formal works best on control logic: arbiters, FSMs, FIFO pointers.
  • Keep them small. One rule per assertion; name them (a_req_ack: assert property (...)) so failures are easy to read.
  • Synthesizable or not? Assertions are ignored by synthesis in normal flows (some FPGA tools can synthesize a few as run-time monitors).

Cover properties and coverage

cover property records how often a scenario happened, proving the tests reach it:

SYSTEMVERILOG
cover property (@(posedge clk) req ##[1:4] ack);       // did a normal handshake occur?
cover property (@(posedge clk) full && wr_en);         // did we try to write when full?

For data-oriented coverage use a covergroup (previewed in classes and randomization):

SYSTEMVERILOG
covergroup alu_cg @(posedge clk);
  op:   coverpoint opcode { bins arith[] = {ADD, SUB}; bins logic_[] = {AND_OP, OR_OP}; }
  size: coverpoint len    { bins one = {1}; bins few = {[2:4]}; bins many = {[5:$]}; }
  op_x_size: cross op, size;
endgroup

Verification closes by checking code coverage (lines, branches, toggles, FSM states, automatically collected) and functional coverage (the covergroups and cover properties you wrote). Passing tests with low coverage means the tests are weak, not that the design is good.

The second stand-in implements the covergroup idea: values from a (simulated) random test fall into bins and the report lists which bins were hit and which are holes.

C Stand-in: functional coverage bins and holes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The hole in len=5+ shows the test never produced a long packet; the fix is a constraint change or a directed test, as described in the classes lesson.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does req |-> ##[1:4] ack require?

    The antecedent req starts a window; ack must be true in at least one of the next 1 to 4 cycles.
  2. 2. What is the difference between |-> and |=>?

    a |=> b is shorthand for a |-> ##1 b: the non-overlapped form shifts the consequent one cycle later.
  3. 3. Why use disable iff (!rst_n) in a property?

    During reset the signals are not meaningful, so checks are turned off until reset is released.
  4. 4. A property whose antecedent never occurs in a test is reported as passed. What does that tell you?

    Vacuous passes prove nothing. Pair assertions with cover properties to see that the situation was actually exercised.

Summary

  • Immediate assertions check the current values; concurrent assertions (assert property) check behaviour over time at each clock.
  • Core operators: ##n, |->, |=>, [*n], $rose, $stable, $past; gate with disable iff.
  • bind attaches checkers without changing the design; formal tools can prove properties for all inputs.
  • Use cover property and covergroups to measure what tests exercised and find holes.

Next: Project: A Small ALU