In the previous tutorial we learned to create an infinite loop by using the loop statement. We also learned how the break out of a loop by using the exit statement. But what if we want the loop to iterate a certain number of times? The For-Loop is the easiest way to accomplish this.

The For-Loop allows you to iterate over a fixed range of integers or enumerated items. The item belonging to the current iteration will be available within the loop through an implicitly declared constant.

This blog post is part of the Basic VHDL Tutorials series.

The syntax of the For-Loop is:

for <c> in <r> loop
end loop;

The <c> is an arbitrary name for a constant that will be available inside of the loop. The <r> is a range of integers or enumerated values which the loop will iterate over. An integer range can be either incrementing or decrementing.

The VHDL code for an incrementing range including all 10 numbers from 0 to 9:

0 to 9

The VHDL code for a decrementing range including all 10 numbers from 9 to 0:

9 downto 0

The VHDL code for a range including only the number 0:

0 to 0

The VHDL code for an empty range that doesn’t have any numbers at all:

0 to -1

Exercise

The final code we created in this tutorial:

entity T04_ForLoopTb is
end entity;
 
architecture sim of T04_ForLoopTb is
begin
 
    process is
    begin
 
        for i in 1 to 10 loop
            report "i=" & integer'image(i);
        end loop;
        wait;
         
    end process;
 
end architecture;

The output to the simulator console when we pressed the run button in ModelSim:

VSIM 2> run
# ** Note: i=1
#    Time: 0 ns  Iteration: 0  Instance: /t04_forlooptb
# ** Note: i=2
#    Time: 0 ns  Iteration: 0  Instance: /t04_forlooptb
# ** Note: i=3
#    Time: 0 ns  Iteration: 0  Instance: /t04_forlooptb
# ** Note: i=4
#    Time: 0 ns  Iteration: 0  Instance: /t04_forlooptb
# ** Note: i=5
#    Time: 0 ns  Iteration: 0  Instance: /t04_forlooptb
# ** Note: i=6
#    Time: 0 ns  Iteration: 0  Instance: /t04_forlooptb
# ** Note: i=7
#    Time: 0 ns  Iteration: 0  Instance: /t04_forlooptb
# ** Note: i=8
#    Time: 0 ns  Iteration: 0  Instance: /t04_forlooptb
# ** Note: i=9
#    Time: 0 ns  Iteration: 0  Instance: /t04_forlooptb
# ** Note: i=10
#    Time: 0 ns  Iteration: 0  Instance: /t04_forlooptb

Need the Questa/ModelSim project files?

Let me send you a Zip with everything you need to get started in 30 seconds

How does it work?

Tested on Windows and Linux Loading Gif.. How it works

Analysis

Not totally unexpectedly, our For-Loop iterated ten times before terminating. The value of i is printed to the simulator console ten times at simulation time 0. There’s no wait statement inside of the loop, and therefore the loop takes zero time to complete. Finally, the program goes into an infinite pause on the wait;.

We learned how to convert an integer to a string by using integer'image(), and we used the & character to join the two strings together.

Takeaway

  • The For-Loop can iterate over an incrementing or decrementing integer range
  • An incrementing range is denoted by to, and a decrementing range by downto
  • An integer can be converted to a string by using integer'image()
  • Two strings can be joined by using the string concatenation character &

Go to the next tutorial »

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *