;=======================================================================
;   Copyright (C) Copr. 1990 by Sidney J. Kelly
;                   All Rights Reserved.
;           Sidney J. Kelly
;           150 Woodhaven Drive
;           Pittsburgh, PA 15228
;           home phone 412-561-0950 (7pm to 9:30pm EST)
;-----------------------------------------------------------------------
; Function prototype in QBASIC
; DECLARE FUNCTION VideoStat% ()
;
; VideoStatus Analyzer
; Program returns video information for use by QBASIC
; returns highest level of SCREEN mode supported by the Hardware.
; Returns information about the PRIMARY display only
;
; Exceptions:
; MCGA color returns a mode 11, so can contrast w/ VGA
; HERC w/o MSHERC.COM reports a -3 so you can tell user to correct matters
; COMPAQ with CGA reports -2 so can force use of monchrome attributes.
; EGA's with CGA diplays report -8 if have 64kb of memory
; EGA's with EGD display with only 64kb of memory report 8
;
; Returns:
; Mono         =   0
; CGA          =   2
; PGA          =   2   (Untested, assume PGA would be treated as CGA)
; COMPAQ - CGA =  -2   to tell that user may have a single color display
; HERC         =   3   if have HERC and MSHERC.COM
; HERC         =  -3   if have HERC but w/o MSHERC.COM
; AT&T         =   4,  but only if have just CGA display, not EGA or VGA
;     Microsoft QuickC/C makes Olivetti EGA and VGA return a separate code
;     I don't know why anyone would care if the EGA/VGA emulation is good.
;
; EGA64        =   8   Because resolution limited for 64k EGA
; EGA64 w/CGA  =  -8   Because resolution limited to CGA modes
; EGA          =   9   Have minimum of 128kb of video ram
; EGA w/CGA    =  -9   Since resolution limited to CGA modes
; MCGA         =  11   If have an analog color display
; MCGA         = -11   If have an EGA EGD digital color display, since
;                      resolution of this display is limited to EGA modes
; VGA          =  13
; EGA/VGA mono =  10   I guess will catch MCGA mono too
;
; Updated  8/9/90
; Version 0.9
;
; Tested:     CGA, HERC clone, EGA mono, EGA color, VGA color, COMPAQ CGA,
;             VGA clone, EGA clone (ACER, ATI EGA Wonder)
; Not tested: ATT, PGA, VGA monochrome, MCGA (color or monochrome)
;=======================================================================


DOSSEG          ;I have had problems unless this is used
.model medium   ;I use QUICK C / QUICK ASM which uses MASM 5.1 simplified
.code           ;directives

     public VIDEOSTAT, INCOLOR

HERC_PORT       Equ     03bah       ; Herc display status port
COMPAQ          DB     'COMPAQ'     ; BIOS ID string in COMPAQ's
COMPAQ_LEN      Equ     6           ; length of string
ATT             DB     'OLIVETTI'   ; BIOS ID string for OLIVETTI/AT&T
ATT_LEN         Equ     8           ; length of string

; Please do not remove
Copyright       DB    13,10,'Copyright Copr. (C) 1990 Sidney J. Kelly'13,10'
Copyright1      DB    'All Rights Reserved',13,10,26

VIDEOSTAT   PROC far
     Push    BP                 ; save BP because old PC Video BIOS
                                ; didn't necessarily preserve this
     Push    ES                 ; save all registers used
     Push    DI
     Push    SI
                                ; this just tests for primary display
vgatest:                        ; because that is all QBASIC cares about
     Mov     AX,1A00h
     Mov     BX,10h             ; Test for VGA adapter
     Int     10h
     Cmp     AL,1Ah
     Jne     egatest            ; VGA does not exist so test for EGA
                                ; VGA exists, primary display info
     Cmp     BL,8               ; routine returns primary display in BL
     Jne     @F
     Jmp     Short  vga_color   ; VGA color exists. This does not test
                                ;  if VGA adapter clone is in emulation
                                ;  mode, such as HERC emulation
                                ;  because QBASIC does not check
                                ;  for this either.
