Nets, Ports and Multiple Drivers
Give every signal one driver, keep tri-states at the pads, and connect instances so that a swapped port cannot hide.
You Will Be Able To
- Explain why a variable takes one driver and how a net resolves several.
- Predict the value of a wire, a
wandand aworwith two drivers. - Choose between a tri-state and a mux for a shared signal.
- Find a wrong port connection, and choose the connection style that would have refused it.
- Lesson
- R9.2
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
An interrupt flag was set in one always_ff block and cleared in another. Both simulators ran it, and the flag rose and fell. The synthesized block had no flop at all. Its interrupt pin was tied to 0.
irq (netlist) is what Synthesize built from the shipped design: 0 cells, with irq tied to constant 0.- Cycle 0
Edge 2:
setwas 1. Simulation raises the flag. The netlist has no flop to raise. - Cycle 2
Edge 4:
clrlowers the flag in simulation. The second procedure did that. - Cycle 4
Edge 6:
setandclrtogether. The brief sayssetwins. Simulation shows 0, because the second procedure wrote last. - Cycle 4
With one procedure the priority is written down, and the flag stays at 1.
Icarus 12 and Verilator 5.052 print the same simulation row. Only Lint and the synthesizer object.
A Variable Has One Driver; a Net Resolves Several
SystemVerilog has two kinds of signal. A variable (logic, bit, int) holds the last value written to it. A net (wire) holds nothing: it shows what its drivers put on it at this moment.
The two kinds are written in different places. A procedure such as always_comb or always_ff may write only variables. A net takes its value from an assign or from a port. For always_comb y = a; with y declared wire, Icarus 12 stops with y is not a valid l-value.
A net may have several drivers. The simulator combines them with a fixed table, called resolution. For a wire, two drivers that disagree give x.
- Driver
- Interconnect
- Datapath
- Design
- Data
- Structure
- Two drivers
Each
assignis a driver. Both are always on, and they disagree. - Simulation
The wire resolves 0 against 1 to x. Everything that reads
wnow sees an unknown. - The chip
Two outputs are wired together. One pulls up and the other pulls down, and current flows between them.
Read the diagram as text
- assign w = a: drives 0
- assign w = b: drives 1
- wire w: resolves its drivers
- Logic that reads w: sees x
- The chip: two gate outputs shorted
- assign w = a → wire w: 0
- assign w = b → wire w: 1
- wire w → Logic that reads w: x
- wire w → The chip: a fight
A driver that is off puts z on the wire. Then the other driver decides the value alone.
| Driver 1 | Driver 2 | wire | wand | wor |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | x | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
| 0 | z | 0 | 0 | 0 |
| 1 | z | 1 | 1 | 1 |
| 0 | x | x | 0 | x |
| 1 | x | x | x | 1 |
| z | z | z | z | z |
| z | x | x | x | x |
Measured on Icarus 12. tri is another name for wire. wand resolves like an AND gate and wor like an OR gate. Both are for pad-level wiring, never for RTL inside a block.
A variable written by two continuous assignments is an error. For two assigns on one logic, Icarus 12 stops with Unresolved net/uwire y cannot have multiple drivers. Lint warns MULTIDRIVEN.
The net type uwire asks for the same rule on a net: one driver only. Icarus 12 refuses a second driver on a uwire with the same message. Verilator 5.052 did not: it ran the design and printed a value.
One Flag, Two Procedures
Two always_ff blocks that write one variable are not two drivers in simulation. Each write replaces the value, and the last one at an edge wins. That is why the lab runs.
Hardware has no "last write". A flop has one D input.
always_ff block and cleared in another.- Neutral
- Tool
- Result
- Report
- Structure
- Simulation
Both simulators run both blocks at each edge. The flag rises on
setand falls onclr. - Lint
Lint warns
MULTIDRIVEN: the variable is written inalways_ffand also by another process. - Synthesis
The synthesizer warns
multiple conflicting driversandResolved using constant. It reports success with 0 cells.
Read the diagram as text
- Two always_ff blocks: both write irq
- Simulators: Icarus 12, Verilator 5.052
- Lint: Verilator -Wall
- Synthesizer: Yosys 0.70, slang
- A working flag: the last write wins
- MULTIDRIVEN: two warnings
- 0 cells: irq tied to 0
- Two always_ff blocks → Simulators: run both
- Two always_ff blocks → Lint: reads
- Two always_ff blocks → Synthesizer: reads
- Simulators → A working flag: run
- Lint → MULTIDRIVEN: warns
- Synthesizer → 0 cells: netlist
The synthesis run did not fail. Read its warnings, and look at the cell count.
The rule is one procedure per variable. always_ff and always_comb state that rule, and Lint checks it. Put the set, the clear and their priority in one block.
Tri-States Belong at the Pads
A tri-state driver can turn itself off: assign pad = oe ? out : 1'bz;. With oe at 0 it drives z, and another driver may use the wire. This is how one pin carries data in both directions.
Inside a block the same idea is tempting for a shared bus. Each talker gets an assign with its own enable.
An internal bus with two tri-state drivers
output wire [3:0] bus
// ...
assign bus = en_a ? a : 4'bz;
assign bus = en_b ? b : 4'bz;
Icarus 12: zzzz when nobody talks, 0xx1 when both do. Lint is silent. Synthesize on this flow refuses it: 8 $_TBUF_ cells and multiple conflicting drivers.
The same bus as a mux
output logic [3:0] bus
// ...
always_comb begin
if (en_a) bus = a;
else if (en_b) bus = b;
else bus = 4'h0;
end
A known value in every case, and a stated priority. Synthesize on this flow: 13 cells, 3 levels of logic.
The mux has two things the tri-state bus lacks. It says who wins when two enables overlap. It says what the bus carries when nobody talks.
At a pad the tri-state is right. A pad cell with one tri-state driver and an inout port builds 1 $_TBUF_ cell on this flow, with no warning.
What Kind of Signal Is a Port?
A port declared in the module header has a direction and a data type. From those two the language picks its kind: net or variable.
| Declaration | Kind | Why it matters |
|---|---|---|
input logic a | net (wire logic) | the instance above drives it; a second driver inside resolves against it |
output logic y | variable | one driver inside: an assign or one procedure |
output wire y | net | several assigns may drive it, as for a tri-state |
inout wire pad | net | both sides drive it; it must be a net |
input a (no type) | net, 1 bit, logic | the old default: a wire |
The defaults of the standard. An input is a net so that the module above can drive it through the port.
Because an input port is a net, an assign to it inside the module is a second driver. Icarus 12 compiles that. Lint stops with ASSIGNIN, and Synthesize refuses it.
Four Ways to Connect an Instance
The lesson's example is a subtractor sub8 with ports a, b, cin, d and bout. The parent holds signals with the same names and computes 9 minus 4.
By position, with a and b in the wrong order
sub8 u_sub (b, a, cin, d, bout);
Icarus 12 prints d=251 bout=1: 4 minus 9. Lint is silent, and Synthesize builds the same 52 cells as the correct instance.
By name, with .*
sub8 u_sub (.*);
Icarus 12 prints d=5 bout=0. Each port takes the parent signal with its own name, so there is no order to get wrong.
| Style | Example | Two same-width ports swapped | A name that does not exist |
|---|---|---|---|
| By position | sub8 u (a, b, cin, d, bout); | silent on all three tools | does not apply: no names are used |
| By name | sub8 u (.a(a), .b(b), ...); | cannot happen by order; a wrong pair is visible in the text | an error |
.name | sub8 u (.a, .b, .cin, .d, .bout); | cannot happen | an error on all three tools |
.* | sub8 u (.*); | cannot happen | an error on all three tools |
For .* with no cin in the parent, Icarus 12 says Wildcard named port connection (.*) did not find a matching identifier for port 3 (cin).
.name and .* also want the sizes to match. With a 4-bit cin in the parent, Synthesize refuses the .* instance. Lint warns WIDTHTRUNC. Icarus 12 ran it and used bit 0.
You can mix the styles: sub8 u (.cin(carry), .*); names the one port that differs and lets .* connect the rest.
An input that the instance leaves out is not an error. .cin() or a missing cin gives a floating input: Icarus 12 printed d=x bout=x. Lint warns PINCONNECTEMPTY or PINMISSING.
The Lab: Give the Flag One Owner
| Port | Direction | Width | Meaning |
|---|---|---|---|
| clk | input | 1 | clock |
| rst_n | input | 1 | reset, asynchronous, active-low |
| set | input | 1 | the event: raises the flag |
| clr | input | 1 | software lowers the flag |
| irq | output | 1 | the flag; stays until cleared |
- Clock
- rising edge
- Reset
rst_nasynchronous, active-low. After resetirqis 0- Corner cases
setandclrat the same edge:setwins. Done when: Run showsirq=1at edges 6 and 7, Lint is silent, and Synthesize reports 2 cells
Run as shipped
The flag rises at edge 2 and falls at edge 4. At edge 6 it reads 0, where the brief wants 1.
Lint
Two
MULTIDRIVENwarnings nameirq: written inalways_ffand by another process, with different clocking.Synthesize
The run succeeds with 0 cells. The warnings say
multiple conflicting driversandResolved using constant. The Gates tab is empty.Give it one owner
Replace the two blocks with the one below. Run shows
irq=1at edges 6 and 7. Synthesize reports 2 cells: 1 flop and 1$_OR_.
The fix: one procedure, one priority
always_ff @(posedge clk or negedge rst_n) // one procedure owns the flag
if (!rst_n) irq <= 1'b0;
else if (set) irq <= 1'b1;
else if (clr) irq <= 1'b0;Asked in interviews
What is the difference between a net and a variable?
A variable stores the last value written and takes one driver. A net stores nothing and shows the resolved value of all its drivers. Two drivers that disagree on a
wiregive X.Why avoid tri-state buses inside a chip?
Two enables can overlap and short two drivers, and an idle bus floats. A mux has one driver, a stated priority and a default. Keep tri-states for bidirectional pads.
Why connect ports by name?
By position, two ports of the same width can change places and no tool reports it. Named,
.nameand.*connections match on the port name, so order cannot matter.
Try It
The bench changes its inputs 1 ns after an edge and prints 1 ns after the next one. In simulation both blocks run at every edge, and the flag follows set and clr. At edge 6 both are 1, and the block that wrote last decided.
Synthesize reports 0 cells, 0 flops and 0 levels of logic. It found two drivers on irq, a flop and a constant 0, and kept the constant. The run counts as a success, so only the warnings and the empty Gates tab show the problem.
Synthesize the design to measure its cells and inspect the mapped gates.
e2 set=1 clr=0 irq=1 e3 set=0 clr=0 irq=1 e4 set=0 clr=1 irq=0 e5 set=0 clr=0 irq=0 e6 set=1 clr=1 irq=0 e7 set=0 clr=0 irq=0Original design · 0 cells · 0 flip-flops · 0 latches · 0 logic levels. Synthesis uses Yosys; counts depend on the toolchain.
Read included file: tb.sv
`timescale 1ns/1ps
module tb;
logic clk = 0;
always #5 clk = ~clk; // rising edges at 5, 15, 25, ...
logic rst_n = 1; // falls at 1 ns: a real edge, so an asynchronous reset acts before any clock
int edge_n = -1;
always @(posedge clk) edge_n = edge_n + 1;
logic set = 0, clr = 0;
logic irq;
irq_flag dut (.*);
// Advance to the next rising edge, wait for the design to settle, then print one line.
task automatic tick();
@(posedge clk); #1;
if (rst_n) $display("e%0d set=%b clr=%b irq=%b", edge_n, set, clr, irq);
endtask
initial begin
#1 rst_n = 0; tick(); tick(); rst_n = 1; // asserted at 1 ns, edges 0 and 1 in reset, released 1 ns after edge 1
set = 1; tick(); // the event
set = 0; tick();
clr = 1; tick(); // software clears it
clr = 0; tick();
set = 1; clr = 1; tick(); // both at one edge: set must win
set = 0; clr = 0; tick();
$finish(0); // quiet: no "$finish called" line
end
endmodule
Check Yourself
Common Mistakes
Wrong Declare every internal signal wire, so that the type never gets in the way.
A wire accepts a second driver and resolves it, so a pasted assign becomes an X at run time.
Fix Declare a signal with one driver as logic. Icarus 12 then refuses the second assign at compile time.
wire [3:0] sel;
assign sel = a;
assign sel = b; // compiles; x where a and b differlogic [3:0] sel;
assign sel = a;
assign sel = b; // error: cannot have multiple driversWrong Connect an instance by position because the port list is short.
Two ports of the same width can change places, and no tool sees anything wrong.
Fix Connect by name, or with .* when the names match. The order then does not matter.
Wrong Leave an input you do not need open, as in .cin().
An open input floats at z, and logic that reads it gives x or an unplanned value.
Fix Tie an unused input to the value the block needs, such as .cin(1'b0).
Remember
- A variable takes one driver. A net resolves several, and two that disagree on a
wiregive X. - Write each variable in one procedure. Two
always_ffblocks on one flag simulated here and built 0 cells. - Keep tri-states at the pads. Inside a block, a mux states the priority and the idle value.
- An input port is a net and an output
logicport is a variable. - Connect by name or with
.*. By position, two same-width ports can swap and no tool reports it.