4.constraints
4.constraints
○ Verification engineers create verification plan for the design to target each test feature/scenario. In directed tests exhaustive testing is done(Time consuming).
○ In Complex designs has lot of scenarios and corner cases which are better verified by Randomized tests with less effort and time.
Example: If there is a pheripheral that needs its registers to be configured for AXI bus transaction, then we configure those registers differently to achieve a good
coverage. A test will configure the pheripheral registers with random values every time with a different seed thereby achieving different scenarios for every run
which is effective to exercise corner cases and spot any hidden bugs.
○ variables declared random using rand or randc keyword. the function randomize() (randomize(v1,v2) randomizes variable v1 and v2) is invoked as part of the
class object to randomize all the rand type variables. normal variables holds its default value.
○ Random variables values are uniformly distributed over the range, (Eg the variable data(Left figure) in the above code is 8-bit unsigned integer with a range
from 0 to 255) . If this variable is randomized without any constraints then any value it take with probabality (1/256).
○ when variables declared under randc then, the same value will repeat only after all the values have assigned atleast once. randc values are solved before other
random values.
4.constraints Page 1
Array Randomization: array randomization allows to randomize the elements of an array individually or collectively based on specified constraints.
4.constraints Page 2
endclass
endclass Write a constraint to generate 10-digit phone number with first 3-digits(853)
class Ph;
Write a constraint rand int num[$];
4) To generate the generate the 5 unique real elements between 1.62 and 1.98 in
ascending order constraint cst4{num.size()==10;
class abc; foreach(num[i])
rand real a[5]; if(i==0) num[i]==8;
else if(i==1) num[i]==5;
constraint cst{ else if(i==2) num[i]==3;
foreach(a[i]) { else num[i] inside {[0:9]};}
a[i] inside {[1.62:1.98]}; endclass
if(i>0) a[i]>a[i-1]; //for ascending order
}
unique {a};} //for unique elements
endclass
module tb();
abc a1=new();
initial begin
a1.randomize();
$display("%0p", a1.a);
end
endmodule
Note: $sformatf() is the system task which allows to format the data into a string variable.
Example
string formatted_str;
int value=42;
formatted_str=$sformatf("The value is %0d",value); //This system task formats the integer value into the string variable(formatted_str) with message " The value is 42".
4.constraints Page 3
Implication Operator
An implication operator (->) is used in constraint to establish the relation between two variables in constraint. If the expression in LHS is satisfied implies the RHS must
be satisfied but vicevarsa is need not be satisfied.
• Nested if-else blocks are allowed and multiple constraint statements require them to be enclosed in curly braces{ }.
class abc;
rand bit [3:0] m1;
rand bit m2;
endclass
Write a constraint to create the random array of unique Integers with size between 10 and 16 whose values are in descending o rder.
class array_1;
rand unsigned int myarray[ ];
4.constraints Page 4
constraint c_val{myarray.size inside {[10:16]};
foreach (myarray[i])
if(i>0) myarray[i-1]>myarray[i];
unique{my_array};}
endclass
Note: If constraint specified in the base class gets modified at the extended class by again specifying the constraint at the same name as that of the base class.
Multidimensional array: SV supports constraints which can be applied to multi-dimensional arrays aswell. The below example shows multi-dimensional array with a
packed structure.
4.constraints Page 5
packed structure.
4.constraints Page 6
Note: constraint_mode function is used to enable, disable and getting the status of whether the constraint is active or not.
4.constraints Page 7
endclass solve b before a}
endclass
.
What is System verilog Static Constraint?
Static constraint can be shared across successive class instances. When a static constraint can be enabled/disabled by object handle(constraint_mode()) of its class , The mode
change with one handle of a class will reflect at all other objects of same type in static constraints this is not the case with non static constraints.
Example:
By default the constraints are dynamic in nature, In the below example
Static constraints share their status(active/inactive) across successive randomization but not incase of dynamic.
4.constraints Page 8
4.constraints Page 9
<Variable memory partitions, variable partitions with spaceinbetween, Partition for program and data>
4.constraints Page 10
➢ Explain Randomization methods in SV?
Random variables are declared as rand or randc inside a class are randomized using the builtin method randomize(), The method returns 1/0 for successful/unsuccessful
randomization.
Note: There are some callback functions defined in class and are automatically called by randomize() before and after computing the random values.
• pre_randomize() is called before computing the random values and post_randomize is called after computing the random values thus we can change the randomization
characteristics of the object.
• Both functions are not virtual but behave like virtual methods, If we manually try make them virtual lead to error.
• If randomize() fails then post-randomize() cant be activated variables retain their previous values.
Note: There should not be any contradictory/overriding between original and inline constraint.
4.constraints Page 11
Note: There should not be any contradictory/overriding between original and inline constraint.
endmodule
Note: Disabling the constraint which is not actually exist leads to compile error
4.constraints Page 12
➢ Explain weighted randomcase in SV?
Sometimes we comeacross a scenarios where we need to pick one statement out of many. The randcase introduces a case statement that randomly selects one of its
branches. The case item expressions are positive integer values that represents a weights associated with each item.
4.constraints Page 13
Note: If a branch is assigned a 0 weight then that branch will not be taken, If all branches are assigned 0 weight then none of the branch taken at all(run time warning
gets popedup).
Seed in Constraint?
4.constraints Page 14