;	[]===========================================================[]
;
;	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
;----------------------------------------------------------------------------
;R20	06/07/96 RCH	Let more routines become option routine to save
;			more run-time space
;R19	01/26/96 RCH	Kill some unused codes to save more space
;R13A	05/08/95 KVN	if "Turbo_Pin_Handler" compile codes relocated
;R18	03/06/95 RCH	Some chipsets like VIA/496G control system speed
;			by flushing L1 cache, the system will hang when WB
;			CPU is plugged , so quaify the output pin to be out
;			or not, depends on CPU type.
;R17	12/29/94 RCH	Don't support IBM standard KBC to save more space
;R16	08/29/94 RAY	Align whole file to "PARA"
;R15	08/01/94 RCH	The KB-200 will send back a gargage byte after sending
;			Bx command. It cause keystoke abnormal if this command
;			is in INT 13H FDD access.
;R11A	03/28/94 DNL	Don't set CPU cache on/off in protected mode while 586
;			CPU installed
;R14	03/03/94 RAY	Don't include any .EXT files
;R13	03/03/94 RAY	Add BIOS.CFG option: No_Turbo_Pin_Handler
;R12	12/28/93 RCH	Don't support C&T keyboard controller any more
;R09A	11/22/93 RCH	Don't push DS
;R11	11/19/93 RCH	Use bit 4 of A20_BYTE to check if set CPU cache 
;			on/off or not when system is in protected mode
;R10	10/13/93 RCH	"Read_Isr" become public routine
;R09	09/27/93 RCH	Save & Restore DS 
;R08	09/27/93 RCH	Added Green PC clock output control
;R07	09/27/93 RCH	Added Pin 31 as turbo switch input
;R06	09/22/93 RCH	Don't trun pin 29/30 high after reading turbo switch
;			 input.
;R05	08/26/93 RAY	Check setup setting before doing Turbo Switch Input
;			scanning
;R04	08/20/93 RCH	Check IRQ 1 & IRQ 12 only, because CHECKIT v3.0 turn
;			 on IRQ 4 in service register after COM port testing
;R03	08/19/93 RCH	Don't send C0H command while system is running windows
;			 if the keyboard controller is jetkey v3.0
;R02	08/17/93 RCH	Fixed "Regional" KBC failure on turbo/normal change
;			 by hardware input from Pin 29
;R01	03/10/93 RCH	Added special speed control by toggle method support

		PAGE	62,132
		TITLE	SETSPEED ---  AT ROM-BIOS SPEED CHANGE ROUTINE
.386p
;[]-----------------------------------[]
;
;   Award Software 386/486 BIOS
;     Speed Switching routines
;   Initial Revision 16-Jul-1990
;
;[]-----------------------------------[]

.XLIST
		INCLUDE BIOS.CFG

		INCLUDE COMMON.EQU
		INCLUDE ATORGS.EQU
		INCLUDE	POST.EQU

		INCLUDE COMMON.MAC

;R14		INCLUDE ATORGS.EXT
;R14		INCLUDE AKBRD.EXT
;R14		INCLUDE CACHE.EXT					

		EXTRN	BEEP_KEY:NEAR		;R14
		EXTRN	BUFFER_8042_FULL:NEAR	;R14
		EXTRN	OPTION_BYTE:BYTE	;R14
		EXTRN	OUT_8042:NEAR		;R14
		EXTRN	OUT_8042_FULL:NEAR	;R14
		EXTRN	SETTINGS:BYTE		;R14
		EXTRN	Set_Cache:near		;R14
		EXTRN	SPD_DEF:BYTE		;R14
		EXTRN	SP_STATE:BYTE		;R14
		EXTRN	SYSTEM_BYTE:BYTE	;R14
		EXTRN	SYSTEM4_BYTE:BYTE	;R14
		EXTRN	SYSTEM5_BYTE:BYTE	;R14
		extrn	Ct_Speed:near
.LIST

;[]---------------------------[]
;
;   Low memory init (1st 64k)
;
;[]---------------------------[]


G_RAM		SEGMENT	USE16 AT 40H				      
		INCLUDE	G_RAM.INC				      
G_RAM		ENDS						      

