;	[]===========================================================[]
;
;	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	04/16/99 BAR	Initial Revision.


;****************************************************************
;*								*
;*	SUBROUTINES TO SUPPORT MOSEL 1M TYPE FLASH ROM		*
;*								*
;****************************************************************

ifndef	Flash_IN_SMBASE	
;[]========================================================================[]
; MOSEL_V29C51001T_Flash_Erase :
;
;	Erase MOSEL_V29C51001T PARAMETER BLOCK 
;
;Saves : None
;Input : es:di 
;	 cx = length
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
MOSEL_V29C51001T_Flash_Erase	Proc	Near
 		pusha
		add	cx,511		;one page is 512 byte 
		shr	cx,9
MOSEL_V29C51001T_Erase_loop:
ifdef ESCD_M2
		mov	byte ptr gs:[05555h],0aah	;
		mov	byte ptr gs:[02aaah],055h	;MOSEL
		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			;save erase 
		mov     di, 05555h		; es:di = 05555h
		mov     al, 0AAh		; 1st byte [05555] = AAh
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 02AAAh		; es:di = 02AAAh
		mov     al, 055h		; 2nd byte [02AAA] = 55h
		mov	dx,offset Ct_Set_Flash
		call	dx
		mov     di, 05555h		; es:di = 05555h
		mov     al, 080h		; 3rd byte [05555] = 80h
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 05555h		; es:di = 05555h
		mov     al, 0AAh 		; 4th byte [05555] = AAh
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 02AAAh		; es:si = 02AAAh
		mov     al, 055h		; 5th byte [02AAA] = 55h
		mov	dx,offset Ct_Set_Flash
		call	dx
		pop	di			;pop erase page

endif ;ESCD_M2
		mov     al, 30h			; 6th byte es:[di] = 30h
		mov	dx,offset Ct_Set_Flash	
		call	dx			;erase one page
		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 
		call	MOSEL_Check_Toggle_Ready

		mov	dx,offset Ct_Get_Flash		
		call	dx
		cmp	al,0ffh					;erase OK
		jne	short MOSEL_V29C51001T_Erase_Fail	
		add	di,512					;next page
		loop	short MOSEL_V29C51001T_Erase_loop		;
		clc
		mov	al,88h
		out	0efh,al
		popa
		ret
MOSEL_V29C51001T_Erase_Fail:
		mov	al,99h
		out	0efh,al
		stc
		popa
		ret
MOSEL_V29C51001T_Flash_Erase	endp

;[]====================================================================[]
; MOSEL_V29C51001T_Flash_Write :
;
;	Program MOSEL_V29C51001T PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]

MOSEL_V29C51001T_Flash_Write	Proc	Near

		pusha
MOSEL_V29C51001T_Prg_Next_Byte:
		mov	al,ds:[si]			;Read Data
MOSEL_V29C51001T_Verify_Retry:
		call	MOSEL_V29C51001T_Prg_Sequence
		mov	dx,offset Ct_Set_Flash		
		call	dx				;Witre data to es:[di]
		mov	dx,offset Write_Delay		;wait 10us
		call	dx
		call	MOSEL_Check_Toggle_Ready

	;; Make Sure Program Correct
		mov	dx,offset Ct_Get_Flash		
		call	dx
		mov	ah,ds:[si]
		cmp	ah,al
		je	short MOSEL_V29C51001T_Verify_OK
		stc					;Set Program Fail
		jmp	short MOSEL_V29C51001T_Prog_Exit

MOSEL_V29C51001T_Verify_OK:
		inc	si
		inc	di
		loop	MOSEL_V29C51001T_Prg_Next_Byte
		clc
MOSEL_V29C51001T_Prog_Exit:
		popa
		ret
MOSEL_V29C51001T_Flash_Write	Endp

MOSEL_V29C51001T_Prg_Sequence	proc	near
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
		mov	al,0aah
		mov	dx,offset Ct_Set_Flash
		call	dx
			    
		mov	di,02aaah
		mov	al,055h
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov	di,05555h
		mov	al,0a0h
		mov	dx,offset Ct_Set_Flash
		call	dx

		pop	ax
		pop	di
endif ;ESCD_M2
		ret
MOSEL_V29C51001T_Prg_Sequence	endp

