Implementation Of SachOS (Part 03)

Sachithra_Manamperi
4 min readAug 6, 2021

This is the third in a series of articles on this topic. Let's see how to integrate outputs into your workflow.

You can read My first article using this link.👇

I’m following “The Little Book About OS Development” by Erik Helin and Adam Renberg.
It’s first and foremost necessary to develop our first driver, which will serve to act as a layer between the kernel and the hardware, offering a higher level of abstraction than interacting directly with the hardware.

Interacting with the Hardware

Memory-mapped I/O and I/O ports are two ways to interact with the hardware. Writing to a specific memory address updates the hardware if the hardware utilizes memory-mapped I/O.

Assembly code instructions “out” and “in” must be used to interconnect and communicate with hardware that has I/O ports. Data to send and the I/O port’s address are required inputs for “out.” A single parameter is required for the instruction “in,” which is the I/O port’s address. Using I/O ports, the frame buffer’s cursor can be controlled by the hardware.

The Framebuffer

When you use the framebuffer, you can show a buffer of memory on the screen.

Writing Text

Text can be written directly to the console using a framebuffer and memory-mapped I/O (memory-mapped I/O). You’ll need to create a directory named drivers and add this code to a file called “frame buffer.h.”

Moving the Cursor

In order to move the framebuffer’s pointer, two distinct I/O ports must be used.
It is not possible to directly execute the “out” assembly code instruction in C.
So it would be wise to encapsulate “out” in an assembly code-based function that could be accessed from C.
“Io.s” is a file that contains the following code for this.

Then, using this code, you must create a header file called “io.h” in the drivers folder.

“frame_buffer.h” file contains a C function for cursor movement.

The Driver

In order for the rest of the OS code to communicate with the framebuffer, the driver must provide an interface to the framebuffer. Add this function to the “frame buffer.h” file, when you’d want to do so.

The Serial Ports

An interface for communicating between hardware devices, the serial port is used to connect devices together. Simple to use, the serial port can be used to log data in Bochs. It is common for computers to have more than one serial port, but we shall only use one of them. Serial ports will only be used for logging purposes. The serial ports will be used just for output and not input. I/O ports are entirely responsible for controlling serial ports.

Configuration of The Serial Ports

“serial port. h” is located in the drivers directory, and this code needs to be included there to accomplish this.

Writing Data to the Serial Port

The data I/O port is used to send data to the serial port. FIFO queues must be empty prior to writing. You’ll need to add these functions to the “serial port.h” file if you want to do that and write data to the serial port.

The “in” assembly code instruction allows you to read the contents of an I/O port. The “in” assembly code instruction cannot be used directly from C, so it must be wrapped. As a result, you can add this code to the “io.s” file.

“io.h” in the drivers directory must be modified to include this code.

This line should be included at the end of “bochsrc.txt” file in order to get output from the serial port one into a file called “com1.out”.

com1: enabled=1, mode=file, dev=com1.out

The “loader.s” file must be modified as follows.

Lastly, your “kmain.c” file must look like this in order to make it work properly

String and string length can be changed to produce any output you desire by just changing the string and string length. Run your OS by using the “make run” command.

This string is also seen in “com1.out”.
Your understanding of how to integrate outputs should be clear by now if it isn’t already. You may check out my Github repository by clicking on the link below.

https://github.com/SachPrecious/SachOS

--

--

Sachithra_Manamperi

Undergraduate | Software Engineering | Dharmaraja College Kandy | Sri Lankan