 In this video we are going to study one of the concepts that is behind most of the sequential digital circuits out there, and that is the concept of finite state machines. Now in order to think about this concept, let's imagine an example. Let's think of a traffic light. Traffic light is initially at a stage in which the light is red. After a certain amount of time, this light makes a transition into a different state in which the color is green. You know this light is going to remain green for a while and then eventually we'll make another transition to another state in which it is yellow and eventually as well make another transition to red. So this is the typical sequence of steps we find in a traffic light. Now if we think a little bit more in detail, what makes a transition between these two states? Well typically it's a timer. It's some sort of signal that says that certain amount of time has gone by. With this signal we can think of it, this timer is actually an input to this machine. So this diagram can be rewritten in such a way that the transition from red to green only happens, let's call this input t, this transition only happens when t is equal to 1. In other words when the timer notifies us that certain amount of time has gone by. However, if the traffic light is at its red state and this timer has not reached its value, we can represent that transition saying that for t equals 0. In other words, while the timer returns a value 0, the light remains at the state red and the same type of transitions can be done in the remaining states. When the light is green and the timer hasn't turned into 1, the light remains at green. But when the timer becomes 1 again, the light makes a transition between green or from green to yellow. And similarly we have the same situation here for t equals 0 and the transition from t equals 1. Well this diagram we have here is what we call a finite state machine. It is a machine that makes transition from state to state based on basically two conditions, the state in which we are currently and the value of certain inputs. Now in terms of Boolean logic, we can represent this transition as a truth table. We can write what are the conditions such that for any given value of t and the current state, we calculate the next state of the machine. In this case, we have six combinations, t is 0 and we are at a red state, t is 1 and we are at a red state, t is 0 and we are in green, 1 and we are in green and 0 and we are in yellow and 1 and we are in yellow. So these are the six possible combinations we have between the states which are red, green and yellow and the possibility of two values in the input. Now the next state, we can write it for each of the combinations. If t is equal to 0 and I am at a red state, this corresponds precisely to this transition which means that the next state, the light is going to remain red. However, when I am in the red state and the time is equal to 1, the transition is done to the green state. And similarly we can write the following transitions. If t is equal to 0 and we are in the green state, we remain in the green state and if it's 1, we make a transition to the yellow state. If we are in the yellow state, the t is equal to 0, we remain in the yellow state and whenever t is equal to 1 and we are in the yellow state, we make a transition to the red state. Okay, now suppose that I actually want to make sure that this machine controls a real traffic light. But this traffic light is a circuit that has two beats such that beat 0 and beat 1 encode the light that is going to be on. And the encoding that I have is that whenever I want the light to be red, there should be a 00 in this input. Whenever I want the green line to be on, it should be 01 and whenever I want the yellow light to be on, these two beats should have a 1-0 value. So immediately we can see how to this truth table, which represents a transition between states, I can now add the value of an output that is going to encode the proper beats such that this traffic light is controlled. In this case, red is 00, green is 01, 01, 1-0, 1-0 and 0-0 again. Now as we can see now, if we apply the same encoding for here, and we put a 00, 00, 01, 01, 1-0 and 1-0, what we have here is basically a truth table. And it's a truth table that at the same time is telling us how to make a transition from one state to the next. And this is the definition of a finite state machine. Now let's consider a second example. Let's suppose that my system is a little bit different. I have a sensor that is telling me if the traffic has stopped or the traffic is going. So I have these two states here, stop and go. I do have the same input as before. I have a timer that tells me, let's call it T, that tells me if I make a transition from this state to this state. And I can also remain here or remain here. But now let's think about this difference. Suppose I have another input that tells me if trucks are waiting. In other words, if the trucks are stopped here, then this signal, let's call it TR for trucks, this signal will be 1. Same thing happens when the traffic is circulating, but I still sense trucks going by, this signal will still be 1. Now what happens here is the following. I would like to produce an output from this machine that warns me when there is the possibility of pollution. Now the possibility of pollution occurs when I am in the stop state and the trucks are waiting. In other words, when we have all traffic that has stopped and the truck sensor detects trucks that have been stopped here, then the output of my machine should be 1. However, when the traffic is circulating, when there is no traffic stop, then I should ignore this trucks waiting signal and the pollution should be 0. Now let's work out the details here. The easy ones to make the transition are from this state to this state and from this state to this state. So if I am at the stop state, so these are the two inputs. If I am at the stop state and the timer tells me that it's time to make a transition, I don't care what is the value of trucks waiting, I'll just make another transition. Same thing happens here. If the timer is 1, I don't care what is the value of the other input, I make this transition here. Something similar occurs here. If traffic is flowing and my timer tells me it's not time yet, I don't care what is the value of the other signal, this is what I'm representing by an X, and I make exactly this transition. Now let's think carefully about this transition over here. Two things can happen producing this transition, which is the value 0, 0, and the value 0, 1. Now we agree that the time hasn't come yet to make a transition, therefore the traffic is waiting, but now looking at this second signal over here, which corresponds to the truck waiting, I have two possible situations. The one above is telling me that the time has not arrived yet, but I don't have any trucks waiting, therefore the pollution should be 0. However, in this situation the pollution signal should be 1. The way we are going to represent this functionality here is by attaching to each one of these inputs, or each one of these transitions, we are going to attach the value of the output. Now when is pollution detected? Definitely not when the traffic is going, not when I make this transition, not when I open the lights so that traffic can circulate, but precisely on this condition. When the traffic cannot circulate yet, I detect trucks waiting, pollution is 1. In this case over here, even though we remain on the same state, in this case the pollution signal is 0, because even though the time hasn't arrived yet to make a transition and let the cars go, I'm not detecting any trucks, therefore the pollution is equal to 0. If you look at it carefully, there is a very significant difference between these two machines, because now the output that I'm interested in, it is not attached to the state as it was here, because the state was controlling the traffic light, but here the output depends not only on the state, but also on the value of the input. Now, analogously to how we did it here, we can write a truth table in which we write all possible eight cases, which is what is the value of T, what is the value of trucks, and what is the value of a state, and then we write eight cases, 0, 0 stop, 0, 0 go, 0, 1 stop, 0, 1 go, 1, 0 stop, 1, 0 go, 1, 1 stop, and 1, 1 go, and what we can do is calculate the next state. Let's write the state plus, and we see that from a 0, 0, if you are in the stop, we make a transition to the same stop, 0, 0 go, we remain in go, 0, 1 stop, this is a very important one, 0, 1 stop corresponds to this transition, which means we're still in the state stop, 0, 1 go means that we are in this state, and we have the value 0, 1, so we remain in go, 1, 0 stop, this is the case in which we have one transition, 1, time has come up, 0, and therefore make a transition to go, 1, 0 go is this condition over here, we make a transition to stop, 1, 1 stop is a transition to go, and 1, 1 go is a transition to stop, so with this column over here we have captured the transition of this system, but as you can see what we haven't captured yet is the behavior of this pollution signal that I'm interested in, and in order to do that I need to write an extra column here in which, depending on the value of the states and the inputs, then I have to write my output, and in this case it's very easy because the pollution signal is 1 here, as we said before, and 0 in the rest. If we apply an encoding and we say that a stop is encoded as 0, and go is encoded at 1, and we apply this translation to this table, we end up again with a truth table. Now again, what is the difference between these two finite state machines? The mechanism is more or less the same except the tiny difference is that in here the outputs that are controlling certain system out there are directly derived from the states, remember, red, green, and yellow was the only thing we needed, whereas in here the output not only depends on the state, but also depends on the input. These two type of machines have different names, this type over here is what is called as a Moore finite state machine, and the type down here is called Mealy finite state machine. And as you can see, for some cases a Moore machine offers you a much better implementation or a much more intuitive implementation of a system whereas a Mealy machine offers you a more intuitive implementation for some other scenarios. In both cases the truth table over here can be translated into a set of flip-flops and gates that end up implementing the finite state machine.