FIFO full and empty flags in VHDL - Time & Space Complexity
When working with FIFO buffers in VHDL, it's important to understand how the full and empty flags update as data moves in and out.
We want to know how the time to update these flags changes as the FIFO size grows.
Analyze the time complexity of the following VHDL process that updates full and empty flags.
process(clk)
begin
if rising_edge(clk) then
if write_enable and not full then
write_pointer <= (write_pointer + 1) mod FIFO_SIZE;
end if;
if read_enable and not empty then
read_pointer <= (read_pointer + 1) mod FIFO_SIZE;
end if;
full <= ((write_pointer + 1) mod FIFO_SIZE) = read_pointer;
empty <= write_pointer = read_pointer;
end if;
end process;
This code updates pointers and flags for a FIFO buffer on each clock cycle.
Look for operations that repeat as input size changes.
- Primary operation: Pointer increment and flag comparison happen every clock cycle.
- How many times: Exactly once per clock cycle, regardless of FIFO size.
The number of operations per clock cycle stays the same no matter how big the FIFO is.
| Input Size (FIFO_SIZE) | Approx. Operations per Cycle |
|---|---|
| 10 | 5 (increments and comparisons) |
| 100 | 5 (same operations) |
| 1000 | 5 (still the same) |
Pattern observation: The work does not increase with FIFO size; it stays constant.
Time Complexity: O(1)
This means the time to update full and empty flags does not grow as the FIFO gets bigger.
[X] Wrong: "The full and empty flags take longer to update as the FIFO size increases because more data is involved."
[OK] Correct: The flags update by simple pointer comparisons and increments, which happen in constant time regardless of FIFO size.
Understanding constant-time operations like flag updates in hardware design shows you can reason about efficient designs, a valuable skill in many technical roles.
What if the full flag was calculated by scanning the entire FIFO memory instead of pointer comparison? How would the time complexity change?
Practice
Solution
Step 1: Understand FIFO full flag meaning
The FIFO full flag signals when the buffer has reached its maximum capacity.Step 2: Interpret the flag's implication
When full, no more data can be written safely to the FIFO.Final Answer:
The buffer is full and cannot accept more data -> Option DQuick Check:
FIFO full flag = buffer full [OK]
- Confusing full flag with empty flag
- Thinking full means partially filled
- Assuming full flag resets the buffer
Solution
Step 1: Identify flag signal requirements
Flags represent binary states: high or low (1 or 0).Step 2: Choose suitable VHDL type
std_logic is used for single-bit signals representing logic levels.Final Answer:
std_logic -> Option AQuick Check:
Flags use std_logic type [OK]
- Using integer or real for binary flags
- Using string type for flags
- Confusing std_logic_vector with std_logic
full_flag <= '1' when (write_ptr + 1) mod depth = read_ptr else '0';
What does this expression check?
Solution
Step 1: Analyze the condition
The expression checks if incrementing write_ptr wraps to read_ptr.Step 2: Understand FIFO full logic
When next write position equals read pointer, FIFO is full to avoid overwrite.Final Answer:
If the next write position equals the read position, indicating full -> Option BQuick Check:
Next write = read pointer means full [OK]
- Confusing full condition with empty condition
- Ignoring modulo wrap-around
- Assuming write_ptr = read_ptr means full
empty_flag <= '1' when write_ptr = read_ptr + 1 else '0';
Solution
Step 1: Recall FIFO empty condition
FIFO is empty when write_ptr equals read_ptr, no data to read.Step 2: Compare given condition
The condition uses write_ptr = read_ptr + 1, which is incorrect for empty.Final Answer:
The condition should be write_ptr = read_ptr for empty -> Option CQuick Check:
Empty flag triggers when pointers are equal [OK]
- Using offset in empty condition
- Confusing full and empty pointer conditions
- Incorrect flag signal type
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?
Solution
Step 1: Calculate (write_ptr + 1) mod depth
(7 + 1) mod 8 = 8 mod 8 = 0.Step 2: Compare with read_ptr
read_ptr = 6, so 0 != 6, condition false, full_flag = '0'.Final Answer:
'0', FIFO is not full -> Option AQuick Check:
Next write pointer != read pointer means not full [OK]
- Forgetting modulo wrap-around
- Assuming full when pointers differ
- Mixing up read and write pointers
Ready to prove it?
Pass the test and this pattern moves unknown
