Module 9: SystemVerilog for Design & Verification · 9.2 SystemVerilog Verification Basics

Constraints and Constraint Solving

Unconstrained random generation produces every mathematically possible bit pattern. However, real hardware protocols define valid ranges and forbidden states. If an ALU defines six operations, a 3-bit opcode field contains two illegal states. SystemVerilog constraint blocks instruct the built-in constraint solver to generate only valid stimulus.

Basic Range Constraints

Use the inside operator to restrict variables to a set of valid values:

class alu_transaction;
    rand bit [2:0] opcode;
    rand bit [7:0] operand_a, operand_b;

    constraint valid_opcode {
        opcode inside {[0:5]};   // Generates only opcodes 0 through 5; excludes 6 and 7
    }
endclass

When you call randomize(), the solver eliminates values outside the specified range. The solver computes solutions directly without requiring iterative rejection loops.

Relational Constraints Across Fields

Constraints can express mathematical relationships across multiple transaction fields:

class packet_transaction;
    rand bit [15:0] length;
    rand bit [15:0] payload_start;

    constraint valid_packet {
        length inside {[64:1518]};              // Standard Ethernet frame bounds
        payload_start < length;                  // Header must precede packet end
        payload_start + 46 <= length;            // Minimum payload boundary
    }
endclass

The solver analyzes all three inequalities simultaneously. It guarantees that generated pairs satisfy all protocol relationships concurrently.

Constraint Solver Conflicts

If you write conflicting constraints, the solver cannot find a valid mathematical solution. In this condition, randomize() returns zero to indicate failure:

if (!txn.randomize()) begin
    $fatal(1, "ERROR: randomize() failed: unsatisfiable constraints");
end

Always check the return status of randomize(). Unsatisfiable constraints indicate specification errors in the testbench.

Soft Constraints and Weighted Distributions

Use the soft keyword to declare default constraints that external tests can override:

constraint typical_case {
    soft opcode == 3'b000;   // Preferred default; testbenches can override
}

Use the dist operator to generate non-uniform weighted statistical distributions:

constraint burst_bias {
    burst_len dist { 1 := 70, [2:8] := 20, 16 := 10 };
}

In this example, single-beat transfers occur 70 percent of the time. Short bursts occur 20 percent of the time, and maximum bursts occur 10 percent of the time. This approach mirrors realistic bus traffic profiles while testing burst boundaries.

Practice Exercise

Author constraints for a memory transaction class. Field addr is a 10-bit address that must be 32-bit word aligned (lowest two bits zero). Field is_write must generate reads 75 percent of the time and writes 25 percent of the time.

Solution
class mem_transaction;
    rand bit [9:0] addr;
    rand bit       is_write;

    constraint aligned_addr {
        addr[1:0] == 2'b00;                   // Enforces word alignment
    }

    constraint rw_distribution {
        is_write dist { 0 := 75, 1 := 25 };   // 75% read operations, 25% write operations
    }
endclass

The aligned_addr constraint sets bits [1:0] to zero. The dist operator assigns probabilities without prohibiting write transactions.

SystemVerilog constraints restrict random variables to valid protocol subsets. The solver resolves complex mathematical dependencies across multiple variables simultaneously. Engineers use dist and soft constructs to model realistic traffic distributions. The next article introduces modular testbench architectures with drivers, monitors, and scoreboards.

← Randomization With rand and randc Toward a UVM-Ready Testbench →