;	[]===========================================================[]
;
;	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.
;
; 	[]===========================================================[]
;

		PAGE	60,132
		TITLE	ESCDCHIP  -- NVM interface for PnP ESCD
;----------------------------------------------------------------------------
;Rev	Date	 Name	Description
;----------------------------------------------------------------------------
;R34  	02/25/99 BAR	Support 32bit address to update Flash.
;R33	02/03/99 BAR 	Support Flash_Part_Erase .
;R32	01/26/99 BAR	Support update ESCD DMI in SMM mode.
;			define switch "Flash_IN_SMBASE"
;R31	09/29/98 KVN	Release DMI and ESCD pool in flash ROM when not define
;			"ESCD_SUPPORT" and "FLASH_SUPPORT" in BIOS.CFG
;R30	08/20/98 KEN	Supplement declaration of "ESCD_SUPPORT".
;R29	06/05/98 MAX	Support CMOS default value save to 2M ROM Flash function
;R28A	05/25/98 KEN	Fixed bug that the system hang-up while processing
;			Build_DMI_Pool at POST_99s on MediaGX board and
;			the memory size is exceeded 64MB.
;			The bug was appeared after the modification of R28 of
;			ESCDCHIP.ASM that updating flash ROM with flat mode.
;R28	05/08/98 KEN	Fixed bug that the system reboot while updating ESCD
;			at POST_99s when the CPU is AMD K6/3D and the USB
;			keyboard is plugged.
;			The causation is that the SMI is issued while the
;			system is resided in protected mode.
;			The solution is that using flat mode for flash ROM
;			reading and writing.
;R27	07/25/97 KEN	Defined the routine 'Read_A20_Status' to be public
;			for other modules' using.
;R26A	05/20/97 AVN	Let MXIC 28F2000PPC reduce to alone in BIOS support.
;R26	04/11/97 AVN	Link MX28F2000PPC EEPROM into 2M EEPROM Group
;R25	01/17/96 AVN	Speed Up INTEL ESCD/DMI Update Speed By Default, And
;			Define 'Speed_Down_EEPROM_Update' For Update Soly If
;			To Soon To Update Fail.
;R24	12/24/96 AVN	Support MP Secondary Update Code For 1M/2M EEPROM
;R23	12/10/96 AVN	Suppost Auto Detect EEPROM ID at POST 6 For 1M EEPROM
;			By Define 'AUTO_CHK_1M_EEPROM'
;R22	11/14/96 AVN	Fixed Pass for AMIDIAG.EXE PnP BIOS test.
;R21	10/16/96 AVN	Added 2M BIOS Support MXIC 28F2000PPC EEPROM.
;			Define 'MXIC_28F2000PPC'
;R20	09/23/96 AVN	Added 2M BIOS Support INTEL 28F002BXT EEPROM.
;			And Save Move Code.
;R19C	09/23/96 AVN	Fix Dummy Flash Routine Code Size Define by 2MROMLEN.INC
;			If (2M FLASH).NVM > Dummy Size And Show ERROR Message.
;R19B	09/06/96 AVN	Move Dummy Flash Routine From F000h:8000h to End of
;			This File, For SiS596 Compiler Code Cover F000h:8000h
;			and Copy Flash Routine Will Cover BIOS Code.
;R19A	08/20/96 AVN	Detect EEPROM Type at post 6, to support 2M EEPROM
;			for save more code in F segment.
;			POST 6s Will copy correct flash hook to f800h:4
;R17B	08/02/96 KVN	Reduce compare RAM area size for save elapsed time in
;			check A20 routine for DMI POST
;R19	07/09/96 AVN	Added AMD AM29F002T/ SST 29EE020 2M Type EEPROM Support.
;R18	05/28/96 AVN	Added AMD29F010 16K Block EEPROM Support.
;R17A	05/15/96 KVN	Use segment:offset character for detect A20 ON/OFF to
;			fixed some mother board detect A20 spent much time
;R17	05/10/96 RCH	BIOS should keep the status of A20 while read/write
;			ESCD for ESCD_M2 to prevent system reset with EMM386
;			and ICU together.
;R16A	04/23/96 RCH	Don't touch BTB if the CPU stepping is 051x or 052x
;			due to extreme performance degraded for 0512H 
;			stepping.
;R16	04/08/96 RCH	System hang while updating ESCD if INTEL/P55C CPUs
;			is used and flash's PGM pin is connected to SMEMW. 
;			To solve this, BIOS disable BTB before accessing ESCD 
;			and turn on it after updating ESCD.
;R15	04/05/96 KEN	Let the Flash_Write and Flash_Read routines can be
;			called directly, not only the PNPBIOS.
;R14	03/27/96 RAY	Support MXIC_28F1000AP become standard.
;R13A	01/15/96 KEN	Add ESCD_M2 definition to reserve original updating
;			ESCD method.
;R13	01/12/96 KEN	Support updating ESCD with high memory (below 4GB).
;R12	12/27/95 RCH	BIOS need to support run-time ESCD for EISA systems
;R11	12/26/95 ROB	Update for EISA/PNP.
;R10	12/26/95 TNY	Fix UMC 8890C chipset bug.
;R09A	12/18/95 RAY	Support MXIC 28F1000AP flash (4k unit) only if
;			MXIC_28F1000AP is defined in BIOS.CFG
;R09	12/09/95 RAY	Support more than one Flash in one BIOS for PnP
;R08	11/30/95 HTR	CPU L1 cache always enable fixed
;R07	11/17/95 RCH	Don''t change FARHIT bit of Cyrix 6x86 CPU
;R06	11/16/95 RCH	Split this file into various files according to type
;			of flash memory and backup this file to ESCDCHIP.11F
;R05	10/26/95 RCH	Fixed system hang while updating ESCD with M1 CPU
;R04A	10/23/95 KEN	Fix bug for erase fail, and mask un-neccessary codes.
;R04	10/18/95 KVN	Added MXIC 16K unit Pnp FLASH feature support
;R03	09/18/95 KEN	Fix 16 bit stack fault, when install CS4232 in windows.
;R02	05/11/95 KEN	Fix bug for always successful writing flash ROM, when
;			using ROM emulator.
;R01	04/19/95 KEN	Fixed 'STI' bug when flash rom access
;R00	10/01/94 KEN	Initial Revision

.386P

.XLIST
		INCLUDE	BIOS.CFG
		INCLUDE	COMMON.MAC
		INCLUDE	PNP.EQU
		include	common.equ			;R05
		include	cmos.equ			;R05
		include	cpu.equ				;R08
		include	8042.equ			;R17

;R26A ifdef	Flash_2M_support				;R26
;R26A G_RAM		SEGMENT	USE16 AT 40H			;R26
;R26A 		INCLUDE	G_RAM.INC			;R26
;R26A G_RAM		ENDS					;R26
;R26A ifdef	DMI_ENABLED					;R26
;R26A 		INCLUDE	DMI.EQU				;R26
;R26A endif;	DMI_ENABLED					;R26
;R26A 		INCLUDE	FSHADOW.EQU			;R26
;R26A endif;	Flash_2M_support				;R26

;R09 - starts
ifndef	AUTO_DETECT_EEPROM_ID				;R23
FLASH_TYPE_SST		EQU	1
FLASH_TYPE_28F001BXT	EQU	2
FLASH_TYPE_MXIC		EQU	3
;R09 - ends
ifdef	AMD29F010			;R18
FLASH_TYPE_AMD29F	EQU	4	;R18
endif;	AMD29F010			;R18
;R19A ifdef	AM29F002T			;R19
;R19A FLASH_TYPE_AM29F002T	EQU	5	;R19
;R19A endif;	AM29F002T			;R19
endif;	AUTO_DETECT_EEPROM_ID				;R23

.LIST

;[]========================================================================[]
;
;	External label definition
;
;[]========================================================================[]

ifdef	PNP_BIOS
ifdef	FLASH_SUPPORT
		EXTRN	Ct_Buffer_Request:near		;PNPCHIP.ASM
		EXTRN	Ct_Save_Shadow:near		;PNPCHIP.ASM
		EXTRN	Ct_Restore_Shadow:near		;PNPCHIP.ASM
		EXTRN	Ct_Enable_Flash:near		;PNPCHIP.ASM
;R13		EXTRN	Ct_Get_Flash:near		;PNPCHIP.ASM
;R13		EXTRN	Ct_Set_Flash:near		;PNPCHIP.ASM
		EXTRN	Wait_Refresh:near		;ATORGS.ASM
		EXTRN	Check_16Bit_Stack:near		;PNPBIOS.ASM;R15
;R13		EXTRN	Check_16Bit_Stack:near		;PNPBIOS.ASM;R03
;R13		extrn	Ct_GetAll_Flash:near		;R05
		extrn	Get_Cyrix:near			;R05
		extrn	Set_Cyrix:near			;R05
		extrn	UNlock_Cyrix:near		;R05
		extrn	Lock_Cyrix:near			;R05
		extrn	Cpu_Cache:near			;R05
		extrn	Get_Cmos:near			;R05
ifdef	ESCD_M2						;R13A
		extrn	GDTR1:near			;R13
		extrn	ESCD_INDEX:abs			;R13
ifdef Flash_16K_8K_8K_Unit                              ;R29
ifdef  Support_CMOS_Backup_Data 
		extrn	F000_INDEX:abs           	;R29		
endif ; Support_CMOS_Backup_Data 
endif ; Flash_16K_8K_8K_Unit	                        ;R29
		extrn	F0DATA_INDEX:abs		;R22
		extrn	A20_On:near			;R13
		extrn	A20_Off:near			;R13
		extrn	Out_8042:near			;R17
		extrn	Out_8042_Full:near		;R17
		extrn	A20_Byte:near			;R17
;R26A ifdef	Flash_2M_support				;R26
;R26A 		extrn	Ct_Shadow_Write:near		;R26
;R26A endif;	Flash_2M_support				;R26
else	;ESCD_M2					;R13A
		EXTRN	Ct_Get_Flash:near		;R13A;PNPCHIP.ASM
		EXTRN	Ct_Set_Flash:near		;R13A;PNPCHIP.ASM
		EXTRN	Ct_GetAll_Flash:near		;R13A;PNPCHIP.ASM
endif	;ESCD_M2					;R13A
ifdef	DMI_ENABLED					;R34
		extrn	Read_DMI:near			;R34
		extrn	Write_DMI:near			;R34