@@:
     Cmp     BL,7
     Jne     @f
     Jmp     vga_mono           ; VGA mono exists, no effort is made to
                                ; distinguish this from EGA MCGA mono
@@:
     Cmp     BL,11
     Jne     @f
     Jmp     vga_mono           ; MCGA mono exists
@@:
     Cmp     BL,12
     Jne     @f
     Jmp     Short mcga_color   ; MCGA analog color exists
@@:
     Cmp     BL,10
     Jne     @f
     Jmp     Short mcga_ecd     ; MCGA w/EGA display
@@:
     Cmp     BL,4               ; EGA color screen found, we have to
     Jne     @f                 ; test if EGA is attached to EGD
     Jmp     Short egatest      ; monitor or just plain CGA monitor
@@:
     Cmp     BL,5
     Jne     @f
     Jmp     Short vga_mono     ; EGA mono screen found
@@:
     Cmp     BL,2
     Jne     @F
     Jmp     cga                ; CGA color found
@@:
     Cmp     BL,1
     Jne     @f
     Jmp     herctest           ; MDA mono found
@@:
     Cmp     BL,6               ; test for PGA, treat as a CGA
     Jne     @f
     Jmp     cga                ; I guess this should work, untested
        
@@:                             ; With everything else
     Jmp     unknown            ; merely assume can do normal b&w text

egatest:
     Mov     AX,1200h
     Mov     BX,10h
     Int     10h
     Cmp     BL,10h             ; EGA changes BL from 10h
     Je      cgatest
     Push    BX                 ; Save BX temporarily
     Xor     AX,AX
     Mov     ES,AX
     Mov     BX,ES:[0487h]      ; check if EGA is the active monitor
     And     BX,8               ; look at bit 3
     Cmp     BX,1               ; if bit set then EGA is inactive
     Jne     @f
     Pop     BX
     Jmp     Short cgatest      ; if EGA not active (primary) check CGA
@@:
     Pop     BX
     Cmp     BH,0               ; if BH = 1 then have MONO display
     Je      ega_color
     Jmp     Short vga_mono

vga_color:
     Mov     AX,13              ; max screen mode is QBASIC SCREEN 13
     Jmp     Quit

mcga_color:
     Mov     AX,11              ; near max screen mode is QBASIC SCREEN 11
     Jmp     Quit               ; MCGA can do SCREEN 13 too, but not 12.

mcga_ecd:
     Mov     AX,-11             ; a warning that MCGA has EGA display
     Jmp     Quit               ; attached (Untested)

ega_color:
;*********** Based on information returned by AH 12h, BL 10h of INT 10h,
;*********** this routine reads the EGA Sense switches stored in CL
;*********** to see if really have a primary monitor that can do EGA graphics.
;*********** If do not have an EGD/multisync display, report a -9.
;*********** If have a 64kb EGA reports 8 if have EGD or -8 if have CGA
;*********** It is unclear if this will work on all clone EGA's.
;*********** It works with a VGA clone and ATI EGA Wonder as well as an
;*********** ACER EGA clone.
;*********** CL information derived from RAM memory at 0040:0088h
;***********
;*********** Source of CL information:
;*********** Ferro, "Programmer's Guide to the EGA and VGA Cards,"
;*********** (Addison-Wesley 1988), page 337.  Yet on page 473 says you can't
;*********** tell what CL will report.
;*********** Similar information hidden in Wilton, "Programmer's Guide to
;***********   PC & PS/2 Video Systems" (Microsoft 1987), page 520.
;***********
;***********  Wilton's routine also appears in other books,
;***********  see Tisch's (?) DOS system developer's book published by ABACUS
;***********
;*********** See also, Woram, "PC Configuration Book" (Bantam 1987),
;***********   pages 279-280 Provides listing of IBM official switch
;***********   settings which does not seem to match anything
;***********   in the above two books.
    
     Cmp     CL,9               ; read EGA Sense switches to see if
                                ; have EGD display and not a CGA display
     JE      ega1
     Mov     AX,-9              ; report have a CGA type display
     Cmp     BL,0
     JNE     @f
     Inc     AX                 ; report have a CGA display w/ 64k Memory
