Do a operating system that supports graphical interfaces (on)

xiaoxiao2021-03-06  53

Original: http://www.binghua.com/Article/class6/class7/200409/267.html (reproduced and quote please note the original author and source) (PDF: http://www.binghua.com/soft/ Class9 / Class16 / 200409 / 65.html) (Source code: http://www.binghua.com/soft/class9/class16/200409/64.html) Be a support system that supports graphical interfaces (on)

Version 0.01

(Corresponding PYOS version number: 2004_09_18_14_00)

Harbin Institute of Technology Xie Yubo

(email: xieyubo@126.com URL: http://purec.binghua.com)

(QQ: 13916830 Harbin Institute of Technology BBSID: IAMXIAOHAN)

Foreword

Graphical Interface (GUI) is almost all mainstream operating systems and applications, because it provides excellent human-computer interaction interface, Microsoft's famous windows is a very successful example, it is said that Microsoft's philosophy has One is "Let the computer become more and more fools, anyone can operate it", it is obvious that the GUI interface is necessary.

If there is an operating system support, the program with graphical interface is relatively easy, because the operating system manages the graphics card for you, provides you with a variety of drawing points, draw lines, painting rectangles, Fills and other graphics functions, you only need to display things you want to display, by directly calling such a function, the operating system will complete the rest of the rest.

However, if you plan to write an operating system from your head, you can't use the convenience function provided by the existing operating system, how should you complete such a graphical interface?

This article intends to continue with the PYOS system as an example, brief description how to make your own operating system support the graphical interface. If you want to better understand this article, you need to know a little understanding of the boot process of the operating system, you can refer to the reference 1 of this article, in addition, you still need to know the assembly language, this can be referred to Reference 2 of this article 2. The experimental code of this article is completed by assembly and C language. If you don't understand the C language, you can refer to the reference 3 of this article.

The standard GUI interface should include the display of the graphical interface and support for user input, and the most widely used user input device used in the GUI interface is the mouse. Therefore, this paper is intended to be described in two parts. The above describes the display of the graphical interface, which describes the support of the mouse.

You are welcome to contact me because of the defense and wrong or any suggestions. I will track feedback on this article on the Harbin Institute of Technology (http://purec.binghua.com), and the data and source code described herein can also be found above.

First, the graphics interface standard VESA introduction

The so-called standard is actually an agreement, such as the graphics card interface standard, which is a protocol that communicates between the graphics card and the host. Through this protocol, the host can operate and control the graphics card. For example, the host should make the graphics card in (x, y) a point, you need to do what to do with the graphics, which has a clear instructions and regulations on the protocol, so if we have to operate a graphics, only need to follow the protocol It's okay to the regulations, the agreement here is like an instructions.

With the development of the graphics card, there have been many protocols, such as the EGA protocol, CGA protocol, VGA protocol, etc., and now the most widely used is known as VESA-called VESA. The latest protocol version is now 3.0, but because all graphics cards are not supported by this protocol, especially many virtual machines are not supported, this article will be the foundation of 2.0 as a description, because each version is Under compatibility, the 2.0-based program is fully available from version 3.0. The VESA standard includes a lot of sub-standards, which are most useful for the operating system is the VESA BIOS EXTENSION. In the actual system writing, we follow this standard, by calling the BIOS's 0x10 number interrupt, and the graphics card Operation, when calling this number interrupt, the AX register is stored in the function you want to use. For example, the VESA 2.0 standard specifies that the 0x4F00 function can return the VESA standard supported by the graphics card. The 0x4F01 function can return information specified by the display mode, such as the ranks pixel, how much is the number of bytes per pixel. Calling these features is very convenient, for example, by reading the VESA standard, we know that the 0x4f02 function can be used to set the display mode. When this feature is called, the BX is stored in the display mode of the display mode, so if we want Set the display mode of the graphics card to 0x111 mode, then we should write the following code:

MOV AX, 0X4F02 ;; Set the interrupt function number, indicating the use of 0x4f02

MOV BX, 0x111 ;; Set the display mode number, indicating the use of 0x111 display mode

INT 0x10 ;; Call the 0x10 number of the BIOS, set the graphics function

After executing the above code, the graphics card is set to the 0x111 display mode, then what is the display feature? Please see the table below:

(Table 1 VESA standard defined display mode (part))

Mode number

Resolution

color

0x100

640 * 400

256

0x101

640 * 480

256

0x102

800 * 600

16

0x103

800 * 600

256

0x104

1024 * 768

16

0x105

1024 * 768

256

0x106

1280 * 1024

16

0x107

1280 * 1024

256

0x10d

300 * 200

1: 5: 5: 5

0x10e

320 * 200

5: 6: 5

0x10f

320 * 200

8: 8: 8

0x110

640 * 480

1: 5: 5: 5

0x111

640 * 480

5: 6: 5

0x112

640 * 480

8: 8: 8

0x113

800 * 600

1: 5: 5: 5

0x114

800 * 600

5: 6: 5

0x115

800 * 600

8: 8: 8

0x116

1024 * 768

1: 5: 5: 5

0x117

1024 * 768

5: 6: 5

0x118

1024 * 768

8: 8: 8

0x119

1280 * 1024

1: 5: 5: 5

0x11a

1280 * 1024

5: 6: 5

0x11b

1280 * 1024

8: 8: 8

The above table lists the partial display modes defined by the VESA standard, with two colors. One is the so-called "palette" mode, one is the so-called "true color" mode, "palette" mode mainly in compatible with the previous old-fashioned graphics card, the number of memory cards generally speak very Less, therefore, the graphics card can only store 256 (8-bit) or less 16 color (4 digits), as shown in the previous table. These color organizations becomes a table on the graphics card, which is called a palette, and then the color information per point is actually a subscript, which is used to retrieve real colors from the palette. For example, the color of a point is 2, then the third color in the palette is used (since the number starts from 0, the 2 corresponds to the third item in the palette). Obviously, not all colors can be recorded in a small palette, so in general, for a given color, we need to display it with its most similar colors in the palette. Now the graphics card generally have a lot of memory, so it can complete a point of color information. We know that any color can be synthesized by red (R), Green (G), Lan (B) of different intensity (brightness), so we have to record or give a point color information, only need Given the three colors (brightness) of red, green, and blue, there is a variety of different coding methods, such as "5: 6: 5" mode, indicating the highest 5 points The red intensity is used in the middle of 6, indicates the green strength, and the last 5 represents the strength of the blue, which means a color, a total of 16 bits, 2B, and "8: 8: 8" said, The highest 8 digits represent red strength, then 8 bits, indicates the green strength, the last 8 bits represent the strength of the blue, one color is 24 bits, i.e., 3 bytes. And so on. Since these colors are recorded by real records, this mode is also referred to as "true color" mode, as shown in the back part of Table 1.

It can be seen from the above description, we can select the appropriate display mode according to our needs, and then call the interrupt setting display card defined in the VBE standard.

The VBE standard describes a large number of functions, which is not intended to describe it all, only describe the features required by the following text, if you want to know the features described throughout the VBE standard, refer to the reference 4 of this article.

Below we will describe the 0x4f01 functionality that needs to be used below. This feature returns the corresponding information of the display mode supported by the graphics card to the address specified by the user. It can be called as follows:

MOV AX, 0x4F01 ;; Indicates to use 0x4f01 function to get display mode information

MOV CX, 0x111 ;; Indicates information to get 0x111 display mode

Mov ES, 0x9000

MOV DI, 0x0001 ;; The above two sentences indicate the putting information in ES: DI

; (Here is 0x9000: 0x0001), this is a memory

; Initial address, and this block memory is at least 256b size

The returned information is a 256-byte, a very large structure, here we only introduce some of our part of interest.

Where the offset is 40 in the returned structure, ES: DI 40, a linear address is stored with 4 bytes, which is the linear address of this graphics in this mode, so if we directly Write data to this address, then this data will be written directly to the memory so that the information we need to display can be displayed. This is the so-called "direct written screen". The returned structure also includes other many information such as pixels, column pixels such as column pixels, column pixels in this mode. If you need to know more, please refer to the reference 4 of this article.

Second, use PYOS to conduct experiments

2.1 Boot code analysis

It is very happy that this experiment we don't need to understand too much basic knowledge, and you can enter the experiment very quickly. Let's take a look at our experiments.

Before experimenting, we need to set a display mode we need experiment, here we are temporarily set to 640 * 480, the color mode used is 5: 6: 5 mode, namely 0x111 mode.

Let's take a look at our final results of this experiment:

Now let's take a look at our boot code:

Main: ;; main program

;; below set segment register

Mov AX, Boot_SEG

MOV DS, AX

MOV AX, TEMP_DATA_SEG

Mov SS, AX

MOV SP, 0xFFFF

MOV [Boot_Driver], DL ;; get started drive letter

CALL OPEN_A_20 ;; Open A20 address line

Call save_boot_driver ;; save the drive letter

Call show_message ;; Display startup information

Call read_setup ;; Read setup program

JMP DWORD SETUP_SEG: SETUP_OFFSET ;; Jump to Setup

This is the main function code of the PYOS bootr. It is now very simple. At the beginning, you need to set a segment register, such as initializing the Data Segment Register (DS), Stack Segment Register (SS), And stack pointers (SP), etc., then read the SETUP program (Steup.asm), read the setup program (STEUP_SEG: setup_offset) For detailed code of each subroutine, please refer to the source code of this experiment, which has a detailed note.

Below, let's take a look at the setup (setup.asm) program:

MAIN:

; Initialize the register, because the BIOS interrupt and CALL will use the stack or SS register

; At the time of the CPU start or reset, the BIOS is initialized, and now the segment transfer, we need to reset

MOV AX, Setup_SEG

MOV DS, AX

MOV AX, TEMP_DATA_SEG

Mov SS, AX

MOV SP, 0xFFFF

; Display startup information

Call show_message

; Get started drive

Call get_boot_driver

; Set display mode

Call set_vesa_model

; Read the Kernel program

Call read_kernel

; Read the word library

Call read_font_lib

; Picture of reading HIT

Call read_hit_pbmp

;; The following starts to initialize the protection mode

LGDT [GDT_DESCRIPTOR] ;; Descriptor loaded with GDT

;; The following settings enter the 32-bit protection mode operation

CLI ;; Off Interrupt

MOV Eax, Cr0

OR EAX, 1

MOV CR0, EAX

JMP DWORD 0x8: Kernel_ENTRY

The above program is also very simple, first, reinitialize the segment register, then set the graphic display mode, then read the Kernel program (this is the true operating system core code), then read into the word library (this is There will be a more detailed description below), then read into a HIT picture (Hit is the English abbreviation of "Harbin Institute of Technology", the picture is a BMP format, which will be described in detail below), then Convert to the work in the protection mode, and finally switch to the kernel program in the protection mode, that is, the true operating system kernel code is executed. For this part of the work to switch to the protection mode, there is a very detailed description of the reference 1 of this article. If you have doubts about this, please refer to it. Let's take a look at this core subroutine set_vesa_model code, and the rest of the code, please refer to the source program of this experiment.

SET_VESA_MODEL: ;; Setting the graphics pattern

Push ES

Push fs

; Setting the graphics pattern

MOV AX, 0x4F02

MOV BX, 0x4111 ;; 640 * 480 (5: 6: 5)

INT 0x10

; Advanced Linear Address in this mode

MOV BX, Setup_SEG

MOV ES, BX

Mov Di, VESA

;; Call 0x4f01 function number, get information

MOV AX, 0x4f01

MOV CX, 0x111

INT 0x10

; Deposit linear address

MOV EAX, [ES: VESA 40]

MOV BX, TEMP_DATA_SEGMOV FS, BX

MOV [fs: 1], EAX

POP ES

POP FS

RET

The program is very simple, and the following is a simple explanation, after entering the program, we set the display mode of the graphics card first:

; Setting the graphics pattern

MOV AX, 0x4F02

MOV BX, 0x4111 ;; 640 * 480 (5: 6: 5)

INT 0x10

The above line of code is introduced in the previous description of the VBE standard. Here we set 640 * 480 (5: 6: 5), that is, the corresponding 0x111 mode (see Table 1), however, you will find it here BX is 0x4111 instead of 0x111, why? This is because we want to use linear address mode, that is, by directly accessing physical memory empty, access all of the memory space, therefore, the time to send BX should be 0x4000 | mode number, for this : 0x4000 | 0x111 = 0x4111 (the mode number is performed with 0x4000, which is also specified by the VBE standard).

Subsequently, we use the 0x4f01 function to get the linear address of the graphics card in this mode:

; Advanced Linear Address in this mode

MOV BX, Setup_SEG

MOV ES, BX

MOV BX, TEMP_DATA_SEG

MOV FS, BX

Mov Di, VESA

;; Call 0x4f01 function number, get information

MOV AX, 0x4f01

MOV CX, 0x111

INT 0x10

Due to ES = TEMP_DATA_SEG, DI = VESA, the read information is stored in the TEMP_DATA_SEG: VESA, and then, the linear address of the graphics card in this mode is removed from the information:

; Deposit linear address

MOV EAX, [ES: VESA 40]

MOV BX, TEMP_DATA_SEG

MOV FS, BX

MOV [fs: 1], EAX

POP ES

POP FS

RET

The above code is very simple. Since the linear address of the graphics card in this mode is existing in the returned information block, it is read from the "MOV EAX, [ES: VESA 40]". In Eax, then put it in [FS: 1], since FS is equal to TEMP_DATA_SEG, it is stored in TEMP_DATA_SEG: 1.

SETUP_SEG, VESA, TEMP_DATA_SEG in the above program are the constants originally defined in the program. For details, please refer to the source code.

2.2 PYOS memory structure

Since PYOS in this experiment has no file system and memory management and allocation program, in this experiment, PYOS uses absolute positioning of disk and memory, that is, all data in memory is fixed, let's let Let's take a look at the memory structure of PYOS. 0x0000: 0x7c00: The code stored here is the code of BOOT.ASM, and BIOS is automatically read when power is powered.

0x1000: 0x0000: The code stored here is the code of setup.asm, read by boot.asm

0x2000: 0x0000: The kernel code is stored here (mainly KERNLE.C), read by setup.asm

0x6000: 0x0000: The picture data stored here is Hit, read by setup.asm

0x7000: 0x0000: The English point branch library data is stored here, read by setup.asm

0x8000: 0x0000: The Chinese point branch library data is stored here, readup.

0x9000: 0x0000: Starting the drive letter here, deposited by boot.asm

0x9000: 0x0001: The linear address of the graphics card memory is stored here.

2.3 PYOS graphics drive

After the program is finally jumped into the kernel code by "JMP DWORD 0x8: Kernel_ENTRY", the operating system starts, let's take a look at this kernel code:

#include "system.h"

#include "vesa.h"

Void kernel_main ()

{

// system initialization

SYSTEM_INIT ();

// Qingping screen

Unsigned short color = vesa_compond_rgb (255, 255, 255);

// painted rectangle

VESA_DRAW_RECT (0, 0, 639, 479, Color, 1);

............ (The rest of the drawing code)

For (;;);

}

The above code is very simple. It first made the system initialization, then make a clear screen, the PYOS clear screen is a way to use the most snail, which uses the entire screen as a rectangle (x: 0 ~ 639, y: 0 ~ 479), then use a color to draw every point in this rectangle. VESA_COMPOND_RGB () This function is used to integrate the user input R (red), G (green), and blue (b) a 16-bit integer (because the 5: 6: 5 model is used, so there is a total of one The color of the point is indicated by a 16-bit integer), then it calls the VESA_DRAW_RECT () function to draw this rectangle, let's take a look at this function:

// Resemble a rectangular function

Void VESA_DRAW_RECT (unsigned int x1, unsigned int y1, unsigned int x2, unsigned y2, unsigned short color, int dose_fill_it)

{

VESA_DRAW_X_LINE (Y1, X1, X2, Color);

VESA_DRAW_X_LINE (Y2, X1, X2, Color);

VESA_DRAW_Y_LINE (X1, Y1, Y2, Color);

VESA_DRAW_Y_LINE (X2, Y1, Y2, Color);

IF (dose_fill_it) {VESA_FILL_RECT (X1, Y1, X2, Y2, Color);

}

}

Very simple, it first draws a rectangular border, then determines whether the VESA_FILL_RECT () function is populated according to the user's input parameter. Let's take a look at this fill function:

// Rectangular filling function

Void VESA_FILL_RECT (unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned short color)

{

For (int x = x1; x

For (int y = y1; y

VESA_DRAW_POINT (X, Y, Color);

}

}

}

gosh! It's too simple, it is to call the VESA_DRAW_POINT () function, using the same color to draw all the points in the rectangle, so we have to trace the source to see the VESA_DRAW_POINT () function:

// Draw point function

Void Vesa_Draw_point (unsigned int x, unsigned int y, unsigned short color)

{

Static const unsigned int x_max = 639; // Each line pixel

Static const unsigned int y_max = 479; // Each column number

/ / Prevent offshore

IF (x> x_max) {

X = x_max;

}

IF (y> y_max) {

Y = y_max;

}

// Get the contact linear address

Unsigned short * video = (unsigned short *) (* (unsigned int *) 0x90001)));

/ / The offset of the calculation point

Unsigned int offset = y * (x_max 1) x;

* (Video Offset) = Color;

}

