Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 6. DEVELOPING ESSENTIAL DEVICE DRIVERS AND A


FILESYSTEM 67


int rows = offset / (2* MAX_COLS );
offset = get_screen_offset (79, rows);
// Otherwise , write the character and its attribute byte to
// video memory at our calculated offset.
} else {
vidmem[offset] = character;
vidmem[offset +1] = attribute_byte;
}

// Update the offset to the next character cell , which is
// two bytes ahead of the current cell.
offset += 2;
// Make scrolling adjustment , for when we reach the bottom
// of the screen.
offset = handle_scrolling(offset );
// Update the cursor position on the screen device.
set_cursor(offset );
}

Let’s tackle the easiest of these functions first: getscreenoffset. This function
will map row and column coordinates to the memory offset of a particular display char-
acter cell from the start of video memory. The mapping is straightforward, but we must
remember that each cell holds two bytes. For example, if I want to set a character at
row 3, column 4 of the display, then the character cell of that will be at a (decimal)
offset of 488 ((3 80 (i.e. the the row width) + 4) 2 = 488) from the start
of video memory. So ourgetscreenoffsetfunction will look something like that in
Figure XXX.


// This is similar to get_cursor , only now we write
// bytes to those internal device registers.
port_byte_out(REG_SCREEN_CTRL , 14);
port_byte_out(REG_SCREEN_DATA , (unsigned char)( offset >> 8));
port_byte_out(REG_SCREEN_CTRL , 15);

Now let’s look at the cursor control functions,getcursor()andsetcursor(),
which will manipulate the display controller’s registers via a set of I/O ports. Using the
specfic video devices I/O ports to read and write its internal cursor-related registers, the
implementation of these functions will look something like that in Figure XXX.


cursor_offset -= 2* MAX_COLS;

// Return the updated cursor position.
return cursor_offset;
}

int get_cursor () {
// The device uses its control register as an index
// to select its internal registers , of which we are
// interested in:
Free download pdf