
Parts 19 and 20 asked whether an ID was in the queue. The next step is to ask for the entry itself. A load wants the data from the most recent store to the same address that is still waiting in a store queue. A response wants the request that carries its address. The FIFO still drains in order, but beside that sits a lookup port that searches by key and returns a matching entry. That is an associative FIFO.
This is Part 21 of the FIFO series. It covers the match logic, and the part that is easy to get wrong: when several entries match, picking the right one in circular order, where the youngest entry can sit at a lower index than the oldest.
Key takeaways
- An associative FIFO stores a key with every entry and adds a lookup port that returns a hit, the index of a matching entry and its data.
- When several entries match, the answer is usually the youngest one. Age follows the ring, not the slot index.
- A two-pass priority encoder solves it: look for the highest match below the write index first, and only if there is none, take the highest match overall.
- Valid bits must gate every compare, because freed slots keep their old keys.
- The lookup path is compare, priority encode, then a DEPTH-to-1 data mux. That chain sets the depth at which the design stops meeting timing.
In this part
- What problem an associative FIFO solves
- The interface
- Youngest match in circular order
- RTL sketch
- Corner cases
- Timing, area and power
- Verification checklist
- Interview questions
- Where it is used in real chips
- The rest of the series
What problem an associative FIFO solves
The classic case is a store queue or write buffer. Writes wait in a FIFO to drain to memory in order. Meanwhile a read to the same address arrives. Reading memory would return stale data, because the newer value is still in the queue. The fix is to search the queue by address and forward the data from the most recent matching write. If two writes to that address are waiting, the read must see the second one.
Other cases look the same from the hardware side. A request queue matches a response by address to find which request it completes. A miss queue checks whether an incoming miss can join an existing entry. A translation request queue looks up a page number to avoid issuing the same walk twice. In each case the FIFO order is untouched and the search is a read-only view of what is stored.
Whether you want the youngest or the oldest match depends on the question. Forwarding wants the youngest, because it holds the newest value. Completing a response in order usually wants the oldest. The logic is symmetric, so this part builds the youngest and notes the change for the oldest.
The interface

The FIFO side is familiar: push, wkey, wdata, full, pop, rkey, rdata and empty. The lookup port takes l_key and returns l_hit, l_idx and l_data. As in the earlier parts, the lookup sees the entries stored at the start of the cycle. A push in the same cycle is not visible yet, and a pop in the same cycle is still visible.
Youngest match in circular order

