CDC Course / Module 4: Asynchronous FIFO Design
Lesson 4.3

Full/Empty Flag Generation

Lesson 4.2 built flags that are provably safe — combinational, always safely late, never allowing an overflow or underflow. This lesson is about what happens when reality pushes back on that clean design: your timing report says the flag path is too slow, or a downstream engine needs more than zero cycles of warning before the FIFO actually fills. Both fixes are common. Both are also easy to get subtly, dangerously wrong — including one specific mistake this lesson will prove causes real data corruption, not just a theoretical risk.

1

Why the hard boundary sometimes isn't enough

The FULL and EMPTY flags from 4.2 are combinational — a pure wire from the comparator to whatever reads them. Two things push real designs away from that clean picture:

  • Timing closure. As clock frequency climbs, that comparator (an (N+1)-bit XOR/compare) can become the longest path in the design. Registering its output breaks the path into two shorter ones — at the cost of one extra cycle of latency before the flag is visible.
  • Zero warning is sometimes too little. A combinational FULL flag tells a consumer "stop right now" — but if that consumer is itself pipelined (say, an AXI-style streaming source that takes a few cycles to actually stop asserting valid after seeing back-pressure), "right now" isn't early enough. It needs a flag that fires before the hard boundary.

Both fixes are legitimate and widely used. Both change the safety analysis from 4.2 in ways that are easy to miss.

2

Registering the flag — and the bug it introduces

The obvious fix for a slow comparator: add a register on its output.

always @(posedge clk_a) full_reg <= full_comb;
one flip-flop, seemingly harmless

If full_reg is only used for status reporting, this is completely safe — it's just a slightly stale copy of an already-safe signal. The danger appears the moment full_reg — not full_comb — is what actually gates writes. That extra register adds one more cycle between the FIFO becoming truly full and the gating logic finding out, and during that cycle, nothing is stopping another write from going through.

This is not a theoretical risk. Gating writes on a naively registered flag — using the exact same equality-based comparator from 4.2, just with a flip-flop bolted onto its output — was simulated across 324 clock-ratio and write/read-rate combinations. Result: 5,401 overflow events and 3,462 underflow events. The exact same test with the un-registered, combinational flag from 4.2: zero, across every combination.

The root cause is subtle: 4.2's FULL formula is an exact-equality check — it fires the instant occupancy hits precisely the "lapped by one DEPTH" Gray pattern. With a register in the gating path, the pointer can advance past that exact instant before the registered flag catches up — and an equality check that gets skipped over never fires again until the pointer wraps all the way back around.

3

The fix: threshold occupancy, not exact equality

The correct approach reuses the decode-then-subtract technique from 4.2's "why not decode back to binary" section — but this time it's exactly the right tool, because a threshold comparison (>=), unlike an equality check, stays true even if the pointer jumps past the trigger point:

occupancy = (wptr_bin − rptr_bin_sync) & mask;
full_comb = (occupancy >= DEPTH − 1); // one slot early, to absorb the register stage
full_reg <= full_comb; // and full_reg is what actually gates writes
threshold set one slot early specifically to cover the added register latency

Two changes from the naive version, and both are load-bearing: the comparison is >= instead of == (so it latches instead of momentarily missing the trigger), and the threshold is DEPTH − 1 instead of DEPTH — deliberately declaring full one write early, exactly enough to absorb the extra register's one cycle of lag before the true boundary is reached.

✅ Verified — same 324-combination sweep, three implementations compared
ImplementationOverflow eventsUnderflow events
Combinational (Lesson 4.2)00
Naive registered (exact-equality, no margin)5,4013,462
Compensated registered (threshold, 1-slot margin)00

The exact same fix applies to EMPTY, symmetrically: threshold at occupancy ≤ 1 instead of ≤ 0, using <= instead of exact equality.

4

Almost-full / almost-empty: early warning on purpose

Once you have real occupancy available (which the compensated approach above computes anyway), programmable watermark flags are almost free: compare that same occupancy count against a threshold that isn't DEPTH or 0, but a margin you choose.

