Module 6: Verilog for Testbench & Simulation · 6.1 Testbench Architecture

Guilty Until Proven Innocent

A clean synthesis report and closed timing constraints do not prove functional correctness. Consider a complex SPI controller: the tool completes synthesis, timing analysis passes at 100 MHz, and the bitfile configures the target FPGA. However, upon testing with a logic analyzer, the hardware fails to transmit data. Debugging complex digital logic directly on hardware pins is slow and difficult. A simulation testbench discovers functional defects rapidly before hardware implementation.

The Event-Driven Simulation Engine

The Icarus Verilog simulator (iverilog) operates as an event-driven discrete simulation engine. Every signal possesses a defined logic value. A transition in signal value constitutes an event. The simulator stores scheduled events in a time-ordered execution queue (Figure 1).

Event-driven simulation
Figure 1: Events queued by time, processed in order

When executing procedural statements:

a = 1;
#10;
a = 0;

The simulator sets variable a to 1 at current simulation time. The timing control #10 inserts an event into the queue ten time units forward. When simulation time reaches that threshold, the simulator sets a to 0.

When a clock edge toggles, all sensitive procedural blocks evaluate concurrently. Scheduled nonblocking assignments resolve at the end of the simulation time step. The simulator advances time by leaping between scheduled events. Physical digital hardware operates using identical event-driven principles.

Comparing Hardware Debugging with Software Debugging

Software developers use interactive debuggers, stack traces, and runtime print statements. Physical FPGA and ASIC hardware lacks these debugging features:

  • Internal flip-flop states transition at full clock frequency without software pauses.
  • External observation remains restricted to physical I/O pins.
  • A faulty signal on an output pin often originates from registers deep within the internal logic hierarchy.

Locating the root cause of a defect on physical hardware can take days. In simulation, an engineer can inspect any internal node or register across time in seconds.

Minimal Testbench Example

The following code verifies a two-input AND gate:

// and_gate.v: Design Under Test (DUT)
module and_gate (
    input  wire a,
    input  wire b,
    output wire y
);
    assign y = a & b;
endmodule

The testbench wrapper instantiates the DUT, applies stimulus, and logs outputs:

`timescale 1ns / 1ps

module tb_and_gate();
    reg  a, b;
    wire y;

    // Instantiate Design Under Test
    and_gate dut (
        .a(a),
        .b(b),
        .y(y)
    );

    initial begin
        $dumpfile("tb_and_gate.vcd");
        $dumpvars(0, tb_and_gate);

        a = 0; b = 0; #10;
        $display("a=%b b=%b  y=%b  (expected 0)", a, b, y);

        a = 0; b = 1; #10;
        $display("a=%b b=%b  y=%b  (expected 0)", a, b, y);

        a = 1; b = 0; #10;
        $display("a=%b b=%b  y=%b  (expected 0)", a, b, y);

        a = 1; b = 1; #10;
        $display("a=%b b=%b  y=%b  (expected 1)", a, b, y);

        $display("done");
        $finish;
    end
endmodule

Compile and execute the simulation with Icarus Verilog:

iverilog -o sim_and tb_and_gate.v and_gate.v
vvp sim_and

Key characteristics of this testbench:

  1. The testbench module contains no ports because it resides at the top of the simulation hierarchy.
  2. DUT input drivers use the reg type because procedural statements in the initial block assign them.
  3. The DUT output uses the wire type to receive driven combinational values.
  4. System tasks $dumpfile and $dumpvars record signal transitions into a Value Change Dump (VCD) file.
  5. System task $display formats and prints text output.
  6. System task $finish terminates the simulation.

In this testbench context, reg indicates a variable driven by procedural code rather than a physical sequential flip-flop.

The Verification Mindset

Software development often presumes code is functional until a defect occurs. Digital hardware verification requires the opposite premise. Consider a digital design defective until thorough verification tests prove otherwise.

Every tested input combination provides functional evidence. Every untested state transition represents a potential failure mode. The ratio of tested functionality to total functionality defines verification coverage.

The next article examines the architectural structure of production testbenches, comprising four core functional components.

The Four Parts of a Testbench →