Tuesday 17 March 2015

Generate randc functionality from rand variable

It's easy to get the first cycle of random numbers by pushing values on a list in post_randomize() and adding a constraint that keeps the values in the list excluded from the next solution.

class A;
   rand bit [7:0] myvar;
   bit [7:0] list[$];
   constraint cycle {
     unique {myvar, list};
   }
   function void post_randomize;
      list.push_back(myvar);  //store each myvar into list[$]
   endfunction 
endclass

The real problem is knowing when to start the cycle over by clearing the list. If the exact number of possible values for myvar is known, you can add a pre_randomize() method that deletes the list when hitting that limit.

function void pre_randomize;
  if (list.size() == limit)
  begin
    list = {};  //delete queue
  end
endfunction

Otherwise you will have to check the result of calling randomize() and assume it fails because it has exhausted the list of values.

2 comments:

  1. How can generate without using unique. Is it possible?

    ReplyDelete
    Replies
    1. yes it is possible but we have to check the content of the list[$] by looping into it.

      Delete