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 wand and a wor with 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.

One Flag, Two ProceduresThe lab bench, 1 ns after edges 2 to 7. irq (netlist) is what Synthesize built from the shipped design: 0 cells, with irq tied to constant 0.
012345clksetclrirq (simulation)irq (netlist)irq (one procedure)no flopset and clr
Scroll horizontally ↔
  1. Cycle 0

    Edge 2: set was 1. Simulation raises the flag. The netlist has no flop to raise.

  2. Cycle 2

    Edge 4: clr lowers the flag in simulation. The second procedure did that.

  3. Cycle 4

    Edge 6: set and clr together. The brief says set wins. Simulation shows 0, because the second procedure wrote last.

  4. 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.

Two Drivers on One WireArrows show values moving from each driver to the wire, and from the wire to the logic that reads it.
  • Driver
  • Interconnect
  • Datapath
  • Design
  • Data
  • Structure
Two drivers on one wireArrows show values moving from each driver to the wire, and from the wire to the logic that reads it. driver assign w = a drives 0 driver assign w = b drives 1 interconnect wire w resolves its drivers datapath Logic that readsw sees x design The chip two gate outputsshorted 01xa fight
  1. driver assign w = adrives 0
    • 0 → wire w
  2. driver assign w = bdrives 1
    • 1 → wire w
  3. interconnect wire wresolves its drivers
    • x → Logic that reads w
    • a fight → The chip
  4. datapath Logic that reads wsees x
  5. design The chiptwo gate outputs shorted
  1. Two drivers

    Each assign is a driver. Both are always on, and they disagree.

  2. Simulation

    The wire resolves 0 against 1 to x. Everything that reads w now sees an unknown.

  3. 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 1Driver 2wirewandwor
00000
01x01
11111
0z000
1z111
0xx0x
1xxx1
zzzzz
zxxxx

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.

Three Tools Read the Two BlocksArrows show what each tool does with a flag set in one always_ff block and cleared in another.
  • Neutral
  • Tool
  • Result
  • Report
  • Structure
Three tools read the two blocksArrows show what each tool does with a flag set in one `always_ff` block and cleared in another. neutral Two always_ffblocks both write irq tool Simulators Icarus 12, Verilator5.052 tool Lint Verilator -Wall tool Synthesizer Yosys 0.70, slang result A working flag the last write wins report MULTIDRIVEN two warnings report 0 cells irq tied to 0 run bothreadsreadsrunwarnsnetlist
  1. neutral Two always_ff blocksboth write irq
    • run both → Simulators
    • reads → Lint
    • reads → Synthesizer
  2. tool SimulatorsIcarus 12, Verilator 5.052
    • run → A working flag
  3. result A working flagthe last write wins
  4. tool LintVerilator -Wall
    • warns → MULTIDRIVEN
  5. report MULTIDRIVENtwo warnings
  6. tool SynthesizerYosys 0.70, slang
    • netlist → 0 cells
  7. report 0 cellsirq tied to 0
  1. Simulation

    Both simulators run both blocks at each edge. The flag rises on set and falls on clr.

  2. Lint

    Lint warns MULTIDRIVEN: the variable is written in always_ff and also by another process.

  3. Synthesis

    The synthesizer warns multiple conflicting drivers and Resolved 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.

DeclarationKindWhy it matters
input logic anet (wire logic)the instance above drives it; a second driver inside resolves against it
output logic yvariableone driver inside: an assign or one procedure
output wire ynetseveral assigns may drive it, as for a tri-state
inout wire padnetboth sides drive it; it must be a net
input a (no type)net, 1 bit, logicthe 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.

StyleExampleTwo same-width ports swappedA name that does not exist
By positionsub8 u (a, b, cin, d, bout);silent on all three toolsdoes not apply: no names are used
By namesub8 u (.a(a), .b(b), ...);cannot happen by order; a wrong pair is visible in the textan error
.namesub8 u (.a, .b, .cin, .d, .bout);cannot happenan error on all three tools
.*sub8 u (.*);cannot happenan 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

Design brief
PortDirectionWidthMeaning
clkinput1clock
rst_ninput1reset, asynchronous, active-low
setinput1the event: raises the flag
clrinput1software lowers the flag
irqoutput1the flag; stays until cleared
Clock
rising edge
Reset
rst_n asynchronous, active-low. After reset irq is 0
Corner cases
set and clr at the same edge: set wins. Done when: Run shows irq=1 at edges 6 and 7, Lint is silent, and Synthesize reports 2 cells
  1. 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.

  2. Lint

    Two MULTIDRIVEN warnings name irq: written in always_ff and by another process, with different clocking.

  3. Synthesize

    The run succeeds with 0 cells. The warnings say multiple conflicting drivers and Resolved using constant. The Gates tab is empty.

  4. Give it one owner

    Replace the two blocks with the one below. Run shows irq=1 at edges 6 and 7. Synthesize reports 2 cells: 1 flop and 1 $_OR_.

The fix: one procedure, one priority
Lint is silent. Synthesize reports 2 cells, 1 flop, 1 level of logic.
  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
  1. 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 wire give X.

  2. 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.

  3. Why connect ports by name?

    By position, two ports of the same width can change places and no tool reports it. Named, .name and .* 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.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: 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=0

Original 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

Predict the outputA pad cell drives 1 onto its pin. The device on the other side of the pin drives 0. Then the pad cell turns its driver off. What does the bench print?
module pad_cell (input logic oe, input logic out, output logic in, inout wire pad);
  assign pad = oe ? out : 1'bz;
  assign in  = pad;
endmodule
module tb;
  logic oe = 1, out = 1, ext_oe = 1, ext = 0;
  wire  pad, in;
  pad_cell u (.*);
  assign pad = ext_oe ? ext : 1'bz;   // the device on the other side of the pin
  initial begin
    #1 $write("in=%b ", in);
    oe = 0;
    #1 $display("in=%b", in);
    $finish(0);
  end
endmodule
Spot the bugEach option shares y between two sources. a must win when both enables are 1, and y must be 0 when neither is. Which option fails the bench?
ConceptOn this flow, what does Synthesize report for the lab as shipped, with irq written in two always_ff blocks?
ConceptA module has the port input logic a. Which statement about a is true?
ConceptAn instance uses .*. The module has an input cin, and the parent has no signal with that name. What happens?

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.

Before
wire [3:0] sel;
assign sel = a;
assign sel = b;   // compiles; x where a and b differ
After
logic [3:0] sel;
assign sel = a;
assign sel = b;   // error: cannot have multiple drivers

Wrong 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 wire give X.
  • Write each variable in one procedure. Two always_ff blocks 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 logic port is a variable.
  • Connect by name or with .*. By position, two same-width ports can swap and no tool reports it.

Practice

Report a problem with this page