TDEB Top Bar

Trending
Semiconductor Industry News — The Digital Electronics Blog
Follow Us

TDEB Masthead

The Semiconductor Hub

Semiconductors.  Chips and Systems.  Innovation.

Indexed by leading Search Engines, AI and referenced by Engineers worldwide 500+ Articles 10M+ Pageviews 30+ Reports 50+ Citations

ID-Aware FIFO Design: ID Presence, Duplicates and Per-ID Counts (FIFO Series, Part 19)

The Semiconductor Hub
by
0
Share Facebook X LinkedIn WhatsApp Email
Hand-drawn cover card for FIFO Series Part 19, ID-Aware FIFO, with a sketch of entries moving from push to pop.

In Part 18 the FIFO handed out its own tags, so every tag was unique and an update could index straight into the array. Many queues do not get that luxury. The ID comes from outside: a transaction ID on a bus, a thread number, a source port. The producer picks it, and the same ID can appear several times in the queue at once. Now a different question matters: is this ID in the queue right now, and how many times?

This is Part 19 of the FIFO series, the ID-aware FIFO. It covers why logic outside the FIFO needs that answer, how to compute it by searching every entry, how duplicates change the problem, and what it costs as the depth and the ID width grow.

Key takeaways

  • An ID-aware FIFO stores a producer-supplied ID with every entry and answers presence queries: is ID X stored, and how many times.
  • Duplicates are the normal case, not an error. A single hit bit is not enough when the caller needs to know when the last copy leaves.
  • The direct implementation compares the query ID with every valid entry, then ORs the matches for a hit and counts them for a count.
  • A per-slot valid bit must gate every compare. Freed slots still hold old IDs, and they must never match.
  • Search cost grows with depth times ID width, per query port. When every ID must be answered every cycle, a different structure wins, which is the subject of Part 20.

In this part

What problem an ID-aware FIFO solves

Most of the time the question comes from an ordering rule. Bus protocols such as AXI let transactions with different IDs complete out of order, but transactions with the same ID must stay in order. A bridge or interconnect that holds requests in a FIFO and can also send some requests down a faster path has to know, before it lets a new request with ID X skip ahead, whether an older request with ID X is still waiting. If it is, the new one must wait behind it.

The same question shows up as a hazard check. A cache miss queue asks whether a miss to the same line is already pending, so it can merge instead of sending a duplicate request. A response router asks whether any request from a given source is still outstanding before it lets that source change mode. An error handler asks whether a faulty thread has anything left in flight. In each case the FIFO keeps its strict order for the data, and the ID check runs beside it.

Duplicates make it harder than it sounds. If ID 3 is stored twice and one copy pops, ID 3 is still present. Logic that waits for "ID 3 gone" needs a count, or at least a correct hit that only drops when the last copy leaves.

The interface

Hand-drawn block diagram of an ID-aware FIFO: each entry has a valid bit, an ID and data; push and pop on the sides; a query ID at the bottom feeds a bank of compares whose results give q_hit and q_count.

The write side is push, wid, wdata and full. The read side is pop, empty and the head entry as rid and rdata. The query port takes q_id and returns q_hit and q_count, the number of stored entries with that ID.

One rule needs writing down: which state does a query see? In the design below, the answer reflects the entries stored at the start of the cycle. A push in the same cycle is not counted yet, and a pop in the same cycle is still counted. That makes the query a clean function of registers, which is the easiest version to time and to verify. If the caller needs the same-cycle push included, add it outside as one extra compare.

Hand-drawn row of eight FIFO slots holding IDs, with the read pointer at slot 2 and six entries stored; a query for ID 3 matches slots 2, 4 and 6, giving a hit and a count of three, while free slot 0 still holds an old 3 that does not match.

The direct approach treats the ID column as a small content-addressable memory. Every slot compares its ID with q_id, and the result is ANDed with the slot's valid bit. The OR of those match bits is q_hit. Their population count is q_count.

The valid bit is not optional. In the example above, slot 0 was freed by an earlier pop and still holds ID 3. Without the valid gate it would report a fourth match. You can derive validity from the pointers, by checking whether each slot index lies between the read and write pointers, but that puts a subtraction and compare per slot in front of every match. A stored valid bit per slot, set on push and cleared on pop, is cheaper and faster.

Notice that the search does not care about order. The match vector says where copies of ID X are, not which is oldest. For presence and count that is fine. When you need the oldest or youngest match, for example to forward data, the priority has to follow the circular order from the read pointer. That is the associative FIFO in Part 21.

RTL sketch

A flip-flop FIFO with a show-ahead head, a stored valid bit per slot, and one query port that returns a hit and a count:

module id_aware_fifo #(
  parameter int W     = 32,
  parameter int IDW   = 4,
  parameter int DEPTH = 8,                 // power of two
  localparam int AW   = $clog2(DEPTH)
) (
  input  logic           clk,
  input  logic           rst_n,
  input  logic           push,
  input  logic [IDW-1:0] wid,
  input  logic [W-1:0]   wdata,
  output logic           full,
  input  logic           pop,
  output logic [IDW-1:0] rid,
  output logic [W-1:0]   rdata,
  output logic           empty,
  input  logic [IDW-1:0] q_id,
  output logic           q_hit,
  output logic [AW:0]    q_count
);
  logic [W-1:0]   data [DEPTH];
  logic [IDW-1:0] id   [DEPTH];
  logic [DEPTH-1:0] valid;
  logic [AW:0]    wptr, rptr;
  logic [DEPTH-1:0] match;

  wire do_pop  = pop  && !empty;
  wire do_push = push && (!full || do_pop);

  assign empty = (wptr == rptr);
  assign full  = ((wptr ^ rptr) == {1'b1, {AW{1'b0}}});
  assign rid   = id[rptr[AW-1:0]];
  assign rdata = data[rptr[AW-1:0]];

  always_comb begin
    q_count = '0;
    for (int i = 0; i < DEPTH; i++) begin
      match[i] = valid[i] && (id[i] == q_id);
      q_count  = q_count + match[i];
    end
  end
  assign q_hit = |match;

  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;
      id[wptr[AW-1:0]]   <= wid;
    end
