Microsoft KB Archive/45699

From BetaArchive Wiki
Knowledge Base

Article ID: 45699

Article Last Modified on 8/16/2005


  • Microsoft QuickBasic 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBasic 4.5 for MS-DOS
  • Microsoft BASIC Compiler 6.0
  • Microsoft BASIC Compiler 6.0b
  • Microsoft BASIC Professional Development System 7.0

This article was previously published under Q45699


This article describes how to BLOAD and BSAVE EGA and VGA SCREEN modes (7, 8, 9, 10, 11, 12, and 13) in Microsoft QuickBasic versions 4.00 4.00b and 4.50 for MS-DOS; in Microsoft Basic Compiler versions 6.00 and 6.00b for MS-DOS; and in Microsoft Basic PDS version 7.00 for MS-DOS.


SCREEN modes 11, 12, and 13 require a VGA card, and SCREEN modes 7, 8, 9, and 10 require an EGA or VGA card.

Using QuickBasic or the Basic compiler, the BLOAD and BSAVE technique required for EGA and VGA screens is more involved than the technique used for CGA and Hercules screen modes. EGA and VGA screen modes 7, 8, 9, 10, 11, and 12 are stored differently in video memory than other screen modes. EGA and VGA memory (except SCREEN mode 13) is broken up into four different bit planes: blue, green, red, and intensity.

Each bit plane is addressed in parallel; that is, the planes are all in one location, stacked on top of one another. When manipulating a particular address in video memory, the address refers to 4 bytes, not just 1 (1 byte in each bit plane). To read or write EGA/VGA screens, we need to send or retrieve information to or from each of the four bit planes separately. Each plane can be accessed by placing information in registers located on the Graphics Controller.

Programming EGA and VGA graphics depends heavily on the Graphics Controller. The Graphics Controller manages the CPU and video memory when executing EGA/VGA read and write operations. To access the Graphics Controller, sending output to a set of ports is necessary. Two registers are important when reading and writing EGA/VGA screens: map mask and read map select. These registers are located on the Graphics Controller.

Accessing the map mask and read map registers requires writing to two ports. Writing to the first port (the address register) is necessary to select the desired register, and the second (the data register) to send information to the selected register. The second port is used by all the registers. To select the map mask register for writing (i.e., BLOADing) EGA/VGA screens, send a 2 (the index for the map mask register) to port &H3C4 (the sequencer address register). Then, put the bit plane number (0 to 3) you want to write to in port &H3C5 (the data register). To select the read map register for reading (i.e., BSAVEing) EGA/VGA screens, send a 4 (the index for the read map register) to port &H3CE (the graphics address register). Then, put the bit plane number you want to read in port &H3CF (the data register). This procedure is demonstrated in the subprogram, GRAPHSUB.BAS, shown below.

Different screen modes have different resolutions. Therefore, variable amounts of EGA/VGA memory must be BLOADed or BSAVEd. The formula for calculating the number of bytes to be BLOADed or BSAVEd (with the exception of SCREEN mode 13) is as follows:

   Number of bytes = (Maximum y-coordinate)*(Maximum x-coordinate)/8

For example, SCREEN mode 9 has a resolution of 640 x 350. The number of pixels in this screen mode is 640 * 350 or 224,000 pixels/bits. To obtain the number of bytes to save or load, divide this number by 8. Therefore, the number of bytes to save or load is 28,000.

In SCREEN mode 13 (VGA medium resolution), the Graphics Controller is not used. As in other non-EGA/VGA modes, a contiguous block of memory is BLOADed or BSAVEd. SCREEN mode 13 has a resolution of 320 x 200 with 256K colors (1 byte per pixel); therefore, 64,000 bytes need to be saved or loaded.


The following are excellent resources for detailed information concerning EGA and VGA screen modes and how to program for them:

"Programmer's Guide to PC and PS/2 Video Systems" by Richard Wilton, published by Microsoft Press (1987)

"The New Peter Norton Programmer's Guide to the IBM PC and PS/2" by Peter Norton, published by Microsoft Press (1988)

"The Waite Group's Microsoft C Programming for the PC Revised Edition" by Robert Lafore, published by The Waite Group, Inc. (1989)

"The Programmer's PC Sourcebook" by Thom Hogan, published by Microsoft Press (1988)


Below is a Basic program, MAIN.BAS, which is a driver program that CALLs the BLOAD and BSAVE EGA/VGA subprogram EgaVgaSub further below. The EgaVgaSub subprogram module can be easily pasted into your own programs. MAIN.BAS is as follows:

' Sample program that calls the EgaVgaSub subprogram. This program
' works with QuickBasic versions 4.00, 4.00b, and 4.50, Microsoft
' Basic Compiler versions 6.00 and 6.00b for MS-DOS, and Microsoft
' Basic PDS version 7.00 for MS-DOS.

TYPE ModeType            'Type to describe the SCREEN mode selected.
     MaxX AS INTEGER     'Maximum value of x coordinate.
     MaxY AS INTEGER     'Maximum value of y coordinate.
     Mode AS INTEGER     'Mode selected in the SCREEN statement.
     NoColors AS INTEGER 'Max. # of colors available in selected mode.

DIM ScreenRec AS ModeType

