---
title: Parameterized and generated hardware
heading: Parameters, generate loops, widths that follow from them, and reduction trees
eyebrow: S3 · Lesson 6 · Parameterized and generated hardware
xp: 50
---

::: hook
A FIFO, an adder tree or a delay line is written once and used at a dozen sizes. Verilog's parameters and generate blocks make that possible: the module takes its width, depth or number of inputs as a parameter, and elaboration builds the hardware for that size before synthesis begins. The design work moves from drawing gates to writing the rules that relate sizes to each other: how many address bits a depth needs, how wide a sum of N numbers grows, how many levels a reduction tree takes.
:::

::: play
- The stems show how many levels a reduction tree takes to combine N inputs with k-input nodes, $\lceil\log_kN\rceil$; the dashed stems are the 2-input tree.
- Each level combines k results into one, so wider nodes make shallower trees: 6-input LUTs reach 64 inputs in 3 levels where 2-input gates need 6.
- The readout counts the nodes a 64-input tree needs, $\lceil 63/(k - 1)\rceil$.
:::

```sim plot
view: graph
x: N = 2..1024 log
y: L = 0..11
samples: 400
slider: k = 2..8 step 1 = 4
plot: ceil(log(N)/log(k) - 1e-9) | label: k-input tree levels
plot: ceil(log(N)/log(2) - 1e-9) | dashed | label: 2-input tree levels
readout: nodes for 64 inputs = ceil(63/(k - 1)) | digits: 3
```

::: predict
## A parameterized adder tree sums N = 100 inputs of 16 bits with 2-input adders. How many adders and levels does it build, and how wide must its output be to never overflow?

- [x] 99 adders in 7 levels, with a 23-bit output
- [ ] 50 adders in 7 levels, with a 17-bit output
- [ ] 99 adders in 100 levels, with a 116-bit output
- [ ] 100 adders in 6 levels, with a 22-bit output

::: reveal
Each 2-input adder turns two values into one, so reducing 100 to 1 takes 99 adders, arranged in $\lceil\log_2100\rceil = 7$ levels. Each level can carry one bit further, so the sum needs $16 + 7 = 23$ bits. A good parameterized module computes all three from N and W itself.
:::
:::

::: nameit
## Parameters

```verilog
module adder_tree #(parameter N = 8, parameter W = 16) (
  input  [N*W-1:0] in,
  output [W+$clog2(N)-1:0] sum);
```

Parameters are constants fixed at elaboration; each instance can override them. `localparam` defines derived constants that callers can't change.

## Widths that follow

- A memory of D words needs $\lceil\log_2D\rceil$ address bits: `$clog2(D)`.
- A counter that must hold the value M itself needs $\lceil\log_2(M + 1)\rceil$ bits.
- A sum of N values of W bits needs $W + \lceil\log_2N\rceil$ bits to never overflow; a product of W-bit and V-bit values needs W + V.

## Generate blocks

`generate for` replicates instances or logic N times, each copy with its own index; `generate if` chooses between structures from a parameter, for example a carry chain on an FPGA and a prefix adder in an ASIC. Like loops in an `always @*` block, they unroll at elaboration into parallel hardware: nothing iterates at run time.

## Reduction trees

Combining N inputs with k-input nodes (adders, comparators, OR gates) takes $\lceil(N - 1)/(k - 1)\rceil$ nodes in $\lceil\log_kN\rceil$ levels. Registering after each level makes a pipelined tree: one result per clock, with a latency of one cycle per level.
:::

::: example
## A parameterized memory has D = 1000 words, and a counter must count from 0 up to 1000 and to 1024. How many bits does each need?

::: solution
1. **Address.**
   $$\lceil\log_2 1000\rceil = 10\ \text{bits}$$
2. **Counter to 1000.** It holds 1001 values, and 1000 < 1024:
   $$\lceil\log_2 1001\rceil = 10\ \text{bits}$$
