Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 6. DEVELOPING ESSENTIAL DEVICE DRIVERS AND A


FILESYSTEM 68


// reg 14: which is the high byte of the cursor ’s offset
// reg 15: which is the low byte of the cursor ’s offset
// Once the internal register has been selected , we may read or
// write a byte on the data register.
port_byte_out(REG_SCREEN_CTRL , 14);
int offset = port_byte_in(REG_SCREEN_DATA) << 8;
port_byte_out(REG_SCREEN_CTRL , 15);
offset += port_byte_in(REG_SCREEN_DATA );
// Since the cursor offset reported by the VGA hardware is the
// number of characters , we multiply by two to convert it to
// a character cell offset.
return offset *2;
}

void set_cursor(int offset) {
offset /= 2; // Convert from cell offset to char offset.
// This is similar to get_cursor , only now we write
// bytes to those internal device registers.

So now we have a function that will allow us to print a character at a specific
location of the screen, and that function encapsulates all of the messy hardware specific
stuff. Usually, we will not want to print each charater to the screen, but rather a whole
string of characters, so let’s create a friendlier function,printat(...), that takes a
pointer to the first character of a string (i.e. achar *) and prints each subsequent
character, one after the other, from the given coordinates. If the coordinates(-1,-1)
are passed to the function, then it will start printing from the current cursor location.
Ourprintat(...)function will look something like that in Figure XXX.


void print_at(char* message , int col , int row) {
// Update the cursor if col and row not negative.
if (col >= 0 && row >= 0) {
set_cursor(get_screen_offset(col , row));
}
// Loop through each char of the message and print it.
int i = 0;
while(message[i] != 0) {
print_char(message[i++], col , row , WHITE_ON_BLACK );
}
}

And purely for convenience, to save us from having to typeprintat(‘‘hello’’,
-1,-1), we can define a function,print, that takes only one argument as in Figure
XXX.


void print(char* message) {
print_at(message , -1, -1);
}
Free download pdf