Verilog Testbench with cocotb¶
DigSim can be the simulator for cocotb testbenches. Yosys
synthesizes the Verilog design into a netlist, and DigSim simulates it in the Python
process of the tests, so there is no simulator to install and no build step to compile
the design. cocotb's tests run as they do with other simulators: await RisingEdge(dut.clk),
dut.count.value, clocks, triggers and the results file.
A proof of concept
DigSim as a cocotb simulator is experimental. python -m digsim.cocotb works with
cocotb 2.1, the release on PyPI. make SIM=digsim, the DigSim runner of
cocotb_tools.runner and --cocotb-simulator=digsim of cocotb's pytest plugin need a
cocotb with DigSim support, which is not released yet.
Install DigSim and cocotb into a virtual environment, see Installation:
pip install digsim-engine cocotb
Running the tests¶
A cocotb test module, like counter_tests.py of cocotb's
first steps, runs on a Verilog
design with:
python -m digsim.cocotb counter.sv --toplevel counter --test-modules counter_tests
The test modules are found in the current folder. digsim-cocotb is the same command.
--toplevel and --test-modules default to the environment variables COCOTB_TOPLEVEL
and COCOTB_TEST_MODULES, and the other COCOTB_ variables, like COCOTB_TEST_FILTER
and COCOTB_RESULTS_FILE, work as with other simulators. Arguments that start with +
are plusargs for the tests (cocotb.plusargs).
| Option | |
|---|---|
--toplevel NAME |
The top module, needed with several files or with -I and -D (one file: its top module) |
--test-modules NAMES |
The Python modules with the tests, separated by commas |
-I FOLDER |
A folder to include files from, after the folder of the including file |
-D NAME[=VALUE] |
A macro, like `define, also a string: '-DFILE="data/rom.hex"' |
-P NAME=VALUE |
A value for a parameter of the top module, an integer like 8, 0x10 or 8'd5 |
--functional |
Synthesize into functional blocks instead of gates, which simulates faster |
--waves FILE |
Write the waveforms to a VCD file |
--no-signals |
Leave out the named signals inside the design, and the logic that only they use, for a faster simulation |
--netlist FILE |
Only synthesize: write the netlist to a .json file, then give the file instead of the Verilog files to run the tests on it |
With cocotb's Makefiles, runner and pytest plugin¶
With a cocotb that has DigSim support, DigSim is chosen like the other simulators:
make SIM=digsim
make SIM=digsim WAVES=1 # sim_build/<toplevel>.vcd
VERILOG_INCLUDE_DIRS gives include folders, and COMPILE_ARGS can give macros and
parameters as -DNAME=VALUE and -PNAME=VALUE. SIM_ARGS=--no-signals leaves out the
signals inside the design. With the runner, build_args=["--no-signals"] leaves them out
of the netlist, and test_args=["--no-signals"] out of the simulation.
The Python runner synthesizes the design in build(), to sim_build/<toplevel>.json,
and test() runs the tests on that netlist (it synthesizes again for other parameters):
from cocotb_tools.runner import get_runner
runner = get_runner("digsim")
runner.build(sources=["counter.sv"], hdl_toplevel="counter")
runner.test(test_module="counter_tests", hdl_toplevel="counter", waves=True)
cocotb's pytest plugin selects DigSim with --cocotb-simulator=digsim, or finds it with
auto (the digsim-cocotb command) when no other simulator is installed.
What the tests see¶
- The ports of the top module, and the named signals inside it, the wires and registers:
dut.valuefor a register of the top module,dut.u_sub.signalfor one of the submoduleu_sub. Triggers likeRisingEdgework on all of them. They are kept with the logic that drives them, also when no output depends on them, seekeep_signalsin Synthesis. A submodule without signals of its own is not seen, nor a generate block. - A vector is a packed object (
PackedArrayObject,PackedObjectin cocotb 2.1), with the range and the signedness of its declaration:dut.down[11]is the bit of index 11 oflogic [11:4] down. A signal with a constant value, like a register that only aninitialblock sets, is read only. - The inputs of the top module are set with deposits, and the signals inside get
deposits (
dut.value.value = 5on a register): the signal has the value now, and the logic that drives it keeps it until it drives again, a register until its next active clock edge, a gate until one of its inputs changes. In a netlist of gates every bit has a gate of its own. A signal that is an input (assign b = a) has a buffer of its own, for its deposits. Deposits on the signals inside need digsim-native 0.6.0 with the native engine. A force, or a write to an output or a constant, is logged as an error of thegpilogger and ignored, like a write that a simulator rejects. - A value is defined, or undefined (
X) or not driven (Z) as a whole: a write of0b01X0setsX. The bits of an input can be set one at a time (dut.din[0]), a vector with an undefined bit reads asX. - An
enumdeclared without a type (anint, signed) reads as unsigned: Yosys doesn't mark it signed. - One time step is a picosecond, and every gate has a delay of one step, so that the
gates of the netlist settle long before the next edge of a clock of the tests. A test
that reads an output in the time step of a clock edge sees its old value, as with
registers in other simulators.
await Timer(1, "ns")after the edge gives the new one. - The VCD file (
--waves) is in picoseconds, with the ports, the gates and the named signals.
Only the synthesizable subset of Verilog and SystemVerilog works: no initial blocks
with delays, no $display, no VHDL. SystemVerilog timeunit and timeprecision are
ignored, DigSim has a time of its own.