Range must be bounded by constant expressions

I am trying to implement the following in systemverilog.

always_comb
    begin
	for(int i=0;i<=124;i=i+6)
	    begin
	        offset = DFF_data[ ((2*i)+1):(2*i) ]; // Error !	
		data_output[i+offset]= data_input[i];
	    end
   end 

I get the following error in the line marked above: Range must be bounded by constant expressions. I can see the error ocuurs beacuse of the use of “i” in defining the range of array DFF_data. But writing this without “i” seems impractical. I am using QuestaSim 10.1d. Has anyone come across such a situation?

Verilog integral expressions must have a constant width determined at compile time.

You need to use a indexed part-select which allows you to specify a variable base expression with a constant width. (this comes from Verilog. system verilog - Indexing vectors and arrays with +: - Stack Overflow)

logic [15:0] down_vect;
logic [0:15] up_vect;
down_vect[lsb_base_expr +: width_expr]
up_vect[msb_base_expr +: width_expr]
down_vect[msb_base_expr -: width_expr]
up_vect[lsb_base_expr -: width_expr]

So you can write

offset = DFF_data[ (2*i) +: 2 ];

In reply to vishalkewlani:

Hello Guys,

I am facing the same problem with 2variables.

offset = DFF_data[ ((2j)+7):(2i) ];

Here i am having the 2 variables i&j.
So how to do part select.
Means to say that my width will vary.

Please answer the question.

In reply to raja.kuraku:

You can use a shift and mask operation

offset = (DFF_data >> 2*i) & 2**(width)-1

I’ll leave it to you or someone else to figure out the width expression and make sure I have the right operator precedence.

In reply to dave_59:

module wr_strb;
bit [15:0] Data = 'hFFFF;
bit [15:0] WDATA;

task strobe(int r);
WDATA[(4*r-1):0] = 'hx;
WDATA[15:(4*r)] =  Data[15:(4*r)];
$display("WDATA = %0h",WDATA);
endtask

initial
begin
strobe(2);
end
endmodule

Hi Dave,
i’m getting an error : “Range must be bounded by constant expression” for the above code.
If you have any solution for this please guide me.

thanks and regards,
Saroj Kumar Sahoo

In reply to saroj kumar sahoo:

function void strobe(int r);
  bit [15:0] mask;
  mask = (15'b1 << 4*r) - 1;
  WDATA =  Data ^ (mask & 15'hXXXX);
  $display("WDATA = %0h",WDATA);
endfunction

In reply to dave_59:

Thank you very much Dave.

data_in[7*(j+1)+j:8*j] // while i am doing this in sv this error comes----> Range must be bounded by constant expressions how to get the same functionality

In reply to mansi m joshi:

data_in[j+:8]

hello dave in this above example if only 8 bit slicing needs for ex : [7:0] [15:8] [23:16] [31:24] [39:32] [47:40] [55:48] [63:56] and so on

its done by using this : data_in[8*(j)+:8]

Hi Dave,

It looks ssingh’s original intention was to add a working implementation choosing 2-bit out of the data:
offset = DFF_data[ ((2*i)+1):(2*i) ];
So the equivalent should become:
offset = DFF_data[ (2*i) +: 2 ];
instead of choosing only width=1 with:
offset = DFF_data[ (2*i) +: 1 ];

1 Like