Write a constraint to generate a random value of 10 bits with set bits and no two set bits should be next to each other.
My idea to solve this problem was to use replication operator to randomly generate multiples of 2’b10.
Is there any better approach to solve it?
class sample;
rand bit [9:0] x;
rand bit [1:0] as;
constraint a{
as inside {[1:5]};
}
function void post_randomize();
x = {as{2'b10}};
endfunction
endclass
module tb;
sample s = new();
initial begin
repeat(10) begin
s.randomize();
$display("%b",s.x);
end
end
endmodule
I ran it on 2 simulators, and I got compile errors. When I ran it on a 3rd simulator, I got a compile warning.
You declared “as” as a 2-bit value, which means it can only take on values 0-3. It can not be 4 or 5, as your constraint allows. This means that you never set the MSB’s of “x” to 1.
Here is one way which meets your spec and runs on multiple simulators:
class sample;
rand bit [9:0] x;
constraint c {
foreach (x[i]) if (i<9) {x[i], x[i+1]} != 3;
}
endclass