So welcome to unit 12 in our operating system module in which we are going to develop the sys class. The last class in our operating system, yeah So let's see what it takes to implement the Sys class. Well, the Sys class consists of, I think, four methods, of which the most interesting one is a private method that users don't get to see. And this is our init function. Now I wish to remind you in we don't have public and private modifiers for our class members, but we can write functions and simply not tell the world about them. And in it is one of these functions, which is absolutely necessary for the operating system, but this function is used strictly for OS services, as I will just set out to describe. Before I do so, I want to take a diversion and say a few words about bootstrapping, sometimes called booting. Well I wasn't sure exactly how to define bootstrapping so I went to Wikipedia. And Wikipedia said that bootstrapping is the process of loading the basic software into the memory of a computer after power-on or general reset, especially the operating systems which will then take care of loading other software as needed. That's what Wikipedia says, so it must be true. And, in fact it is true, because think about it. When you turn on your laptop, what happens? Well, there is some software which is burned into your computer. And this little software goes to the disc, loads the operating system kernel, the most important part of the OS. The OS starts running and sets itself to do all sorts of things, and then it loads another program that shows the windows on the screen. And then you can select some word processor, write something, or email or whatnot. So this description is actually quite apt. Now what do we have to do in order to make this magic happen? And indeed, it is a magic. You take the piece of electronics and push a button and then, boom, it starts to play Tetris or Sound of Music or watch a movie or what not. It is absolutely magical. How do you make this magic happen? Well, in the Jack Hack platform, we do it using four different contracts, that all of them have to be fulfilled at various levels of the computer. So first of all, the hardware contract is that when the computer is reset, execution always starts with the instruction, which happens to be located in ROM[0], okay? This is equivalent to this software which is burnt into the ROM of your computer. All right, now the VM contract is such that the VM translator, when it translates the program that you want to run, always puts at the top of the ROM, at ROM[0], it puts the following two instructions. Set the stack pointer to 256, which is a hack VM convention, and call Sys.init. These commands must be written in machine language, of course not in VM manager, so this is just sell your code. All right, the next contract is coming from Jack, and if you recall when you write the program like Tetris or whatever or an accounting software or whatnot, there must be one class called main and within this class, there must be one method called main. And this is the entry point to your application. And you find such conventions in every programming language, and Jack is no exception. So this is something that the Jack programmer is responsible for providing. If you take all these things that is written here into account, you will see that in order to complete the bootstrapping picture, what remains to be done is to cause Sys.init. Well, first of all, you have to have a sys.init because the ROM is going to call this method, so sys.init should be a function which is available in the operating system somewhere, well most naturally in the Sys class. This init function should do two things. First of all, it should initialize the Operating System so the the OS would be ready to perform all it's super important operations when the application programs will start using its services. And then Sys.init calls Main.main(). And by doing so, Sys.init achieves two objectives. First of all, it sets up the OS, and second of all, it fires up the application that the user wanted to run. So if you take all these contracts together you get how we implement the bootstrapping logic on our computer, and it's kind of intriguing that these contracts have to be fulfilled by very different agents if you will. The builder of the hardware has to do something, the person or the team that implements the VM translator must obey this contract, the Jack program must obey the contract, the team that builds the operating system, but if each one of these teams that may well have never met each other plays according to the rules of the game, then everything will work smoothly like a great symphonic orchestra. All right, so let us open up the Sys class. And the first thing that I want to discuss is indeed this init function. And as we just discussed, the init function should, first of all, go through all the OS classes that have an init function and call them one by one. So this will cause everyone of these classes to get initialized. So Math is going to create whatever it needs in order to do mathematical computations. Memory will create the arrays that it needs in order to access the RAM and so on and so forth. So, for every class that has an init function, we have to call it. Some OS classes don't have an init function, so we don't have to call them. Array, I think, doesn't have one. So after we call all the init functions of the OS classes that have init functions, then we simply call Main.main() and that's it. And then from this point on, everything that happens is at the mercy of the programmer that developed the high level application. So that's the init function, which is by far the most interesting function in this Sys class. Then we have a few more utilities, which are necessary but not terribly exciting. We have a halt utility that is designed to stop the computer and whatever it is currently doing. And I shouldn't stay stop, but it's designed to create the illusion that the computer has stopped. Because one way to implement this whole thing is simply to put the computer into an infinite loop. So let's hope you can do. If you want to, it's not probably the most elegant way to do it, but it works. Then we have a wait function which is necessary when we implement also sort of the interactive programs and we want to control the length for various operations and so on with user interaction. So the wait function, according to its API, receives a certain parameter, which is the duration in milliseconds. And it waits for that duration. So if you provided the number three, it will wait for three milliseconds. Now how should you implement it? Well, once again I recommend that you use a loop. And the loop should have some delay factor in it, some constant that playing with this constant will cause you, will help you make sure that the run time of this loop will be such that it conforms to the required duration. So if your computer is very fast, then the delay factor will have to be large, because you want to slow it down. If your computer is slow, you will want a small delay factor to make this loop operate fast enough. So you have to get yourself a watch or you can use your cellphone for this and write this loop and then time it. Play with different values of this delay factor, until your loop conforms to what you want to do. So it's very difficult to time one millisecond, because it's a thousandth of a second. So you can time how long this loop runs, let's say for 5,000 milliseconds, which is five seconds, and then if the loop ended in three seconds then you have to do something to delay factor to play with it. So you'll get a wait implementation, which is platform dependent. It depends on the strength of your processor of your own computer and that's okay. Operating Systems have some platform-specific parameters and this is one of them. Finally we have the housekeeping utility called error, which simply receives some number of an error code and prints it nicely on the screen and this is so trivial that I don't want to discuss it. And that's basically our Sys class, which is also the last class in our operating system. And I think that it has some poetic beauty in it, that this class, which is last in our OS in terms of the order of development, is also the class that implements the beginning of everything because that's what init is designed to do. And so this reminds me of a quote by, I think, T.S. Eliot, who said that we will not cease from explorations, and when we come back, we will know the place for the first time. I think that this quote is very relevant to this course because you guys were willing to go through all these explorations, and go deep and wide inside the computer in general, and always in particular. When you come back from these explorations, you all of a sudden understand these things like allocating space in memory, drawing circles, converting strings to numbers, and vice versa. All of a sudden these things are no longer a mystery. And they are no longer a mystery because you guys had the character to sit through all these lectures and not lose your consciousness in the middle I hope. And you also have the character to build this thing, which is what we'll do in the next unit.