---
title: Memories and FIFOs in RTL
heading: Register files, inferred RAMs, and sizing the buffers between blocks
eyebrow: S3 · Lesson 12 · Memories and FIFOs in RTL
xp: 50
---

::: hook
Storage dominates most chips' area, and how it's written in RTL decides whether the synthesizer builds thousands of flip-flops or one dense memory block. A memory written with a synchronous read maps onto an FPGA's block RAM or an ASIC's SRAM macro; the same memory read asynchronously may explode into flip-flops and multiplexers. Between blocks that run at different rates, FIFOs absorb bursts, and sizing one is a small calculation that every interface designer does: how deep must it be so that a burst never overflows it while the reader catches up?
:::

::: play
- A FIFO's occupancy over time: a burst of B words arrives at one word per write clock while the reader takes words at a fraction r of that rate, continuously.
- During the burst the FIFO fills at the difference of the rates, $1 - r$ words per clock; afterward it drains at r. The peak, $B(1 - r)$, is the depth the FIFO must have.
- Push r toward 1 and almost no buffering is needed; at r = 0.5 it must hold half the burst.
:::
:::

```sim plot
view: graph
x: t = 0..600
y: o = 0..300
slider: B = 32..512 = 256
slider: r = 0.1..0.95 = 0.75
plot: step(B - t)*(1 - r)*t + step(t - B)*((B*(1 - r) - r*(t - B)) + abs(B*(1 - r) - r*(t - B)))/2 | samples: 600 | label: FIFO occupancy, words (t in write clocks)
readout: peak occupancy, the depth needed = B*(1 - r)
readout: clocks to drain after the burst = B*(1 - r)/r
```

::: predict
## A 256-word burst is written at 100 MHz into a FIFO read continuously at 75 MHz. How deep must the FIFO be?

- [ ] 256 words
- [ ] 192 words
- [x] 64 words
- [ ] 25 words

::: reveal
64 words. During the burst's 256 write clocks the reader removes 0.75 × 256 = 192 words, so 64 accumulate: $B(1 - f_\text{read}/f_\text{write})$. Designers add margin for the synchronizer latency at the pointers (lesson 24) and round up to a power of two, so this FIFO would be 64 or 128 deep.
:::
:::

::: nameit
## Memories in RTL

```verilog
reg [31:0] mem [0:1023];
always @(posedge clk) begin
  if (we) mem[waddr] <= wdata;
  rdata <= mem[raddr];          // synchronous read: maps to block RAM
end
```

