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

$finish, $stop and Time

An infinite simulation run consumes compute resources and halts automated verification pipelines. Every production testbench must include an explicit termination command and a timeout watchdog. When a hardware check fails, the testbench must log the exact simulation timestamp and failure context.

Simulation Termination: $finish Versus $stop

Verilog provides two distinct termination tasks:

  • $finish: Terminates the simulation engine and returns control to the host operating system with an exit status code. Automated scripts and continuous integration runners require $finish.
  • $stop: Suspends simulation execution and opens an interactive simulator command prompt for signal inspection.
if (error_count == 0)
    $display("PASSED: all %0d tests.", test_count);
else
    $display("FAILED: %0d of %0d tests.", error_count, test_count);

$finish;

Do not depend on procedural blocks ending naturally. A free-running clock generator schedules continuous events. Without an explicit $finish call, the simulator executes indefinitely.

Measuring Simulation Time

Verilog provides three time query tasks:

  • $time: Returns a 64-bit integer representing simulation time in current timescale units. This task is the primary time query.
  • $realtime: Returns a real floating-point number, capturing fractional time steps when precision requires it.
  • $stime: A legacy 32-bit integer function that wraps quickly during long nanosecond simulations. Avoid using $stime.
`timescale 1ns / 1ps

module tb_time_demo();
    initial begin
        #10.5;
        $display("$time     = %0d", $time);       // Prints 10 (rounded integer)
        $display("$realtime = %0f", $realtime);   // Prints 10.500000 (floating point)
        $finish;
    end
endmodule

Configure timestamp formatting using $timeformat and the %t specifier:

initial $timeformat(-9, 3, " ns", 10);
$display("At %t: expected=%h actual=%h  FAIL", $time, expected, actual);

Hardware Deadlock Protection: The Watchdog Timer

If a digital design experiences a deadlock, an expected handshake signal such as done may never assert. A statement such as wait (done); stalls execution permanently.

Implement an independent watchdog timer in a parallel initial block:

`timescale 1ns / 1ps

module tb_with_watchdog();

    parameter MAX_SIM_TIME = 10_000;    // Maximum simulation timeout in ns

    reg  clk, rst_n, start;
    wire done;

    // Clock generator
    initial clk = 0;
    always #5 clk = ~clk;

    // Parallel watchdog process
    initial begin
        #MAX_SIM_TIME;
        $fatal(1, "TIMEOUT at %0t: Design deadlocked without asserting done", $time);
    end

    // Main stimulus sequence
    initial begin
        $dumpfile("tb_with_watchdog.vcd");
        $dumpvars(0, tb_with_watchdog);

        rst_n = 0; start = 0;
        repeat (4) @(posedge clk);
        rst_n = 1;

        @(posedge clk); #2;
        start = 1;
        @(posedge clk); #2;
        start = 0;

        wait (done); // If done never asserts, the watchdog fires

        $display("Completed at %t", $time);
        $finish;
    end

endmodule

Configure MAX_SIM_TIME to approximately five to ten times the expected completion duration. If the design deadlocks, the watchdog terminates the run and reports the timestamp.

Standardized Error Severity Tasks

Verilog and SystemVerilog define four severity reporting tasks:

  • $fatal(level, message): Prints a fatal error message and terminates simulation with a non-zero exit code. Argument level (0 to 2) controls diagnostic detail.
  • $error(message): Logs an error message and increments internal simulator error tallies without stopping execution.
  • $warning(message): Logs an operational warning condition.
  • $info(message): Logs informational progress notices.
// Abort simulation upon critical failure
if (actual !== expected)
    $fatal(1, "At %t: test %0d FAILED, expected=%h got=%h", $time, test_num, expected, actual);

// Log error and continue regression testing
if (actual !== expected) begin
    $error("At %t: test %0d FAILED, expected=%h got=%h", $time, test_num, expected, actual);
    error_count = error_count + 1;
end

Use $fatal for unrecoverable errors such as timeout conditions. Use $error for mismatched vector outputs when you need to gather complete regression statistics.

The next article examines file input/output tasks for logging simulation data and storing regression results.

← $display, $monitor and $strobe Writing to Files →