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.
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:
Both fixes are legitimate and widely used. Both change the safety analysis from 4.2 in ways that are easy to miss.
The obvious fix for a slow comparator: add a register on its output.
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.
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:
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.
| Implementation | Overflow events | Underflow events |
|---|---|---|
| Combinational (Lesson 4.2) | 0 | 0 |
| Naive registered (exact-equality, no margin) | 5,401 | 3,462 |
| Compensated registered (threshold, 1-slot margin) | 0 | 0 |
The exact same fix applies to EMPTY, symmetrically: threshold at occupancy ≤ 1 instead of ≤ 0, using <= instead of exact equality.
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.
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.
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.
Switch between all three implementations yourself and watch the occupancy trace turn red on overflow. 👇
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.
Try it yourself — copy this into our Verilog simulator and check the waveform. 👇
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
`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
>=/<= instead of exact equality, and set the threshold one slot early per register stage in the gating path.