Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • Start Here6
  • SystemVerilog Language Foundations28
  • Object-Oriented SystemVerilog for Verification30
  • Constraint Randomization — Interview-Critical Deep Dive36
  • SystemVerilog Assertions — Interview-Critical Deep Dive40
  • Functional Coverage in SystemVerilog22
  • Testbench Architecture in SystemVerilog23
    • Testbench Anatomy & Layering
    • Stimulus: Generators, Transactions & Drivers
      • Transaction Design for Stimulus
      • Layered Constraint Architecture
      • Shaping Stimulus with Knobs and Policies
      • Protocol Legality and Error Injection
      • Randomizing Scenarios, Not Just Items
      • Generator Patterns
      • Driver Design
      • Mixing Directed and Random Stimulus
    • Monitors, Scoreboards & Reference Models
    • Self-Checking Tests & Regression
    • From Hand-Built TB to UVM
  • Advanced Language & Integration25
  • Senior & Interview Preparation21
  • Principal-Level Verification28
Loading lesson

‹Stimulus: Generators, Transactions & Drivers
  1. Topics
  2. SystemVerilog
  3. Testbench Architecture in SystemVerilog
  4. Stimulus: Generators, Transactions & Drivers
  5. Mixing Directed and Random Stimulus

Part 6 · Testbench Architecture · Intermediate

Mixing Directed and Random Stimulus

Directed bring-up, random volume, and targeted closure driven by coverage.

On this page

    The three-phase arc

    Projects sequence three phases instead of choosing one style. Directed proves life, constrained-random volume explores at scale, and targeted-random closes the holes that volume misses.

    Phase

    Goal

    Currency

    Directed bring-up

    TB and DUT alive

    One write, one read, reset twice

    Random volume

    Explore at scale

    Broad constraints times many seeds

    Targeted closure

    Close named holes

    Narrowed constraints per hole

    Bug rate per cycle peaks in the volume phase. Effort per bug peaks in closure, where each hole is bespoke.

    One generator for directed and random

    The next listing is not run in this course's checks. It is a fragment that uses bus_txn from the transaction lesson.

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    // not-run: fragment: bus_txn is defined in the transaction-design listing, no top module
    class mixed_generator;
      mailbox #(bus_txn) out;
      bus_txn directed_q[$];
      bus_txn blueprint;
      int unsigned n;
      event done;
      function new(mailbox #(bus_txn) out, int unsigned n);
        this.out = out; this.n = n; blueprint = new();
      endfunction
      function void add_directed(bit write, bit [15:0] addr, bit [31:0] wdata = '0);
        bus_txn t = new();
        t.kind = write ? WRITE : READ;
        t.addr = addr;
        t.wdata = wdata;
        t.source = "directed";
        t.rand_mode(0);
        directed_q.push_back(t);
      endfunction
      task run();
        int unsigned sent = 0;
        while (directed_q.size() > 0) begin
          out.put(directed_q.pop_front());
          sent++;
        end
        while (sent < n) begin
          if (!blueprint.randomize()) $fatal(1, "randomize failed");
          out.put(blueprint.clone());
          sent++;
        end
        -> done;
      endtask
    endclass
    
    class hole_txn extends bus_txn;
      constraint c_target {
        addr >= 16'hF000;
        gap == 0;
      }
    endclass

    Directed items drain first with rand_mode off, so no later solve perturbs the script. The driver never distinguishes sources because both emit one transaction class. Targeted-random pins only the hole neighbourhood and leaves all else random, so adjacent holes fall for free.

    Common mistakes

    Common pitfall

    Forgetting rand_mode(0) on injected directed items lets a later solve rewrite the script. Frozen means frozen: the solver must not touch scripted fields.

    Interview

    Likely question: directed or random?

    The arc answers it: directed to bring up, random for volume, targeted-random to close. The mixing mechanism is a directed queue ahead of a random blueprint, and the engine of phase three is merge, classify, constrain, rerun.

    Check yourself Why does targeted-random beat pure directed at closure?

    Unpinned fields keep exploring around the hole. A fully scripted test checks exactly one point and misses neighbours.

    Check yourself When does random-on-day-one hurt?

    During bring-up the TB and DUT debug mix together. Directed life checks separate bench bugs from design bugs first.

    Testbench

    A three-phase stand-in plus a testbench that checks order, volume, and targeting. A hole checker plays the role of c_target, and it must reject items outside the hole.

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    module tb_phases;
      localparam int N_DIRECTED = 2;
      localparam int N_VOLUME = 64;
      localparam int N_HOLE = 8;
      reg [15:0] directed_addr [0:1];
      reg        directed_write [0:1];
      reg [15:0] lfsr;
      reg [15:0] vol_addr [0:63];
      reg [15:0] hole_addr [0:7];
      reg [2:0]  hole_gap [0:7];
      integer i;
      integer j;
      integer problems;
      // c_target as a checker: address in the hole window, back-to-back.
      function automatic bit in_hole(input [15:0] a, input [2:0] g);
        return (a >= 16'hF000) && (g == 0);
      endfunction
      initial begin
        problems = 0;
        // phase 1: directed write then read of one address
        directed_write[0] = 1; directed_addr[0] = 16'h0000;
        directed_write[1] = 0; directed_addr[1] = 16'h0000;
        // phase 2: volume from an LFSR, which must not repeat in 64 steps
        lfsr = 16'h0001;
        for (i = 0; i < N_VOLUME; i = i + 1) begin
          vol_addr[i] = lfsr;
          lfsr = {lfsr[14:0], lfsr[15] ^ lfsr[13] ^ lfsr[12] ^ lfsr[10]};
        end
        for (i = 0; i < N_VOLUME; i = i + 1)
          for (j = i + 1; j < N_VOLUME; j = j + 1)
            if (vol_addr[i] == vol_addr[j]) problems = problems + 1;
        // phase 3: hole items pin the window and the gap, low bits still vary
        for (i = 0; i < N_HOLE; i = i + 1) begin
          hole_addr[i] = 16'hF000 | (vol_addr[i] & 16'h0FFF);
          hole_gap[i] = 0;
          if (!in_hole(hole_addr[i], hole_gap[i])) problems = problems + 1;
        end
        if (hole_addr[0] == hole_addr[N_HOLE-1]) problems = problems + 1;
        // the checker must reject items outside the hole
        if (in_hole(16'h1234, 3'd0)) problems = problems + 1;
        if (in_hole(16'hF100, 3'd2)) problems = problems + 1;
        if (problems == 0) $display("PASS: 2 directed first, 64 distinct volume, 8 back-to-back hole items");
        else $fatal(1, "FAIL: found %0d phase problems", problems);
        $finish;
      end
    endmodule

    Related topics

    • Driver Design

    • Generator Patterns

    Previous

    174 / 259

    Next: Monitors, Scoreboards & Reference Models

    Up next

    Up nextMonitors, Scoreboards & Reference Models

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Transaction Design for Stimulus
    • Layered Constraint Architecture
    • Shaping Stimulus with Knobs and Policies