Bird
Raised Fist0
VHDLprogramming~10 mins

FIFO full and empty flags in VHDL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - FIFO full and empty flags
Start
↓
Check write pointer == read pointer?
↓
Is FIFO empty?
↓
Set empty=1
↓
Allow write?
↓
Update pointers
↓
Repeat checks
The FIFO uses write and read pointers to track data. When pointers are equal, FIFO can be empty or full, distinguished by additional logic. Flags full and empty are set accordingly.
Execution Sample
VHDL
process(clk)
begin
  if rising_edge(clk) then
    if write_enable and not full then
      fifo_mem(write_ptr) <= data_in;
      write_ptr <= (write_ptr + 1) mod FIFO_SIZE;
    end if;
    if read_enable and not empty then
      data_out <= fifo_mem(read_ptr);
      read_ptr <= (read_ptr + 1) mod FIFO_SIZE;
    end if;
    full <= ((write_ptr + 1) mod FIFO_SIZE) = read_ptr;
    empty <= write_ptr = read_ptr;
  end if;
end process;
This code updates write and read pointers on clock edges, writes and reads data, and sets full and empty flags based on pointer positions.
Execution Table
Stepwrite_ptrread_ptrwrite_enableread_enablefull flagempty flagAction
1001001Write enabled, FIFO empty, write data, increment write_ptr to 1
2101000Write enabled, FIFO not full, write data, increment write_ptr to 2
3200100Read enabled, FIFO not empty, read data, increment read_ptr to 1
4211100Write and read enabled, write_ptr to 3, read_ptr to 2
5321010Write enabled, write_ptr +1 mod size == read_ptr, full flag set
6320110Read enabled, read_ptr increments to 3, full flag cleared
7330001No write/read, pointers equal, empty flag set
8331001Write enabled, FIFO empty, write_ptr increments to 4, empty cleared
💡 Simulation ends after step 8 showing full and empty flags update correctly with pointer changes.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8
write_ptr012233334
read_ptr000122333
full000001100
empty100000010
Key Moments - 3 Insights
Why can the FIFO be empty and full when write_ptr equals read_ptr?
When write_ptr equals read_ptr, the FIFO can be either empty or full. The full flag is set when incrementing write_ptr would equal read_ptr (step 5), while empty is set when pointers are equal without that condition (step 7). This distinction avoids confusion.
Why do we use modulo operation on pointers?
The modulo operation wraps the pointers around the FIFO size, making the FIFO circular. This allows continuous writing and reading without overflow, as seen in pointer increments at each step.
What happens if write_enable is high but FIFO is full?
If write_enable is high but full flag is set (step 5 and 6), the write operation is blocked to prevent overwriting unread data, ensuring data integrity.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 5, what is the value of the full flag?
A1
B0
CUndefined
DDepends on read_enable
💡 Hint
Check the 'full flag' column in row for step 5 in the execution_table.
At which step does the empty flag become 1 again after being cleared?
AStep 6
BStep 7
CStep 4
DStep 8
💡 Hint
Look at the 'empty flag' column in execution_table rows; it becomes 1 at step 7.
If write_enable is 1 but full flag is 1, what happens to write_ptr?
AIt increments by 1
BIt resets to 0
CIt stays the same
DIt decrements by 1
💡 Hint
Refer to steps 5 and 6 where full=1 and write_enable=1 but write_ptr does not increment.
Concept Snapshot
FIFO full and empty flags use write and read pointers.
full flag is set when next write_ptr equals read_ptr.
empty flag is set when write_ptr equals read_ptr.
Pointers wrap around using modulo FIFO size.
Write blocked if full; read blocked if empty.
This prevents data overwrite or underflow.
Full Transcript
This visual execution shows how FIFO full and empty flags are set using write and read pointers. When pointers are equal, FIFO can be empty or full, distinguished by checking if incrementing write pointer equals read pointer. The full flag prevents writing when FIFO is full, and empty flag prevents reading when FIFO is empty. Pointers wrap around using modulo operation to create a circular buffer. The execution table traces pointer values, flags, and actions step-by-step, clarifying common confusions about pointer equality and flag settings.

Practice

