S32 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

Constructs and the hardware they make

VerilogHardware
assign y = s ? a : b;2-to-1 multiplexer
if ... else if ... elsepriority chain: earlier conditions win, depth grows with the number of branches
case (sel) with distinct constantsparallel multiplexer or decoder, depth about log2⁡ of the cases
a + b, a * b, a < ban 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 ⌈log2⁡N⌉ levels as a balanced tree; as a priority chain it takes N − 1 levels.
  • An n-to-2n decoder has 2n 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, ⌈log2⁡N⌉ levels deep.
  • A multiplexer is Y=S¯A+SB, 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
  1. Count the multiplexers. Both versions use 15 two-input multiplexers.
  2. Depth of each. The balanced tree, from a case statement on a 4-bit select, is ⌈log2⁡16⌉=4 levels deep; the if-else chain is 15 levels deep.
  3. Delay at 30 ps per level. ttree=4×30=120pstchain=15×30=450ps The chain's extra delay is a third of a 1 GHz clock period.
  4. 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
  1. Compare each bit pair. One XNOR gate per bit pair: 32 gates.
  2. Combine the 32 outputs. An AND tree takes 31 two-input ANDs in ⌈log2⁡32⌉=5 levels.
  3. Total. 32+31=63gates 6 levels deep, counting the XNOR.
  4. A magnitude comparator. a<b 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
  1. From 4-to-1 multiplexers. Each level divides the inputs by four: 16+4+1=21 four-input multiplexers in 3 levels.
  2. From 2-to-1 multiplexers. It takes 63 of them in 6 levels.
  3. 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-2n 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-2n 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°.

Explain why

Why does an if statement without an else in a combinational block create a 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.