Soft Constraints

In System Verilog, soft constraints are a powerful feature used to provide default or preferred values during randomization without enforcing them strictly. They allow the randomization process to use the default constraint values only when no other constraints (hard constraints or conflicting conditions) override them. This flexibility is beneficial in scenarios where certain values are desirable but not mandatory.

What Are Soft Constraints?

Soft constraints in SystemVerilog are defined using the soft keyword within a constraint block. These constraints act as recommendations or preferences, and the randomization engine will attempt to honor them. However, if hard constraints or other conditions make the soft constraint infeasible, it will be ignored.

The syntax of a soft constraint looks like this:

constraint example_constraint {
    soft variable == value;
}

In this context:

  • The soft keyword specifies that the constraint is optional.
  • If possible, the randomization process will use the value specified by the soft constraint.
  • If the soft constraint conflicts with other constraints, it is bypassed.

Example: Soft Constraints

Let’s consider a test scenario where we want to generate packet sizes. The packets should have a size range inside of 64 to 512 bytes, but also there is an inline constraint which is saying the size should be less than 64.

class Packet;
    rand int unsigned size; // Packet size

    // Hard constraint: Packet size must be between 64 and 512 bytes
    constraint size_constraint {
      size inside {[64:512]};
    }

endclass

module tb;
    initial begin
        Packet pkt = new();

        // Randomize and print packet sizes
      for (int i=0;i<9;i++) begin
         pkt.randomize() with {size < 64;};
         $display("Randomized Packet Size: %0d", pkt.size);
        end
    end
endmodule

Output:

Error-[CNST-CIF] Constraints inconsistency failure
testbench.sv, 20
  Constraints are inconsistent and cannot be solved.
  Please check the inconsistent constraints being printed above and rewrite 
  them.

Using Soft constraint:

The above error can be solved using soft constraint.

class Packet;
    rand int unsigned size; // Packet size

    // Hard constraint: Packet size must be between 64 and 512 bytes
    constraint size_constraint {
      size inside {[64:512]};
    }

endclass

module tb;
    initial begin
        Packet pkt = new();

        // Randomize and print packet sizes
      for (int i=0;i<9;i++) begin
        pkt.randomize() with {soft size < 64;};
         $display("Randomized Packet Size: %0d", pkt.size);
        end
    end
endmodule

Output:

Randomized Packet Size: 198
Randomized Packet Size: 509
Randomized Packet Size: 345
Randomized Packet Size: 410
Randomized Packet Size: 224
Randomized Packet Size: 252
Randomized Packet Size: 72
Randomized Packet Size: 391
Randomized Packet Size: 334

Advantages of Using Soft Constraints

  1. Default Values: Soft constraints allow you to define default or preferred values without making them mandatory.
  2. Flexibility: They are ignored in the presence of conflicting hard constraints, ensuring that randomization does not fail.
  3. Improved Coverage: Soft constraints help guide randomization towards specific values while still allowing variability.