Bidirectional Constraint

A bidirectional constraint ensures that two or more variables satisfy a relationship that is valid regardless of which variable is assigned first. For example, if you have two variables A and B with the constraint A + B == 100, randomization will generate values for A and B such that their sum equals 100. Either A or B can be set first, and the other will adjust accordingly to satisfy the constraint.

Bidirectional constraints are naturally supported in System Verilog because of its powerful constraint-solving engine. There is no need for special syntax; they are created by defining constraints that establish dependencies between variables.

Example 1: Sum of Two Variables

Let’s consider a simple example where the sum of two variables, a and b, is constrained to be a fixed value.

class Example;
    rand int a, b;

    constraint sum_constraint {
        a + b == 100;
    }
endclass

module tb;
    initial begin
        Example ex = new();

        // Randomize and display results
      repeat(5) begin
            if (ex.randomize()) begin
                $display("Randomized Values: a = %0d, b = %0d (a + b = %0d)", ex.a, ex.b, ex.a + ex.b);
            end else begin
                $error("Randomization failed!");
            end
        end
    end
endmodule

Explanation:

  1. The sum_constraint establishes a bidirectional relationship between a and b.
  2. During randomization, either a or b can influence the other to satisfy the constraint a + b == 100.
  3. Random values for a and b will always sum to 100, regardless of the order in which they are evaluated.

Output:

Randomized Values: a = -1716839591, b = 1716839691 (a + b = 100)
Randomized Values: a = 49469230, b = -49469130 (a + b = 100)
Randomized Values: a = 778265211, b = -778265111 (a + b = 100)
Randomized Values: a = 1239380613, b = -1239380513 (a + b = 100)
Randomized Values: a = -90365624, b = 90365724 (a + b = 100)

Example 2: Conditional Relationship Between Variables

Bidirectional constraints can also model conditional relationships between variables.

class Example;
  rand bit[7:0] x, y;

    constraint conditional_constraint {
        if (x > 50) {
            y == x / 2; // If x > 50, y must be half of x
        } else {
            y inside {[0:25]}; // If x <= 50, y can be any value between 0 and 25
        }
    }
endclass

module tb;
    initial begin
        Example ex = new();

        // Randomize and display results
      repeat (5) begin
            if (ex.randomize()) begin
                $display("Randomized Values: x = %0d, y = %0d", ex.x, ex.y);
            end else begin
                $error("Randomization failed!");
            end
        end
    end
endmodule

Explanation:

  1. The conditional_constraint creates a bidirectional dependency between x and y.
  2. If x > 50, the value of y depends on x being half of it.
  3. If x <= 50, y is independently randomized within a range.

Output:

Randomized Values: x = 33, y = 9
Randomized Values: x = 47, y = 24
Randomized Values: x = 43, y = 4
Randomized Values: x = 19, y = 21
Randomized Values: x = 16, y = 15