3. **Counter to 1024.** It holds 1025 values:
   $$\lceil\log_2 1025\rceil = 11\ \text{bits}$$
4. **The off-by-one.** That's one bit more than Verilog's clog2 gives for 1024, the classic off-by-one in parameterized counters:

```verilog
$clog2(1024)  // 10
```

**Answer:** 10 bits for the address, 10 for the counter to 1000 and 11 for the counter to 1024.
:::
:::

::: example
## A pipelined adder tree sums 16 inputs of 16 bits, with a register after every adder. How many adders and flip-flops does it take, and what is its latency?

::: solution
1. **Count the adders.** Four levels of 8, 4, 2 and 1 adders:
   $$8 + 4 + 2 + 1 = 15\ \text{adders}$$
2. **Count the flip-flops.** Each level's results grow a bit, 17 to 20 bits wide:
   $$\begin{aligned} &8 \times 17 + 4 \times 18 \\ &+ 2 \times 19 + 1 \times 20 \\ &= 266 \end{aligned}$$
3. **Latency.** One register per level makes it 4 cycles, and a new sum comes out every cycle.

**Answer:** 15 adders and 266 flip-flops, with a latency of 4 cycles.
:::
:::

::: example
## A generate loop builds a 32-tap delay line of 12-bit samples. How many flip-flops is that, and how many LUTs if the FPGA's LUTs can act as 32-deep shift registers?

::: solution
1. **As flip-flops.**
   $$32 \times 12 = 384\ \text{flip-flops}$$
2. **As shift-register LUTs.** One 32-deep shift-register LUT per bit: 12 LUTs.
3. **When synthesis uses them.** It infers the LUT version only if the delay line has no reset and no taps read from the middle, a constraint worth knowing when writing the generate loop.

**Answer:** 384 flip-flops, or 12 LUTs used as shift registers.
:::
:::

::: check
kind: parts
given: N = 12 | 50 | 100 | 300; W = 8 | 12 | 16

## A parameterized adder tree sums N = {N} inputs of W = {W} bits with 2-input adders.

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

### How many adders does it build?

::: misses
- [N/2] That's only the first level.
- [any] Each adder removes one value.
:::
:::

::: part
answer: ceil(log(N)/log(2) - 1e-9)
tolerance: 0

### How many levels deep is it?

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

::: part
answer: W + ceil(log(N)/log(2) - 1e-9)
unit: bits
tolerance: 0

### How wide must the sum be?

::: misses
- [W + 1] One bit per level, not one in all.
- [any] $W + \lceil\log_2N\rceil$.
:::
:::
:::

::: check
kind: parts
given: D = 100 | 1000 | 4096; M = 64 | 256 | 1024 | 4096

## A module is parameterized by a memory depth D = {D} and a counter limit M = {M}.

::: part
answer: ceil(log(D)/log(2) - 1e-9)
unit: bits
tolerance: 0

### How many address bits does the memory need?

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

::: part
answer: ceil(log(M + 1)/log(2) - 1e-9)
unit: bits
tolerance: 0

### How many bits must the counter have to hold the value M?

::: misses
- [ceil(log(M)/log(2) - 1e-9)] $\lceil\log_2M\rceil$ is one bit short when M is a power of two.
- [any] $\lceil\log_2(M + 1)\rceil$.
:::
:::
:::

::: check
kind: parts
given: L = 2 | 3 | 4 | 5; W = 8 | 12 | 16

## A pipelined adder tree has $2^L$ inputs of {W} bits (L = {L}), with a register after every adder.

::: part
answer: 2^L - 1
tolerance: 0

### How many adders does it have?

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

::: part
answer: W*(2^L - 1) + 2^(L + 1) - L - 2
tolerance: 0

### How many flip-flops, if each level's results are one bit wider than the level before?

::: misses
- [W*(2^L - 1)] The widths grow level by level.
- [any] $\sum_l(N/2^l)(W + l) = W(N - 1) + 2N - L - 2$.
:::
:::

