A synchronous FIFO decouples producer and consumer on the same clock. Write and read pointers chase each other through a circular memory — but the hard part is knowing when the FIFO is full vs empty when both pointers look equal.
A sync FIFO has one clock domain. wr_en advances the write pointer; rd_en advances the read pointer. full blocks writes; empty blocks reads. Occupancy ranges from 0 to DEPTH entries.
Write pointer chases read pointer around a ring buffer. Occupancy = distance between them (mod DEPTH).
A circular parking garage with numbered spots. The write pointer is where the next car parks; the read pointer is where the next car leaves. When they point to the same spot, the garage is either completely empty or completely full — you need an extra bit of bookkeeping to tell which.
| Method | How it works | Pros | Cons |
|---|---|---|---|
| Count register | count increments on write-only, decrements on read-only, holds on simultaneous rd+wr | Intuitive; easy almost-full (count >= AF_LEVEL) | Extra adder; width = $clog2(DEPTH)+1 |
| Pointer compare + MSB | Extend pointers by 1 bit; full when wr_ptr == {~rd_ptr[MSB], rd_ptr[MSB-1:0]} | No count adder; classic ASIC style | Harder to reason about; MSB toggle on wrap |
| Keep one slot empty | Never let wr_ptr catch rd_ptr → effective DEPTH-1 | Simplest empty/full | Wastes one entry |
empty = (wr_ptr == rd_ptr) alone is correct only if you sacrifice one storage slot. For full DEPTH entries, equal pointers mean either empty or full — you must disambiguate with count or MSB trick.
// sync_fifo — count-based full/empty, parameterized DEPTH/WIDTH
module sync_fifo #(
parameter DEPTH = 16,
parameter WIDTH = 8,
parameter ADDR_W = $clog2(DEPTH),
parameter ALMOST_FULL = 14 // assert when count >= this
) (
input wire clk,
input wire rst_n,
input wire wr_en,
input wire [WIDTH-1:0] wdata,
output wire full,
output wire almost_full,
input wire rd_en,
output wire [WIDTH-1:0] rdata,
output wire empty
);
reg [WIDTH-1:0] mem [0:DEPTH-1];
reg [ADDR_W:0] count; // DEPTH+1 bits to hold 0..DEPTH
reg [ADDR_W-1:0] wr_ptr, rd_ptr;
wire wr_fire = wr_en && !full;
wire rd_fire = rd_en && !empty;
assign full = (count == DEPTH);
assign empty = (count == 0);
assign almost_full = (count >= ALMOST_FULL);
assign rdata = mem[rd_ptr];
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
count <= {(ADDR_W+1){1'b0}};
wr_ptr <= {ADDR_W{1'b0}};
rd_ptr <= {ADDR_W{1'b0}};
end else begin
case ({wr_fire, rd_fire})
2'b10: begin
mem[wr_ptr] <= wdata;
wr_ptr <= (wr_ptr == DEPTH-1) ? {ADDR_W{1'b0}} : wr_ptr + 1'b1;
count <= count + 1'b1;
end
2'b01: begin
rd_ptr <= (rd_ptr == DEPTH-1) ? {ADDR_W{1'b0}} : rd_ptr + 1'b1;
count <= count - 1'b1;
end
2'b11: begin
mem[wr_ptr] <= wdata;
wr_ptr <= (wr_ptr == DEPTH-1) ? {ADDR_W{1'b0}} : wr_ptr + 1'b1;
rd_ptr <= (rd_ptr == DEPTH-1) ? {ADDR_W{1'b0}} : rd_ptr + 1'b1;
// count unchanged — simultaneous rd+wr
end
default: ;
endcase
end
end
endmodule
Simultaneous read+write: When both wr_fire and rd_fire are true, count stays the same — one entry enters, one leaves. This is the common case in steady-state streaming.
// MSB-extended pointer compare — no count register
// wr_ptr, rd_ptr are (ADDR_W+1) bits wide
wire [ADDR_W:0] wr_ptr_ext, rd_ptr_ext;
assign empty = (wr_ptr_ext == rd_ptr_ext);
assign full = (wr_ptr_ext == {~rd_ptr_ext[ADDR_W], rd_ptr_ext[ADDR_W-1:0]});
// Pointer increment (wrap at DEPTH, toggle MSB on wrap)
// On each wr_fire: wr_ptr_ext <= wr_ptr_ext + 1 (mod 2*DEPTH)
// On each rd_fire: rd_ptr_ext <= rd_ptr_ext + 1 (mod 2*DEPTH)
The extra MSB flips each time the pointer wraps around the ring. When write has lapped read by a full revolution, the MSBs differ — that's full. When MSBs and lower bits match — that's empty.
Upstream logic needs advance warning before the FIFO overflows. almost_full fires when count >= ALMOST_FULL, typically set to DEPTH - 2 or DEPTH - margin depending on upstream latency.
| Signal | When asserted | Typical consumer |
|---|---|---|
empty | count == 0 | Reader must not pop |
full | count == DEPTH | Writer must not push |
almost_full | count >= AF_LEVEL | Flow-control FSM stops upstream early |
almost_empty | count <= AE_LEVEL | Wake downstream prefetch logic |
"Why almost-full instead of just full?" — The writer may have 2–3 cycles of pipeline latency before it can stop. Almost-full gives a cushion so you never attempt a write when full is already true.
module sync_fifo_tb;
reg clk, rst_n, wr_en, rd_en;
reg [7:0] wdata;
wire full, almost_full, empty;
wire [7:0] rdata;
sync_fifo #(.DEPTH(4), .WIDTH(8), .ALMOST_FULL(3)) dut (
.clk(clk), .rst_n(rst_n),
.wr_en(wr_en), .wdata(wdata), .full(full), .almost_full(almost_full),
.rd_en(rd_en), .rdata(rdata), .empty(empty)
);
initial clk = 0; always #5 clk = ~clk;
integer errors, expected_count, i;
reg [7:0] queue [0:15];
integer q_head, q_tail;
initial begin
errors = 0; q_head = 0; q_tail = 0;
rst_n = 0; wr_en = 0; rd_en = 0; wdata = 0;
#20 rst_n = 1;
// Fill to full
for (i = 0; i < 4; i = i + 1) begin
@(posedge clk); wr_en = 1; wdata = 8'h10 + i;
queue[q_tail] = wdata; q_tail = q_tail + 1;
end
@(posedge clk); wr_en = 0;
if (!full) begin $display("FAIL: should be full"); errors++; end
if (!almost_full) begin $display("FAIL: almost_full"); errors++; end
// Overflow attempt — must not corrupt
@(posedge clk); wr_en = 1; wdata = 8'hFF;
@(posedge clk); wr_en = 0;
if (full) begin $display("FAIL: write when full accepted"); errors++; end
// Drain and check order
while (q_head < q_tail) begin
@(posedge clk); rd_en = 1;
@(posedge clk); rd_en = 0;
if (rdata !== queue[q_head]) begin
$display("FAIL: expected %h got %h", queue[q_head], rdata);
errors++;
end
q_head = q_head + 1;
end
if (!empty) begin $display("FAIL: should be empty"); errors++; end
if (errors == 0) $display("=== ALL TESTS PASSED ===");
else $display("=== %0d FAILED ===", errors);
$finish;
end
endmodule
Many blocks expose FIFOs as valid-ready ports instead of raw wr_en/rd_en. The adapter is straightforward:
// Wrap sync_fifo with valid-ready push/pop semantics assign s_ready = !full; assign m_valid = !empty; assign wr_en = s_valid && s_ready; assign rd_en = m_valid && m_ready; assign wdata = s_data; assign m_data = rdata; // Transfer occurs when: // push: s_valid && !full // pop: !empty && m_ready // Skid buffer (7.1) is the 1-deep special case of this pattern
The sync FIFO is a warehouse with a loading dock (write) and shipping dock (read). Valid-ready adapters are the dock workers who only move pallets when both the truck and dock door are ready — wr_en and rd_en are the internal "forklift active" signals.
full asserts; extra wr_en is ignoredalmost_full fires before full with correct margin