Function in constraint
Sometimes constraint value has to be decided based on a mathematical model or some certain calculations. There is a possibility where calculation may differ based on input provided.
Writing a complete code inside a constraint block may create confusion. So, a separate function can be written which has these mathematical calculations. A function can be called in various constraints by passing input arguments. The same function can be used as a method call by a created object or by the inherited child class.
Function in constraint example
class seq_item;
rand bit [5:0] value;
rand bit sel;
constraint value_c {value == get_values(sel);}
function bit [5:0] get_values(bit sel);
return (sel? 'h10: 'h20);
endfunction
endclass
module constraint_example;
seq_item item;
initial begin
item = new();
repeat(3) begin
item.randomize();
$display("constraint value = %0h", item.value);
end
$display("On functiopn call: value = %0h", item.get_values(1));
end
endmodule
Output:
constraint value = 10
constraint value = 20
constraint value = 10
On functiopn call: value = 10
System Verilog Tutorials