Range must be bounded by constant expressions

Hi,


for(int i =0 ;i<size;i++) begin
    if(i == 0) begin
      byte_que[i] = index;
    end
    else if (i == 1) begin
      if(index == 0) begin
        byte_que[i] = command;
      end
      else 
        byte_que[i] = address_data[7:0];
    end
    else begin
      byte_que[i] = address_data[i*8+7:i*8]; // I am getting error in this line
    end
  end

while executing the code I am getting the following error : -

i2c_hl_driver.svh(99): Range must be bounded by constant expressions

can anyone help me how to resolve this one

Thanks

In reply to lalithjithan:

The key question is: what is ‘size’? It cannot be a variable.

In reply to chr_sue:

In reply to lalithjithan:
The key question is: what is ‘size’? It cannot be a variable.

size is a logic type,like below : -

logic [2:0] size;

if(cmd == 0)
size = 2;
else
size = 3;
for(int i =0 ;i<size;i++) begin
    if(i == 0) begin
      byte_que[i] = index;
    end
    else if (i == 1) begin
      if(index == 0) begin
        byte_que[i] = command;
      end
      else 
        byte_que[i] = address_data[7:0];
    end
    else begin
      byte_que[i] = address_data[i*8+7:i*8]; // I am getting error in this line
    end
  end




In reply to lalithjithan:

As I said you cannot use a variable for this. size has to be a parameter.

In reply to lalithjithan:

If you want to extract byte ‘i’, you can do the following:

byte_que[i] = address_data[i*8 +: 8];

This means slice 8 bits starting from index ‘i*8’. The number of bits you slice (in this case 8) has to be a constant. The starting position can be a variable.

In reply to chr_sue:

Why we can’t use variable for this?
@lalithjithan: Maybe the expression is out of boundary of address_data. Why is the width of address_data?

In reply to chris_le:

In reply to chr_sue:
Why we can’t use variable for this?
@lalithjithan: Maybe the expression is out of boundary of address_data. Why is the width of address_data?

the width of address_data is 32 bit

logic [31:0] address_data

In reply to Tudor Timi:

In reply to lalithjithan:
If you want to extract byte ‘i’, you can do the following:

byte_que[i] = address_data[i*8 +: 8];

This means slice 8 bits starting from index ‘i*8’. The number of bits you slice (in this case 8) has to be a constant. The starting position can be a variable.

Hi,i tried

byte_que[i] = address_data[i*8 +: 8];

but i getting the following results :-

i am passing address_data as = 32’h123456

but my queue has : -

byte_que[0] = 4

//------------------
byte_que[1] = 56
byte_que[2] = 12
byte_que[3] = 0
byte_que[4] = x
//------------------

In reply to chris_le:

In reply to chr_sue:
Why we can’t use variable for this?
@lalithjithan: Maybe the expression is out of boundary of address_data. Why is the width of address_data?

I meant you can noz use variables together with this line of code:

byte_que[i] = address_data[i*8+7:i*8];

Because the slice is not constructed by a constant expression.

In reply to lalithjithan:

No idea what you’re doing to get those results, but the following example works as expected:


module test;
  byte unsigned byte_que[4];
  bit [31:0] address_data = 32'h123456;
  
  initial begin
    foreach (byte_que*)
      byte_que[i] = address_data[i*8 +: 8];
    $display("%p", byte_que);
  end
endmodule

You get [i]'{'h56, 'h34, 'h12, 'h0}* printed.

In reply to Tudor Timi:

In reply to lalithjithan:
No idea what you’re doing to get those results, but the following example works as expected:


module test;
byte unsigned byte_que[4];
bit [31:0] address_data = 32'h123456;
initial begin
foreach (byte_que*)
byte_que[i] = address_data[i*8 +: 8];
$display("%p", byte_que);
end
endmodule

You get [i]'{'h56, 'h34, 'h12, 'h0}* printed.

thank you Its working now.