Array in "randomize with" constraint is not properly incrementing its index

This is the code:


bit [14:0] addr_arr[3] = {15'h7ffc, 15'h7ff2, 15'h7ffb};

int msg_id = 0;

repeat(3) begin
  this.pkt.randomize() with {addr == addr_arr[msg_id];};
  msg_id++;
end

Now what happens is it seems that the randomized addr is incrementing only after 2 cycles.

Cycle 1: addr = 15’h7ffc
Cycle 2: addr = 15’h7ffc // It does not go to the next member of the array
Cycle 3: addr = 15’h7ff2 // The second index of the array appeared on the 3rd cycle. It should appear on the 2nd cycle

What’s happening here?

In reply to Reuben:

This behavior may be because of the post increment operator (msg_id++).
Can you try with msg_id = msg_id + 1; ?

In reply to S.P.Rajkumar.V:

I tried that also.
Actually I put a $display before this.pkt.randomize to print if the array is properly incrementing the index and I saw that there’s no problem. But the randomization still equate the addr to the previous index.

In reply to Reuben:

And then when I tried to change the code like this one below, it works.


repeat(3) begin
  this.pkt.randomize();
  this.pkt.addr = addr_arr[msg_id];
  msg_id++;
end

In reply to Reuben:

I tried to simulate the following and found no issue.
Currently, I don’t have access to Questasim and so I ran on NC.


class packet;

rand bit [15:0] addr;

endclass

module randomize;

bit [14:0] addr_arr[3] = {15’h7ffc, 15’h7ff2, 15’h7ffb};
int msg_id = 0;
packet pkt;

initial begin
pkt = new();

repeat(3) begin
  $display ("msg_id is: %0d, addr_arr[msg_id] is %0h", msg_id, addr_arr[msg_id]);
  pkt.randomize() with {addr == addr_arr[msg_id];};
  $display ("pkt address choosen is: %0h", pkt.addr);
  msg_id++;
end

end

endmodule


Output:
msg_id is: 0, addr_arr[msg_id] is 7ffc
pkt address choosen is: 7ffc
msg_id is: 1, addr_arr[msg_id] is 7ff2
pkt address choosen is: 7ff2
msg_id is: 2, addr_arr[msg_id] is 7ffb
pkt address choosen is: 7ffb

In reply to S.P.Rajkumar.V:

Hmmm… This bug is weird… I’ll check my code if there’s something causing it to behave like this.
I’m using NC also.

In reply to Reuben:

I have a question in the following code that

module randomize;
 
bit [14:0] addr_arr[3] = {15'h7ffc, 15'h7ff2, 15'h7ffb};
int msg_id = 0;
packet pkt;
 
initial begin
    pkt = new();
 
    repeat(3) begin
      $display ("msg_id is: %0d, addr_arr[msg_id] is %0h", msg_id, addr_arr[msg_id]);
      pkt.randomize() with {addr == addr_arr[msg_id];};
      $display ("pkt address choosen is: %0h", pkt.addr);
      msg_id++;
    end
end

Mighty be this is bug as in the repeat 3 the pkt is not created object for 3 times so it might have the object 
So i think there the code should be like this 
initial begin
   
    repeat(3) begin
      pkt = new();
      $display ("msg_id is: %0d, addr_arr[msg_id] is %0h", msg_id, addr_arr[msg_id]);
           pkt.randomize() with {addr == addr_arr[msg_id];};
      $display ("pkt address choosen is: %0h", pkt.addr);
      msg_id++;
    end
end

Thanks
Aditya

In reply to aditya:

Actually I also have the variable msg_id inside the class packet.
I’m not sure if it have any effect on the bug.


class packet;
       int        msg_id; 
  rand bit [14:0] addr;
endclass

In reply to Reuben:

yes, as part of the inline randomization, all the variable will be pointing to the class which you are randomizing. Not from the class where you are calling.