Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 6. DEVELOPING ESSENTIAL DEVICE DRIVERS AND A


FILESYSTEM 69


Another useful, but not too difficult function, isclearscreen(...), which will
allow us to tidy up our screen by writing blank characters at every position. Figure
XXX shows how we might implement such a function.


void clear_screen () {
int row = 0;
int col = 0;
/* Loop through video memory and write blank characters. */
for (row=0; row <MAX_ROWS; row++) {
for (col=0; col <MAX_COLS; col++) {
print_char(’ ’, col , row , WHITE_ON_BLACK );
}
}

// Move the cursor back to the top left.
set_cursor(get_screen_offset (0, 0));
}

6.2.3 Scrolling the Screen


If you expected the screen to scroll automatically when your cursor reached the bottom
of the screen, then your brain must have lapsed back into higher-level computer land.
This can be forgiven, because screen scrolling seems like such a natural thing that we
simply take for granted; but working at this level, we have complete control over the
hardware, and so must implement this feature ourselves.
In order to make the screen appear to scroll when we reach the bottom, we must
move each character cell upwards by one row, and then clear the last row, ready for
writing the new row (i.e. the row that would otherwise have been written beyond the
end of the screen). This means the the top row will be overwritten by the second row,
and so the top row will be lost forever, which we will not concern ourselves with, since
our aim is to allow the user to see the most recent log of activity on their computer.
A nice way to implement scrolling is to call a function, which we will define as
handlescrolling, immediately after incrementing the cursors position in ourprintchar.
The the roll ofhandlescrolling, then, is to ensure that, whenever the cursor’s video
memory offset is incremented beyond the last row of the screen, the rows are scrolled
and the cursor is repositioned within the last visible row (i.e. the new row).
Shifting a row equates to copying all of its bytes --- two bytes for each of the 80
character cells in a row --- to the address of the previous row. This is a perfect op-
portunity for adding a general purposememorycopyfunction to our operating system.
Since we are likely to use such a function in other areas of our OS, let’s add it to the
filekernel/util.c. Ourmemorycopyfunction will take addresses of the source and
destination and the number of bytes to copy, then, with a loop, will copy each byte as
in Figure XXX.


/* Copy bytes from one place to another. */
void memory_copy(char* source , char* dest , int no_bytes) {
Free download pdf