prog_full = (occupancy >= ALMOST_FULL_THRESH);
prog_empty = (occupancy <= ALMOST_EMPTY_THRESH);
set ALMOST_FULL_THRESH = DEPTH gives you back exactly the hard FULL flag — verified as an exact-match sanity check before this lesson was written
🚦 The yellow-light analogy

FULL and EMPTY are the red light — stop, no exceptions. PROG_FULL and PROG_EMPTY are the yellow light — a warning with enough lead time for something already in motion to react before it has to slam the brakes.

Unlike the hard FULL/EMPTY flags, watermark flags are advisory — nothing overflows or underflows if a watermark lags by a cycle or two, so they don't carry the same registration hazard from Section 2. That said, if a watermark is used to gate something safety-critical, the same threshold-margin reasoning applies.

5

Does the flag itself need a synchronizer?

Not to be used where it's generated. FULL is built entirely from local write-domain registers plus an already-synchronized copy of the read pointer — by the time it exists, it's already safe to use anywhere in the write domain, no further synchronizer needed.

But if a third domain wants to look at it — a CPU reading a status register over an unrelated bus clock, or an interrupt controller in a different domain — that's a brand new clock crossing. FULL is a single bit, so Lesson 3.1's two-flop synchronizer is exactly the right (and sufficient) tool. Forgetting this is a common real bug: the FIFO logic itself is airtight, but the status register that exposes full to software was wired up as a plain wire across domains.

6

Checklist

⚖️
Threshold, not equality
Any flag derived from an occupancy count must use >= or <=. Exact equality can be skipped over the moment there's any latency in the gating path.
➖
Margin for every register stage
Each pipeline stage between the comparator and the point that actually gates writes/reads needs its own slot of threshold margin.
🔁
Re-synchronize for a third domain
A flag is only "already safe" inside the domain it was generated in. Crossing into any other domain is a new crossing that needs its own synchronizer.

Switch between all three implementations yourself and watch the occupancy trace turn red on overflow. 👇

🧪

Interactive Lab: Combinational vs. Registered Flags

An 8-deep FIFO (N=3). Pick a flag implementation and watch what happens to occupancy — it should never exceed 8 or go negative. PROG_FULL/PROG_EMPTY use your chosen watermark thresholds and are always the safe threshold-based version, independent of the flag mode.

—
attempt succeeded attempt blocked

Try it yourself — copy this into our Verilog simulator and check the waveform. 👇

fifo_flags.v
module fifo_flags #(
    parameter ADDR_WIDTH = 3,
    parameter ALMOST_FULL_THRESH  = 6,
    parameter ALMOST_EMPTY_THRESH = 2
) (
    // Write domain
    input  wire                  clk_a,
    input  wire                  rst_a_n,
    input  wire [ADDR_WIDTH:0]   wptr_bin,          // local, already up to date
    input  wire [ADDR_WIDTH:0]   rptr_gray_sync,     // already synchronized (Lesson 3.1)
    output reg                   full,               // registered, safe to gate writes
    output reg                   prog_full,

    // Read domain
    input  wire                  clk_b,
    input  wire                  rst_b_n,
    input  wire [ADDR_WIDTH:0]   rptr_bin,          // local, already up to date
    input  wire [ADDR_WIDTH:0]   wptr_gray_sync,     // already synchronized (Lesson 3.1)
    output reg                   empty,              // registered, safe to gate reads
    output reg                   prog_empty
);

    localparam DEPTH = 1 << ADDR_WIDTH;
    localparam [ADDR_WIDTH:0] PTR_MASK = (1 << (ADDR_WIDTH + 1)) - 1;

    // ---- Gray -> binary (cascaded XOR, Lesson 4.1) ----
    function [ADDR_WIDTH:0] gray2bin(input [ADDR_WIDTH:0] g);
        integer i;
        begin
            gray2bin[ADDR_WIDTH] = g[ADDR_WIDTH];
            for (i = ADDR_WIDTH - 1; i >= 0; i = i - 1)
                gray2bin[i] = gray2bin[i+1] ^ g[i];
        end
    endfunction

    // ---- Write domain: occupancy, FULL, PROG_FULL ----
    wire [ADDR_WIDTH:0] rptr_bin_sync = gray2bin(rptr_gray_sync);
    wire [ADDR_WIDTH:0] wr_occupancy  = (wptr_bin - rptr_bin_sync) & PTR_MASK;

    // Threshold set to DEPTH-1, not DEPTH: absorbs this register's own 1-cycle
    // latency. Using >= (not ==) means the flag latches even if occupancy jumps
    // straight past the exact trigger value in one cycle.
    always @(posedge clk_a or negedge rst_a_n) begin
        if (!rst_a_n) begin
            full      <= 1'b0;
            prog_full <= 1'b0;
        end else begin
            full      <= (wr_occupancy >= (DEPTH - 1));
            prog_full <= (wr_occupancy >= ALMOST_FULL_THRESH);
        end
    end

    // ---- Read domain: occupancy, EMPTY, PROG_EMPTY ----
    wire [ADDR_WIDTH:0] wptr_bin_sync = gray2bin(wptr_gray_sync);
    wire [ADDR_WIDTH:0] rd_occupancy  = (wptr_bin_sync - rptr_bin) & PTR_MASK;

    always @(posedge clk_b or negedge rst_b_n) begin
        if (!rst_b_n) begin
            empty      <= 1'b1;
            prog_empty <= 1'b1;
        end else begin
            empty      <= (rd_occupancy <= 1);
            prog_empty <= (rd_occupancy <= ALMOST_EMPTY_THRESH);
        end
    end