PRINT "Please input the correct options below.  NOTE: if BLOADing a"
PRINT "file, please make sure the file exists in the current"
PRINT "directory or the program may hang!"
PRINT : PRINT "Option to BLOAD or BSAVE EGA screen data files--"
INPUT "Enter (1) for BLOAD or (2) for BSAVE: ", ReadWrite

PRINT : PRINT "Enter the filename (maximum 7 characters) to BLOAD or"
PRINT "BSAVE EGA screen data to or from (do not include extension):";
INPUT FileName$

SCREEN 9 'SCREEN statement that invokes EGA 640x350 16 color mode.
         'Can substitute mode 9 with mode 7, 8, 10, 11, 12 or 13 here.

ScreenRec.MaxX = 640  'CHANGE AS NEEDED:
                      ' Maximum x coordinate for mode 9 is 640.
ScreenRec.MaxY = 350  'CHANGE AS NEEDED:
                      ' Maximum y coordinate for mode 9 is 350.
ScreenRec.Mode = 9    'CHANGE AS NEEDED: Mode selected in the SCREEN
                      ' statement.
ScreenRec.NoColors = 15   'CHANGE AS NEEDED:
                          ' Maximum number of colors available in
                          ' mode 9 (0 to 15 colors).

IF ReadWrite = 1 THEN GOTO CallSub 'BLOAD- no need to output graphics.

FOR i = 1 TO 200          'Generate random lines in random colors.
    x1 = INT(ScreenRec.MaxX * RND)
    y1 = INT(ScreenRec.MaxY * RND)
    x2 = INT(ScreenRec.MaxX * RND)
    y2 = INT(ScreenRec.MaxY * RND)
    co = INT(ScreenRec.NoColors * RND)
    LINE (x1, y1)-(x2, y2), co

CallSub: CALL EgaVgaSub(FileName$, ScreenRec.Mode, ReadWrite)

INPUT "Press <ENTER> to restore screen ...", a$

' ---------------------------------------------------------------
' EgaVgaSub is a SUBprogram that will BSAVE or BLOAD a given
' file to or from SCREEN mode 7, 8, 9, 10, 11, 12 or 13.
' SCREENs 11, 12, and 13 require a VGA card, and
' SCREENs 7, 8, 9, and 10 require an EGA or VGA card.
' EgaVgaSub will produce four files with extension .GRA which
' contain graphics information from each bit plane (with the
' exception of SCREEN modes 10 and 13).
' Variable:
' FileName$-- the name of the file to be BLOADed or BSAVEd.
' Mode     -- the SCREEN mode being used.
' RW       -- the choice to BLOAD or BSAVE the file.  If RW=1,
'             then the file is BLOADed.  Otherwise, the file is
'             BSAVEd.
' Compatibility:
' This subprogram works with QuickBasic versions 4.00, 4.00b and
' 4.50, Basic compiler 6.00 and 6.00b for MS-DOS and Basic PDS 7.00
' for MS-DOS.
' ---------------------------------------------------------------

SELECT CASE mode     'Determine how much to BSAVE.

'Mode 7 is 320x200- save/load 8000 bytes.
'Mode 8 is 640x200- save/load 16000 bytes.
'Modes 9 and 10 are 640x350- save/load 28000 bytes.
'Modes 11 and 12 are 640x480- save/load 38400 bytes.
'Mode 13 is 320x200x(1byte/256 colors)- save/load 64000 bytes.

  CASE 7
       total! = 8000
  CASE 8
       total! = 16000
  CASE 9 TO 10
       total! = 28000
  CASE 11 TO 12
       total! = 38400
  CASE 13
       total! = 64000
       PRINT "ERROR: Non EGA/VGA graphics mode!"
       GOTO NonEGAorVGA

IF mode = 10 THEN    'SCREEN mode 10 only has two bit planes
   cycle = 1         'because it is used on a monochrome display.
   cycle = 3         'SCREEN modes 7, 8, 9, 11, and 12 have four
END IF               'bit planes.

DEF SEG = &HA000   'Define the segment for EGA/VGA graphics.
                   'BSAVEing and BLOADing SCREEN mode 13 does not
IF mode = 13 THEN  'require the use of the graphics map register.
 IF RW = 1 THEN                   'BLOAD the file.
   f$ = FileName$ + "0" + ".GRA"  'Load the file into VGA memory.
   BLOAD f$, 0                    '0 is the offset to page 0.
 ELSE                             'BSAVE the file.
   f$ = FileName$ + "0" + ".GRA"  'Save VGA memory in a file.
   BSAVE f$, 0, total!        'Save the visual page, at offset 0.

 FOR i = 0 TO cycle     'Cycle through each bit plane of EGA/VGA.
     IF RW = 1 THEN          'BLOAD files.
        OUT &H3C4, 2         'We want to index the map register.
        OUT &H3C5, 2 ^ i     'Bit plane we want to reference.

        'Load each file into its corresponding bit plane.
        f$ = FileName$ + CHR$(i + 48) + ".GRA"
        BLOAD f$, 0          '0 is the offset to page 0.
     ELSE                    'BSAVE files.
        OUT &H3CE, 4         'Select Read Map Select Register.
        OUT &H3CF, i         'Select the bit plane to save.

        'Save each bit plane in its own file.
        f$ = FileName$ + CHR$(i + 48) + ".GRA"
        BSAVE f$, 0, total!  'Save the visual page, at offset 0.
     END IF

DEF SEG                      'Restore the segment.


Additional query words: QuickBas BasicCom

Keywords: KB45699