- A memory of D words needs $\lceil\log_2D\rceil$ address bits.
- **Block RAMs** in FPGAs (for example 36 Kb each, configurable as 1K × 36, 512 × 72 and so on) and SRAM macros in ASICs need a registered read. An asynchronous read can force the memory into flip-flops (or LUT RAM, for small memories).
- **Read-during-write** to the same address can return old data, new data or don't-care depending on the RAM; write RTL that doesn't depend on it, or match the primitive's behavior.
- **Register files** with many ports (a CPU's 2 read, 1 write) are built from flip-flops or special multiported cells, and their area grows quickly with ports.

## FIFOs

A synchronous FIFO is a dual-port memory plus a write pointer and a read pointer. Give each pointer one extra bit beyond the address: equal pointers mean **empty**; pointers equal except the extra bit mean **full** (the writer has lapped the reader). For a D-deep FIFO, pointers are $\log_2D + 1$ bits.

## Sizing for bursts

If a burst of B words arrives at rate $f_w$ while the reader drains at $f_r < f_w$, the backlog peaks at the burst's end:

$$\text{depth} \geq B\left(1 - \frac{f_r}{f_w}\right)$$

plus margin for latency (pointer synchronization across clock domains adds a few entries). If the reader also has idle cycles, use its average rate.
:::

::: example
## Size the FIFO for 256-word bursts written at 100 MHz and read continuously at 75 MHz. How long after the burst until it's empty?

::: solution
1. **Depth.** The FIFO absorbs the difference in rates over the burst:
   $$\begin{aligned} D &\geq 256(1 - 0.75) \\ &= 64\ \text{words} \end{aligned}$$
2. **Drain time.** After the burst, the 64 words drain at 75 MHz:
   $$\begin{aligned} t &= \frac{64}{75\ \text{MHz}} \\ &= 0.85\ \text{µs} \end{aligned}$$
   about 85 write clocks.
3. **Spacing the bursts.** The next burst must not start before then, or the FIFO's needed depth grows: the average write rate across bursts must stay below the read rate.

**Answer:** 64 words deep, and it empties 0.85 µs (about 85 write clocks) after the burst.
:::
:::

::: example
## A design needs an 8192 × 32 memory on an FPGA with 36 Kb block RAMs usable as 1K × 32. How many blocks does it take, and how many address bits?

::: solution
1. **Total bits.**
   $$8192 \times 32 = 262{,}144\ \text{bits}$$
2. **Blocks.** Each block holds $1024 \times 32 = 32{,}768$ data bits in this shape:
   $$\frac{262{,}144}{32{,}768} = 8\ \text{blocks}$$
3. **Address bits.** $\log_2 8192 = 13$ bits: 10 go to every block, and the top 3 select which block's output to use.
4. **From flip-flops.** It would take 262,144 of them, far more than most FPGAs have for logic.

**Answer:** 8 block RAMs and 13 address bits.
:::
:::

::: example
## Bursts of 64 words arrive at 200 MHz; the reader runs at 100 MHz but is busy one cycle in four. How deep must the FIFO be?

::: solution
1. **Reader's average rate.**
   $$0.75 \times 100 = 75\ \text{MHz}$$
2. **Backlog at the end of a burst.**
   $$\begin{aligned} B &= 64\left(1 - \frac{75}{200}\right) \\ &= 40\ \text{words} \end{aligned}$$
3. **Round up.** To a power of two, 64, which also covers the few entries lost to pointer synchronization if the clocks are unrelated.

**Answer:** at least 40 words, so a 64-word FIFO.
:::
:::

::: check
given: D = 300 | 1000 | 6000
answer: ceil(log(D)/log(2))
tolerance: 0

## How many address bits does a {D}-word memory need?

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

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

::: check
given: B = 64 | 256 | 1000; w = 100 | 200; r = 40 | 70
answer: B*(1 - r/w)
tolerance: 0.5%

## A {B}-word burst is written at {w} MHz into a FIFO read at {r} MHz. How deep must it be (no margin)?

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

::: misses
- [B*r/w] That's how many words the reader removes during the burst.
- [any] Not yet. The backlog grows at $f_w - f_r$.
:::
:::

::: check
given: D = 16 | 64 | 512
answer: log(D)/log(2) + 1
tolerance: 0.4

## How many bits does each pointer of a {D}-deep FIFO need, to tell full from empty?

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

::: misses
- [log(D)/log(2)] The extra bit distinguishes full from empty when the addresses match.
- [any] Not yet. Address bits plus one wrap bit.
:::
:::

::: check
given: D = 2048 | 8192 | 32768; W = 16 | 32
answer: ceil(D*W/32768 - 0.0001)
tolerance: 0

## How many 32 Kb block RAMs does a {D} × {W} memory need, assuming each holds 32,768 data bits in the needed shape?

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

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

::: check
given: B = 128 | 512; r = 0.5 | 0.8; w = 100 | 250
answer: B*(1 - r)/(r*w*1e6)*1e9
unit: ns
tolerance: 0.5%

## After a {B}-word burst at {w} MHz into a FIFO read at {r} times that rate, how long does the FIFO take to drain?

::: right
Right. The backlog $B(1 - r)$ drains at $rf_w$: {answer} ns.
:::

::: misses
- [any] Not yet. Peak occupancy over the read rate.
:::
:::

::: check
given: D = 64 | 256; W = 8 | 32
answer: D*W
tolerance: 0

## A {D} × {W} register file is built from flip-flops. How many?

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

::: misses
- [any] Not yet. Words times bits.
:::
:::

::: check
kind: parts
given: D = 512 | 2048 | 8192; B = 64 | 256 | 1024; w = 100 | 200; r = 50 | 75

## A FIFO built on a {D}-word memory takes {B}-word bursts written at {w} MHz and read continuously at {r} MHz.

::: part
answer: round(log(D)/log(2))
unit: bits
tolerance: 0

### How many address bits does the memory need?

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

::: part
answer: round(log(D)/log(2)) + 1
unit: bits
tolerance: 0

### How wide is each pointer, to tell full from empty?

::: misses
- [any] One bit more than the address.
:::
:::

::: part
answer: B*(1 - r/w)
tolerance: 0.5%

### How many words does a burst leave queued at its end?

::: misses
- [any] $B(1 - f_r/f_w)$.
:::
:::
:::

::: check
kind: derivation
vars: B = 16..4096, w = 1e7..1e9, r = 1e6..1e9

## A burst of B words is written at rate w while the reader drains at rate r < w.

::: step
answer: B/w

### How long does the burst last?
:::

::: step
answer: B*(1 - r/w)

### What backlog does it leave?
:::

::: step
answer: B*(1 - r/w)/r

### How long after the burst until the FIFO is empty?
:::

::: right
Right. The depth covers the difference in rates over the burst; the drain time is that backlog at the reader's rate.
:::
:::

::: explain
## Why does a FIFO's pointer need one more bit than its address?

::: model
A FIFO is full when the writer has written exactly as many more words than the reader has read as there are slots, and empty when they've written and read the same number. In both cases the write and read addresses point at the same slot, so the addresses alone can't tell full from empty. Adding one more bit to each pointer counts how many times each has wrapped around the memory. If the pointers are completely equal, the reader has caught up: empty. If the addresses match but the extra bits differ, the writer is a full lap ahead: full. That single bit replaces a separate occupancy counter.
:::
:::

::: practice

::: check
given: D = 100 | 700 | 1000 | 3000 | 5000 | 20000
answer: ceil(log(D)/log(2))
tolerance: 0

## Address bits for {D} words?

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

::: check
given: B = 100 | 400; w = 150 | 300; r = 60 | 120
answer: B*(1 - r/w)
tolerance: 0.5%

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

::: misses
- [any] $B(1 - f_r/f_w)$.
:::
:::

::: check
given: D = 16 | 32 | 64 | 256 | 1024 | 4096
answer: log(D)/log(2) + 1
tolerance: 0.4

## Pointer bits for a {D}-deep FIFO?

::: misses
- [any] $\log_2D + 1$.
:::
:::
:::
