How to create your own OS #3

Rasalingam Ragul
5 min readAug 6, 2021

Hello friends welcome back this is the third part of the series if you miss earlier parts go throw this link : List: How to create your own OS (medium.com)

Play with `Outputs`

This article will present how to display text on the console as well as writing data to the serial port. Furthermore, we will create our first driver, that is, code that acts as a layer between the kernel and the hardware, providing a higher abstraction than communicating directly with the hardware. The first part of this chapter is about creating a driver for the framebuffer to be able to display text on the console. The second part shows how to create a driver for the serial port. Bochs can store output from the serial port in a file, effectively creating a logging mechanism for the operating system.

Interacting with the Hardware

There are two different ways to interact with the hardware, which are memory-mapped I/O and I/O ports. If the hardware uses memory-mapped I/O then you can write to a specific memory address and the hardware will be updated with the new data.

If the hardware uses I/O ports then the assembly code instructions “out” and “in” must be used to communicate with the hardware. The instruction “out” takes two parameters which are the address of the I/O port and the data to send. The instruction “in” takes a single parameter, the address of the I/O port, and returns data from the hardware. The cursor of the framebuffer is one example of hardware controlled via I/O ports.

The Framebuffer

The framebuffer is a hardware device that is capable of displaying a buffer of memory on the screen. The framebuffer has 80 columns and 25 rows, and the row and column indices start at 0 (so rows are labelled 0–24).

Writing Text

Writing text to the console via the framebuffer is done with memory-mapped I/O. The starting address of the memory-mapped I/O for the framebuffer is 0x000B8000 . The memory is divided into 16 bit cells, where the 16 bits determine both the character, the foreground color and the background color. The highest eight bits is the ASCII [28] value of the character, bit 7 - 4 the background and bit 3 - 0 the foreground, as can be seen in the following figure:

Bit:     | 15 14 13 12 11 10 9 8 | 7 6 5 4 | 3 2 1 0 |
Content: | ASCII | FG | BG |

The available colors are shown in the following table:

So, you need to create a directory called drivers in your OS directory and add this code to a file called “frame_buffer.h”. It will help to keep everything organized. I’ll put this file after the moving cursor part because I try to reduce your cofution.

Moving the Cursor

Moving the cursor of the framebuffer is done via two different I/O ports. The “out” assembly code instruction can’t be executed directly in C. Therefore it is a good idea to wrap “out” in a function in assembly code that can be accessed from C. So, here is the code for the file called “io.s”.

then we create a header file call ‘io,h’ in the driver directory,

now we create “frame_buffer.h” file in the driver directory,

The Serial Ports

The serial port is an interface for communicating between hardware devices. The serial port is easy to use, and, more importantly, it can be used as a logging utility in Bochs. If a computer has support for a serial port, then it usually has support for multiple serial ports, but we will only make use of one of the ports. This is because we will only use the serial ports for logging. Furthermore, we will only use the serial ports for output, not input. The serial ports are completely controlled via I/O ports.

Configuring the Serial Port

The first data that need to be sent to the serial port is configuration data. In order for two hardware devices to be able to talk to each other they must agree upon a couple of things. These things include:

  • The speed used for sending data (bit or baud rate)
  • If any error checking should be used for the data (parity bit, stop bits)
  • The number of bits that represent a unit of data (data bits)

Writing Data to the Serial Port

Writing data to the serial port is done via the data I/O port. However, before writing, the transmit FIFO queue has to be empty (all previous writes must have finished). The transmit FIFO queue is empty if bit 5 of the line status I/O port is equal to one.

now we need config serial port so lets create a file “call serial_port.h” in driver directory ,

now you add this code ‘com1: enabled=1, mode=file, dev=com1.out’ in your already created “borchsrc.txt” file like this,

then make follow changes in your already created “kmain.c” file,

you can edit the buffer array with your text.

Now you add ‘io.o’ in your already created Makefile file OBJECTS part like this,

now all parts are done lets boot our OS and watch our text in our OS no more the delay open your terminal and run ‘make run’ command then press ‘c’ in the terminal now you can see your out put in your own OS like this

and all so you can see your text in ‘com1.out’ file your directory ,

this the end of this article if you need more knowledge about this refer the below references,

Reference:

My GitHub repository : rragul/windOS (github.com)

--

--