;	[]===========================================================[]
;
;	NOTICE: THIS PROGRAM BELONGS TO AWARD SOFTWARE INTERNATIONAL(R)
;	        INC. IT IS CONSIDERED A TRADE SECRET AND IS NOT TO BE 	
;	        DIVULGED OR USED BY PARTIES WHO HAVE NOT RECEIVED	
;	        WRITTEN AUTHORIZATION FROM THE OWNER.
;
; 	[]===========================================================[]
;

;----------------------------------------------------------------------------
;Rev	Date	 Name	Description
;----------------------------------------------------------------------------
;R09	06/13/96 JKY	Add S3_86CM65 Enable_LCD judge
;R08	05/23/97 KGN	Add C&T65555 PCI VGA Support
;R07A   05/14/97 JKY	Support S3_86CM65 of INT15 Hook
;R07	05/07/97 RAX	Add switch for support more VGA chip of INT15 hook
;R06	04/01/97 RAY	Add new switch: CRT_Control_Not_In_F000
;			By defining this switch, you can include the
;			CRT_CTRL.INC(in which the ENABLE_CRT & DISABLE_CRT
;			is located) into E000 to save the F000 code
;R05	03/27/97 DNL	Added codes for Notebook Power Management
;R03B	01/29/97 DNL	Fixed coding mistake
;R04	10/22/96 DNL	Added codes for Notebook Power Management
;R03A	10/04/96 DNL	Fixed bug of system hlt when the CPU enters SMM under
;			Protected mode or V86 mode.
;R03	09/20/96 DNL	Added VGA BIOS function call hook support
;R02	08/13/96 DNL	Fixed coding mistake
;R01	06/04/96 DNL	Add define for make Non PM_SUPPORT bios
;R00	05/24/96 RAY	Initial Revison extracted from APM.ASM

ifdef	PM_SUPPORT					;R01
ifndef	No_CRT_Control
;[]===============================================================[]
;Procedure:	Enable_CRT
;
;Function :	Routine to enable CRT display signal output
;
;Registers:	DX, AX - destroyed
;
;Input    :	DS = PM_RAM
;
;Output   :	None
;
;[Note]	  :	This is a public routine which the users(PMI Handler)
;		can called during any PM mode
;[]==================================================================[]
		ASSUME	DS:PM_RAM
		Public	Enable_CRT
Enable_CRT	Proc	Near

ifdef	LCD_CRT_OPTION
	CRT_OUTPUT	=	00h			;R04
ifdef	CHIP_655XX
	CRT_OUTPUT	=	00h
endif	;CHIP_655XX
ifdef	CL_GD754X
	CRT_OUTPUT	=	01h
endif	;CL_GD754X
;R09 - start
ifdef	S3_86CM65
	CRT_OUTPUT	=	01h
endif	;S3_86CM65
;R09 - end
		cmp	byte ptr ds:Display_Mode,CRT_OUTPUT
		je	short No_LCD_Exist

		Call	Enable_LCD
No_LCD_Exist:
endif	;LCD_CRT_OPTION

ifdef	VGA_Chip_Control
		xor	al,al
		Call	Ct_VGA_Control
endif	;VGA_Chip_Control

;R02 ifdef	Notebook_Power_Management
;R02 		Call	LCD_BACKLIGHT_ON
;R02 endif	;Notebook_Power_Management

ifndef	None_DPMS_STANDARD
		cmp	dword ptr DPMS_LABEL,'DPMS'	; Is DPMS support ?
		jne	short @F
		mov	bh,0
		call	DPMS_Function
		jmp	short End_Enable_CRT
	@@:
endif	;None_DPMS_STANDARD

		call	Decide_3D4_Or_3B4

		mov	bl,80h			;Enable V/H SYNC signals
		call	Prg_VH_SYN		;  of the VGA card

		xor	bl,bl			;enable screen
		call	En_Dis_Screen

End_Enable_CRT:
		ret

Enable_CRT	Endp

ifdef	LCD_CRT_OPTION
		Public	Enable_LCD
