Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 6. DEVELOPING ESSENTIAL DEVICE DRIVERS AND A


FILESYSTEM 66


a driver interface file,screen.h, in ourdriversfolder. Due to our use ofwildcardfile
inclusion in our makefile,screen.cwill (as will any other C files placed in that folder)
be automatically compiled and linked to our kernel.
Firstly, let’s define the following constants inscreen.h, to make our code more
readible:


#define VIDEO_ADDRESS 0xb8000
#define MAX_ROWS 25
#define MAX_COLS 80
// Attribute byte for our default colour scheme.
#define WHITE_ON_BLACK 0x0f

// Screen device I/O ports
#define REG_SCREEN_CTRL 0x3D4
#define REG_SCREEN_DATA 0x3D5

Then, let’s consider how we would write a function,printchar(...), that dis-
plays a single character at a specific column and row of the screen. We will use this
function internally (i.e. privately), within our driver, such that our driver’s public in-
terface functions (i.e. the functions that we would like external code to use) will build
upon it. We now know that video memory is simply a specific range of memory ad-
dresses, where each character cell is represented by two bytes, the first byte is the ASCII
code of the character, and the second byte is an attribute byte, that allows us to set
a colourscheme of the character cell. Figure XXX shows how we could define such a
function, by making use of some other functions that we will define: getcursor(),
setcursor(),getscreenoffset(), andhandlescrolling().


/* Print a char on the screen at col , row , or at cursor position */
void print_char(char character , int col , int row , char attribute_byte) {
/* Create a byte (char) pointer to the start of video memory */
unsigned char *vidmem = (unsigned char *) VIDEO_ADDRESS;

/* If attribute byte is zero , assume the default style. */
if (! attribute_byte) {
attribute_byte = WHITE_ON_BLACK;
}

/* Get the video memory offset for the screen location */
int offset;
/* If col and row are non -negative , use them for offset. */
if (col >= 0 && row >= 0) {
offset = get_screen_offset(col , row);
/* Otherwise , use the current cursor position. */
} else {
offset = get_cursor ();
}

// If we see a newline character , set offset to the end of
// current row , so it will be advanced to the first col
// of the next row.
if (character == ’\n’) {
Free download pdf