|  | 
|  |  | 
 |  
|  |  |  |  
|  |  | 
 |  
|  |  | randomize() |  
|  |  | In last page we saw basic usage of randomize() method. randomize() method in Systemverilog has following extension. |  
|  |  | 
 |  
|  |  | 
  constraint_mode()  rand_mode()  randomize() with  pre_randomize()  post_randomize() |  
|  |  | 
 |  
|  |  |  |  
|  |  | 
 |  
|  |  | constraint_mode() |  
|  |  | Systemverilog provides the predefined constraint_mode() method to control whether a constraint is active or inactive. All constraints are initially active. |  
|  |  | 
 |  
|  |  | Syntax |  
|  |  | 
 |  
|  |  | task object[.constraint_identifier]::constraint_mode( bit on_off );
or
function int object.constraint_identifier::constraint_mode();
 |  
|  |  | 
 |  
|  |  | Where |  
|  |  | 
 |  
|  |  | 
object_name Is the name of the object in which the constraint block is defined.constraint_identifierIs the name of the constraint block to which the operation is applied.1 (ON) Sets the specified constraint block to active so that it is enforced on subsequent calls to the randomize() method.0 (OFF) Sets the specified constraint block to inactive so that it is not enforced on subsequent calls to the randomize() method.A compiler error will be issued if the specified constraint block does not exist within the class hierarchy. |  
|  |  | 
 |  
|  |  | constraint_mode method is really usefull when we want to randomize a class which is coded for different configs. Below example shows we can use constraint mode. |  
|  |  | 
 |  
|  |  | Example : constraint_mode() |  
|  |  | 
 |  
|  |  | 
  1 typedef enum {UNICAST=11,MULTICAST,BROADCAST} pkt_type;
  2 
  3 program constaint_mode_ex;
  4   class frame_t; 
  5     rand pkt_type ptype;
  6     rand integer len;
  7     rand bit  [7:0] payload [];
  8     constraint common {
  9       payload.size() == len;
 10     }
 11     // Constraint the members
 12     constraint unicast {
 13       len <= 2;
 14       ptype == UNICAST;
 15     }
 16     // Constraint the members
 17     constraint multicast {
 18       len >= 3;
 19       len <= 4;
 20       ptype == MULTICAST;
 21     }
 22     function string getType(pkt_type ltype);
 23       begin
 24         case(ltype)
 25          UNICAST   : getType = "UNICAST";
 26          MULTICAST : getType = "MULTICAST";
 27          BROADCAST : getType = "BROADCAST";
 28          default   : getType = "UNKNOWN";
 29         endcase
 30       end
 31     endfunction
 32     // Print the members of the class
 33     task print() ;
 34       begin
 35         integer i =0;
 36         $write("Packet type %s\n",getType(ptype));
 37         $write("Size of frame is %0d\n",len);
 38         if (payload.size() > 0) begin
 39           $write("Payload is ");
 40           for (i=0; i < len; i++) begin
 41             $write(" %2x",payload[i]);
 42           end
 43           $write("\n");
 44         end
 45       end  
 46     endtask
 47   endclass
 48  
 49   initial begin 
 50      frame_t frame = new();
 51      integer j = 0;
 52      $write("-------------------------------\n");
 53      // Do contraint for Unicast frame
 54      frame.multicast.constraint_mode(0);
 55      if (frame.multicast.constraint_mode() == 0) begin
 56        if (frame.randomize() == 1) begin
 57          frame.print(); 
 58        end else begin
 59          $write("Failed to randomize frame\n");
 60        end
 61      end else begin
 62         $write("Failed to disable constraint multicast\n");
 63      end
 64      $write("-------------------------------\n");
 65      // Check the status of constraint multicast
 66      $write ("Constraint state of multicast is %0d\n",
 67          frame.multicast.constraint_mode());
 68      $write("-------------------------------\n");
 69      // Now disable the unitcast and enable multicast
 70      frame.unicast.constraint_mode(0);
 71      frame.multicast.constraint_mode(1);
 72      if (frame.randomize() == 1) begin
 73        frame.print(); 
 74      end else begin
 75        $write("Failed to randomize frame\n");
 76      end
 77      $write("-------------------------------\n");
 78   end
 79 endprogram
You could download file constraint_mode_ex.sv here |  
|  |  | 
 |  
|  |  | Simulation Output : constraint_mode() |  
|  |  | 
 |  
|  |  |  -------------------------------
 Packet type UNICAST
 Size of frame is 1
 Payload is  7d
 -------------------------------
 Constraint state of multicast is 0
 -------------------------------
 Packet type MULTICAST
 Size of frame is 3
 Payload is  df 40 f7
 -------------------------------
 |  
|  |  | 
 |  
|  |  | 
 |  
|  |  | 
 |  
|  |  |  |  
|  |  | 
 |  |  | 
|  
 |  
 |  
 | 
| 
 | 
|    |  
| Copyright © 1998-2025 |  
| Deepak Kumar Tala - All rights reserved |  
| Do you have any Comment? mail me at:deepak@asic-world.com
 |  |