Packages provide a mechanism for storing and sharing data, methods, property, parameters that can be re-used in multiple other modules, interfaces or programs. They have explicitly named scopes that exist at the same level as the top-level module. So, all parameters and enumerations can be referenced via this scope. Putting such definitions and declarations inside a package also avoids cluttering the global namescope. Packages can then be imported into the current scope where items in that package can be used.

Note that items within packages cannot have hierarchical references to identifiers except those created within the package or made visible by the import of another package.


All packages have to be enclosed within the package and endpackage keywords.

package my_pkg;
	typedef enum bit [1:0] { RED, YELLOW, GREEN, RSVD } e_signal;
	typedef struct { bit [3:0] signal_id;
                     bit       active;
                     bit [1:0] timeout; 
                   } e_sig_param;
	function common ();
    	$display ("Called from somewhere");
    task run ( ... );


Randomization of variables in a class can be disabled using rand_mode method call.

This is very similar to the constraint_mode() method used to Disable Constraints. So a disabled random variable is treated the same as if they had not been declared rand or randc.

rand_mode can be called both as a function and task. Current state of the variable will be returned if it is called as a function.

	// Disables randomization of variable [variable_name] inside [class_object] class
	[class_object].[variable_name].rand_mode (0);   
	// Enables randomization of variable [variable_name] inside [class_object] class
	[class_object].[variable_name].rand_mode (1);   


All constraints are by default enabled and will be considered by the SystemVerilog constraint solver during randomization. A disabled constraint is not considered during randomization.

Constraints can be enabled or disabled by constraint_mode().


constraint_mode() can be called both as a task and as a function.

When called as a task, the method does not return anything. The task is supplied with an input argument to either turn on or off the given constraint. When called as a function, the method returns the current state of the given constraint.

// Called as a task
class_obj.const_name.constraint_mode(0); 			// Turn off the constraint
class_obj.const_name.constraint_mode(1); 			// Turn on the constraint
// Called as a function
status = class_obj.const_name.constraint_mode(); 	// status is an int variable to hold return value


constraint_mode() is a built-in method and cannot be overriden !

Consider that a class already has well written constraints and there is a need to randomize the class variables with a set of different constraints decided by the user. By using the with construct, users can declare in-line constraints at the point where the randomize() method is called. These additional constraints will be considered along with the object's original constraints by the solver.


class Item;
  rand bit [7:0] id;
  constraint c_id { id < 25; }

module tb;
  initial begin
    Item itm = new ();
    itm.randomize() with { id == 10; }; 		// In-line constraint using with construct
    $display ("Item Id = %0d",;


Variables that are declared as rand or randc inside a class are randomized using the built-in randomize() method. The method returns 1 if randomization was successful, and 0 if it failed. It can fail due to a variety of reasons like conflicting constraints, solver could not come up with a value that meets all constraints and such. Class objects are not randomized automatically, and hence we should always call the randomize() method to do randomization.


	virtual function int randomize ();


Let's look at a simple example to see how randomize() can be called.

class Beverage;
  rand bit [7:0]	beer_id;
  constraint c_beer_id { beer_id >= 10;
                        beer_id <= 50; };

module tb;
   Beverage b;
    initial begin
      b = new ();
      $display ("Initial beerId = %0d", b.beer_id);
      if (b.randomize ()) 
      	$display ("Randomization successful !");
      $display ("After randomization beerId = %0d", b.beer_id);