Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> VHDL

Mastering While Loops in VHDL: Dynamic Iteration Control

Building on our earlier exploration of for loops, this tutorial dives into the while loop—a powerful construct that lets you control iteration based on dynamic conditions rather than a predetermined range.

The while loop executes its body repeatedly as long as the specified condition remains true. This makes it ideal for scenarios where the number of iterations depends on runtime data or complex logic.

This post is part of the Basic VHDL Tutorials series.

Syntax

while <condition> loop
    -- loop body
end loop;

The <condition> must evaluate to a boolean value. It can be a simple comparison or a more elaborate expression. The condition is checked before each pass, ensuring the loop only continues when it remains true.

Condition Examples

Relational Operators

=equal
/=not equal
<less than
<=less than or equal
>greater than
>=greater than or equal

Logical Operators

not atrue if a is false
a and btrue if a and b are true
a or btrue if a or b are true
a nand btrue if a or b is false
a nor btrue if a and b are false
a xor btrue if exactly one of a or b are true
a xnor btrue if a and b are equal

Exercise

Watch the video tutorial below to see how a variable can drive a while loop in practice:

Sample Code

entity T05_WhileLoopTb is
end entity;

architecture sim of T05_WhileLoopTb is
begin

    process is
        variable i : integer := 0;
    begin

        while i < 10 loop
            report "i=" & integer'image(i);
            i := i + 2;
        end loop;
        wait;

    end process;

end architecture;

Simulator Output

VSIM 2> run
# ** Note: i=0
#    Time: 0 ns  Iteration: 0  Instance: /t05_whilelooptb
# ** Note: i=2
#    Time: 0 ns  Iteration: 0  Instance: /t05_whilelooptb
# ** Note: i=4
#    Time: 0 ns  Iteration: 0  Instance: /t05_whilelooptb
# ** Note: i=6
#    Time: 0 ns  Iteration: 0  Instance: /t05_whilelooptb
# ** Note: i=8
#    Time: 0 ns  Iteration: 0  Instance: /t05_whilelooptb

Analysis

We initialize i to 0 and use i < 10 as the loop guard. Because the loop increments i by 2 each cycle, the last value printed is 8. On the next pass, the condition evaluates to false (10 is not less than 10), and the loop exits. The subsequent wait; suspends the process indefinitely, a common pattern for testbenches.

Mastering While Loops in VHDL: Dynamic Iteration Control

Key Takeaways

Take the Basic VHDL Quiz – part 1 or continue to the next tutorial.

VHDL

  1. Leveraging In‑Process Procedures for Cleaner VHDL FSM Design
  2. Using Impure Functions in VHDL: Enhancing FSM Readability and Maintainability
  3. Mastering VHDL Functions: A Practical Guide to Efficient Design
  4. Using Procedures in VHDL: Simplify Your Design with Reusable Code
  5. Mastering VHDL Port Map Instantiation: A Practical Guide
  6. Mastering the Case-When Statement in VHDL: Efficient Multiplexer Design
  7. Mastering Signed and Unsigned Types in VHDL: A Practical Guide
  8. Mastering VHDL Wait Statements: Wait On, Wait Until, and Combined Usage
  9. Mastering For‑Loops in VHDL: A Practical Guide
  10. Mastering Loop and Exit Constructs in VHDL: A Practical Guide