;	[]===========================================================[]
;
;	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
;----------------------------------------------------------------------------
;R00	03/29/99 BAR	Initial Revision.


;****************************************************************
;*								*
;*	SUBROUTINES TO SUPPORT SST 1M TYPE FLASH ROM		*
;*								*
;****************************************************************

;[]========================================================================[]
; SST39SF010_Flash_Erase :
;
;	Erase SSTSF020 PARAMETER BLOCK (FA000h - FBFFFh)
;
;Saves : None
;Input : None
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
SST_39SF010_Flash_Erase	Proc	Near
ifndef	Flash_IN_SMBASE	
		pusha
		add	cx,4094		;one page is 4096 byte 
		shr	cx,12		;mininum page erase is 4K
SST_39SF010_Erase_loop:
ifdef ESCD_M2
		mov	byte ptr gs:[05555h],0aah	;
		mov	byte ptr gs:[02aaah],055h	;SST
		mov	byte ptr gs:[05555h],080h	;page erase
		mov	byte ptr gs:[05555h],0aah	;command
		mov	byte ptr gs:[02aaah],055h	;
else ;ESCD_M2
		push	di
		mov     di, 05555h		; es:di = 05555h	
		mov     al, 0AAh		; 1st byte [0555] = AAh
		mov	dx,offset Ct_Set_Flash 
		call	dx

		mov     di, 02AAAh		; es:di = 02AAAh
		mov     al, 055h		; 2nd byte [0AAA] = 55h
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 05555h		; es:di = 05555h	
		mov     al, 80h			; 3rd byte [0555] = 80h
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 05555h		; es:di = 05555h	
		mov     al, 0AAh 		; 4th byte [0555] = AAh
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 02AAAh		; es:di = 02AAAh
		mov     al, 055h		; 5th byte [0AAA] = 55h
		mov	dx,offset Ct_Set_Flash
		call	dx

		pop	di
endif ;ESCD_M2

		mov     al, 30h			; 6th byte [0555] = 30h
		mov	dx,offset Ct_Set_Flash
		call	dx
		mov	dx,offset EEPROM_Wait_10ms
		call	dx
ifdef	More_Flash_Wait_Time
		mov	dx,offset EEPROM_Wait_10ms
		call	dx
		mov	dx,offset EEPROM_Wait_10ms
		call	dx
endif;	More_Flash_Wait_Time


		mov	dx,offset Ct_Get_Flash
		call	dx
		mov	bh,al
SSTSF010_Erase_Verify:
		mov	dx,offset Ct_Get_Flash
		call	dx
		mov	bl,bh
		xor	bh,al
		test	bh,40h
		mov	bh,al
		jz	short SSTSF010_Erase_OK

		test	bl,20h
		jz	short SSTSF010_Erase_Verify

		mov	dx,offset Ct_Get_Flash
		call	dx
		xor	bl,al
		test	bl,40h
		jz	short SSTSF010_Erase_OK

SSTSF010_Erase_Fail:
		stc
		popa
		ret

SSTSF010_Erase_OK:
		cmp	al,0ffh
		jne	short SSTSF010_Erase_Fail
		add	di,4096					;next page
		loop	short SST_39SF010_Erase_loop		;
		clc
		popa
		ret
else;	Flash_IN_SMBASE
		pushad
		add	cx,4095		;one page is 4096 byte 
		shr	cx,12		;mininum page erase is 4K
SST_39SF010_Erase_loop:
		mov	byte ptr es:[0FFFF5555h], 0aah	;
		mov	byte ptr es:[0FFFF2aaah], 055h	;SST
		mov	byte ptr es:[0FFFF5555h], 080h	;page erase
		mov	byte ptr es:[0FFFF5555h], 0aah	;command
		mov	byte ptr es:[0FFFF2aaah], 055h	;
		mov	byte ptr es:[edi], 30h
		push	cx
ifndef	More_Flash_Wait_Time
		mov	cx,350		;delay 10ms
else;	More_Flash_Wait_Time
		mov	cx,350*3	;delay 30ms
endif;	More_Flash_Wait_Time
		call	dx		
		pop	cx

		mov	al,byte ptr es:[edi]
		mov	bh,al
SSTSF010_Erase_Verify:
		mov	al,byte ptr es:[edi]
		mov	bl,bh
		xor	bh,al
		test	bh,40h
		mov	bh,al
		jz	short SSTSF010_Erase_OK

		test	bl,20h
		jz	short SSTSF010_Erase_Verify

		mov	al,byte ptr es:[edi]
		xor	bl,al
		test	bl,40h
		jz	short SSTSF010_Erase_OK

SSTSF010_Erase_Fail:
		stc
		popad
		ret

SSTSF010_Erase_OK:
		cmp	al,0ffh
		jne	short SSTSF010_Erase_Fail
		add	di,4096					;next page
		loop	short SST_39SF010_Erase_loop		;
		clc
		popad
		ret

endif;	Flash_IN_SMBASE	
SST_39SF010_Flash_Erase	endp