The compare gives a match vector, one bit per slot. The hard part is picking from it. In a ring, age does not follow the slot index. With the read pointer at slot 5 and the write pointer at slot 2, the entries in age order are slots 5, 6, 7, 0 and 1. Slot 0 is younger than slot 6, even though its index is lower.
The trick is to split the ring at the write index. Slots below the write index were written most recently, in increasing index order, so among them the highest index is the youngest. Slots at or above the write index are all older than any slot below it, and among them too the highest index is the youngest. So:
- Mask the match vector with "index below the write index". If anything is left, the youngest match is its highest set bit.
- Otherwise, the youngest match is the highest set bit of the whole match vector.
In case 1 of the figure, key 7 matches slots 0 and 6. Slot 0 is below the write index, so it wins. In case 2, the matches are slots 6 and 7, none below the write index, so slot 7 wins. Slot 3 holds an old 7 in both cases, but it is free, so the valid bit keeps it out.
The same works when the FIFO is full and the two pointers share an index, and when nothing has wrapped. For the oldest match, mask with "index at or above the read index" instead, and take the lowest set bit.
RTL sketch
module assoc_fifo #(
parameter int W = 32,
parameter int KW = 16, // key width
parameter int DEPTH = 8, // power of two
localparam int AW = $clog2(DEPTH)
) (
input logic clk,
input logic rst_n,
input logic push,
input logic [KW-1:0] wkey,
input logic [W-1:0] wdata,
output logic full,
input logic pop,
output logic [KW-1:0] rkey,
output logic [W-1:0] rdata,
output logic empty,
input logic [KW-1:0] l_key,
output logic l_hit,
output logic [AW-1:0] l_idx,
output logic [W-1:0] l_data
);
logic [W-1:0] data [DEPTH];
logic [KW-1:0] key [DEPTH];
logic [DEPTH-1:0] valid, match, newer;
logic [AW:0] wptr, rptr;
function automatic logic [AW-1:0] msb_index(input logic [DEPTH-1:0] v);
msb_index = '0;
for (int i = 0; i < DEPTH; i++) if (v[i]) msb_index = i[AW-1:0];
endfunction
wire do_pop = pop && !empty;
wire do_push = push && (!full || do_pop);
always_comb
for (int i = 0; i < DEPTH; i++) begin
match[i] = valid[i] && (key[i] == l_key);
newer[i] = (i < wptr[AW-1:0]); // written since the write index wrapped
end
assign l_hit = |match;
assign l_idx = |(match & newer) ? msb_index(match & newer) : msb_index(match);
assign l_data = data[l_idx];
assign empty = (wptr == rptr);
assign full = ((wptr ^ rptr) == {1'b1, {AW{1'b0}}});
assign rkey = key[rptr[AW-1:0]];
assign rdata = data[rptr[AW-1:0]];
always_ff @(posedge clk or negedge rst_n)
if (!rst_n) begin
wptr <= '0;
rptr <= '0;
valid <= '0;
end else begin
if (do_pop) valid[rptr[AW-1:0]] <= 1'b0;
if (do_push) valid[wptr[AW-1:0]] <= 1'b1; // wins on the same slot
if (do_push) wptr <= wptr + 1'b1;
if (do_pop) rptr <= rptr + 1'b1;
end
always_ff @(posedge clk)
if (do_push) begin
data[wptr[AW-1:0]] <= wdata;
key[wptr[AW-1:0]] <= wkey;
end
endmodule
The function is written as a loop for clarity. Synthesis turns it into a priority encoder. In a real design you would likely write both encoders as one structure over a vector twice the width, which is a common way to express a circular priority.
Corner cases
- Matches on both sides of the write index. The newer region always wins, however many older matches there are. This is the case a single priority encoder gets wrong.
- Lookup of the entry popping this cycle. It still hits, and its data is still valid this cycle. If the caller acts on
l_idxnext cycle, it must allow for the slot being freed or reused by then. - Push of a matching key in the same cycle. Not visible until next cycle. For store-to-load forwarding this matters: a read and a write to the same address in the same cycle need an explicit rule, either a bypass from
wdataor a stall of the read. - Partial matches. If entries are partial writes, a full-key match may cover only some of the bytes the reader needs. Then the lookup must also check coverage, or merge bytes from several entries. Part 22 adds byte enables.
- Freed slots. Keys stay behind after a pop. Every compare is gated by the valid bit, and the set-over-clear order keeps a slot valid when it is popped and pushed in the same cycle at full.
Timing, area and power
- The lookup chain. KW-bit compares, a reduce for the two-way choice, a priority encoder about log2 of DEPTH deep, and a DEPTH-to-1 mux of W bits. For deep or wide queues, keep the match vector one-hot and use an AND-OR mux driven by it, which avoids encoding to an index and decoding it again.
- Narrow the compare. Comparing a hash or a subset of the key bits first, then confirming with the full key a cycle later, shortens the path when a late answer is acceptable.
- Area. Each lookup port costs DEPTH comparators of KW bits plus the encoder and mux. Two lookup ports cost twice that. Keys in flops are needed for the compare, so only the data can move to SRAM, and then the data read takes a cycle after the match.
- Power. The comparators toggle whenever the lookup key changes. Hold
l_keysteady, or gate the compare bank, when no lookup is requested.
Verification checklist
- A reference queue of key and data. Every cycle, search it from the youngest end for
l_keyand compare hit, index and data. - Assertions:
a_hit_valid: assert property (@(posedge clk) disable iff (!rst_n) l_hit |-> valid[l_idx] && key[l_idx] == l_key); a_no_hit: assert property (@(posedge clk) disable iff (!rst_n) !l_hit |-> (match == '0)); - Coverage: matches only in the newer region, only in the older region, in both, a hit when full, a hit on the entry popping this cycle, and a key that only lives in a freed slot.
- Use a small key pool so multiple matches are common, and run long enough for the pointers to wrap many times, since the bug classes here depend on where the write index sits.
A cycle-accurate Python model of this RTL ran 480,000 random cycles at depths 2 to 16 and key pools of 2, 4 and 16 against a reference queue searched from the youngest end, and matched on every lookup, including tens of thousands of cycles with matches on both sides of the write index.
Interview questions
1. How do you find the youngest matching entry in a circular buffer?
A strong answer explains why the highest index is not the youngest once the pointers wrap, then gives the split at the write index, or the doubled-vector form of the same idea.
2. What changes if you need the oldest match instead?
A strong answer moves the split to the read index, picks the lowest set bit, and names a use, such as matching responses in order.
3. A load and a store to the same address arrive in the same cycle. What should forwarding do?
A strong answer defines the rule explicitly: bypass the incoming store data, or stall the load a cycle, and explains the timing cost of the bypass.
4. Your lookup misses timing at 32 entries. What do you change?
A strong answer uses a one-hot AND-OR mux instead of encode then decode, considers a partial-key or hashed first compare, and considers registering the match vector.
5. How do partial writes change the lookup?
A strong answer says a key match is not enough: the entry must cover all requested bytes, otherwise the load must merge from several entries or wait for the queue to drain.
Where it is used in real chips
Associative queues are common wherever data waits in order but must be found by content. Processor store queues and write buffers forward data to younger loads. Cache miss queues merge requests to the same line. DMA engines and NoC network interfaces match responses to outstanding requests. PCIe and AXI interface logic matches completions to requests, and accelerators use the same structure to detect address conflicts between work items in flight.
The rest of the series
Part 20 covered the FIFO-based ID tracker, a presence vector for every ID every cycle.
Part 22 lets a match change the queue: the write-coalescing FIFO, which merges a new write into a pending entry for the same address and combines byte enables.


Your comments will be moderated before it appears here.