---
title: Memory arrays and programmable logic
heading: Organizing memory, and logic you can rewrite after it's built
eyebrow: C7 · Lesson 45 · Memory arrays and programmable logic
xp: 40
---

::: hook
A memory is a lookup table: give it an address and it returns the word stored there. Turn that around and a lookup table is a circuit: store a function's truth table in a small memory, feed the function's inputs to the address lines, and the memory computes the function. That's exactly what an FPGA does, thousands of times over, with programmable wiring between the tables, so one chip can become any digital circuit you describe. This lesson covers how memories are organized and combined, and how memory becomes logic.
:::

::: play
- A 3-input lookup table, the heart of an FPGA: eight configuration bits $K_0$ to $K_7$ feed an 8-to-1 multiplexer whose select lines are the logic inputs A, B and C.
- It's configured as 3-input odd parity: $K_1$, $K_2$, $K_4$ and $K_7$ are 1. Flip A, B and C and Y follows $A \oplus B \oplus C$.
- Now change the configuration bits: set only $K_7$ and it becomes a 3-input AND, with no change of wiring.
:::
:::

```sim logic
input A, B, C, K0, K1, K2, K3, K4, K5, K6, K7
output Y
set K1 = 1
set K2 = 1
set K4 = 1
set K7 = 1
assign Y = A ? (B ? (C ? K7 : K6) : (C ? K5 : K4)) : (B ? (C ? K3 : K2) : (C ? K1 : K0))
```

::: predict
## A memory chip holds 512K words of 8 bits. How many address lines does it need?

- [ ] 8
- [ ] 512
- [x] 19
- [ ] 24

::: reveal
19: 512K is $2^{19}$ words (524,288), and each address line doubles the number of words. The 8 bits of width are the data lines, not address lines. The chip stores $2^{19} \times 8$ = 4 Mbit.
:::
:::

::: nameit
## Organization

A memory of $2^n$ words of m bits has n address lines and m data lines, and stores $2^n \times m$ bits. Physically it's a nearly square grid of cells: part of the address drives a **row decoder** that selects one word line, and the rest drives **column multiplexers** that pick the wanted bits from the long row, which keeps both the decoder and the lines short.

## Building bigger memories

- **Wider words:** put chips side by side, sharing the address lines; each supplies some of the data bits.
- **More words:** put chips in parallel on the data lines and decode the extra (top) address bits into **chip selects**, so exactly one chip responds.

The number of chips is (words needed ÷ words per chip) × (width needed ÷ width per chip).

## Memory as logic

A $2^n \times m$ ROM implements any m functions of n inputs: the address is the input and each data bit is one function's truth table. A **PLA** (programmable logic array) saves space for sparse functions with a programmable AND plane producing product terms and an OR plane combining them.

## FPGAs

An FPGA is a grid of **configurable logic blocks**, each with a few **k-input lookup tables** ($2^k$ bits of configuration memory driving a $2^k$:1 multiplexer, typically k = 4 to 6) and flip-flops, surrounded by programmable routing. Functions of more than k inputs are split across several LUTs. FPGAs add block RAMs, multiplier (DSP) blocks and clock managers. Loading a new **bitstream** changes every LUT and connection: the same chip becomes a different circuit.
:::

::: example
## Build a 64K × 16 memory from 16K × 8 chips.

::: solution
1. **Width.**
   $$\frac{16}{8} = 2$$
   chips side by side.
2. **Depth.**
   $$\frac{64\text{K}}{16\text{K}} = 4$$
   rows of chips, selected by a 2-to-4 decoder on the top 2 of the 16 address bits.
3. **Total.** 8 chips, each seeing the low 14 address bits.

**Answer:** 8 chips, in 4 rows of 2.
:::
:::

::: example
## A 4-input LUT implements a 7-input AND. How many LUTs does it take?

::: solution
1. **First LUT.** One LUT can AND 4 inputs.
2. **Second LUT.** A second ANDs its output with the remaining 3.
3. **In general.** An n-input AND needs $\lceil(n - 1)/3\rceil$ 4-input LUTs.

**Answer:** two LUTs.
:::
:::

::: example
## A full adder is stored in a ROM. How many address and data bits, and how many bits in all? What about a 4-bit adder with carry in?

::: solution
1. **Full adder.** Three inputs (A, B, carry in) address $2^3$ = 8 words, each holding two outputs (sum and carry):
   $$8 \times 2 = 16\ \text{bits}$$
2. **4-bit adder.** It has 9 inputs and 5 outputs (four sum bits and a carry out):
   $$2^9 \times 5 = 2560\ \text{bits}$$
3. **Chained full adders.** Chaining four full-adder ROMs needs only 64 bits: decomposing a function into small blocks is what keeps lookup-table logic practical.

**Answer:** 3 address bits and 2 data bits, 16 bits in all; a 4-bit adder with carry in needs 2560 bits.
:::
:::

