---
title: "Inside an FPGA: LUTs, flip-flops and routing"
heading: How a field-programmable gate array becomes any circuit, the lookup table that implements any small function from a few memory bits, how many functions a LUT can hold, flip-flops beside every LUT, programmable routing and its delay, hard blocks for arithmetic and memory, and how FPGAs compare with custom chips
eyebrow: D10 · Lesson 2 · Inside an FPGA
xp: 40
---

::: hook
An FPGA is a chip that doesn't know what it is until it's configured. Inside are hundreds of thousands of tiny lookup tables, each a scrap of memory that can act as any logic function of its few inputs, every one paired with a flip-flop, all sitting in a sea of programmable switches that can wire them together in nearly any pattern. Loading a configuration file turns this blank fabric into a CPU, a video processor or a radio, and loading another turns it into something else. Verilog's job is to describe the circuit; the tools' job is to map it onto that fabric.
:::

::: play
- A 2-input lookup table: four configuration bits C0 to C3 hold the function's truth table, and the inputs A and B select one of them through a tree of multiplexers.
- Set C0 to C3 to 0, 0, 0, 1 and the LUT is an AND gate; 0, 1, 1, 1 makes OR; 0, 1, 1, 0 makes XOR. Any of the 16 functions of two inputs is just a different pattern.
- Real FPGAs use 6-input LUTs: 64 configuration bits each.
:::

```sim logic
input A, B, C0, C1, C2, C3
output F
assign F = B ? (A ? C3 : C2) : (A ? C1 : C0);
set C3 = 1
```

::: predict
## How many different logic functions can a single 4-input LUT implement?

- [ ] 16
- [ ] 256
- [x] 65,536
- [ ] 4

::: reveal
A 4-input LUT stores one output bit for each of the $2^4$ = 16 input combinations, so it holds 16 configuration bits, and every pattern of those bits is a different function: $2^{16}$ = 65,536 functions. A 6-input LUT holds 64 bits and so any of $2^{64}$, about 1.8 × 10¹⁹, functions of six inputs.
:::
:::

::: nameit
## Lookup tables

A k-input **LUT** is a $2^k$-bit memory addressed by its inputs. Any function of k inputs fits in one LUT, so the number of gates in the original logic doesn't matter, only the number of inputs. Functions of more inputs are split across several LUTs: a wide AND of n inputs takes about $\lceil(n - 1)/(k - 1)\rceil$ k-input LUTs in a tree.

## Logic blocks

Each LUT sits next to a flip-flop (or two), and groups of them form a **logic block** (a slice or CLB), with a dedicated fast **carry chain** for adders and counters running between neighbors.

## Routing

LUTs connect through a mesh of wires and programmable switches. Routing takes most of an FPGA's area and often most of a signal's delay, so a design's speed depends heavily on how the tools place its logic.

## Hard blocks

Common functions are built in as fixed silicon: **DSP blocks** (multiply-accumulate units), **block RAMs** (tens of kilobits each), clock managers (PLLs, D5), high-speed serial transceivers, and sometimes whole processor cores.

## FPGA or ASIC

The flexibility costs area, speed and power: a design typically takes 10 to 40 times the area of a custom chip (D7), runs 3 to 4 times slower and uses several times the power. But there are no masks and no NRE, and a mistake costs a recompile instead of a new chip.
:::

::: example
## How many 6-input LUTs does a 16-input AND need? How many LUT configuration bits does a device with 100,000 6-LUTs hold?

::: solution
1. **LUTs for the AND.**
   $$\left\lceil \frac{16 - 1}{6 - 1} \right\rceil = 3$$
   Two LUTs each AND six inputs, and a third combines their outputs with the remaining four.
2. **Configuration bits.** Each 6-input LUT holds 64 bits:
   $$\begin{aligned} &100\,000 \times 64 \\ &= 6.4\ \text{million bits} \\ &= 0.8\ \text{MB} \end{aligned}$$
3. **Routing.** That's before counting the routing switches, which need many more.

**Answer:** the 16-input AND takes 3 LUTs, and the device's LUTs hold 6.4 million configuration bits, about 0.8 MB.
:::
:::

::: example
## What are the configuration bits of a 2-input LUT that computes A AND NOT B?

::: solution
1. **Find where the output is 1.** A AND NOT B is 1 only for A = 1, B = 0.
2. **Find the bit that input selects.** A = 1, B = 0 selects C1 in the multiplexer tree.
3. **Fill in the bits.** C1 holds the 1 and the rest hold 0: C0 = 0, C1 = 1, C2 = 0, C3 = 0.

