The Synchronous FIFO and the Full/Empty Problem
A synchronous FIFO uses a single common clock for write and read operations. When designing circular pointer logic, engineers encounter a fundamental ambiguity: When the write pointer equals the read pointer, the queue is either completely empty or completely full. Without disambiguation logic, equal pointer values cannot distinguish these opposite states.
The Full and Empty State Ambiguity
Consider a 4-entry circular buffer.
Both wr_ptr and rd_ptr initialize to 0.
With zero entries stored, wr_ptr == rd_ptr, indicating an empty buffer.
If a producer writes 4 consecutive entries without any read operations, wr_ptr increments through 1, 2, 3, and wraps back to 0.
The condition wr_ptr == rd_ptr evaluates to true again, but the buffer is full.
Comparing address pointers alone fails to distinguish empty from full. Misidentifying these states causes the consumer to read invalid data or prevents the producer from writing valid data.
Resolving Ambiguity with an Extra Pointer Bit
Industry designs resolve pointer ambiguity by widening pointers by one additional most-significant bit.
For a memory array of DEPTH entries, memory addressing requires $\log_2(\text{DEPTH})$ bits.
Designers declare pointers with $\log_2(\text{DEPTH}) + 1$ bits:
wire full = (wr_ptr[PTR_WIDTH-1] != rd_ptr[PTR_WIDTH-1]) &&
(wr_ptr[PTR_WIDTH-2:0] == rd_ptr[PTR_WIDTH-2:0]);
wire empty = (wr_ptr == rd_ptr);
The extra most-significant bit acts as a lap counter:
- Empty State: All pointer bits, including the extra most-significant bit, match exactly (
wr_ptr == rd_ptr). - Full State: The lower address bits match, but the extra most-significant bits differ. This difference confirms that the write pointer wrapped around the buffer once more than the read pointer:
Complete Parameterized Synchronous FIFO Implementation
`timescale 1ns / 1ps
`default_nettype none
module sync_fifo #(
parameter DATA_WIDTH = 8,
parameter DEPTH = 16, // Must equal a power of two
parameter ADDR_WIDTH = $clog2(DEPTH)
) (
input wire clk, rst_n,
input wire wr_en,
input wire [DATA_WIDTH-1:0] wdata,
output wire full,
input wire rd_en,
output reg [DATA_WIDTH-1:0] rdata,
output wire empty
);
reg [DATA_WIDTH-1:0] mem [0:DEPTH-1];
reg [ADDR_WIDTH:0] wr_ptr, rd_ptr; // Extended bit width for lap tracking
// Write path
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
wr_ptr <= 0;
end else if (wr_en && !full) begin
mem[wr_ptr[ADDR_WIDTH-1:0]] <= wdata;
wr_ptr <= wr_ptr + 1'b1;
end
end
// Read path
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
rd_ptr <= 0;
rdata <= {DATA_WIDTH{1'b0}};
end else if (rd_en && !empty) begin
rdata <= mem[rd_ptr[ADDR_WIDTH-1:0]];
rd_ptr <= rd_ptr + 1'b1;
end
end
// Status flag generation
assign empty = (wr_ptr == rd_ptr);
assign full = (wr_ptr[ADDR_WIDTH] != rd_ptr[ADDR_WIDTH]) &&
(wr_ptr[ADDR_WIDTH-1:0] == rd_ptr[ADDR_WIDTH-1:0]);
endmodule
Notice the protection terms wr_en && !full and rd_en && !empty.
These guards prevent pointer advancement during overflow and underflow conditions.
Notice also that memory indexing expressions mem[wr_ptr[ADDR_WIDTH-1:0]] discard the upper lap bit, using only physical address bits.
Power-of-Two Depth Constraint
This pointer tracking architecture requires DEPTH to be an exact power of two ($2^N$).
Powers of two ensure that binary overflow in the lower address bits coincides exactly with toggling the extra lap bit.
Non-power-of-two depths require modulo comparator logic or dedicated occupancy counters, which increase silicon area and propagation delay.
Self-Checking Verification Testbench
module tb_sync_fifo;
parameter DATA_WIDTH = 8, DEPTH = 4; // Small depth to force pointer wrap quickly
reg clk = 0, rst_n, wr_en, rd_en;
reg [DATA_WIDTH-1:0] wdata;
wire [DATA_WIDTH-1:0] rdata;
wire full, empty;
sync_fifo #(.DATA_WIDTH(DATA_WIDTH), .DEPTH(DEPTH)) dut (
.clk(clk), .rst_n(rst_n), .wr_en(wr_en), .wdata(wdata), .full(full),
.rd_en(rd_en), .rdata(rdata), .empty(empty)
);
always #5 clk = ~clk;
integer i;
initial begin
$dumpfile("tb.vcd");
$dumpvars(0, tb_sync_fifo);
rst_n = 0; wr_en = 0; rd_en = 0; wdata = 0;
@(negedge clk); rst_n = 1;
if (!empty) $display("FAIL: Expected empty after reset deassertion");
// Fill buffer to capacity
for (i = 0; i < DEPTH; i = i + 1) begin
wdata = i; wr_en = 1;
@(negedge clk);
end
wr_en = 0;
if (!full) $display("FAIL: Expected full flag assertion after %0d writes", DEPTH);
else $display("PASS: FIFO reports full correctly");
// Attempt invalid write while full: verify internal memory protection
wdata = 8'hFF; wr_en = 1;
@(negedge clk);
wr_en = 0;
// Drain all stored entries and verify order
for (i = 0; i < DEPTH; i = i + 1) begin
rd_en = 1;
@(negedge clk);
rd_en = 0;
if (rdata !== i)
$display("FAIL: Expected %0d, received %0d", i, rdata);
end
if (!empty) $display("FAIL: Expected empty flag assertion after complete drain");
else $display("PASS: FIFO drained cleanly without corruption from invalid write");
$finish;
end
endmodule
The testbench attempts an illegal write transaction while full = 1.
Draining the queue confirms that the illegal write did not overwrite stored data.
Test suites must cycle through full and empty states multiple times to verify pointer wraparound robustness.
Using an extra pointer bit resolves the full-empty ambiguity cleanly. The next article introduces Gray code pointers to prepare for asynchronous clock domain crossings.