::: check
given: k = 10 | 16 | 20 | 30
answer: k
tolerance: 0

## How many address lines does a memory of $2^k$ words need, for k = {k}?

::: right
Right. {answer}.
:::

::: misses
- [any] Not yet. Each address line doubles the number of words.
:::
:::

::: check
given: W = 16 | 64 | 512; m = 8 | 16 | 32
answer: W*1024*m/8/1024
unit: KiB
tolerance: 0.5%

## A memory holds {W}K words of {m} bits. How many kibibytes is that?

::: right
Right. {answer}.
:::

::: misses
- [W*m] Convert bits to bytes: divide by 8.
- [any] Not yet. Words × bits, then ÷ 8 for bytes.
:::
:::

::: check
given: Wt = 64 | 256; mt = 16 | 32; Wc = 16 | 32; mc = 4 | 8
answer: Wt/Wc*mt/mc
tolerance: 0

## How many {Wc}K × {mc} chips make a {Wt}K × {mt} memory?

::: right
Right. (words ratio) × (width ratio) = {answer}.
:::

::: misses
- [Wt/Wc] That's only the depth; also stack chips side by side for width.
- [mt/mc] That's only the width; also add rows for depth.
- [any] Not yet. Depth ratio times width ratio.
:::
:::

::: check
given: n = 4 | 8 | 10; m = 1 | 4 | 8
answer: 2^n*m
tolerance: 0

## How many bits must a ROM store to implement {m} functions of {n} inputs?

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

::: misses
- [n*m] One stored bit per truth-table row per function.
- [any] Not yet. A row for every input combination.
:::
:::

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

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

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

::: misses
- [k] It stores a whole truth table.
- [any] Not yet. One bit per input combination.
:::
:::

::: check
given: n = 16 | 19 | 22
answer: ceil((n - 1)/3)
tolerance: 0

## How many 4-input LUTs does an {n}-input AND gate take?

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

::: misses
- [ceil(n/4)] The LUTs' outputs must be combined too: each extra LUT absorbs only 3 new inputs.
- [any] Not yet. The first LUT takes 4 inputs; each later one takes 3 new inputs plus the previous output.
:::
:::

::: check
kind: boolean
vars: A, B
answer: ~A & B | A & ~B
ops: and, or, not

## A 2-input LUT's configuration bits are $K_0$ = 0, $K_1$ = 1, $K_2$ = 1, $K_3$ = 0 (selected by AB). What function does it compute?

::: right
Right. XOR: 1 in rows 01 and 10.
:::

::: misses
- [A | B] Row 11 holds 0.
- [any] Not yet. The configuration bits are the truth table.
:::
:::

::: check
kind: parts
given: D = 64 | 256; W = 32 | 64; d = 16 | 64; w = 8 | 16

## A {D}K × {W} memory is built from {d}K × {w} chips.

::: part
answer: D/d*W/w
tolerance: 0

### How many chips?

::: misses
- [D/d] Count the chips side by side for width too.
- [any] $\frac{D}{d}\times\frac{W}{w}$.
:::
:::

::: part
answer: log(D/d)/log(2)
tolerance: 0

### How many address bits drive the chip-select decoder?

::: misses
- [any] $\log_2\frac{D}{d}$.
:::
:::
:::

::: check
kind: derivation
vars: n = 2..32, m = 1..64, k = 3..8

## An n-input, m-output function is built as a ROM, or as an n-input AND from k-input LUTs.

::: step
answer: 2^n*m

### How many bits does the ROM need?
:::

::: step
answer: ceil((n - 1)/(k - 1))

### How many k-input LUTs does the AND need?
:::

::: right
Right. The ROM grows exponentially; decomposed logic doesn't.
:::
:::

::: explain
## How can a lookup table replace any logic gate network?

::: model
Any combinational function of k inputs is completely described by its truth table, $2^k$ output values. A lookup table stores exactly those values in small memory cells and uses the inputs as an address, through a multiplexer, to read the one that belongs to the current input combination. Whatever gates the function would need, the LUT produces the same output for every input. Changing the stored bits changes the function, which is how an FPGA becomes a different circuit when it loads a new configuration.
:::
:::

::: practice

::: check
given: k = 12 | 18 | 24
answer: 2^k
tolerance: 0

## How many words can {k} address lines select?

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

::: check
given: W = 4 | 16; n = 2 | 8
answer: W*n
unit: K
tolerance: 0.5%

## {n} chips of {W}K words each are stacked for depth. How many words in total (in K)?

::: misses
- [any] Multiply.
:::
:::

::: check
given: rows = 512 | 1024; cols = 256 | 1024
answer: log(rows)/log(2)
tolerance: 0.5%

## A memory array has {rows} rows. How many address bits drive its row decoder?

::: misses
- [any] $\log_2$ of the rows.
:::
:::
:::