Enable_LCD	Proc	Near

		Call	LCD_BACKLIGHT_ON	;R02
		jnc	short @F		;R02

ifdef	CHIP_655XX
		mov	ax,5f51h
		mov	bl,0
		Call	Video_Func

		mov	ax,5f54h
		mov	bl,0
		Call	Video_Func

		mov	ax,5f51h
		mov	bl,ds:Display_Mode
		Call	Video_Func
endif	;CHIP_655XX

ifdef	CL_GD754X
		mov	cl,20h
		Call	Get_Cr_reg
		and	al,11101111b		;Disable Activate Standby mode
		Call	Set_Cr_reg
endif	;CL_GD754X
@@:						;R02
		ret
Enable_LCD	Endp

		Public	Disable_LCD
Disable_LCD	Proc	Near

		Call	LCD_BACKLIGHT_OFF	;R02
		jnc	short @F		;R02

ifdef	CHIP_655XX
		mov	ax,5f54h
		mov	bl,1
		Call	Video_Func
endif	;CHIP_655XX
ifdef	CL_GD754X
		mov	cl,20h
		Call	Get_Cr_reg
		or	al,00010000b		;Enable Activate Standby mode
		Call	Set_Cr_reg
endif	;CL_GD754X
@@:
		ret
Disable_LCD	Endp

endif	;LCD_CRT_OPTION

;[]==================----============================================[]
;Procedure:	Disable_CRT
;
;Function :	Routine to disable CRT display signal output
;
;Registers:	DX, AX - destroyed
;
;Input    :	DS = PM_RAM
;
;	ifdef	DPMS_STANDARD
;		PM_RAM:DPMS_Request_State = 0 DPMS ON
;					  = 1 DPMS STANDBY
;					  = 2 DPMS SUSPEND
;					  = 4 DPMS OFF
;					  = 8 DPMS REDUCE ON
;	endif	;DPMS_STANDARD
;
;Output   :	None
;
;[Note]	  :	This is a public routine which the users(PMI Handler)
;		can called during any PM mode
;[]==================================================================[]
		Public	Disable_CRT
Disable_CRT	Proc	Near

ifdef	LCD_CRT_OPTION
		cmp	byte ptr ds:Display_Mode,CRT_OUTPUT
		je	short No_LCD

		Call	Disable_LCD
NO_LCD:
endif	;LCD_CRT_OPTION

ifdef	VGA_Chip_Control
		mov	al,1
		Call	Ct_VGA_Control
endif	;VGA_Chip_Control

;R02 ifdef	Notebook_Power_Management
;R02 		Call	LCD_BACKLIGHT_OFF
;R02 endif	;Notebook_Power_Management

ifndef	None_DPMS_STANDARD
		cmp	dword ptr DPMS_LABEL,'DPMS'	; Is DPMS support ?
		jne	short @F
		mov	bh,PM_RAM:DPMS_Request_State	; BH = request state
		test	PM_RAM:DPMS_Capability,bh	; Is request match ?
		jz	short @F			; NO , skip ...
		call	DPMS_Function
		jmp	short End_Disable_CRT
	@@:
endif	;None_DPMS_STANDARD

		call	Decide_3D4_Or_3B4

		mov	bl,20h			;Disable screen
		call	En_Dis_Screen

		xor	bl,bl			;Disable V/H SYNC signals
		call	Prg_VH_SYN		;  of the VGA card

End_Disable_CRT:
		ret

Disable_CRT	Endp

;-------------------------------------------
;-------------------------------------------
Decide_3D4_Or_3B4	Proc	Near
		mov	dx,3cch
		in	al,dx
		mov	dx,3d4h
		test	al,1
		jnz	short @F
		mov	dx,3b4h
	@@:
		ret
Decide_3D4_Or_3B4	Endp

