Assertions (SVA)

https://www.doulos.com/knowhow/systemverilog/systemverilog-tutorials/ Q. One confusing thing is whether I can use $rose or $fall within property? B/c I saw some examples in the past using it but GPT said it’s not allowed so I’m confused. But I’ve seen some course slide including it within property so I dunno… Q. Where can ‘immediate assertion’ go? Is this the same for ‘concurrent’ assertion? —> Immediate assertions in SystemVerilog can be placed almost anywhere within procedural blocks. + functions/task —> concurrent assertions cannot be used directly inside procedural blocks like always or initial blocks. —> They can be written as standalone assert property statements outside procedural blocks. —> Concurrent assertions require a clocking event (@posedge clk, for example) to manage timing, as they evaluate over multiple cycles.
assert property (@(posedge clk) sequence_name) else $error("Property failed");
assert property (property_name) else $error("Property failed");
property example_property; 
	@(posedge clk) rising_edge_sequence |-> d; 
endproperty 

assert property (example_property) else $error("Property failed");

sequence s1;
    a1 ##2 b2;
endsequence
Why? Immediate assertions don’t involve time or clock cycles and are expected to execute in a single simulation step. Hence, they’re restricted to places where timing is not a factor, such as inside procedural blocks (e.g., always, initial, functions, and tasks). In contrast, concurrent assertions can’t be placed in these procedural blocks since they need a clocking context for multi-cycle evaluation. What does ‘assert’ mean? —> state a fact or belief confidently and forcefully. —> in programming, assertion statement should always evaluate to ‘true’ at that point in code execution ‘Assert’ vs ‘verify’ Assert stops while verify continues even if the condition is not met. ‘Immediate Assertion’ vs ‘if’ An assertion is more compact than an if-statement. However, note that the logic is reversed compared to the if-statement above. // Simple (immediate) assert statement assert(<expression>);
module design_ex (input clk, reqA, reqB, in);
  always @(posedge clk) begin 
    assert (reqA || reqB) else $error("assertion failed");
    assert (in == 0) else $warning("assertion warning for in == 0");
  end
endmodule
Recall, in many testbench generations, you specifically create some input signals and checked whether the output was what you expected. This form of testing is called ‘assertion’ testing where we explicitly define input vectors and expected outputs so that the testbench systematically determines whether any given test passes or fails. —> The idea is to assert that the signal should never be 'X'. If it is, the assertion will fail, indicating an issue. Assertion Definition An assertion is a statement about your design that you expect to be true always. - Formal Verification, Erik Seligman et al. description of a property of the design as per specification Description of property as per design specification When do you use Assertions? “The most familiar assertions look for errors such as two signals that should be mutually exclusive or a request that was never followed by a grant. These error checks should stop the simulation as soon as they detect a problem. Assertions can also check arbitration algorithms, FIFOs, and other hardware. These are coded with the assert property statement. “ Assertions help describe complex behaviors about the design requirements and properties in a clear & concise manner “Assertions rely on expressions evaluating true or false, which are one-bit values. It’s up to you to write boolean expressions that resolve to 1-bit (like equality), or iterate over the bits.” From Dave in Simens EDA You check
  1. Timing
  2. Sequence
  3. Data integrity
  4. State machine validation
  5. Protocol compliance
  6. Coverage points
  7. Etc...
Types of Assertion
  1. Immediate assertion - think of immediate assertions as a simple if statement. —> Evaluated immediately
    1. Def: These assertions do not depend upon a clock edge or reset.
    2. equivalent to an if condition within an always_comb block.
  2. Concurrent assertion - span time and are triggered (evaluated) relative to a clock edge
    1. Uses property which differentiates it from immediate assertion
    2. think of concurrent assertions as if statements within an always_ff block.
    3. The keyword property distinguishes an immediate from a concurrent assertion.
—> basically one needs clocking context (concurrent) while the other don’t need it (immediate) Timing and Sequence —> related but distinct concept in SVA Timing Def: specific moments or intervals at which events occur relative to the clock cycles in a digital system. Checks for delays, durations, or specific time windows. Ex) Timing ===========
property p_delay;
    @(posedge clk) start_signal ##[5:10] stop_signal;
endproperty

assert property (p_delay);
—> Here, the stop_signal must occur between 5 and 10 clock cycles after the start_signal. ============= Sequence Def: The order in which events(signals) occur. It's about specifying how different events(signals) are related in terms of their order of occurrence. Checks a series of events that must happen in a specific order. A sequence can be thought of a simple boolean expression that gets evaluated on a single clock edge or it can be a sequence of events that gets evaluated across multiple cycles. Sequences can be composed of Boolean expressions, temporal operators, and other sequences, allowing for the description of complex timing relationships and conditions. Sequences by themselves do not assert correctness or failure; they simply describe a pattern of behavior over time. In general, it is a good idea to define the clocks in property definitions and keep the sequences independent of the clocks. This will help increase the re-use of the basic sequence definitions. Ex)
sequence seq;
	a ##2 b; 
endsequence;
property p;
	@(posedge clk) seq;  //DEFINE THE CLK IN PROPERTY
