Skip to content
systemcoding.dev
Hardware

Dump Waveforms From a Testbench With $dumpfile and $dumpvars

Record simulation waveforms with $dumpfile and $dumpvars, choose which scopes to dump, switch dumping on and off, and read the VCD file Icarus Verilog writes.

6 min read
Summarize in AI
Flat illustration of logic gates wired into a small circuit, with the KiCad, Haskell and SparkFun logos.

When a simulation prints FAIL, the next question is always why, and the answer is almost never in the last line of output. It is in the history: which signal changed first, how many cycles before the bad value appeared, what the inputs were at that moment. A waveform dump records that history. Verilog has two system tasks for it, $dumpfile and $dumpvars, defined in IEEE 1364 and implemented by Icarus Verilog 12.0, Verilator 5.032 and every commercial simulator. This post shows the minimum that works, what each argument does, how to dump less (or more) on purpose, and what the resulting Value Change Dump (VCD) file contains so you can recognise a bad one.

The minimum that works

Put the two calls at the start of an initial block in the testbench, before any stimulus:

dump_tb.vverilog
`timescale 1ns/1ps

module dump_tb;
    reg        clk = 1'b0;
    reg        rst = 1'b1;
    wire [3:0] count;

    counter dut (.clk(clk), .rst(rst), .count(count));

    always #5 clk = ~clk;

    initial begin
        $dumpfile("dump.vcd");
        $dumpvars(0, dump_tb);
        #22 rst = 1'b0;
        #60 $finish;
    end
endmodule

$dumpfile takes one argument, the file name, and it must run before $dumpvars. It does not open the file by itself; it only records the name. $dumpvars starts the recording. If you swap the order, or call only $dumpvars, the simulator picks a default name (Icarus uses dump.vcd) and some tools silently dump nothing.

With the counter module from the previous posts in this cluster, build and run:

Shell
iverilog -g2012 -o dump_sim counter.v dump_tb.v
vvp dump_sim
Text
VCD info: dumpfile dump.vcd opened for output.

That message comes from Icarus's VCD writer. Verilator and commercial simulators print different text or nothing, so do not use the line to check that dumping worked. Check the file instead.

The arguments of $dumpvars

$dumpvars(levels, scope1, scope2, ...) takes a depth and a list of things to record.

CallRecords
$dumpvars;every signal in the whole design
$dumpvars(0, dump_tb);dump_tb and every level below it
$dumpvars(1, dump_tb);only signals declared directly in dump_tb
$dumpvars(2, dump_tb);dump_tb plus one level of instances inside it
$dumpvars(0, dump_tb.dut);everything inside the dut instance, none of the testbench
$dumpvars(0, dump_tb.dut.count);the single signal count

The first argument is the depth. 0 means "all levels below", and 1 means "this scope only". Any other number counts levels downward from the named scope. The remaining arguments are scopes or individual variables, named with hierarchical references. If you give a depth and no scope, the depth applies from the top of the design.

Depth matters because dump files grow with the number of signals and the number of changes. A design with a hundred instances dumped at depth 0 writes every internal wire on every clock, and you will wait for the viewer to load it. Start with $dumpvars(1, tb) plus the one dut instance you care about, then widen when the bug narrows.

verilog
initial begin
    $dumpfile("narrow.vcd");
    $dumpvars(1, dump_tb);
    $dumpvars(0, dump_tb.dut.count);
end

Calls add up: the second $dumpvars extends what the first selected rather than replacing it.

Control while the simulation runs

Four companion tasks change the recording during the run:

TaskEffect
$dumpoffstops recording; every dumped signal is written as x at that time
$dumponresumes recording and writes the current values
$dumpallwrites a checkpoint of every dumped signal's present value
$dumpflushflushes buffered data to disk without closing the file
$dumplimit(n)stops dumping when the file reaches n bytes

The practical use is skipping a boring part of the run. If a bug appears only after a long start-up sequence, switch the dump off and on around the interesting window:

window_tb.vverilog
`timescale 1ns/1ps

module window_tb;
    reg        clk = 1'b0;
    reg        rst = 1'b1;
    wire [3:0] count;

    counter dut (.clk(clk), .rst(rst), .count(count));

    always #5 clk = ~clk;

    initial begin
        $dumpfile("window.vcd");
        $dumpvars(0, window_tb);
        $dumpoff;
        #22 rst = 1'b0;
        #1000;
        $dumpon;
        #100;
        $dumpoff;
        $finish;
    end
endmodule

The file now holds only the 100 ns around time 1022 ns, plus the all-x record at time zero that $dumpoff writes. When you open it in the viewer the earlier part of the run is a flat unknown, which is the expected result and not a failed dump.

$dumpflush earns its place when a simulation crashes or you stop it with Ctrl-C. The VCD writer buffers output, and a killed process loses the buffered tail. Call $dumpflush every so often in a long run, or after a checkpoint you can not afford to lose.

What a VCD file contains

Open dump.vcd in a text editor. The file is plain text with a header and then a list of timestamped changes. The first part declares what was recorded:

Text
$date
	Mon Nov 23 10:30:00 2026
$end
$version
	Icarus Verilog
$end
$timescale
	1ps
$end
$scope module dump_tb $end
$var wire 1 ! clk $end
$var wire 1 " rst $end
$var wire 4 # count [3:0] $end
$scope module dut $end
$upscope $end
$upscope $end
$enddefinitions $end

The $timescale 1ps line is the simulator's precision, not the 1ns unit in your timescale directive. The $var lines give each signal a short identifier (!, ", #), its width and its name. The identifiers are what the change records use, which keeps the file compact. The body follows:

Text
$dumpvars
0!
1"
b0 #
$end
#5000
1!
#10000
0!
#15000
1!
#25000
0"
b1 #

Each # line is a time in the declared unit: #5000 is 5000 ps, which is 5 ns. Under it, 1! sets the one-bit signal ! (clk) to 1, and b1 # sets the four-bit vector # (count) to binary 1. Only changes are written, so a signal that holds its value for a thousand cycles costs nothing.

This format is also your debugging tool for a dump that "worked" but shows nothing. If the body after $enddefinitions is empty, the $dumpvars call came too late, named a scope that does not exist, or the run ended before the first event.

Pick a format that suits the size

VCD is text and large. Icarus can write a compressed format instead, selected at run time without touching the testbench:

Shell
vvp dump_sim -fst

The -fst option makes vvp write the Fast Signal Trace format, which GTKWave reads natively, into the same file name. Keep the .vcd extension in the testbench or rename the file to .fst yourself; GTKWave detects the format from the content, not the name. For a design that runs for millions of cycles, FST can be an order of magnitude smaller than VCD, but measure on your own design before promising a ratio to anyone.

Verilator does not run $dumpfile and $dumpvars the same way. It ignores them in the Verilog and writes traces from the C++ harness when the model was built with --trace. A later post in this cluster covers that route.

Common mistakes

  • **No $dumpvars.** The file exists, has a header, and holds no changes.

  • Dumping the design instead of the testbench. $dumpvars(0, dut) fails because dut is not a top-level name. Use the full path from the top: dump_tb.dut.

  • Two testbenches in one build. Both call $dumpfile, the last one wins, and you open the wrong file. Give each a unique name.

  • **Running past $finish.** Output after $finish never runs, so move any final $dumpall above it.

Also inDebugging
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.