This function is the foundation of the entire Pyos Vesa graphics card, all line, painted rectangles, fill rectangles are completed, now we will analyze, pay attention to the following line of code:

// Get the contact linear address

Unsigned short * video = (unsigned short *) (* (unsigned int *) 0x90001)));

First, in Section 2.2, we know that the linear address of the graphics card is stored in 0x9000: 0001 (this linear address is 32 bits, ie 4b), so converts 0x9000: 0001 to linear addresses is 0x90001, then, we are in C The language turns it to force into a pointer to the unsigned int type, then pass the * operator, which is the pointer, that is, the data stored at 0x90001, that is, the linear address stored here, Then convert it to a Unsigned Short type pointer, because each point is used 2B bytes, which is represented by a UNSIGNED SHORT type, this linear address is also (0,0 Add the address of the point.

Subsequently, we passed the number of coordinates: X, row coordinate: Y, calculated the offset of the (x, y) point, and finally assigning the color information to the memory address of this (x, y) point, then (X, Y) The color we specified is displayed. In fact, it is so simple! Display of 2.4 English, Chinese and graphics

Let's talk about the graphics, here is mainly referring to the chart, that is, the so-called BMP format graphic display, the bitmap is the record of each point (bit) on this pair, which contains each The color information of a point, such as a 24-bit bitmap, that is, each point on this pair uses a 24-bit data, ie 3 byte data saved. Of course, the stored data can be the color index value in the palette, or RGB data, etc. So, we can read the color data of each point, then write them into the corresponding display memory, just like the 2.3 section, a little bit of drawing them all, of course When using a bitmap, you need to know how many bytes of this pair map each point, how many points are there, how many lines have a total of a row, or a list is stored, of course, These are actual realization problems, and there are not many described here. You can do your own implementation after understanding the principles, create your own graphics format.

Each English character is also the same as each Chinese character. They are also pictures, but, in general, this picture only needs to use a color display, so we don't need to record the color information of every point. And only need to record if each point needs to be displayed, do not need to be displayed, we can remember 0, the point you need to display We can record 1, so if there is 8 points, we only need 8 Bit, ie 1B can be stored, No. 0 to 7th, corresponding to 0 ~ 7 points on the actual row, if which bit is 1, then draw the point on the location on the graphics card.

The Chinese characters used in PYOS are the 16 * 16 dot matrix. In this font, each Chinese character is indicated by 16 * 16 points, that is, 16 lines, 16 points per line, by the previous, we can Take 16 points of each row, in 16, namely 2b, so, a Chinese character accounts for 2 * 16 = 32b, which is also called a word, people put all the Chinese characters, all the words, doing together The formation of a franchery is formed, and only the corresponding word is required from the font, then each bit of it is analyzed, and finally, the color value assigns the color value on the corresponding point on the display (graphics card).

The English characters are also the same. It is used in PYOS. In this form of English point, in this font, each English is expressed in 8 * 16 points, that is, 16 lines, 8 points per line, 1B Space, so the space takes up to 1 * 16 = 16b, the principle and method of display are all previously described.

Now the question is how to know an English character or a Chinese character in the starting position in the font. For English, each character has an ASCII code. In the English-language library, the English symbol is usually stored in the ASCII code. Therefore, with this English ASCII code, multiply by each English character needed. Space, you can get the English characters in the English word library. Chinese is slightly troublesome, we know, a Chinese is represented by two bytes, the first byte is equal to the zone code of the Chinese characters, plus 161, the second byte is equal to the number of Chinese characters plus 161, Chinese characters are zone Saving with the location, there are 94 districts in the national standard Chinese characters. There are 94 bits per district. This is also the "location code" that we often say, the Chinese characters are stored in a district in one area in the area code, Therefore, after knowing the location code of Chinese characters, we can calculate the location of the Chinese in the font. For example, the first section of a Chinese character is CH1, the second byte is CH2, so the zone code of the Chinese characters is Sector = CH1-161, The bit number of the Chinese character is position = CH2-161, the position in the word library is: pOS_IN_FONT = (Sector * 94 position) * size, whose size is the space occupied by each Chinese word in the font.

It should be noted that the above is some principles description, and the actual people do not necessarily use this, such as this PYOS experiment, because the data you need is very small, so, don't use it so much All the fonts are all loaded, so, in advance, a program is pre-written to extract the words you need to display from the font, then load this generated small grade library into memory, and finally displayed, it is extracted from the small font. The order sequentially displayed (please refer to the description in this experimental program). Of course, how to extract small franks from the font base, you need to use the above principles.

Detailed description of Chinese character display can be found in the reference 5 of this article.

The franca mentioned above is a pointing a branch library. Each word in this font is like a bitmap. There is also a vector font. For information on this, please refer to the relevant document.

Third, further explanation about this experiment

This experiment describes how to develop the GUI interface development of the operating system, but the description is very simple, there are many shortcomings, there are a few more points here:

First, the settings of the graphics to the graphics card described in this article are completed in real mode. This is mainly due to the relatively early in VESA, and the support of the protection mode is limited. Now VESA 3.0 provides a relatively complete relative complete Protection mode interface, you can reset the resolution of the graphics, display mode, etc. in the protection mode. But VESA 2.0 does not provide such an interface that you cannot reset them in protection mode. There are three ways to solve, one is before your operating system enters the real mode, let the user choose the display mode. After the operating system enters the protection mode, it will no longer allow the user to choose (this is not a solution), the second is Re-switching the protection mode, three is to switch to the virtual 8086 (Virtual 86) mode, the latter two ways are difficult, and more troublesome, interested can try.

Second, there is a considerable amount of data in this experiment, such as the number of display pixels per row, each line displaying the number of bytes of pixels is known, set to fixed, although there is no problem in most cases, But as a more versatile or better system, you should be dynamically read from the VBE standard, because you may have your graphics card that you have previously assumed the value, which can be introduced in the first chapter, by calling The 0x4f00 and 0x4f01 function obtains corresponding data and information. The graphics pattern used in this PYOS is a linear address mode or a linear buffer mode. That is, the video card's memory address is complete and continuously mapped to the linear memory space, and some graphics cards do not support this mode, but the so-called page needs to be used. Mode, you will be divided into a few pages, 64KB or 128KB per page, and then map them to a 64KB or 128KB interval in memory, if you need to display more than 64KB or 128KB, you need to call the function calls provided via VBE. Switch, switch to your specified page (shoot your specified page map in memory).

In addition, when PYOS is set to the graphics card mode, there is no error, whether the graphics card supports the set mode, which should not be preached in advance, but must be detected.

These include refer to the reference 4 of this article to obtain resolution information.

Again, the graphic drive of this PYOS is too simple to be too snail, which is complete through the drawpoint, and only the painting line is completed, and the diagonal line cannot be drawn, and these involves a considerable graphic. Knowledge of learning, interested friends can study research.

Finally, this experiment can only be seen, and the user cannot enter the operation, but it can't be called an interface. In the next section, it will describe how to write a mouse driver to make up for this defect.

references

1. "Operating System Guidance Research" (Version 0.02) (Xie Yubo) http://purec.binghua.com (Pure C Forum)

2. "IBM PC Assembly Language and Programming" (Copy Edition) (PETER ABEL) Tsinghua University Press

3. "C language university practical tutorial" (Su Xiaohong, Chen Huipeng, Sun Zhigang) Electronic Industry Press

4. "VESA BIOS EXTENSION CORE FUNCTIONS Standards" http://www.vesha.org

5. "Little Tiger" (Net, Pure C Forum (http://purec.binghua.com) is reproduced)

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

New Post(0)