{***********************************************************************}
{									}
{ __EquipScn  Determine which adapters are present			}
{									}
{ procedure __EquipScn; 						}
{									}
{ Description __EquipScn determines which video adapters are installed	}
{	      and the video modes available with the installed equip-	}
{	      ment.  The following Unit_Scn exported global variables	}
{	      are initialized:						}
{									}
{	      _MonoAdapter	 Monochrome adapter			}
{	      _ColorAdpater	 Color/graphics adapter 		}
{	      _EgaAdapter	 EGA adapter				}
{	      _HercAdapter	 Hercules adapter			}
{	      _McgaAdapter	 PS/2 Model 30 adapter			}
{	      _VgaAdapter	 PS/2 Model 50+ adapter 		}
{									}
{	      The following implementation global variables are 	}
{	      initialized:						}
{									}
{	      AvailColorModes_	 Set of modes available for color	}
{	      AvailMonoModes_	 Set of modes available for mono	}
{	      AvailColorRows_	 Set of rows available for color	}
{	      AvailMonoRows_	 Set of rows available for mono 	}
{	      DualDisplay_	 Two adapters in system?		}
{	      EgaMemory_	 Memory installed on EGA (in Kb)	}
{	      EgaMonitor_	 Display attached to EGA board		}
{	      AnalogMonitor_	 Display attached to VGA/MCGA		}
{									}
{	      __EquipScn is called during the Unit_Scn initialization.	}
{									}
{ Version     4.00 (C)Copyright Blaise Computing Inc. 1987		}
{_______________________________________________________________________}

