Random variable slice

I have a variable of 4mega bits,( bit[(4x1024x1024)-1:0] msg_str).
I want to use the part select of this in code which is varying
like msg_str[1295:0],msg_str[129:0]
I cannot keep a variable like
int x = 1295;
msg_str[x:0] ;(syntax error)
what is the correct approach for this?

Thanks,
Abhishek

You can use Parameterized array size !

parameter int ARRAY_SIZE = 10; // Parameter definition

reg [ARRAY_SIZE-1:0] data; // Array using the parameter

Thanks,
Suraj

ARRAY_SIZE is random, and it depends on size of some other array
like
msg_len = msg.size();
len = (msg_lenx8) - 1;
after getting len , I have to use it in msg_str[len:0]

Do you have really a bit-type or is it a string, what you want to use. The variable name suggests the string type. Please clarify.

assuming it is bit-type operation. and variable declaration is :-

bit[(4x1024x1024)-1:0] msg_str;

If you want to use a ‘part select’ of this variable msg_str, note that SystemVerilog does not support using variables directly in part-select indices. One solution is to define a function that takes the start and end indices as arguments and returns the selected part of the bit vector.

Here is an example of such a function:

function bit [(ARRAY_SIZE-1):0] get_part_select(bit [(ARRAY_SIZE-1):0] vector, int s, int e);
    bit [(ARRAY_SIZE-1):0] result;
    vector = vector << ((ARRAY_SIZE-1)-s);
    vector = vector >> ((ARRAY_SIZE-1)-s)+e;
    return vector;
  endfunction

run on edaplayground

SystemVerilog does not allow variable sized part selects. The best approach to this depends on how you plan to use the slice

If you only plan on manipulating groups of bits, I suggest using dynamic arrays of bits along with the streaming operator

module top;
  
  bit  msg_str[] = {>>{64'hABCD_EF01_2345_6789}};
  bit buffer[];
  int bytes;
  
  initial repeat (4) begin
    void'(randomize(bytes) with {width inside {[1:7]} ;});
    buffer = {>>{msg_str with [0+:bytes*8]}};
    for(int i=0;i<buffer.size()/8;i++) $write("%h ",byte'(buffer[i+:8]));
    $display;
  end
endmodule

If everything needs to be a packed array vector, you can use a mask to isolate the bits you needs to work with.

type(msg_str) mask;
mask = (1<<width) -1;

It is a bit type

Thank you for the reply,

this will return the part selected value, with appended zeros also. I don’t need that zero’s.
will give an example

bit[(4x1024x1024)-1:0] msg_str;
__msg[ ] = {'h54,'h68,'h20,'h6f};(this data is available)
msg_len = __msg.size();

    for(int l = 0; l< msg_len; l++)
	msg_str_[((msg_len-1)-l)*8+:8] = __msg[l];

(now msg_str will have …000000000000000005468206f)
I want only the valid part of the variable like
msg_str[((msg_lenx8)-1):0] , zero’s are not needed because the msg_str[((msg_lenx8)-1):0] should be used in concatenation.
({__I[127:0],__q[31:0],C[255:0],msg_str[((msg_lenx8)-1):0]}

You can try this:

  bit [(ARRAY_SIZE-1):0]  msg_str;
  bit [ARRAY_SIZE][1]       msg_str1;
  . . . . . .
  msg_str  = 'h12345678;
  msg_str1 = msg_str;
  . . . . . . 
    $display("msg_str        = 0x%h", msg_str);
    $display("msg_str1       = 0x%h", msg_str1);
    $display("msg_str1[7:0]  = 0x%h", msg_str1[ARRAY_SIZE-1-7:ARRAY_SIZE-1]);
    $display("msg_str1[15:0] = 0x%h", msg_str1[ARRAY_SIZE-1-15:ARRAY_SIZE-1]);

Output:
msg_str = 0x12345678
msg_str1 = 0x12345678
msg_str1[7:0] = 0x78
msg_str1[15:0] = 0x5678

run on edaplayground

Thank you
As you have kept
msg_str1[ARRAY_SIZE-1-7:ARRAY_SIZE-1]);
msg_str1[ARRAY_SIZE-1-15:ARRAY_SIZE-1]);
[7:0] and [15:0] , I need that as variable like [X:0] ,
msg_str1[ARRAY_SIZE-1-X:ARRAY_SIZE-1]);if we have constant value we get the result, I cannot keep a variable in place of 7 and 15

You can do

type(msg_str) mask;
mask = (1<<X) -1;
msg = {__I[127:0],__q[31:0],*C[255:0]} << X+1;
msg = msg | (msg_str & mask);

Thank you,
what should be length of the msg variable declared?
(__I =128 bits, __q=32 bits, C=256 bits, and msg_str is not fixed)
(if i declare msg as (__I+__q+C = (416 bits) + msg_str bits (not fixed))
for ex if we consider msg_str is 32 bits, 416+32 = 448 bits
bit [499:0] msg, some big value I will declare, when values are assigned, [447:0] have the value and other will be zero. If I have loaded zero’s it will affect my result.

It seem like you would be better off using dynamic unpacked arrays of bits or bytes instead of fixed sized packed arrays. But you have not shown enough of what your are trying to implement to help any further.