In this lecture, we will add a key buffer so that the other modules and user programs can read the keys
instead of just printing it on the screen. We have a working keyboard handler to receive the key characters
,
and all we need to do in this video is put these characters in the buffer.
OK, let's get started.
In the keyboard header file,
we define the buffer structure which we will use to manage the read and write operation of key buffer
.
The buffer is an array with 500 bytes. We also have front and end
which specifies the current front and end of the characters.
Let’s go back to the keyboard.c file.
If we read a character from keyboard and it’s a valid character, we will write it in the buffer.
In the function write key buffer,
we check to see if the buffer is full. This buffer is actually a circular queue.
So if the end is one element away reaching to the front, it means that the buffer is full and we simply return
,
Otherwise we write the data in the buffer, update the end and write it back to the key buffer for later use.
Here we % size, which means if the end is pointing to the last element, we will start from the beginning again
.
OK, let's move on.
After we done the writing, we will wake up the process waiting for the keyboard. In this system,
when a program wants to read a key and there is no key in the buffer, we will put it into sleep.
So here we will wake it up.
The wait argument we pass in this case is -2 which represents the processes waiting for the keyboard IO
.
Ok next we move to function read buffer.
In this function, we will check if the buffer is empty before we read a key. If it is empty,
we will put the process into sleep as we just talked about.
And if we have keys in the buffer, we will return the key to the caller and update the front to point to
the next location.
In this example, the user program will read a key and print it on the screen. In order to do that,
we need to add new system call functions just like we did before.
So what we are going to do is we are going to add keyboard read function in system call module.
As you see, we add sys keyboard read function. In this function,
we do nothing but call function read key buffer.
Don’t forget to change the checks since we add one more system call function.
Ok we finished the kernel part.
As for the user part, we test it with the user2 program.
As we did before, we first add system call in the library.
So in the syscall.asm file,
we define
keyboard readu.
The index number is 4.
It has no parameters, so we don’t allocate space on the stack and we clear rdi register.
Then int
80
After we return from kernel, we simply return.
And then we global
keyboard readu.
OK, now we can build the library.
Also, we need to add the declaration in the lib.h file.
The function returns a key character.
Alright, let’s open main.c file.
And we get rid of the code here.
Then we use while loop
to read a key constantly and print it on screen.
So we define a character array with two elements.
And then we can read a key to the first element of the array.
Next, we print
Ok let’s build the project.
We copy the binary file to the boot folder
and then we build the kernel project.
OK, let's press some keys to do the test.
OK, you can see the character is printed on screen in yellow.
This is because in the system call module, the attribute of the characters in the screen buffer is set to E
which means the character is printed in yellow.
OK, that's it for this lecture.
See you in the next video.