TestBencher Pro and Reactive Test Bench Help

6.5 Random Number Generation and Constraints

6.5 Random Number Generation and Constraints

Previous topic Next topic  

6.5 Random Number Generation and Constraints

Previous topic Next topic  

Random values can be generated for variables by calling the randomize method on them (e.g my_variable.randomize). TestBencher will only generate a randomize method for a variable if at least one of the fields of the variable's class has the Random property set to rand or randc. When randomization is used, constraints can be used to specify the valid ranges of the random numbers generated.

Open the Classes and Variables dialog to set the Random Property and define any Constraints:

Open the Classes and Variables dialog from either the diagram (for diagram-level variables) or from the Project (for project-level variables):

For diagram-level variables, click the View Variables button in the Diagram window.

For project-level variables, right click the Transaction-Level Model folder (or most  of the folders in that section) and select Classes and Variables from the context menu.

Cv_dlg_opening

Select the Class Definitions tab or the Variables tab depending on what you are editing.

If you working with a class, select the library and class that you want to constrain.

Set the Random field the variable or class parameter to one of the Random properties. This causes a randomize method to be created. This can be called from the Sequencer process of the the value needs to be randomized. See Section 6.12 Variables and Class Field Properties for more information.

random_drop_down

If you want to constrain the range of random values for the class, go to the Class definitions tab and press the Constraints button to open the Constraints dialog.

Cv_dlg_class_method_constraing

Notice that the title bar of the dialog indicates the class that you are editing.

Enter a min and max constraint for each field.

random_simple_constraint

Randomizing a variable's fields using the Randomize Function

Double-click on the Transaction-Level Model folder in the Project window to open the template file and scroll down to the Sequencer Process as show in Step 5: Define a Sequencer Process. Then call the randomize function for the variable.

For Verilog:

VariableName.randomize;

For VHDL:

randomize(VariableName);

If the variable is an instance of a class with several fields, then the randomize function will only set values for the fields that have a Random property. You must subsequently set the non-random fields.

 

// Example of mixed random and non-random

writeData.randomize; //sets rand fields

writeData.addr = 0xffff; // set a non-rand field

Apply_write(writeData); //use the variable 

Randomizing parameters to a transaction call

Every transactor (timing diagram) has a class that contains the parameters to that transactior's Apply call. Variables of this class can be randomized.

Double-click on the Transaction-Level Model folder in the Project window to open the template file and scroll down to the Sequencer Process as show in Step 5: Define a Sequencer Process.

If all of the timing diagram parameters are random variables, then just call the ApplyRandom function instead of the Apply call for the transaction.

 ApplyRandom_DiagramName;

 

If the parameters for the diagram are mixed random and non-random, then randomize a variable of  the class first, set the non-random properties, and finally pass in the variable through a normal apply call as demonstrated above in the Randomizing a Variable's Fields section.