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?

Simulation in five steps: source files, compile, elaborate, simulate, then the results
Every simulator follows the same steps: compile the source files, elaborate them into one design, simulate, and read the results.
StepWhat happensTypical errors here
CompileEach file is read and checked for syntaxMissing semicolon, unknown keyword, wrong language mode
ElaborateModules are connected into one design starting from the top moduleModule not found, port width mismatch, two top modules
SimulateTime runs forward; the testbench drives inputs and checks outputsYour test's own FAIL messages, timeouts, X values

Which free simulators can run SystemVerilog?

SimulatorCostSystemVerilog supportGood for
Vivado simulator (in AMD Vivado)Free edition availableBroad, including classes, constraints and the UVM libraryFollowing this whole course, including UVM
VerilatorFree, open sourceDesign code and a growing share of testbench featuresFast simulation of large designs
Icarus VerilogFree, open sourceVerilog, plus part of SystemVerilogSmall designs and quick checks
EDA PlaygroundFree account, runs in a browserDepends on the simulator chosenTrying 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.

adder.sv
module adder (
  input  logic [7:0] a, b,
  output logic [8:0] sum
);
  assign sum = a + b;
endmodule
  • logic [7:0] is an 8-bit signal. The sum is 9 bits so the carry out of 255 + 255 is kept.
  • assign describes combinational logic: sum always equals a + b.
tb.sv
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
endmodule
  • adder dut (...) places the design inside the testbench. DUT means design under test.
  • check sets 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 is directed.
  • Directed mode runs three hand-picked cases, including the largest sum. Random mode runs 100 random pairs.
  • $finish ends the simulation.

Commands for each simulator

Icarus Verilog
iverilog -g2012 -o sim adder.sv tb.sv
vvp sim
vvp sim +MODE=random
  • iverilog compiles and elaborates; -g2012 turns on SystemVerilog; -o sim names the output.
  • vvp sim runs it. Anything after the file name starting with + is a plusarg.
Verilator
verilator --binary --timing --top-module tb adder.sv tb.sv
./obj_dir/Vtb
./obj_dir/Vtb +MODE=random
  • --binary builds a program you can run; --timing allows delays such as #1.
  • The program is written to obj_dir/ and named after the top module.
Vivado simulator
xvlog -sv adder.sv tb.sv
xelab tb -s sim
xsim sim -runall -testplusarg MODE=random
  • Three commands, one per step: xvlog compiles, xelab elaborates from the top module tb, xsim simulates.
  • -runall runs until $finish. Plusargs are passed with -testplusarg (no +).
Simulator output (the same on all three)
Running in directed mode
PASS

Running in random mode
PASS

What 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=random or +MODE=random simply choose which stimulus to send.
  • The name after + is chosen by whoever wrote the testbench. TRAFFIC and MODE are not special keywords.
  • Plusargs are read during the simulate step, so changing one does not need a new compile.
System functionReturnsUse it for
`$test$plusargs("VERBOSE")`1 if `+VERBOSE` was givenOn 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 $urandom produce 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); end to 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), so logic or int is 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.

Keep going