Jump to content

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?

Link to comment
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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...