﻿1
00:00:12,740 --> 00:00:18,830
Welcome to the for loop Wildblue lecture in this lecture I'm going to introduce and explain how the

2
00:00:18,830 --> 00:00:23,170
for loop and while loop can be used in VHDL.

3
00:00:23,180 --> 00:00:29,240
First let's talk about the for loop for loop is commonly used to generate multiple instances of the

4
00:00:29,240 --> 00:00:33,100
same logic in VHDL for loops.

5
00:00:33,110 --> 00:00:35,090
They go away after synthesis.

6
00:00:35,090 --> 00:00:41,150
So we commonly use these to construct circuits for you know if we want to generate something over and

7
00:00:41,150 --> 00:00:43,260
over a for loop is a great way to do this.

8
00:00:43,280 --> 00:00:45,580
But after synthesis the four loops go away.

9
00:00:45,590 --> 00:00:52,280
They're just used to help make our code a lot cleaner is a lot less lines and a little more easy to

10
00:00:52,280 --> 00:00:53,190
understand.

11
00:00:53,360 --> 00:01:01,480
The key words in a for loop bar you have four in two loop and end loop.

12
00:01:01,680 --> 00:01:10,080
So an example of a for loop would be we have two signals who have signaled a b and c and their standard

13
00:01:10,080 --> 00:01:15,520
logic vectors from four down to zero so we know that there are five bits wide.

14
00:01:15,900 --> 00:01:20,910
And now we have our four loop statement where we're going to use this to generate R and gates.

15
00:01:20,910 --> 00:01:26,560
So we have our four I mean zero 2 for loop.

16
00:01:26,580 --> 00:01:32,850
So what that's going to do is take this value by and it's going to iterate from 0 to 4.

17
00:01:32,850 --> 00:01:38,260
So we'll get five iterations of 0 1 2 3 and 4.

18
00:01:38,460 --> 00:01:43,640
And inside of that loop whatever statements we replace is going to be iterated five times.

19
00:01:43,650 --> 00:01:45,630
So we have the statement.

20
00:01:45,640 --> 00:01:48,750
See why is equal to.

21
00:01:48,930 --> 00:01:51,460
I ended with be-I.

22
00:01:51,600 --> 00:01:58,860
So this is going to generate 5 different and gates where we have the input signal A and B will be added

23
00:01:58,860 --> 00:02:02,000
together and create the signal C..

24
00:02:02,010 --> 00:02:09,270
So if you look we have the five different and gates where our age zero is added with a zero and the

25
00:02:09,270 --> 00:02:10,770
output is zero.

26
00:02:10,860 --> 00:02:17,730
And this goes on for a one be one all the way down to a four before we get our output see.

27
00:02:17,790 --> 00:02:20,250
And this really is a quick and easy way.

28
00:02:20,310 --> 00:02:32,310
Instead of writing C I yield to AI and B or C zeroes equals to a 1 or 0 0 9 C one is equal to a 1 and

29
00:02:32,310 --> 00:02:37,160
B 1 all the way down to a 4 or C 4 is equal to a 4.

30
00:02:37,170 --> 00:02:40,910
And before this is a lot cleaner and quicker way of doing this.

31
00:02:40,920 --> 00:02:43,530
Now here we only have five and gates.

32
00:02:43,710 --> 00:02:49,950
But if you have a lot more complex circuit where you're working with say 100 and Gates or however many

33
00:02:49,950 --> 00:02:52,720
you have this is a quick and easy way to do that.

34
00:02:52,800 --> 00:02:58,320
And you can also do more complex things where you can implement flip flops or component instantiations

35
00:02:58,320 --> 00:02:59,550
of different circuits.

36
00:02:59,670 --> 00:03:05,820
But this is the general idea after synthesis this for loop example the for the for loops going to go

37
00:03:05,820 --> 00:03:06,470
away.

38
00:03:06,510 --> 00:03:12,320
We're just going to generate for endgames Now we'll talk about a while.

39
00:03:12,410 --> 00:03:15,420
These are very similar in nature to a for loop.

40
00:03:15,440 --> 00:03:17,300
However there are several differences.

41
00:03:17,370 --> 00:03:22,970
And the big one being that the condition is checked before the loop is entered in a for loop.