MOSEL_Check_Toggle_Ready	proc near

		push    ax
		push	bx
		mov	dx,offset Ct_Get_Flash	
		call	dx
		and     al, 40h
		mov	ah,al
MOSEL_CTR_Tog2:
		mov	bl,ah				;ifndef ESCD_M2 break ah 
		mov	dx,offset Ct_Get_Flash
		call	dx		      
		and     al, 40h
		cmp     al, bl
		je      short MOSEL_CTR_Tog3
		xchg	ah,al
		jmp	short MOSEL_CTR_Tog2
MOSEL_CTR_Tog3:
		pop	bx
		pop     ax
		ret

MOSEL_Check_Toggle_Ready	endp

else;	Flash_IN_SMBASE
;[]========================================================================[]
; MOSEL_V29C51001T_Flash_Erase :
;
;	Erase MOSEL_V29C51001T PARAMETER BLOCK (FA000h - FBFFFh)
;
;Saves : None
;Input : None
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
MOSEL_V29C51001T_Flash_Erase	Proc	Near
 		pushad
		add	cx,511		;one page is 512 byte 
		shr	cx,9
MOSEL_V29C51001T_Erase_loop:
		mov	byte ptr es:[0FFFF5555h],0aah	;
		mov	byte ptr es:[0FFFF2aaah],055h	;MOSEL
		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],030h		;
		push	cx
ifdef	More_Flash_Wait_Time				
		mov	cx,350*3
else;	More_Flash_Wait_Time 
		mov	cx,350		;350*30
endif;	More_Flash_Wait_Time 
		call	dx		;delay
		pop	cx
		call	MOSEL_Check_Toggle_Ready

		mov	al,byte ptr es:[edi]
		cmp	al,0ffh					;erase OK
		jne	short MOSEL_V29C51001T_Erase_Fail
		add	edi,512					;next page
		loop	short MOSEL_V29C51001T_Erase_loop		;
		clc
		popad
		ret
MOSEL_V29C51001T_Erase_Fail:
		stc
		popad
		ret
MOSEL_V29C51001T_Flash_Erase	endp

;[]====================================================================[]
; MOSEL_V29C51001T_Flash_Write :
;
;	Program MOSEL_V29C51001T PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]

MOSEL_V29C51001T_Flash_Write	Proc	Near

		pushad
MOSEL_V29C51001T_Prg_Next_Byte:
		mov	al,ds:[esi]			;Read Data
MOSEL_V29C51001T_Verify_Retry:
		call	MOSEL_V29C51001T_Prg_Sequence
		mov	es:[edi],al			;Read Data
		push	cx
		mov	cx,1	  ;Write_Delay		;wait 10us
		call	dx
		pop	cx
		call	MOSEL_Check_Toggle_Ready

	;; Make Sure Program Correct
		mov	al,es:[edi]
		mov	ah,ds:[esi]
		cmp	ah,al
		je	short MOSEL_V29C51001T_Verify_OK
		stc					;Set Program Fail
		jmp	short MOSEL_V29C51001T_Prog_Exit

MOSEL_V29C51001T_Verify_OK:
		inc	esi
		inc	edi
		loop	MOSEL_V29C51001T_Prg_Next_Byte
		clc
MOSEL_V29C51001T_Prog_Exit:
		popad
		ret
MOSEL_V29C51001T_Flash_Write	Endp

MOSEL_V29C51001T_Prg_Sequence	proc	near
		mov	byte ptr es:[0FFFF5555h],0aah	;
		mov	byte ptr es:[0FFFF2aaah],055h
		mov	byte ptr es:[0FFFF5555h],0a0h	;byte program
		ret
MOSEL_V29C51001T_Prg_Sequence	endp

MOSEL_Check_Toggle_Ready	proc near

		push    ax
		push	bx
		mov	al,es:[edi]
		and     al, 40h
		mov	ah,al
MOSEL_CTR_Tog2:
		mov	bl,ah				;ifndef ESCD_M2 break ah 
		mov	al,es:[edi]
		and     al, 40h
		cmp     al, bl
		je      short MOSEL_CTR_Tog3
		xchg	ah,al
		jmp	short MOSEL_CTR_Tog2
MOSEL_CTR_Tog3:
		pop	bx
		pop     ax
		ret

MOSEL_Check_Toggle_Ready	endp


endif;	Flash_IN_SMBASE		
