Wednesday, 17 August 2016

Gotcha: Function in SystemVerilog Constraint

Use of Function in SystemVerilog constraints can lead to unwanted results/conflicting constraints as functions introduces Solve..Before relationship in SV constants.

See below example for more understanding.

In above code, intention is to generate random value of variable 'a' so that its inside 50 to 200.

Output of the above code:

# Value of a = 100
# ** Error: Assertion error.
#    Time: 0 ns  Scope: top File: constraints_gotcha.sv Line: 26
# Value of a = 100
# ** Error: Assertion error.
#    Time: 0 ns  Scope: top File: constraints_gotcha.sv Line: 26
# Value of a = 100
# ** Error: Assertion error.
#    Time: 0 ns  Scope: top File: constraints_gotcha.sv Line: 26
# Value of a = 100
# ** Error: Assertion error.
#    Time: 0 ns  Scope: top File: constraints_gotcha.sv Line: 26
# Value of a = 100
# Value of a = 181
# ** Error: Assertion error.
#    Time: 0 ns  Scope: top File: constraints_gotcha.sv Line: 26
# Value of a = 181
# Value of a = 62
# Value of a = 142
# Value of a = 142


Explanation - Here in constraint c1, function 'get()' is dependent on variable 'a'. So, value of variable 'a' will be solved before solving the constraint with the function. Because of this dependency, constraint solver will not solve constraint in parallel.