**Answer:** C0 = 0, C1 = 1, C2 = 0 and C3 = 0.
:::
:::

::: example
## How many 6-input LUTs does a 16-bit equality comparator need?

::: solution
1. **Compare the bit pairs.** Each LUT can compare three bit pairs (six inputs) and output whether all three match:
   $$\left\lceil \frac{16}{3} \right\rceil = 6\ \text{LUTs}$$
2. **Combine the results.** One more LUT ANDs their six outputs:
   $$6 + 1 = 7\ \text{LUTs}$$
   in two levels of logic.

**Answer:** 7 LUTs, in two levels of logic.
:::
:::

::: check
given: k = 3 | 4 | 5 | 6
answer: 2^k
tolerance: 0.4

## How many configuration bits does a {k}-input LUT hold?

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

::: misses
- [k] One bit per input combination, not per input.
- [any] Not yet. One bit for each row of the truth table.
:::
:::

::: check
given: k = 2 | 3 | 4
answer: 2^(2^k)
tolerance: 0.4

## How many different functions can a {k}-input LUT implement?

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

::: misses
- [2^k] That's the number of configuration bits; each pattern of them is a function.
- [any] Not yet. Count the possible truth tables.
:::
:::

::: check
given: n = 8 | 16 | 32; k = 3 | 4
answer: ceil((n - 1)/(k - 1))
tolerance: 0.4

## About how many {k}-input LUTs does an {n}-input AND gate need?

::: right
Right. $\lceil(n - 1)/(k - 1)\rceil$ = {answer}.
:::

::: misses
- [ceil(n/k)] The LUTs that combine results use up inputs too: $(n - 1)/(k - 1)$.
- [any] Not yet. Each extra LUT adds k − 1 new inputs.
:::
:::

::: check
given: N = 20 | 100 | 900; k = 4 | 6
answer: N*2^k/1000
tolerance: 0.5%

## A device has {N} thousand {k}-input LUTs. How many million configuration bits do the LUTs alone hold?

::: right
Right. $N \times 2^k$ = {answer} million bits.
:::

::: misses
- [any] Not yet. Bits per LUT times LUTs.
:::
:::

::: check
kind: parts
given: n = 12 | 24 | 36; k = 4 | 6

## An n-input AND, n = {n}, is built from {k}-input LUTs.

::: part
answer: ceil((n - 1)/(k - 1))
tolerance: 0

### How many LUTs does it need?

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

::: part
answer: 2^k
unit: bits
tolerance: 0

### How many configuration bits does each LUT hold?

::: misses
- [any] $2^k$.
:::
:::
:::

::: check
kind: parts
given: b = 8 | 16 | 32

## A {b}-bit equality comparator is built from 6-input LUTs, three bit pairs per LUT, then the results are ANDed.

::: part
answer: ceil(b/3)
tolerance: 0

### How many LUTs compare the bit pairs?

::: misses
- [any] $\lceil b/3\rceil$.
:::
:::

::: part
answer: ceil(b/3) + ceil((ceil(b/3) - 1)/5)
tolerance: 0

### How many LUTs in all, counting those that AND the results (6 inputs each)?

::: misses
- [any] Add $\lceil(m - 1)/5\rceil$ for m results.
:::
:::
:::

::: explain
## Why can one lookup table implement any function of its inputs?

::: model
A logic function of k inputs is completely described by its truth table, which lists one output bit for each of the 2^k input combinations. A lookup table is a small memory with exactly that many bits, addressed by the inputs: whatever combination arrives selects one stored bit, which becomes the output. Filling the memory with the function's truth table therefore makes the LUT behave exactly like that function, however many gates it would take to build it, and loading a different pattern makes it a different function. That's what lets an FPGA be configured into any circuit, as long as each piece of logic is cut into chunks with few enough inputs.
:::
:::

::: practice

::: check
given: k = 2 | 3 | 4 | 5 | 6 | 7 | 8
answer: 2^k
tolerance: 0.4

## Bits in a {k}-LUT?

::: misses
- [any] $2^k$.
:::
:::

::: check
given: n = 5 | 9 | 13 | 20 | 33 | 64 | 100; k = 3 | 4 | 5 | 6
answer: ceil((n - 1)/(k - 1))
tolerance: 0.4

## LUTs for an {n}-input AND with {k}-LUTs?

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