1 00:00:00,470 --> 00:00:05,780 In this lecture, we will add a key buffer so that the other modules and user programs can read the keys 2 00:00:05,780 --> 00:00:11,960 instead of just printing it on the screen. We have a working keyboard handler to receive the key characters 3 00:00:11,960 --> 00:00:12,590 , 4 00:00:13,160 --> 00:00:16,880 and all we need to do in this video is put these characters in the buffer. 5 00:00:17,570 --> 00:00:18,810 OK, let's get started. 6 00:00:19,880 --> 00:00:21,470 In the keyboard header file, 7 00:00:23,000 --> 00:00:27,560 we define the buffer structure which we will use to manage the read and write operation of key buffer 8 00:00:27,710 --> 00:00:28,670 . 9 00:00:29,560 --> 00:00:35,950 The buffer is an array with 500 bytes. We also have front and end 10 00:00:35,950 --> 00:00:39,520 which specifies the current front and end of the characters. 11 00:00:39,520 --> 00:00:40,930 Let’s go back to the keyboard.c file. 12 00:00:45,970 --> 00:00:51,700 If we read a character from keyboard and it’s a valid character, we will write it in the buffer. 13 00:00:52,670 --> 00:00:54,850 In the function write key buffer, 14 00:00:58,880 --> 00:01:03,250 we check to see if the buffer is full. This buffer is actually a circular queue. 15 00:01:04,400 --> 00:01:11,150 So if the end is one element away reaching to the front, it means that the buffer is full and we simply return 16 00:01:11,810 --> 00:01:12,440 , 17 00:01:12,440 --> 00:01:18,140 Otherwise we write the data in the buffer, update the end and write it back to the key buffer for later use. 18 00:01:19,150 --> 00:01:24,790 Here we % size, which means if the end is pointing to the last element, we will start from the beginning again 19 00:01:24,790 --> 00:01:25,500 . 20 00:01:26,660 --> 00:01:27,700 OK, let's move on. 21 00:01:30,760 --> 00:01:36,750 After we done the writing, we will wake up the process waiting for the keyboard. In this system, 22 00:01:36,760 --> 00:01:41,960 when a program wants to read a key and there is no key in the buffer, we will put it into sleep. 23 00:01:42,460 --> 00:01:44,110 So here we will wake it up. 24 00:01:44,710 --> 00:01:50,950 The wait argument we pass in this case is -2 which represents the processes waiting for the keyboard IO 25 00:01:50,950 --> 00:01:51,780 . 26 00:01:53,050 --> 00:01:55,660 Ok next we move to function read buffer. 27 00:01:58,840 --> 00:02:04,510 In this function, we will check if the buffer is empty before we read a key. If it is empty, 28 00:02:04,510 --> 00:02:07,680 we will put the process into sleep as we just talked about. 29 00:02:08,350 --> 00:02:13,960 And if we have keys in the buffer, we will return the key to the caller and update the front to point to 30 00:02:13,960 --> 00:02:14,940 the next location. 31 00:02:15,950 --> 00:02:22,100 In this example, the user program will read a key and print it on the screen. In order to do that, 32 00:02:22,100 --> 00:02:25,880 we need to add new system call functions just like we did before. 33 00:02:26,980 --> 00:02:32,200 So what we are going to do is we are going to add keyboard read function in system call module. 34 00:02:37,900 --> 00:02:41,760 As you see, we add sys keyboard read function. In this function, 35 00:02:41,800 --> 00:02:44,240 we do nothing but call function read key buffer. 36 00:02:45,280 --> 00:02:50,020 Don’t forget to change the checks since we add one more system call function. 37 00:02:51,120 --> 00:02:52,980 Ok we finished the kernel part. 38 00:02:53,730 --> 00:02:57,420 As for the user part, we test it with the user2 program. 39 00:02:58,600 --> 00:03:02,110 As we did before, we first add system call in the library. 40 00:03:03,170 --> 00:03:05,570 So in the syscall.asm file, 41 00:03:07,320 --> 00:03:08,160 we define 42 00:03:09,250 --> 00:03:12,640 keyboard readu. 43 00:03:15,660 --> 00:03:17,280 The index number is 4. 44 00:03:20,070 --> 00:03:26,060 It has no parameters, so we don’t allocate space on the stack and we clear rdi register. 45 00:03:29,450 --> 00:03:30,260 Then int 46 00:03:32,800 --> 00:03:33,280 80 47 00:03:34,440 --> 00:03:36,960 After we return from kernel, we simply return. 48 00:03:39,610 --> 00:03:40,720 And then we global 49 00:03:42,680 --> 00:03:43,560 keyboard readu. 50 00:03:45,250 --> 00:03:47,020 OK, now we can build the library. 51 00:03:57,990 --> 00:04:01,470 Also, we need to add the declaration in the lib.h file. 52 00:04:04,910 --> 00:04:06,890 The function returns a key character. 53 00:04:10,480 --> 00:04:12,820 Alright, let’s open main.c file. 54 00:04:15,060 --> 00:04:16,980 And we get rid of the code here. 55 00:04:18,800 --> 00:04:20,209 Then we use while loop 56 00:04:22,540 --> 00:04:25,680 to read a key constantly and print it on screen. 57 00:04:27,440 --> 00:04:31,070 So we define a character array with two elements. 58 00:04:34,810 --> 00:04:38,630 And then we can read a key to the first element of the array. 59 00:04:41,630 --> 00:04:42,650 Next, we print 60 00:04:47,550 --> 00:04:48,990 Ok let’s build the project. 61 00:04:51,710 --> 00:04:53,900 We copy the binary file to the boot folder 62 00:05:00,790 --> 00:05:02,530 and then we build the kernel project. 63 00:05:07,990 --> 00:05:10,390 OK, let's press some keys to do the test. 64 00:05:11,690 --> 00:05:15,820 OK, you can see the character is printed on screen in yellow. 65 00:05:16,360 --> 00:05:24,290 This is because in the system call module, the attribute of the characters in the screen buffer is set to E 66 00:05:24,550 --> 00:05:28,090 which means the character is printed in yellow. 67 00:05:29,260 --> 00:05:31,030 OK, that's it for this lecture. 68 00:05:31,060 --> 00:05:32,500 See you in the next video.