How to simulate Verilog (on your computer or in a browser)
What simulating Verilog means, a minimal design and testbench, the commands to run it with Icarus Verilog, and how to do the same in a browser.
By BitFlow · · 7 min read
What simulating means
A simulator runs your hardware description on a computer: it applies inputs over time and calculates how every signal responds. You do not need a chip or a board. You write a testbench, a Verilog module that drives the design's inputs and checks or prints its outputs, and the simulator produces text output and a waveform you can inspect.
A design and a testbench
// and_gate.v
module and_gate (input wire a, input wire b, output wire y);
assign y = a & b;
endmodule
The design under test.
// tb.v
module tb;
reg a, b;
wire y;
and_gate dut (.a(a), .b(b), .y(y));
initial begin
$dumpfile("wave.vcd"); // record a waveform file
$dumpvars(0, tb);
a = 0; b = 0; #10;
a = 0; b = 1; #10;
a = 1; b = 0; #10;
a = 1; b = 1; #10;
$finish;
end
always @(a or b) #1 $display("a=%b b=%b y=%b", a, b, y);
endmodule
The testbench: it declares the inputs as reg, connects the design, steps through all four input combinations, and records the signals.
In a testbench the inputs are reg because you assign them from an initial block, and the outputs are wire. #10 waits ten time units. $dumpfile and $dumpvars write the waveform. These system tasks are for simulation only and do not become hardware.
Running it with Icarus Verilog
iverilog -o sim and_gate.v tb.v # compile
vvp sim # run
gtkwave wave.vcd # look at the waveform
Icarus Verilog is an open-source simulator. iverilog compiles, vvp runs, and GTKWave displays the .vcd file.
The output lists y for each input pair. For a larger design, Verilator is another open-source option that compiles Verilog to C++ and runs quickly, and SystemVerilog support varies by simulator, so check what yours supports.
Simulating in a browser
If you would rather not install anything, BitFlow's Sandbox does the same thing in a browser: you edit the design and testbench, press Run, and get the console output and an interactive waveform. The simulation runs on BitFlow's servers using the same open-source tools, so running code needs a free account.
Browse the Design Studio's tools (circuit editor, state-machine designer, truth tables) for free. They generate Verilog and a testbench you can run.
Open the Sandbox, paste a design and testbench, and run it.
Solve graded problems, where a hidden testbench checks your design.
Do I need to install anything to simulate Verilog?+
On your own computer you need a simulator such as Icarus Verilog or Verilator and, to view waveforms, a viewer such as GTKWave. In a browser tool such as BitFlow's Sandbox you do not install anything.
What is a testbench?+
A Verilog module with no ports that connects to your design, drives its inputs over time and checks or prints its outputs. It is used only in simulation.
What is a VCD file?+
A Value Change Dump: a text file recording every change of every signal over time. Waveform viewers such as GTKWave read it to draw the signals.