Switch-Level Modeling

How RyuSim simulates bidirectional switches, switch-connected islands and trireg charge storage.

Switch-level modeling (IEEE 1800-2023 clause 28) describes a circuit as transistors that pass signals in both directions, and as nets that store charge. RyuSim simulates the bidirectional pass switches, the strength they pass, and trireg nets. What it cannot simulate exactly, it rejects by name at compile time.

Bidirectional switches

A tran joins two nets so that a signal on either one reaches the other (28.8). A tranif1 conducts while its control is 1 and a tranif0 while it is 0; when it does not conduct, it blocks. The rtran, rtranif0 and rtranif1 forms are resistive: each hop reduces the strength of the signal it passes (28.14). Save a chain of resistive switches as chain.sv:

module chain; logic d = 1'b1; wire r0, r1, r2, r3, r4; assign r0 = d; // strong rtran t1 (r0, r1); // strong -> pull rtran t2 (r1, r2); // pull -> weak rtran t3 (r2, r3); // weak -> medium rtran t4 (r3, r4); // medium -> small initial begin #1 $display("r0=%v r1=%v r2=%v r3=%v r4=%v", r0, r1, r2, r3, r4); $finish; end endmodule
ryusim compile chain.sv --top chain -o chain_obj ./chain_obj/build/chain_sim
r0=St1 r1=Pu1 r2=We1 r3=Me1 r4=Sm1

%v prints each net's strength and value (21.2.1.4).

Switch-connected islands

The nets that switches join form a switch-connected island. RyuSim solves every net of an island together, because their values depend on each other (4.9.5), and it solves the island from scratch whenever one of its inputs changes: a driver, a pull, or a switch control. Nothing is kept from the previous solve, so a value is never held up by its own reflection. When a driver lets go, the nets it drove float, unless a trireg on the island holds a charge: stored charge is the one thing that persists from one solve to the next (see trireg charge storage).

Each net takes the strongest signal that reaches it. Two signals of equal strength and opposite value give x. A switch passes a signal's strength as follows:

Signal inThrough tran, tranif0, tranif1Through rtran, rtranif0, rtranif1 (Table 28-8)
supplystrongpull
strongstrongpull
pullpullweak
largelargemedium
weakweakmedium
mediummediumsmall
smallsmallsmall

