Additional to the controllability feauters supported by SystemVerilog, following are more points with which controlabiity can be achieved.
In the following example, MACROS MIN_D and MAX_D are defined. Set the MIN and MAX values in the pre_randomize as shown. As MIN_D and MAX_D are macros, they can be assigned from command line. Biggest disadvantage for the method shown below is dynamic controllability.
EXAMPLE: `define MAX_D 100
`define MIN_D 50
class Base;
randinteger Var;
integer MIN,MAX;
constraint randge { Var < MAX ; Var > MIN ;}
functionvoid pre_randomize ();
this.MIN = `MIN_D;
this.MAX = `MAX_D;
$display( " PRE_RANDOMIZE : MIN = %0d , MAX = %0d ",MIN,MAX);
endfunction endclass
program inhe_42;
Base obj;
initial begin obj = new();
for(int i=0 ; i < 100 ; i++)
if(obj.randomize())
$display(" Randomization sucsessfull : Var = %0d ",obj.Var);
else $display("Randomization failed");
end endprogram
RESULTS:
# PRE_RANDOMIZE : MIN = 50 , MAX = 100 # Randomization sucsessfull : Var = 91 # PRE_RANDOMIZE : MIN = 50 , MAX = 100 # Randomization sucsessfull : Var = 93 # PRE_RANDOMIZE : MIN = 50 , MAX = 100 # Randomization sucsessfull : Var = 77 # PRE_RANDOMIZE : MIN = 50 , MAX = 100 # Randomization sucsessfull : Var = 68 # PRE_RANDOMIZE : MIN = 50 , MAX = 100 # Randomization sucsessfull : Var = 67 # PRE_RANDOMIZE : MIN = 50 , MAX = 100 # Randomization sucsessfull : Var = 52 # PRE_RANDOMIZE : MIN = 50 , MAX = 100 # Randomization sucsessfull : Var = 71 # PRE_RANDOMIZE : MIN = 50 , MAX = 100 # Randomization sucsessfull : Var = 98 ...etc.
As in this example,a single object is created and randomized 100 times. Due to this,pre_reandomize is called 100 times, which may not be preferred.
By assigning the values while declaration itself this can be avoided. Simpler way to achieve the above logic.
EXAMPLE: `define MAX_D 100
`define MIN_D 50
class Base;
randinteger Var;
integer MIN = `MIN_D;
integer MAX = `MAX_D;
constraint range { Var < MAX ; Var > MIN ;}
endclass
program inhe_43;
Base obj;
initial begin obj = new();
for(int i=0 ; i < 100 ; i++)
if(obj.randomize())
$display(" Randomization sucsessfull : Var = %0d ",obj.Var);
else $display("Randomization failed");
end endprogram
RESULTS:
# Randomization sucsessfull : Var = 91 # Randomization sucsessfull : Var = 93 # Randomization sucsessfull : Var = 77 # Randomization sucsessfull : Var = 68 # Randomization sucsessfull : Var = 67 # Randomization sucsessfull : Var = 52 # Randomization sucsessfull : Var = 71 # Randomization sucsessfull : Var = 98 # Randomization sucsessfull : Var = 69 # Randomization sucsessfull : Var = 70 ...etc.
With the above approach also, dynamic controlability is lost. For dynamic controllability, define a task, pass this values as arguments when ever the changed is needed.
EXAMPLE: class Base;
randinteger Var;
integer MIN = 10,MAX = 20; // Define default values,If function set is not called,with this it will work
constraint randge { Var < MAX ; Var > MIN ;}
task set (integer MIN,integer MAX);
this.MIN = MIN;
this.MAX = MAX;
$display( " SET : MIN = %0d , MAX = %0d ",MIN,MAX);
endtask endclass
program inhe_44;
Base obj;
initial begin obj = new();
obj.set(0,100) ;
for(int i=0 ; i < 5 ; i++)
if(obj.randomize())
$display(" Randomization sucsessfull : Var = %0d ",obj.Var);
else $display("Randomization failed");
obj.set(50,100) ;
for(int i=0 ; i < 5 ; i++)
if(obj.randomize())
$display(" Randomization sucsessfull : Var = %0d ",obj.Var);
else $display("Randomization failed");
end endprogram RESULTS:
# SET : MIN = 0 , MAX = 100 # Randomization sucsessfull : Var = 24 # Randomization sucsessfull : Var = 68 # Randomization sucsessfull : Var = 43 # Randomization sucsessfull : Var = 11 # Randomization sucsessfull : Var = 4 # SET : MIN = 50 , MAX = 100 # Randomization sucsessfull : Var = 52 # Randomization sucsessfull : Var = 71 # Randomization sucsessfull : Var = 98 # Randomization sucsessfull : Var = 69 # Randomization sucsessfull : Var = 70
More simpler way to dynamically modifying the constraints is by modifying the data members of class via object reference.
EXAMPLE: class Base;
randinteger Var;
integer MIN = 20,MAX =30;
constraint randge { Var < MAX ; Var > MIN ;}
endclass
program inhe_45;
Base obj;
initial begin obj = new();
obj.MIN = 0;
obj.MAX = 100;
for(int i=0 ; i < 5 ; i++)
if(obj.randomize())
$display(" Randomization sucsessfull : Var = %0d ",obj.Var);
else $display("Randomization failed");
$display("MIN and MAX changed");
obj.MIN = 50;
obj.MAX = 100;
for(int i=0 ; i < 5 ; i++)
if(obj.randomize())
$display(" Randomization sucsessfull : Var = %0d ",obj.Var);
else $display("Randomization failed");
end endprogram
RESULTS:
# Randomization sucsessfull : Var = 24 # Randomization sucsessfull : Var = 68 # Randomization sucsessfull : Var = 43 # Randomization sucsessfull : Var = 11 # Randomization sucsessfull : Var = 4 # MIN and MAX changed # Randomization sucsessfull : Var = 52 # Randomization sucsessfull : Var = 71 # Randomization sucsessfull : Var = 98 # Randomization sucsessfull : Var = 69 # Randomization sucsessfull : Var = 70