How to use array return function in Constraint?

Hi,

Please tell me how to use array return type function in constraint, Below I attached one example.

module array_return_test();
  
  class array_test;
    typedef bit [3:0] arr[$];
    bit [3:0] g_arr[$];
    rand bit [3:0] c_arr[$];
 
    constraint AAA  {
                        c_arr == test(1,2);
                    }

    function arr test(input bit [3:0] a=1,b=2);
        bit [3:0] lcl_arr[$];
        for(int i=0;i<5;i++)
          begin
            lcl_arr.push_back(a+b);
          end
        return lcl_arr;
    endfunction
  endclass
 
  array_test at= new();
  initial
    for(int i=0;i<5;i++)
    begin
      at.randomize();
      at.g_arr = at.test(i,i+1);
      $display($time," C_ARRY = %p; gbl array is %p",at.c_arr,at.g_arr);
    end
endmodule

In reply to Pawan Kalyan:

Hi,
Please tell me how to use array return type function in constraint, Below I attached one example.

module array_return_test();
class array_test;
typedef bit [3:0] arr[$];
bit [3:0] g_arr[$];
rand bit [3:0] c_arr[$];
constraint AAA  {
c_arr == test(1,2);
}
function arr test(input bit [3:0] a=1,b=2);
bit [3:0] lcl_arr[$];
for(int i=0;i<5;i++)
begin
lcl_arr.push_back(a+b);
end
return lcl_arr;
endfunction
endclass
array_test at= new();
initial
for(int i=0;i<5;i++)
begin
at.randomize();
at.g_arr = at.test(i,i+1);
$display($time," C_ARRY = %p; gbl array is %p",at.c_arr,at.g_arr);
end
endmodule

You can’t just constraint a queue equal a queue. Several options to fix this:

  1. Calculate c_arr queue at post_randomize function
  2. Declare a temporarily to hold test(1,2) and constraint:

  constraint AAA  {
    c_arr.size() == temp_arr.size();
    foreach(temp_arr[i]) c_arr[i] = temp_arr[i];
  }

In reply to Pawan Kalyan:

You can only constrain arrays by individual elements. There is no randomization in your example. Please explain why you need this to be part of a constraint. Otherwise, put your function call in pre_ or post_ randomize, or don’t use randomize at all.

Hi Dave,

I want to assign returned queue(function arr test(input bit [3:0] a=1,b=2);) to another queue in constraint(c_arr == test(1,2);), Then I will pass that queue to another return type function.

Thanks,
PK.

In reply to Pawan Kalyan:

If you just want to make an assignment, just write

c_arr = test(1m2);

Hi Dave,

Same issue coming.

module array_return_test();

class array_test;
typedef bit [3:0] arr[]; bit [3:0] g_arr[];
rand bit [3:0] c_arr[$];
int g =1;
int k =2;

constraint AAA  {
                    c_arr == test(g,k);
                    //c_arr == test(1,2);
                }

function arr test(input bit [3:0] a=1,b=2);
    bit [3:0] lcl_arr[$];
    for(int i=0;i<5;i++)
      begin
        lcl_arr.push_back(a+b);
      end
    return lcl_arr;
endfunction

endclass

array_test at= new();
initial
for(int i=0;i<5;i++)
begin
at.randomize();
at.g_arr = at.test(i,i+1);
$display($time," C_ARRY = %p; gbl array is %p",at.c_arr,at.g_arr);
end
endmodule

Results:
Parsing design file ‘fun_return.sv’
Top Level Modules:
array_return_test
No TimeScale specified

Error-[IVCB-INTVAR] Non integral variable
fun_return.sv, 11
array_return_test, “c_arr”
The variable c_arr is not an integral type.
Change the type of the variable or remove it from the constraint expression.

Error-[IVCB-INTVAR] Non integral variable
fun_return.sv, 11
array_return_test, “this.test(this.g, this.k)”
The variable this.test(this.g, this.k) is not an integral type.
Change the type of the variable or remove it from the constraint expression.
[b]
2 errors
CPU time: .083 seconds to compile

In reply to Pawan Kalyan:

Hi Dave,
Same issue coming.
module array_return_test();
class array_test;
typedef bit [3:0] arr[]; bit [3:0] g_arr[];
rand bit [3:0] c_arr[]; int g =1; int k =2; constraint AAA { c_arr == test(g,k); //c_arr == test(1,2); } function arr test(input bit [3:0] a=1,b=2); bit [3:0] lcl_arr[];
for(int i=0;i<5;i++)
begin
lcl_arr.push_back(a+b);
end
return lcl_arr;
endfunction
endclass
array_test at= new();
initial
for(int i=0;i<5;i++)
begin
at.randomize();
at.g_arr = at.test(i,i+1);
$display($time," C_ARRY = %p; gbl array is %p",at.c_arr,at.g_arr);
end
endmodule
Results:
Parsing design file ‘fun_return.sv’
Top Level Modules:
array_return_test
No TimeScale specified
Error-[IVCB-INTVAR] Non integral variable
fun_return.sv, 11
array_return_test, “c_arr”
The variable c_arr is not an integral type.
Change the type of the variable or remove it from the constraint expression.
Error-[IVCB-INTVAR] Non integral variable
fun_return.sv, 11
array_return_test, “this.test(this.g, this.k)”
The variable this.test(this.g, this.k) is not an integral type.
Change the type of the variable or remove it from the constraint expression.
[b]
2 errors
CPU time: .083 seconds to compile

As dave_59 already mentioned “You can only constrain arrays by individual elements” in your code you’re attempting to constrain the entire array, if you need to do so either do a foreach loop or assign it in the post_randomize function.

HTH,

-R