# Constraint Question

**URL:** <https://verificationacademy.com/forums/t/constraint-question/35507>\
**Category:** UVM\
**Tags:** UVM\
**Created:** [December 24, 2018, 8:02am UTC](https://verificationacademy.com/forums/t/constraint-question/35507 "2018-12-24T08:02:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kmishra](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/f19dbf/32.png) [@kmishra](https://verificationacademy.com/forums/u/kmishra)\
**Post date:** [December 24, 2018, 8:02am UTC](https://verificationacademy.com/forums/t/constraint-question/35507/1 "2018-12-24T08:02:58Z")

</div>

How to write constraint, where A will be divisible by 6 and it it’s true, then B should be nearest to A.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [December 24, 2018, 8:23am UTC](https://verificationacademy.com/forums/t/constraint-question/35507/2 "2018-12-24T08:23:02Z")

</div>

_In reply to [kmishra](https://verificationacademy.com/forums/t/constraint-question/35507):_

Here is most of your constrain, except I don’t know what “B should be nearest to A” means.

```verilog
(A % 6) ==0 -> (????)

```

Can you give some examples of values you are looking for that meet your constraint?

---

<div class="post-metadata">

**Author:** ![jjuxec](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jjuxec](https://verificationacademy.com/forums/u/jjuxec)\
**Post date:** [December 24, 2018, 11:45am UTC](https://verificationacademy.com/forums/t/constraint-question/35507/3 "2018-12-24T11:45:22Z")

</div>

_In reply to [kmishra](https://verificationacademy.com/forums/t/constraint-question/35507/2):_

From the statement which is “nearest”, I guess that you have another variable(s) other than A and B. The case you state says that B must be nearest to A, so if we assume that you have a sequence item which is named as “dummy\_seq\_item”, you can write sth like this:

--------------Your seq\_item code-------------------  
class dummy\_seq\_item extends uvm\_sequence\_item;  
`uvm\_object\_utils(dummy\_seq\_item)

function new(string name=“dummy\_seq\_item”);  
super.new(name);  
endfunction

rand int A;  
rand int B;  
rand int C;

constraint A\_is\_first{  
solve A before B;  
solve A before C;  
}  
constraint which\_is\_closer{  
((A%6) == 0 ) → ( B\>C );  
};

endclass

let’s say, if you have D, E and etc. other than B and C, you can write a swap function instead of right hand side of the constraint. If it returns 1, constraint will be valid.

---

<div class="post-metadata">

**Author:** ![kmishra](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/f19dbf/32.png) [@kmishra](https://verificationacademy.com/forums/u/kmishra)\
**Post date:** [December 28, 2018, 6:32am UTC](https://verificationacademy.com/forums/t/constraint-question/35507/4 "2018-12-28T06:32:16Z")

</div>

_In reply to [yuksek](https://verificationacademy.com/forums/t/constraint-question/35507/3):_

> _In reply to [kmishra](https://verificationacademy.com/forums/t/constraint-question/35507/3):_  
> From the statement which is “nearest”, I guess that you have another variable(s) other than A and B. The case you state says that B must be nearest to A, so if we assume that you have a sequence item which is named as “dummy\_seq\_item”, you can write sth like this:  
> --------------Your seq\_item code-------------------  
> class dummy\_seq\_item extends uvm\_sequence\_item;  
> `uvm\_object\_utils(dummy\_seq\_item)  
> function new(string name=“dummy\_seq\_item”);  
> super.new(name);  
> endfunction  
> rand int A;  
> rand int B;  
> rand int C;  
> constraint A\_is\_first{  
> solve A before B;  
> solve A before C;  
> }  
> constraint which\_is\_closer{  
> ((A%6) == 0 ) → ( B\>C );  
> };  
> endclass  
> let’s say, if you have D, E and etc. other than B and C, you can write a swap function instead of right hand side of the constraint. If it returns 1, constraint will be valid.

Thanks for the suggestions, got some idea now.
