D10 · Lesson 10 · Memories and FIFOs
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
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.
- 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: words. The readout gives it.
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?
The burst lasts 1000/100 = 10 µs, during which the reader removes 600 words, so the FIFO must hold the other 400: = 1000 × 0.4. After the burst it drains in 400/60 = 6.7 µs. Designers round up to a power of two, 512.
Name it
Memories in 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 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
words. A FIFO can only smooth out bursts: if the average write rate exceeds the read rate, it eventually overflows whatever its size.
Worked example
A memory holds 4096 words of 16 bits. How many bits and address lines? How many 36-kilobit block RAMs at least?
Show the solution
- Total bits.
- Address bits.
- Block RAMs. A 36-kilobit block RAM holds 36,864 bits: so it takes at least 2.
Answer 65,536 bits, 12 address lines and at least 2 block RAMs.
Worked example
A 512-word FIFO uses pointers with one extra bit. How wide are they, and when is it full?
Show the solution
- Pointer width. One extra bit on top of the address:
- 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.
Worked example
A burst of 100 words is written at 100 MHz while the reader drains at 80 MHz. How deep must the FIFO be?
Show the solution
- What the reader removes. The burst lasts 1 µs, and in that time the reader removes 80 words.
- What piles up.
- Choose the depth. A 32-word FIFO, the next power of two, holds them.
- Between bursts. The reader must then catch up before the next burst.
Answer it must hold 20 words, so a 32-word FIFO.
Check
How many address bits does a memory of 1024 words need?
Check
A 1000-word burst arrives at 100 M words/s while the reader takes 80 M words/s. How many words must the FIFO hold?
Check
A 4096-word, 8-bit memory is built from 36-kilobit block RAMs. At least how many does it need?
Check
A FIFO of depth 256 detects full and empty with pointers that carry one extra bit. How wide is each pointer?
Check
A memory holds 4096 words of 8 bits.
(a)
How many bits?
(b)
How many 36-kilobit block RAMs at least?
Check
A burst of 100 words is written at 100 MHz while the reader drains at 50 MHz.
(a)
How many words pile up?
(b)
What power-of-two FIFO depth holds them?
Explain why
Why does a FIFO need some way to tell full from empty beyond comparing addresses?
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
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.