Function call in System Verilog constraint

I have a constraint like this:

constraint constraint_len {
soft len == calculate_len(x);
}

x is itself a local constrained random variable.

The function “calculate_len” seems to calculate the correct length and returns it, but “len” is always coming up to be a 1.

Any hints on what may be going wrong?

Why did you use soft constraint here?
Do you have put any constraint on len variable that is always constraining len to be 1?

-UrvishP

I have also tried not to use the soft constraint. It makes no difference.
When I replace the function call in the constraint with the actual expression which calculates the lem in the function, everything works correctly. len also prints correctly from the function.
Either the value returned is not the correct calculated value or the constraint does not record the correct value which is returned.

See Verification Course Blog | VeriLog Courses | CVC

“Random variables used as function arguments shall establish an implicit variable ordering or priority. Constraints that include only variables with higher priority are solved before other, lower priority constraints. Random variables solved as part of a higher priority set of constraints become state variables to the remaining set of constraints.”

For example:

class B; 
  rand int x, y; 
  constraint C { x <= F(y); } 
  constraint D { y inside { 2, 4, 8 } ; } 
endclass

forces y to be solved before x.


Ben Cohen http://www.systemverilog.us/

  • SystemVerilog Assertions Handbook, 3rd Edition, 2013
  • A Pragmatic Approach to VMM Adoption
  • Using PSL/SUGAR … 2nd Edition
  • Real Chip Design and Verification
  • Cmpt Design by Example
  • VHDL books

In reply to sunilkakkar:

Did you figure out what was wrong in your code?
Just curious.
There was not enough of the class given for me to determine if Ben’s reply was applicable in your case (since you said that the function, which would be solved first, did indeed print correct (non 1?) values).
Or perhaps I didn’t get Ben’s explanation.

In reply to sunilkakkar:

I have a constraint like this:
constraint constraint_len {
soft len == calculate_len(x);
}
x is itself a local constrained random variable.
The function “calculate_len” seems to calculate the correct length and returns it, but “len” is always coming up to be a 1.
Any hints on what may be going wrong?

Please post more code to see what is real problem. You are using soft contraint, please make sure len variable aren’t being contrainted somewhere-else in child class.

In reply to sunilkakkar:

I have a constraint like this:
constraint constraint_len {
soft len == calculate_len(x);
}
x is itself a local constrained random variable.
The function “calculate_len” seems to calculate the correct length and returns it, but “len” is always coming up to be a 1.
Any hints on what may be going wrong?

Please check with giving return value and return type for function calculate_len().
It will work!