endif;	DMI_ENABLED					;R34
endif	;FLASH_SUPPORT
;R11 - start
IF	BUS_TYPE EQ EISA_BUS
                extrn   Func_D8:near
		extrn	EISA_MOTHERBOARD_ID:byte
	        extrn   GET_NVMADDR:near
	        extrn   READ_NVM_WORD:near
                extrn   EXTRACT_INFO:near
                extrn   CLR_STRUCT:near
                include EISA.EQU
ENDIF	;BUS_TYPE EQ EISA_BUS
;R11 - end
endif	;PNP_BIOS

DGROUP		GROUP	FCODE
FCODE		SEGMENT	USE16 DWORD PUBLIC 'CODE'
		ASSUME	CS:DGROUP,DS:DGROUP

ifdef	PNP_BIOS
;R30ifdef	ESCD_SUPPORT
		include	dallas.nvm			;R06
		include	EisaPnP.nvm			;R12

ifdef	FLASH_SUPPORT

ifdef	ESCD_SUPPORT					;R30
;****************************************************************
;*								*
;*	SUBROUTINES TO SUPPORT FLASH ROM FOR ESCD		*
;*								*
;****************************************************************

;******************************************************************************
; NOTE:	This festure subroutines must be resided at E000h shadow RAM.
;******************************************************************************

;[]========================================================================[]
;Procedure:	Ct_ESCD_Info
;Function :
;Input    :	SS:EBP = index structure
;			DWORD PTR [EBP+2] = pointer for MinESCDWriteSize(word)
;			DWORD PTR [EBP+6] = pointer for ESCDSize(word)
;			DWORD PTR [EBP+10] = pointer for NVStorageBase(dword)
;Output   :	CF = 0 successful
;		CF = 1 fail
;Registers:
;Note	  :
;[]========================================================================[]
		public	Ct_ESCD_Info
Ct_ESCD_Info	proc	near

;Minimum write storage size

		mov	di, [ebp+4]
		mov	es, di
		mov	di, [ebp+2]
ifdef	ESCD_M2						;R13A
;R22		mov	word ptr es:[di], 6000h		;R13
		mov	word ptr es:[di], 4000h		;R22
;R13		mov	word ptr es:[di], ESCD_SIZE
else	;ESCD_M2					;R13A
		mov	word ptr es:[di], ESCD_SIZE	;R13A
endif	;ESCD_M2					;R13A

;Maximum storage size

		mov	di, [ebp+8]
		mov	es, di
		mov	di, [ebp+6]
ifdef	ESCD_M2						;R13A
		mov	word ptr es:[di], 1000h		;R13
;R13		mov	word ptr es:[di], ESCD_SIZE
else	;ESCD_M2					;R13A
		mov	word ptr es:[di], ESCD_SIZE	;R13A
endif	;ESCD_M2					;R13A

;Non-volatile storage 32-bit physical base address

		mov	di, [ebp+12]
		mov	es, di
		mov	di, [ebp+10]
ifdef	ESCD_M2						;R13A
		mov	dword ptr es:[di], ESCD_BASE	;R13
;R13		mov	dword ptr es:[di], 000F0000h
else	;ESCD_M2					;R13A
		mov	dword ptr es:[di], 000F0000h	;R13A
endif	;ESCD_M2					;R13A

		clc
		ret
Ct_ESCD_Info	endp

;[]========================================================================[]
;Procedure:	Ct_Get_ESCD
;Function :
;Input    :	DS = ESCD storage selector/segment (reported by Ct_ESCD_Info)
;		ES:DI = pointer for caller's ESCD buffer
;Output   :	CF = 0 successful
;		CF = 1 fail
;Registers:
;Note	  :
;[]========================================================================[]
		public	Ct_Get_ESCD
Ct_Get_ESCD	proc	near
;R05		mov	si, ESCD_ADDR
;R05		mov	cx, 6
;R05		call	Flash_Read
;R05		cmp	dword ptr es:[di+2], 'GFCA'	;ESCD signature
;R05		jne	short Ct_Get_ESCD_Fail
;R05		mov	cx, es:[di]			;size in ESCD structure
;R05		cmp	cx, ESCD_SIZE
;R05		ja	short Ct_Get_ESCD_Fail
;R05		jcxz	short Ct_Get_ESCD_Fail
;R05		call	Flash_Read
;R05 - start
;R13		mov	si, ESCD_ADDR
		push	gs	;R22
;R34 - start
ifdef		ESCD_32bits			;R34
		mov	esi, ESCD_BASE		;escd_real_base
		mov	gs, wBiosSelector	
elseifdef	ESCD_M2	
;R34 - start
;R34 ifdef	ESCD_M2						;R13A
;R22		mov	si, ESCD_ADDR-8000h		;R13
		mov	si, ESCD_REAL_BASE	;R22
		mov	gs, wBiosSelector	;R22
else	;ESCD_M2					;R13A
		mov	ds, wBiosSelector		;R13A
		mov	si, ESCD_ADDR			;R13A
endif	;ESCD_M2					;R13A
		mov	cx, ESCD_SIZE			;total size
		call	Flash_Read

		cmp	dword ptr es:[di+2], 'GFCA'	;ESCD signature
		jne	short Ct_Get_ESCD_Fail
;R05 - end
		pop	gs	;R22
		clc
		ret

Ct_Get_ESCD_Fail:

		pop	gs	;R22
		stc
		ret
Ct_Get_ESCD	endp

;[]========================================================================[]
;Procedure:	Ct_Set_ESCD
;Function :
;Input    :	DS:SI = pointer for caller's ESCD buffer
;		ES = ESCD storage selector/segment (reported by Ct_ESCD_Info)
;		CX = length
;Output   :	CF = 0 successful
;		CF = 1 fail
;Registers:
;Note	  :
;[]========================================================================[]
		public	Ct_Set_ESCD
Ct_Set_ESCD	proc	near

		push	gs	;R22
;R13 - start
		cmp	cx, ESCD_SIZE
		ja	short Ct_Set_ESCD_Fail
		jcxz	short Ct_Set_ESCD_Fail

;R34 - start
ifdef		ESCD_32bits
		mov	edi, ESCD_BASE		;escd_real_base
		mov	gs, wBiosSelector	
elseifdef	ESCD_M2	
;R34 - start
;R34 ifdef	ESCD_M2					;R13A
;R22		mov	di, ESCD_ADDR-08000h	;R22
		mov	di, ESCD_REAL_BASE	;R22
		mov	gs, wBiosSelector	;R22
else	;ESCD_M2				;R13A
		mov	es, wBiosSelector	;R13A
		mov	di, ESCD_ADDR		;R13A
endif	;ESCD_M2				;R13A
		call	Flash_Write
		pop	gs	;R22
		ret

Ct_Set_ESCD_Fail:
		pop	gs	;R22
		stc
		ret
;R13 - end
;R13		push	bx		;save BX	;R09
;R13
;R13		mov	eax, cr0	;R08
;R13		mov	ax, TRUE	;R08
;R13		test	eax, CR0_CD	;R08
;R13		jz	CPU_Cache_On	;R08
;R13		mov	ax, FALSE	;R08
;R13CPU_Cache_On:				;R08
;R13		push	ax		;R08	;Save CPU cache status
;R13
;R13		cmp	cx, ESCD_SIZE
;R13		ja	short Ct_Set_ESCD_Fail
;R13		jcxz	short Ct_Set_ESCD_Fail
;R13
;R13		call	Disable_M1_BTB		;disable BTB of M1	;R05
;R13
;R13;R09 		mov	di, ESCD_ADDR
;R13;R09 		call	Flash_Erase
;R13;R09 ;R05		jc	short Ct_Set_ESCD_Fail
;R13;R09 		jc	short Set_Escd_Fault				;R05
;R13;R09 		mov	di, ESCD_ADDR
;R13;R09 		call	Flash_Write
;R13
;R13;R09 - starts
;R13;------------------------------------------------------------------------
;R13
;R13		mov	bx, offset Flash_Brand_Tbl
;R13
;R13Try_Next_Flash_Brand:
;R13
;R13		push	bx
;R13		mov	bl, cs:[bx]
;R13		cmp	bl, -1				;all tried ?
;R13		je	short Write_Flash_Fail		;yes
;R13
;R13		mov	di, ESCD_ADDR
;R13		call	Flash_Erase
;R13		jc	short Write_This_Flash_Fail	;erase fail
;R13
;R13		mov	di, ESCD_ADDR
;R13		call	Flash_Write
;R13 		jnc	short Write_Flash_Sucess	;write success!
;R13
;R13Write_This_Flash_Fail:
;R13
;R13		pop	bx
;R13 		inc	bx				;next flash
;R13		jmp	short Try_Next_Flash_Brand
;R13		push	bx
;R13
;R13Write_Flash_Fail:
;R13
;R13 		stc					;indicate fail
;R13
;R13Write_Flash_Sucess:
;R13
;R13		pop	bx
;R13
;R13;----------------------------------------------------------------
;R13;R09 - ends
;R13
;R13		call	Enable_M1_BTB		;disable BTB of M1	;R05
;R13
;R13End_Set_ESCD:								;R09
;R13
;R13		pop	ax	 	;R08	Restore CPU cache status
;R13		pushf			;R08	Save Flag
;R13		call	CPU_Cache	;R08
;R13		popf			;R08	Restore Flag
;R13
;R13		pop	bx				;restore BX	;R09
;R13		ret
;R13
;R13Ct_Set_ESCD_Fail:							;R09
;R13
;R13		stc
;R13		jmp	short End_Set_ESCD				;R09
;R13
;R13;R09 Set_Escd_Fault:								;R05
;R13;R09 		call	Enable_M1_BTB		;disable BTB of M1	;R05
;R13;R09 
;R13;R09 Ct_Set_ESCD_Fail:
;R13;R09 
;R13;R09 		pop	ax	 	;R08
;R13;R09 		call	CPU_Cache	;R08
;R13;R09 
;R13;R09 		pop	bx						;R09
;R13;R09 		stc
;R13;R09 		ret

Ct_Set_ESCD	endp
endif	;ESCD_SUPPORT					;R30

;R09 - starts
;R23 ifndef	Flash_2M_support				;R19A
ifndef	AUTO_DETECT_EEPROM_ID				;R23
		Public	Flash_Brand_Tbl
Flash_Brand_Tbl:

ifdef	MXIC_28F1000PL

	;if 16K block flash, we only support MXIC_28F1000PL

		db	FLASH_TYPE_MXIC

elseifdef	AMD29F010				;R18
		db	FLASH_TYPE_AMD29F		;R18