endmodule
tb_fifo_flags.v
`timescale 1ns/1ps
module tb_fifo_flags;

    localparam ADDR_WIDTH = 3;

    reg clk_a = 0, clk_b = 0;
    reg rst_a_n = 0, rst_b_n = 0;
    reg [ADDR_WIDTH:0] wptr_bin = 0, rptr_bin = 0;
    reg [ADDR_WIDTH:0] rptr_gray_sync = 0, wptr_gray_sync = 0;
    wire full, prog_full, empty, prog_empty;

    always #5   clk_a = ~clk_a;
    always #3.5 clk_b = ~clk_b;

    fifo_flags #(.ADDR_WIDTH(ADDR_WIDTH), .ALMOST_FULL_THRESH(6), .ALMOST_EMPTY_THRESH(2)) dut (
        .clk_a(clk_a), .rst_a_n(rst_a_n), .wptr_bin(wptr_bin), .rptr_gray_sync(rptr_gray_sync),
        .full(full), .prog_full(prog_full),
        .clk_b(clk_b), .rst_b_n(rst_b_n), .rptr_bin(rptr_bin), .wptr_gray_sync(wptr_gray_sync),
        .empty(empty), .prog_empty(prog_empty)
    );

    initial begin
        $dumpfile("fifo_flags.vcd");
        $dumpvars(0, tb_fifo_flags);

        #20 rst_a_n = 1; rst_b_n = 1;

        // Drive the write pointer up to DEPTH (8) with the read side parked,
        // watching prog_full assert at 6, then full assert at 7 (DEPTH-1 margin)
        repeat (8) begin
            @(posedge clk_a);
            wptr_bin = wptr_bin + 1;
            #1 $display("@%0t: wptr_bin=%0d prog_full=%b full=%b", $time, wptr_bin, prog_full, full);
        end

        #40 $finish;
    end

endmodule

🎯 What to remember

  • Registering a flag purely for reporting is always safe. Registering a flag that gates writes/reads changes the safety analysis — the extra cycle of latency needs to be accounted for.
  • Naively registering 4.2's exact-equality FULL check and using it for gating is a real, verified bug: 5,401 overflow events across a 324-combination sweep, versus zero for the combinational original.
  • The fix: compute actual occupancy, compare with >=/<= instead of exact equality, and set the threshold one slot early per register stage in the gating path.
  • Almost-full/almost-empty watermarks reuse that same occupancy calculation with a programmable threshold — set the threshold to DEPTH or 0 and you get back the hard flags exactly.
  • A flag is safe within the domain it's generated in. Reading it from a third clock domain is a brand new crossing that needs its own two-flop synchronizer.
Advertisement