S3 · Lesson 2 · Combinational design in Verilog
always @*, case statements, priority, and the latches nobody asked for
Most of a digital design's area and delay is combinational logic: multiplexers choosing between results, decoders driving enables, comparators, arithmetic. Verilog gives several ways to write the same function, and they don't all synthesize to the same hardware. An if-else chain makes a priority structure; a case statement can make a balanced multiplexer tree; a missing else makes a latch. This lesson shows how each construct maps to gates, so you can write the structure you mean and estimate its size and depth before the synthesizer does.
- A 4-to-1 multiplexer written as nested conditionals:
assign Y = S1 ? (S0 ? I3 : I2) : (S0 ? I1 : I0);. Flip the select switches S1 and S0 and the data switches I0 to I3. - The synthesizer builds this as three 2-to-1 multiplexers in two levels: S0 picks within each pair, S1 picks between the pairs.
- An if-else chain with the same function would make a priority chain three levels deep; for four inputs the difference is small, for 32 it isn't.
Predict
A combinational always @* block assigns y = a; inside if (en) but has no else. What does the synthesizer build?
A latch. When en is off the block doesn't assign y, so Verilog's semantics say y keeps its previous value, and the only hardware that remembers without a clock is a level-sensitive latch. Synthesizers warn about it ("inferred latch"), and it's almost always a bug: latches complicate timing analysis and can glitch. The fix is a default assignment at the top of the block (y = 0;) or an else on every if.
Name it
Constructs and the hardware they make
| Verilog | Hardware |
|---|---|
assign y = s ? a : b; | 2-to-1 multiplexer |
if ... else if ... else | priority chain: earlier conditions win, depth grows with the number of branches |
case (sel) with distinct constants | parallel multiplexer or decoder, depth about of the cases |
a + b, a * b, a < b | an adder, a multiplier, a comparator (an adder in disguise) |
for loop (constant bounds) | the body replicated, in parallel |
Rules for combinational blocks
- Use
always @*so the sensitivity list includes every input automatically. - Use blocking
=. - Assign every output on every path: defaults at the top are the easy way.
- Give every case a
default.
Size and depth
- An N-to-1 multiplexer takes N − 1 two-input multiplexers in levels as a balanced tree; as a priority chain it takes N − 1 levels.
- An n-to- decoder has outputs, each an n-input AND.
- An N-bit equality comparator is N XNOR gates and an AND tree of N − 1 two-input gates, levels deep.
- A multiplexer is , the basic building block of FPGA routing and of every datapath.
Resource sharing
Synthesis tools can share one adder between two additions that never happen in the same cycle (selected by a multiplexer), trading a multiplexer's area and delay for an adder's area. Writing the sharing explicitly in RTL makes the intent clear.
Worked example
How do a 16-to-1 multiplexer's tree and priority-chain versions compare?
Show the solution
- Count the multiplexers. Both versions use 15 two-input multiplexers.
- Depth of each. The balanced tree, from a case statement on a 4-bit select, is levels deep; the if-else chain is 15 levels deep.
- Delay at 30 ps per level. The chain's extra delay is a third of a 1 GHz clock period.
- When to use a chain. Priority chains are right when the priority is real (an interrupt controller), and wrong when the cases are mutually exclusive.
Answer both take 15 two-input multiplexers, but the tree is 4 levels (120 ps) deep and the chain 15 levels (450 ps).
Worked example
How many 2-input gates does a 32-bit equality comparator take, and how deep is it?
Show the solution
- Compare each bit pair. One XNOR gate per bit pair: 32 gates.
- Combine the 32 outputs. An AND tree takes 31 two-input ANDs in levels.
- Total. 6 levels deep, counting the XNOR.
- A magnitude comparator. is a 32-bit subtraction's carry-out, deeper unless built with a fast adder (lesson 8).
Answer 63 two-input gates, 6 levels deep.
Worked example
An FPGA's 6-input LUT can hold a 4-to-1 multiplexer. How many LUTs and levels does a 64-to-1 multiplexer take, against a tree of 2-to-1 multiplexers?
Show the solution
- From 4-to-1 multiplexers. Each level divides the inputs by four: four-input multiplexers in 3 levels.
- From 2-to-1 multiplexers. It takes 63 of them in 6 levels.
- Why LUTs grow. Wider primitives cut both the count and the depth, which is why FPGA vendors keep making their LUTs wider.
Answer 21 LUTs in 3 levels, against 63 two-to-one multiplexers in 6 levels.
Check
Write a 2-to-1 multiplexer: Y = A when S = 0, B when S = 1, using AND, OR and NOT.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
How many 2-to-1 multiplexers does a 24-to-1 multiplexer need?
Check
How many levels deep is a balanced tree of 2-to-1 multiplexers for 24 inputs?
Check
How many outputs does a 5-to- decoder have?
Check
How many 2-input gates does a 40-bit equality comparator take (XNORs plus an AND tree)?
Check
A 16-way selection is written as an if-else chain instead of a balanced case tree. With 25 ps per multiplexer level, how much longer is the chain's worst-case delay?
Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.
Check
A design needs a 16-to-1 multiplexer and a 3-to- decoder.
(a)
How many 2-to-1 multiplexers does the multiplexer take as a tree?
(b)
How many levels deep is the tree?
(c)
How many 2-input AND gates does the decoder take if each output is built separately?
Check
An N-bit equality comparator is built from XNOR gates and a balanced tree of 2-input ANDs.
Step 1
How many XNOR gates does it need?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many gates in all?
Step 3
How many gate levels deep is it, treating as exact?
Explain why
Why does an if statement without an else in a combinational block create a latch?
Verilog's rule is that a variable keeps its value until something assigns it. In a combinational block that assigns the output only when a condition is true, the language therefore says that when the condition is false the output holds whatever it had before. Holding a value means memory, and since there's no clock involved, the synthesizer builds the only memory that matches: a level-sensitive latch enabled by the condition. The designer usually meant pure logic, with some definite value in the other case. Giving the output a default at the top of the block, or an else, removes the memory and the latch.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.