quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif Introduction

Constraint-driven test generation allows users to automatically generate tests for functional verification. Random testing can be more effective than a traditional, directed testing approach. By specifying constraints, one can easily create tests that can find hard-to-reach corner cases. SystemVerilog allows users to specify constraints in a compact, declarative way. The constraints are then processed by a solver that generates random values that meet the constraints.

   

space.gif

In Verilog we had used method $random for generating random integer values, this is Verilog build in System Task which returns 32 bit random value. This is no good for object randomization (here object mean class based). To help with class based object's to be randomized, SystemVerilog supports rand variables and randomize() method, we will be looking this in detail below.

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Random Variables

Class properties can be declared random using the rand and randc modifiers.

   

space.gif

  • Systemverilog can randomize scalar variables of type integer, reg, and enumerated type. Bit variables can be any size supported by Systemverilog.
  • Arrays can be declared rand or randc, in which case all of their member elements are treated as rand or randc.
  • Associative arrays, dynamic arrays can be declared rand or randc.
  • Associative arrays can be declared rand or randc, however, only the elements in the numeric key range of 0 to n-1 will be randomized.
   

space.gif

As seen above variables can be declared as

   

space.gif

  • rand
  • randc
   

space.gif

rand

Variables declared with the rand keyword are standard random variables. Their values are uniformly distributed over their range.

   

space.gif

   

space.gif

randc

Variables declared with the randc keyword are random-cyclic variables that cycle through all the values in a random permutation of their declared range. Random-cyclic variables can only be reg or enumerated types, and are limited to a maximum size of 16 bits, so the maximum range for any randc variable is 0 to 65535.

   

space.gif

The basic idea is that randc randomly iterates over all the values in the range and that no value is repeated within an iteration. When the iteration is finished, a new iteration is automatically started.

   

space.gif

  ../images/main/bullet_star_pink.gif Example : Random Variables
   

space.gif


  1 typedef enum { UNICAST=11, MULTICAST, BROADCAST} pkt_type;
  2 
  3 program rand_ex;
  4   class frame_t;
  5     rand pkt_type ptype;
  6     rand integer len;
  7     randc bit [1:0] no_repeat;
  8     rand bit  [7:0] payload [];
  9     // Constraint the members
 10     constraint legal {
 11       len >= 2;
 12       len <= 5;
 13       payload.size() == len;
 14     }
 15     function string getType(pkt_type ltype);
 16       begin
 17         case(ltype)
 18          UNICAST   : getType = "UNICAST";
 19          MULTICAST : getType = "MULTICAST";
 20          BROADCAST : getType = "BROADCAST";
 21          default   : getType = "UNKNOWN";
 22         endcase
 23       end
 24     endfunction
 25     // Print the members of the class
 26     task print();
 27       begin
 28         integer i =0;
 29         $write("Packet type %s\n",getType(ptype));
 30         $write("Size of frame is %0d\n",len);
 31         if (payload.size() > 0) begin
 32           $write("Payload is ");
 33           for (i=0; i < len; i++) begin
 34             $write(" %2x",payload[i]);
 35           end
 36           $write("\n");
 37         end
 38         $write("no_repeat is %d\n",no_repeat);
 39       end  
 40     endtask
 41   endclass
 42 
 43   initial begin
 44     frame_t frame = new();
 45     integer j = 0;
 46     // Print frame before randomize
 47     $write("-------------------------------\n");
 48     frame.print(); 
 49     $write("-------------------------------\n");
 50     for (j = 0 ; j < 10;j++) begin
 51       if (frame.randomize() == 1) begin
 52         // Print frame after randomize
 53         frame.print(); 
 54       end else begin
 55         $write("Failed to randomize frame\n");
 56       end
 57       $write("-------------------------------\n");
 58     end
 59   end
 60 endprogram
You could download file rand_ex.sv here
   

space.gif

  ../images/main/bullet_star_pink.gif Simulation : Random Variables
   

space.gif

 -------------------------------
 Packet type UNKNOWN
 Size of frame is x
 no_repeat is 0
 -------------------------------
 Packet type MULTICAST
 Size of frame is 3
 Payload is  0b df 40
 no_repeat is 0
 -------------------------------
 Packet type BROADCAST
 Size of frame is 3
 Payload is  fa 4e 15
 no_repeat is 1
 -------------------------------
 Packet type BROADCAST
 Size of frame is 5
 Payload is  c4 aa c4 cf 4f
 no_repeat is 2
 -------------------------------
 Packet type UNICAST
 Size of frame is 3
 Payload is  2c ce 05
 no_repeat is 3
 -------------------------------
 Packet type BROADCAST
 Size of frame is 2
 Payload is  60 5f
 no_repeat is 2
 -------------------------------
 Packet type BROADCAST
 Size of frame is 4
 Payload is  41 3f 12 f4
 no_repeat is 3
 -------------------------------
 Packet type BROADCAST
 Size of frame is 3
 Payload is  88 01 31
 no_repeat is 1
 -------------------------------
 Packet type UNICAST
 Size of frame is 3
 Payload is  4f 00 dd
 no_repeat is 0
 -------------------------------
 Packet type UNICAST
 Size of frame is 5
 Payload is  45 f2 a2 a1 fd
 no_repeat is 0
 -------------------------------
 Packet type UNICAST
 Size of frame is 4
 Payload is  20 e1 97 c6
 no_repeat is 3
 -------------------------------
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com