rand_mode:
The random nature of variables declared as rand
or randc can be turned on or off dynamically by using in-built method called
rand_mode(). rand_mode() can be called
as function or task.
In below example, rand_mode of all variable of
class is disabled and enabled.
---------------------------------------------------------------------
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class rand_mo; | |
rand integer Var1; | |
randc integer Var2; | |
endclass | |
program rand_mo_p_23; | |
rand_mo obj = new(); | |
initial begin | |
void'(obj.randomize()); | |
$display(" 1 : Var1 : %d, Var2 : %d ",obj.Var1, obj.Var2); | |
// Var1 and Var2 will be treated as State variables. | |
obj.rand_mode(0); | |
void'(obj.randomize()); | |
$display(" 2 : Var1 : %d, Var2 : %d ",obj.Var1, obj.Var2); | |
// Var1 and Var2 will be treated as random variables. | |
obj.rand_mode(1); | |
void'(obj.randomize()); | |
$display(" 3 : Var1 : %d, Var2 : %d ",obj.Var1, obj.Var2); | |
end | |
endprogram | |
//Output: | |
// 1 : Var1 : -902462825, Var2 : 906460592 | |
// 2 : Var1 : -902462825, Var2 : 906460592 //Remain same (not randomized) | |
// 3 : Var1 : 69704603, Var2 : 1917593266 |
---------------------------------------------------------------------
We can also change rand_mode of specific variable.
Consider below example,
---------------------------------------------------------------------
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class rand_mo; | |
rand integer Var1; | |
rand integer Var2; | |
endclass | |
program rand_mo_p_24; | |
rand_mo obj = new(); | |
initial begin | |
void'(obj.randomize()); | |
$display(" 1 : Var1 : %d Var2 : %d ",obj.Var1,obj.Var2); | |
// Var1 will become State variable | |
obj.Var1.rand_mode(0); | |
void'(obj.randomize()); | |
$display(" 2 : Var1 : %d Var2 : %d ",obj.Var1,obj.Var2); | |
// Var2 will also become State variable | |
obj.Var2.rand_mode(0); | |
$display(" 3 : Var1 : %d Var2 : %d ",obj.Var1,obj.Var2); | |
// Var1 will become random variable | |
obj.Var1.rand_mode(1); | |
void'(obj.randomize()); | |
$display(" 4 : Var1 : %d Var2 : %d ",obj.Var1,obj.Var2); | |
end | |
endprogram | |
//Output: | |
// 1 : Var1 : -902462825 Var2 : -1241023056 // Botn Var1 and Var2 are randomized | |
// 2 : Var1 : -902462825 Var2 : 69704603 // Var1 remain unchanged (Not randomized) | |
// 3 : Var1 : -902462825 Var2 : 69704603 // Var1 and Var2 both remain unchanged (Not randomized) | |
// 4 : Var1 : -1877783293 Var2 : 69704603 // Var1 changed (randomized), Var2 reamin unchanged (Not randomized) |
---------------------------------------------------------------------
When rand_mode method is called as function, it
returns the active status of the specified random variable.
When called as a function, rand_mode() returns
the current active state of the specified random variable. It returns 1 if the
variable is active (ON) and 0 if the variable is inactive (OFF).
---------------------------------------------------------------------
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class rand_mo; | |
rand integer Var1; | |
rand integer Var2; | |
endclass | |
program rand_mo_p_24; | |
rand_mo obj = new(); | |
initial begin | |
void'(obj.randomize()); | |
$display(" 1 : Var1 : %d Var2 : %d ",obj.Var1,obj.Var2); | |
obj.Var1.rand_mode(0); | |
void'(obj.randomize()); | |
$display(" 2 : Var1 : %d Var2 : %d ",obj.Var1,obj.Var2); | |
if(obj.Var1.rand_mode()) begin | |
$display(" 3a : Var1 is random"); | |
end | |
else begin | |
$display(" 3b : Var1 is nonrandom"); | |
end | |
void'(obj.randomize()); | |
$display(" 4 : Var1 : %d Var2 : %d ",obj.Var1,obj.Var2); | |
end | |
endprogram | |
//Output: | |
// 1 : Var1 : -902462825 Var2 : -1241023056 | |
// 2 : Var1 : -902462825 Var2 : 69704603 | |
// 3b : Var1 is nonrandom | |
// 4 : Var1 : -902462825 Var2 : -1877783293 |
---------------------------------------------------------------------
A compiler error shall be issued if the specified
variable does not exist within the class hierarchy or even though it exists but
not declared as rand or randc. The following example illustrates the second
case.
rand_mode of rand static variable,
---------------------------------------------------------------------
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A; | |
rand static integer Var1; | |
rand integer Var2; | |
endclass | |
program A_p_27; | |
A obj_1 = new; | |
A obj_2 = new; | |
initial begin | |
obj_2.Var1.rand_mode(0); | |
obj_2.Var2.rand_mode(0); | |
repeat(2) begin | |
void'(obj_1.randomize()); | |
$display(" 1 : obj_1.Var1 : %0d, obj_1.Var2 : %0d, obj_2.Var1 : %0d, obj_2.Var2 : %0d",obj_1.Var1,obj_1.Var2,obj_2.Var1,obj_2.Var2); | |
void'(obj_2.randomize()); | |
$display(" 2 : obj_1.Var1 : %0d, obj_1.Var2 : %0d, obj_2.Var1 : %0d, obj_2.Var2 : %0d",obj_1.Var1,obj_1.Var2,obj_2.Var1,obj_2.Var2); | |
end | |
end | |
endprogram | |
//Output: | |
// 1 : obj_1.Var1 : x, obj_1.Var2 : -902462825, obj_2.Var1 : x, obj_2.Var2 : x | |
// 2 : obj_1.Var1 : x, obj_1.Var2 : -902462825, obj_2.Var1 : x, obj_2.Var2 : x | |
// 1 : obj_1.Var1 : x, obj_1.Var2 : -1241023056, obj_2.Var1 : x, obj_2.Var2 : x | |
// 2 : obj_1.Var1 : x, obj_1.Var2 : -1241023056, obj_2.Var1 : x, obj_2.Var2 : x |
---------------------------------------------------------------------
constraint_mode
SystemVerilog supports to change the status of
constraint block dynamically.
The constraint_mode() method can be used to active/inactive
constraint. By default all the constraint blocks are active. When a constraint is inactive, it is not
considered by the randomize() method.
It can be used as task or function.
When called as a task, the argument to the
constraint_mode task method determines the operation to be performed.
When called as a function, constraint_mode()
returns the current active state of the specified constraint block. It returns
1 if the constraint is active (ON) and 0 if the constraint is inactive (OFF).
---------------------------------------------------------------------
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// object_name.constraint_mode | |
class rand_mo; | |
rand integer Var1; | |
rand integer Var2; | |
constraint Var_1 { Var1 == 20;} | |
constraint Var_2 { Var2 == 10;} | |
endclass | |
program rand_mo_p_38; | |
rand_mo obj = new(); | |
initial begin | |
//By default all constraints are active. | |
void'(obj.randomize()); | |
$display(" 1 : Var1 : %0d, Var2 : %0d ",obj.Var1, obj.Var2); | |
//Both constraints Var_1 and Var_2 are turned off. | |
obj.constraint_mode(0); | |
void'(obj.randomize()); | |
$display(" 2 : Var1 : %0d, Var2 : %0d ",obj.Var1, obj.Var2); | |
//Both constraints Var_1 and Var_2 are turned on. | |
obj.constraint_mode(1); | |
void'(obj.randomize()); | |
$display(" 3 : Var1 : %0d, Var2 : %0d ",obj.Var1, obj.Var2); | |
end | |
endprogram | |
//Output: | |
// 1 : Var1 : 20, Var2 : 10 | |
// 2 : Var1 : -755415376, Var2 : -334455186 | |
// 3 : Var1 : 20, Var2 : 10 |
---------------------------------------------------------------------
---------------------------------------------------------------------
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// object_name.constraint_name.constraint_mode | |
class rand_mo; | |
rand integer Var1; | |
rand integer Var2; | |
constraint Var_1 { Var1 == 20;} | |
constraint Var_2 { Var2 == 10;} | |
endclass | |
program rand_mo_p_38; | |
rand_mo obj = new(); | |
initial begin | |
void'(obj.randomize()); | |
$display(" Var1 : %0d, Var2 : %0d ",obj.Var1, obj.Var2); | |
obj.Var_1.constraint_mode(0); | |
void'(obj.randomize()); | |
$display(" Var1 : %0d, Var2 : %0d ",obj.Var1, obj.Var2); | |
obj.Var_1.constraint_mode(1); | |
void'(obj.randomize()); | |
$display(" Var1 : %0d, Var2 : %0d ",obj.Var1, obj.Var2); | |
end | |
endprogram | |
//Output: | |
// Var1 : 20, Var2 : 10 | |
// Var1 : -755415376, Var2 : 10 | |
// Var1 : 20, Var2 : 10 |
---------------------------------------------------------------------
---------------------------------------------------------------------
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Used as Function | |
class rand_mo; | |
rand integer Var1; | |
rand integer Var2; | |
constraint Var_1 { Var1 == 20;} | |
constraint Var_2 { Var2 == 10;} | |
endclass | |
program rand_mo_p_38; | |
rand_mo obj = new(); | |
initial begin | |
//By default all constraints are active. | |
void'(obj.randomize()); | |
$display(" 1 : Var1 : %0d, Var2 : %0d",obj.Var1, obj.Var2); | |
//Both constraint Var_1 is are turned off. | |
obj.Var_1.constraint_mode(0); | |
void'(obj.randomize()); | |
$display(" 2 : Var1 : %0d, Var2 : %0d",obj.Var1, obj.Var2); | |
if (obj.Var_1.constraint_mode()) | |
$display(" 3a : Var_1 constraint si active"); | |
else | |
$display(" 3b : Var_1 constraint si inactive"); | |
if (obj.Var_2.constraint_mode()) | |
$display(" 4a : Var_2 constraint si active"); | |
else | |
$display(" 4b : Var_2 constraint si inactive"); | |
void'(obj.randomize()); | |
$display(" 5 : Var1 : %0d, Var2 : %0d ",obj.Var1, obj.Var2); | |
end | |
endprogram | |
//Output: | |
// 1 : Var1 : 20, Var2 : 10 | |
// 2 : Var1 : -755415376, Var2 : 10 | |
// 3b : Var_1 constraint si inactive | |
// 4a : Var_2 constraint si active | |
// 5 : Var1 : -334455186, Var2 : 10 |
---------------------------------------------------------------------
nice content !!
ReplyDeleteNice article sagar, but is ther any other alterative to disable randomization without using rand_mode
ReplyDelete