DGROUP		GROUP	FCODE
;R16FCODE		SEGMENT	DWORD USE16 PUBLIC 'CODE'
FCODE		SEGMENT	USE16 PARA PUBLIC 'CODE'		;R16
		ASSUME	CS:FCODE

;[]-----------------------------------------------------------[]
;
; Procedure Name: XSpeed
;	
;	XSPEED is called by floppy routine to slow down fast systems
;	with slow disk controllers or dma.  
;
;
; Saves: 
;		all
; Input:
;		al = 0 - to low speed
;		   = 1 - to high speed
;
; Output:
;		None
;
; [Example:]
;
; [Note:]
;	This routine is used to slow down the system, but not alter the
;	cursor and beep.
;
;
;
; Author: Dennis Chang
; Date: July 16, 1990
;
; Name	| Date		| Description
; -----------------------------------------------------------------
; DC	| 06/18/90	| Initial version for EISA BIOS 4.0
;;[]==================================================================
;
		ALIGN	4  

		public	XSpeed
XSpeed		proc	near
		pusha
		and	al,00000001b		; mask off speaker beep flag
ifdef XSpeed_Switch_Int_Cache
		extrn	Cpu_Cache:Near
		call	Cpu_Cache
else ;XSpeed_Switch_Int_Cache
		call	sp_no_chg_cursor    	; change speed but not cusror
endif ;XSpeed_Switch_Int_Cache
					    
xsp_exit:
		popa
		ret
XSpeed		endp


;[]-------------------------------------------------------------------[]
;
; Procedure Name: SetSpeed
;	
;	SetSpeed is used to do the speed switching for EISA system.
;
;
; Saves: 
;	None
; Input:
;		al, bit 0 = 1 to switch high, 0 to switch lo
;		    bit 7 = 1 to beep speaker, 0 to stay silent.
;
; Output:
;	None
;
; [Example:]
;
; [Note:]
;
;	EISA uses timer to hold the system from time to time to
;	slow down the system.
;
;
; Author: Dennis Chang
; Date: July 16, 1990
;
; Name	| Date		| Description
; -----------------------------------------------------------------
; DC	| 07/16/90	| Initial version for EISA 4.0
;	|		|
;[]==================================================================

		ALIGN	4  

		public	SetSpeed
SetSpeed	proc	near
		
;R09A		push	ds				;R09

		test	cs:[SYSTEM_BYTE],spd_sw_disable
		jnz	sp_3  

ifdef	Turbo_Pin_Handler	;R13A
;R13A ifndef	No_Turbo_Pin_Handler	;R13

		test	byte ptr cs:SYSTEM_BYTE,80h	;turbo switch enabled?
		jz	short No_Switch_In

		push	ax
		push	ds

		mov	ax,G_RAM
		mov	ds,ax
		mov	al,ds:[Turbo_Pin_Status]

;R07 - start
		mov	ah,00010000b			;assume Pin 31
		test	byte ptr cs:SYSTEM_BYTE,10h
		jnz	short @F
;R07 - end

		mov	ah,00000100B			;assume Pin 29
		test	byte ptr cs:SYSTEM_BYTE,40h
		jz	short @F
		mov	ah,00001000B			;use Pin 30
@@:
		and	al,ah
;R04		or	al,al
;R04		jz	short check_enable
;R04shr_until_high:
;R04		cmp	al,1
;R04		je	short check_enable
;R04		shr	al,1
;R04		jmp	short shr_until_high
;R04check_enable:
;R04		test	al,01h

		pop	ds
		pop	ax

		jz	short sp_3
No_Switch_In:

;R13A endif	;Turbo_Pin_Handler	;R13
endif	;No_Turbo_Pin_Handler	;R13A

; change cursor shape

comment %
		push	ax
		push	bx
		mov	ah,3			; read cursor type
		mov	bh,0			; page to read from
		int	10h			; cursor type in cx
		pop	bx
		pop	ax
%

		push	ds
		push	ax
		mov	ax,g_ram
		mov	ds,ax
		assume	ds:g_ram
		pop	ax

		mov	ch,1			  ; block for slow speed
		and	init_err_flg,not ief_spd  ; assume low speed

		test	al,1			; low speed ?
		jz	short sp_1		; jump if so
		or	init_err_flg,ief_spd	; set hi speed
		mov	ch,cl
		dec	ch			; 2 lines for high speed

