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

Randomization With rand and randc

Section 6.4 introduced directed testbenches using hand-crafted input vectors. Directed tests validate expected operating conditions and known boundary cases effectively. However, engineers cannot predict every input permutation in complex designs. SystemVerilog provides automated randomization to explore vast state spaces and expose unforeseen corner-case defects.

Declaring Random Variables

Add the rand modifier to class fields to enable automated value generation:

class alu_transaction;
    rand bit [7:0] operand_a, operand_b;
    rand bit [2:0] opcode;
    bit [7:0]      expected_result;   // Not randomized: computed by the reference model
endclass

Call the built-in randomize() method to assign pseudo-random values to all rand variables:

initial begin
    alu_transaction t = new();
    repeat (5) begin
        if (!t.randomize()) begin
            $fatal("Randomization failed");
        end
        $display("a=%h b=%h op=%b", t.operand_a, t.operand_b, t.opcode);
    end
end

Every execution of randomize() creates a new combination of input variables. This automated generation produces thousands of test stimuli without manual vector authoring.

Comparing rand and randc

SystemVerilog provides two distinct randomization keywords:

  • rand: Generates a uniform random distribution across the variable range. Values may repeat across consecutive iterations.
  • randc: Generates a cyclic random permutation. Every value in the range appears exactly once before any value repeats.
class opcode_generator;
    randc bit [2:0] opcode;   // Cycles through all 8 binary values without repeating
endclass

initial begin
    opcode_generator g = new();
    repeat (8) begin
        void'(g.randomize());
        $display("opcode = %b", g.opcode);   // All 8 permutations appear once
    end
end

Use randc for small, finite state spaces like CPU opcodes, FSM state values, and arbitration ports. Avoid using randc on wide 32-bit or 64-bit data words. Cycling through billions of permutations consumes excessive simulation memory and processor time. Use standard rand for wide data buses and memory payloads.

Uncovering Corner Cases

Consider an arithmetic circuit with complex overflow logic. A verification engineer might author directed tests for zero and maximum values. However, subtle timing bugs or off-by-one errors often hide in obscure data transitions. Randomized testing executes high-volume stimulus sequences that expose unexpected edge cases.

Industry verification environments combine directed and random techniques. Engineers author directed tests for baseline smoke testing and compliance verification. They use constrained-random tests to achieve high functional coverage and expose hidden design defects.

Integrating Random Stimulus with Scoreboards

Generating random stimulus requires automated checking mechanisms. A randomized testbench must include a reference model and an automated scoreboard as described in Module 6. The scoreboard computes expected outputs and flags discrepancies immediately. Without self-checking scoreboards, randomized tests cannot verify design correctness.

Practice Exercise

A 4-bit priority encoder accepts 16 distinct input combinations. Determine whether rand or randc is the preferred modifier to verify all 16 inputs with minimum test cycles.

Solution

Use randc. The randc modifier guarantees that all 16 values appear exactly once across 16 successive randomize() calls. In contrast, standard rand selects values independently with replacement. Standard rand often requires significantly more than 16 calls to cover all values due to statistical duplication. For small, enumerable input spaces, randc provides optimal, bounded coverage.

The rand keyword enables pseudo-random stimulus generation, while randc enforces non-repeating cyclic permutations. Randomized stimulus reaches obscure hardware states that manual directed tests overlook. Always combine randomization with self-checking reference scoreboards. The next article explains how to restrict random values to valid protocol ranges using constraints.

← Classes and Objects for Verification Constraints and Constraint Solving →