;R19A 	;; 2M Type EEPROM
;R19A elseifdef	AM29F002T				;R19
;R19A 		db	FLASH_TYPE_AM29F002T		;R19
;R19A elseifdef	SST_PH29EE020				;R19
;R19A 		db	FLASH_TYPE_SST			;R19

else	;MXIC_28F1000PL

;R14  ifdef MXIC_28F1000AP					;R09A
		db	FLASH_TYPE_MXIC			;R09A
;R14  endif	;MXIC_28F1000AP					;R09A

		db	FLASH_TYPE_SST
		db	FLASH_TYPE_28F001BXT
;R09A		db	FLASH_TYPE_MXIC

endif	;MXIC_28F1000PL

		db	-1
endif;	AUTO_DETECT_EEPROM_ID				;R23
;R23 endif;	Flash_2M_support				;R19A

;[]========================================================================[]
;Procedure:	Flash_Erase
;Function :
;Input    :	none
;		BL = 1      : Do SST flash erase
;		     2      : Do Intel 28F001BXT flash erase
;		     3      : Do MXIC 28F001BXT flash erase
;		     others : Reserved for future 
;Output   :	CF = 0 successful
;		CF = 1 fail
;Registers:
;Note	  :
;[]========================================================================[]
;R13		Public	Flash_Erase
;R13Flash_Erase	Proc	Near
;R13		xor	bh, bh			;indicates a Flash Erase
;R13		call	Flash_Erase_Or_Flash_Write
;R13		ret
;R13Flash_Erase	Endp

;R17 - start
ifdef	ESCD_M2	
;Function : Mark flag on stack "bPnpModeFlag" for A20 status
;Input    : none
;Output   : none
		public	Check_A20_AndRecord	;R24
Check_A20_AndRecord proc	near
		and	bPnpModeFlag, NOT A20_HIGH;assume A20 is low
		call	Read_A20_Status		;A20 on if no carry
		jc	short WA20_Not_On	;A20 is off
		or	bPnpModeFlag, A20_HIGH	;set flag for A20 is high
WA20_Not_On:
		ret
Check_A20_AndRecord endp

;R31;Function : Return status of A20
;R31;Input    : none
;R31;Output   : carry clear - A20 is on
;R31;	    non-carry   - A20 in off
;R31		public	Read_A20_Status			;R27
;R31Read_A20_Status	proc	near
;R31		push	es				;R17A
;R31		push	ds				;R17A
;R31		pusha
;R31		cli
;R31
;R31;R17A		test	byte ptr cs:A20_Byte,01h  ;fast port 92H supported ?
;R31;R17A		jz	short Check_8042
;R31;R17A
;R31;R17A		in	al,92H
;R31;R17A		NEWIODELAY
;R31;R17A		test	al,02H			;A20 on ?
;R31;R17A		jnz	short Yes_A20On		;yes , not check 8042 again
;R31;R17ACheck_8042:
;R31;R17A;
;R31;R17A;	Clear output buffer
;R31;R17A;
;R31;R17A
;R31;R17A		xor	cx,cx			; clear output buffer first
;R31;R17A		mov	bx,0ff0fh		; re-tries for compaq, bh=ff
;R31;R17A
;R31;R17A		ALIGN	4
;R31;R17Agswt_s1:
;R31;R17A		NEWIODELAY
;R31;R17A		in	al,STAT8042		; flush keystrokes
;R31;R17A
;R31;R17A		test	al,OBF_8042		; output buffer empty?
;R31;R17A		jz	short gswt_s2		; jump if so
;R31;R17A
;R31;R17A		NEWIODELAY
;R31;R17A		in	al,DATA8042		; clear buffer
;R31;R17A
;R31;R17A		loop	short gswt_s1
;R31;R17A		jmp	short gswt_s5		; time out, buffer not clear
;R31;R17A
;R31;R17A;
;R31;R17A;	Read 8042 input port
;R31;R17A;
;R31;R17A
;R31;R17Agswt_s2:
;R31;R17A		mov	al,0d0h 		; read input port
;R31;R17A		call	Out_8042
;R31;R17A
;R31;R17A		call	Out_8042_Full		; data ready?
;R31;R17Agswt_s4:
;R31;R17A		in	al,DATA8042		; read input port data
;R31;R17A
;R31;R17A		cmp	al,bh			; should force one retry
;R31;R17A		je	short gswt_s6
;R31;R17A
;R31;R17A		mov	bh,al			; two tries at least
;R31;R17A
;R31;R17A		xor	cx,cx
;R31;R17A		loop	short $ 		; delay between commands
;R31;R17A
;R31;R17A		dec	bl
;R31;R17A		jnz	short gswt_s2		; retry...
;R31;R17A
;R31;R17Agswt_s5:
;R31;R17A		stc				; A20 is off
;R31;R17A		jmp	short A20_Exit
;R31;R17Agswt_s6:
;R31;R17A		test	al,02H			;a20 on ?
;R31;R17A		jz	short gswt_s5		;no
;R31;R17A start
;R31;Check A20 state by comparing data between 0:0 and 10000H:0, if the contain
;R31;is the same than the A20 is off, otherwise A20 in on
;R31		xor	si,si
;R31		mov	ds,si			;DS:SI = 0000:0000
;R31		mov	di,0ffffh
;R31		mov	es,di
;R31		mov	di,10h			;ES:DI = 0FFFF:0010
;R31;R17B		mov	cx,10000h-10h		;size = 64K-10h
;R31		mov	cx,100h			;R17B size = 100h
;R31		rep	cmpsb
;R31		stc
;R31		je	short A20_Exit		;if equate A20 is off
;R31;R17A end
;R31Yes_A20On:
;R31		clc				;mark flag for A20 on
;R31
;R31A20_Exit:
;R31		popa
;R31		pop	ds			;R17A
;R31		pop	es			;R17A
;R31		ret
;R31Read_A20_Status	endp
endif;	ESCD_M2	

;R17 - end

ifndef	Flash_IN_SMBASE			;R32
;[]========================================================================[]
;Procedure:	Flash_Write
;Function :
;Input    :	DS:SI = point to source buffer
;		ES:DI = point to distinct buffer (Range = FD000h-FDFFFh)
;		CX = length
;
;Output   :	CF = 0 successful
;		CF = 1 fail
;Registers:
;Note	  :
;[]========================================================================[]
;R26A F43BiosSelector		EQU	word  ptr [ebp+8]	;R26
;R26A gds_bios_selector  	EQU 	[ebp+FN_OFS+0Ch]	;R26

		public	Flash_Write
Flash_Write	proc	near
;R13 - start
		pushf
		pushad
		cli