(1/5)
1. What does the FIFO full flag indicate in a VHDL design?
easy
A. The buffer is partially filled
B. The buffer is empty and has no data to read
C. The buffer is being reset
D. The buffer is full and cannot accept more data

Solution

  1. Step 1: Understand FIFO full flag meaning

    The FIFO full flag signals when the buffer has reached its maximum capacity.
  2. Step 2: Interpret the flag's implication

    When full, no more data can be written safely to the FIFO.
  3. Final Answer:

    The buffer is full and cannot accept more data -> Option D
  4. Quick Check:

    FIFO full flag = buffer full [OK]
Hint: Full flag means no more writes allowed [OK]
Common Mistakes:
  • Confusing full flag with empty flag
  • Thinking full means partially filled
  • Assuming full flag resets the buffer
2. Which VHDL signal type is most appropriate for FIFO full and empty flags?
easy
A. std_logic
B. integer
C. real
D. string

Solution

  1. Step 1: Identify flag signal requirements

    Flags represent binary states: high or low (1 or 0).
  2. Step 2: Choose suitable VHDL type

    std_logic is used for single-bit signals representing logic levels.
  3. Final Answer:

    std_logic -> Option A
  4. Quick Check:

    Flags use std_logic type [OK]
Hint: Flags are single-bit signals, use std_logic [OK]
Common Mistakes:
  • Using integer or real for binary flags
  • Using string type for flags
  • Confusing std_logic_vector with std_logic
3. Given this VHDL snippet for a FIFO full flag:
full_flag <= '1' when (write_ptr + 1) mod depth = read_ptr else '0';

What does this expression check?
medium
A. If the write pointer equals the read pointer, indicating empty
B. If the next write position equals the read position, indicating full
C. If the FIFO is partially full
D. If the FIFO is being reset

Solution

  1. Step 1: Analyze the condition

    The expression checks if incrementing write_ptr wraps to read_ptr.
  2. Step 2: Understand FIFO full logic

    When next write position equals read pointer, FIFO is full to avoid overwrite.
  3. Final Answer:

    If the next write position equals the read position, indicating full -> Option B
  4. Quick Check:

    Next write = read pointer means full [OK]
Hint: Full when next write equals read pointer [OK]
Common Mistakes:
  • Confusing full condition with empty condition
  • Ignoring modulo wrap-around
  • Assuming write_ptr = read_ptr means full
4. Identify the error in this FIFO empty flag assignment:
empty_flag <= '1' when write_ptr = read_ptr + 1 else '0';
medium
A. The write_ptr and read_ptr should be swapped
B. The flag should be std_logic_vector, not std_logic
C. The condition should be write_ptr = read_ptr for empty
D. The else part should assign '1' instead of '0'

Solution

  1. Step 1: Recall FIFO empty condition

    FIFO is empty when write_ptr equals read_ptr, no data to read.
  2. Step 2: Compare given condition

    The condition uses write_ptr = read_ptr + 1, which is incorrect for empty.
  3. Final Answer:

    The condition should be write_ptr = read_ptr for empty -> Option C
  4. Quick Check:

    Empty flag triggers when pointers are equal [OK]
Hint: Empty when write_ptr equals read_ptr [OK]
Common Mistakes:
  • Using offset in empty condition
  • Confusing full and empty pointer conditions
  • Incorrect flag signal type
5. You have a FIFO with depth 8, write_ptr = 7, read_ptr = 6.
Using this full flag logic:
full_flag <= '1' when (write_ptr + 1) mod 8 = read_ptr else '0';

What is the value of full_flag?
hard
A. '0', FIFO is not full
B. '1', FIFO is full
C. Undefined, pointers out of range
D. '0', FIFO is empty

Solution

  1. Step 1: Calculate (write_ptr + 1) mod depth

    (7 + 1) mod 8 = 8 mod 8 = 0.
  2. Step 2: Compare with read_ptr

    read_ptr = 6, so 0 != 6, condition false, full_flag = '0'.
  3. Final Answer:

    '0', FIFO is not full -> Option A
  4. Quick Check:

    Next write pointer != read pointer means not full [OK]
Hint: Calculate (write_ptr+1) mod depth carefully [OK]
Common Mistakes:
  • Forgetting modulo wrap-around
  • Assuming full when pointers differ
  • Mixing up read and write pointers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard