User-Defined Primitives

How RyuSim simulates UDPs: the table rules, timing, limits, two-state mode and VPI.

A user-defined primitive (UDP, IEEE 1800-2023 clause 29) is a gate described by a truth table. Standard-cell libraries and gate-level netlists are built from them. RyuSim simulates combinational, level-sensitive and edge-sensitive UDPs, including tables that mix level and edge entries, with initial values, instance delays, drive strength and instance arrays.

Example

Save a rising-edge D flip-flop and a testbench as udp.sv:

primitive dff_p (q, d, clk); output q; reg q; input d, clk; table // d clk : q : q+ 1 (01) : ? : 1; 0 (01) : ? : 0; ? (?0) : ? : -; * ? : ? : -; endtable endprimitive module udp_demo; reg d = 1'b0, clk = 1'b0; wire q; dff_p u (q, d, clk); initial begin #1 d = 1'b1; #1 clk = 1'b1; #1 $display("q=%b", q); $finish; end endmodule
ryusim compile udp.sv --top udp_demo -o udp_obj ./udp_obj/build/udp_demo_sim

How a table is read

Inputs that change together

Clause 29 does not say in which order a sequential UDP processes several inputs that change in the same update, such as a clock and its data input both driven by one assignment. RyuSim applies them one at a time in the order the UDP declares its ports. A flip-flop declared (q, clk, d) sees the clock edge first and captures the old d; declared (q, d, clk) it captures the new one. Other simulators may order such changes by event arrival, so a testbench that depends on it is racy in any simulator.

A sequential UDP's output changes in the nonblocking-assignment region of the time step in which the UDP evaluates. Every UDP that a clock edge triggers therefore evaluates before any of their outputs change, so a chain of flip-flop UDPs with no delay shifts one stage per edge, as it does in other simulators. Without an instance delay, a combinational UDP's output changes immediately, like a built-in gate's. An instance delay postpones either kind of output by that delay.

Limits

RyuSim compiles each UDP's table into a lookup table when it compiles the design. A combinational UDP costs one table read per evaluation, and a sequential UDP one table read per input that changed in the update. It accepts at most 10 inputs on a combinational UDP and 9 on a sequential one, the minimum 29.3.2 requires, and rejects a wider UDP at compile time with unsupported construct: udp-input-count(#602).

Two-state mode

Under --2state a UDP looks its table up exactly as in four-state, and an x result reads as 0. See Initialization and 2-state.

VPI and cocotb

Through VPI a UDP instance is a vpiUdp object with vpiDefName, vpiPrimType (vpiSeqPrim or vpiCombPrim), vpiSize and vpiPrimTerm terminals whose values read like a gate's. Its definition (vpiUdpDefn), table entries (vpiTableEntry) and writing its state with vpi_put_value are not built; each returns a vpiError with code RYUSIM-UNSUPPORTED.

cocotb 2.1 does not map vpiUdp objects, so dut.<udp_instance> raises AttributeError. The nets connected to a UDP read and drive normally.

Not yet supported

Cell libraries usually pair UDPs with specify blocks and timing checks, which are not implemented yet (clauses 30–32). Their functional models, which have no specify block, run.