The Conditional Operator
In digital hardware design, the ternary conditional operator (?:) infers a multiplexer directly. The continuous assignment assign out = sel ? a : b; describes the 2-to-1 multiplexer introduced in Section 1.5. This operator provides a concise syntax for data routing in combinational RTL.
The 2-to-1 Multiplexer
The conditional operator follows this syntax:
condition ? value_if_true : value_if_false
wire [7:0] mux_out = sel ? input_1 : input_0;
When sel equals 1, the output routes input_1. When sel equals 0, the output routes input_0. For an 8-bit bus, the synthesis tool creates eight parallel 2-to-1 multiplexers or eight FPGA lookup table (LUT) primitives.
Arbitrary Conditional Expressions
The condition expression can include any relational, reduction, or logical statement. Any non-zero result evaluates to true:
wire [7:0] larger = (a > b) ? a : b; // Comparator driving multiplexer
wire [7:0] bypass = (a == 8'h00) ? b : a;
wire [7:0] safe_data = (|a) ? a : 8'hFF; // Unary reduction condition
wire [7:0] clamped = (a > 8'd200 && a < 8'd255) ? 8'd200 : a;
The synthesis tool implements the comparison circuit, connects the 1-bit comparator output to the multiplexer select port, and routes data buses to input channels.
Common RTL Applications
Digital designs frequently use the conditional operator for data steering and signal bounding:
- Tri-State I/O Driver (physical chip boundary):
assign bus = oe ? data : 8'bz;
- Minimum and Maximum Selectors:
assign min_val = (a < b) ? a : b;
assign max_val = (a > b) ? a : b;
- Arithmetic Saturation Logic:
assign sum_wide = {1'b0, operand_a} + {1'b0, operand_b};
assign result = sum_wide[8] ? 8'hFF : sum_wide[7:0];
- Two’s-Complement Absolute Value:
assign abs_a = a[7] ? (~a + 1'b1) : a; // Negates negative values
Cascaded Priority Multiplexer Chains
Chaining multiple conditional operators creates an N-way selection tree:
wire [7:0] out = (sel == 2'b00) ? a :
(sel == 2'b01) ? b :
(sel == 2'b10) ? c :
d;
Chained conditional operators evaluate sequentially from top to bottom. The first condition that evaluates to true routes its input:
wire [7:0] priority_out =
err_critical ? 8'hFF : // Highest priority
err_warning ? 8'h80 : // Active only when err_critical is low
data_valid ? data : // Active when neither error condition is asserted
8'h00; // Default inactive state
This cascaded structure implements hardware priority encoders and bus arbiters. Place the highest-priority condition first in the expression chain.
Conditional Expressions Versus Procedural Blocks
Continuous conditional assignments and procedural statements synthesize into identical hardware:
// Continuous assignment style
assign out_cond = (sel == 2'b00) ? a :
(sel == 2'b01) ? b :
(sel == 2'b10) ? c : d;
// Procedural block style
reg [7:0] out_always;
always @(*) begin
if (sel == 2'b00) out_always = a;
else if (sel == 2'b01) out_always = b;
else if (sel == 2'b10) out_always = c;
else out_always = d;
end
For one or two conditions, the continuous conditional assignment is concise. For multiple outputs or more than four selector states, use a procedural case statement:
always @(*) begin
case (s)
3'b000: out = a;
3'b001: out = b;
3'b010: out = c;
3'b011: out = d;
3'b100: out = e;
3'b101: out = f;
3'b110: out = g;
default: out = h;
endcase
end
The conditional operator ?: is the only conditional branching construct supported inside continuous assign statements. Procedural if-else statements require an always block.
Hardware Synthesis Characteristics
| Verilog Expression | Synthesized Hardware Primitive | Propagation Delay |
|---|---|---|
sel ? a : b (1-bit) |
Single 2-to-1 multiplexer | 1 multiplexer delay |
sel ? a : b ($N$-bit) |
Parallel array of $N$ 2-to-1 multiplexers | 1 multiplexer delay |
(a > b) ? a : b |
Comparator and 2-to-1 multiplexer | Comparator delay + 1 multiplexer delay |
| $K$-stage cascaded chain | Priority multiplexer tree | Up to $K$ multiplexer delays |
Long priority chains accumulate propagation delay across cascaded multiplexer stages. Inspect static timing analysis reports when using deep priority chains in high-frequency clock domains.
Practice Exercise
Design a 4-to-1 multiplexer for 8-bit buses using only continuous assignments. The module accepts a 2-bit select signal sel and four data inputs (d0 through d3). The module must also generate a 1-bit valid output that asserts high when sel routes d0 or d1.
Solution
`timescale 1ns / 1ps
`default_nettype none
module mux4_with_valid (
input wire [1:0] sel,
input wire [7:0] d0, d1, d2, d3,
output wire [7:0] out,
output wire valid
);
assign out = (sel == 2'b00) ? d0 :
(sel == 2'b01) ? d1 :
(sel == 2'b10) ? d2 :
d3;
assign valid = ~sel[1]; // Select values 2'b00 and 2'b01 have bit 1 cleared
endmodule
The select signal values 2'b00 and 2'b01 both contain a 0 at index 1. Inspecting the most significant bit reduces the valid signal logic to a single inverter.
The final article in this section examines operator precedence rules and common synthesis pitfalls in multi-operator expressions.