All courses

Verilog · Lesson 6 of 93

A reproducible, self-checking simulation

Verilog Introduction

Define what passing means

A waveform is evidence to inspect, not an automatic pass/fail result. A useful testbench predicts the output, compares it with the DUT, reports mismatches, and terminates. A watchdog also fails a run that never completes.

The first simulation is a guided preview: modules, procedural statements, and operators are taught in detail in later chapters. Keep this working example and return to it as you learn those constructs.

Save the two modules below together in gate_tb.sv. The DUT uses ordinary Verilog-style RTL; the testbench uses the SystemVerilog $fatal task for an unambiguous failure. Therefore compile this exercise in SystemVerilog mode, even though the design itself does not require SystemVerilog.

Run all input combinations and catch a deliberate bug

Verilog
`timescale 1ns/1ps
module and_gate(input wire a, b, output wire y);
  assign y = a & b;
endmodule

module gate_tb;
  reg a, b;
  wire y;
  integer i;
  and_gate dut(.a(a), .b(b), .y(y));
  initial begin
    $dumpfile("gate.vcd");
    $dumpvars(0, gate_tb);
    for (i = 0; i < 4; i = i + 1) begin
      {a,b} = i[1:0];
      #1;
      if (y !== (a & b)) $fatal(1, "Mismatch at vector %0d", i);
    end
    $display("PASS: four gate cases");
    $finish;
  end
  initial begin
    #100;
    $fatal(1, "Timeout");
  end
endmodule

With Icarus Verilog installed, run: iverilog -g2012 -s gate_tb -o gate_sim gate_tb.sv, then vvp gate_sim. Expect one PASS line and successful termination. Open gate.vcd in a waveform viewer to correlate vectors with time.

Change the DUT operator from & to | and rerun. At least one mismatch must terminate the run; restore the correct design afterward. A test that cannot detect this deliberate defect is not checking the intended behavior.

The #1 delay allows this small combinational model to settle; it is testbench stimulus timing, not synthesizable hardware timing. Case inequality !== treats an unexpected X or Z as a mismatch rather than allowing an unknown comparison to escape an if check.

Record enough information to repeat the run

Keep the source file list, top-module name, language mode, tool version, command line, expected PASS text, and process exit result. Compile success is not test success. A VCD file alone is not proof that assertions passed.

SystemVerilog support varies by simulator and version. RTL, class randomization, covergroups, concurrent assertions, and UVM are separate capabilities; compiling a simple module does not establish support for all of them. Check the installed tool's documented features before interpreting an unsupported construct as a language error.

Further study: Icarus command-line reference and Verilator input-language support.

Further study and tool references

Check the language mode and supported features for your installed simulator.

Verilator language supportIcarus compiler flags