Simulate a Verilog Module With Icarus Verilog and View It in GTKWave
Compile a Verilog counter with Icarus Verilog 12, run it with vvp, dump a VCD file and read the waveform in GTKWave, from one empty directory.

Before a design goes anywhere near an FPGA, you simulate it. Simulation means a program reads your Verilog, pretends to be the hardware, applies inputs over time and records every signal. Icarus Verilog is a free, small simulator for that job, and GTKWave is the viewer that draws the recorded signals as waveforms. This post takes you from an empty directory to a waveform on screen using Icarus Verilog 12.0 and GTKWave 3.3, with a four-bit counter as the design under test. You need no hardware and no prior Verilog beyond knowing what a module is.
The three pieces
A simulation has three files and two programs.
The design is the module you want to check. It describes hardware: here, a counter. The testbench is another Verilog module that has no ports. It instantiates the design, drives its inputs with a clock and a reset, and decides when to stop. The dump is a file the simulator writes during the run, in the Value Change Dump (VCD) format, listing each time a signal changed and its new value. GTKWave reads that file.
The two programs are iverilog, which compiles Verilog into an intermediate file, and vvp, which executes that file. Icarus separates compiling from running, so you will type two commands before you see anything.
The design
Create a file named counter.v:
module counter (
input wire clk,
input wire rst,
output reg [3:0] count
);
always @(posedge clk) begin
if (rst)
count <= 4'd0;
else
count <= count + 4'd1;
end
endmoduleRead it line by line. The module has a clock input, a reset input and a four-bit output called count. The always @(posedge clk) block runs on every rising edge of the clock. On each edge, if rst is high the counter returns to zero; otherwise it adds one. The <= is a nonblocking assignment, the form you use for anything that stores a value on a clock edge. A four-bit counter wraps from 15 back to 0 because 15 plus one does not fit in four bits.
The testbench
The testbench generates the clock and the reset, and it names the dump file:
`timescale 1ns/1ps
module counter_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("counter.vcd");
$dumpvars(0, counter_tb);
#22 rst = 1'b0;
#200 $finish;
end
endmoduleThe timescale line says one time unit in this file is one nanosecond, with picosecond precision for fractions. always #5 clk = ~clk; flips the clock every 5 ns, so the period is 10 ns. The initial block runs once at time zero. $dumpfile names the VCD file and $dumpvars(0, counter_tb) records every signal in counter_tb and everything below it; the 0 means all levels of hierarchy. The reset is held high for 22 ns, released, and the simulation ends 200 ns later with $finish.
The instance is named dut, short for design under test, which is the convention that makes waveforms easy to read.
Compile and run
Compile both files into one executable simulation file, then run it:
iverilog -g2012 -o counter_sim counter.v counter_tb.v
vvp counter_simVCD info: dumpfile counter.vcd opened for output.-g2012 selects the SystemVerilog 2012 language generation, which accepts everything above and is a sensible default. -o names the output. If you leave the flag out, Icarus writes a file called a.out. The vvp run prints one informational line because the testbench called $dumpfile, and it stops by itself at the $finish. Check that counter.vcd now exists in the directory.
If you mistype a name, iverilog stops at compile time with a message that includes the file and line. A missing module looks like this:
counter_tb.v:9: error: Unknown module type: counter
1 error(s) during elaboration.That means counter.v was not on the command line. Add it and compile again.
Open the waveform
gtkwave counter.vcdGTKWave opens with a hierarchy tree on the left. Click counter_tb, then dut, and the signals appear in a list below. Select clk, rst and count, and press the Append button (or drag them) to put them in the waveform pane. Use Zoom Fit to see the whole run.
What you should see, working from the testbench:
| Time (ns) | Event |
|---|---|
| 0 to 22 | rst is 1 and count is 0 |
| 25 | first rising edge with rst low: count becomes 1 |
| 35 | count becomes 2 |
| 165 | count becomes 15 |
| 175 | count wraps to 0 |
The clock's rising edges are at 5, 15, 25, 35 and so on, because the clock starts at 0 and flips at 5. The reset is released at 22 ns, between the edges at 15 and 25, so the edge at 25 is the first one that counts. The edge at 5 and the edge at 15 both see reset high, so the counter stays at 0 through them.
To display count as a number rather than four separate bits, right-click it, choose Data Format, then Decimal. GTKWave remembers nothing between sessions unless you save the signal list: use File, Write Save File, and open that file with gtkwave counter.vcd counter.gtkw next time.
Why the waveform looks the way it does
Two details trip up every beginner. First, outputs change just after the clock edge, not exactly on it, because the nonblocking assignment updates the register after the current time step's evaluation. In the viewer the change lines up with the edge to within the simulator's resolution. Second, the testbench holds the reset low only after the first edges, so the first values on the screen are not "wrong"; they are the reset state doing its job.
A good habit is to start every design with a reset and to read the waveform for the reset behaviour before you read anything else. If the design's outputs are x (unknown, drawn in red in GTKWave) after reset, you have a register that nothing initialises.
Put the commands in a Makefile
You will repeat compile, run and view dozens of times, so give them names. A small Makefile keeps the file list in one place and rebuilds only when a source changes.
SRC = counter.v counter_tb.v
counter.vcd: counter_sim
vvp counter_sim
counter_sim: $(SRC)
iverilog -g2012 -o counter_sim $(SRC)
view: counter.vcd
gtkwave counter.vcd counter.gtkw
clean:
rm -f counter_sim counter.vcdRecipe lines must start with a tab character, not spaces. Running make view compiles if a source file is newer than counter_sim, runs the simulation if the executable is newer than the VCD, and opens GTKWave with the saved signal list. If counter.gtkw does not exist yet, GTKWave opens the VCD and prints a warning about the missing save file, which you can ignore until you save one.
iverilog -g2012 -o counter_sim counter.v counter_tb.v
vvp counter_sim
VCD info: dumpfile counter.vcd opened for output.
gtkwave counter.vcd counter.gtkwThe $dumpvars level argument also deserves a note, because large designs make large files. $dumpvars(0, counter_tb) dumps every level below counter_tb. $dumpvars(1, counter_tb) dumps only the signals declared directly in counter_tb and nothing inside dut. For a counter the difference is invisible, but for a design with a hundred submodules, starting at level 1 and widening on demand keeps the VCD small enough to open quickly.
A change you can try
Edit the testbench to release reset at 12 ns instead of 22. Predict the first edge on which count becomes 1 before you run it. The rising edges are at 5 and 15, so reset is already low at 15 and the counter should start at 15 ns. Recompile, run and compare. This predict-then-check loop is the habit that makes simulation useful: a waveform you did not predict is information, and a waveform you did predict is confirmation.


