I am trying alternatives to using $countones / $onehot() to achieve Powers-of-Two .
Here is a Snippet from LRM Section 18.3 ::
class Bus;
rand bit[31:0] data;
endclass
Bus bus ;
initial begin
if ( bus.randomize() with { ( data & ( data - 1 ) ) == 0 ; } // Issue here !!
$display("Success")
end
I believe there is an issue with the Code in LRM above that data can also be Constrained to be 0 which is Not a Power-of-Two !!
I believe we need one more additional in-line Constraint ::
if ( bus.randomize() with { ( data & ( data - 1 ) ) == 0 ; data != 0 ; } // Equivalent to constraint :: $countones(data) == 1 ;
[Q1] Also need some suggestion to achieve the same using Shift Operator ::
class Bus;
rand bit[4:0] data;
constraint ONEHOT_LEFT_SHIFT { 1 << data ; } // [B] Need behavior similar to $countones(data) == 1'b1 ;
endclass
[B] Any suggestions what I could add in constraint ONEHOT_LEFT_SHIFT ?