;[]====================================================================[]
; SSTSF010_Flash_Write :
;
;	Program SSTSF020 PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]
SST_39SF010_Flash_Write	Proc	Near
ifndef	Flash_IN_SMBASE	
		pusha
SSTSF010_Prg_Next_Byte:
		mov	bx,25				;Program Retry Count
		mov	al,ds:[si]			;Read Data

SSTSF010_Verify_Retry:
		call	SSTSF010_Prg_Sequence
		mov	dx,offset Ct_Set_Flash
		call	dx
		call	SSTSF010_Check_Toggle_Ready
		jc	short SSTSF010_Toggle_error

	;; Make Sure Program Correct
		mov	dx,offset Ct_Get_Flash
		call	dx
		cmp	al,ds:[si]
		je	short SSTSF010_Verify_OK
SSTSF010_Toggle_error:
		dec	bx
		jnz	short SSTSF010_Verify_Retry

		stc					;Set Program Fail
		jmp	short SSTSF010_Prog_Exit

SSTSF010_Verify_OK:
		inc	si
		inc	di
		loop	SSTSF010_Prg_Next_Byte
		clc

SSTSF010_Prog_Exit:

		popa
		ret
else;	Flash_IN_SMBASE	
		pushad
SSTSF010_Prg_Next_Byte:
		mov	bx,25				;Program Retry Count
		mov	al,ds:[esi]			;Read Data

SSTSF010_Verify_Retry:
		call	SSTSF010_Prg_Sequence
		mov	byte ptr es:[edi] , al
		call	SSTSF010_Check_Toggle_Ready
		jc	short SSTSF010_Toggle_error

	;; Make Sure Program Correct
		mov	al, byte ptr es:[edi]
		cmp	al, byte ptr ds:[esi]
		je	short SSTSF010_Verify_OK
SSTSF010_Toggle_error:
		dec	bx
		jnz	short SSTSF010_Verify_Retry

		stc					;Set Program Fail
		jmp	short SSTSF010_Prog_Exit

SSTSF010_Verify_OK:
		inc	esi
		inc	edi
		loop	SSTSF010_Prg_Next_Byte
		clc

SSTSF010_Prog_Exit:

		popad
		ret

endif;	Flash_IN_SMBASE	
SST_39SF010_Flash_Write	Endp

SSTSF010_Prg_Sequence	proc	near
ifndef	Flash_IN_SMBASE	
ifdef ESCD_M2
		mov	byte ptr gs:[5555h],0aah	;
		mov	byte ptr gs:[2aaah],055h
		mov	byte ptr gs:[5555h],0a0h	;page erase
else ;ESCD_M2
		push	di
		push	ax

		mov     di, 05555h		; es:di = 05555h	
		mov	al,0aah
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 02AAAh		 ;ds:si = 02AAAh
		mov	al,55h
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 05555h		; es:di = 05555h	
		mov	al,0a0h
		mov	dx,offset Ct_Set_Flash
		call	dx

		pop	ax
		pop	di
endif; ESCD_M2
else;	Flash_IN_SMBASE
		mov	byte ptr es:[0FFFF5555h],0aah	;
		mov	byte ptr es:[0FFFF2aaah],055h
		mov	byte ptr es:[0FFFF5555h],0a0h	;page erase
endif;	Flash_IN_SMBASE
		ret
SSTSF010_Prg_Sequence	endp

SSTSF010_Check_Toggle_Ready	proc	near
		clc
ifndef	Flash_IN_SMBASE
		push	ax
		push	bx
SSTSF010_CTR_TOG1:
		mov	dx,offset Ct_Get_Flash
		call	dx
		mov	ah,ds:[si]			;Read Source Bit once
		and	ax,8080h
		cmp	al,ah
		je	short SSTSF010_CTR_TOG3

SSTSF010_CTR_TOG2:
		mov	bl,ah
		mov	dx,offset Ct_Get_Flash
		call	dx
		and	al,0a0h
		test	al,20h
		jz	short SSTSF010_CTR_TOG1
		and	al,80h
		cmp	al,bl
		je	short SSTSF010_CTR_TOG3
		stc
SSTSF010_CTR_TOG3:
		pop	bx
		pop	ax
else;	Flash_IN_SMBASE	
		push	ax
		push	bx
SSTSF010_CTR_TOG1:
		mov	al , byte ptr es:[edi]
		mov	ah, ds:[esi]			;Read Source Bit once
		and	ax,8080h
		cmp	al,ah
		je	short SSTSF010_CTR_TOG3

SSTSF010_CTR_TOG2:
		mov	bl,ah
		mov	al , byte ptr es:[edi]
		and	al,0a0h
		test	al,20h
		jz	short SSTSF010_CTR_TOG1
		and	al,80h
		cmp	al,bl
		je	short SSTSF010_CTR_TOG3
		stc
SSTSF010_CTR_TOG3:
		pop	bx
		pop	ax

endif;	Flash_IN_SMBASE
		ret
SSTSF010_Check_Toggle_Ready	endp

