Constraint a 9x9 matrix

I need to constraint the 9x9 pixel matrix with elements to is {0,1}. The number of 0’s should be limited to 10. I tried to do this but did not work.


class my_item;
     
  rand bit a[9][9];
  
  constraint c1{
    foreach(a[i,j])
      a[i][j] inside{0,1};
  }
  
  constraint c2{
  
    a.sum() with ((item==0 ? 1 :0) == 10);
  }
        
endclass

class my_item;

rand bit a[9][9];

constraint c2{

a.sum() with (int'(item==1)) == 10;

}

endclass

In reply to UVM_SV_101:

It’s always much more helpful if you explain what “did not work” means. Did you get a compiler error, or didn’t get results you were expecting? If you did not get the results you were expecting, what were they?

You should have gotten a compiler error because the sum() method only works on one dimension of an array at a time. Each ‘item’ would be an array of nine elements and it is illegal to compare that to zero. You need to nest the sum reduction methods.

  constraint c2{ 
    a.sum(item1) with (item1.sum(item2) with (item2==0 ? 1 : 0))) <= 10;
  }

I assume limited means no more than but if you meant exactly you need to say so. And instead of the conditional operator, I think a cast is more readable.

  constraint c2{ 
    a.sum(item1) with (item1.sum(item2) with (int'(item2==0))) <= 10;
  }

Finally, the first constraint c1 is unnecessary because a single bit can only have the values 0 or 1 anyways.


module tb;
  class my_item;
 
  rand bit a[9][9];
 
    constraint c{a.sum() with ($countones(item)) ==10;}
 
 
  
endclass
  
  initial begin
    my_item m1;
    
    m1=new();
    assert(m1.randomize());
  $display ("%p",m1.a);
  end
  
endmodule

In reply to dave_59:
I tried the second method as you suggested but getting this error:

# ** Error (suppressible): testbench.sv(7): (vopt-7063) Failed to find 'D1' in hierarchical name '$root.D1.sum'.
#         Region: testbench_sv_unit
# ** Error (suppressible): testbench.sv(7): (vopt-7063) Failed to find 'D1' in hierarchical name 'D1.sum.$$'.

how should D1 declared and initialized with ?

In reply to UVM_SV_101:

module test;

class pixel;
rand bit a[9][9];

constraint abc { foreach(a[i,j])
		  a[i][j]==0 dist {0:=71 ,1:=10};}

endclass

pixel pic;

initial
begin
pic=new;

void'(pic.randomize);
$display("%p",pic);
end
endmodule

In reply to juhi_p:

In reply to dave_59:
how should D1 declared and initialized with ?

Sorry, I had a typo. D1 should have been item1. Fixed in the code above.

In reply to dave_59:

constraint c2{
a.sum(item1) with (item1.sum(item2) with (item2==0 ? 1 : 0)) <= 10);
}

It is still compilation failure and what is the item2 for ?

In reply to dave_59:

Hi Dave,

Thank you for the response. I am getting error as

Identifier 'item2' has not been declared yet. If this error is not expected,
  please check if you have set `default_nettype to none.

In reply to mehul_1111:

This works for me

item1/item2 are local variable declarations for iterating over each element. It’s very similar to the foreach(a[ index]) iterator except that ‘item’ represents the actual element, instead of just the index in the foreach.

In reply to dave_59:

constraint c2{
a.sum(item1) with (item1.sum(item2) with (int’(item2==0))) <= 10;
}

Thank you Dave .Now it is compiling . What is 10 in this , is it binary or in decimal ?


class b;
  rand bit a[9][9]; //array of array
  rand int hlpr[9];
  
   constraint c1 {
     foreach  (a[i]) { (a[i].sum(item) with(int'(item)) ) == hlpr[i] ; } //reduce it to 1D hlpr 
       hlpr.sum() with(int'(item)) >= 71; //== 10;
       solve hlpr before a;
                 } 
endclass:b

using helper array improves readability imho

In reply to rag123:


module tb;
class my_item;
rand bit a[9][9];
constraint c{a.sum() with ($countones(item)) ==10;}
endclass
initial begin
my_item m1;
m1=new();
assert(m1.randomize());
$display ("%p",m1.a);
end
endmodule



this is throwing error :

Unsupported operator 'sum over upper dimension of multi-dimensional array' in this constraint expression.
    assert(m1.randomize());

In reply to harishVL003:

In this code $countones(item), item is an unpacked array of 9 elements, each a single bit. $countones wants to see a packed array. So that is not a valid solution.

In reply to dave_59:

Your solution is working correctly. can you explain how constraint is getting evaluated here?

constraint c2{ 
  a.sum(item1) with (item1.sum(item2) with (int'(item2==0))) == 10;
  }