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
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.
Get free access to the Basic VHDL Course
Download the course material and get started.
You will receive a Zip with exercises for the 23 video lessons as VHDL files where you fill in the blanks, code answers, and a link to the course.
By submitting, you consent to receive marketing emails from VHDLwhiz (unsubscribe anytime).
Takeaway
- The For-Loop can iterate over an incrementing or decrementing integer range
- An incrementing range is denoted by
to
, and a decrementing range bydownto
- An integer can be converted to a string by using
integer'image()
- Two strings can be joined by using the string concatenation character
&