sp_1:
		pop	ds
		assume	ds:nothing
comment %
		push	ax
		mov	ah,1			; set cursor type
		int	10h
		pop	ax
%

; start programming counter 2 of timer2 


ifdef	BEEP_FOR_XSPEED						;R19
		call	Speed_Beep
endif;	BEEP_FOR_XSPEED						;R19

sp_no_chg_cursor:
		test	byte ptr cs:[SYSTEM4_BYTE],01h	;speed by change chipset reg.	
		jz	short No_Need_Prg	
		push	ax
		Call	Ct_Speed	;pass control to machine dependent
					; chipset.asm  module		  
		pop	ax
No_Need_Prg:					
		test	byte ptr cs:[SYSTEM4_BYTE],02h	;speed by change cache
		jz	short No_Need_Cache

;R11 - start
;R11A		extrn	A20_Byte:near
;R11A		test	byte ptr cs:[A20_Byte],10h	;change CPU cache allowed?
;R11A		jz	short @F
;R11A start
		push	ax
		mov	al,CMOS_AWARD_2 NMI_OFF
		out	CMOS,al
		NEWIODELAY
		in	al,CMOS+1
		NEWIODELAY
		mov	bl,al
		pop	ax
		and	bl,CPU_TYPE_MASK
		cmp	bl,TYPE_586
		je	short check_Protect
		cmp	bl,TYPE_M1			;R19
		je	short check_Protect		;R19
		cmp	bl,TYPE_AMD5K86			;R19
		je	short check_Protect		;R19

;R19		cmp	bl,TYPE_P54C
;R19		jne	short @F
check_Protect:
;R11A end
;R19		push	eax
;R19		mov	eax,cr0
;R19		test	al,1		;In protected mode
;R19		pop	eax
;R19		jnz	short No_Need_Cache

;R19 - start
		push	ax
		smsw	ax
		test	al,1		;In protected mode
		pop	ax
		jnz	short No_Need_Cache
;R19 - end

@@:
;R11 - end

		push	ax
		call	Set_Cache		; turn on/off cache
		pop	ax
No_Need_Cache:
;R18 - start
ifdef	QUALIFY_SPD_PIN
		extrn	Ct_Spd_Pin_Qualify:near
		call	Ct_Spd_Pin_Qualify
		jc	short Sp_3
endif;	QUALIFY_SPD_PIN
;R18 - end
		call	Out_8042_Pin
sp_3:

;R09A		pop	ds				;R09
 		ret
SetSpeed	endp



;[]-------------------------------------------------------------------[]
;
; Procedure Name: Speed_Beep
;	
;	Beep for speed switching
;
;
; Saves: 
;	None
;
; Input:
;		al, bit 7 = 1 to beep, 0 to quiet
;
; Output:
;		None
;	
; [Example:]
;	
;		      
; [Note:]
;
;
;
; Author: Award Software
; Date: July 16, 1990
;
; Name	| Date		| Description
; -----------------------------------------------------------------
; DC	| 07/16/90	| Initial version for EISA 4.0 (from 3.1)
;	|		|
;[]==================================================================



		public Speed_Beep
speed_beep	proc	near
ifdef	BEEP_FOR_XSPEED						;R19
		push	ax
		test	al,10000000h 		; does caller want us to beep?
		jz	short sb_1		; jump if not.

		test	cs:[spd_def],80h	; are we permitted to beep?
						; use cs for segment, as
						; call to setspeed may be
						; made by application program,
		jnz	short sb_1		; jump if not.

		push	ax			; save hi or low.
		mov	cx,0a000h
		call	beep_key
		pop	ax

		test	al,01h			; setting high or low?
		jz	short sb_1		; one beep if low

		mov	cx,2800h
		align	4
		loop	short $			; pause between beeps
		mov	cx,0a000h		; 2 beeps if high.
		call	beep_key

sb_1:
		pop	ax
		sti
endif;	BEEP_FOR_XSPEED						;R19
		ret
Speed_Beep	endp

