/*========================================================================*/
/*  The following example routine has been provided by the Technical      */
/*  Support staff at Borland International.  It is provided as a          */
/*  courtesy and not as part of a Borland product, and as such, is        */
/*  provided without the assurance of technical support or any specific   */
/*  guarantees.                                                           */
/*========================================================================*/

/*
                        VIDEO ADAPTER DETECTION ROUTINE
                        -------------------------------
The following code is an example for detecting video setup.   The code
returns specific values which allows one to identify the Video Adapter
(MDA, CGA, HERCULES, EGA, PGA, VGA and MCGA) as well as the Monitor,
whenever appropriate ( MONOCHROME, COLOR, TTL or ANALOG).  Information used
in the code was obtained from:

    (i)     HelpPC  ( Quick PC Reference Utility (c) David Jurgens)
    (ii)    Hercules Technologies Reference Manual (c) Hercules
    (iii)   PC System Programming by M. Tischer - Abacus
    (iv)    IBM BIOS Technical Reference Manual - IBM

Since the proper hardware setup was not readily available, the code
has not been tested on all the video systems for which a detection
routine is provided.


*/



#include <stdio.h>
#include <stdlib.h>
#include <dos.h>

#define         NO_DISPLAY      0
#define         MDA             1
#define         CGA             2
#define         HERCULES        3
#define         EGA_COLOR       4
#define         EGA_MONO        5
#define         PGA             6
#define         VGA_MONO        7
#define         VGA_COLOR       8
#define         MCGA_COLOR_TTL  9
#define         MCGA_MONO       10
#define         MCGA_COLOR_ANA  11

static char *display[]=
             {
               "Unknown Video System",
               "Monochrome MDA",
               "Color CGA",
               "Hercules",
               "Color EGA",
               "Monochrome EGA",
               "Color PGA",
               "Monochrome VGA",
               "Color VGA",
               "Digital Color MCGA",
               "Monochrome MCGA",
               "Analog Color MCGA"

#                                                                        Page 3

             };

int VideoInfo( void )
{
    /* ------------------------------------------------------------------- 
*/
    /* The following Code verifies whether Function 1Ah of Interrupt 10h   
*/
    /* is supported.   This Interrupt (documented as supported only on     
*/
    /* VGAs) returns the Video Display Combination...                      
*/
    /* ------------------------------------------------------------------- 
*/
    _AX = 0x1A00;                   /* Function 1Ah Of INT 10h             
*/
    geninterrupt( 0x10 );           /* Call Video Interrupt (BIOS)         
*/
    if ( _AL == 0x1A )              /* AL == 1Ah => Supported Function     
*/
    {
         if ( _BL > 0x09 )          /* BL contains the Primary Adapter     
*/
              _BL--;                /* Information                         
*/
         return( int )_BL;          /* Return Primary Adapter Info..       
*/
    }

    /* ------------------------------------------------------------------- 
*/
    /* If the detection for VGA fails, we proceed to test for the presence 
*/
    /* of an EGA adapter.   Function 12h of Interrupt 10h is only 
supported*/
    /* by EGAs (and above);  It returns the Video Subsystem Configuration. 
*/
    /* 
--------------------------------------------------------------------*/
    _AH = 0x12;                     /* Try EGA verification (Func 12h)     
*/
    _BL = 0x10;                     /* Video Config. info. request         
*/
    _CX = 0x0000;                   /* Clear CX to verify support..        
*/
    geninterrupt( 0x10 );           /* Call Video Interrupt (BIOS)         
*/
    if ( _CX != 0x0000 )            /* If CX != 00 => Func. is supported   
*/
    {
        if ( _BH == 0x00 )          /* BH == 00    => Color sys. active    
*/
            return ( EGA_COLOR );
        else
            return ( EGA_MONO  );   /* BH == 01    => Monochrome sys       
*/
    }

    /* ------------------------------------------------------------------- 

#                                                                        Page 4

*/
    /* The following querries BIOS via the Equipment Determination INT     
*/
    /* The return value enables us to determine the presence of either a   
*/
    /* Monochrome or Color system.                                         
*/
    /* ------------------------------------------------------------------- 
*/
    geninterrupt( 0x11 );               /* Get Equipment Configuration INT 
*/
    _AX &= 0x0030;                      /* Clear all bits but 4-5          
*/
    if ( _AX  !=  0x0030 )              /* If one of the two bits is set.. 
*/
        return ( CGA );                 /*      then its a Color (CGA)     
*/

    /* ------------------------------------------------------------------- 
*/
    /* The following Code (provided by Hercules Technologies on the        
*/
    /* Companion Disk of the Hercules Technical Ref.) identifies a 
Hercules*/
    /* Graphics Card which at the BIOS level appears just like a regular   
*/
    /* MDA                                                                 
*/
    /* ------------------------------------------------------------------- 
*/
    _DX = 0x3BA;                        /* Load Display Status Port        
*/
    _BL = 0x00 ;                        /* Clear Counter                   
*/
    __emit__(0xEC);                     /* ASM   IN    AL, DX  :: Read 
Port*/
    _AL &= 0x80;                        /* Clear all bits save bit 7       
*/
    _AH = _AL;                          /* Save Value of AL in AH          
*/
    _CX = 0x8000;                       /* Set a big loop counter..        
*/
    while( _CX > 0x0000 )
    {
        __emit__(0xEC);                 /* Read the Port DX into AL..      
*/
        _AL  &=  0x80 ;                 /* Clear all bits save bit 7       
*/
        if (_AL != _AH )                /* Compare the bits..              
*/
        {
            _BL++;                      /* Increment BL register/counter   
*/
            if ( _BL >= 10 )            /* Have we seen to successes yet ? 
*/
                return( HERCULES );     /* Found an Hercules...            
*/
        }
        _CX--;

#                                                                        Page 5

    }
    return( MDA );                      /* Hmm. guess it's just an Ol' MDA 
*/
}                                       /* ****  God bless its heart! **** 
*/

int main(void)
{
    printf("VIDEO ADAPTER DETECTION TEST\n");
    printf("----- ------- --------- ----\n\n");
    printf("Current Video System is: %s\n\n", display[ VideoInfo() ]);
    return 0;
}

