Tri-State Buffers, Buses and Four-Valued Logic
Share one bus between several drivers with tri-state buffers, and read the x and z that appear when sharing fails.
You Will Be Able To
- Describe a tri-state buffer and the value z.
- Explain why exactly one driver must be on, and what x and z on a bus mean.
- Predict how wire, wand, wor and tri1 nets resolve several drivers.
- Explain keepers, pull-ups and open-drain lines, and why on-chip buses use muxes.
- Lesson
- G4.10
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
A CPU and a DMA engine share one 8-bit bus. When the DMA engine takes over, its enable rises one step before the CPU's falls. When it finishes, both enables are low. A bus monitor flags x and then z.
- Cycle 1
Both drive. Where 3c and a5 agree the bit is fine; where they differ it is x.
- Cycle 3
Neither drives. Every bit floats: z.
Measured on the simulator. x and z are two of the four values of a SystemVerilog net.
The Tri-State Buffer
A tri-state buffer has a data input, an enable and an output. When enabled, the output follows the data. When disabled, the output is cut off from both supplies: it is high impedance, written z.
| en | d | y |
|---|---|---|
| 1 | 0 | 0 |
| 1 | 1 | 1 |
| 0 | 0 | z |
| 0 | 1 | z |
In SystemVerilog: assign y = en ? d : 1'bz; or the primitive bufif1 (y, d, en). Lesson G9.3 builds one from transistors.
One Driver at a Time
A bus is one set of wires that several devices can drive. Each device drives it through tri-state buffers, and the enables decide whose turn it is.
Exactly one enable may be on. With two, the bits where the drivers disagree become x, an unknown value. With none, every bit is z. Four-valued logic, 0, 1, x and z, lets the simulator show both faults.
- I/O
- Control / FSM
- Bus
- Data
- Control
- Decode
sel = 2 turns on en2 only.
Follow the calls, values and waits
- data sel = 2: sel → Decoder
- One driver
Device 0 drives z; device 2 drives its data, 44. A z loses to a driven value.
Follow the calls, values and waits
- data z: Device 0 → bus
- data 44: Device 2 → bus
- Read
The bus carries 44 to the reader.
Follow the calls, values and waits
- data 44: bus → Reader
Read the diagram as text
- sel: device number
- Decoder: one enable high
- Device 0: tri-state out
- Device 2: tri-state out
- bus: 8 shared wires
- Reader: sees one value
- sel → Decoder: 2 bits
- Decoder → Device 0: en0
- Decoder → Device 2: en2
- Device 0 → bus: z or data
- Device 2 → bus: z or data
- bus → Reader: value
Decoding all enables from one select makes a second enable impossible.
The lab below decodes the enables from a single device number. A decoder gives exactly one enable for every value, so the bus can never see two drivers or none.
How a Net Resolves Several Drivers
When several continuous assignments drive one net, the net type decides the result. A plain wire (or tri) gives x on a conflict. wand gives the AND of the drivers and wor the OR. tri1 floats to 1 instead of z.
| Enables of drivers 5 and A | wire | wand | wor | tri1 |
|---|---|---|---|---|
| 10 | 0101 | 0101 | 0101 | 0101 |
| 11 | xxxx | 0000 | 1111 | xxxx |
| 00 | zzzz | zzzz | zzzz | 1111 |
| 01 | 1010 | 1010 | 1010 | 1010 |
Measured on the simulator: two 4-bit drivers, 0101 and 1010, on nets of four kinds.
Gates treat z as unknown too. 0 AND x is 0, because 0 decides an AND. But 1 AND x is x, and 0 XOR z is x.
Keepers, Pull-Ups and Open Drain
Two circuits keep a bus from floating. A bus keeper weakly drives the last value back onto the bus, so a released bus holds its value. A pull-up resistor weakly pulls the line to 1. Any real driver overrides either one.
An open-drain output can only pull its line low, or let go. With a pull-up, the line is 1 unless some device pulls it low: a wired-AND. I2C and many interrupt lines work this way, and two devices may pull at once without a fight.
| Step | Bus with keeper | Open-drain line |
|---|---|---|
| device 0 drives 0101; nobody pulls | 0101 | 1 |
| nobody drives; device a pulls | 0101 (kept) | 0 |
| device 1 drives 1010; device b pulls | 1010 | 0 |
| nobody drives; both pull | 1010 (kept) | 0 |
| both drive; nobody pulls | xxxx | 1 |
Measured on the simulator. The keeper is a weak driver of the last fully driven value; the line has a pullup.
A keeper cannot fix a fight: with two strong drivers the bus is still x.
The keeper above is a behavioural model: a weak driver of the last fully driven value. SystemVerilog also has trireg nets, which hold their charge, but Icarus 12 refuses them ("trireg nets not supported").
Why On-Chip Buses Use Muxes
Inside a chip, the enables of a shared bus are hard to switch at exactly the same moment. Skew between them can turn two drivers on at once. Where they drive different values, current flows from the supply to ground and the level is wrong.
A bus that nobody drives floats to a level in between, and the receivers waste power. For both reasons, chip designers now prefer multiplexers to on-chip tri-state buses.
This site's synthesis flow refuses two drivers on one net with "multiple conflicting drivers". The same 8-bit selection written as a mux synthesized to 8 mux cells.
So tri-state drivers stay at the pins of a chip, for shared off-chip buses and bidirectional data lines. Inside, a mux or an AND-OR tree picks the source.
Asked in interviews
What is the difference between x and z?
z means no driver: the net floats. x means unknown: two drivers disagree, or an input is unknown. A gate that reads z treats it as x.
What happens when two tri-state drivers are on at once?
In simulation the bits where they differ become x. In silicon, where they drive different values, current flows from the supply through both drivers to ground. Power is wasted and the level is wrong.
What is an open-drain output for?
It only pulls low, so with a pull-up several devices can share a line safely. The line is a wired-AND: low when any device pulls.
Try It
For every device number exactly one enable is high. The bus carries that device's data, and the bench finds no x or z.
Add the line if (sel == 2'd1) en[0] = 1'b1; after the decoder line and run. Read 1 now shows XX: devices 0 and 1 drive at once.
Delete en[sel] = 1'b1; and run. Every read is zz: nobody drives.
Press Synthesize. The decoder itself is plain logic, 4 cells. The tri-state drivers live in the bench.
Synthesize the design to measure its cells and inspect the mapped gates.
sel=0 en=0001 bus=11 sel=1 en=0010 bus=22 sel=2 en=0100 bus=44 sel=3 en=1000 bus=88 0 of 4 reads wrongOriginal design · 4 cells · 0 flip-flops · 0 latches · 1 logic levels. Synthesis uses Yosys; counts depend on the toolchain.
Read included file: tb.sv
// tb.sv: four devices share one 8-bit bus through tri-state drivers (read-only).
// The bench reads the bus for every device number and fails on any x or z.
module tb;
logic [1:0] sel;
logic [3:0] en;
wire [7:0] bus;
logic [7:0] data [4];
int bad = 0;
en_dec dut (.sel(sel), .en(en));
for (genvar k = 0; k < 4; k++) begin : dev
assign bus = en[k] ? data[k] : 'z; // device k's tri-state driver
end
initial begin
data[0] = 8'h11; data[1] = 8'h22; data[2] = 8'h44; data[3] = 8'h88;
for (int k = 0; k < 4; k++) begin
sel = k[1:0];
#1;
if (bus !== data[k]) bad++;
$display("sel=%0d en=%b bus=%h%s", sel, en, bus, bus === data[k] ? "" : " wrong");
end
$display("%0d of 4 reads wrong", bad);
end
endmodule
Check Yourself
Common Mistakes
Wrong Let two drivers overlap during a hand-over.
For that moment two outputs fight. In simulation every bit where they disagree turns to x; in silicon a large current flows.
Fix Decode the enables from one select, so that only one can be active. Or turn the old driver off before the new one turns on.
Wrong Leave the bus with no driver.
An undriven net is z. Logic that reads it sees an unknown value, and a real CMOS input left floating can draw current.
Fix Park the bus on a default driver, or add a keeper or a pull-up.
Wrong Test for x with ==.
bus == 8'hxx gives x, not 1, so the test is never true. The equality operators treat x and z as unknown.
Fix Use the case equality ===, which compares x and z as values.
if (bus == 8'hzz) $display("floating");if (bus === 8'hzz) $display("floating");Wrong Build an on-chip bus from tri-state buffers.
This site's synthesis flow refuses it ("multiple conflicting drivers"), and most ASIC flows avoid internal tri-states.
Fix Inside a chip, select the source with a mux. Keep tri-state drivers for pins and off-chip buses.
Remember
- A tri-state buffer drives its data when enabled and z when not.
- On a shared bus exactly one driver must be on: two give x where they differ, none give z.
- wire gives x on a conflict; wand gives the AND, wor the OR, tri1 floats to 1.
- A keeper holds the last value, a pull-up gives 1, and open-drain plus a pull-up is a wired-AND.
- Inside a chip, use a mux; keep tri-state drivers for pins.