A control that is x or z leaves the switch neither on nor off. IEEE 1800-2023 4.9.5 defines the result by trying every such switch both conducting and not conducting: a net that has the same value in every case keeps it, and any other net gets the range the cases span. A 1 that may or may not reach a net prints as StH, and a 0 as StL. RyuSim does exactly that. An island with more than 16 such switches at once (a 217-way case split) stops the simulation with the named error switch-island-x-enumeration(#523), on stderr and in ryusim-run.json, rather than return an approximate value.

A tranif may take a turn-on and a turn-off delay (28.16). The delay applies to its control: the switch starts or stops conducting that long after the control changes. Nothing is delayed through the bidirectional terminals.

Switches across module boundaries

An island often spans modules: a transmission-gate cell with inout ports, instanced in a chain. RyuSim solves such an island in the module where its nets resolve, the one that instantiates the cell, so every instance joins the same island. Save the chain as tgate.sv:

module tgate (inout a, inout b, input en); tranif1 t (a, b, en); endmodule module tgate_chain; logic dv = 1'b1, e0 = 1'b1, e1 = 1'b1, e2 = 1'b1; wire n0, n1, n2, n3; assign n0 = dv; tgate c0 (.a(n0), .b(n1), .en(e0)); tgate c1 (.a(n1), .b(n2), .en(e1)); tgate c2 (.a(n2), .b(n3), .en(e2)); initial begin #1 $display("n0=%v n3=%v", n0, n3); e1 = 1'b0; #1 $display("n0=%v n3=%v", n0, n3); $finish; end endmodule
ryusim compile tgate.sv --top tgate_chain -o tgate_obj ./tgate_obj/build/tgate_chain_sim
n0=St1 n3=St1 n0=St1 n3=HiZ

trireg charge storage

A trireg net stores charge (6.6.4). While a driver drives it, it is in the driven state and takes the driver's value. When every driver is high impedance, it is in the capacitive state: it keeps its last value at its charge strength, small, medium (the default) or large. A trireg that has never been driven holds x at its charge strength (6.7.1).

Capacitive trireg nets that a switch connects share their charge: the larger charge wins, and equal charges of different values give x (6.6.4.1). A decay time, the third delay of the declaration, turns a stored 0 or 1 into x once the net has been undriven that long; driving the net again before then cancels the decay (28.16.2). `default_decay_time sets the decay time of the trireg nets that declare none, in the modules that follow it (E.2). Save the example of Figure 28-26 as capacitor.sv:

module capacitor; logic data = 1'b1, gate = 1'b1; trireg (large) #(0, 0, 50) cap1; nmos nmos1 (cap1, data, gate); initial begin #10 gate = 1'b0; // capacitive from t=10 #5 $display("t15 cap1=%v", cap1); #50 $display("t65 cap1=%v", cap1); // decayed at t=60 $finish; end endmodule
ryusim compile capacitor.sv --top capacitor -o capacitor_obj ./capacitor_obj/build/capacitor_sim
t15 cap1=La1 t65 cap1=LaX

Primitive arrays and terminal width

Every terminal of a gate, switch or UDP connects one bit. An input terminal of a single instance may also take a constant whose value fits in one bit, such as 1 or 32'd0. A pull source is the exception: pullup and pulldown place their value on every bit of the net they connect (28.10). Any other wider connection is rejected as gate-terminal-width(#608), or udp-terminal-width(#602) for a UDP.

An array of instances connects per 28.3.6. A terminal as wide as the array gives each instance one bit, its lowest bit (bit 0) to the instance at the right-hand index, the rightmost dimension fastest. A one-bit terminal connects to every instance. Any other width is an error.

Under --2state

With --2state, no value is ever x or z, so a switch control is 0 or 1 and a switch is simply on or off. An island resolves an equal-strength tie of 0 and 1 to 1. A trireg starts at 0, keeps its last driven 0 or 1 while it is capacitive, and a decayed charge reads 0. See Initialization and 2-state.

VPI and cocotb

Each switch is a vpiSwitch object with its vpiPrimType and its terminals, and a trireg reports vpiNetType vpiTriReg. An island net reads like any net. A vpi_put_value to an island net, a deposit or a force, is refused with the RYUSIM-UNSUPPORTED error (#523), because the island's solve owns the net's value; the net keeps the value its island solves to.

What RyuSim rejects

Each of these is a compile-time error that names the construct. --unsupported=warn turns it into a warning for triage only, and the simulation then does not execute it.

ConstructWhat triggers itWhy
gate-terminal-width(#608)A gate or switch terminal wider than one bit, or a constant whose value does not fit in one bitA primitive terminal is one bit; a wider one was silently truncated or zero-extended
switch-net-delay(#523)A net delay on a net in an islandThe LRM does not say how a net delay applies across a switch
switch-wired-net(#523)A wand, wor, triand or trior net in an islandThe LRM defines no per-node wired function across a switch
switch-terminal-dynamic(#523)A switch terminal that is not a constant one-bit selectThe island's shape must be known at compile time
switch-island-hoist(#523)A switch terminal on an input or output port net, an interface net, a hierarchical name, or an instance port whose connection is not a net bitStrength crosses those connections one way only, so the island cannot be solved where its nets resolve
switch-udn-net(6.6.7, 28.8)A tran, tranif0 or tranif1 that connects nets of a user-defined nettype, whatever the data type or resolution function28.8 resolves the connected nets with the nettype's resolution function, treats the switch as off for an x or z control and reduces no strength, unlike the island solve of 4.9.5; exact lowering is tracked in #634
switch-net-force(#523)force or release of a net in an islandThe LRM does not say whether a forced value crosses a switch
countdrivers-switch-net(#523)$countdrivers of a net in an islandThe LRM does not say how an island's drivers count
trireg-port-collapse(#523)A port connection that joins a trireg and another net into one net whose charge would be stored where the net does not resolveThe charge must be stored where the net resolves (23.3.3.7, Table 23-1)
trireg-decay-delay-mode(#523)A trireg decay time under `delay_mode_zero, _path or _unitThe LRM does not say what those modes do to a decay time
default-trireg-strength(#523)`default_trireg_strengthIts 0–250 integer has no mapping onto small, medium or large (E.3)
RYUSIM-UNSUPPORTED (VPI, #523)A vpi_put_value to a net in an islandThe island's solve owns the net's value