;[]-------------------------------------------------------------------[]
;
; Procedure Name: Out_8042_Pin
;	
;	Output 8042 Pin 23 or(and) Pin 24 high or low to change speed or
;	 light LED
;
; Saves: 
;	None
;		
;
; Input:
;		al = 1 : Speed high
;		al = 0 : Speed Low
; Output:
;		None
;	
; [Example:]
;	
;		      
; [Note:]
;
;
;
; Author: Award Software
; Date: August 30, 1991
;
; Name	| Date		| Description
; -----------------------------------------------------------------
; RCH	| 30-Aug-90	| Initial version 
;	|		|
;[]==================================================================

END_OF_INT	EQU	20H			; end of interrupt code
A8259		EQU	20H			; first 8259
DATA8042	EQU	60H
STAT8042	EQU	64H
LOW_ACTIVE_HI  EQU   1
		even

KBC_TABLE:	dw	offset Award_Turbo
		dw	offset Ami_Turbo
		dw	offset Phoenix_Turbo
		dw	offset Ct_Turbo
		dw	offset Ibm_Turbo
		dw	offset Ami_Turbo

		public	Out_8042_Pin	
Out_8042_Pin	proc	near
		pusha
		push	ax
		test	CS:[SETTINGS],SPD_SW_DISABLE
		jnz	short No_Speed_Setting

		xor	bh,bh
		mov	bl,cs:SYSTEM5_BYTE		;K/B type
		shl	bx,1
		add	bx,offset KBC_TABLE
		mov	dx,cs:[bx]
;R01 - start
ifdef	SPECIAL_TOGGLE_DTK
		push	ax
		push	dx
		call	dx
		pop	dx
		pop	ax

		push	ax
		mov	ah,al			
		and	ah,1
		extrn	Get_Current_Speed:near
		call	Get_Current_Speed
		cmp	ah,al
		pop	ax
		je	short No_Speed_Setting
		
		xor	al,1
endif;	SPECIAL_TOGGLE_DTK
;R01 - end
		call	dx
		
No_Speed_Setting:
		pop	ax
		popa
		ret
Out_8042_Pin	endp


Award_Turbo	proc	near

ifdef	AWARD_KBC_SUPPORT					;R19
		mov	si,offset Award_Turbo_Tbl
		call	Turbo_Out

		test	cs:[SP_STATE],0f0H	
		jz	short No_27_30_Award

		call	Read_Input_Port
		jc	short No_27_30_Award

		push	ax

		and	ah,0FH			; save low 4 bits
		or	ah,90h			;outpue input port command
		mov	al,cs:[SP_STATE]
		shr	al,4

		mov	bh,ah
		or	bh,al			;high speed command

		mov	bl,ah
		not	al
		and	bl,al			;low speed command

		mov	dl,0f0h
		pop	ax
		call	Single_Command_Output
No_27_30_Award:
endif;	AWARD_KBC_SUPPORT					;R19
		ret
Award_Turbo	endp
Award_Turbo_Tbl:
ifdef	AWARD_KBC_SUPPORT					;R19
		db	0b2h,0bah,04h			;pin 23
		db	0b3h,0bbh,08h			;pin 24
endif;	AWARD_KBC_SUPPORT					;R19
		db	0				;end
	
Ami_Turbo_Tbl:
		db	0b4h,0bch,04h			;pin 23
		db	0b5h,0bdh,08h			;pin 24
		db	0b0h,0b8h,10h			;pin 27
		db	0b1h,0b9h,20h			;pin 28
		db	0b2h,0bah,40h			;pin 29
		db	0b3h,0bbh,80h			;pin 30
		db	0				;end

Phoenix_Turbo_Tbl:
ifdef	PHOENIX_KBC_SUPPORT					;R19
		db	0eah,0e5h,04h			;pin 23
		db	0e6h,0e9h,08h			;pin 24
endif;	PHOENIX_KBC_SUPPORT					;R19
		db	0				;end
Ami_Turbo	proc	near
		mov	si,offset Ami_Turbo_Tbl
Turbo_Out:
		mov	bx,cs:[si]
		or	bl,bl
		jz	short Ami_Over
		mov	dl,cs:[si+2]
		call	Single_Command_Output
		add	si,3			;next output pin
		jmp	Turbo_Out
Ami_Over:
		ret
Ami_Turbo	endp

Phoenix_Turbo	proc	near
ifdef	PHOENIX_KBC_SUPPORT					;R19
		mov	si,offset Phoenix_Turbo_Tbl
		jmp	Turbo_Out
