Constraints failure

Hi,
I want my constraint to throw error if the value of a particular variable is not within the specified range.

Consider the following snippet:

rand bit [3:0] x;

constraint x_var_constr {x inside {[3:5]};}

if the value is no in that specified range [3:5], say x is 7. I want to throw out error. How can I do that?

Thanks

I think you can do something like this.
Assuming that this code is inside a class and lets say the instance name of it is “obj”.
obj.x = 7; //Variable getting assigned from somewhere
obj.x.rand_mode(0); //Disables randomization of this variable.
if(!obj.randomize()) `uvm_error(get_type_name(),“Error Message”);

When call to randomize is executed, x won’t get randomized as rand_mode is 0 but the constraint will still get solved and will lead to failure of randomize() call.

2 Likes

A simpler thing you can do is call obj.randomize(null). This checks all the constraints without doing any randomization (returns true if the constraints are satisfied, returns false otherwise).

2 Likes