;R26A ;R26 - start
;R26A ifdef	Flash_2M_support
;R26A 		cmp	byte ptr cs:[0E05AH],05h	;MXIC 2M Type EEPROM Flag
;R26A 		jne	No_DMI_CPU_PnP_Backup
;R26A 
;R26A 		push	es
;R26A 		mov	ax, G_RAM
;R26A 		mov	es, ax
;R26A 		assume	es: G_RAM
;R26A 	      	test	es:[INIT_ERR_FLG],FLASH_IN_POST
;R26A 		pop	es
;R26A 		jnz	Backup_PnP_DMI_CPU_to_RAM	;Skip if in POST
;R26A 
;R26A Backup_PnP_DMI_CPU_To_Shadow:
;R26A 	; Copy ESCD and DMI and CPU update code to shadow
;R26A 
;R26A 		push	es
;R26A 		pushad
;R26A 		push	ds
;R26A 		push	es
;R26A 		pop	ds
;R26A 
;R26A 	; 1st Backup DMI and CPU update code to shadow
;R26A 		smsw	ax				;get machine word
;R26A 		test	al,1				;in protected mode ?
;R26A 		jnz	short Backup_in_Protect_mode1	;No, use original selector
;R26A 
;R26A 		push	0f000h
;R26A 		pop	es
;R26A 		jmp	short @f
;R26A 
;R26A Backup_in_Protect_mode1:
;R26A 		cmp	di, ESCD_REAL_BASE
;R26A 		mov	es, F43BiosSelector
;R26A 		je	short @f
;R26A 
;R26A ifdef	DMI_ENABLED
;R26A 		cmp	di, DMI_OFFSET
;R26A 		mov	es, gds_bios_selector
;R26A 		je	short @f
;R26A endif;	DMI_ENABLED
;R26A 
;R26A 		push	0f000h
;R26A 		pop	es
;R26A 
;R26A @@:
;R26A ifdef	DMI_ENABLED
;R26A 		mov	di, Shadow_DMI_Backup_Address
;R26A 		mov	si, DMI_OFFSET			;Start address from
;R26A 							;fff8000:0
;R26A 		mov	cx, 1024*8			;8k size
;R26A 		call	Backup_PnP_DMI_P6_Block
;R26A endif;	DMI_ENABLED
;R26A 
;R26A 	; 2nd Backup ESCD code to shadow
;R26A 	; Read ESCD Head information
;R26A 		mov	si, ESCD_REAL_BASE
;R26A 		mov	di, Shadow_ESCD_Backup_Address
;R26A 		mov	cx, 6h
;R26A 		call	Backup_PnP_DMI_P6_Block
;R26A 
;R26A 		cmp	dword ptr es:[Shadow_ESCD_Backup_Address+2], 'GFCA'
;R26A 		jne	short EEPROM_No_ESCD_Buffer_Now
;R26A 
;R26A 		mov	si, ESCD_REAL_BASE
;R26A 		mov	di, Shadow_ESCD_Backup_Address
;R26A 		mov	cx,word ptr es:[Shadow_ESCD_Backup_Address]
;R26A 		call	Backup_PnP_DMI_P6_Block
;R26A 
;R26A EEPROM_No_ESCD_Buffer_Now:
;R26A 		pop	ds
;R26A 		popad
;R26A 
;R26A 	;Write source buffer to PnP DMI CPU Backup Shadow
;R26A 
;R26A 		pushad
;R26A 
;R26A 		add	di, Shadow_DMI_Backup_Address
;R26A 
;R26A 		push	es
;R26A 		pushad
;R26A 		cli
;R26A 
;R26A 		push	ebp			;save parameter pointer
;R26A 		xor	eax,eax
;R26A 		call	Ct_Buffer_Request	;buffer request for saving status
;R26A 		shl	eax,1
;R26A 		call	Check_16Bit_Stack	;16-bit stack ?
;R26A 		jz	short @f		;jump if 16-bit stack
;R26A 
;R26A 		sub	esp, eax		;reduce stack for saving status
;R26A 		mov	ebp, esp		;status buffer index
;R26A 		jmp	short Copy_Source_Buffer_To_Shadow
;R26A @@:
;R26A 		sub	sp, ax
;R26A 		movzx	ebp, sp
;R26A 
;R26A Copy_Source_Buffer_To_Shadow:
;R26A 		call	Ct_Save_Shadow		;save shadow status
;R26A 		Call	Ct_Shadow_Write
;R26A 		rep	movsb
;R26A 		call	Ct_Restore_Shadow	;restore shadow status
;R26A 
;R26A 		xor	eax, eax
;R26A 		call	Ct_Buffer_Request
;R26A 
;R26A 		shl	eax,1
;R26A 		add	esp,eax
;R26A 		pop	ebp			;restore parameter pointer
;R26A 
;R26A 		clc
;R26A 		popad
;R26A 		pop	es
;R26A 
;R26A Input_Source_Buffer_Fail:
;R26A 		popad
;R26A 		pop	es
;R26A 
;R26A 	; ESCD DMI CPU code source from shadow
;R26A 		smsw	ax				;get machine word
;R26A 		test	al,1				;in protected mode ?
;R26A 		jnz	short Backup_in_Protect_mode2	;No, use original selector
;R26A 
;R26A 		push	0f000h
;R26A 		pop	ds
;R26A 		jmp	short @f
;R26A 
;R26A Backup_in_Protect_mode2:
;R26A 		cmp	di, ESCD_REAL_BASE
;R26A 		mov	ds, F43BiosSelector
;R26A 		je	short @f
;R26A 
;R26A ifdef	DMI_ENABLED
;R26A 		cmp	di, DMI_OFFSET
;R26A 		mov	ds, gds_bios_selector
;R26A 		je	short @f
;R26A endif;	DMI_ENABLED
;R26A 
;R26A 		push	0f000h
;R26A 		pop	ds
;R26A 
;R26A @@:
;R26A ifdef	DMI_ENABLED
;R26A 		mov	di, DMI_OFFSET
;R26A 		mov	cx, 1024*8			;8k size
;R26A 		mov	si, Shadow_DMI_Backup_Address
;R26A else;	DMI_ENABLED
;R26A 		mov	di, ESCD_REAL_BASE
;R26A 		xor	cx, cx
;R26A 		mov	si, Shadow_ESCD_Backup_Address
;R26A endif;	DMI_ENABLED
;R26A 		cmp	dword ptr ds:[Shadow_ESCD_Backup_Address+2], 'GFCA'
;R26A 		jne	short Write_PnP_DMI_CPU_Code_From_Shadow
;R26A 		add	cx,word ptr ds:[Shadow_ESCD_Backup_Address]
;R26A 		jmp	short Write_PnP_DMI_CPU_Code_From_Shadow
;R26A 
;R26A Backup_PnP_DMI_CPU_to_RAM:
;R26A 	; Copy ESCD and DMI and CPU update code to RAM
;R26A 
;R26A 		pushad
;R26A ifdef	DMI_ENABLED
;R26A 		mov	si, DMI_OFFSET			;Start address from
;R26A 							;fff8000:0
;R26A else;	DMI_ENABLED
;R26A 		mov	si, ESCD_REAL_BASE - 2000h
;R26A endif;	DMI_ENABLED
;R26A 		mov	cx, 1024*16			;16k size
;R26A 		push	PnP_DMI_CPU_Backup_Seg
;R26A 		pop	es
;R26A 		xor	di, di
;R26A 		call	Backup_PnP_DMI_P6_Block_to_RAM
;R26A 		popad
;R26A 
;R26A 	;Write source buffer to PnP DMI CPU Backup Segment
;R26A 		rep	movsb
;R26A 
;R26A 		push	PnP_DMI_CPU_Backup_Seg
;R26A 		pop	ds
;R26A 		xor	si, si				;Start address from
;R26A 							;5000:0
;R26A 		xor	di, di
;R26A 		mov	cx, 1024*16			;16k size
;R26A 
;R26A Write_PnP_DMI_CPU_Code_From_Shadow:
;R26A No_DMI_CPU_PnP_Backup:
;R26A endif;	Flash_2M_support
;R26A ;R26 - end

ifndef	ESCD_M2					;R13A
		mov	eax, cr0		;R13A
		mov	ax, TRUE		;R13A
		test	eax, CR0_CD		;R13A
		jz	CPU_Cache_On		;R13A
		mov	ax, FALSE		;R13A
CPU_Cache_On:					;R13A
		push	ax			;R13A;Save CPU cache status
						;R13A
		call	Disable_M1_BTB		;R13A;disable BTB of M1
						;R13A
else	;ESCD_M2				;R13A
		smsw	ax			;R15;get machine word
		push	ax			;R15;save machine word
		test	al,1			;R15;in protected mode ?
		jnz	short @f		;R15;yes, use original selector
;R15		test	bPnpModeFlag, PNP_PROT	;entry with protected mode ?
;R15		jnz	short @f		;yes, use original selector

		call	Check_A20_AndRecord	;read A20 status	;R17

		push	ds
		call	A20_On
		pop	ds

                lgdt    fword ptr cs:GDTR1

		mov	eax, cr0
		or	al, 1			; protect mode enable
		mov	cr0, eax

;R28		FAR_JMP DGROUP:$+4, 08H		; clear prefetch, bios cs

		mov	ax, ESCD_INDEX
;R29
ifdef	Flash_16K_8K_8K_Unit
ifdef   Support_CMOS_Backup_Data 
test	bPnpModeFlag,CMOS_FLASH_R_W  ;CMOS_FLASH_R_W is High
jz      short  Change_ESCD_W_BASE 

	      	mov	ax, 	F000_index
Change_ESCD_W_BASE:
endif  ;Support_CMOS_Backup_Data 
endif  ;Flash_16K_8K_8K_Unit
;R29

		mov	es, ax			; es = FFFF8000h

		mov	ax,F0DATA_INDEX		; R22
		mov	gs, ax			; R22 gs = F000h

;R28 - start
		mov	eax, cr0
		and 	al, NOT 01H		;go back to real mode
		mov	cr0, eax
;R28 - end

@@:
		push	ebp			;save parameter pointer
endif	;ESCD_M2				;R13A

		xor	eax,eax
		call	Ct_Buffer_Request	;buffer request for saving status

		shl	eax,1

ifndef	ESCD_M2					;R15
		push	ax			;R15
		smsw	ax			;R15
		test	al, 1			;R15
		pop	ax			;R15
		jz	short @f		;R15;jump if real mode
endif	;ESCD_M2				;R15
		call	Check_16Bit_Stack	;R15;16-bit stack ?
		jz	short @f		;R15;jump if 16-bit stack
;R15		test	bPnpModeFlag, PNP_STACK32
;R15		jz	short @f

		sub	esp, eax		;reduce stack for saving status
		mov	ebp, esp		;status buffer index
		jmp	short Flash_Write1

@@:
		sub	sp, ax
		movzx	ebp, sp
Flash_Write1:

		call	Ct_Save_Shadow		;save shadow status
		call	Ct_Enable_Flash		;enable Flash ROM write

ifndef	AUTO_DETECT_EEPROM_ID				;R23
;R23 ifndef	Flash_2M_support			;R19A
		mov	bx, offset Flash_Brand_Tbl

Try_Next_Flash_Brand:

		cmp	byte ptr cs:[bx], -1		;all tried ?
		je	short Flash_Write_Fail		;yes

		push	bx				;save table index

		mov	bl, cs:[bx]

;R23 else;	Flash_2M_support			;R19A
else;	AUTO_DETECT_EEPROM_ID				;R23
		
;R23		mov	bl,byte ptr cs:[0E05AH]
;R21		cmp	byte ptr cs:[bx], -1		;all tried ?
		cmp	byte ptr cs:[0E05AH],-1		;R21
		je	short Flash_Write_Fail		;yes
;R23 endif;	Flash_2M_support			;R19A
endif;	AUTO_DETECT_EEPROM_ID				;R23

		call	Do_Flash_Erase
;R23 ifndef	Flash_2M_support			;R19A
;R23		jc	short Write_This_Flash_Fail
;R23 else;	Flash_2M_support			;R19A
;R23 		jc	short Flash_Write_Fail	;R19A
;R23 endif;	Flash_2M_support			;R19A
ifndef	AUTO_DETECT_EEPROM_ID				;R23
		jc	short Write_This_Flash_Fail	;R23
else;	AUTO_DETECT_EEPROM_ID				;R23
 		jc	short Flash_Write_Fail		;R23
endif;	AUTO_DETECT_EEPROM_ID				;R23

		call	Do_Flash_Write
;R23 ifndef	Flash_2M_support			;R19A
;R23		jc	short Write_This_Flash_Fail
;R23 else;	Flash_2M_support			;R19A
;R23 		jc	short Flash_Write_Fail	;R19A
;R23 endif;	Flash_2M_support			;R19A
ifndef	AUTO_DETECT_EEPROM_ID				;R23
		jc	short Write_This_Flash_Fail	;R23
else;	AUTO_DETECT_EEPROM_ID				;R23
 		jc	short Flash_Write_Fail		;R23
endif;	AUTO_DETECT_EEPROM_ID				;R23

; Set ESCD successful

;R23 ifndef	Flash_2M_support			;R19A
ifndef	AUTO_DETECT_EEPROM_ID				;R23
 		pop	bx			;dummy pop
endif;	AUTO_DETECT_EEPROM_ID				;R23
;R23 endif;	Flash_2M_support			;R19A

		call	Ct_Restore_Shadow	;restore shadow status

		xor	eax, eax
		call	Ct_Buffer_Request

		shl	eax,1
		add	esp,eax

ifndef	ESCD_M2					;R13A
		call	Enable_M1_BTB		;R13A;disable BTB of M1
						;R13A
		pop	ax	 		;R13A;Restore CPU cache status
		call	CPU_Cache		;R13A
else	;ESCD_M2				;R13A
		pop	ebp			;restore parameter pointer

		pop	ax			;R15;original machine word
		test	al,1			;R15;in protected mode ?
		jnz	short @f		;R15;yes, no change mode
;R15		test	bPnpModeFlag, PNP_PROT	;entry with protected mode ?
;R15		jnz	short @f		;yes, no change selector

;R28		mov	eax, cr0
;R28		and 	al, NOT 01H		;go back to real mode
;R28		mov	cr0, eax
;R28
;R28		FAR_JMP	DGROUP:$+4, 0F000h	;flush CPU instruction queue