endif;	PHOENIX_KBC_SUPPORT					;R19
		ret
Phoenix_Turbo	endp

Ct_Turbo	proc	near
;R12		test	cs:SYSTEM4_BYTE,PS2_MOUSE	;ps2 mouse support
;R12		jnz	short No_23_28_Ct
;R12
;R12		call	Out_Turbo_D0_D1
;R12
;R12No_23_28_Ct:
;R12		test	cs:[SP_STATE],0f0H	
;R12		jz	short No_27_30_Ct
;R12
;R12		call	Read_Input_Port
;R12		jc	short No_27_30_Ct
;R12
;R12		push	ax
;R12
;R12		mov	al,cs:[SP_STATE]
;R12		shr	al,4
;R12
;R12		mov	bh,ah
;R12		or	bh,al			;high speed command
;R12
;R12		mov	bl,ah
;R12		not	al
;R12		and	bl,al			;low speed command
;R12
;R12		mov	dl,0f0h
;R12		pop	ax
;R12		call	Out_Ct_8042
;R12No_27_30_Ct:
		ret
Ct_Turbo	endp

Ibm_Turbo	proc	near
ifdef	IBM_STD_KBC_SUPPORT	  			;R17
		call	Out_Turbo_D0_D1
endif;	IBM_STD_KBC_SUPPORT	  			;R17
		ret
Ibm_Turbo	endp

Out_Ct_8042	proc	near
;Input - BL = low speed value to output
;	 BL = high speed value to output
;R12		push	ax			; save again.
;R12		mov	al,0a1H 		; now write it back
;R12		call	Out_8042
;R12		call	Buffer_8042_Full
;R12		pop	ax
;R12		jne	short Ct_No_Out
;R12
;R12		push	ax
;R12		mov	al,02h
;R12		out	DATA8042,al
;R12		NEWIODELAY
;R12		call	Buffer_8042_Full	;
;R12		pop	ax
;R12		
;R12		push	ax
;R12		test	al,1			;Q: hi speed requested ?
;R12		jnz	short High_Spd00		; Y: do it
;R12		mov	al,bl	 		; N: set for low speed
;R12		test	cs:[spd_def], LOW_ACTIVE_HI   ;reversed?
;R12		jz	short Out_8042_Command0	;no, use bits as they are
;R12		mov	al,bh			;reverse output
;R12
;R12		jmp	Out_8042_Command0		
;R12High_Spd00:
;R12		mov	al,bh
;R12		test	cs:[spd_def], LOW_ACTIVE_HI   ;reversed?
;R12		jz	short Out_8042_Command0	;no, use bits as they are
;R12		mov	al,bl
;R12Out_8042_Command0:
;R12		out	DATA8042,al
;R12		NEWIODELAY
;R12		call	Buffer_8042_Full	;
;R12		pop	ax
;R12Ct_No_Out:
		ret
Out_Ct_8042	endp

ifdef	AWARD_KBC_SUPPORT					;R20
		public	Read_Input_Port
Read_Input_Port	proc	near
;return - ah : input port current value
		cli
		mov	ah,al
		push	ax
		mov	al,0c0H 		; read output bits
		call	Out_8042
		call	Buffer_8042_Full	; and wait for command accepted
		pop	ax
        	jne	short Read_Fail

		push	ax
		call	Out_8042_Full		; check for output buffer full
		pop	ax
        	jz	short Read_Fail

		IODELAY
		in	al,DATA8042		; now have output port bits
		NEWIODELAY

		xchg	ah,al
		clc
		ret		
Read_Fail:
		xchg	ah,al
		stc
		ret
Read_Input_Port	endp
endif;	AWARD_KBC_SUPPORT					;R20

;R08 - start
;Input : AL - 0 --> set low output
;	 AL - 1 --> set high output
;Note  : Now , we support 8042s with AMI compatible command set only.
ifdef	PM_SUPPORT
ifdef	LIGHT_GREEN_SUPPORT					;R19
		public	Out_Green_8042
Out_Green_8042	proc	near
		pusha
		mov	si,offset Ami_Turbo_Tbl
Green_Out:
		mov	bx,cs:[si]
		or	bl,bl
		jz	short Green_Over
		mov	dl,cs:[si+2]
		call	Green_Command_Output
		add	si,3			;next output pin
		jmp	Green_Out
