std::randomize examples

Sometimes an example is just enough. Honestly, I do search and copy a lot. It is simply because I cannot remember everything. Sometimes I pay a lot of time to understand and get the code run perfectly and then completely forget about it after 1 year. This post is to store some of Systemverilog std::randomize examples that I created/collected, just for me to copy later. :D


Description Code Link
systemverilog std::randomize array with constrains on unique element, number of elements, array sum, constrains for each element value using foreach
  int d_array[]; // also works with queue d_array[$]

  std::randomize(d_array) with {                                
    unique {d_array};
    d_array.size() == 10;
    d_array.sum    == 150;
    foreach (d_array[i]) {
      d_array[i] < 100;
      d_array[i] > 1;
      d_array[i] != i;
    } };
      
systemverilog std::randomize array using sum with
    int d_array[]; //also work with queue, try d_array[$]

    std::randomize(d_array) with {
      d_array.size() == 5;
      d_array.sum() with ( item > 10 ? item:0)  == 100;
      foreach (d_array[i]) {
        d_array[i] inside {[0:100]};
      }
    };
      
systemverilog std::randomize using implication constraints
    int d_array[]; 

    std::randomize(d_array) with {
      d_array.size() == 3;
      d_array[0] > 10 -> d_array[1] inside {0,1,2};
      d_array[0] < 10 -> d_array[1] inside {3,4,5}
                         & d_array[2] inside {6,7,8};
    };     
      
systemverilog std::randomize using condition constraints
    int d_array[]; 

    std::randomize(d_array) with {
      d_array.size() == 3;
      d_array[0] inside {[0:15]};
      if (d_array[0] > 10) {
        d_array[1] inside {0,1,2};
        d_array[2] inside {10,11,12};
      }
      else if (d_array[0] > 5) {
        d_array[1] inside {3,4,5};
        d_array[2] inside {6,7,8};
      }
      else {
        d_array[1] inside {13,14,15};
        d_array[2] inside {16,17,18};
      }
    };
      
systemverilog std::randomize with multiple variables
  int d_array[]; 
  int num;

  std::randomize(d_array, num) with {
    num inside {[0:100]};
    d_array.size()  == 3;
    d_array.sum(item) with ( item > 10? item:0) == num;
    foreach (d_array[i]){
      d_array[i] inside {[0:100]};
    }
  };     
      
systemverilog std::randomize with weight distribution
  int d_array[]; 

  std::randomize(d_array) with {
    d_array.size()  == 1000;
    foreach (d_array[i]){
      d_array[i] dist { [0:50]:/80, [51:99]:/20 };
    }
  }; // 80% d_array element value will be in range [0:50]
      
systemverilog std::randomize with solve .. before
  int d_array[]; 
  int num;

  std::randomize(d_array, num) with {
    solve num before d_array;
    num inside {[0:100]};
    d_array.size()  == 3;
    d_array.sum(item) with ( item > 10? item:0) == num;
    foreach (d_array[i]){
      d_array[i] inside {[0:100]};
    }
 };     
      
systemverilog std::randomize with multi-dimensional array, constraint sum of all element, size for each dimension
  int d_array [][]; 

  std::randomize(d_array) with {
    d_array.sum() with (item.sum() with (item)) == 1000;  // total value of the array

    d_array.size() == 10; 

    foreach (d_array[i]) {
      d_array[i].size() inside {[2:10]};
    }

    foreach (d_array[i,j]) {
      d_array[i][j] inside {[0:100]};
    }

  };
      
systemverilog std::randomize with multi-dimensional array, 2nd example
    int d_array [5][]; 
  
    std::randomize(d_array) with {
          unique {d_array};
          foreach (d_array[i]) {
                  d_array[i].size() inside {[1:2]};
          }
          foreach (d_array[i,j]) {
                   d_array[i][j] inside {[0:16]};
          }
    };
      
systemverilog std::randomize with associative array, using enum as index
  typedef enum {red=1, green, blue, pink, yellow} color_e;
  int d_array[color_e] = '{red:10, green:20, blue:30, pink:40, yellow:50};
  //must construct the array element before randomizing.
  
  function void display();
    std::randomize(d_array) with {
      unique{d_array};
      foreach (d_array[i]) {
        d_array[i] < 50;
        d_array[i] > 0;
      }
    };
      
systemverilog std::randomize with enum variable
  typedef enum {RED=11, GREEN=22, BLUE=33, PINK=44, YELLOW=55} color_e;
  color_e m_color; 
  color_e m_2nd_color;
  
  function void display();
    std::randomize(m_2nd_color, m_color) with {
      m_color     inside {RED,GREEN, YELLOW};

      // redundant code, no need to declare this below constraint
      // the constraint solver only selects the value inside the set of enum labels
      m_2nd_color inside {m_2nd_color};   
    };
              
      
systemverilog std::randomize with 2 variable of 2 difference objects
  Packet pkt1;
  Packet pkt2;
  
  initial begin
    pkt1 = new();
    pkt2 = new();
    
    std::randomize(pkt1.count, pkt2.count) with {
      pkt1.count >0;
      pkt2.count >0;
      (pkt1.count + pkt2.count) == 100;
    };
  end 

      
systemverilog std::randomize with this and local::
TDB
    
TDB

      


[Tags systemverilog  ]