;R17 - start
		test	bPnpModeFlag, A20_HIGH	;the original state of A20 is on ?
		jnz	short @F
;R17 - end

		push	ds
		call	A20_Off
		pop	ds

@@:
endif	;ESCD_M2				;R13A
		popad
		popf
		clc
		ret

;R23 ifndef	Flash_2M_support			;R19A
ifndef	AUTO_DETECT_EEPROM_ID				;R23
Write_This_Flash_Fail:

		pop	bx				;restore table index
 		inc	bx				;next flash
		jmp	short Try_Next_Flash_Brand
endif;	AUTO_DETECT_EEPROM_ID				;R23
;R23 endif;	Flash_2M_support			;R19A

Flash_Write_Fail:

		call	Ct_Restore_Shadow	;restore shadow status

		xor	eax, eax
		call	Ct_Buffer_Request

		shl	eax,1
		add	esp,eax

ifndef	ESCD_M2					;R13A
		call	Enable_M1_BTB		;R13A;disable BTB of M1
						;R13A
		pop	ax	 		;R13A;Restore CPU cache status
		call	CPU_Cache		;R13A
else	;ESCD_M2				;R13A
		pop	ebp			;restore parameter pointer

		pop	ax			;R15;original machine word
		test	al,1			;R15;in protected mode ?
		jnz	short @f		;R15;yes, no change mode
;R15		test	bPnpModeFlag, PNP_PROT	;entry with protected mode ?
;R15		jnz	short @f		;yes, no change selector

;R28		mov	eax, cr0
;R28		and 	al, NOT 01H		;go back to real mode
;R28		mov	cr0, eax
;R28
;R28		FAR_JMP	DGROUP:$+4, 0F000h	;flush CPU instruction queue

;R17 - start
		test	bPnpModeFlag, A20_HIGH	;the original state of A20 is on ?
		jnz	short @F
;R17 - end

		push	ds
		call	A20_Off
		pop	ds

@@:
endif	;ESCD_M2				;R13A
		popad
		popf
		stc
		ret
;R13 - end
;R13		mov	bh, 01h			;indicates a Flash Write
;R13		call	Flash_Erase_Or_Flash_Write
;R13		ret
Flash_Write	endp

;R26A ;R26 - start
;R26A ifdef	Flash_2M_support
;R26A Backup_PnP_DMI_P6_Block	proc	near
;R26A 		push	es
;R26A 		pushad
;R26A 		cli
;R26A 
;R26A 		smsw	ax			;get machine word
;R26A 		push	ax			;save machine word
;R26A 		test	al,1			;in protected mode ?
;R26A 		jnz	short @f		;yes, use original selector
;R26A 
;R26A 		call	Check_A20_AndRecord	;read A20 status
;R26A 
;R26A 		push	ds
;R26A 		call	A20_On
;R26A 		pop	ds
;R26A 
;R26A                 lgdt    fword ptr cs:GDTR1
;R26A 
;R26A 		mov	eax, cr0
;R26A 		or	al, 1			;protect mode enable
;R26A 		mov	cr0, eax
;R26A 
;R26A 		FAR_JMP DGROUP:$+4, 08H		;clear prefetch, bios cs
;R26A 
;R26A 		mov	ax, ESCD_INDEX
;R26A 		mov	ds, ax			;ds = FFFF8000h
;R26A 
;R26A @@:
;R26A 		push	ebp			;save parameter pointer
;R26A 
;R26A 		xor	eax,eax
;R26A 		call	Ct_Buffer_Request	;buffer request for saving status
;R26A 
;R26A 		shl	eax,1
;R26A 
;R26A 		call	Check_16Bit_Stack	;16-bit stack ?
;R26A 		jz	short @f		;jump if 16-bit stack
;R26A 
;R26A 		sub	esp, eax		;reduce stack for saving status
;R26A 		mov	ebp, esp		;status buffer index
;R26A 		jmp	short Backup_PnP_DMI_P6_Block_1
;R26A 
;R26A @@:
;R26A 		sub	sp, ax
;R26A 		movzx	ebp, sp
;R26A 
;R26A Backup_PnP_DMI_P6_Block_1:
;R26A 
;R26A 		call	Ct_Save_Shadow		;save shadow status
;R26A 		Call	Ct_Shadow_Write
;R26A 		rep	movsb
;R26A 		call	Ct_Restore_Shadow	;restore shadow status
;R26A 
;R26A 		xor	eax, eax
;R26A 		call	Ct_Buffer_Request
;R26A 
;R26A 		shl	eax,1
;R26A 		add	esp,eax
;R26A 
;R26A 		pop	ebp			;restore parameter pointer
;R26A 
;R26A 		pop	ax			;original machine word
;R26A 		test	al,1			;in protected mode ?
;R26A 		jnz	short @f		;yes, no change mode
;R26A 
;R26A 		mov	eax, cr0
;R26A 		and 	al, NOT 01H		;go back to real mode
;R26A 		mov	cr0, eax
;R26A 
;R26A 		FAR_JMP	DGROUP:$+4, 0F000h	;flush CPU instruction queue
;R26A 
;R26A 		test	bPnpModeFlag, A20_HIGH	;the original state of A20 is on ?
;R26A 		jnz	short @F
;R26A 
;R26A 		push	ds
;R26A 		call	A20_Off
;R26A 		pop	ds
;R26A 
;R26A @@:
;R26A 		clc
;R26A 		popad
;R26A 		pop	es
;R26A 		ret
;R26A Backup_PnP_DMI_P6_Block	endp
;R26A 
;R26A Backup_PnP_DMI_P6_Block_to_RAM	proc	near
;R26A 		push	ds
;R26A 		push	es
;R26A 		pushad
;R26A 		cli
;R26A 
;R26A 		smsw	ax			;get machine word
;R26A 		push	ax			;save machine word
;R26A 		test	al,1			;in protected mode ?
;R26A 		jnz	short @f		;yes, use original selector
;R26A 
;R26A 		call	Check_A20_AndRecord	;read A20 status
;R26A 
;R26A 		push	ds
;R26A 		call	A20_On
;R26A 		pop	ds
;R26A 
;R26A                 lgdt    fword ptr cs:GDTR1
;R26A 
;R26A 		mov	eax, cr0
;R26A 		or	al, 1			;protect mode enable
;R26A 		mov	cr0, eax
;R26A 
;R26A 		FAR_JMP DGROUP:$+4, 08H		;clear prefetch, bios cs
;R26A 
;R26A 		mov	ax, ESCD_INDEX
;R26A 		mov	ds, ax			;ds = FFFF8000h
;R26A 
;R26A @@:
;R26A 		rep	movsb
;R26A 
;R26A 		pop	ax			;original machine word
;R26A 		test	al,1			;in protected mode ?
;R26A 		jnz	short @f		;yes, no change mode
;R26A 
;R26A 		mov	eax, cr0
;R26A 		and 	al, NOT 01H		;go back to real mode
;R26A 		mov	cr0, eax
;R26A 
;R26A 		FAR_JMP	DGROUP:$+4, 0F000h	;flush CPU instruction queue
;R26A 
;R26A 		test	bPnpModeFlag, A20_HIGH	;the original state of A20 is on ?
;R26A 		jnz	short @F
;R26A 
;R26A 		push	ds
;R26A 		call	A20_Off
;R26A 		pop	ds
;R26A 
;R26A @@:
;R26A 		clc
;R26A 		popad
;R26A 		pop	es
;R26A 		pop	ds
;R26A 		ret
;R26A Backup_PnP_DMI_P6_Block_to_RAM	endp
;R26A 
;R26A endif;	Flash_2M_support
;R26A ;R26 - end
;R32 - start
else;	Flash_IN_SMBASE
;[]========================================================================[]
;Procedure:	Flash_Write
;Function :
;Input    :	DS:SI = point to source buffer
;		ES:DI = point to distinct buffer (Range = FD000h-FDFFFh)
;		CX = length
;
;Output   :	CF = 0 successful
;		CF = 1 fail
;Registers:
;Note	  :
;[]========================================================================[]
		extrn	Flash_service:near

		public	Flash_Write
Flash_Write	proc	near
		pushf
		push	bx
;R34		push	gs
;R34		push	es
;R34		pop	gs		;gs = wBiosSelector
		call	Turn_ON_A20
		mov	bx,1
		call	Flash_service
		call	Return_A20_Status
;R34		pop	gs
		pop	bx
		jc	short Flash_Write_Fail
		popf
		clc
		ret
Flash_Write_Fail:
		popf
		stc
		ret
Flash_Write	endp

;[]==================================================[]
;Procedure:	Flash_Read
;Function :
;Input    :	DS:SI = point to source buffer (Range = FD000h-FDFFFh)
;		ES:DI = point to distinct buffer
;		CX = length
;Output   :	CF = 0 successful
;		CF = 1 fail
;Registers:
;Note	  :
;[]==================================================[]
		public	Flash_Read
Flash_Read	proc	near
		pushf
		push	bx
;R34		push	gs
;R34		push	ds
;R34		pop	gs		;gs = wBiosSelector
		call	Turn_ON_A20
		xor	bx,bx
		call	Flash_service
		call	Return_A20_Status
;R34		pop	gs
		pop	bx
		popf
		clc
		ret
Flash_Read	endp

;R33 - start
;[]==================================================[]
;Procedure:	Flash_Part_Erase
;Function :
;Input    :	
;		EDI = point to clear address
;		CX = clear length
;Output   :	CF = 0 successful
;		CF = 1 fail
;Registers:
;Note	  :
;[]==================================================[]
		public 	Flash_Part_Erase
Flash_Part_Erase	Proc
		pushf
		push	bx
		push	gs
		push	es
		pop	gs		;gs = wBiosSelector
		call	Turn_ON_A20
		mov	bx,2
		call	Flash_service
		call	Return_A20_Status
		pop	gs
		pop	bx
		jc	short Flash_Clear_Fail
		popf
		clc
		ret
Flash_Clear_Fail:
		popf
		stc
		ret
Flash_Part_Erase	endp
;R33 - end

;[]==================================================[]
;Procedure:	Trun on A20
;Function :
;Input    :	None
;Output   :	None
;Registers:	None
;Note	  :
;[]==================================================[]

Turn_ON_A20:
		push	ax
		smsw	ax			;get machine word
		test	al,1			;in protected mode ?
		jnz	short Turn_ON_A20_Ret	;yes, use original selector

		call	Check_A20_AndRecord	;read A20 status
		push	ds
		call	A20_On
		pop	ds