Green_Over:
		popa
		ret
Out_Green_8042	endp

Green_Command_Output	label	near
		push	ax

;Pin 23,24,27 and 28 are not avilable in PS2 mode system , we don't send
;these command to KBC to avoid unpredictable error

		cmp	dl,20h			
		ja	short @F			;Pin 29 or Pin 30

		test	cs:SYSTEM4_BYTE,PS2_MOUSE	;ps2 mouse support
		jnz	short No_Extra_Byte0			

@@:		

		test	cs:[OPTION_BYTE],DL	;Q: Speed on this PIN ?
		jz	short No_Extra_Byte0

		jmp	short Out_Single_Command
endif;	LIGHT_GREEN_SUPPORT					;R19
endif;	PM_SUPPORT

;R08 - end

		public	Single_Command_Output
Single_Command_Output	proc	near
;Input - BL = low output command
;	 BH = high output command
;	 DL = bit to check if output or not
		push	ax

;Pin 23,24,27 and 28 are not avilable in PS2 mode system , we don't send
;these command to KBC to avoid unpredictable error

		cmp	dl,20h			
		ja	short @F			;Pin 29 or Pin 30

		test	cs:SYSTEM4_BYTE,PS2_MOUSE	;ps2 mouse support
		jnz	short No_Extra_Byte0			

@@:		

		test	cs:[SP_STATE],DL	;Q: Speed on this PIN ?
		jz	short No_Extra_Byte0
Out_Single_Command:				;R08
		test	al,1			;Q: hi speed requested ?
		jnz	short High_Spd		; Y: do it
		mov	al,bl	 		; N: set for low speed
		test	cs:[spd_def], LOW_ACTIVE_HI   ;reversed?
		jz	short Out_8042_Command	;no, use bits as they are
		mov	al,bh			;reverse output
No_Reverse_0:
		jmp	Out_8042_Command		
High_Spd:
		mov	al,bh
		test	cs:[spd_def], LOW_ACTIVE_HI   ;reversed?
		jz	short Out_8042_Command	;no, use bits as they are
		mov	al,bl
Out_8042_Command:
		cli					;R15

		out	STAT8042,al		;set/reset pin
		NEWIODELAY
		call	Buffer_8042_Full	;wait for command accepted

;R15		cmp	byte ptr cs:SYSTEM5_BYTE,AMIKBC	;AMI K/B controller?
;R15		jne	short No_Extra_Byte0
;R15
;R15		mov	ah,2
;R15		extrn	Out_8042_Full1:near
;R15		call	Out_8042_Full1		; check for byte in controller
;R15
;R15 - start
		push	cx
		xor	cx,cx
@@:
		in	al,STAT8042
		NEWIODELAY
		test	al,OBF_8042		; any data from KBC
		loopz	short @B
		pop	cx

		in	al,DATA8042		; read garbarge byte
		NEWIODELAY
;R15 - end

		in	al,DATA8042		; get garbage byte
		NEWIODELAY
No_Extra_Byte0:
		pop	ax
		ret
Single_Command_Output	endp


ifdef	IBM_STD_KBC_SUPPORT	  			;R17
Out_Turbo_D0_D1	proc	near
		mov	ah,al

		test	cs:SYSTEM4_BYTE,PS2_MOUSE	;ps2 mouse support?
		jnz	short D0_D1_Exit

		test	byte ptr CS:[SP_STATE],0ch	;pin 23 & 24 enabled?
		jz	short D0_D1_Exit

		cli
		push	ax
		mov	al,0D0H 		; read output bits
		call	Out_8042
		pop	ax
        	jne	short D0_D1_Exit

		push	ax
		call	Out_8042_Full		; check for output buffer full
		pop	ax
        	jz	short D0_D1_Exit

		IODELAY
		in	al,DATA8042		; now have output port bits
		NEWIODELAY

		and	al,0FH			; save low 4 bits
		or	al,0D0H 		; and add upper control

		test	ah,01H			; if low speed, go change.
		jz	short SlwSpeed
		mov	cl,CS:[SP_STATE]	; speed switch pin
		and	cl,0CH			; bit 2 and 3 only
		or	al,cl			; set to hi speed
		jmp	short D0_D1_1

