Module 6: Verilog for Testbench & Simulation · 6.2 Simulation-Only Constructs

$display, $monitor and $strobe

Verilog provides three primary system tasks for console output: $display, $monitor, and $strobe. These tasks execute at different phases within the discrete simulation time step. Using the incorrect printing task can display stale register values from previous simulation cycles.

Execution Timing Across Simulation Regions

1. Immediate Output with $display

The $display task executes immediately when reached in the Active simulation region. It logs signal values at that precise instant. If placed in an edge-triggered sequential block, it prints the value before nonblocking assignments (<=) resolve:

always @(posedge clk) begin
    q <= d;
    $display("q=%b", q);     // Prints the pre-update value of q
end

Use $display to log stimulus events, procedural transactions, and test status messages.

2. Settled Output with $strobe

The $strobe task defers execution until the end of the simulation time step. It prints after all nonblocking assignments and delta cycles settle:

always @(posedge clk) begin
    q <= d;
    $strobe("q=%b", q);      // Prints the updated value of q
end

Use $strobe to observe registered outputs and state machine variables at active clock edges.

3. Continuous Event Monitoring with $monitor

The $monitor task installs an ongoing signal watcher. Whenever any signal argument changes value, the simulator prints a log line at the end of that simulation time step:

initial begin
    $monitor("At %0t: clk=%b d=%b q=%b", $time, clk, d, q);
end

Only one $monitor task can remain active at any time. A subsequent call to $monitor overwrites the preceding watcher. Use $monitoroff and $monitoron to suspend and resume logging during noisy reset sequences.

Format Specifiers

Specifier Data Representation
%b Binary format
%o Octal format
%d Signed decimal format
%h Hexadecimal format
%t Simulation time, formatted by $timeformat
%s Character string format
%f, %e Floating-point real format (decimal or scientific)
%0d, %0h Suppresses leading zeroes and whitespace padding

The %0d modifier eliminates leading spaces, generating clean numerical printouts.

The %t specifier displays simulation time. Configure time display units using the $timeformat system task:

initial $timeformat(-9, 3, " ns", 10);    // Units: 10^-9 (ns), 3 decimal places, suffix, minimum width

The system task $write functions identically to $display, but omits the trailing newline character:

$write("a=%b b=%b  ", a, b);
$write("y=%b  ", y);
if (y !== expected) $write("FAIL"); else $write("pass");
$display(""); // Outputs final newline

Demonstration: Side-by-Side Timing Comparison

The following testbench illustrates the timing behavior of all three printing tasks:

`timescale 1ns / 1ps

module dff (
    input  wire clk,
    input  wire d,
    output reg  q
);
    always @(posedge clk)
        q <= d;
endmodule

module tb_print_demo();

    reg  clk, d;
    wire q;

    dff dut (.clk(clk), .d(d), .q(q));

    initial clk = 0;
    always #5 clk = ~clk;

    initial $timeformat(-9, 0, " ns", 6);

    initial begin
        $monitor("[MONITOR] %t  clk=%b d=%b q=%b", $time, clk, d, q);
    end

    initial begin
        $dumpfile("tb_print_demo.vcd");
        $dumpvars(0, tb_print_demo);

        d = 0;

        @(posedge clk);
        $display("[DISPLAY] %t  clk=%b d=%b q=%b  (pre-update value)", $time, clk, d, q);
        $strobe ("[STROBE]  %t  clk=%b d=%b q=%b  (post-update value)", $time, clk, d, q);

        #2;
        d = 1;
        $display("[DISPLAY] %t  d=1 driven, q=%b", $time, q);

        @(posedge clk);
        $display("[DISPLAY] %t  clk=%b d=%b q=%b  (pre-update value)", $time, clk, d, q);
        $strobe ("[STROBE]  %t  clk=%b d=%b q=%b  (post-update value)", $time, clk, d, q);

        #2;
        d = 0;

        @(posedge clk);
        #2;

        $finish;
    end

endmodule

The simulation generates this execution log:

[MONITOR]      0 ns  clk=0 d=x q=x
[MONITOR]      0 ns  clk=0 d=0 q=x
[MONITOR]      5 ns  clk=1 d=0 q=x
[DISPLAY]      5 ns  clk=1 d=0 q=x  (pre-update value)
[STROBE]       5 ns  clk=1 d=0 q=0  (post-update value)
[MONITOR]      5 ns  clk=1 d=0 q=0
[MONITOR]      7 ns  clk=1 d=1 q=0
[MONITOR]     10 ns  clk=0 d=1 q=0
[MONITOR]     15 ns  clk=1 d=1 q=0
[DISPLAY]     15 ns  clk=1 d=1 q=0  (pre-update value)
[STROBE]      15 ns  clk=1 d=1 q=1  (post-update value)
[MONITOR]     15 ns  clk=1 d=1 q=1

At timestamp 5 ns, $display executes in the Active region before the nonblocking assignment updates q. In contrast, $strobe evaluates after q <= d completes. Signal q transitions from x to 0, causing $monitor to trigger a second output line.

Guidelines for console logging:

  • Use $display for procedural stimulus messages and test verdicts.
  • Use $strobe to sample registered outputs at active clock edges.
  • Use $monitor for initial debugging of low-complexity signal traces. Avoid invoking $monitor inside procedural loops.

The next article examines simulation control tasks: terminating runs, reading simulation time, and implementing watchdog timers.

← Automatic Pass and Fail $finish, $stop and Time →