endproperty
USE property name
a_1 : assert property(p); // assertion name
OR you can just have the property right after the assertion valid_gnt_chk: assert property(@posdege (clk) disable iff (rst) (vld && dat == 8'h55) |-> ##3 ack); / A property may involve checking of one or more sequential behaviors beginning at various times. A property can hence be constructed using multiple sequences combined logically or sequentially. Property asserts a certain condition or a set of conditions must be true or false. Properties are used to define assertions or assumptions about the behavior of the system. @posedge(tst_signal) waits until a rising edge(=posedge) event is seen on the tst_signal. —> this make sure that assertion checks are synchronized with the clock Is there a difference between $rose(tst_signal) and @posedge(tst_signal )? Yes, there is a difference. @posedge(tst_signal) waits until a rising edge event is seen on the tst_signal. However, $rose() is a system task that checks if the sampled value of the signal changed to 1 between previous sample and the current sample (Previous sample could be a 0/x/z). Accordingly, $rose(tst_signal) only returns true if there are at least two sampled values. sequence s_a_eq_b; @posedge(clk) (a ==b); endsequence —> In the given sequence s_a_eq_b, the use of @posedge(clk) indicates that the condition (a == b) should be evaluated at every positive edge of the clock clk. / we want to check some condition (a==b) but we want it to be synchronized with clock :) / WHEN to check the CONDITION —> the sequence s_a_eq_b describes a condition (a == b) that is to be checked at the positive edge of the clock (@posedge(clk)). By itself, this sequence just describes a series of events or conditions to be observed; it does not assert whether the condition being true or false meets or violates the design's requirements. —> To actually check for correctness or to enforce that a should equal b at every positive clock edge, you would need to use this sequence within a property and then assert that property. property p_a_eq_b; assert s_a_eq_b; endproperty; assert property (p_a_eq_b) else $error("a does not equal b at the positive edge of clk"); ============ sequence s_ab; a ##1 b; // b is high one cycle after a is high endsequence sequence s_cd; c ##2 d; // d is high 2 cycle after c is high endsequence property p; @(posedge clk) s_ab (and, or, not, intersect, within, first_match) s_cd; endproperty Here, @posedge (clk) is moved within property. Q. What’s the correct way? Use @posedge(clk) within sequence or within property? —> I think… in many cases @(posedge clk) is used within property… but it’s not wrong to use it within sequence… Q. What can be in sequence? Ex) Sequence ============ sequence s_start_then_stop; start_signal ##1 stop_signal; endsequence Or property prop_direct_sequence; @(posedge clk) a ##1 b; endproperty assert property (prop_direct_sequence); ============= ============== Property The parents statement translates into two properties —> formal properties form assertion!!
  1. It is like a ‘specialized functions’ used in assertions, specifically designed to check for timing and sequence-related conditions in your hardware design.
  2. Properties are formulated in a way that expresses complex timing relationships and sequences of events.
    1. A property in SVA is defined using sequences and temporal operators to describe complex timing and ordering relationships between events.
    2. You build property using ‘sequence’ blocks
  3. Properties are used within assertions.
  4. Reusable definitions : Like functions, properties define a behavior or condition that can be reused.
  5. They are used in assert, assume, and cover statements.
  6. Effectively combines ‘boolean logic’ with ‘’temporal logic’
  7. ✅ Summary : Formal way of representing temporal and boolean logic using function.
Property Example ========== property check_sequence; @(posedge clk) start_signal ##[2:5] end_signal; // the property checks that end_signal occurs 2 to 5 clock cycles after start_signal. endproperty assert property (check_sequence); ============ Q. How is this different from ‘sequence’? Conclusion Timing and sequence are often interconnected in digital design. A sequence dictates the order of events, while timing specifies when these events occur. In many assertions, both concepts are used together. For instance, you might assert that a certain sequence of events occurs within a specific timing window. However, they represent different checks in assertions. A sequence doesn't necessarily specify exact timing, and timing checks don't always dictate a specific order. —> but ‘order’ is defined by time though…? =======

Operator

  1. Delay operator
    1. ##
  2. Implication operator - implies sth happens x cycle later if RHS is true.
    1. Boolean (time) ??
OperatorUse
##n ##[m:n]Delay operators - Fixed time interval and Time inteval range
!, ||, &&Boolean operators
|->Overlapping implication
|=>Nonoverlapping implication
not, or, andProperty operators
Repetition operator
OperatorUse
[*n] [*m:n]Continuous repetition operator. The expression repeats continuously for the specified range of cycles.
[->n] [->m:n]Go to repetition operator. Indicates there's one or more delay cycles between each repetition of the expression. a[->3] is equivalent to (!a[*0:$] ##1 a) [*3]
[=n] [=m:n]Non-consecutive implication. a[=3] is equivalent to (!a[*0:$] ##1 a ##1 !a[*0:$]) [*3]
===== Temporal Operators in SVA
  1. Delay Operator ##
  2. Implication Operator (+overlapping implication operator)
  3. Edge detection
  4. Repetition operator
    1. Consecutive repetition [*n] - true for n cycles
    2. Non consecutive repetition [->]
    3. [=]
  5. Edge Detection
  6. Enclosed Duration
Repetition operator Consecutive repetition operator
  • Indicates that the sequence repeats itself a specified number of times.
    • s1 ##2 s2 [*4] ##5 s3 is same as s1 ##2 (s2 ##1 s2 ##1 s2 ##1 s2) ##5 s3 or, or simply s1 ##2 s2 ##1 s2 ##1 s2 ##1 s2 ##5 s3
Implication (Operator) Purpose : For checking ‘sequence’ (?) s1 |-> s2 ; if s1 is true, then s2 must be true. Types of implication operator
  1. Non-overlapping (|=>) : The RHS is evaluated one clock cycle after LHS is true
  2. Overlapping (|->) : The RHS is evaluated from the same cycle LHS is true
Q. Can implication operator be used in sequences? No, it can be used only in properties. It is a precondition match to evaluate property expressions. Implication operators express a relationship between an antecedent (left-hand side) and a consequent (right-hand side), which is handled in properties rather than sequences. They are intended for use within properties to assert a relationship between sequences or conditions. —> Allowed Operators in Sequences Within a sequence, you can use these temporal operators to define patterns:
  • ## (Cycle Delay): Specifies the number of cycles between events.
  • [ ] (Repetition): Specifies how many times an event or condition must hold.
    • For example, a[*3] means a should hold for 3 cycles.
  • or, and, intersect: Used for logical combinations of events within the sequence.
https://www.systemverilog.io/verification/sva-basics/#cover-property Ex)? // FIFO level cannot go down without a pop. property FifoLevelCheck; @(posedge clk) disable iff (rst) (!rd_vld) |-> ##1 (fifo_level >= $past(fifo_level)); endproperty FifoLevelCheck_C: assume property (FifoLevelCheck); Example of SVA =============== /* File name : bus_arb_assertions.svh */ prop_a_cnt: assert property ( @(posedge clk) disable iff (rst) a_vld |-> (a_cnt == $past(a_cnt) + 1)); property valid_state; @(posedge clk) disable iff (rst) $onehot0(current_state); endproperty prop_valid_state: assert property (valid_state); // ... other assertions and cover props =============== “if c is high on a clock cycle, then on next cycle, value of a and b is equal” ap_a_eq_b : assert property((@posedge clk) c |=> (a == b)); ** use of EXCLUSIVE OR - XOR —> both signals can’t be the same A signal is flagged for exactly once clock (err) |-> (ctrl_incr ^ $past(ctrl_incr)) ===== There’s 2 assertions here.
  1. Assertion ‘prop_a_cnt’
    1. This assertion checks a sequence with an implication.
  2. Assertion ‘prop_valid_state’
    1. This assertion is checking a property named valid_state, which verifies that at most one bit of current_state is high at any given time ($onehot0).
Cover
  1. In SVA, cover points are written exactly the same way as assertion expressions.
  2. Replacing the assert keyword with cover, you ask the simulator tool to update the coverage database if that expression is true.
Ampere Filtering Question Would this SV code, catch 'X' related issues? If ( my_signal == 'X', assert (&quot;Saw X in my_signal&quot;)? ============ iff In the context of SystemVerilog assertions (SVA), iff stands for "if and only if". It is used within an assertion to specify a condition under which the assertion is active or evaluated. When the condition specified by iff is false, the assertion is not checked, essentially disabling the assertion under certain conditions. This mechanism is particularly useful for making assertions conditional, such as enabling them only when the design is not in a reset state or under specific operational modes. Consider an assertion that checks if a signal valid becomes high in the cycle immediately following a request signal req going high, but you want this assertion to be checked only when the system is not in a reset state. You can use iff to conditionally check the assertion like so: systemverilog Copy code assert property (@(posedge clk) disable iff (reset_n == 0) req |-> ##1 valid); In this example:
  • The assertion is checked on every positive edge of clk.
  • The disable iff (reset_n == 0) part means that the assertion is disabled (not checked) if reset_n is 0, which typically indicates a reset condition.
  • The assertion itself (req |-> ##1 valid) specifies that whenever req goes high, valid must go high at the next clock cycle.

Key Points

  • iff provides a way to ensure that assertions are only checked when it makes sense for the design's current state, reducing false negatives during simulation and formal verification.
  • It is particularly useful in scenarios where certain conditions, such as reset phases, initialization sequences, or specific operational modes, make the normal operation of the design irrelevant or different from its standard behavior.
  • Using iff helps in writing more precise and condition-aware assertions, improving the efficiency and relevance of the verification process.
================= System Functions
FunctionUse
$rosereturns true if the LSB of the expression changed to 1. Otherwise, it returns false.
$fellreturns true if the LSB of the expression changed to 0. Otherwise, it returns false.
$stablereturns true if the value of the expression did not change. Otherwise, it returns false.
$past(expression, num_cycles)Returns value of expression from num_cycles ago
$countonesReturns the number of 1s in an expression
$onehotReturns true if exactly one bit is 1. if no bits or more than one bit is 1, it returns false.
$onehot0Returns true is no bits or just 1 bit in the expression is 1
$isunknownReturns true if any bit in the expression is 'X' or 'Z'