Complete Instructions to BLOAD and BSAVE EGA and VGA Screens

This article was previously published under Q45699
This article has been archived. It is offered "as is" and will no longer be updated.
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.004.00b and 4.50 for MS-DOS; in Microsoft Basic Compiler versions 6.00and 6.00b for MS-DOS; and in Microsoft Basic PDS version 7.00 forMS-DOS.
More information
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 techniquerequired for EGA and VGA screens is more involved than the techniqueused 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 otherscreen modes. EGA and VGA memory (except SCREEN mode 13) is broken upinto four different bit planes: blue, green, red, and intensity.

Each bit plane is addressed in parallel; that is, the planes are allin one location, stacked on top of one another. When manipulating aparticular address in video memory, the address refers to 4 bytes, notjust 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 fourbit planes separately. Each plane can be accessed by placinginformation in registers located on the Graphics Controller.

Programming EGA and VGA graphics depends heavily on the GraphicsController. The Graphics Controller manages the CPU and video memorywhen executing EGA/VGA read and write operations. To access theGraphics 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 theGraphics Controller.

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

Different screen modes have different resolutions. Therefore, variableamounts of EGA/VGA memory must be BLOADed or BSAVEd. The formula forcalculating the number of bytes to be BLOADed or BSAVEd (with theexception 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 numberof pixels in this screen mode is 640 * 350 or 224,000 pixels/bits. Toobtain 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 isnot used. As in other non-EGA/VGA modes, a contiguous block of memoryis BLOADed or BSAVEd. SCREEN mode 13 has a resolution of 320 x 200with 256K colors (1 byte per pixel); therefore, 64,000 bytes need tobe saved or loaded.
The following are excellent resources for detailed informationconcerning 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 thatCALLs the BLOAD and BSAVE EGA/VGA subprogram EgaVgaSub further below.The EgaVgaSub subprogram module can be easily pasted into your ownprograms. MAIN.BAS is as follows:
DECLARE SUB EgaVgaSub (FileName$, Mode AS INTEGER, RW AS INTEGER)'********************************************************************' 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.END TYPEDIM ScreenRec AS ModeTypeDIM ReadWrite AS INTEGERCLSPRINT "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: ", ReadWritePRINT : PRINT "Enter the filename (maximum 7 characters) to BLOAD or"PRINT "BSAVE EGA screen data to or from (do not include extension):";INPUT FileName$CLS         'CHANGE AS NEEDED: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), coNEXT iCallSub: CALL EgaVgaSub(FileName$, ScreenRec.Mode, ReadWrite)INPUT "Press <ENTER> to restore screen ...", a$ENDSUB EgaVgaSub (FileName$, mode AS INTEGER, RW AS INTEGER) STATIC' ---------------------------------------------------------------' 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  CASE ELSE       PRINT "ERROR: Non EGA/VGA graphics mode!"       GOTO NonEGAorVGAEND SELECTIF mode = 10 THEN    'SCREEN mode 10 only has two bit planes   cycle = 1         'because it is used on a monochrome display.ELSE   cycle = 3         'SCREEN modes 7, 8, 9, 11, and 12 have fourEND IF               'bit planes.DEF SEG = &HA000   'Define the segment for EGA/VGA graphics.                   'BSAVEing and BLOADing SCREEN mode 13 does notIF 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. END IFELSE 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 NEXT iEND IFDEF SEG                      'Restore the segment.NonEGAorVGA:END SUB				
QuickBas BasicCom

Article ID: 45699 - Last Review: 10/26/2013 11:39:00 - Revision: 3.0

Microsoft BASIC Professional Development System 7.0

  • kbnosurvey kbarchive KB45699