The last chip that we have to build in this week is called counter and before we set out to describe the counter, I'd like to say a few words about the context in which counters come to play. So, let us assume that they have a domestic robot in my home and I want to cause this robot to bake some brownies for me. So here's what I'm going to do. I'm going to write a very specific program consisting of something like 50 instructions that tell the robot exactly how to bake these brownies. And I'm going to mount this recipe on the kitchen wall and next to the recipe, I'm going to put a counter that emits a number that tells the robot which instruction the robot has to execute next. So it will begin with the number zero because my instructions are numbered from zero to 49, and the contract is going to be as follows, when the robot carries out set of instructions. At the end of this cycle, if you will, the counter increments by one. And then the robot takes a look at the counter and the robot sees that it has to execute instruction number one. It executes instruction number one. Counter becomes two, executes instruction number two, and so on. Now, in addition to this basic increment, I also want to be in full control of the situation, so I also want to be able to come in and change the counter to a specific number, like 17. Irrespective of what the counter shows now, three, four, five, whatever it is, I want to Come in and put the number 17 instead. And then have the counter continue to count from 17 onward. Now this is very important because suppose that the robot finished baking the brownies and I want to now bake another set of brownies. Well, in order to do this I can go to the counter and put the number zero in it and this will cause The robot to start from the beginning. Now, another reason why we want to set the content to some other values is because you know when the robot starts to bake the second batch of brownies, the oven is already warm. So we have to skip the whole set of instructions that tell the robot how to turn on the oven. So the first instruction in the program will probably be something like, if the oven is working go to instruction 11 or something else. Skip all the instructions that deal with turning on the oven. That's another reason why we want to be able to set the counter to a particular value. So we see that basically we have to support three generic or primitive operations which are as follows. Fetch the first instruction, in order to do this we want to set The program counter to zero. The other operation which is kind of the default operation is fetch the next instruction, and in order to do this, we have to be able to increment the current state of the counter by one. And finally, we want to be able to go to a particular instruction, fetch and execute it, and in order to do this, we have to be able to set the program counter to some desired value. So what is a counter? The counter is a chip, a hardware device, that somehow realizes this abstraction, that somehow supports these three primitive operations. So let's talk about this abstraction in more detail. And I'm going to use the same chip diagrams that we used earlier in other units in this course. So we have a black box description of a program counter. We see that we have 16-bit input coming in, containing a certain 16-bit value. We have a 16-bit output going out. And we also have three control bits which are called load, inc and reset. Inc stands for increment. Now, how does this thing is supposed to work? Well, here's a more formal description of the desired operation of this 16-bit counter. If the reset bit equals one, if the reset bit is asserted, well in that case, I want the counter to emit zero. So, in the next cycle, the counter will emit the number zero. If the load bit is asserted, well in that case I want to set the counter to a particular value. So if I want the counter to go to number 17, well, I put the number 17 or in binary. I put the 16-bit value that represents 17 in the input, and I assert the load bit. This will bypass the regular increment of the counter and it will set the counter to the number 17 in the next cycle. And if inc equals one, if the inc bit is asserted, well, in that case, the output of the counter will be the current state of the counter, plus one. This is a default operation of the counter. And finally, if none of these control bits is asserted, the counter does nothing, it emits its current state. So, this is the desired functionality of the counter and your job is to write the necessary HDL statements that will actually realize this desired functionality. Now, if you don't see how to do it, don't worry about it because we're going to have a complete unit dedicated to project three. And in this unit, we're going to talk about various tips and guidelines On how to build every chip including this counter chip. Now, before we go on to talk about this unit, I'd like to give you an example of how the counter chip actually operates using our hardware simulator. So this is what we're going to do next. To get started we load the program counter, built-in chip. So we go to the tools folder and we look for built-in chips. And inside the built in chips, we search the program counter .hdl file. Here it is. This is a built-in chip and we see that it has a GUI side effect that shows the contents of the program counter, which is essentially a register with some control bits. And indeed, we see that it has a 16-bit input, a 16-bit output and three control bits named load, inc, and reset. So, let us load some value into the program counter. So, we want to put in the number 23. We have to run the clock in order to commit this value. But we see that actually nothing happens. We are running the clock, nothing seems to happen. And nothing seems to happen because we forgot to assert the load bit. So, let us assert the load bit and run the clock. And indeed, we see that the program counter now contains 23, but it doesn't really count everything, does it? It seems to be fixed on 23. Well, it's fixed because we forgot to assert the inc load width. So let us do that. We assert the inc load width, and still nothing seems to happen. Well, nothing seems to happen because in every cycle we do two things. We tell the register, the program counter to increment but we also load it with 23 in every cycle, so it stays at 23. So if we want the program counter to finally count anything. We have to turn off the load bit, run the clock, and finally it looks like we are cooking with gas. The program counter advances in every cycle by one. And this is the classical operation of a program counter. In fact, we can click the fast forward icon. And this will do sort of an infinite loop that runs the clock forward. And we see that indeed, in every cycle, the program counter advances by one. Very nice, let's stop the clock and let us reset the program counter. So, we set the reset bit one and we hope the program counter will turn to zero. And let's run the clock, and indeed we see that in the next cycle, the program counter is zero. Well, it doesn't count again, because reset is still one. So we have to turn off the reset bit and run the clock, and we see that now indeed, the program counter progresses nicely in every cycle. So, this has been a demo of the program counter. And let's go back to the lecture. So, this is the end of the unit that describes how to build a counter. And actually, this is the end of the description of all the chips that we're going to build this week. And in the next unit, we're going to give you all sorts of tips and guidelines on how to build these chips and how to submit project three.