Turn_ON_A20_Ret:

		pop	ax
		ret
;[]==================================================[]
;Procedure:	Restore  A20
;Function :
;Input    :	None
;Output   :	None
;Registers:	None
;Note	  :
;[]==================================================[]

Return_A20_Status:
		pushf
		push	ax
		smsw	ax			;get machine word
		test	al,1			;in protected mode ?
		jnz	short Return_A20_Status_Ret	;yes, use original selector

		test	bPnpModeFlag, A20_HIGH	;the original state of A20 is on ?
		jnz	short Return_A20_Status_Ret
		push	ds
		call	A20_Off
		pop	ds
Return_A20_Status_Ret:
		pop	ax
		popf
		ret


endif;	Flash_IN_SMBASE
;R32 - end
ifndef	Flash_IN_SMBASE				;R32

;[]========================================================================[]
;Procedure:	Flash_Erase_Or_Flash_Write
;Function :
;Input    :	DS:SI = point to source buffer
;		ES:DI = point to distinct buffer (Range = FD000h-FDFFFh)
;		CX = length
;		BH = 0 : Flash Erase
;		     1 : Flash Write
;		BL = 1      : Do SST flash
;		     2      : Do Intel 28F001BXT flash
;		     3      : Do MXIC 28F001BXT flash
;		     others : Reserved for future 
;Output   :	CF = 0 successful
;		CF = 1 fail
;Registers:
;Note	  :
;[]========================================================================[]
;R13Flash_Erase_Or_Flash_Write	Proc	Near
;R13
;R13		pushf
;R13		cli
;R13
;R13		pushad
;R13		xor	eax,eax
;R13		call	Ct_Buffer_Request	;buffer request for saving status
;R13		shl	eax,1
;R13		call	Check_16Bit_Stack
;R13		jz	short @f
;R13		sub	esp, eax		;reduce stack for saving status
;R13		mov	ebp, esp		;status buffer index
;R13		jmp	short Flash_Write1
;R13@@:
;R13		sub	sp, ax
;R13		movzx	ebp, sp
;R13Flash_Write1:
;R13
;R13		push	bx
;R13		call	Ct_Save_Shadow		;save shadow status
;R13		call	Ct_Enable_Flash		;enable Flash ROM write
;R13		pop	bx
;R13
;R13		or	bh, bh			;execute a Flash Erase ?
;R13		jz	short _Do_Erase		;yes !
;R13		call	Do_Flash_Write		;program FD000h block
;R13		jmp	short _End_Flash_Write
;R13
;R13	_Do_Erase:
;R13
;R13		call	Do_Flash_Erase		;program FD000h block
;R13
;R13	_End_Flash_Write:
;R13
;R13		pushf
;R13		call	Ct_Restore_Shadow	;restore shadow status
;R13		xor	eax, eax
;R13		call	Ct_Buffer_Request
;R13		pop	bp
;R13
;R13		shl	eax,1
;R13		add	esp,eax
;R13
;R13		push	bp			;restore flag
;R13		popf
;R13
;R13		popad
;R13
;R13		jc	short @f
;R13		popf
;R13		clc
;R13		ret
;R13@@:
;R13		popf
;R13		stc
;R13		ret
;R13
;R13Flash_Erase_Or_Flash_Write	Endp

;[]========================================================================[]
;Input	:	BL = 1      : Do SST flash Erase
;		     2      : Do Intel 28F001BXT flash Erase
;		     3      : Do MXIC 28F001BXT flash Erase
;		     others : Reserved for future 
;
;Output	:	CF = Erase fail
;		NC = Erase Successfully
;[]========================================================================[]
Do_Flash_Erase	Proc	Near

ifndef	AUTO_DETECT_EEPROM_ID				;R23
		push	bx			;R19A
;R23 ifndef	Flash_2M_support			;R19A
		cmp	bl, 1
		jne	short @F
		call	SST010_Flash_Erase
		jmp	short End_Do_Flash_Erase
	@@:
		cmp	bl, 2
		jne	short @F
		call	F001BXT_Flash_Erase
		jmp	short End_Do_Flash_Erase
	@@:
		cmp	bl, 3
		jne	short @F
		call	MXIC_Flash_Erase
		jmp	short End_Do_Flash_Erase
	@@:
ifdef	AMD29F010					;R18
		cmp	bl, 4				;R18
		jne	short @F			;R18
		call	AMD29F_Flash_Erase		;R18
		jmp	short End_Do_Flash_Erase	;R18
	@@:						;R18
endif;	AMD29F010					;R18
;R19A ifdef	AM29F002T					;R19
;R19A 		cmp	bl, 5				;R19
;R19A 		jne	short @f			;R19
;R19A 		call	AM29F002T_Flash_Erase		;R19
;R19A 		jmp	short End_Do_Flash_Erase	;R19
;R19A 		@@:					;R19
;R19A endif;	AM29F002T					;R19
;R19A - start
;R23 else;	Flash_2M_support
;R20		cmp	bl, 1
;R20		jne	short @F
;R20		mov	bx,word ptr cs:[_2M_FLASH_ERASE_ENTRY_POINT]
;R20		call	bx
;R20		jmp	short End_Do_Flash_Erase
;R20	@@:
;R20		cmp	bl, 2
;R20		jne	short @f
;R20		mov	bx,word ptr cs:[_2M_FLASH_ERASE_ENTRY_POINT]
;R20		call	bx
;R20		jmp	short End_Do_Flash_Erase
;R20	@@:
;R20 		cmp	bl, 3
;R20		jne	short @f
;R20		mov	bx,word ptr cs:[_2M_FLASH_ERASE_ENTRY_POINT]
;R20		call	bx
;R20		jmp	short End_Do_Flash_Erase
;R20	@@:
;R20 - start
;R21 		cmp	bl, 4
;R23 		cmp	bl, 5				;R21
;R23 		ja	short @f
;R23		mov	bx,word ptr cs:[_2M_FLASH_ERASE_ENTRY_POINT]
;R23		call	bx
;R23 		jnc	short End_Do_Flash_Erase
;R23 		jmp	short End_Do_Flash_Erase
;R23 	@@:
;R20 - end
;R23 endif;	Flash_2M_support
;R19A - end
		stc

End_Do_Flash_Erase:

		pop	bx			;R19A
;R23 - start
else;	AUTO_DETECT_EEPROM_ID
		mov	bx,word ptr cs:[FLASH_ERASE_ENTRY_POINT];R23
		call	bx
endif;	AUTO_DETECT_EEPROM_ID
;R23 - end

		ret
Do_Flash_Erase	Endp

;[]========================================================================[]
;Input	:	BL = 1      : Do SST flash Write
;		     2      : Do Intel 28F001BXT flash Write
;		     3      : Do MXIC 28F001BXT flash Write
;		     others : Reserved for future 
;
;Output	:	CF = Write fail
;		NC = Write Successfully
;[]========================================================================[]
Do_Flash_Write	Proc	Near

ifndef	AUTO_DETECT_EEPROM_ID				;R23
;R23 ifndef	Flash_2M_support				;R19A
		cmp	bl, 1
		jne	short @F
		call	SST010_Flash_Write
		jmp	short End_Do_Flash_Write
	@@:
		cmp	bl, 2
		jne	short @F
		call	F001BXT_Flash_Write
		jmp	short End_Do_Flash_Write
	@@:
		cmp	bl, 3
		jne	short @F
		call	MXIC_Flash_Write
		jmp	short End_Do_Flash_Write
	@@:
ifdef	AMD29F010					;R18
		cmp	bl, 4				;R18
		jne	short @F			;R18
		call	AMD29F_Flash_Write		;R18
		jmp	short End_Do_Flash_Write	;R18
	@@:						;R18
endif;	AMD29F010					;R18
;R19A ifdef	AM29F002T					;R19
;R19A 		cmp	bl, 5				;R19
;R19A 		jne	short @f			;R19
;R19A 		call	AM29F002T_Flash_Write		;R19
;R19A 		jmp	short End_Do_Flash_Write	;R19
;R19A 		@@:					;R19
;R19A endif;	AM29F002T					;R19
;R19A - start
;R23 else;	Flash_2M_support
;R20		cmp	bl, 1
;R20		jne	short @F
;R20		mov	bx,word ptr cs:[_2M_FLASH_WRITE_ENTRY_POINT]
;R20		call	bx
;R20		jmp	short End_Do_Flash_Write
;R20	@@:
;R20		cmp	bl, 2
;R20		jne	short @f
;R20		mov	bx,word ptr cs:[_2M_FLASH_WRITE_ENTRY_POINT]
;R20		call	bx
;R20		jmp	short End_Do_Flash_Write
;R20	@@:
;R20 		cmp	bl, 3
;R20		jne	short @f
;R20		mov	bx,word ptr cs:[_2M_FLASH_WRITE_ENTRY_POINT]
;R20		call	bx
;R20		jmp	short End_Do_Flash_Write
;R20	@@:
;R20 - start
;R21 		cmp	bl, 4
;R23  	cmp	bl, 5				;R21
;R23 		ja	short @f
;R23		mov	bx,word ptr cs:[_2M_FLASH_WRITE_ENTRY_POINT]
;R23		call	bx
;R23 		jnc	short End_Do_Flash_Write
;R23 		jmp	short End_Do_Flash_Write
;R23 	@@:
;R20 - end
;R23 endif;	Flash_2M_support
;R19A - end
		stc

	End_Do_Flash_Write:

;R23 - start
else;	AUTO_DETECT_EEPROM_ID
		mov	bx,word ptr cs:[FLASH_WRITE_ENTRY_POINT]
		call	bx
endif;	AUTO_DETECT_EEPROM_ID
;R23 - end

		ret

Do_Flash_Write	Endp
endif;	Flash_IN_SMBASE				;R32

;[]====================================================================[]
; Delay 500ms for erasing completed
; Check refresh toggle bit
;[]====================================================================[]
		public	Erase_Delay			;R23
Erase_Delay	proc	near
		push	bx
		push	cx
		xor	bx, bx
		mov	cx, 17000
		call	Wait_Refresh
		pop	cx
		pop	bx
		ret
Erase_Delay	endp

;[]====================================================================[]
; Delay 10us for programming completed
; Check refresh toggle bit
;[]====================================================================[]
		public	Write_Delay			;R23
Write_Delay	proc	near
		push	bx
		push	cx
		xor	bx, bx
ifdef	Speed_Down_EEPROM_Update			;R25
		mov	cx, 20				;R25
else;	Speed_Down_EEPROM_Update			;R25
		mov	cx, 10				;R25
