Constraining the Sum of Even Indices of an array

Hi All,

Is it possible to constraint the sum of even indices of an array to certain fixed value(In this example sum of even array indices should be 100).

Performing sum of an even array indices by below logic.


 a.sum() with (((32'(a[item.index]))%32'h2) == 32'b0);

Code :



  class mem;
    rand bit[7:0] a[];
    rand bit[7:0] s;

    constraint array_c {
      a.size() == 10;
    }

      constraint c_sum { s == a.sum() with (((32'(a[item.index]))%32'h2) == 32'b0);  // Performing sum only on even index
        s <= 100;  // constraining sum to 100.
      }


   
   function void post_randomize();
     foreach(a[i]) begin
       $display("********************************************************");
       $display("a[%0d] =%0d",i,a[i]);
     end  
       $display("********************************************************");
   endfunction
  
  endclass

module top;
  mem mm;
  bit succ;
  initial begin
    mm = new();
    repeat(1) begin
      succ = mm.randomize();
    end  
  end
endmodule



I am getting below thing as output.

********************************************************

a[0] =139

********************************************************

a[1] =75

********************************************************

a[2] =3

********************************************************

a[3] =195

********************************************************

a[4] =211

********************************************************

a[5] =57

********************************************************

a[6] =53

********************************************************

a[7] =158

********************************************************

a[8] =35

********************************************************

a[9] =255

********************************************************

quit -f

Could anyone help me to solve this issue.

In reply to Tulasiram:

Your constraint should be

 constraint c_sum { s == a.sum() with (item.index%2 ? 0 : item);  // Performing sum only on even index
        s == 100;  // constraining sum to 100.
      }

1 Like