Fig. Verilator in Action: Your First Regression Test in 30 Minutes · themed illustration
Verilator is the most active open-source Verilog/SystemVerilog simulator: it compiles RTL into a C++ model, typically runs an order of magnitude faster than commercial event-driven simulators, and is free. This article gets it running — in 30 minutes.
brew install verilator;apt install verilator (the distro version is old; build from source for the latest);git clone https://github.com/verilator/verilator then build per the README, ~5 minutes.An 8-bit counter DUT (counter.sv):
module counter #(parameter W = 8) (
input logic clk, rst_n,
output logic [W-1:0] q
);
always_ff @(posedge clk or negedge rst_n)
if (!rst_n) q <= '0;
else q <= q + 1;
endmodule
C++ testbench (sim_main.cpp):
#include "Vcounter.h"
#include "verilated.h"
int main(int argc, char** argv) {
Verilated::commandArgs(argc, argv);
Vcounter dut;
int errors = 0;
for (int i = 0; i < 256; i++) {
dut.clk = 0; dut.eval();
dut.clk = 1; dut.eval();
if (dut.q != (i & 0xff)) errors++;
}
return errors ? 1 : 0;
}
Build and run:
verilator --binary --trace --timing counter.sv sim_main.cpp
./obj_dir/Vcounter
Exit code 0 means the regression passed. --trace generates a .vcd waveform viewable with GTKWave; --timing enables timing semantics such as always_ff.
--x-initial-edge or an explicit reset to avoid simulation/synthesis mismatch;--x-assign handling;#5 clk = ~clk needs the --timing switch in Verilator 5; for pure cycle simulation, drive the clock from a counter.read_verilog → synth → write_netlist) for a synthesis consistency comparison;