42
00:03:22,970 --> 00:03:28,100
We specifically tell the loop How many times we're going to iterate like with our example we said we're

43
00:03:28,100 --> 00:03:29,610
going from zero to four.

44
00:03:29,690 --> 00:03:33,320
So we knew right off the bat we're going to be iterating five times.

45
00:03:33,320 --> 00:03:39,370
However in a while loop we have a condition and this condition is checked before we go into the loop.

46
00:03:39,440 --> 00:03:42,990
And then every time we iterate through that loop we check that condition.

47
00:03:43,100 --> 00:03:47,840
And if that condition evaluates to true then we get out of the loop.

48
00:03:48,020 --> 00:03:53,300
So gives it that loop continues running until the condition evaluates to false.

49
00:03:53,300 --> 00:03:57,710
So it's important to know that the condition eventually evaluates to false.

50
00:03:57,740 --> 00:04:04,820
So if we look at the syntax of how the while loop would take place we have the loop name while condition

51
00:04:05,180 --> 00:04:08,690
and the condition is whatever you set out to be.

52
00:04:08,690 --> 00:04:14,340
While that's true we're going to be executing the statements and then we end our loop.

53
00:04:14,480 --> 00:04:19,080
And so you could do the same exact thing you do in a while versus a for loop.

54
00:04:19,220 --> 00:04:24,230
However you just have to make sure at some point in time that your condition will evaluate to a false

55
00:04:26,220 --> 00:04:31,290
so for example we have a process of calling it P2.

56
00:04:31,440 --> 00:04:38,160
We have it evaluates on the Ellen underscores the variable the one we're saying that to evaluate one

57
00:04:38,670 --> 00:04:39,750
and then we begin.

58
00:04:39,840 --> 00:04:42,810
Now we have our while loop for a while.

59
00:04:42,810 --> 00:04:48,290
The one is equal to less than or equal to ninety nine.

60
00:04:48,300 --> 00:04:49,630
We want to loop through.

61
00:04:49,770 --> 00:04:57,510
So our out Z is being set to Alan underscore z z 1 plus 8.

62
00:04:57,600 --> 00:05:03,250
And really the important thing to note here that is the one is constantly being incremented the values

63
00:05:03,300 --> 00:05:06,350
being set to Z One plus one.

64
00:05:06,360 --> 00:05:13,800
So we're going to increment through this loop 100 times or 99 times because you will start with z one

65
00:05:13,800 --> 00:05:19,830
starts at 1 and we'll go through nine nine times until the while the one is less than or equal to ninety

66
00:05:19,830 --> 00:05:20,190
nine.

67
00:05:20,200 --> 00:05:21,720
We'll just keep looping through.

68
00:05:21,990 --> 00:05:27,470
And so once we hit the one hundredth time we'll get out of the loop and the process and get out.

69
00:05:27,540 --> 00:05:33,400
Now if we would take out the statement z one is equal to Z One plus one.

70
00:05:33,920 --> 00:05:39,680
We would create a condition we call an infinite loop if that while loop will never go to falls.

71
00:05:39,690 --> 00:05:41,720
Then you'll sit in that loop and spin forever.

72
00:05:41,850 --> 00:05:47,580
And this can be a problem either your synthesizer will catch this and spit on a warning or if it doesn't

73
00:05:47,940 --> 00:05:52,850
hear program or your code and be HGL will never get out of this process.

74
00:05:52,890 --> 00:05:54,500
And so that can cause an issue.

75
00:05:54,500 --> 00:05:57,310
So that's just something have to be careful with when you're working with a while loop.

76
00:05:57,330 --> 00:06:00,010
Just be very cautious of infinite loops.

77
00:06:01,340 --> 00:06:06,260
So the main difference if we can bear for a loop while loop for loop will iterate a specific amount

78
00:06:06,260 --> 00:06:13,100
of times where a while loop will iterate based upon the code you tell it until the condition is false.

79
00:06:13,340 --> 00:06:18,880
And when you're working with a while loop just be very very cautious of an infinite loop.

80
00:06:19,010 --> 00:06:21,780
Now that you have the knowledge of for loops and while loops.

81
00:06:21,800 --> 00:06:27,740
Next we're going to learn about when else statements and with select when statements.

