Sunday 13 November 2011

Can you change to a VGA graphics mode using the BIOS? in C programming

Can you change to a VGA graphics mode using the BIOS?

Yes. Interrupt 10 hex, the Video BIOS, handles switching between text and graphics modes (among other things). When you execute a program that changes from text mode to graphics mode and back (even if the program is Microsoft Windows), the Video BIOS is requested to perform the change. Each different setting is called a display mode.

To change the display mode, you must call the Video BIOS through the “int10” services. That is, you must make interrupt calls to the interrupt handler at interrupt 10. This is just like making DOS calls (int21), except the interrupt number is different. Following is a piece of sample code that calls Video BIOS function 0 to switch from standard text mode (Mode 3) to a mode number from the command line and back:
#include <stdlib.h>
#include <dos.h>
main(int argc, char ** argv)
{
union REGS regs;
int mode;
/* accept Mode number in hex */
sscanf(argv[1], “%x”, &mode);
regs.h.ah = 0; /* AH=0 means “change display mode” */
regs.h.al = (char)mode; /* AL=??, where ?? is the Mode number */
regs.x.bx = 0; /* Page number, usually zero */
int86(0x10, &regs, &regs); /* Call the BIOS (int10) */
printf(“Mode 0x%X now active\n”, mode);
printf(“Press any key to return... “);
getch();
regs.h.al = 3; /* return to Mode 3 */
int86(0x10, &regs, &regs);
}

One interesting feature of this particular function that isn’t shown here is the capability to change display modes without clearing the screen. This feature can be extremely useful in certain circumstances. To change modes without affecting screen contents, simply OR hex 80 to the display mode value you place into the AL register. For instance, if you want to switch to mode 13 (hex), you put hex 93 in AL. The remaining codestays unchanged. Today, VGA cards also adhere to the VESA Video BIOS standard in their support of the extended display modes (see the following sidebar for an explanation). However, it requires a new “change display mode” function to support these extended modes. Per the VESA standard, you use function hex 4F rather than function 0 in the preceding example to switch VESA modes. The following example code is a modification of the preceding example to incorporate VESA mode numbers.

#include <stdlib.h>
#include <dos.h>
main(int argc, char ** argv)
{
union REGS regs;
int mode;
/* accept Mode number in hex */
sscanf(argv[1], “%x”, &mode);
regs.x.ax = 0x4F02; /* change display mode */
regs.x.bx = (short)mode; /* three-digit mode number */
int86(0x10, &regs, &regs); /* Call the BIOS (int10) */
if(regs.h.al != 0x4F){
printf(“VESA modes NOT supported!\n”);
}
else{
printf(“Mode 0x%X now active\n”, mode);
printf(“Press any key to return... “);
getch();
}
regs.h.al = 3; /* return to Mode 3 */
int86(0x10, &regs, &regs);
}

Note that this now conflicts with that hex 80 “don’t clear the screen” value. For VESA, it has simply moved from the high-order bit of the two-digit number to the high-order bit of the three-digit value (all VESA modes are three digits in size—again, see the sidebar for details). Therefore, to change to VESA mode 101, you make the VESA mode number 901, and the screen’s contents will be preserved. All About Display Modes IBM created a display mode standard that attempted to define all the display modes that could ever possibly be needed. These included all the possible pixel depths (number of colors) that would ever be needed. So IBM created 19 display modes (numbered from 0 to 13 hex). Table XIV.8a shows the display mode standard.




See anything you recognize? Mode 3 is the 80´25 color text mode that you see when you turn on your PC. Mode 12 is what you get when you select “VGA” as your Microsoft Windows 3.x driver (you know, the one that comes with Windows). Note the lack of any display mode featuring more than 256 colors, or higher than 640´480 resolution. Modes 4, 9, and D for many years were the popular choice of DOS game makers, featuring a “big” resolution of 320´200 and enough colors (4 or 16) to display decent graphics. Mode 13 is the display mode used for just about all the popular action games, including DOOM (I and II), id Software’s new Heretic, Apogee’s Rise of the Triad, Interplay’s Descent, and many others. In truth, many of these action games perform little tricks on the VGA cards that convert Mode 13 into 320´240, with more memory pages to improve graphics appearance and speed—they call it Mode X.

So where did all the other display modes, the ones you’re used to, come from? They were madeup by VGA card manufacturers. The other display modes you might be familiar with (800´600, 1024´768, 1280´1024, and even 1600´1200) come from a “melting pot” of sources, but regardless of their origins, the VGA card makers put them on their VGA cards to increase the cards’ value. Such modes are usually called extended display modes. Thanks to the wonders of competitionand capitalism, the card makers migrated toward these higher display modes. Others have been tried (ever heard of 1152´900?) but weren’t received as well as these modes. OK, so what is VESA, and what does it have to do with VGA cards? Although the VGA card makers all chose to support the same display modes (even the extended ones), each implemented these extended modes in its own proprietary way. Game and productivity software makers were stressed to support each proprietary method of each VGA card on the market. A group of manufacturers and other representatives formed a committee to standardize as much as possible the setup and programming of these cards. VESA (Video Electronics Standards Association) is this committee.

The VESA committee adopted a standard of the extended display modes so that software could make common BIOS calls that would set up and initialize all VGA cards that adhered to this standard. It probably is safe to say that 100 percent of all VGA cards sold in the United States support the VESA standard in one form or another. All VESA modes (which is what the standardized set of display modes is called) utilize mode numbers that are nine bits wide rather than the eight-bits-wide standard mode. Having a nine-bit mode number allows the VESA modes to be three hex digits long rather than two in the IBM standard (the ones from 0 to 13 hex shown in Table XIV.8a), thereby avoiding a numbering conflict. Therefore, all VESA mode values are above 100 hex. Here’s how the VESA modes work: You want to program your VGA card to display 1024´768 at 256 colors, which happens to be VESA mode 105. You make a BIOS call using 105 as the display mode number. The VESA mode number is translated into the internal proprietary number by the Video BIOS (sometimes called the VESA BIOS ) to actually perform the mode switch. VGA card manufacturers supply a VESA BIOS with each VGA card to perform these translations so that all you have to worry about is the VESA number. Table XIV.8b shows the latest list of VESA display modes (it is an ever-evolving standard).


VESA display modes.
--------------------------------------------------------------------------------------------------------------------
Resolution                                      Colors                            VESA Mode
---------------------------------------------------------------------------------------------------------------------
640´400                                             256                               100
640´480                                             256                               101
640´480                                             32,768                           110
640´480                                             65,536                           111
640´480                                             16.7 million                    112
800´600                                             16                                 102
800´600                                             256                                103
800´600                                             32,768                           113
800´600                                             65,536                           114
800´600                                             16.7 million                    115
1024´768                                           16                                 104
1024´768                                           256                               105
1024´768                                           32,768                           116
1024´768                                           65,536                           117
1024´768                                           16.7 million                    118
1280´1024                                         16                                 106
1280´1024                                         256                               107
1280´1024                                         32,768                           119
1280´1024                                         65,536                           11A
1280´1024                                         16.7 million                    11B
-----------------------------------------------------------------------------------------------------------------

Notice that these are the modes you are accustomed to seeing, especially if you use MicrosoftWindows.

Cross Reference:

XIV.6: What are interrupts?

No comments:

Post a Comment