RTL Design / Module 7: Datapath & Memory Systems
Lesson 7.2

Sync FIFO — design + full/empty truth

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.

Definition

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.

1

Block diagram — pointer FIFO

Sync FIFO block diagram

Write pointer chases read pointer around a ring buffer. Occupancy = distance between them (mod DEPTH).

Analogy

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.

2

Full/empty — two standard approaches

MethodHow it worksProsCons
Count registercount increments on write-only, decrements on read-only, holds on simultaneous rd+wrIntuitive; easy almost-full (count >= AF_LEVEL)Extra adder; width = $clog2(DEPTH)+1
Pointer compare + MSBExtend pointers by 1 bit; full when wr_ptr == {~rd_ptr[MSB], rd_ptr[MSB-1:0]}No count adder; classic ASIC styleHarder to reason about; MSB toggle on wrap
Keep one slot emptyNever let wr_ptr catch rd_ptr → effective DEPTH-1Simplest empty/fullWastes one entry
Trap

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.

3

Parameterized sync FIFO (count-based)

sync_fifo.v
// 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.

4

Pointer-compare full/empty (MSB trick)

fifo_ptr_flags.v
// 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.

5

Almost-full and back-pressure

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.

SignalWhen assertedTypical consumer
emptycount == 0Reader must not pop
fullcount == DEPTHWriter must not push
almost_fullcount >= AF_LEVELFlow-control FSM stops upstream early
almost_emptycount <= AE_LEVELWake downstream prefetch logic
Interview angle

"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.

6

Self-checking testbench sketch

sync_fifo_tb.v
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
▶ Run sync FIFO in simulator
7

Valid-ready wrapper around sync FIFO

Many blocks expose FIFOs as valid-ready ports instead of raw wr_en/rd_en. The adapter is straightforward:

fifo_vr_adapter.v
// 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
Analogy

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.

8

Verification checklist

  • Reset leaves FIFO empty with pointers at 0 and count = 0
  • Fill to DEPTH — full asserts; extra wr_en is ignored
  • Drain to empty — read order matches write order (FIFO property)
  • Simultaneous rd+wr at full-1 occupancy — count stable, no corruption
  • almost_full fires before full with correct margin

🎯 What to remember

  • Sync FIFO = ring buffer + write/read pointers on one clock.
  • Equal pointers ≠ always empty — use count or MSB extension for full DEPTH.
  • Count-based flags are easier to debug; pointer-compare is area-efficient at scale.
  • Almost-full is a flow-control signal, not a storage state — plan margin for upstream latency.
Advertisement