Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 37 ยท Design Patterns

FIFOs and Ready/Valid Handshakes

Design a synchronous FIFO in VHDL with full and empty flags, then connect producer and consumer with a ready/valid handshake so no data is lost or duplicated.

When two blocks exchange data at unpredictable moments, you need a way to say "I have something" and "I can take it" and, often, a small buffer in between. Two patterns solve almost all of it: the ready/valid handshake and the FIFO (first in, first out buffer). Together they appear in nearly every real design, from UARTs to video pipelines to bus bridges.

VHDL is read-only on this site; the C programs are cycle-by-cycle models of the same logic, which you can run and change.

The ready/valid handshake

Two signals travel with the data:

  • valid (from the producer): "the data on the bus is real".
  • ready (from the consumer): "I can accept data this cycle".

A transfer happens on a clock edge when both are high. Neither side waits for the other to go first: a producer may raise valid whether or not ready is high, and the consumer may raise ready before data exists. If valid is high and ready is low, the producer must hold the data stable until the transfer happens.

flowchart LR
  P["producer"] -->|"data, valid"| C["consumer"]
  C -->|"ready"| P

A transfer occurs on every clock edge where valid = 1 and ready = 1.

VHDL
-- consumer side, one transfer per edge with valid and ready both high
process (clk)
begin
  if rising_edge(clk) then
    if valid = '1' and ready = '1' then
      received <= data;
      count    <= count + 1;
    end if;
  end if;
end process;

Rules that avoid deadlock: the producer must not wait for ready before asserting valid; and the consumer may wait for valid before asserting ready, but it is better not to.

The FIFO

A FIFO is an array with a write pointer and a read pointer that wrap around (a circular buffer), plus a counter or flags for empty and full.

VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity fifo is
  generic (WIDTH : natural := 8; DEPTH : natural := 16);
  port (clk, rst : in std_logic;
        wr_en  : in  std_logic;
        din    : in  std_logic_vector(WIDTH-1 downto 0);
        full   : out std_logic;
        rd_en  : in  std_logic;
        dout   : out std_logic_vector(WIDTH-1 downto 0);
        empty  : out std_logic);
end entity;

architecture rtl of fifo is
  type mem_t is array (0 to DEPTH-1) of std_logic_vector(WIDTH-1 downto 0);
  signal mem   : mem_t;
  signal wr_ptr, rd_ptr : natural range 0 to DEPTH-1 := 0;
  signal count : natural range 0 to DEPTH := 0;
begin
  full  <= '1' when count = DEPTH else '0';
  empty <= '1' when count = 0     else '0';
  dout  <= mem(rd_ptr);                      -- first word at the head

  process (clk)
    variable do_wr, do_rd : boolean;
  begin
    if rising_edge(clk) then
      if rst = '1' then
        wr_ptr <= 0; rd_ptr <= 0; count <= 0;
      else
        do_wr := wr_en = '1' and count /= DEPTH;
        do_rd := rd_en = '1' and count /= 0;
        if do_wr then
          mem(wr_ptr) <= din;
          wr_ptr <= (wr_ptr + 1) mod DEPTH;
        end if;
        if do_rd then
          rd_ptr <= (rd_ptr + 1) mod DEPTH;
        end if;
        if do_wr and not do_rd then count <= count + 1;
        elsif do_rd and not do_wr then count <= count - 1;
        end if;
      end if;
    end if;
  end process;
end architecture;

Design points:

  • Guard against overflow and underflow: writes while full and reads while empty are ignored. Without the guards a full FIFO silently overwrites old data.
  • Simultaneous read and write in one cycle leaves count unchanged.
  • This version reads dout combinationally from mem ("first-word fall-through"). A registered read output suits block RAM better but delays data by one cycle.
  • A FIFO between two different clocks needs Gray-coded pointers (CDC lesson); the code above is single-clock.

FIFO as a handshake buffer

Connect the FIFO to the handshake world with ready = not full on the input side, and valid = not empty on the output side:

VHDL
in_ready  <= not full_s;
wr_en     <= in_valid and not full_s;
out_valid <= not empty_s;
rd_en     <= out_ready and not empty_s;

The FIFO now absorbs bursts: the producer can run ahead of the consumer by up to DEPTH words.

A C model of the FIFO

The model below mirrors the VHDL: arrays, pointers, a count, guards. A scripted run writes bursts and reads slower than the writes, reporting dropped writes and empty reads.

C C stand-in: circular-buffer FIFO with full/empty guards

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Watch count climb to 4 (full) while the producer bursts: further writes report FULL, and those values are lost unless the producer respects ready. The consumer's reads then return data in the same order it was written.

Using the handshake properly

The version above loses data when the producer ignores the full flag. With a proper handshake the producer holds its word until the transfer is accepted, so nothing is lost.

C C stand-in: producer holds data until ready (no loss)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Every word arrives, in order, even though the consumer takes only one word every three cycles; the producer is simply stalled (its valid stays high while ready is low).

Sizing a FIFO

Depth depends on the burst length and the rate difference. If a producer sends B words in a burst while the consumer drains at a fraction r of the producer's rate, you need about B x (1 - r) words of buffering, plus margin for pipeline delays between the full flag and the producer reacting. FIFOs whose depth is a power of two make pointer wrap-around free. Use "almost full" flags when the producer reacts a few cycles late.

Test a FIFO with random reads and writes against a software queue, and always include the cases "write while full", "read while empty" and "read and write in the same cycle". Most FIFO bugs live in those three.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When does a ready/valid transfer happen?

    The data moves only when the producer offers it (valid) and the consumer accepts it (ready) at the same edge.
  2. 2. If valid is high and ready is low, the producer must

    Changing or withdrawing data before it is accepted would lose or corrupt it.
  3. 3. What happens in the VHDL FIFO if the read and write both occur in one cycle?

    One word enters and one leaves, so the number of stored words stays the same.
  4. 4. Why do FIFO pointers wrap around?

    After reaching the last address, a pointer returns to 0, reusing freed locations.

Summary

  • Ready/valid: a transfer happens when both are high on a clock edge; the producer holds data until accepted.
  • A FIFO is a circular buffer with write and read pointers and a count (or flags) for empty/full.
  • Guard writes when full and reads when empty; simultaneous read and write keeps the count.
  • ready = not full and valid = not empty connect a FIFO to handshake ports.
  • Size the FIFO from burst length and rate difference; use Gray pointers for two clocks.

Next: Project: A Traffic Light Controller