python-dpic · tutorial

DPI-C from scratch, then Python as a golden model

An incremental tutorial on the SystemVerilog Direct Programming Interface, measured on Verilator 5.052, ending with Python functions called as bit-exact reference models from a SystemVerilog testbench.

8steps, ≈ 2 h each
458lab checks, all passing
52measured Verilator gaps
15book errata fixed

Get started

Each step is a guide plus a lab. The guide reads in the browser (this site, or tutorial/index.html from a clone, offline). The lab is a folder of small SystemVerilog + C/Python testbenches that Verilator builds and runs; check.py asserts every number the guide quotes.

You needForMeasured with
Verilator 5.xevery lab (--binary --timing)5.052
A C/C++ compilerevery labApple clang, macOS x86_64
Python 3 with python3-configcheck.py; embedding in steps 07–083.14
numpystep 08 (FIR golden model)2.x
z3step 06 (randomize() calls it as an external solver)
ccache (optional)faster rebuilds
git clone https://github.com/leandrotozzi/python-dpic.git
cd python-dpic/tutorial/01_first_call/lab
python3 check.py        # builds every testbench in build/, runs them, asserts the results

Every guide has the same layout: text → worked examples → exercises with hidden solutions → self-graded quiz → lab walkthrough → cheat sheet. Try an exercise before opening its solution; the files it refers to are in the step's lab/ (lab/ex/ for exercises). The index on the left of each guide ticks off sections as you read; the cards below show that progress.

Everything was measured on macOS. Linux runs the same commands; the few macOS-only details are flagged on the Verilator gaps page. Commercial simulators (Questa, VCS, Xcelium) follow the same IEEE 1800 Annex H rules; where they differ from Verilator the guides say so.

Part A · DPI-C

Spear & Tumbush ch. 12

Part B · Python

the same SV imports, backed by Python

Steps 06 and 08 share two DUTs: crc32_stream.sv (CRC-32/IEEE, bit-exact with zlib.crc32) and fir8.sv (8-tap Q1.15 FIR, round half-up, saturating). Raw measurements behind every guide: NOTES.md.

References