Slwspeed:
		mov	cl,CS:[SP_STATE]	; speed switch pin
		and	cl,0CH			; strip unused bits
		mov	ch,cl
		not	ch			; turn to low speed
		and	al,ch			;
D0_D1_1:  	test	cs:[SPD_DEF],01H	; is low speed active low?
		jz	short D0_D1_2   	; then al is correct value
		xor	al,cl			; else reverse polarity of
						; bits 2 and 3.
D0_D1_2:  
		push	ax			; save again.
		mov	al,0D1H 		; now write it back
		call	Out_8042
		pop	ax
        	jne	D0_D1_Exit
		
		push	ax
		out	DATA8042,al
		NEWIODELAY
		call	Buffer_8042_Full	;
		pop	ax

D0_D1_Exit:
		mov	al,ah
		ret
Out_Turbo_D0_D1	endp
endif;	IBM_STD_KBC_SUPPORT	  			;R17

include	8042.equ

comment %
Wait_Obf	proc	near
;		xor	cx,cx			;R02

Wait_For_OBF:
		in	al,stat8042
		newiodelay
		test	al,OBF_8042
		jnz	short @F		;R02
;		loop	short Wait_For_OBF	;R02
		jmp	Wait_For_OBF
@@:
		ret
Wait_Obf	endp
%

ifdef	Turbo_Pin_Handler	;R20

Out_8042_Cmd	proc	near
		out	STAT8042,al
		jmp	Buffer_8042_Full
Out_8042_Cmd	endp


		public	Out_Cmd
Out_Cmd		proc	near
		out	64h,al

B529:
		xor	cx,cx
B529_1:
		jcxz	short @F
@@:
		in	al,64h
		and	al,3
		cmp	al,3
		je	short @F
		test	al,2
		loopnz	short B529_1			
       		ret
@@:
		xor	cx,cx
		ret
Out_Cmd		endp
	 

		public	Wait_Obf
Wait_Obf	proc	near	
B53b:
		xor	cx,cx
B53b_1:
		jcxz	short @F
@@:
		in	al,64h
		test	al,1

		loopz	short B53b_1
		jz	short @F
		in	al,60h
		NEWIODELAY
@@:
		ret
Wait_Obf	endp

		public	Read_Isr		;R10
Read_Isr	proc	near
		mov	al,0bh			;set ISR to read
		out	dx,al
		NEWIODELAY
		in	al,dx				
		iodelay					
		ret
Read_Isr	endp
endif;	Turbo_Pin_Handler	;R20

		public	Turbo_Pin_Hdlr
Turbo_Pin_Hdlr	proc	near

ifdef	Turbo_Pin_Handler	;R13A
;R13A ifndef	No_Turbo_Pin_Handler	;R13

		sti
		test	byte ptr cs:SYSTEM_BYTE,80h	;turbo switch enabled?
		jz	No_Turbo_Switch

		pusha
		push	es
		push	ds
		mov	ax,G_RAM
		mov	ds,ax

		cli

		in	al,21h
		push	ax			;save IRQ 1 status
		or	al,2			;disable IRQ 1
		out	21h,al
		IODELAY


		inc	byte ptr ds:[Turbo_Pin_Interval]
		cmp	byte ptr ds:[Turbo_Pin_Interval],18
		jne	No_Turbo_Pin_Hdlr

		test	word ptr ds:[INIT_ERR_FLG],IEF_IN_POST
		jnz	No_Change_Speed

;R05 - Start
		test	ds:[FDD_VERIFY_CMD_FLAG],01h
		jz	No_Change_Speed
;R05 - End

		;Check if any other IRQ in service
		mov	dx,20h		
		call	Read_Isr

		test	al,02h			;IRQ 1?	;R04
		jnz	No_Change_Speed		

     
		mov	dx,0a0h
		call	Read_Isr
		test	al,10h			;IRQ 12?;R04
		jnz	No_Change_Speed		;R04
		
;The IBF & OBF of 8042 should be empty
		xor	cx,cx
@@:
		in	al,stat8042
		newiodelay

		test	al,08h				;In windows ?
		jz	no_change_speed
		test	al,IBF_8042+OBF_8042
		jz	short @F
		loop	short @B
		jmp	no_change_speed