endif;	Speed_Down_EEPROM_Update			;R25
;R25		mov	cx, 2
		call	Wait_Refresh
		pop	cx
		pop	bx
		ret
Write_Delay	endp
;R09 - ends

;R09;[]========================================================================[]
;R09;Procedure:	Flash_Erase
;R09;Function :
;R09;Input    :	none
;R09;Output   :	CF = 0 successful
;R09;		CF = 1 fail
;R09;Registers:
;R09;Note	  :
;R09;[]========================================================================[]
;R09
;R09Flash_Erase	proc	near
;R09
;R09		pushf			;R01
;R09		cli
;R09
;R09		pushad
;R09		xor	eax,eax
;R09		call	Ct_Buffer_Request	;buffer request for saving status
;R09		shl	eax,1
;R09		call	Check_16Bit_Stack	;R03
;R09		jz	short @f		;R03
;R09		sub	esp, eax		;reduce stack for saving status
;R09		mov	ebp, esp		;status buffer index
;R09		jmp	short Flash_Erase1	;R03
;R09@@:						;R03
;R09		sub	sp, ax			;R03
;R09		movzx	ebp, sp			;R03
;R09Flash_Erase1:					;R03
;R09
;R09		call	Ct_Save_Shadow		;save shadow status
;R09		call	Ct_Enable_Flash		;enable Flash ROM write
;R09		call	Ct_Flash_Erase		;erase FD000h block
;R09
;R09		pushf				;save flag
;R09		call	Ct_Restore_Shadow	;restore shadow status
;R09		xor	eax,eax
;R09		call	Ct_Buffer_Request
;R09		pop	bp			;restore flag to stack
;R09
;R09		shl	eax,1
;R09		add	esp, eax		;reduce stack for saving status
;R09
;R09		push	bp			;restore flag
;R09		popf
;R09
;R09		popad
;R09
;R09;R01		sti
;R09;R01		ret
;R09		jc	short @f	;R01
;R09		popf			;R01
;R09		clc			;R01
;R09		ret			;R01
;R09@@:					;R01
;R09		popf			;R01
;R09		stc			;R01
;R09		ret			;R01
;R09Flash_Erase	endp

;R09;[]========================================================================[]
;R09;Procedure:	Flash_Write
;R09;Function :
;R09;Input    :	DS:SI = point to source buffer
;R09;		ES:DI = point to distinct buffer (Range = FD000h-FDFFFh)
;R09;		CX = length
;R09;Output   :	CF = 0 successful
;R09;		CF = 1 fail
;R09;Registers:
;R09;Note	  :
;R09;[]========================================================================[]
;R09		public	Flash_Write
;R09Flash_Write	proc	near
;R09
;R09		pushf			;R01
;R09		cli
;R09
;R09		pushad
;R09		xor	eax,eax
;R09		call	Ct_Buffer_Request	;buffer request for saving status
;R09		shl	eax,1
;R09		call	Check_16Bit_Stack	;R03
;R09		jz	short @f		;R03
;R09		sub	esp, eax		;reduce stack for saving status
;R09		mov	ebp, esp		;status buffer index
;R09		jmp	short Flash_Write1	;R03
;R09@@:						;R03
;R09		sub	sp, ax			;R03
;R09		movzx	ebp, sp			;R03
;R09Flash_Write1:					;R03
;R09
;R09		call	Ct_Save_Shadow		;save shadow status
;R09		call	Ct_Enable_Flash		;enable Flash ROM write
;R09		call	Ct_Flash_Write		;program FD000h block
;R09
;R09		pushf
;R09		call	Ct_Restore_Shadow	;restore shadow status
;R09		xor	eax, eax
;R09		call	Ct_Buffer_Request
;R09		pop	bp
;R09
;R09		shl	eax,1
;R09		add	esp,eax
;R09
;R09		push	bp			;restore flag
;R09		popf
;R09
;R09		popad
;R09
;R09;R01		sti
;R09;R01		ret
;R09		jc	short @f	;R01
;R09		popf			;R01
;R09		clc			;R01
;R09		ret			;R01
;R09@@:					;R01
;R09		popf			;R01
;R09		stc			;R01
;R09		ret			;R01
;R09Flash_Write	endp

ifndef	Flash_IN_SMBASE			;R32
;[]========================================================================[]
;Procedure:	Flash_Read
;Function :
;Input    :	DS:SI = point to source buffer (Range = FD000h-FDFFFh)
;		ES:DI = point to distinct buffer
;		CX = length
;Output   :	CF = 0 successful
;		CF = 1 fail
;Registers:
;Note	  :
;[]========================================================================[]
		public	Flash_Read
Flash_Read	proc	near

;R13 - start
		pushf
		pushad
		cli

ifdef	ESCD_M2					;R13A
		smsw	ax			;R15;get machine word
		push	ax			;R15;save machine word
		test	al,1			;R15;in protected mode ?
		jnz	short @f		;R15;yes, use original selector
;R15		test	bPnpModeFlag, PNP_PROT	;entry with protected mode ?
;R15		jnz	short @f		;yes, use original selector

		call	Check_A20_AndRecord	;read A20 status	;R17

		push	ds
		call	A20_On
		pop	ds

                lgdt    fword ptr cs:GDTR1

		mov	eax, cr0
		or	al, 1			; protect mode enable
		mov	cr0, eax

;R28		FAR_JMP DGROUP:$+4, 08H		; clear prefetch, bios cs

		mov	ax, ESCD_INDEX
;R29
ifdef	Flash_16K_8K_8K_Unit
ifdef  Support_CMOS_Backup_Data 
test	bPnpModeFlag,CMOS_FLASH_R_W  ;CMOS_FLASH_R_W is High
jz      short  Change_ESCD_R_BASE 

	      	mov	ax, F000_index
Change_ESCD_R_BASE:
endif  ;Support_CMOS_Backup_Data 
endif  ;Flash_16K_8K_8K_Unit
;R29

		mov	ds, ax			; ds = 00000000h

		mov	ax,F0DATA_INDEX		; R22
		mov	gs, ax			; R22 gs = F000h

;R28 - start
		mov	eax, cr0
		and 	al, NOT 01H		;go back to real mode
		mov	cr0, eax
;R28 - end

@@:
		push	ebp			;save parameter pointer
endif	;ESCD_M2				;R13A

		xor	eax,eax
		call	Ct_Buffer_Request	;buffer request for saving status

		shl	eax,1

ifndef	ESCD_M2					;R15
		push	ax			;R15
		smsw	ax			;R15
		test	al, 1			;R15
		pop	ax			;R15
		jz	short @f		;R15;jump if real mode
endif	;ESCD_M2				;R15
		call	Check_16Bit_Stack	;R15;16-bit stack ?
		jz	short @f		;R15;jump if 16-bit stack
;R15		test	bPnpModeFlag, PNP_STACK32
;R15		jz	short @f

		sub	esp, eax		;reduce stack for saving status
		mov	ebp, esp		;status buffer index
		jmp	short Flash_Read1

@@:
		sub	sp, ax
		movzx	ebp, sp
Flash_Read1:

		call	Ct_Save_Shadow		;save shadow status
		call	Ct_Enable_Flash		;enable Flash ROM write

ifdef	ESCD_M2					;R13A
		rep	movsb
else	;ESCD_M2				;R13A
		call	Ct_GetAll_Flash		;R13A
endif	;ESCD_M2				;R13A

		call	Ct_Restore_Shadow	;restore shadow status

		xor	eax, eax
		call	Ct_Buffer_Request

		shl	eax,1
		add	esp,eax

ifdef	ESCD_M2					;R13A
		pop	ebp			;restore parameter pointer

		pop	ax			;R15;original machine word
		test	al,1			;R15;in protected mode ?
		jnz	short @f		;R15;yes, no change mode
;R15		test	bPnpModeFlag, PNP_PROT	;entry with protected mode ?
;R15		jnz	short @f		;yes, no change selector

;R28		mov	eax, cr0
;R28		and 	al, NOT 01H		;go back to real mode
;R28		mov	cr0, eax
;R28
;R28		FAR_JMP	DGROUP:$+4, 0F000h	;flush CPU instruction queue

;R17 - start
		test	bPnpModeFlag, A20_HIGH	;the original state of A20 is on ?
		jnz	short @F
;R17 - end

		push	ds
		call	A20_Off
		pop	ds

@@:
endif	;ESCD_M2				;R13A
		popad
		popf
		clc
		ret
;R13 - end
;R13		pushf			;R01
;R13		cli
;R13
;R13		pushad
;R13		push	ds
;R13		push	es
;R13
;R13		xor	eax,eax
;R13		call	Ct_Buffer_Request	;buffer request for saving status
;R13		shl	eax,1
;R13		call	Check_16Bit_Stack	;R03
;R13		jz	short @f		;R03
;R13		sub	esp, eax		;reduce stack for saving status
;R13		mov	ebp, esp		;status buffer index
;R13		jmp	short Flash_Read1	;R03
;R13@@:						;R03
;R13		sub	sp, ax			;R03
;R13		movzx	ebp, sp			;R03
;R13Flash_Read1:					;R03
;R13
;R13		call	Ct_Save_Shadow		;save shadow status
;R13		call	Ct_Enable_Flash		;enable Flash ROM write
;R13
;R13;R05		mov	ax, es
;R13;R05		push	ds
;R13;R05		pop	es
;R13;R05		mov	ds, ax
;R13;R05		xchg	si, di
;R13;R05@@:
;R13;R05		call	Ct_Get_Flash
;R13;R05		mov	ds:[si], al
;R13;R05		inc	si
;R13;R05		inc	di
;R13;R05		loop	short @b
;R13
;R13		call	Ct_GetAll_Flash		;R05
;R13
;R13		call	Ct_Restore_Shadow	;restore shadow status
;R13
;R13		xor	eax, eax
;R13		call	Ct_Buffer_Request
;R13		shl	eax,1
;R13		add	esp,eax
;R13
;R13		pop	es
;R13		pop	ds
;R13		popad
;R13
;R13;R01		sti
;R13;R01		ret
;R13		jc	short @f	;R01
;R13		popf			;R01
;R13		clc			;R01
;R13		ret			;R01
;R13@@:					;R01
;R13		popf			;R01
;R13		stc			;R01
;R13		ret			;R01
Flash_Read	endp
endif;	Flash_IN_SMBASE				;R32

