Getting Started with Icarus Verilog and GTKWave

· 6 min read

Icarus Verilog is an open-source Verilog simulator used for digital design and hardware description language (HDL) verification. Combined with GTKWave, it provides an integrated simulation and waveform visualization environment. This tutorial explains how to install both tools and verify a 4:1 multiplexer design.

Icarus Verilog (iverilog) compiles Verilog source code into an executable program for simulation. It supports the IEEE-1364 Verilog standard and simulates small to medium digital designs.

GTKWave is an open-source waveform viewer. It displays Value Change Dump (VCD) files generated during simulation.

Installing Icarus Verilog

Most Linux package managers include Icarus Verilog. You can also compile the software from source code to obtain recent features.

Ubuntu and Debian Installation

Install Icarus Verilog using the Advanced Package Tool (apt):

sudo apt update
sudo apt install -y iverilog

Verify Installation

Verify the compiler installation and check the version:

iverilog -v

The command line outputs the installed version and compilation flags.

Building from Source (Optional)

Compile the software from source code to use the latest release:

# Install dependencies
sudo apt install -y autoconf gperf flex bison

# Clone repository
git clone https://github.com/steveicarus/iverilog.git
cd iverilog

# Build and install
sh autoconf.sh
./configure
make
sudo make install

Refer to the official documentation for detailed build instructions.

Installing GTKWave

GTKWave displays signal transitions recorded in VCD simulation files.

Ubuntu and Debian Installation

Install GTKWave through apt:

sudo apt install -y gtkwave

Verify Installation

Check the GTKWave version:

gtkwave --version

macOS Installation

On macOS, install GTKWave using Homebrew:

brew install gtkwave

Windows Installation

For Windows operating systems, download the official installer from the GTKWave website.

Creating a Multiplexer Design

This section implements a parameterized 4:1 multiplexer in Verilog to demonstrate the simulation flow.

Create Project Directory

Create a directory for project files and navigate into it:

mkdir -p ~/designs
cd ~/designs

Create the Multiplexer Design (mux4to1.v)

Create a file named mux4to1.v:

// mux4to1.v - 4:1 Multiplexer
module mux4to1 (
    input  wire [1:0] sel,     // 2-bit select signal
    input  wire [7:0] in0,     // Input 0 (8-bit)
    input  wire [7:0] in1,     // Input 1 (8-bit)
    input  wire [7:0] in2,     // Input 2 (8-bit)
    input  wire [7:0] in3,     // Input 3 (8-bit)
    output reg  [7:0] out      // Output (8-bit)
);

    always @(*) begin
        case (sel)
            2'b00: out = in0;
            2'b01: out = in1;
            2'b10: out = in2;
            2'b11: out = in3;
            default: out = 8'h00;
        endcase
    end

endmodule

Create the Testbench (tb_mux4to1.v)

Create a file named tb_mux4to1.v:

