Getting Started with SystemVerilog using Verilator and Surfer
SystemVerilog is a hardware description and verification language used in digital design. This tutorial explains how to configure a simulation environment using Verilator and the Surfer waveform viewer. You will compile a 2:1 multiplexer, run the simulation executable, and inspect signal transitions.
Verilator is an open-source tool that translates Verilog and SystemVerilog code into optimized C++ or SystemC models. The C++ compiler builds these models into executable simulation binaries. Verilator provides high execution speed for cycle-accurate digital design verification. Refer to the official documentation for detailed architectural concepts.
Surfer is an open-source waveform viewer written in Rust. It provides fast waveform rendering and supports VCD, FST, and GHW simulation formats. Surfer can run natively on Linux, macOS, and Windows, or execute in a web browser using WebAssembly.
This tutorial guides you through the installation of Verilator and Surfer on Linux. You will then write a SystemVerilog multiplexer, compile the design with Verilator, and view waveforms in Surfer.
Installing Verilator
You can install Verilator using an operating system package manager or by compiling the source code directly.
Installation via Package Manager
On Ubuntu or Debian distributions, install the packaged release using apt:
sudo apt install verilator
Operating system repositories often distribute older package versions. To access recent SystemVerilog features and bug fixes, compile Verilator from source.
Installing from Source Code
Install Build Dependencies
Install the prerequisite compilers, development headers, and build utilities:
sudo apt update
sudo apt install -y git perl python3 make autoconf g++ flex bison ccache
sudo apt install -y libgoogle-perftools-dev numactl perl-doc
sudo apt install -y libfl2 libfl-dev zlib1g zlib1g-dev
sudo apt install help2man
Clone and Compile Verilator
Clone the official git repository and compile the stable branch:
git clone https://github.com/verilator/verilator
cd verilator
git checkout stable # Choose your branch
autoconf
./configure
make -j$(nproc)
sudo make install
The compilation time depends on available CPU cores and memory bandwidth. Multi-core systems complete the compilation process faster.
Verify Installation
Check the installed Verilator version:
verilator --version
The terminal displays version details and git commit identifiers:
circuit@quest:~/verilator$ verilator --version
Verilator 5.040 2025-08-30 rev v5.040-50-g0a9d9db5a
Installing Surfer
You can install Surfer using precompiled binary packages or by building from source code with Cargo.
Installing Precompiled Binaries
Download Surfer
Download the binary archive from the release repository:
wget https://gitlab.com/api/v4/projects/42073614/jobs/artifacts/main/raw/surfer_linux.zip?job=linux_build
You can also find release archives on the GitLab release page.
Extract and Install
The archive contains two binaries: surfer for local visualization and surver for remote waveform serving. Extract and install the executable files:
unzip surfer-linux.zip
sudo mv surfer /usr/local/bin/
sudo chmod +x /usr/local/bin/surfer
You can also leave the binary in a local directory and append that directory to your PATH environment variable.
Verify Installation
Check that Surfer is accessible from your shell:
surfer --version
Compiling Surfer from Source
On ARM64 architectures or custom distributions, compile Surfer from source using Rust.
Install Dependencies and Rust
Install the required development headers:
sudo apt install openssl libssl-dev pkg-config
Install the Rust toolchain using rustup:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Follow the default prompts to complete the installation. Ensure that ~/.cargo/bin is in your PATH variable.
Install Surfer with Cargo
Run cargo to fetch and build the Surfer package:
cargo install --git https://gitlab.com/surfer-project/surfer.git surfer
If the build system reports that OpenSSL cannot be found, ensure pkg-config is installed:
Could not find openssl via pkg-config:
Could not run PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 pkg-config --libs --cflags openssl
The pkg-config command could not be found.
Most likely, you need to install a pkg-config package for your OS.
Try apt install pkg-config, or yum install pkg-config, or brew install pkgconf
or pkg install pkg-config, or apk add pkgconfig depending on your distribution.
If you've already installed it, ensure the pkg-config command is one of the
directories in the PATH environment variable.
If you did not expect this build to link to a pre-installed system library,
then check documentation of the openssl-sys crate for an option to
build the library from source, or disable features or dependencies
that require pkg-config.
Install pkg-config and repeat the build command:
sudo apt install pkg-config
After installation, the binary resides in ~/.cargo/bin/surfer. Execute surfer to launch the graphical interface.
Manual Clone and Build
To build Surfer locally without installing it globally, run:
git clone git@gitlab.com:surfer-project/surfer.git
cd surfer
git submodule update --init --recursive
cargo build --release
The compiled binary will be located in the target/release/ directory.
Creating a Multiplexer Design
This section implements a 2:1 multiplexer in SystemVerilog and creates a self-checking testbench.
Create Project Directory
Create a workspace directory and navigate into it:
mkdir -p ~/designs
cd ~/designs
Create the Multiplexer Design (mux2to1.sv)
Create a file named mux2to1.sv:
// mux2to1.sv - 2:1 Multiplexer
module mux2to1 (
input logic sel, // Select signal
input logic [7:0] a, // Input A (8-bit)
input logic [7:0] b, // Input B (8-bit)
output logic [7:0] out // Output (8-bit)
);
always_comb begin
if (sel)
out = b;
else
out = a;
end
endmodule
Create the Testbench (tb_mux2to1.sv)
Create a file named tb_mux2to1.sv:
// tb_mux2to1.sv - Testbench for 2:1 Multiplexer
module tb_mux2to1;
logic sel;
logic [7:0] a;
logic [7:0] b;
logic [7:0] out;
// Instantiate the multiplexer
mux2to1 dut (
.sel(sel),
.a(a),
.b(b),
.out(out)
);
// Clock for time reference (optional)
logic clk = 0;
always #5 clk = ~clk;
// Stimulus
initial begin
// Initialize VCD dump for waveform viewing
$dumpfile("mux2to1.vcd");
$dumpvars(0, tb_mux2to1);
// Test cases
$display("Starting MUX testbench...");
// Test 1: sel=0, should select input a
sel = 0; a = 8'hAA; b = 8'h55;
#10;
$display("Test 1: sel=%b, a=%h, b=%h, out=%h (expected AA)", sel, a, b, out);
// Test 2: sel=1, should select input b
sel = 1; a = 8'hAA; b = 8'h55;
#10;
$display("Test 2: sel=%b, a=%h, b=%h, out=%h (expected 55)", sel, a, b, out);
// Test 3: Change inputs with sel=0
sel = 0; a = 8'hFF; b = 8'h00;
#10;
$display("Test 3: sel=%b, a=%h, b=%h, out=%h (expected FF)", sel, a, b, out);
// Test 4: Change inputs with sel=1
sel = 1; a = 8'hFF; b = 8'h00;
#10;
$display("Test 4: sel=%b, a=%h, b=%h, out=%h (expected 00)", sel, a, b, out);
// Test 5: Multiple transitions
sel = 0; a = 8'h12; b = 8'h34;
#10;
sel = 1;
#10;
sel = 0;
#10;
sel = 1;
#10;
$display("Testbench completed!");
$finish;
end
endmodule
Verilate and Simulate the Design
Older versions of Verilator required a separate C++ wrapper file to execute simulations. Modern Verilator versions support native SystemVerilog testbenches without manual C++ wrappers.
Use the --binary flag to generate a standalone executable directly from SystemVerilog source files:
verilator --binary --trace mux2to1.sv tb_mux2to1.sv
Verilator translates the code and compiles a native binary at ./obj_dir/Vmux2to1. The obj_dir directory contains the intermediate C++ model files.
Execute the simulation binary:
./obj_dir/Vmux2to1
The terminal displays testbench messages and simulation results:
The simulation generates a mux2to1.vcd file containing waveform data.
Viewing Waveforms with Surfer
Launch Surfer to inspect the simulation trace:
surfer mux2to1.vcd
Surfer opens the waveform interface with the tb_mux2to1 hierarchy visible in the left pane. Expand the hierarchy and drag signals into the display area.
Verify the following circuit operations:
- When
sel = 0, outputoutmatches inputa. - When
sel = 1, outputoutmatches inputb. - Output values transition immediately when select or input pins change state.
This toolchain provides an open-source SystemVerilog simulation and verification environment. Verilator provides fast compilation and execution speeds. Surfer provides modern waveform visualization for digital hardware development.