---
title: Combinational design in Verilog
heading: always @*, case statements, priority, and the latches nobody asked for
eyebrow: S3 · Lesson 2 · Combinational design in Verilog
xp: 50
---

::: hook
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.
:::

::: play
- 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.
:::
:::

```sim logic
module mux4(input I0, input I1, input I2, input I3, input S1, input S0, output Y);
assign Y = S1 ? (S0 ? I3 : I2) : (S0 ? I1 : I0);
endmodule
set I1 = 1
set I2 = 1
```

::: predict
## A combinational `always @*` block assigns `y = a;` inside `if (en)` but has no else. What does the synthesizer build?

- [ ] An AND gate: y is 0 when en is off
- [x] A latch that holds y's old value when en is off
- [ ] A flip-flop clocked by en
- [ ] Nothing: it's an error

::: reveal
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.
:::
:::

::: nameit
## 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 $\log_2$ 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 $\lceil\log_2N\rceil$ levels as a balanced tree; as a priority chain it takes N − 1 levels.
- An n-to-$2^n$ decoder has $2^n$ 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, $\lceil\log_2N\rceil$ levels deep.
- A multiplexer is $Y = \bar SA + 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.
:::

::: example
## How do a 16-to-1 multiplexer's tree and priority-chain versions compare?

::: 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 $\lceil\log_2 16\rceil = 4$ levels deep; the if-else chain is 15 levels deep.
3. **Delay at 30 ps per level.**
   $$\begin{aligned} t_\text{tree} &= 4 \times 30 \\ &= 120\ \text{ps} \\ t_\text{chain} &= 15 \times 30 \\ &= 450\ \text{ps} \end{aligned}$$
   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).
:::
:::

::: example
## How many 2-input gates does a 32-bit equality comparator take, and how deep is it?

::: 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 $\lceil\log_2 32\rceil = 5$ levels.
3. **Total.**
   $$32 + 31 = 63\ \text{gates}$$
   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.
:::
:::

::: 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?

::: 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
kind: boolean
vars: S, A, B
answer: ~S & A | S & B
ops: and, or, not

## Write a 2-to-1 multiplexer: Y = A when S = 0, B when S = 1, using AND, OR and NOT.

::: right
Right. $\bar SA + SB$, or anything equivalent.
:::

::: misses
- [S & A | ~S & B] The select is inverted: S = 0 should pick A.
- [any] Not yet. One AND term for each data input, enabled by S or its complement.
:::
:::

::: check
given: N = 8 | 24 | 100
answer: N - 1
tolerance: 0

## How many 2-to-1 multiplexers does an {N}-to-1 multiplexer need?

::: right
Right. N − 1 = {answer}, whether as a tree or a chain.
:::

::: misses
- [N] Each 2-to-1 mux removes one candidate: N − 1 of them leave one.
- [any] Not yet. Count how many choices each mux eliminates.
:::
:::

::: check
given: N = 6 | 24 | 100
answer: ceil(log(N)/log(2))
tolerance: 0

## How many levels deep is a balanced tree of 2-to-1 multiplexers for {N} inputs?

::: right
Right. $\lceil\log_2N\rceil$ = {answer} levels.
:::

::: misses
- [N - 1] That's the priority chain's depth.
- [any] Not yet. Each level halves the candidates.
:::
:::

::: check
given: n = 3 | 5 | 7
answer: 2^n
tolerance: 0

## How many outputs does an {n}-to-$2^n$ decoder have?

::: right
Right. $2^n$ = {answer}.
:::

::: misses
- [any] Not yet. One output per input combination.
:::
:::

::: check
given: N = 12 | 40 | 64
answer: 2*N - 1
tolerance: 0

## How many 2-input gates does an {N}-bit equality comparator take (XNORs plus an AND tree)?

::: right
Right. N XNORs and N − 1 ANDs: {answer}.
:::

::: misses
- [N] Combine the N bit comparisons with N − 1 more gates.
- [any] Not yet. Compare each bit, then AND the results.
:::
:::

::: check
given: N = 8 | 16 | 32; g = 25 | 40
answer: (N - 1 - ceil(log(N - 0.5)/log(2)))*g
unit: ps
tolerance: 1%

## An {N}-way selection is written as an if-else chain instead of a balanced case tree. With {g} ps per multiplexer level, how much longer is the chain's worst-case delay?

::: right
Right. $(N - 1) - \lceil\log_2N\rceil$ extra levels: {answer} ps.
:::

::: misses
- [(N - 1)*g] Subtract the tree's own depth.
- [any] Not yet. Compare the two depths.
:::
:::

::: check
kind: parts
given: N = 8 | 16 | 32 | 64; n = 3 | 4 | 5

## A design needs an {N}-to-1 multiplexer and an {n}-to-$2^n$ decoder.

::: part
answer: N - 1
tolerance: 0

### How many 2-to-1 multiplexers does the multiplexer take as a tree?

::: misses
- [any] Each 2-to-1 removes one input.
:::
:::

::: part
answer: round(log(N)/log(2))
tolerance: 0

### How many levels deep is the tree?

::: misses
- [N - 1] That's the priority chain's depth.
- [any] $\log_2 N$.
:::
:::

::: part
answer: 2^n*(n - 1)
tolerance: 0

### How many 2-input AND gates does the decoder take if each output is built separately?

::: misses
- [2^n] Each output is an n-input AND, which takes n − 1 two-input gates.
- [any] $2^n(n - 1)$.
:::
:::
:::

::: check
kind: derivation
vars: N = 2..1024

## An N-bit equality comparator is built from XNOR gates and a balanced tree of 2-input ANDs.

::: step
answer: N

### How many XNOR gates does it need?
:::

::: step
answer: 2*N - 1

### How many gates in all?
:::

::: step
answer: 1 + log(N)/log(2)

### How many gate levels deep is it, treating $\log_2N$ as exact?
:::

::: right
Right. Comparators, like multiplexers and decoders, grow linearly in area but only logarithmically in depth when built as trees.
:::
:::

::: explain
## Why does an if statement without an else in a combinational block create a latch?

::: model
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

::: check
given: N = 6 | 8 | 12 | 16 | 32 | 50 | 64
answer: N - 1
tolerance: 0

## 2-to-1 muxes for an {N}-to-1 mux?

::: misses
- [any] N − 1.
:::
:::

::: check
given: N = 5 | 10 | 17 | 24 | 40 | 100
answer: ceil(log(N)/log(2))
tolerance: 0

## Tree depth for {N} inputs?

::: misses
- [any] $\lceil\log_2N\rceil$.
:::
:::

::: check
given: N = 4 | 8 | 16 | 20 | 32 | 48 | 64
answer: 2*N - 1
tolerance: 0

## Gates in an {N}-bit equality comparator?

::: misses
- [any] 2N − 1.
:::
:::
:::
