Design verification course · Lesson 3 of 18
How to Run a SystemVerilog Simulation: Free Simulators, Compile and Run Steps
Why this matters
Simulation is where verification engineers spend most of their day: running tests, reading logs and looking at waveforms. Every later lesson in this course assumes you can compile a design and a testbench and run them.
At a glance
- A simulator runs your Verilog or SystemVerilog code as if it were hardware, one moment of time after another.
- Every simulator does three steps: compile, elaborate, simulate.
- Free options: the Vivado simulator, Verilator, Icarus Verilog, and browser-based EDA Playground.
- A plusargis an option you add to the run command, such as**
+MODE=random, that the testbench reads to change what it does. - Results come out as a log (text, with PASS or FAIL) and optionally waveforms** (signals over time).
What does a simulator do?
- Simulator: a program that executes hardware description code and computes every signal's value over time.
- Top module: the module the simulator starts from; for verification this is the testbench, which contains the design.
- Simulation time: the time inside the simulation (nanoseconds, clock cycles), not the time on your clock.
- Log: the text the testbench prints, such as PASS, FAIL and error messages.
- Waveform: a recording of signals over simulation time, viewed in a waveform viewer.
What are the steps of a simulation?
| Step | What happens | Typical errors here |
|---|---|---|
| Compile | Each file is read and checked for syntax | Missing semicolon, unknown keyword, wrong language mode |
| Elaborate | Modules are connected into one design starting from the top module | Module not found, port width mismatch, two top modules |
| Simulate | Time runs forward; the testbench drives inputs and checks outputs | Your test's own FAIL messages, timeouts, X values |
Which free simulators can run SystemVerilog?
| Simulator | Cost | SystemVerilog support | Good for |
|---|---|---|---|
| Vivado simulator (in AMD Vivado) | Free edition available | Broad, including classes, constraints and the UVM library | Following this whole course, including UVM |
| Verilator | Free, open source | Design code and a growing share of testbench features | Fast simulation of large designs |
| Icarus Verilog | Free, open source | Verilog, plus part of SystemVerilog | Small designs and quick checks |
| EDA Playground | Free account, runs in a browser | Depends on the simulator chosen | Trying code with nothing installed |
Commercial simulators from the large EDA companies are what most companies use. The language is the same, so what you learn here carries over; only the command names change.
A minimal design and testbench
The design is an 8-bit adder. The testbench gives it inputs, compares the output with the expected sum, and prints PASS or FAIL.
module adder (
input logic [7:0] a, b,
output logic [8:0] sum
);
assign sum = a + b;
endmodulelogic [7:0]is an 8-bit signal. The sum is 9 bits so the carry out of 255 + 255 is kept.assigndescribes combinational logic:sumalways equalsa + b.
module tb;
logic [7:0] a, b;
logic [8:0] sum;
int errors = 0;
string mode;
adder dut (.a(a), .b(b), .sum(sum));
task automatic check(input logic [7:0] x, y);
a = x; b = y;
#1;
if (sum !== x + y) begin
errors++;
$display("FAIL: %0d + %0d gave %0d", x, y, sum);
end
endtask
initial begin
if (!$value$plusargs("MODE=%s", mode)) mode = "directed";
$display("Running in %s mode", mode);
if (mode == "random")
repeat (100) check(8'($urandom_range(255)), 8'($urandom_range(255)));
else begin
check(0, 0);
check(255, 1);
check(255, 255);
end
if (errors == 0) $display("PASS");
else $display("FAILED with %0d errors", errors);
$finish;
end
endmoduleadder dut (...)places the design inside the testbench. DUT means design under test.checksets the inputs, waits 1 time unit (#1) for the output to settle, and compares it with the expected value.!==also catches unknown (X) values.$value$plusargs("MODE=%s", mode)reads a plusarg from the command line. If there is none, the mode isdirected.- Directed mode runs three hand-picked cases, including the largest sum. Random mode runs 100 random pairs.
$finishends the simulation.
Commands for each simulator
iverilog -g2012 -o sim adder.sv tb.sv
vvp sim
vvp sim +MODE=randomiverilogcompiles and elaborates;-g2012turns on SystemVerilog;-o simnames the output.vvp simruns it. Anything after the file name starting with+is a plusarg.
verilator --binary --timing --top-module tb adder.sv tb.sv
./obj_dir/Vtb
./obj_dir/Vtb +MODE=random--binarybuilds a program you can run;--timingallows delays such as#1.- The program is written to
obj_dir/and named after the top module.
xvlog -sv adder.sv tb.sv
xelab tb -s sim
xsim sim -runall -testplusarg MODE=random- Three commands, one per step:
xvlogcompiles,xelabelaborates from the top moduletb,xsimsimulates. -runallruns until$finish. Plusargs are passed with-testplusarg(no+).
Running in directed mode
PASS
Running in random mode
PASSWhat is a plusarg, such as +TRAFFIC=random?
- A plusarg is a word starting with
+added to the run command. It is not part of the code. - The testbench reads it with
$test$plusargs(is it there?) or$value$plusargs(what is its value?). - It lets one compiled testbench run many tests:
+TRAFFIC=directed,+TRAFFIC=randomor+MODE=randomsimply choose which stimulus to send. - The name after
+is chosen by whoever wrote the testbench.TRAFFICandMODEare not special keywords. - Plusargs are read during the simulate step, so changing one does not need a new compile.
| System function | Returns | Use it for |
|---|---|---|
| `$test$plusargs("VERBOSE")` | 1 if `+VERBOSE` was given | On or off switches |
| `$value$plusargs("MODE=%s", mode)` | 1 if found, and puts the value in `mode` | Choosing a test, a count or a file name |
What is a random seed?
- Random functions such as
$urandomproduce a fixed sequence from a starting number called the seed. - Same seed, same numbers: a failing random test can be rerun exactly to debug it.
- Different seeds, different numbers: a regression runs the same test with many seeds to reach more cases.
- Each simulator has an option to set the seed; always record it with every failure.
How do you see waveforms?
- Add
initial begin $dumpfile("waves.vcd"); $dumpvars(0, tb); endto the testbench to record every signal into a VCD file. - Open the file in a waveform viewer such as GTKWave (free). The Vivado simulator also has its own waveform window.
- Use waveforms to debug a failure: find the clock where the output first goes wrong, then look at the inputs just before it.
How do you read the log?
- Look for the testbench's own PASS or FAIL line first.
- For each FAIL, note the inputs printed and the simulation time.
- Warnings during compile are worth reading: a width mismatch warning often points straight at a bug.
- No PASS line at all usually means the simulation stopped early or never reached
$finish.
Common mistakes
- Forgetting the SystemVerilog flag (
-g2012,-sv), sologicorintis an error. - Two top-level modules, or the wrong one chosen, so the testbench never runs.
- Typing a plusarg with a different name or case than the testbench expects; it is silently ignored.
- Trusting PASS from a testbench that checks nothing. Plant a bug once and make sure the test fails.
Quick revision
- Compile, elaborate, simulate.
- Plusargs choose options at run time without recompiling.
- Same seed, same random numbers; record seeds of failing runs.
- Debug from the log first, then the waveform.
Exercises
The adder is changed to assign sum = {1'b0, a + b};. Which directed checks fail, and why?
255 + 1 and 255 + 255 fail (they give 0 and 254). Inside the concatenation, a + b is only 8 bits wide, so the carry is lost. 0 + 0 still passes, which is why boundary values belong in every directed test.
A random test fails once in 50 runs. What do you need to reproduce it?
The seed of the failing run. Rerunning with the same seed replays exactly the same random inputs, so the failure appears every time and can be debugged.
The simulator reports "module adder not found". Which step failed, and what is the usual cause?
Elaboration. The file containing the module was not given to the compiler, or the module name is spelled differently from where it is used.
Why use a plusarg instead of editing the testbench to switch between tests?
One compiled testbench can run every test, which saves compile time and lets a regression script choose tests and options from the command line without touching the code.
Interview questions
What are the steps of a simulation?
Compile (read and check each file), elaborate (connect the modules into one design from the top module) and simulate (run time forward and execute the testbench).
What is the difference between $test$plusargs and $value$plusargs?
$test$plusargs only reports whether a plusarg is present. $value$plusargs also reads its value into a variable, using a format such as %s or %d.
Why is === or !== used in checks instead of == or !=?
== returns X when either side has an unknown or high-impedance bit, and an if treats X as false, so a broken output could slip through. === and !== compare X and Z bits exactly.
What is random stability, and why does it matter?
Getting the same random values from the same seed every run. Without it, a failing random test could not be reproduced to debug it.
How do you debug a failing simulation?
Read the first failure in the log, note the time and inputs, open the waveform at that time, trace the wrong output back to its cause, then decide whether the bug is in the design, the testbench or the spec.
Sources
Frequently asked questions
How do I run a SystemVerilog simulation for free?
Install a free simulator such as the Vivado simulator, Verilator or Icarus Verilog, or use EDA Playground in a browser. Compile the design and testbench, elaborate from the testbench as the top module, then run the simulation.
What does +TRAFFIC=random mean in a simulation command?
It is a plusarg: an option added to the run command that the testbench reads with $value$plusargs. The testbench writer chose the name TRAFFIC, and the value random tells it which kind of stimulus to send.
What is the difference between compile and elaborate?
Compiling checks each file on its own. Elaborating connects all the modules into one design starting from the top module, which is where missing modules and port mismatches are found.