Question about error (Range with must be constant expression)

I have a randomization code like this where variable are defined as:
bit [15:0] a, size_a, c
int b;
int lsb_index;

std:: randomize(a) with {a >= b;
a[lsb_index : 0] == 0;__
(a + size_a) <= c;};

I have compilation error " Range width must be constant expression " for line that is highlighted.

Any idea why I have that error and how I can resolve it?

Hey, can you please write the code in the eda playground, so that i can look into it, also in the code that you have provided here, semicolon is missing after this “bit [15:0] a, size_a,c”

In reply to Chisiya:

Semicolon is missing while doing copy/paste here. It is present in original code.

Regarding eda playground, I am not sure what you mean about you can look into it? I have never used eda playground before.

from the compilation error, it seems you need to give some constant value to lsb_index, you can try and check, also, it would be better if you provide more information related to the code, i can make sense of it then.

In reply to bpatel:
Array range requires constant expression. Its alternative is to use mask.


// code snippet 
bit [15:0] mask = 16'hffff ;
rand bit[3:0] lsb_indx ;

constraint c_a {
  lsb_indx inside {[0:15]}; // how many bits needs to be masked. 
  a == a  &  (mask << lsb_indx) ;
}


In reply to Chisiya:
If I give constant value, it works.
But assigning constant value to lsb_index before randomization call generates compile error.

bit [15:0] a, size_a, c;
int b;
int lsb_index;

lsb_index = 19;
std:: randomize(a) with {a >= b;
a[lsb_index : 0] == 0;
(a + size_a) <= c;};

I have compilation error " Range width must be constant expression " for line that is highlighted.

Any idea why I have that error and how I can resolve it?

In reply to bpatel:

I believe that the compiler needs have a bit-wise width defined during compilation and default variable declarations are automatic that can be changed on the fly during the simulation. So, it’s the requirements for the compiler to have constant value while accessing the bit-width.
Even with static declaration of the lsb_index won’t work since the memory is not allocated until the object for the class gets created which happens during the simulation.

Apart from that, not related to the question,
other issues:

  • the width of the a is [15:0] where you are trying to get [19:0] is another issue in the code.
  • since rand is not used, all other variables except a won’t be randomizing and make the in-line constraint to generate 0s for all the variables.