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

Dynamically Resizable FIFO: Grow and Shrink the Depth Without Losing Order (FIFO Series, Part 35)

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

Most FIFOs have a depth fixed at design time. A queue that shares a memory with other queues, or a buffer whose depth software tunes for latency or power, must change depth while traffic is flowing. The hard part is not the new depth. It is getting there without dropping, duplicating or reordering a single entry that is already inside.

This is Part 35 of the FIFO Series. We separate the easy kind of resizing, a soft limit on occupancy, from the real kind, where the ring itself gets longer or shorter. Then we build a FIFO that grows and shrinks on request, applies the change at the first safe moment, and never stalls longer than it has to.

Key takeaways

  • A soft limit, where full compares the count to a programmable threshold, is enough when the goal is only to cap occupancy. It frees no storage.
  • Changing the length of the ring is safe only when the live entries do not wrap past the end of either the old or the new size.
  • Keeping the read pointer and a count, and deriving the write index from them, makes the change a single register update with no data movement.
  • A grow waits at most until the reader wraps. A shrink also steers new writes below the new size, which can hold pushes for a while.
  • Verify with random resize requests in the middle of random traffic, a scoreboard, and an assertion that no write ever lands on a live entry.

In this part

What problem a resizable FIFO solves

There are two different requests hiding behind the word resize. The first is "do not let this queue hold more than N entries". That is a soft limit: keep the storage, keep the pointers, and assert full when the count reaches N. Lowering N while the FIFO holds more than N entries is harmless. full stays high until the reader drains below the new limit. Nothing moves, and nothing is lost.

The second request is "give up the top of your storage" or "use more storage". It comes from a manager moving space between queues that share a memory, or from a power manager switching off the top of a large buffer. Now the ring changes length and the wrap point moves. If the live entries straddle the old wrap point at that moment, the order breaks. That case is the subject of this part.

The interface

The data side is the familiar one: push, wdata, full, pop, rdata and empty, with a registered read. The resize side has a request pulse, resize_req, the new size, resize_to, between 1 and MAX, and a status bit, resize_busy, that stays high until the change has taken effect. The current size and count are useful outputs for software and for the manager that moves space around.

Hand-drawn block diagram of a resizable FIFO: a storage array of MAX entries with an active size marker, a state block holding rptr, count and size, and resize_req, resize_to and resize_busy signals.

A new request while one is pending replaces it, so the manager writes the size it wants and waits for resize_busy to drop.

Micro-architecture: when is a resize safe?

Store the state as the read index rptr, the number of entries count, and the active size. The write index is not stored. It is (rptr + count) mod size. Because rptr is always below the size and count is at most the size, the sum is below twice the size and the modulo is a single compare and subtract. This choice is what makes resizing clean: change size, and the write index follows by itself.

Call end = rptr + count, the position one past the newest entry, before any wrap. If end is no larger than the size, the live entries are contiguous: they sit in rptr to end - 1 with no wrap. Changing the size from S to T is safe exactly when the entries are contiguous in both: end is at most S and at most T. The entries stay where they are, the read index is unchanged, and the next write goes to end under the new size.

Hand-drawn sequence for growing a FIFO from 8 to 12 entries: first the entries wrap and the resize waits, then the reader wraps and the entries are contiguous so the resize applies, then the writer continues past entry 8.

Growing from S to a larger T needs only end at most S. If the entries wrap at the moment of the request, wait. Once the reader passes the old end and wraps to zero, the entries are contiguous again, and the change applies. The writer then carries on past the old end into the new space instead of wrapping. The wait is at most one lap of the reader.

Hand-drawn sequence for shrinking a FIFO from 8 to 5 entries: entries reach past 5, then the writer wraps and is held at entry 5, then the reader wraps and entries 0 to 4 fit in the new size so the resize applies.

Shrinking from S to a smaller T needs end at most T, which may never happen on its own if the writer keeps filling the top of the old ring. So while a shrink is pending, the FIFO steers the writer. If the entries are contiguous but reach above T, writes continue up to the old end and wrap. Once the entries wrap, the writer is in the low part of the ring, and any write at index T or above is held. When the reader wraps, every live entry is below T, and the change applies, at most about two laps of the reader after the request. The hold condition reduces to one compare: the entries wrap and the write index is at least T, which is the same as end >= S + T.

RTL sketch

