;       SCCSID = @(#)i_devct3.asm	6.1 91/02/15
;      SCCSID = @(#)i_devct3.asm	1.4 90/01/22
; ****************************************************************************
; *									     *
; *			  IBM/Microsoft Confidential			     *
; *									     *
; *		    Copyright (c) IBM Corporation  1987, 1990		     *
; *		    Copyright (c) Microsoft Corp.  1987, 1990		     *
; *			      All Rights Reserved			     *
; *									     *
; ****************************************************************************
;***  i_devct3.asm - routines for category 3 IOCTL calls
;
;	Internal routines used by API IOCTL library routines
;	David Silverstein
;	Copyright MicroSoft Corporation, 1986
;	11-May-1986
;
;	This file contains support routines for screen control IOCTLs
;

include basemaca.inc
include	dos2equ.inc
include	error.inc
include	d_ioctl.inc


; These constants are declared here only temporarily until a common include file
; is available.

BASE_MONO	equ	0b0000000h
BASE_CGA	equ	0b8000000h
BASE_EGA	equ	0C0000000h
SIG_EGA		equ	0aa55h

D5_GetScrnBufSelec	equ	70h		; Get Screen Buffer Selector
B_WriteDot		equ	12h		; BIOS write dot function
BIOS_VIDEO_IO		equ	10h		; BIOS video io call

_TEXT	segment	byte public 'CODE'

	assume	CS:_TEXT, DS:nothing

	PUBLIC	SCREENCTRL

MonoBaseAddrs	dd	BASE_MONO	; far ptr to monochrome card base addrs
EgaBaseAddrs	dd	BASE_EGA	; far ptr to EGA card base address
CgaBaseAddrs	dd	BASE_CGA	; far ptr to CGA card base address

SCREENCTRL	proc	near

	mov	ax,[Function]			; get function code
	and	ax,NOT IGNORE_ERROR		; mask off error action bit
	cmp	ax,D5_GetScrnBufSelec		; is this get screen buffer
						; selector?
	je	scrnctrl1			; go process
	mov	ax,error_invalid_function	; all others are invalid
	jmp	short scrnctrl2			; go exit

scrnctrl1:

; Here we find out if there is an EGA, CGA, or MONO card currently installed.
; NOTE: The EGA could be pretending to be a CGA.

	push	es

	les	bx,_TEXT:[MonoBaseAddrs]	; get monochrome card base addrs

	mov	al,es:[bx]			; get byte, if present
	not	al				; make it different
	mov	ah,al				; save it
	mov	es:[bx],al			; attempt write to screen memory
	mov	al,es:[bx]			; get it back
	cmp	ah,al				; did the value change?
	jne	gsbs1				; no, no screen card here
	not	ah				; get back original byte
	mov	es:[bx],ah			; restore it
	jmp	short gsbs3

; try EGA

gsbs1:
	les	bx,_TEXT:[EgaBaseAddrs]		; get EGA card base address
	cmp	word ptr es:[bx],SIG_EGA	; is a valid EGA ROM signature
						; present?
	jne	gsbs2				; try for another

; we may have an EGA

	mov	ah,B_WriteDot			; bios call to a write pocka dot
	mov	bl,10h				; test if an EGA is present
	int	BIOS_VIDEO_IO
	test	bl,bl				; is bl zero? (EGA)
	je	gsbs3				; got it

; must be a CGA

gsbs2:
	les	bx,_TEXT:[CgaBaseAddrs]		; get CGA card base address

gsbs3:
	mov	ax,es				; save selector
	les	bx,[DataArea]			; get address of data area
	mov	es:[bx],ax			; load selector for return
	xor	ax,ax				; clear error code

	pop	es

scrnctrl2:
	ret

SCREENCTRL	endp
_TEXT		ends

		end
