---
title: Memories and FIFOs
heading: Describing memories in Verilog so the tools build block RAM, address width and capacity, synchronous reads and dual ports, the first-in first-out buffer with its read and write pointers, full and empty flags, and sizing a FIFO to absorb a burst
eyebrow: D10 · Lesson 10 · Memories and FIFOs
xp: 40
---

::: hook
Registers are fine for a few values, but a frame of video, a packet of network data or a lookup table needs thousands of words. FPGAs provide dedicated **block RAMs** for this, and ASIC libraries provide compiled memories, and Verilog code written in the right pattern tells the tools to use them. The most common memory structure in digital design is the **FIFO**, a queue that lets a fast producer and a slower consumer, or two different clock domains, hand data to each other without either waiting on the other's every step.
:::

::: play
- A FIFO absorbing a burst: data arrives at w million words per second for B words, while the reader takes r million per second continuously.
- Occupancy (solid) climbs during the burst at the difference of the rates, then drains at the read rate.
- The FIFO must be at least as deep as the peak: $B(1 - r/w)$ words. The readout gives it.
:::

```sim plot
view: graph
x: t = 0..40
y: n = 0..1200
slider: w = 50..200 step 5 = 100
slider: r = 10..200 step 5 = 60
slider: B = 100..2000 step 50 = 1000
plot: ((w - r)*t*step(B/w - t) + ((w - r)*B/w - r*(t - B/w))*step(t - B/w))*step(w - r) + 0*t | samples: 600 | label: words in the FIFO (t in µs)
readout: depth needed, words = (B*(1 - r/w) + abs(B*(1 - r/w)))/2 | digits: 4
```

::: predict
## A 1000-word burst arrives at 100 million words per second, and the reader takes 60 million per second. How deep must the FIFO be?

- [ ] 1000 words
- [ ] 600 words
- [x] 400 words
- [ ] 60 words

::: reveal
The burst lasts 1000/100 = 10 µs, during which the reader removes 600 words, so the FIFO must hold the other 400: $B(1 - r/w)$ = 1000 × 0.4. After the burst it drains in 400/60 = 6.7 µs. Designers round up to a power of two, 512.
:::
:::

::: nameit
## Memories in Verilog

```verilog
reg [7:0] mem [0:1023];          // 1024 words of 8 bits
always @(posedge clk) begin
  if (we) mem[waddr] <= wdata;   // synchronous write
  rdata <= mem[raddr];           // synchronous read
end
```

A memory of D words of W bits stores D × W bits and needs $\lceil\log_2D\rceil$ address bits. Written with a registered read like this, the tools map it onto block RAM (FPGA block RAMs hold 18 or 36 kilobits each); asynchronous reads force it into slower, larger LUT memory. **Dual-port** memories have separate read and write addresses, often on separate clocks.

## The FIFO

A FIFO is a dual-port memory used as a ring buffer, with a **write pointer** and a **read pointer** that advance and wrap around. It's **empty** when the pointers are equal; **full** when the write pointer has lapped the read pointer, which is detected by giving each pointer one extra bit: equal addresses with differing extra bits means full. An **asynchronous FIFO** crosses clock domains by passing Gray-coded pointers through synchronizers (lesson 8).

## Sizing

To absorb a burst of B words written at rate w while reads continue at rate r < w, the FIFO needs at least

$$B\left(1 - \frac{r}{w}\right)$$

words. A FIFO can only smooth out bursts: if the average write rate exceeds the read rate, it eventually overflows whatever its size.
:::

::: example
## A memory holds 4096 words of 16 bits. How many bits and address lines? How many 36-kilobit block RAMs at least?

::: solution
1. **Total bits.**
   $$4096 \times 16 = 65\,536\ \text{bits}$$
2. **Address bits.**
   $$\lceil\log_2 4096\rceil = 12$$
3. **Block RAMs.** A 36-kilobit block RAM holds 36,864 bits:
   $$\frac{65\,536}{36\,864} = 1.78$$
   so it takes at least 2.

**Answer:** 65,536 bits, 12 address lines and at least 2 block RAMs.
:::
:::

::: example
## A 512-word FIFO uses pointers with one extra bit. How wide are they, and when is it full?

::: solution
1. **Pointer width.** One extra bit on top of the address:
   $$\lceil\log_2 512\rceil + 1 = 10\ \text{bits}$$
2. **When it's full.** The low 9 bits of the two pointers match and the top bits differ: the writer is exactly one lap ahead.

**Answer:** the pointers are 10 bits wide, and the FIFO is full when their low 9 bits match and their top bits differ.
:::
:::