// tb_mux4to1.v - Testbench for 4:1 Multiplexer
`timescale 1ns/1ps

module tb_mux4to1;
    // Testbench signals
    reg  [1:0] sel;
    reg  [7:0] in0, in1, in2, in3;
    wire [7:0] out;

    // Instantiate the multiplexer (Device Under Test)
    mux4to1 dut (
        .sel(sel),
        .in0(in0),
        .in1(in1),
        .in2(in2),
        .in3(in3),
        .out(out)
    );

    // Clock for time reference
    reg clk = 0;
    always #5 clk = ~clk;  // 10ns period (100MHz)

    // Test stimulus
    initial begin
        // Initialize VCD dump for waveform viewing
        $dumpfile("mux4to1.vcd");
        $dumpvars(0, tb_mux4to1);

        // Display header
        $display("Time\tsel\tin0\tin1\tin2\tin3\tout");
        $display("================================================");

        // Initialize inputs
        sel = 2'b00;
        in0 = 8'hAA;
        in1 = 8'hBB;
        in2 = 8'hCC;
        in3 = 8'hDD;

        // Test Case 1: Select input 0
        #10;
        sel = 2'b00;
        #10;
        $display("%0t\t%b\t%h\t%h\t%h\t%h\t%h", $time, sel, in0, in1, in2, in3, out);

        // Test Case 2: Select input 1
        sel = 2'b01;
        #10;
        $display("%0t\t%b\t%h\t%h\t%h\t%h\t%h", $time, sel, in0, in1, in2, in3, out);

        // Test Case 3: Select input 2
        sel = 2'b10;
        #10;
        $display("%0t\t%b\t%h\t%h\t%h\t%h\t%h", $time, sel, in0, in1, in2, in3, out);

        // Test Case 4: Select input 3
        sel = 2'b11;
        #10;
        $display("%0t\t%b\t%h\t%h\t%h\t%h\t%h", $time, sel, in0, in1, in2, in3, out);

        // Test Case 5: Change inputs while selecting
        in0 = 8'h11;
        in1 = 8'h22;
        in2 = 8'h33;
        in3 = 8'h44;

        sel = 2'b00;
        #10;
        $display("%0t\t%b\t%h\t%h\t%h\t%h\t%h", $time, sel, in0, in1, in2, in3, out);

        sel = 2'b01;
        #10;
        $display("%0t\t%b\t%h\t%h\t%h\t%h\t%h", $time, sel, in0, in1, in2, in3, out);

        sel = 2'b10;
        #10;
        $display("%0t\t%b\t%h\t%h\t%h\t%h\t%h", $time, sel, in0, in1, in2, in3, out);

        sel = 2'b11;
        #10;
        $display("%0t\t%b\t%h\t%h\t%h\t%h\t%h", $time, sel, in0, in1, in2, in3, out);

        // Test Case 6: Rapid switching
        repeat (8) begin
            sel = $random % 4;
            #10;
            $display("%0t\t%b\t%h\t%h\t%h\t%h\t%h", $time, sel, in0, in1, in2, in3, out);
        end

        // Finish simulation
        $display("================================================");
        $display("Simulation completed successfully!");
        #10;
        $finish;
    end

    // Timeout watchdog
    initial begin
        #1000;
        $display("ERROR: Simulation timeout!");
        $finish;
    end

endmodule

Compiling and Running the Simulation

Compile with Icarus Verilog

Compile the design file and the testbench with the iverilog command:

iverilog -o mux4to1.vvp tb_mux4to1.v mux4to1.v

Command arguments:

  • -o mux4to1.vvp: Output executable file for the simulation runtime.
  • tb_mux4to1.v: Testbench source file.
  • mux4to1.v: Design under test source file.

The compiler generates mux4to1.vvp, which contains intermediate code for the simulation engine.

Run the Simulation

Execute the compiled simulation using the vvp runtime engine:

vvp mux4to1.vvp

The terminal displays simulation logs and testbench output:

Icarus Verilog 4:1 Mux Simulation

The testbench generates a mux4to1.vcd file containing digital waveform data.

Viewing Waveforms with GTKWave

Open the generated VCD file in GTKWave:

gtkwave mux4to1.vcd

GTKWave opens the graphical interface with the simulation trace.

GTKWave Mux Simulation

Adding Signals to the Waveform View

To add signals to the display:

  1. Locate the SST (Signal Search Tree) pane on the left side of the window.
  2. Click tb_mux4to1 to expand the design hierarchy.
  3. Select the required signals:
    • sel
    • in0, in1, in2, in3
    • out
  4. Click Append or drag the signals into the Signals pane.

You can also use Surfer to inspect digital waveforms. Refer to the Surfer tutorial for installation instructions.

GTKWave Navigation

Use the following controls to navigate waveforms:

  • Zoom In and Out: Click the magnifying glass icons or rotate the mouse scroll wheel.
  • Zoom Fit: Click the Zoom Fit button to display the complete simulation duration.
  • Time Cursor: Click on the waveform area to place a primary time marker.
  • Measure Time: Right-click and drag the cursor to measure time intervals between edges.
  • Change Radix: Right-click a signal name, select Data Format, and choose Hexadecimal, Binary, or Decimal.
  • Search Values: Select a signal line and press Ctrl+F to search for specific data states.

Waveform Observations

Verify the following circuit operations in the waveform viewer:

  • The sel bus cycles through binary states 00, 01, 10, and 11.
  • The out bus matches the selected input (in0, in1, in2, or in3).
  • The output transitions immediately when inputs or select signals change.

Advanced Simulation Features

Adding Timing Checks

You can add timing checks to verify setup and hold constraints in the testbench:

initial begin
    // Check setup time
    $setuphold(posedge clk, sel, 1, 0.5);
end

Generating Verbose Compiler Output

Pass the -v flag to iverilog to display detailed compiler stages:

iverilog -v -o mux4to1.vvp tb_mux4to1.v mux4to1.v

Verilog System Tasks

Icarus Verilog supports standard Verilog system tasks:

$display("Message");       // Print text to the terminal
$monitor("var=%d", var);   // Print values when variables change
$time;                     // Return the current simulation time
$random;                   // Return a pseudo-random 32-bit integer
$readmemh("file.hex", mem); // Load memory arrays from hexadecimal files

Troubleshooting

Compiler Syntax Errors

If the compiler reports syntax errors, run verification with full warnings enabled:

# Check for common issues
iverilog -t null -Wall tb_mux4to1.v mux4to1.v

Missing VCD File

If the simulation does not create a VCD file, verify that the testbench contains:

$dumpfile("filename.vcd");
$dumpvars(0, module_name);

Empty Waveform Viewer

If GTKWave does not display signals:

  • Verify that the VCD file contains non-zero byte size.
  • Confirm that $dumpvars executes before signal transitions occur.
  • Select and add signals manually from the hierarchy tree.

Unresponsive Simulation

If the simulation process does not terminate:

  • Add a watchdog timer block to terminate runaway simulations.
  • Ensure the testbench calls the $finish system task.
  • Inspect combinational always blocks for circular dependencies.
← Getting Started with SystemVerilog using Verilator and Surfer What Is an Antenna Diode and Why Do We Need It? →