↓ Skip to main content

Calculating FIFO Depth for FPGA Designs: A Practical Guide

Calculating FIFO Depth for FPGA Designs: A Practical Guide

FIFO (First In First Out) is a fundamental storage structure used extensively in FPGA design for data buffering and matching transmission rates between different clock domains or bus widths. A primary parameter in FIFO design is its depth—the number of data entries it can store. Proper depth calculation prevents data overflow while ensuring you do not waste excessive FPGA resources.

🛠️ Factors Influencing FIFO Depth
#

When determining the necessary depth, you must account for several hardware parameters:

  • FIFO Bit Width: This defines the size of each storage unit.
  • Data Word Length: This determines how many bits of effective data are contained in each word.
  • Total Storage Capacity: This dictates the maximum number of data entries the hardware can hold.

For example, in a FIFO with a 32-bit width and an 8-bit word length, each storage unit requires 32/8 = 4 bytes. If the total capacity is 128 bytes, the FIFO can store 128/4 = 32 data entries.

Beyond these dimensions, engineers must consider:

  • FPGA Resource Constraints: Oversized FIFOs consume valuable logic and memory blocks.
  • Application Requirements: Underestimating depth leads to packet loss or data corruption.
  • Storage Density: Using depths that are powers of two (2^n) optimizes resource utilization and simplifies address decoding logic.

For high-reliability real-time systems, understanding how these buffers interact with the OS and hardware is essential. You can find more on real-time system architecture at https://www.vxworks.net.

📝 Calculation Steps
#

To calculate the required FIFO depth, follow these steps:

  1. Analyze the Worst-Case Scenario: Identify the period where the buffer accumulates the most data (Write Volume - Read Volume).
  2. Calculate Maximum Capacity: Divide the total storage capacity by the write bit width to find the maximum word count.
  3. Select the Power of Two: Choose a depth that is the smallest power of two greater than or equal to the maximum required data count.
  4. Binary Conversion: Convert the depth into its binary representation for address line generation.

Note: If the write rate is slower than the read rate, overflow is unlikely. You only need to focus on depth design when the read rate lags behind the write rate.

💻 Code Examples
#

Verilog Example
#

The following Verilog logic demonstrates calculating a FIFO depth of 32 (requiring 5 bits) based on a 32-bit width, 8-bit word length, and 128-byte capacity.

// Example logic for depth calculation
parameter DATA_WIDTH = 32;
parameter WORD_SIZE = 8;
parameter FIFO_SIZE = 128;

wire [4:0] FIFO_DEPTH_WIDTH;
wire [4:0] FIFO_DEPTH;

assign FIFO_CELL_SIZE = DATA_WIDTH / WORD_SIZE;
assign FIFO_MAX_WORDS = FIFO_SIZE / FIFO_CELL_SIZE;
// Logic to find the next power of 2 would be implemented here

SystemVerilog Example
#

SystemVerilog simplifies this process using the built-in $clog2 function.

// FIFO parameters
parameter DATA_WIDTH = 32; // 32-bit
parameter WORD_SIZE = 8;   // 8-bit word length
parameter FIFO_SIZE = 128; // Total capacity 128 bytes

// Size of each FIFO storage cell
localparam FIFO_CELL_SIZE = DATA_WIDTH / WORD_SIZE;

// Maximum words the FIFO can store
localparam FIFO_MAX_WORDS = FIFO_SIZE / FIFO_CELL_SIZE;

// Select the next power of 2 for FIFO depth
localparam FIFO_DEPTH = (FIFO_MAX_WORDS > 0) ? 
                       (2**$clog2(FIFO_MAX_WORDS)) : 1;

// Bit width for the FIFO depth
localparam FIFO_DEPTH_WIDTH = $clog2(FIFO_DEPTH);

📊 Practical Calculation Examples
#

1. Matching Data Bandwidth
#

When matching specific bandwidths, you must account for the serialization factor.

Example: A serial LVDS interface runs at 200MHz with a 10-bit data width.

  • Front-end Bandwidth: 200MHz * 10 bits = 2Gbps.
  • Back-end Bandwidth: 100MHz * 32 bits = 3.2Gbps.

To match the bandwidth, the front-end data must be buffered.

  • Serialization Factor: 3.2Gbps / 2Gbps = 1.6.
  • Required Depth: Since the depth must be greater than the factor, a FIFO depth of 2 is selected.

2. Preventing Data Overflow
#

If the write rate exceeds the read rate, you must increase the depth to accommodate the delta.

Scenario A:

  • Write Rate: 100MB/s
  • Read Rate: 80MB/s
  • Max Wait Time: 50μs
  • Extra Storage Needed: 100MB/s * 50μs = 5000 bits. The FIFO must provide at least 5000 bits of additional depth beyond the standard storage requirements.

Scenario B (Asynchronous FIFO):

  • Write Clock: 100MHz, Read Clock: 80MHz.
  • Write Width: 16-bit, Read Width: 16-bit.
  • Data Input: 960 bits every 100 write cycles.
  • Read Output: 1 data word per clock cycle.

Calculation:

  1. Write Data: In 120 cycles, the maximum data written is 960 bits * 2 = 1920 bits.
  2. Read Data: In the same period (120/100 * 80 * 16 bits), the data read is 1536 bits.
  3. Max Buffer: 1920 - 1536 = 384 bits.
  4. Required Depth: 384 / 16 = 24.
  5. Final Depth: The next power of two is 32.

3. Optimizing Resource Utilization
#

Sometimes, you may need to reduce the FIFO depth to fit hardware constraints.

For instance, an 18Kb block RAM can support a depth of 512. However, if your design is restricted to a 9Kb RAM, you should scale the FIFO depth down to 256 to save resources. Designing for powers of two remains the standard practice to minimize the logic required for address decoding.

Related