RAndomize a Queue in System Verilog

Hi All

Is it possible to randomize a queue in system verilog…

class seq extends base_seq;
rand bit [15:0] my_q [$];

constraint queue_val {
// What should i put here
}

endclass

I want to randomize the queue from my test case…
Please help me gigure this out.

Use pre-post randomize.

for(i=0; i<$random; i++)
my_q.push_back($urandom());

*In reply to Ravi Nagadia:*Do not ever use $random in SystemVerilog, and there is no need for pre_ or post_randomize.

Donald,

You can randomize a queue the same way you randomize a dynamic array. If you constrain the size of a queue, the solver will allocate the elements of the queue to meet the constraint. Then you can use
foreach
to constrain each element.

class seq extends base_seq;
rand bit [15:0] my_q [$];
constraint queue_val {
      my_q.size() inside {[10:20]};
      foreach (my_q[ii]) my_q[ii] >2;
}
endclass

In reply to dave_59:

Thanks Dave.

In reply to dave_59:

Thanks a lot Dave.

I have another problem… In the sequence, i want to check the size of the queue . If the size of the queue is not 0, then i want to randomize the queue. Is it possible to achieve this with constraints.

I want something like the following. But this is a randomization error when i try to do this.

constraint queue_val {
     if(my_q.size == 0) {
       my_q.size() inside {[10:20]};
       foreach (my_q[ii]) my_q[ii] >2;
     }
}

In reply to donald:

You have to check the size of the queue just before randomizing the queue as below,



seq obj = new;
if(obj.my_q.size() != 0)
begin
     if(obj.randomize() == 1)   
     $display("Randomizing is done");
     //Do Something with the Queue here
     else
     $display("Randomization failed");
end
else
$display("\n Didn't Randomize as my_q.size() is not equal to zero \n");]

Thanks,
Sravan K

In reply to donald:
Your constraint fails because you are saying if the size is 0, the size must be between 10 and 20. That constraint can never be met. What you need to do is check the size before randomization.

constraint queue_val {
     if(const'(my_q.size) == 0) {
       my_q.size() inside {[10:20]};
       foreach (my_q[ii]) my_q[ii] >2;
     }
}

A
const
cast says to treat the expression as non-random constant; the value it has before any randomization. It has the same effect as the following

int const_size;
function void pre_randomize();
  const_size = my_q.size();
endfunction
constraint queue_val {
     if(const_size) == 0) {
       my_q.size() inside {[10:20]};
       foreach (my_q[ii]) my_q[ii] >2;
     }
}