class packet;
rand bit [3:0] a;
rand bit [3:0] b;
rand bit [3:0] c;
constraint a_value { a == b + c; }
constraint b_value { b > 15; }
constraint c_value { c < 10; }
endclass
module soft_constr;
initial begin
packet pkt;
pkt = new();
repeat(5) begin
pkt.randomize();
$display(“Value of a = %0d \tb = %0d \tc =%0d”,pkt.a,pkt.b,pkt.c);
end
end
endmodule
In reply to swapnilsrf9:
Because of the following constraint failure. the max value of b is 4’b1111(15). But constraint is saying > 15.
constraint b_value { b > 15; }
if you modify b width to 5 bit, you will get the correct values
class packet;
rand bit [3:0] a;
rand bit [3:0] b;
rand bit [3:0] c;
constraint b_value { b > 3; }
constraint c_value { c < 10; }
constraint a_value { a == b + c; }
endclass
module soft_constr;
initial begin
packet pkt;
pkt = new();
repeat(5) begin
pkt.randomize();
$display(“Value of a = %0d \tb = %0d \tc =%0d”,pkt.a,pkt.b,pkt.c);
end
end
endmodule
i am not getting the satisfied result of a==b+c; when b=13, c=9; what is the calculation behind it?
Again, the size of your variables is leading to overflow conditions. You need to size your variables appropriately to handle all valid conditions.
Also, the randomize() function will return a success/failure code. You should always check this:
class packet;
rand bit [4:0] a;
rand bit [3:0] b;
rand bit [3:0] c;
constraint b_value { b > 3; }
constraint c_value { c < 10; }
constraint a_value { a == b + c; }
endclass
module soft_constr;
initial begin
packet pkt;
pkt = new();
repeat(5) begin
if (!pkt.randomize()) $display("Randomization failed...");
else $display("Value of a = %0d \tb = %0d \tc =%0d",pkt.a,pkt.b,pkt.c);
end
end
endmodule
Please modify width of a to 6, because when b = 13 & c = 9, a is constrained to (b + c), according to your constraint adjusted to 4 bits.
i,e b = 4’b1101b & c = 4’b1001. So, when you add b & c, you will get 10110b, but since a is only 4 bits, the higher bits are truncated. So, you may be getting a as 6 for this randomisation. Am I correct???
Changing a to rand int, or rand bit [5:0] might solve…