::: example
## A burst of 100 words is written at 100 MHz while the reader drains at 80 MHz. How deep must the FIFO be?

::: solution
1. **What the reader removes.** The burst lasts 1 µs, and in that time the reader removes 80 words.
2. **What piles up.**
   $$\begin{aligned} &100 \times \left(1 - \frac{80}{100}\right) \\ &= 20\ \text{words} \end{aligned}$$
3. **Choose the depth.** A 32-word FIFO, the next power of two, holds them.
4. **Between bursts.** The reader must then catch up before the next burst.

**Answer:** it must hold 20 words, so a 32-word FIFO.
:::
:::

::: check
given: D = 256 | 1024 | 4096 | 65536
answer: ceil(log(D)/log(2))
tolerance: 0.4

## How many address bits does a memory of {D} words need?

::: right
Right. $\lceil\log_2D\rceil$ = {answer}.
:::

::: misses
- [any] Not yet. Enough bits to number every word.
:::
:::

::: check
given: B = 500 | 1000 | 4000; w = 100 | 250; r = 40 | 60 | 80
answer: B*(1 - r/w)
tolerance: 0.5%

## A {B}-word burst arrives at {w} M words/s while the reader takes {r} M words/s. How many words must the FIFO hold?

::: right
Right. $B(1 - r/w)$ = {answer} words.
:::

::: misses
- [B] The reader keeps taking words during the burst.
- [B*r/w] That's how many the reader removes; the FIFO holds the rest.
- [any] Not yet. Words written minus words read during the burst.
:::
:::

::: check
given: D = 1024 | 4096 | 16384; W = 8 | 16 | 32
answer: ceil(D*W/36864)
tolerance: 0.4

## A {D}-word, {W}-bit memory is built from 36-kilobit block RAMs. At least how many does it need?

::: right
Right. ⌈DW/36,864⌉ = {answer}.
:::

::: misses
- [any] Not yet. Total bits over bits per block.
:::
:::

::: check
given: D = 16 | 256 | 2048
answer: ceil(log(D)/log(2)) + 1
tolerance: 0.4

## A FIFO of depth {D} detects full and empty with pointers that carry one extra bit. How wide is each pointer?

::: right
Right. $\lceil\log_2D\rceil + 1$ = {answer} bits.
:::

::: misses
- [ceil(log(D)/log(2))] Add the extra bit that tells full from empty.
- [any] Not yet. Address bits plus one.
:::
:::

::: check
kind: parts
given: w = 1024 | 4096 | 16384; b = 8 | 16 | 32

## A memory holds {w} words of {b} bits.

::: part
answer: w*b
unit: bits
tolerance: 0

### How many bits?

::: misses
- [any] Words × width.
:::
:::

::: part
answer: ceil(w*b/36864)
tolerance: 0

### How many 36-kilobit block RAMs at least?

::: misses
- [any] Bits ÷ 36,864, rounded up.
:::
:::
:::

::: check
kind: parts
given: B = 50 | 100 | 1000; r = 50 | 80 | 90

## A burst of {B} words is written at 100 MHz while the reader drains at {r} MHz.

::: part
answer: B*(1 - r/100)
unit: words
tolerance: 0.5%

### How many words pile up?

::: misses
- [any] Burst × (1 − read/write rate).
:::
:::

::: part
answer: 2^ceil(log(B*(1 - r/100))/log(2))
unit: words
tolerance: 0

### What power-of-two FIFO depth holds them?

::: misses
- [any] Next power of two.
:::
:::
:::

::: explain
## Why does a FIFO need some way to tell full from empty beyond comparing addresses?

::: model
A FIFO's write and read pointers chase each other around the same ring of addresses. When the FIFO is empty, the reader has caught up with the writer, so the pointers point to the same address; when it's completely full, the writer has gone all the way around and caught up with the reader from behind, so again the pointers point to the same address. Comparing addresses alone can't distinguish the two. Giving each pointer one extra bit, which flips every time it wraps, records how many laps it has made: equal addresses with equal extra bits means empty, and equal addresses with different extra bits means the writer is one lap ahead, full.
:::
:::

::: practice

::: check
given: D = 2 | 4 | 16 | 100 | 1000 | 1000000
answer: ceil(log(D)/log(2))
tolerance: 0.4

## Address bits for {D} words?

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

::: check
given: B = 10 | 10000; w = 100 | 1000; r = 1 | 99
answer: B*(1 - r/w)
tolerance: 0.5%

## FIFO depth: burst {B}, write {w}, read {r}?

::: misses
- [any] $B(1 - r/w)$.
:::
:::
:::
