0% found this document useful (0 votes)
24 views12 pages

Randomization Methods and Constraints –2 1

The document outlines the randomization methods in SystemVerilog, focusing on the randomize() method and its associated callbacks, pre_randomize and post_randomize. It explains the use of implication constraints and if-else constraints to control random variable assignments, as well as the concept of weighted distribution for managing the frequency of specific values during randomization. Examples are provided to illustrate the implementation of these concepts in code.

Uploaded by

devu16496
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views12 pages

Randomization Methods and Constraints –2 1

The document outlines the randomization methods in SystemVerilog, focusing on the randomize() method and its associated callbacks, pre_randomize and post_randomize. It explains the use of implication constraints and if-else constraints to control random variable assignments, as well as the concept of weighted distribution for managing the frequency of specific values during randomization. Examples are provided to illustrate the implementation of these concepts in code.

Uploaded by

devu16496
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Randomization

methods and
Constraints –2
Randomization Methods

• SystemVerilog randomization provides a built-in method randomize.


• The randomize() method generates random values for all the active
random variables of an object, subject to the active constraints.
• Variables declared with the rand keyword will get random values on
the object.randomize() method call.
• The randomize() method returns 1 if the randomization is successful
i.e on randomization it’s able to assign random values to all the
random variables, otherwise, it returns 0.
• randomize method associated with below callbacks,
pre_randomize ()
post_randomize()
pre_randomize

• The pre_randomize function can be used to set pre-


conditions before the object randomization.
• For example, Users can implement randomization
control logic in pre_randomize function. i.e
randomization enable or disable by using rand_mode()
method.
post_randomize

• The post_randomization function can be used to check


and perform post-conditions after the object
randomization.
• For example, Users can override the randomized values
or can print the randomized values of variables.
Implication constraints

• The implication operator can be used to declaring


conditional relations between two variables. implication
operator is denoted by the symbol ->. The implication
operator is placed between the expression and
constraint
• expression -> constraint
If the expression on the LHS of implication operator (-
>) is true, then the only constraint on the RHS will be
considered.
class seq_item;
rand bit [7:0] value;
rand enum {LOW, HIGH} scale;
constraint scale_c { (scale == LOW) -> value <50; }
endclass
module constraint_example;
seq_item item;
initial begin
item = new();
item2 =
repeat(5) begin
item.randomize();
$display("scale = %s, value = %0d", item.scale.name(), item.value);
end
end
endmodule
if else constraints

• if else block allows conditional executions of constraints.


If the expression is true, all the constraints in the first
constraint/constraint-block must be satisfied, otherwise
all the constraints in the optional else
constraint/constraint-block must be satisfied.
class seq_item;
rand bit [7:0] value;
rand enum {LOW, HIGH} scale;
constraint scale_c { if(scale == LOW) value < 50;
else value >= 50;}
endclass
module constraint_example;
seq_item item;
initial begin
item = new();
repeat(5) begin
item.randomize();
$display("scale = %s, value = %0d", item.scale.name(), item.value);
end
end
endmodule
weighted distribution
• Constraint provides control on randomization, from which the
user can control the values on randomization. it would be
good if it’s possible to control the occurrence or repetition of
the same value on randomization.
• yes its possible, with dist operator, some values can be
allocated more often to a random variable. this is called
a weighted distribution. dist is an operator, it takes a list of
values and weights, separated by := or :/ operator.
• As the name says, in weighted distribution weight will be
specified to the values inside the constraint block. Value with
the more weight will get allocated more often to a random
variable.
• syntax
value := weight or
value :/ weight

• Value – desired value to a random variable


weight – indicates how often the value needs to be considered
on randomization
• The values and weights can be constants or variables,
• value can be single or a range
• the default weight of an unspecified value is := 1
• the sum of weights need not be a 100
Example
• The := operator assigns the specified weight to the
item, or if the item is a range, specified weight to every
value in the range.
addr dist { 2 := 5, [10:12] := 8 };

for addr == 2 , weight 5


addr == 10, weight 8
addr == 11, weight 8
addr == 12, weight 8
Example
• The :/ operator assigns the specified weight to the item, or if
the item is a range, specified weight/n to every value in the
range. where n is the number of values in the range.

addr dist { 2 :/ 5, [10:12] :/ 8 };

for addr == 2 , weight 5


addr == 10, weight 8/3
addr == 11, weight 8/3
addr == 12, weight 8/3

You might also like