;-------------------------------------------
;Input	:	BL = 80h : enable VH SYN
;		     00h : disable VH SYN
;-------------------------------------------
Prg_VH_SYN	Proc	Near

		push	dx
		test	byte ptr PM_RAM:[PM_CTRL_FLAG],V_H_SYNC_ENABLE
		jz	short @F

		mov	al,17h
		out	dx,al
		IODELAY
		inc	dx
		in	al,dx
		IODELAY
		and	al,7Fh
		or	al,bl
		out	dx,al
		IODELAY
@@:
		pop	dx
		ret

Prg_VH_SYN	Endp

;-------------------------------------------
;Input	:	BL = 00h : enable screen
;		     20h : disable screen
;-------------------------------------------
ATTREG		EQU    3C0H
SEQ_INDEX	EQU    3C4H
En_Dis_Screen	Proc	Near
		push	dx

		add     dx,06h
                in      al,dx
		IODELAY

                mov     dx,ATTREG
		in	al,dx
		IODELAY
		mov	al,bl
		xor	al,20h
                out     dx,al
		IODELAY

                mov     dx,SEQ_INDEX
		in	al,dx
		IODELAY

		push	ax		;Save SEQ index value

                mov     al,01h
                out     dx,al
		IODELAY
                inc     dx
                in      al,dx
		IODELAY
                and     al,not 20h
		or	al,bl
                out     dx,al
		IODELAY

		pop	ax		;Restore SEQ index value
		dec	dx
		out	dx,al
		IODELAY

		pop	dx
		ret

En_Dis_Screen	Endp

ifndef	None_DPMS_STANDARD
;-----------------------------------------------------------------
;Input	:	BH = 0	DPMS_ON
;		   = 1	DPMS_STANDBY
;		   = 2	DPMS_SUSPEND
;		   = 4	DPMS_OFF
;		   = 8	DPMS_REDUCE_ON
;------------------------------------------------------------------
DPMS_Function	proc	near
		pushad
		push	es

		in	al,21h
		IODELAY
		push	ax		; Save IRQ mask
		in	al,0a1h
		IODELAY
		push	ax		; Save IRQ mask
		mov	al,0ffh		; mask all of IRQ
		out	21h,al
		out	0a1h,al

		mov     dx,03c4h
		in	al,dx
		push	dx
		push	ax		; Save sequence register index
ifndef	DPMS_No_Save_Int10_Vector
		mov	ax,G_RAM
		mov	es,ax
		mov	eax,es:INT10
		push	eax		; Save current INT10 vector
		push	es
		mov	eax,PM_RAM:INT10_Save
		mov	dword ptr es:INT10,eax	; Set to original INT10 vector
endif	;DPMS_No_Save_Int10_Vector

		mov	ax,4f10h	; VESA Extentsion & VBE/PM Service
		mov	bl,01h		; Set display power state
		xor	cx,cx		; Controller unit number (cx=00 primary)
		mov	es,cx		; ES:DI = 0000:0000
		xor	di,di
		mov	si,offset PM_RAM:Int10_Save
		pushf
		call	dword ptr PM_RAM:[si]

ifndef	DPMS_No_Save_Int10_Vector
		pop	es
		pop	eax
		mov	dword PTR es:INT10,eax	; Restore current INT10 vector
endif	;DPMS_No_Save_Int10_Vector

		pop	ax
		pop	dx
		out	dx,al		; Restore sequence register index

		pop	ax
		out	0a1h,al		; Restore IRQ mask
		pop	ax
		out	21h,al		; Restore IRQ mask

		pop	es
		popad

		ret
DPMS_Function	endp
endif	;None_DPMS_STANDARD

ifdef	LCD_CRT_OPTION
ifdef	CL_GD754X
CR_Index	EQU	3D4h
CR_Data		EQU	3D5h

		Public	Get_Cr_reg
Get_Cr_reg	Proc	Near

		mov	al,cl
		mov	dx,CR_Index
		out	dx,al
		NEWIODELAY
		mov	dx,CR_Data
		in	al,dx
		ret

Get_Cr_reg	Endp

		Public	Set_Cr_reg
Set_Cr_reg	Proc	Near

		xchg	al,cl
		mov	dx,CR_Index
		out	dx,al
		NEWIODELAY
		xchg	al,cl
		mov	dx,CR_Data
		out	dx,al
		ret

