Jump to content
Sign in to follow this  
pushkarkumar

Randomization of dynamic arrays

Recommended Posts

While randomizing a dynamic array we constrain the size of the array somewhat like this:

rand byte data[]; 

constraint size_c { data.size() == 1000; } 

 

My question is regarding creation of dynamic array. I have neither seen nor used the array constructor to construct dynamic array while randomizing. 

How is the array being constructed? What does randomize method do to construct the dynamic array?

Share this post


Link to post
Share on other sites

The data array will be created implicitly based on the .size(), if it isn't already created, whenever you randomize the class.

However, if you allow the simulator to do this implicitly, then you lose the ability to constrain any of the array's variables, which may or may not be something you care about.

 

If you do, then here's how I do this.

 

First, in my class's new function, I new the array to whatever the largest possible size will be. This is important, and perhaps a little wasteful.

Second, I constrain the data.size() as you have above.

Third, I can then constrain the data bytes, usually using a foreach constraint.

When the class is randomized, the data will automatically re-size itself and constrain the data appropriately.

 

So, like this:

class pushk_c extends uvm_object;
   rand int unsigned data_len;
   constraint data_len_cnstr {
      data_len < MAX_DATA_LEN;  // some parameter or constant
   }

   rand byte data[];
   constraint data_cnstr {
      data.size() == data_len;
      foreach(data[idx]) {
         data[idx] == idx;
      }
   }

   function new(string name="pushk");
      super.new(name);
      data = new[MAX_DATALEN];
   endfunction : new
endclass : pushk_c

I would prefer not to self-promote, but I will anyway.   :)

 

I demonstrate this a few times in my book: http://tinyurl.com/AdvancedUVM, or for the e-book: http://tinyurl.com/AdvancedUVM-ebook

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×