;R28A - start
;[]========================================================================[]
;Procedure:	Post_Flash_Write
;Function :	For calling Flash_Write during POST, let the parameters in
;		stack are identical with PNPBIOS.
;Input    :	same as Flash_Write
;Output   :	same as Flash_Write
;[]========================================================================[]
ifdef	DMI_ENABLED					;R34
		public	Post_Flash_Write
Post_Flash_Write	proc	near
		push	edi				;R34
		push	ebp
		movzx	ebp, sp
		sub	sp, DEFAULT_STACK+PARA_SIZE
		mov	bPnpModeFlag, 0
		push	DGROUP				;R34
		pop	gs				;R34
		call	Write_DMI			;R34
;R34		call	Flash_Write
		jc	short Post_Flash_Write_Fail
		add	sp, DEFAULT_STACK+PARA_SIZE
		clc
		pop	ebp
		pop	edi				;R34
		ret
Post_Flash_Write_Fail:
		add	sp, DEFAULT_STACK+PARA_SIZE
		stc
		pop	ebp
		pop	edi				;R34
		ret
Post_Flash_Write	endp
endif;	DMI_ENABLED					;R34

;[]========================================================================[]
;Procedure:	Post_Flash_Read
;Function :	For calling Flash_Read during POST, let the parameters in
;		stack are identical with PNPBIOS.
;Input    :	same as Flash_Read
;Output   :	same as Flash_Read
;[]========================================================================[]
ifdef	DMI_ENABLED 					;R34
		public	Post_Flash_Read
Post_Flash_Read	proc	near
		push	esi				;R34
		push	ebp
		movzx	ebp, sp
		sub	sp, DEFAULT_STACK+PARA_SIZE
		mov	bPnpModeFlag, 0
		push	DGROUP				;R34
		pop	gs				;R34
		mov	ax,cx				;R34
		call	Read_DMI			;R34
;R34		call	Flash_Read
		jc	short Post_Flash_Read_Fail
		add	sp, DEFAULT_STACK+PARA_SIZE
		clc
		pop	ebp
		pop	esi				;R34
		ret
Post_Flash_Read_Fail:
		add	sp, DEFAULT_STACK+PARA_SIZE
		stc
		pop	ebp
		pop	esi				;R34
		ret
Post_Flash_Read	endp
endif;	DMI_ENABLED					;R34
;R28A - end

ifdef	ESCD_M2					;R13A
ifdef	AUTO_DETECT_EEPROM_ID			;R23
		public	Ct_Get_Flash		;R23
endif;	AUTO_DETECT_EEPROM_ID			;R23
Ct_Get_Flash:					;R13A
		mov	al, es:[di]		;R13A
		ret				;R13A
Ct_Set_Flash:					;R13A
		mov	es:[di], al		;R13A
		ret				;R13A
else	;ESCD_M2				;R13A
;R05 - start
;Input : none
;Output: none
;Note  : routine to disable BTB (branch targe buffer)
Disable_M1_BTB	proc	near

		pushad

	;disable L1 cache
		mov	al,FALSE
		call	Cpu_Cache

;R07		mov	si,0fH
		mov	si,0dh				;R07
		call	Set_BtB

		popad

		ret
Disable_M1_BTB	endp

;Input : none
;Output: none
;Note  : routine to enable BTB (branch targe buffer)
Enable_M1_BTB	proc	near
		pushf

		pushad

		xor	si,si		;enable BTB
		call	Set_BtB

;R08	;Enable L1 cache
;R08		mov	al,TRUE
;R08		call	Cpu_Cache

		popad

		popf
		ret
Enable_M1_BTB	endp

;Input : si - value to set for BTB
;	    = 0 to enable BTB
;	    = non-zero to disable BTB
;Output: none
;Note  : routine to program BTB register
Set_BtB		proc	near

	;do nothing if not M1(6x86) CPU plugged
		mov	al,CMOS_AWARD_2
		call	Get_Cmos
		and	al,CPU_TYPE_MASK

;R16 - start
;Disable/Enable BTB of PENTIUM CPU to fix updating ESCD problem for P55C CPU
		cmp	al,TYPE_586	;PENTIUM CPU ?		
		public	P5_BtB_Loc				;R16A
P5_BtB_Loc:							;R16A
;Note : Don't change this instruction , this line will be 	;R16A
;       changed to "jmp short Not_P5_Cpu" by CPU ID detection	;R16A
;	in CPUPOST.ASM (R09)					;R16A
		jne	short Not_P5_Cpu;no, check other CPU	

	; deal with MSR(Model Specific Register) TR12

		mov	ecx,0eh		; select TR12
		RDMSR			; read TR12->edx:eax
		or	al,1		; disable NBP(No Branch Prediction) by
					; or 1's to bit 0

		or	si,si		; to disable BTB
		jnz	short Yes_DisBtB

		and	al,NOT 01H	; enable BTB
Yes_DisBtB:

		mov	ecx,0eh		; select TR12
		WRMSR			; write edx:eax ->TR12

	; flush BTB, prefetch queue

		mov	ecx,cr3		; move CR3 content to ECX
		mov	cr3,ecx		; write ECX content back to CR3
					; to get BTB and prefetch queue flushed

		jmp	short Not_Cx6x86
Not_P5_Cpu:	
;R16 - end

		cmp	al,TYPE_M1
		jne	short Not_Cx6x86

		call	UNlock_Cyrix

	;set bit 6 of index 30H
		mov	cl,30H
		call	Get_Cyrix
		or	al,40H
		call	Set_Cyrix

	;disable BTB
		mov	ebx,28h
		db	0fh,26h,0cbh		;mov tr1,ebx
		db	0fh,24h,0d0h		;mov eax,tr2
;R07		and	al,NOT 0FH
		and	al,NOT 0DH		;R07
		or	ax,si
		db	0fh,26h,0d0h		;mov tr2,eax

	;flush BTB
		mov	bl,03h
		db	0fh,26h,0cbh		;mov tr1,ebx
		xor	bl,bl			;linear BTB flush
		db	0fh,26h,0cbh		;mov tr1,ebx

	;clear bit 6 of index 30H
		mov	cl,30H
		call	Get_Cyrix
		and	al,NOT 40H
		call	Set_Cyrix

		call	lock_Cyrix

Not_Cx6x86:

		ret
Set_BtB		endp
endif	;ESCD_M2					;R13A

;R05 - end

ifndef	Flash_IN_SMBASE				;R32
;R23 ifndef	Flash_2M_support			;R19A
ifndef	AUTO_DETECT_EEPROM_ID			;R23
		include	sst010.nvm		;R06;R10
		include	f001bxt.nvm		;R06
;R10		include	sst010.nvm		;R06
		include	mxic.nvm		;R06
		include	other.nvm		;R06
ifdef	AMD29F010				;R23
		include	amd29f.nvm		;R18
endif;	AMD29F010				;R23
;R19A		include	am29f002.nvm		;R19

else;	AUTO_DETECT_EEPROM_ID			;R23
;R23 else;	Flash_2M_support					;R19A
;R23 ;R19B		extrn	_2M_FLASH_ENTRY_POINT:near		;R19A
;R23 		Public	_2M_FLASH_WRITE_ENTRY_POINT		;R19A
;R23 _2M_FLASH_ERASE_ENTRY_POINT:					;R19A
;R23 		DW	offset _2M_FLASH_ENTRY_POINT		;R19A
;R23 _2M_FLASH_WRITE_ENTRY_POINT:					;R19A
;R23 		DW	?					;R19A
;R23 		Public	_2M_FLASH_ENTRY_POINT			;R19B
;R23 _2M_FLASH_ENTRY_POINT	label	near				;R19B
;R23 ;R19C		DB	120h dup (0)				;R19B
;R23 		include	2MROMLEN.INC				;R19C
;R23 		DB	_2M_EEPROM_LEN DUP (0)			;R19C
;R23 endif;	Flash_2M_support					;R19A

;R23 - start
		Public	EEPROM_Wait_10ms
EEPROM_Wait_10ms	proc	near
		push	bx
		push	cx
		xor	bx, bx
		mov	cx, 350

		PUSH	AX			; save ax.

		ALIGN	4
EEPROM_WR_OUTER_LP:
EEPROM_WR_INNER_LP:
EEPROM_WR_STATE_0:	IN	AL,61H
		TEST	AL,010H
		JZ	SHORT EEPROM_WR_STATE_0

		ALIGN	4
EEPROM_WR_STATE_1:	IN	AL,61H
		TEST	AL,010H
		JNZ	SHORT EEPROM_WR_STATE_1
		LOOP	SHORT EEPROM_WR_INNER_LP

		OR	BX,BX
		JZ	SHORT EEPROM_WR_EXIT
		DEC	BX
		JMP	SHORT EEPROM_WR_OUTER_LP

EEPROM_WR_EXIT:	POP	AX			; restore ax

		pop	cx
		pop	bx
		ret
EEPROM_Wait_10ms	endp

		Public	FLASH_WRITE_ENTRY_POINT
FLASH_ERASE_ENTRY_POINT:
		DW	offset FLASH_ENTRY_POINT
FLASH_WRITE_ENTRY_POINT:
		DW	0
		Public	FLASH_ENTRY_POINT
FLASH_ENTRY_POINT	label	near
 		include	ROMLEN.INC
ifdef	Flash_2M_support
 		DB	_2M_EEPROM_LEN DUP (0)
else;	Flash_2M_support
 		DB	_1M_EEPROM_LEN DUP (0)
endif;	Flash_2M_support
endif;	AUTO_DETECT_EEPROM_ID
;R23 - end
endif;	Flash_IN_SMBASE			;R32

endif	;FLASH_SUPPORT
;R30endif	;ESCD_SUPPORT
endif	;PNP_BIOS

;R31 start
;Function : Return status of A20
;Input    : none
;Output   : carry clear - A20 is on
;	    non-carry   - A20 in off
		public	Read_A20_Status
Read_A20_Status	proc	near
		push	es
		push	ds
		pusha
		cli
;Check A20 state by comparing data between 0:0 and 10000H:0, if the contain
;is the same than the A20 is off, otherwise A20 in on
		xor	si,si
		mov	ds,si			;DS:SI = 0000:0000
		mov	di,0ffffh
		mov	es,di
		mov	di,10h			;ES:DI = 0FFFF:0010
		mov	cx,100h			;size = 100h
		rep	cmpsb
		stc
		je	short A20_Exit		;if equate A20 is off
Yes_A20On:
		clc				;mark flag for A20 on

A20_Exit:
		popa
		pop	ds
		pop	es
		ret
Read_A20_Status	endp
;R31 end

FCODE		ENDS
		END
