Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 37 ยท Testbenches

Stimulus and System Tasks

Drive a Verilog design with delays, loops and exhaustive stimulus, and print results with $display, $write, $monitor, $strobe, $time and format specifiers.

A testbench needs two skills: producing interesting inputs and showing what happened. Inputs come from initial blocks, delays, loops and (later) random numbers. Output comes from system tasks, built-in routines whose names start with $. They exist only in simulation: synthesis tools ignore them, which is exactly what you want for printing and stopping.

This lesson covers the print and control tasks you will use every day, the format specifiers, and three stimulus styles: directed, exhaustive and random. The earlier lesson on the first testbench set up the DUT-plus-stimulus structure.

No Verilog simulator runs on this site, so Verilog code is read-only. The C programs are labelled stand-ins that reproduce how the formats and the "print on change" behaviour work.

The print tasks

Task What it does
$display(...) Prints a line immediately (adds a newline)
$write(...) Like $display, no newline
$monitor(...) Registers a print that runs automatically whenever any argument changes
$strobe(...) Prints at the end of the current time step, after all assignments settled
$time, $realtime The current simulation time
$finish Ends the simulation
$stop Pauses the simulation (drops into the tool's prompt)
VERILOG
initial begin
  $display("time  a    b    sum");
  $monitor("%4t  %b %b %d", $time, a, b, sum);
  a = 4'd3; b = 4'd4;
  #10 a = 4'd9; b = 4'd9;
  #10 $finish;
end

$monitor can have only one active monitor at a time (a new call replaces the old one), and it prints once per time step in which something changed, after the time step has settled. That "settled" behaviour is the difference between $display and $strobe too: a $display right after a non-blocking assignment shows the old value, a $strobe shows the new one.

Format specifiers

Format strings work like C's printf, with hardware-specific letters:

Specifier Meaning
%b Binary (one character per bit, including x and z)
%h or %x Hexadecimal
%d Decimal (right-aligned in a field as wide as the largest value)
%0d Decimal with no padding: use this for readable output
%o Octal
%t Time, formatted by $timeformat
%s String
%m The hierarchical name of the module that called it
%c ASCII character

Plain %d of an 8-bit signal pads to three digits ( 7), which surprises people who expect 7. %0d removes the padding. Binary output of a value with unknown bits prints x, a very useful clue that something was never initialised.

The first C stand-in implements the same formats for an 8-bit value so you can see what %b, %h, %d and %0d would print.

C Stand-in: what %b, %h, %d and %0d print for 8 bits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Directed stimulus

Directed tests list the interesting cases by hand: zero, maximum, boundaries, a typical value. They are short and easy to read, and they document what you believe the design should do.

VERILOG
initial begin
  a = 0;   b = 0;   #10;   // zero
  a = 15;  b = 15;  #10;   // max + max
  a = 15;  b = 1;   #10;   // overflow case
  $finish;
end

Use #10 between patterns so the combinational logic can settle, and so waveforms show one pattern per slot.

Exhaustive stimulus

For small designs, try every input combination. A 4-bit adder with carry in has 2^9 = 512 combinations, trivial for a simulator. A loop with an integer counter does it:

VERILOG
integer i;
initial begin
  for (i = 0; i < 512; i = i + 1) begin
    {cin, a, b} = i[8:0];   // split the counter across the inputs
    #1;
  end
  $finish;
end

Packing the inputs into a concatenation {cin, a, b} and assigning a counter to it is the standard trick. An 8-bit-plus-8-bit adder has 131,072 combinations, still fast; a 32-bit one has too many, which is where random and directed tests take over.

Random stimulus

$random returns a 32-bit pseudo-random signed number; $urandom is unsigned and $urandom_range(max, min) picks within a range (SystemVerilog). Give $random a seed variable so the sequence is repeatable:

VERILOG
integer seed = 1;
initial begin
  repeat (100) begin
    a = $random(seed);
    b = $random(seed);
    #10;
  end
  $finish;
end

Random tests find combinations nobody thought of. Always print the seed on failure: a bug you cannot reproduce is a bug you cannot fix. Constrained randomization with classes is covered in SystemVerilog classes and randomization.

$monitor in miniature

The next stand-in imitates $monitor: the loop applies stimulus and prints a line only when something the monitor watches has changed since the last print. Notice that repeating a value (the second 9, 9) produces no output, as in a real simulator.

C Stand-in: $monitor prints only when a watched value changes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Only three data lines print (times 0, 20 and 40); the repeats at 10, 30 and 50 stay silent, which is exactly why $monitor is a light way to watch a waveform as text.

Stopping a simulation

Without $finish an initial block that ends does not stop the simulation if a clock generator (always #5 clk = ~clk;) keeps running: the simulator would run forever. Always end the main stimulus block with $finish. A timeout watchdog guards against a hung design:

VERILOG
initial begin
  #1_000_000;
  $display("TIMEOUT: simulation did not finish");
  $finish;
end

The underscores in numbers are only visual separators.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which task prints a value without padding when used as %0d?

    %0d is a format specifier, so any of the printing tasks accepts it. It removes the leading padding that plain %d adds.
  2. 2. How many $monitor calls can be active at once?

    A simulator keeps a single active monitor. Use $display or $strobe for additional prints.
  3. 3. a is assigned with a non-blocking assignment at this time step. Which task prints the new value of a in the same time step?

    $strobe prints at the end of the time step, after non-blocking updates; $display runs immediately and sees the old value.
  4. 4. Why print the seed when a random test fails?

    The same seed produces the same pseudo-random sequence, which makes the failure repeatable.

Summary

  • System tasks ($display, $monitor, $strobe, $finish) are simulation-only.
  • Use %b, %h, %d and %0d for readable output; x in a binary print flags uninitialised bits.
  • Directed tests cover known cases, exhaustive loops cover small designs, random tests cover the rest.
  • Seed random generators, print the seed, and always end with $finish and a timeout.

Next: Self-Checking Testbenches