Wednesday, April 4, 2018

Flipping only 4 bits between adjacent elements in an array


Countones method simplify some constraints.. Here is another example.. The program is to constrain such a way that adjacent elements in an array should change only by 4 bits.



class b;
rand bit [7:0] b_v[32];
constraint b_c { 
   foreach(b_v[i]) {
      if(i!=0) {
         $countones(b_v[i] ^ b_v[i-1]) == 4; 
      }
   }
  }
function new();
endfunction

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


module flip_only_4bits;
reg a;
b B = new();

initial
begin
 
  $display("a=%0d",a);
  for(int i=0;i<1;i++)
  begin
    B.randomize();
  end
end
endmodule




Output:-
post_randomize b_v[0]=00011000
post_randomize b_v[1]=01110100
post_randomize b_v[2]=01101111
post_randomize b_v[3]=00111001
post_randomize b_v[4]=01010000
post_randomize b_v[5]=11110110
post_randomize b_v[6]=00110101
post_randomize b_v[7]=10010110
post_randomize b_v[8]=10101100
post_randomize b_v[9]=10110001
post_randomize b_v[10]=10101111
post_randomize b_v[11]=01001110
post_randomize b_v[12]=11000000
post_randomize b_v[13]=01011001
post_randomize b_v[14]=01101111
post_randomize b_v[15]=00011101
post_randomize b_v[16]=01110001
post_randomize b_v[17]=00100111
post_randomize b_v[18]=01000001
post_randomize b_v[19]=11001010
post_randomize b_v[20]=00101011
post_randomize b_v[21]=10011001
post_randomize b_v[22]=11011110
post_randomize b_v[23]=10100110
post_randomize b_v[24]=01100011
post_randomize b_v[25]=10100101
post_randomize b_v[26]=01111101
post_randomize b_v[27]=00100001
post_randomize b_v[28]=01011001
post_randomize b_v[29]=00000011
post_randomize b_v[30]=01001110
post_randomize b_v[31]=10111110