Skip to content

Instantly share code, notes, and snippets.

@sagar5258
Last active September 20, 2017 17:20
// Randmly generate Two-hot (Having 2 one) variable having length of 5 bit
class rand_twohot #(int unsigned L = 5);
rand int unsigned a;
rand int unsigned b;
logic [L-1:0] two_hot;
constraint cn_1 {
a != b;
a >= 0; a < L;
b >= 0; b < L;
}
function void post_randomize();
two_hot = (1 << a) | (1 << b);
endfunction
endclass
module top();
rand_twohot TH;
initial begin
repeat (5) begin
TH = new();
if (TH.randomize()) begin
$display ("Th.a=%0d, TH.b=%0d, TH.two_hot=%b", TH.a, TH.b, TH.two_hot);
end
else begin
$error ("Randomization failed");
end
end
end
endmodule
//Output:
// Th.a=3, TH.b=4, TH.two_hot=11000
// Th.a=1, TH.b=4, TH.two_hot=10010
// Th.a=3, TH.b=1, TH.two_hot=01010
// Th.a=2, TH.b=1, TH.two_hot=00110
// Th.a=3, TH.b=4, TH.two_hot=11000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment