Write Your Own Operating System Tutorial (7)

zhaozj2021-02-08  208

Lesson 7: Start Saying Goodbye to BIOS

Now that we have a boot loader that will load our operating system, and thus can make our operating system larger than one sector, we can now begin to add some complexity to our system. One of the first things to do is to loosen our tie to BIOS. So far we have been using BIOS functions for all of our input and output. BIOS hides all of the input and output from us with its interface, so we do not know exactly how it goes about performing its functions. BIOS can often be slower than handling I / O by ourselves, and in doing it ourselves, we can know exactly what is going on, thus giving us more power, control, and flexibility with the design of our operating system. of course, my main reason .

The area we will begin with is that of text output to the screen. So far we have been using BIOS interrupt 0x10, function 0x0E. We will begin performing text output ourselves. Before we can even start to do this, we need to know a few things. First of all, video memory is mapped to main memory addresses starting at address 0xB0000 (flat address from base of the physical address space). The region of memory holding the text content of the screen starts at 0xB0000 for monochrome displays and 0xB8000 For Color Displays. Try The Latter Address First, And if You can't get it working, Perhaps Try the former. I Will Proceed Assuming The Use of a Color Display.

The first byte (0xB8000) holds the ASCII code for the upper-leftmost character on the screen. The next byte (0xB8001) holds the color / style code for that character. These bytes are followed by two more bytes that hold the ASCII code and color / style code for the next character to the right. The video memory continues alternating ASCII code and style / color code to the end of the first row of text. The next bytes after this represent the first character on the second row, and so ON for the rest of the screen.so, all this next.. Need to Keep Track of the location of the cursor.

Speaking of the cursor, you can write characters to anywhere on the screen (anywhere in video memory). But it will look odd to the user if they are typing on the keyboard and characters are appearing one place on the screen and the little blinking cursor IS elsewhere on the screen. The Video Controller Chip (6845 on the ibm pc) Takes Care of Drawing The BLINKING CURSOR ON The Screen; We Just Need To Tell It Where To Move The Cursor.

The 6845 video controller is connected to I / O port 0x3B4-0x3B5 for a mono display and 0x3D4-0x3D5 for a color display. As far as I can tell, the 6845 has various registers, and (assuming a color display) port 0x3D4 is used to indicate which port we would like to write to, and then we write the data to port 0x3D5. Registers 14-15 tell the 6845 where to draw the blinky cursor. The following is psuedo-code for moving the cursor.

Outbyte (0x3d4, 14); // Write to Register 14 First

Outbyte (0x3d5, (Cursorpos >> 8) & 0xff); // Output High Byteoutbyte (0x3D4, 15); // Again to Register 15

Outbyte (0x3d5, Cursorpos & 0xFF); // Low Byte in this register

The cursor position (in cursorpos) is the character number, starting with 0 and number all the characters in the order that they are arranged in video memory. (The offset in video memory for a given cursor position is cursorpos * 2 for the ASCII code AND (CURSORPOS * 2) 1 for the color / style code.)

Using this region of video memory to output characters and the I / O ports to tell the video controller where to draw the cursor, it is now your job to write a text driver for your operating system. Create a set of functions that you can call to output characters, strings, numbers, pointers, etc to the screen without using BIOS (this means no software interrupts). Make sure that your text driver handles scrolling the text on the screen upward before going off the bottom. Write a function to clear .

Try allowing the user to type characters with the keyboard (you can still use BIOS for keyboard input for now), and echo each character to the screen as it is typed. If you have any troubles writing your text driver, let me know and perhaps I can give you some hints.

As far as the color / style byte is concerned, you can simply use 07 (white on black) for most purposes, but for those of you who are curious, I will explain the different color / style settings. The color / style of a Character is One byte. Those 8 Bits Are Used as Follows.

BITS 3-0: Foreground Color

Bit 3 2 1 0 Color Intensity Red Green Blue

Thase Four Bits Can Be Used in Any and All 16 Combinations. IF Bit 3 IS 1, IT IT INDICES FULL INTENSITY, 0 INDICES HALF INTENSITY. For Example, 3 Would Be CYAN (Blue Green) While 11 Would Be Bright Cyan (Intensity Blue Green) .bit 5: Reverse Video

Bit 6: Black on Colored Background, Color Given Bits 3-0

Bit 7: Blinking Text

For example a code of 0x2c (00101100bin) Would Be Reverse Video Bright Red.

转载请注明原文地址:https://www.9cbs.com/read-3177.html

New Post(0)