Synchronous FIFO design in Verilog

Design a synchronous FIFO using storage, read and write pointers, occupancy tracking, full and empty flags, and boundary checks.

FIFO structure

A synchronous FIFO uses one clock for writes and reads. It needs a storage array, write pointer, read pointer and an unambiguous way to distinguish full from empty. Tracking occupancy with an extra counter is easy to review.

localparam PTR_W = $clog2(DEPTH);
reg [WIDTH-1:0] mem [0:DEPTH-1];
reg [PTR_W-1:0] wr_ptr, rd_ptr;
reg [PTR_W:0]   count;

wire write_fire = write_en && !full;
wire read_fire  = read_en  && !empty;

assign empty = (count == 0);
assign full  = (count == DEPTH);

On a valid write, store the input and advance wr_ptr. On a valid read, capture or expose the selected entry and advance rd_ptr. Update occupancy according to the accepted operations:

case ({write_fire, read_fire})
  2'b10: count <= count + 1'b1;
  2'b01: count <= count - 1'b1;
  default: count <= count;
endcase

Simultaneous accepted read and write leave occupancy unchanged. Pointer wrapping needs special logic when DEPTH is not a power of two.

Boundary policy

Define what happens when a write is requested while full or a read is requested while empty. Common policies reject the operation and raise overflow/underflow status. Also define whether a simultaneous read permits a write when initially full and whether a simultaneous write permits a read when initially empty.

Verification targets

Check reset, first and last entries, wraparound, simultaneous operations, full/empty transitions and data ordering. A scoreboard queue should compare every accepted input with the corresponding output. Assertions should prevent count overflow and underflow.

For unrelated write and read clocks, use an asynchronous FIFO architecture with synchronized Gray-code pointers; this synchronous design is not safe across clock domains.

Continue into the complete lesson

Use the course lesson for the surrounding design rules, exercises, labs and knowledge check.

Open supporting lesson →
Related answers

Continue exploring this topic