Set_Cr_reg	Endp
endif	;CL_GD754X
endif	;LCD_CRT_OPTION

endif	;No_CRT_Control

;R07 - start
	VGA_CALLBACK_HOOK	=	0
ifndef	NO_VGA_INT15_HOOK
ifdef	CHIP_655XX
	VGA_CALLBACK_HOOK	=	1
endif	;CHIP_655XX

ifdef	NeoMagic_2093AB
	VGA_CALLBACK_HOOK	=	1
endif	;NeoMagic_2093AB

ifdef	TRIDENT_9385
	VGA_CALLBACK_HOOK	=	1
endif	;TRIDENT_9385

;R07A - start
ifdef	S3_86CM65
	VGA_CALLBACK_HOOK	=	1
endif	;S3_86CM65
;R07A - end

ifdef	CHIP_65555				;R08
	VGA_CALLBACK_HOOK	=	1	;R08
endif	;CHIP_65555				;R08

endif	;NO_VGA_INT15_HOOK
;R07 - end
;R03A		extrn	Multi_Service_Near:near		;R03
		Public	VIDEO_FUNC
VIDEO_FUNC	Proc	Near

		push	ds
		push	es

		push	eax
ifdef	CRT_Control_Not_In_F000			;R06
		mov	ax, PM_RAM_SEGMENT	;R06
else	;CRT_Control_Not_In_F000		;R06
		call	Get_PM_RAM_Seg
endif	;CRT_Control_Not_In_F000		;R06
		mov	ds, ax
                ASSUME  DS:PM_RAM

		mov	ax,G_RAM
		mov	es,ax
		pop	eax

		mov	esi,dword ptr es:INT10
		push	dword ptr PM_RAM:INT10_Save
		pop	dword ptr es:INT10		; Set to original INT10 vector

;R03 - start
if	VGA_CALLBACK_HOOK	NE	0	;R07
;R07 ifdef	CHIP_655XX
		push	dword ptr es:INT15
		pop	dword ptr PM_RAM:VGA_HOOK_Save
		push	cs
;R03A		push	offset Multi_Service_Near
		push	offset Temp_Int15		;R03A
		pop	dword ptr es:INT15
;R07 endif	;CHIP_655XX
endif	;VGA_CALLBACK_HOOK	NE	0	;R07
;R03 - end
		pop	es
		push	es
		push	esi
		mov	si,offset PM_RAM:INT10_Save
		pushf
		call	dword ptr PM_RAM:[si]

		push	G_RAM				
		pop	es				
		pop	dword PTR es:INT10		; Restore current INT10 vector
;R03 - start
if	VGA_CALLBACK_HOOK	NE	0		;R07 
;R07 ifdef	CHIP_655XX
		push	dword ptr PM_RAM:VGA_HOOK_Save
		pop	dword ptr es:INT15
;R07 endif	;CHIP_655XX
endif	;VGA_CALLBACK_HOOK	NE	0		;R07 
;R03 - end

		pop	es
		pop	ds

		ret

VIDEO_FUNC	Endp
;R03A - start
;R07 ifdef	CHIP_655XX
if	VGA_CALLBACK_HOOK	NE	0		;R07 
ifdef	NEW_SUPERVGA_KERNEL				;R05
		extrn	Int15_Func5F:near		;R05
else	;NEW_SUPERVGA_KERNEL				;R05
ifndef	CRT_Control_Not_In_APM				;R03B
		extrn	Int15_Func5F:near
endif	;CRT_Control_Not_In_APM				;R03B
endif	;;NEW_SUPERVGA_KERNEL				;R05
		Public	Temp_Int15
Temp_Int15	Proc	Near
		Call	Int15_Func5F
		iret
Temp_Int15	Endp
endif	;VGA_CALLBACK_HOOK	NE	0		;R07
;R07 endif	;CHIP_655XX
;R03A - end
endif	;PM_SUPPORT					;R01