procedure __EquipScn;

  const
    ACTIVE     = 1;		       { Active device (VGA test)	}
    ALTERNATE  = 2;		       { Alternate device		}

  var
    SaveCursor : word;
    CrtPort    : word;
    ADisplay   : byte;
    I	       : integer;
    Regs       : registers;


  begin {__EquipScn}

    _MonoAdapter    := _UNKNOWN;       { Initialize exported globals	}
    _ColorAdapter   := _UNKNOWN;
    _EgaAdapter     := _UNKNOWN;
    _HercAdapter    := _UNKNOWN;
    _VgaAdapter     := _UNKNOWN;
    _McgaAdapter    := _UNKNOWN;
    EgaMonitor_     := _NoMonitor;     { Now implementation globals	}
    AnalogMonitor_  := _NoMonitor;
    EgaMemory_	    := 0;

    { First we try BIOS Video function $10, subfunction $1A ("Read Dis- }
    { play Combination Code").	If on return AL is not equal to $1A,	}
    { the function is not supported, so VGA and MCGA are not be present.}

    with Regs do
      begin
	AX := $1A00;
	BX := $0000;
	Intr(_VIDEO,Regs);
	if (AL = $1A) then
	   begin

	     { Now that we know this function is supported, we can	}
	     { initialize the following the adapter indicators.  Upon	}
	     { return from BIOS subfunction $1A, BL has the display	}
	     { code for the active display, and BH for the alternate	}
	     { display. 						}

	     _MonoAdapter  := _ABSENT;
	     _ColorAdapter := _ABSENT;
	     _EgaAdapter   := _ABSENT;
	     _HercAdapter  := _ABSENT;
	     _McgaAdapter  := _ABSENT;
	     _VgaAdapter   := _ABSENT;

	     ADisplay := BL;
	     for I := ACTIVE to ALTERNATE do
	       begin

		 { Set the adapter presence and then the monitor	}
		 { presence.						}

		 case ADisplay of
		   $01	   : _MonoAdapter  := _MONO;
		   $02	   : _ColorAdapter := _COLOR;
		   $04	   : _EgaAdapter   := _COLOR;
		   $05	   : _EgaAdapter   := _MONO;
		   $07,$08 : if (ADisplay = BL) then
				_VgaAdapter := ACTIVE
			     else
				_VgaAdapter := ALTERNATE;
		   $0B,$0C : _McgaAdapter  := _COLOR;
		 end; {case ADisplay}

		 case ADisplay of
		   $07,$0B : AnalogMonitor_ := _AnMonoMonitor;
		   $08,$0C : AnalogMonitor_ := _AnColorMonitor;
		 end; {case ADisplay}

		 ADisplay := BH        { Now the alternate display	}
	       end;
	   end
	else
	   begin

	     { Because the function is not supported neither the VGA	}
	     { nor MCGA are present.					}

	     _McgaAdapter  := _ABSENT;
	     _VgaAdapter   := _ABSENT

	   end;

	{ Next attempt BIOS Video function $10, subfunction $12 	}
	{ ("Return EGA information").  If we already know the EGA is	}
	{ present, just determine the memory installed and if an	}
	{ Enhanced monitor is attached, otherwise test for the EGA's	}
	{ presence.  If any returned information is invalid, then the	}
	{ EGA must not be present.					}

	AX := $1200;
	BX := $FF10;
	CX := $00FF;
	Intr(_VIDEO,Regs);
	if (_EgaAdapter = _UNKNOWN) then
	   begin
	     if ((CL < 12) and (BH <= 1) and (BL <= 3)) then
		begin		       { The EGA is present		}
		  if (BH = 1) then     { Emulating monochrome adapter,	}
		     begin	       { so the monochrome is absent	}
		       _MonoAdapter  := _ABSENT;
		       _EgaAdapter   := _MONO;
		       EgaMonitor_   := _MonoMonitor
		     end
		  else
		     begin	       { Emulating the color/graphics,	}
				       { so CGA is absent.		}
		       _ColorAdapter := _ABSENT;
		       _EgaAdapter   := _COLOR
		     end
		end
	     else
		_EgaAdapter := _ABSENT { The EGA is not present 	}
	   end;

	if (_EgaAdapter <> _ABSENT) then
	   begin
	     EgaMemory_ := 64 + (64 * BL);  { Memory installed in Kb	}
	     if (((CX and $0009) = 9) or ((CX and $0003) = 3)) then
		EgaMonitor_ := _EnhancedMonitor
	     else if (EgaMonitor_ <> _MonoMonitor) then
		EgaMonitor_ := _ColorMonitor
	   end

      end;

    { Now test for the presence of the monochrome adapter and the	}
    { color/graphics adapter.  It is possible that the test for the	}
    { color/graphics adapter will sense its presence, when in fact,	}
    { the Professional Graphics Adapter is emulating it.  To test the	}
    { presence of an adapter, we write an out of range value into the	}
    { cursor position register of the CRT controller chip, and then	}
    { read it back.  If the same value is found in the register, then	}
    { the chip (and hence the adapter) is there; if it is different,	}
    { then it must be absent.						}

    if (_MonoAdapter = _UNKNOWN) then
       begin			       { First, the monochrome adapter	}
	 CrtPort	   := $3B4;    { CRT port address		}
	 Port[CrtPort]	   := $F;      { Index to cursor register.	}
	 SaveCursor	   := Port[CrtPort + 1];   { Save previous value}
	 Port[CrtPort + 1] := 90;      { Set to column 90		}
	 if (Port[CrtPort + 1] = 90) then
	    _MonoAdapter   := _MONO
	 else
	    _MonoAdapter   := _ABSENT;
	 Port[CrtPort + 1] := SaveCursor
       end;

    if (_ColorAdapter = _UNKNOWN) then
       begin			       { Color/graphics adapter?	}
	 CrtPort	   := $3D4;    { CRT port address		}
	 Port[CrtPort]	   := $F;      { Index to cursor register.	}
	 SaveCursor	   := Port[CrtPort + 1];   { Save previous value}
	 Port[CrtPort + 1] := 90;      { Set to column 90		}
	 if (Port[CrtPort + 1] = 90) then
	    _ColorAdapter  := _COLOR
	 else
	    _ColorAdapter  := _ABSENT;
	 Port[CrtPort + 1] := SaveCursor
       end;

    { Now we count up _Adapters. If the number is greater than _COLOR	}
    { (2) then the system has two displays.				}

    DualDisplay_ := ((_MonoAdapter + _ColorAdapter + _EgaAdapter +
				   _McgaAdapter + _VgaAdapter) > _COLOR);

    { If the VGA adapter is the active device (as determined above), we }
    { assign its value based on the current mode, since it can support	}
    { both monochrome and color modes.					}

    if (_VgaAdapter = ACTIVE) then
       with Regs do
	 begin
	   AX := $0F00; 	       { Return current video mode in AL}
	   Intr(_VIDEO,Regs);
	   if ((AL = 7) or (AL = 15)) then
	      _VgaAdapter := _MONO
	   else
	      _VgaAdapter := _COLOR
	 end;

    { Now that we know what combination of adapters is present, we can	}
    { assign the sets AvailColorModes_, AvailMonoModes_, AvailColorRows_}
    { and AvailMonoRows_.						}

    if (DualDisplay_ = FALSE) then     { A single display is present	}
       begin
	 if (_VgaAdapter <> _ABSENT) then
	    begin
	      AvailColorModes_ := [0..6,13,14,16..19];
	      AvailMonoModes_  := [7,15];
	      AvailColorRows_  := [25,30,43,50];
	      AvailMonoRows_   := [25,43,50]
	    end;
	 if (_McgaAdapter  <> _ABSENT) then
	    begin
	      AvailColorModes_ := [0..6,17,19];
	      AvailMonoModes_  := [];
	      AvailColorRows_  := [25,30];
	      AvailMonoRows_   := []
	    end;
	 if (_ColorAdapter = _COLOR) then
	    begin
	      AvailColorModes_ := [0..6];
	      AvailMonoModes_  := [];
	      AvailColorRows_  := [25];
	      AvailMonoRows_   := []
	    end;
	 if (_MonoAdapter = _MONO) then
	    begin
	      AvailColorModes_ := [];
	      AvailMonoModes_  := [7];
	      AvailColorRows_  := [];
	      AvailMonoRows_   := [25]
	    end;
	 if (_EgaAdapter = _COLOR) then
	    begin
	      if (EgaMonitor_ = _EnhancedMonitor) then
		 AvailColorModes_ := [0..6,13,14,16]
	      else
		 AvailColorModes_ := [0..6,13,14];
	      AvailMonoModes_  := [];
	      AvailColorRows_  := [25,43];
	      AvailMonoRows_   := []
	    end;
	 if (_EgaAdapter = _MONO) then
	    begin
	      AvailColorModes_ := [];
	      AvailMonoModes_  := [7,15];
	      AvailColorRows_  := [];
	      AvailMonoRows_   := [25,43]
	    end;
       end
    else			       { Have dual displays		}
       begin
	 if ((_VgaAdapter = _MONO) and (_ColorAdapter = _COLOR)) then
	    begin
	      AvailColorModes_ := [0..6];
	      AvailMonoModes_  := [7,15];
	      AvailColorRows_  := [25];
	      AvailMonoRows_   := [25,43,50]
	    end;
	 if ((_VgaAdapter = _COLOR) and (_MonoAdapter = _MONO)) then
	    begin
	      AvailColorModes_ := [0..6,13,14,16..19];
	      AvailMonoModes_  := [7];
	      AvailColorRows_  := [25,30,43,50];
	      AvailMonoRows_   := [25]
	    end;
	 if ((_EgaAdapter = _MONO) and (_ColorAdapter = _COLOR)) then
	    begin
	      AvailColorModes_ := [0..6];
	      AvailMonoModes_  := [7,15];
	      AvailColorRows_  := [25];
	      AvailMonoRows_   := [25,43]
	    end;
	 if ((_EgaAdapter = _COLOR) and (_MonoAdapter = _MONO)) then
	    begin
	      if (EgaMonitor_ = _EnhancedMonitor) then
		AvailColorModes_ := [0..6,13,14,16]
	      else
		AvailColorModes_ := [0..6,13,14];
	      AvailMonoModes_  := [7];
	      AvailColorRows_  := [25,43];
	      AvailMonoRows_   := [25]
	    end;
       end;

    { Now test for presence of Hercules Graphics Card, which  if	}
    { present, has already been detected as _MonoAdapter:		}

    if (IsHerc) then
       begin
	 _MonoAdapter := _ABSENT;
	 _HercAdapter := _MONO
       end
    else
	 _HercAdapter := _ABSENT

  end;	{__EquipScn}