::: part
answer: L
unit: cycles
tolerance: 0

### What is its latency?

::: misses
- [any] One cycle per level.
:::
:::
:::

::: check
kind: choice

## One module sums an array with a `for` loop inside `always @*`; another instantiates N adder modules in a `generate for`. How do they differ in hardware?

- [ ] The `always` loop runs once per clock cycle, reusing one adder
- [x] Neither iterates at run time: both unroll at elaboration into parallel hardware; the generate loop just builds instances and can register between levels
- [ ] The generate loop runs in simulation only
- [ ] The `always` loop needs a constant clock count to finish

::: misses
- [1] Synthesis unrolls loops; reusing one adder over cycles must be written as a state machine.
- [3] Generate blocks are synthesizable: they're how parameterized structures are built.
- [4] Loops in combinational blocks don't take clock cycles at all.
:::
:::

::: check
kind: derivation
vars: N = 2..4096, k = 2..8

## A reduction combines N inputs with k-input nodes.

::: step
answer: (N - 1)/(k - 1)

### How many nodes does it take, ignoring rounding?
:::

::: step
answer: log(N)/log(k)

### How many levels, ignoring rounding?
:::

::: step
answer: (log(N)/log(2))/(log(N)/log(k))

### By what factor are its levels fewer than a 2-input tree's?
:::

::: right
Right. The depth shrinks by $\log_2k$; the node count by $k - 1$.
:::
:::

::: check
kind: python

## Write `clog2(n)`: the smallest b with $2^b \ge n$ (with clog2(1) = 0), and `tree(n, w)`: return (adders, levels, sum width) for a 2-input adder tree over n inputs of w bits.

```python starter
def clog2(n):
    return 0

def tree(n, w):
    return (0, 0, w)
```

```python solution
def clog2(n):
    b = 0
    while (1 << b) < n:
        b += 1
    return b

def tree(n, w):
    return (n - 1, clog2(n), w + clog2(n))
```

```python tests
assert [clog2(n) for n in (1, 2, 3, 4, 5, 1000, 1024, 1025)] == [0, 1, 2, 2, 3, 10, 10, 11], "Powers of two are exact; one more needs another bit."
assert tree(100, 16) == (99, 7, 23), "The predict: 99 adders, 7 levels, 23 bits."
assert tree(1, 8) == (0, 0, 8), "A single input needs no adders."
```

::: right
Right. Integer arithmetic avoids the floating-point traps of computing $\log_2$ for exact powers of two.
:::
:::

::: explain
## Why does a sum of N values of W bits need $W + \lceil\log_2N\rceil$ bits?

::: model
The largest W-bit unsigned value is just under $2^W$, so a sum of N of them is just under $N\cdot2^W$. Holding numbers up to $N\cdot2^W$ takes $\log_2N + W$ bits, rounded up to a whole number, so $\lceil\log_2N\rceil$ extra bits beyond W. In a binary tree that's one extra bit for each level, since each adder at most doubles the range of its inputs. Signed values follow the same rule because their magnitude range doubles in the same way.
:::
:::

::: practice
::: check
given: N = 20 | 200; W = 10 | 14
answer: W + ceil(log(N)/log(2) - 1e-9)
unit: bits
tolerance: 0

## Sum width for {N} inputs of {W} bits?

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

::: check
given: M = 63 | 64 | 500
answer: ceil(log(M + 1)/log(2) - 1e-9)
unit: bits
tolerance: 0

## Counter bits to hold the value {M}?

::: misses
- [any] $\lceil\log_2(M + 1)\rceil$.
:::
:::

::: check
given: N = 30 | 100; k = 4 | 6
answer: ceil((N - 1)/(k - 1))
tolerance: 0

## {k}-input nodes to reduce {N} inputs?

::: misses
- [any] $\lceil(N - 1)/(k - 1)\rceil$.
:::
:::
:::