@@:

;Before sending C0 command , we send a dummy command to 8042 that
;will disable keyboard interface temporarily to prevent lost key if user
;press keyboard at this moment
		mov	al,0d5h			;dummy command
		call	Out_Cmd
		jcxz	no_change_speed

		mov	al,0C0h
		call	Out_Cmd
		jcxz	no_change_speed
	
		call	Wait_Obf
		jz	no_change_speed

		cmp	al,0e0h
		jne	short @F

		call	Wait_Obf
		jz	no_change_speed

		call	Wait_Obf
		jz	no_change_speed
	
		call	Wait_Obf
		jmp	No_Change_Speed
@@:

		test	byte ptr cs:SYSTEM_BYTE,20h	;low active
		jz	short @F     	  		;no, high active

		xor	al,0FFh
@@:

;R07 - start
		mov	ah,00010000b			;assume Pin 31
		test	byte ptr cs:SYSTEM_BYTE,10h
		jnz	short @F
;R07 - end

		mov	ah,00000100B			;assume Pin 29
		test	byte ptr cs:SYSTEM_BYTE,40h
		jz	short @F
		mov	ah,00001000B			;use Pin 30
@@:
		and	al,ah

		cmp	ds:[Turbo_Pin_Status],al
		je	short No_Change_Speed

		push	ax
;R04		cmp	al,0
		or	al,al				;R04
		je	short Set_Turbo
		mov	al,1				;R04
;R04 shr_until_1:
;R04 		cmp	al,1
;R04 		je	short Set_Turbo
;R04 		shr	al,1
;R04 		jmp	short shr_until_1
Set_Turbo:
		mov	byte ptr ds:[Turbo_Pin_Status],ah

		call	Setspeed

;R02 - start

;R06		test	al,1
;R06		jnz	short @F
;R06	
;R06		cmp	cs:SYSTEM5_BYTE,AMIKBC		;AMI K/B type?
;R06		jne	short @F
;R06		
;R06		mov	bx,0bab2h
;R06		test	byte ptr cs:SYSTEM_BYTE,40h	;Pin 29?
;R06		jz	short Yes_Pin29
;R06		mov	bx,0bbb3h
;R06Yes_Pin29:
;R06		mov	dl,0ffh
;R06		mov	al,1
;R06		call	Single_Command_Output
;R06@@:

;R02 - end

		pop	ax

		mov	ds:[Turbo_Pin_Status],al
No_Change_Speed:


		mov	byte ptr ds:[Turbo_Pin_Interval],0

No_Turbo_Pin_Hdlr:
		pop	ax
		out	21h,al
		IODELAY

		pop	ds
		pop	es
		popa
No_Turbo_Switch:

;R13A endif	;No_Turbo_Pin_Handler	;R13
endif	;Turbo_Pin_Handler	;R13A

		ret

Turbo_Pin_Hdlr	endp

IF BUS_TYPE EQ EISA_BUS

;[]=====================================================================
;
; Procedure Name: Slow_EISA_FD
;	
;	Slow down system speed for floppy disk accessing.
;
;
; Saves: 
;		all
; Input:
;		none
;
; Output:
;		none
;
; [Example:]
;		      
; [Note:]
;	This routine is here because of the following reasons:
;	1. AT&T UNIX can not boot from 1.44M floppy on Intel EISA 486 
;	   (and ECS EISA).
;	2. If we simply use NFS to slow down the system, floppy might not
;	   work on the VERY SLOW speed.
;
;
;
; Author: Dennis Chang
; Date: OCT 03, 1990
;
; Name	| Date		| Description
; -----------------------------------------------------------------
; DC	| 10/03/90	| Initial version for EISA 4.0
;	|		|
;[]==================================================================

		ALIGN	4  
		public	Slow_EISA_FD
Slow_EISA_FD 	proc	near
comment %
		push	ax
		push	si
		mov	al,92h
		out	4bh,al
		newiodelay
		mov	si,offset cs:spd_tbl
		mov	al,byte ptr cs:[si+2]		;get the 2nd speed
		out	4ah,al
		newiodelay
		in	al,4ah

		pop	si
		pop	ax
%
		ret
Slow_EISA_FD	endp
ENDIF

FCODE		ENDS

		END