endmodule

The ID and data arrays have no reset, which saves area. That is only safe because the valid bits are reset and every compare is gated by them.

Corner cases

Hand-drawn timing diagram of a four-entry ID-aware FIFO with the query held at ID 5: three pushes of ID 5 and one of ID 2 raise the count to three, a push and pop at full keeps it full, and pops bring the count back to zero.
  • Push and pop at full. Cycle 4 in the diagram. The pop clears the valid bit of the head slot and the push sets the valid bit of the same slot. The set must win, or the new entry becomes invisible to every query. The order of the two lines in the RTL is what makes it win.
  • The last copy leaves. The pop in cycle 6 removes one copy of ID 5 and the count reads 1 in cycle 7. Only after the pop in cycle 7 removes the last copy does it read 0. Logic that waits on q_hit falling sees exactly that.
  • Same-cycle push of the queried ID. Not counted until the next cycle. If the caller is checking whether a new request may skip ahead, the request it is about to push is not the one it is checking against, so this is usually what you want. Write it in the spec either way.
  • Freed slots with old IDs. Covered by the valid gate, and worth a dedicated test that frees a slot, leaves it unused, and queries its old ID.
  • Reset. Valid bits and pointers reset together. If the FIFO has a synchronous flush as well, the flush must clear all valid bits in the same cycle it moves the read pointer.

Timing, area and power

  • Compare cost. Each query port needs DEPTH comparators of IDW bits, an OR tree and a popcount. That is small for 8 entries and 4-bit IDs, and large for 64 entries and 10-bit IDs.
  • Query path. Compare, then a popcount adder tree of depth about log2 of DEPTH. If only the hit is needed, drop the count: an OR tree is much shorter than an adder tree.
  • More query ports. The whole compare bank is replicated per port. If several clients ask every cycle, the cost multiplies.
  • The other structure. A per-ID counter, incremented on push and decremented on pop, answers any ID with no search. Its cost grows with the number of possible IDs rather than the depth. With 4-bit IDs that is 16 small counters. With 12-bit IDs it is 4,096, and the search wins. Part 20 works through this trade in detail.
  • Power. The compare bank toggles whenever q_id or any stored ID changes. Gate the query when no client is asking.

Verification checklist

  • A reference queue of ID and data pairs. Every cycle, count the reference entries whose ID equals q_id and compare with q_count and q_hit.
  • Assertions:
    a_hit_count: assert property (@(posedge clk) disable iff (!rst_n)
                   q_hit == (q_count != 0));
    a_valid_sum: assert property (@(posedge clk) disable iff (!rst_n)
                   $countones(valid) == (wptr - rptr));
  • Bias the ID distribution. With uniform random IDs over a wide ID space, duplicates are rare and the interesting cases are never hit. Use a small pool of IDs for most of the run.
  • Coverage: every slot holding a duplicate, the count reaching DEPTH for one ID, push and pop at full, the last copy of an ID leaving, and a query of an ID that only lives in a freed slot.

A cycle-accurate Python model of this RTL ran 480,000 random cycles at depths 2 to 16 and ID pools of 2, 4 and 16 against a reference queue, and matched every query and every pop. It also reproduces the timing diagram above cycle for cycle.

Interview questions

1. Why does an ID-aware FIFO need a valid bit per slot?

A strong answer explains that freed slots keep their old IDs, that deriving validity from the pointers costs a compare per slot, and that a stored valid bit with the right set-over-clear priority at full is cheaper and faster.

2. Is a hit bit enough, or do you need a count?

A strong answer says the hit is enough for "is it present now", but a design that tracks presence by setting a bit on push and clearing it on pop breaks with duplicates. It then gives either the full search or a count per ID.

3. When would you search, and when would you keep a counter per ID?

A strong answer compares DEPTH times IDW compare bits per query port against 2 to the power IDW counters, and notes that counters answer all IDs at once while the search answers one per port.

4. Does your query see a push in the same cycle?

A strong answer picks a rule, explains why "state at the start of the cycle" is the easiest to time and verify, and shows how to add the same-cycle push with one external compare if needed.

5. How would you test duplicates properly?

A strong answer narrows the random ID pool so duplicates are common, covers the last copy leaving, and checks the count against a reference every cycle, not only at pops.

Where it is used in real chips

ID-aware queues sit wherever ordering is per ID rather than global. AXI and similar interconnect bridges track outstanding IDs to keep same-ID transactions ordered. Cache miss handling checks for pending requests to the same line. NoC routers and network interfaces track outstanding requests per source. PCIe interface logic tracks outstanding requests by tag, and DMA engines and accelerators check whether a channel or context still has work in flight before switching it.

The rest of the series

Part 18 covered the tagged FIFO, where the queue issues its own tags and metadata can be updated in place.

Part 20 takes the presence question to its hard form, one of the FIFO interview challenges shared by Rahul Behl: flag every stored ID, every cycle, with duplicates.

Share Facebook X LinkedIn WhatsApp Email

Post a Comment

0 Comments

Your comments will be moderated before it appears here.

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!