module resizable_fifo #(
  parameter int W   = 32,
  parameter int MAX = 64,                  // any value, power of two not needed
  localparam int CW = $clog2(MAX + 1)
) (
  input  logic          clk,
  input  logic          rst_n,
  input  logic          push,
  input  logic [W-1:0]  wdata,
  output logic          full,
  input  logic          pop,
  output logic [W-1:0]  rdata,
  output logic          empty,
  input  logic          resize_req,
  input  logic [CW-1:0] resize_to,         // 1 to MAX
  output logic          resize_busy
);
  logic [W-1:0]  mem [MAX];
  logic [CW-1:0] rptr, count, size, tgt;
  logic          pend;

  wire [CW:0]   end_r = rptr + count;      // one past the newest entry, no wrap
  wire [CW-1:0] lim   = (tgt < size) ? tgt : size;
  wire          apply = pend && (count == 0 || end_r <= lim);
  wire [CW-1:0] sz    = apply ? tgt : size;
  wire [CW-1:0] rbase = (apply && count == 0) ? '0 : rptr;
  wire          block = pend && !apply && (end_r >= size + tgt);

  assign empty = (count == 0);
  assign full  = block || (count == sz);
  assign resize_busy = pend;

  wire do_pop  = pop && !empty;
  wire do_push = push && !block && (count != sz || do_pop);

  wire [CW:0]   wsum  = rbase + count;
  wire [CW-1:0] widx  = (wsum >= sz) ? wsum - sz : wsum;
  wire [CW-1:0] rnext = (rptr + 1'b1 == sz) ? '0 : rptr + 1'b1;

  always_ff @(posedge clk or negedge rst_n)
    if (!rst_n) begin
      rptr  <= '0;
      count <= '0;
      size  <= CW'(MAX);
      tgt   <= CW'(MAX);
      pend  <= 1'b0;
    end else begin
      if (resize_req) begin
        pend <= 1'b1;
        tgt  <= resize_to;
      end else if (apply) begin
        pend <= 1'b0;
      end
      if (apply) size <= tgt;
      rptr  <= do_pop ? rnext : rbase;
      count <= count + do_push - do_pop;
    end

  always_ff @(posedge clk) begin
    if (do_push) mem[widx] <= wdata;
    if (do_pop)  rdata <= mem[rptr];
  end
endmodule

In the cycle a resize applies, the pointer moves for that cycle already use the new size through sz. That matters at the boundary: when a grow applies with end exactly equal to the old size, the old ring would send the next write to index 0, and the new ring correctly sends it to index S. The sketch was checked with a cycle-accurate Python model against a reference queue for 900,000 cycles with random resize requests, sizes from 1 to 32 including non-powers of two.

Corner cases

  • Resize while empty. Always safe. The read index resets to zero so that it is valid for any new size, including one smaller than the old read index.
  • Grow with the entries ending exactly at the old end. The entries are contiguous, so the change applies at once, and the next write goes to the first new slot rather than wrapping to zero.
  • Shrink to a size below the current count. The change waits until the reader has drained enough. Meanwhile the writer is held whenever its next slot is at or above the new size. If the reader stops, the resize stops too, which is correct: there is nowhere to put the data.
  • Push and pop at the full boundary in the cycle a shrink applies. The count equals the new size and the read index is zero, so the write goes to index zero, the slot being read. The registered read returns the old value, as in Part 1.
  • A new request while one is pending. The newer size replaces the older one, and the safety check uses the new target from the next cycle on.
  • Soft limit and hard resize together. Keep them separate. A soft limit should never move the wrap point.

Timing, area and power

  • Write index. The adder and the compare and subtract sit in front of the storage write address. At high frequency, register the write index and update it alongside count, recomputing it only in the cycle a resize applies.
  • Flags. full now depends on apply and block, both of which use end_r. As in Part 1, register the flags from next-state values if they feed a wide fanout.
  • Area. The state is three counters of about log2(MAX) bits and a pending target. The storage is sized for MAX, so a resizable FIFO only saves area when the freed space really goes to another user.
  • Power. Once the size is lowered and applied, entries at and above the size are guaranteed unused. Their storage rows or banks can be clock-gated or put into retention, which is one of the main reasons to resize at all. The low-power FIFO of Part 28 covers the gating itself.

Verification checklist

  • A reference-queue scoreboard on every popped value, with resize requests arriving at random times, including back to back.
  • An assertion that each write index is below the active size and never equal to the index of a live entry. In a simulator this needs a small shadow of live indices; formal tools handle it directly.
  • Assertions on the state:
    a_count_le_size: assert property (@(posedge clk) disable iff (!rst_n)
                       count <= size);
    a_rptr_in_range: assert property (@(posedge clk) disable iff (!rst_n)
                       (count != 0) |-> (rptr < size));
    m_fair_reader:   assume property (@(posedge clk) !empty |-> s_eventually pop);
    a_resize_done:   assert property (@(posedge clk) disable iff (!rst_n)
                       pend |-> s_eventually (!pend || resize_req));
    The last pair is a liveness check for formal: if the reader keeps popping, every resize completes unless a newer request replaces it.
  • Coverage: grow and shrink while empty, while contiguous, while wrapped, with a push held by the shrink rule, at the full boundary, and sizes of 1 and MAX.
  • Measure the longest pending time in random runs against the one-lap and two-lap bounds.

Interview questions

1. How do you lower the depth of a FIFO that is half full?

A strong answer first asks whether a soft limit is enough. If storage must really be freed, it explains that the entries must not straddle either wrap point at the moment of change, and describes waiting for a safe point.

2. Why store a count instead of a write pointer here?

A strong answer shows that with the read index and count as state, the write index follows the size automatically, so a resize is one register update with no pointer fix-up and no data movement.

3. A shrink never completes in your simulation. Why?

A strong answer spots that the writer keeps refilling the region above the new size, and adds a rule that holds writes at or above the new size once the entries wrap.

4. Can you resize without waiting at all?

A strong answer says yes, by copying entries into the new region, but that costs read and write bandwidth and a more complex state machine. It weighs that against a wait of at most one or two laps.

5. How would you verify it?

A strong answer combines a scoreboard with random resizes, an assertion that no write hits a live entry, a liveness check under a fair reader, and coverage of each safe and unsafe state.

Where it is used in real chips

Resizable queues show up where buffer space is a shared, managed resource: packet buffers in NoC routers and switches that move space between ports, DMA engines with software-tuned queue depths, PCIe and AXI interfaces where a driver trades buffer size against latency, and accelerators that repartition on-chip memory between input, weight and output staging for each workload.

The rest of the series

Part 34 built a FIFO that sustains a push and a pop every cycle from single-port banks.

Part 36 looks at almost-full thresholds that predict backpressure early enough to cover the latency of the pipeline feeding the FIFO.

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!