S3 · Lesson 6 · Parameterized and generated hardware
Parameters, generate loops, widths that follow from them, and reduction trees
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.
- The stems show how many levels a reduction tree takes to combine N inputs with k-input nodes, ; 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, .
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?
Each 2-input adder turns two values into one, so reducing 100 to 1 takes 99 adders, arranged in levels. Each level can carry one bit further, so the sum needs bits. A good parameterized module computes all three from N and W itself.
Name it
Parameters
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 address bits:
$clog2(D). - A counter that must hold the value M itself needs bits.
- A sum of N values of W bits needs 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 nodes in levels. Registering after each level makes a pipelined tree: one result per clock, with a latency of one cycle per level.
Worked 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?
Show the solution
- Address.
- Counter to 1000. It holds 1001 values, and 1000 < 1024:
- Counter to 1024. It holds 1025 values:
- The off-by-one. That's one bit more than Verilog's clog2 gives for 1024, the classic off-by-one in parameterized counters:
$clog2(1024) // 10
Answer 10 bits for the address, 10 for the counter to 1000 and 11 for the counter to 1024.
Worked 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?
Show the solution
- Count the adders. Four levels of 8, 4, 2 and 1 adders:
- Count the flip-flops. Each level's results grow a bit, 17 to 20 bits wide:
- 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.
Worked 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?
Show the solution
- As flip-flops.
- As shift-register LUTs. One 32-deep shift-register LUT per bit: 12 LUTs.
- 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
A parameterized adder tree sums N = 50 inputs of W = 8 bits with 2-input adders.
(a)
How many adders does it build?
(b)
How many levels deep is it?
(c)
How wide must the sum be?
Check
A module is parameterized by a memory depth D = 1000 and a counter limit M = 64.
(a)
How many address bits does the memory need?
(b)
How many bits must the counter have to hold the value M?
Check
A pipelined adder tree has inputs of 8 bits (L = 3), with a register after every adder.
(a)
How many adders does it have?
(b)
How many flip-flops, if each level's results are one bit wider than the level before?
(c)
What is its latency?
Check
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?
Check
A reduction combines N inputs with k-input nodes.
Step 1
How many nodes does it take, ignoring rounding?
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 levels, ignoring rounding?
Step 3
By what factor are its levels fewer than a 2-input tree's?
Check
Write clog2(n): the smallest b with (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 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a sum of N values of W bits need bits?
The largest W-bit unsigned value is just under , so a sum of N of them is just under . Holding numbers up to takes bits, rounded up to a whole number, so 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
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.