S3 · Lesson 12 · Memories and FIFOs in RTL
Register files, inferred RAMs, and sizing the buffers between blocks
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?
- 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, words per clock; afterward it drains at r. The peak, , 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.
Predict
A 256-word burst is written at 100 MHz into a FIFO read continuously at 75 MHz. How deep must the FIFO be?
64 words. During the burst's 256 write clocks the reader removes 0.75 × 256 = 192 words, so 64 accumulate: . 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.
Name it
Memories in RTL
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 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 bits.
Sizing for bursts
If a burst of B words arrives at rate while the reader drains at , the backlog peaks at the burst's end:
plus margin for latency (pointer synchronization across clock domains adds a few entries). If the reader also has idle cycles, use its average rate.
Worked 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?
Show the solution
- Depth. The FIFO absorbs the difference in rates over the burst:
- Drain time. After the burst, the 64 words drain at 75 MHz: about 85 write clocks.
- 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.
Worked 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?
Show the solution
- Total bits.
- Blocks. Each block holds data bits in this shape:
- Address bits. bits: 10 go to every block, and the top 3 select which block's output to use.
- 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.
Worked 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?
Show the solution
- Reader's average rate.
- Backlog at the end of a burst.
- 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
How many address bits does a 1000-word memory need?
Check
A 256-word burst is written at 100 MHz into a FIFO read at 70 MHz. How deep must it be (no margin)?
Check
How many bits does each pointer of a 64-deep FIFO need, to tell full from empty?
Check
How many 32 Kb block RAMs does an 8192 × 16 memory need, assuming each holds 32,768 data bits in the needed shape?
Check
After a 128-word burst at 250 MHz into a FIFO read at 0.5 times that rate, how long does the FIFO take to drain?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
A 64 × 8 register file is built from flip-flops. How many?
Check
A FIFO built on a 2048-word memory takes 64-word bursts written at 200 MHz and read continuously at 75 MHz.
(a)
How many address bits does the memory need?
(b)
How wide is each pointer, to tell full from empty?
(c)
How many words does a burst leave queued at its end?
Check
A burst of B words is written at rate w while the reader drains at rate r < w.
Step 1
How long does the burst last?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What backlog does it leave?
Step 3
How long after the burst until the FIFO is empty?
Explain why
Why does a FIFO's pointer need one more bit than its address?
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
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.