|
| 1 | + |
| 2 | +**Goal: Clean the code a bit and parse user input** |
| 3 | + |
| 4 | +In this lesson we will do tho things. First, we will clean up the code a bit, so it is ready |
| 5 | +for further lessons. During the previous ones I tried to put things in the most predictable places, |
| 6 | +but it is also a good exercise to know when the code base is growing and adapt it to current |
| 7 | +and further needs. |
| 8 | + |
| 9 | + |
| 10 | +Code cleaning |
| 11 | +------------- |
| 12 | + |
| 13 | +First of all, we will quickly start to need more utility functions |
| 14 | +for handling strings and so on. In a regular OS, this is called the C library, |
| 15 | +or libc for short. |
| 16 | + |
| 17 | +Right now we have a `utils.c` which we will split into `mem.c` and `string.c`, with their respective headers. |
| 18 | + |
| 19 | +Second, we will create a new function `irq_install()` so that the kernel |
| 20 | +only needs to perform one call to initialize all the IRQs. That function |
| 21 | +is akin to `isr_install()` and placed on the same `irq.c`. |
| 22 | +While we're here, we will disable the `kprint()` on `timer_callback()` |
| 23 | +to avoid filling the screen with junk, now that we know that it works |
| 24 | +properly. |
| 25 | + |
| 26 | +There is not a clear distinction between `cpu/` and `drivers/`. |
| 27 | +Keep in mind that I'm |
| 28 | +creating this tutorial while following many others, and each of them |
| 29 | +has a distinct folder structure. The only change we will do for now is to |
| 30 | +move `drivers/ports.*` into `cpu/` since it is clearly cpu-dependent code. |
| 31 | +`boot/` is also CPU-dependent code, but we will not mess with it until |
| 32 | +we implement the boot sequence for a different machine. |
| 33 | + |
| 34 | + |
| 35 | +Keyboard characters |
| 36 | +------------------- |
| 37 | + |
| 38 | +How to access the typed characters, then? |
| 39 | + |
| 40 | +- When a key is pressed, the callback gets the ASCII code via a new |
| 41 | +arrays which are defined at the beginning of `keyboard.c` |
| 42 | +- The callback then appends that character to a buffer, `key_buffer` |
| 43 | +- It is also printed on the screen |
| 44 | +- When the OS wants to read user input, it calls `libc/io.c:readline()` |
| 45 | + |
| 46 | +`keyboard.c` also parses backspace, by removing the last element |
| 47 | +of the key buffer, and deleting it from the screen, by calling |
| 48 | +`screen.c:kprint_backspace()`. For this we needed to modify a bit |
| 49 | +`print_char()` to not advance the offset when printing a backspace |
| 50 | + |
| 51 | + |
| 52 | +Responding to user input |
| 53 | +------------------------ |
| 54 | + |
| 55 | +The keyboard callback checks for a newline, and then calls the kernel, |
| 56 | +telling it that the user has input something. Out final libc function |
| 57 | +is `strcmp()`, which compares two strings and returns 0 if they |
| 58 | +are equal. If the user inputs "END", we halt the CPU. |
| 59 | + |
| 60 | +This is the most basic shell ever, but you should be proud, because |
| 61 | +we implemented it from scratch. Do you realize how cool this is? |
| 62 | + |
| 63 | +If you want to, expand `kernel.c` to parse more stuff. In the future, |
| 64 | +when we have a filesystem, we will allow the user to run some basic commands. |
0 commit comments