SV Constraint Solver

Hi All,

I was reading a book in which there is a description of the SystemVerilog Constraint Solver. Please consider the below code:

class SolveBefore;
  rand bit x; // 0 or 1
  rand bit [1:0] y; // 0, 1, 2, or 3
  constraint c_xy {
    (x==0) -> y==0;
    solve x before y;
  }
endclass

The probability distribution is as follows (The last column denotes the probability):

Solution x y  Probability
A        0 0  1/2
B        0 1  0
C        0 2  0
D        0 3  0 
E        1 0  1/8
F        1 1  1/8
G        1 2  1/8
H        1 3  1/8

Now instead of solve x before y, if I write it as solve y before x, the probabilities are as follows:

Solution x y  Probability
A        0 0  1/8
B        0 1  0
C        0 2  0
D        0 3  0 
E        1 0  1/8
F        1 1  1/4
G        1 2  1/4
H        1 3  1/4

Can anyone help me understand this behaviour?

In reply to atanu.biswas:

It may help to think of
solve x before y;
as
choose x before y;
. See this post for a better explanation.

In your x before y case, there are only two possible choices for the value x. So x has a 50% chance of being 0, and a 50% chance of being 1. Since there is only one solution with x≡0, that solution has a probability of ½. There are four solutions with x≡1, so each solution there has a probability of ½*¼ = ⅛.

In reply to atanu.biswas:

In first case : solve x before y;

x can have 2 values 0 and 1 with equal probabilities, i.e. 1/2.
Now, y can have 4 possible values since it is a 2 bit variable again with equal probabilities.
However, since there is a constraint x==0 → y==0, with solve x before y, it means that whenever x is 0, y has to be 0, i.e. when x=0, probability of y being non-zero is 0.
So, for A x=0 probability is 1/2 and y=0 probability is 1. therefore, 1/2 * 1 = 1/2.
In the next 3 cases B,C,D x=0 is 1/2, but now y cannot have any other value so, y!=0 probability is 0. therefore, B,C,D is 0.

For E,F,G,H, there is no constrain on x being 1 and y=0 or !=0. so, each has there equal probability, hence 1/2*1/4 (x==1 and y can be 0 or not) = 1/8.

In solve y before x
the constraint is still same(solution space), but the probability distribution will change due to solve y before x.
here, y will be solved first, and each value of y has 1/4 probability. After y, x will be solved which has probability of 1/2. Now, again when y=0, probability of x=0 is 1/2. so, in A and E it is 1/8. Again, when y!=0 and x=0, probability is 0. So,B,C,D is 0.
Then for F,G,H it will be 1/4 due to y(1/4) and x(1).

When you write “solve a before b” then the probability will be distributed equally among all the possible values of ‘a’.

case 1: solve x before y:

Here in your example ‘x’ has two possible values 0 and 1, so both gets 50 percent probability
since, there is a constraint stating:
if (x == 0)
(y == 0)
this will hinder all the other combinations of
x = 0 y = 1,2,3 so the possibility of getting x = 0 and y = 0 will be 1/2

case 2: vice versa of above one (solve y before x):

Here the probability will be shared equally among all the possible values of y i.e.,all the four combinations will have 0.25 probability.

The same constraint above will also be applied here and it hinders all the combinations of
x = 0 y = 1,2,3 so the probability of getting x = 0 and y = 0 will be 1/8 (0.25 * 0.5)