@@:
     Jmp     Quit               ; with an EGA adapter

ega1:
     Mov     AX,9               ; max screen mode is QBASIC SCREEN 9
     Cmp     BL,0
     JNE     @f
     Dec     AX                 ; report have EGA w/ EGD display
                                ; but only 64KB memory (makes 9 an 8)
@@:
     Jmp     Short Quit

vga_mono:
     Mov     AX,10              ; max screen mode is QBASIC SCREEN 10
     Jmp     Short Quit         ; may also be a MCGA or EGA mono too.
                                ; since QBASIC does not distinguish
                                ; I don't either
cgatest:
     Xor     AX,AX
     Mov     ES,AX
     Mov     BX,ES:[0463h]      ; check if MONO port in use
     Cmp     BL,0b4h
     Je      herctest           ; this test looks for Mono port address

att_test:
;******* The AT&T CGA is just a CGA that responds to a special mode set
;******* command.  Mode 48h is equivalent to SCREEN 4.  It is possible
;******* to load BIOS ram with this screen mode and fool QBASIC.  Try it and
;******* you will get some weird displays on non-AT&T displays.
;******* Unknown if that "gamemanship" may hurt your monitor.
;*******
;******* Cannot merely attempt to change to mode &H48 to test for AT&T
;******* because some old CGA's (e.g. Tecmar Color Graphics Card) will let you
;******* change mode number to anything you try.  However HERC clones,
;******* EGA clones and VGA clones will only allow a change to a supported
;******* video mode so such a test would work with them, but would be
;******* worthless unless you could be confident that all CGA's were new.

     Mov     AX,0F000h          ; Get ROM ID segment
     Mov     ES,AX              ; Store segment in ES
     CLD                        ; clear the direction flag
     Mov     DI,0C050h          ; point DI at ROM
     Push    DS                 ; Save DS
     Mov     AX,CS
     Mov     DS,AX

     Assume  DS:@code

     Mov     SI,OFFSET ATT      ; DS:SI points to  "OLIVETTI".
     Mov     CX,ATT_LEN         ; CX has length of "OLIVETTI".
     REPE    CMPSB              ; ES:DI destination, DS:SI source

     Jne     compaq_test        ; if no change test for COMPAQ

att_found:
     ; program assumes that if a CGA is active on an AT&T
     ; that built-in adapter is in use.

     Pop     DS                 ; restore DS

     Assume  DS:@data

     Mov     AX,4               ; max screen mode is QBASIC SCREEN 4
     Jmp     Short Quit

compaq_test:
     ; Purpose of this test is to allow other routines to know if
     ; "snow" testing is necessary, as well as to tell programmer
     ; to use mono attributes in program rather than color attributes
     ; which would confuse the viewer of a COMPAQ single color display.

     Mov     DI,0FFEAh          ; Get ID offset & store in DI
     Mov     SI,OFFSET COMPAQ   ; DS:SI points to  "COMPAQ".
     Mov     CX,COMPAQ_LEN      ; CX has length of "COMPAQ".
     REPZ    CMPSB              ; ES:DI destination, DS:SI source
     Pop     DS                 ; restore DS

     Assume  DS:@data

     JZ      found_compaq       ; Zeroflag cleared if no match found

cga:
     Mov     AX,2               ; max screen mode is QBASIC SCREEN 2
     Jmp     Short Quit

found_compaq:
     ; Program assumes that if a CGA is active on a COMPAQ
     ; that built-in single color display is in use because
     ; even though it does not have color it has better
     ; text resoluton.

     Mov     AX,-2              ; return -2 for COMPAQ in CGA mode
     Jmp     Short Quit         ; to tell us that user has a single color
                       ; display
