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

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, ⌈logk⁡N⌉; 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, ⌈63/(k−1)⌉.

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?

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 ⌈log2⁡D⌉ address bits: $clog2(D).
  • A counter that must hold the value M itself needs ⌈log2⁡(M+1)⌉ bits.
  • A sum of N values of W bits needs W+⌈log2⁡N⌉ 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 ⌈(N−1)/(k−1)⌉ nodes in ⌈logk⁡N⌉ 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
  1. Address. ⌈log2⁡1000⌉=10bits
  2. Counter to 1000. It holds 1001 values, and 1000 < 1024: ⌈log2⁡1001⌉=10bits
  3. Counter to 1024. It holds 1025 values: ⌈log2⁡1025⌉=11bits
  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:
$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
  1. Count the adders. Four levels of 8, 4, 2 and 1 adders: 8+4+2+1=15adders
  2. Count the flip-flops. Each level's results grow a bit, 17 to 20 bits wide: 8×17+4×18+2×19+1×20=266
  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.

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
  1. As flip-flops. 32×12=384flip-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

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 2L 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°.

Check

Write clog2(n): the smallest b with 2b≥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 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 W+⌈log2⁡N⌉ bits?

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.