Constraining Array.sum( ) for One/Two Dimesional Unpacked Array

In reply to dave_59:

Hi Dave ,

I use the Simulators at my workplace and not from EDA Playground .
Which version of Questa did you try it on ?

I didn’t quite get your reply " The alternative is converting your 2D array to a 1D array by calculating the index yourself. "

My interpretation was as follows ::

Use a Queue which has each element of A ( 2D Unapcked Array ) and then constraint the sum of Elements in Queue .

The queue ( q ) below would be ::

q = '{ A[0][0] , A[0][1] , A[1][0] , A[1][1] , A[2][0] , A[2][1] }
so I tried to Implement the logic for it inside a Function .


 
rand bit [1:0]  A[][];

rand bit [1:0] q[];

constraint SIZE_CONSTRAINT_1D { A.size()  == 3 ; }


constraint S2_2d {       foreach(A[i])
                      {
		         A[i].size() == 2 ;	  
		      }
	         }

constraint Q_SIZE { q.size() == 6 ; } // 3 X 2 from above 2 Constraints . 


constraint Value_constraint_2d {   foreach(A[i,j])
                                  {
			             A[i][j] inside { [0:1] } ;
				  }
			     }


constraint Q_VAL {  
                        foreach( q[Index] ) 
                     {
                         
                           q[Index] == FUNC(A,Index) ;
	                  
		     }

		 }    

constraint Q_SUM { q.sum() with ( int'(item) ) == 10 ; }

function int FUNC ( input bit [1:0]  A[][] , const ref int Ind );
static int Count ; // Retains its value for Different Calls
int Iter ; // Counts the no. of Iterations of foreach(A[i,j]) 
      
	   foreach(A[i,j])
	   begin
            
	    if ( Iter == Count )
	     begin   
                Iter ++ ; // Can be Commented/Skipped

	     if ( Ind == Count )
             begin
                
		Count++;
                return A[i][j];
	        
	     end
         
	   end
	  
	  else
	   
	   begin

	        Iter ++ ;

	   end
	  
	  end

endfunction



[a] This Unfortunately doesn’t work . Could you please guide me as to what goes wrong in the above Code ? .
I believe the above logic could be expanded to 3D Unpacked Arrays and so on .

[b] Other confusion is regarding [d] above in the Original Question

Why does it Fail ? 


 rand bit [1:0]  Arr[][];
 
 constraint S1D { Arr.size()  == 9 ; }
 
 constraint S2D {       foreach(Arr[i])
                      {
		         Arr[i].size() == 2 ;	  
		      }
	         }

 constraint sum_2d  {
                        
    		          FUNCTN_SUM(Arr) == 27 ;
 
  		     }
 
  typedef int unsigned intu ;
 
  function intu  FUNCTN_SUM (input bit [1:0]  Arr[][] ) ;
 
  $display(" Inside FUNCTN_SUM ");
 
  FUNCTN_SUM = Arr.sum(item) with ( item.sum() with ( int'(item) ) ) ;
 
  endfunction

LRM 18.5.12 Says :: " Function calls in active constraints are executed an unspecified number of times ( at least once ) "



So shouldn’t the function FUNCTN_SUM be called till the Constraint sum_2D is True ?

Thanks in advance .