herc:
     Mov     AX,0EF00h          ; test for MSHERC.COM
     Int     10h
     Cmp     DX,0100h           ; **this is an undocumented call and may
                                ; change ******!!!!!
     Jne     No_MSHerc
     Mov     AX,3               ; max screen mode is QBASIC SCREEN 3
     Jmp     Short Quit         ; wont report SCREEN 3 if MSHERC not
                                ; installed
No_MSHerc:
     Mov     AX,-3              ; report MSHERC.COM is not installed
     Jmp     Short Quit         ; with a -3

herctest:
     ;*****Main Herc test entry point****
     ; Herc display changes Vertical retrace bit during vertical retrace
     ; however normal MDA display does not.
     ; Source for HERC test only:
     ; Hercules Computer Technology, Inc. and Doty,
     ; "Programmer's Guide to Hercules Graphics Cards"
     ; (Addison-Wesley 1988) page 138. 
     ; Similar routines have been  published in PC Magazine
     ; and Young, "Inside DOS: A Programmer's Guide", (Sybex 1990)
     ; page 178, as well as Young's book on OS/2.
     ;
     ; No effort is made to test for Herc INCOLOR or any other HERC
     ; displays since QBASIC would not use the information.
     ; If you must have it, see Doty or Wilton's books listed above.

                                ; test the HERC_Port
     Mov     DX,HERC_PORT       ; load DX with display status port
                                ; a Read-only port
     Xor     BX,BX              ; clear counter
     IN      AL,DX              ; read port
     And     AL,80h             ; mask off every bit but bit 7
     Mov     AH,AL              ; store in AH original status of bit
     Mov     CX,8000h           ; load loop counter

herc1:
     IN      AL,DX              ; read byte
     And     AL,80h             ; clear everything but bit 7
     Cmp     AL,AH              ; keep testing for change in bit 7
     Je      herc2
     Inc     BL                 ; for every change Inc BL
     Cmp     BL,12              ; check routine found vertical
                                ; retrace changed 12 times
     JA      herc               ; found a herc so test for MSHERC

herc2:
     Loop    herc1              ; Herc display not found

unknown:                        ; Handles mono displays too
     Xor     AX,AX              ; return a 0 if just normal text display

Quit:
     Pop     SI                 ; restore saved registers
     Pop     DI
     Pop     ES
     Pop     BP
     Ret
VIDEOSTAT       ENDP

;=======================================================================
; DECLARE FUNCTION INCOLOR%()
;       IF INCOLOR% THEN
;           ... etc.
; Tests if have a color or mono display attached
; returns          -1 if color dsplay (may be a single color display)
;                   0 if BW mono display
; Note will not be fooled by Herc CGA emulation routines.
;
; updated 8/9/90
; Version 0.3
;=======================================================================

INCOLOR PROC   FAR
     Push    ES
     Xor     BX,BX       ;set ES to Ram BIOS area
     Mov     ES,BX
     Mov     BX,ES:[0463h] ;look a port info stored in RAM bios
                         ; at 0000:0463h
     Xor     AX,AX       ;Assume a Mono Display
     Cmp     BL,0B4h
     Je      Done        ;test port address
     Mov     AX,-1       ;COLOR ADAPTER
Done:
     Pop     ES
     Ret
INCOLOR ENDP
END

;----------------------------------------------------------------------
; If you read this far, you are a scholar.  If you really want a good
; video book I would suggest that you buy Michael Abrash's On Graphics
; book about EGA/VGA display's, published by QUE, very advanced.
; For useful information,-- what does this port do--, for all display
; types, Wilton's book is very good.  Ferro's book has more specific
; EGA/VGA information but book is a little disorganized.
;           Sidney J. Kelly
;           150 Woodhaven Drive
;           Pittsburgh, PA 15228
;           home phone 412-561-0950 (7pm to 9:30pm EST)
;----------------------------------------------------------------------

