Index: trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/AHDh_HReset.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/AHDh_HReset.asm	(revision 32)
+++ trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/AHDh_HReset.asm	(revision 33)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	9.12.2007
-; Last update	:	1.8.2010
+; Last update	:	23.8.2010
 ; Author		:	Tomi Tilli
 ; Description	:	Int 13h function AH=Dh, Reset Hard Disk (Alternate reset).
@@ -50,4 +50,5 @@
 
 	call	FindDPT_ForDriveNumber		; DS:DI now points to DPT
+	call	Interrupts_UnmaskInterruptControllerForDriveInDSDI
 	call	AHDh_ResetMasterAndSlave
 	;jc		SHORT .ReturnError			; CF would be set if slave drive present without master
Index: trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HError.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HError.asm	(revision 32)
+++ trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HError.asm	(revision 33)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	30.11.2007
-; Last update	:	1.8.2010
+; Last update	:	23.8.2010
 ; Author		:	Tomi Tilli
 ; Description	:	Error checking functions for BIOS Hard disk functions.
@@ -13,5 +13,4 @@
 ;	Parameters:
 ;		DS:		RAMVARS segment
-;		ES:		BDA segment (zero)
 ;		CF:		Set if timeout
 ;				Cleared if task flag was properly set
@@ -25,10 +24,6 @@
 ALIGN JUMP_ALIGN
 HError_ProcessErrorsAfterPollingTaskFlag:
-	jc		SHORT HError_ProcessTimeoutAfterPollingBSYandSomeOtherStatusBit
-	mov		ax, [es:HDBDA.wHDStAndErr]
-	call	GetBiosErrorCodeToAHfromStatusAndErrorRegistersInAX
-	mov		[es:BDA.bHDLastSt], ah
-	mov		BYTE [es:BDA.bHDTaskFlg], 0
-	ret
+	jnc		SHORT HError_ProcessErrorsAfterPollingBSY
+	; Fall to HError_ProcessTimeoutAfterPollingBSYandSomeOtherStatusBit
 
 ;--------------------------------------------------------------------
Index: trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HIRQ.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HIRQ.asm	(revision 32)
+++ trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HIRQ.asm	(revision 33)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	11.12.2009
-; Last update	:	1.8.2010
+; Last update	:	23.8.2010
 ; Author		:	Tomi Tilli
 ; Description	:	Interrupt handling related functions.
@@ -36,7 +36,7 @@
 ALIGN JUMP_ALIGN
 .TaskFlagPollingComplete:
-	call	HError_ProcessErrorsAfterPollingTaskFlag
 	pop		es
-	ret
+	jmp		HError_ProcessErrorsAfterPollingTaskFlag
+
 
 ;--------------------------------------------------------------------
@@ -187,6 +187,7 @@
 
 	; Reading Status Register acknowledges IDE interrupt
-	call	RamVars_GetSegmentToDS
-	call	HError_GetStatusAndErrorRegistersToAXandStoreThemToBDA
+	;call	RamVars_GetSegmentToDS
+	;call	HError_GetStatusAndErrorRegistersToAXandStoreThemToBDA
+	LOAD_BDA_SEGMENT_TO	ds, ax
 	mov		BYTE [BDA.bHDTaskFlg], 0FFh		; Set task flag
 
Index: trunk/XTIDE_Universal_BIOS/Src/Initialization/DetectDrives.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Initialization/DetectDrives.asm	(revision 32)
+++ trunk/XTIDE_Universal_BIOS/Src/Initialization/DetectDrives.asm	(revision 33)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	17.3.2010
-; Last update	:	2.5.2010
+; Last update	:	23.8.2010
 ; Author		:	Tomi Tilli
 ; Description	:	Functions for detecting drive for the BIOS.
@@ -24,5 +24,5 @@
 ALIGN JUMP_ALIGN
 DetectDrives_FromAllIDEControllers:
-	call	Initialize_GetIdeControllerCountToCX
+	call	RamVars_GetIdeControllerCountToCX
 	mov		bp, ROMVARS.ideVars0			; CS:BP now points to first IDEVARS
 ALIGN JUMP_ALIGN
Index: trunk/XTIDE_Universal_BIOS/Src/Initialization/Initialize.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Initialization/Initialize.asm	(revision 32)
+++ trunk/XTIDE_Universal_BIOS/Src/Initialization/Initialize.asm	(revision 33)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	23.3.2010
-; Last update	:	1.8.2010
+; Last update	:	23.8.2010
 ; Author		:	Tomi Tilli
 ; Description	:	Functions for initializing the BIOS.
@@ -82,25 +82,5 @@
 	mov		bl, INTV_BOOTSTRAP
 	mov		si, Int19h_LateInitialization
-	; Fall to Initialize_InstallInterruptHandler
-
-;--------------------------------------------------------------------
-; Installs any interrupt handler.
-;
-; Initialize_InstallInterruptHandler
-;	Parameters:
-;		BX:		Interrupt vector number (for example 13h)
-;		ES:		BDA and Interrupt Vector segment (zero)
-;		CS:SI:	Ptr to interrupt handler
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		BX
-;--------------------------------------------------------------------	
-ALIGN JUMP_ALIGN
-Initialize_InstallInterruptHandler:
-	eSHL_IM	bx, 2					; Shift for DWORD offset
-	mov		[es:bx], si				; Store offset
-	mov		[es:bx+2], cs			; Store segment
-	ret
+	jmp		Interrupts_InstallHandlerToVectorInBXFromCSSI
 
 
@@ -122,286 +102,8 @@
 	call	RamVars_GetSegmentToDS
 	LOAD_BDA_SEGMENT_TO	es, ax
-	call	Initialize_InterruptVectors
+	call	Interrupts_InitializeInterruptVectors
 	call	DetectDrives_FromAllIDEControllers
 	call	CompatibleDPT_CreateForDrives80hAnd81h
-	jmp		Initialize_ResetDetectedDrives
-
-
-;--------------------------------------------------------------------
-; Initializes Interrupt Vectors.
-;
-; Initialize_InterruptVectors
-;	Parameters:
-;		DS:		RAMVARS segment
-;		ES:		BDA and Interrupt Vector segment (zero)
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		All except segments
-;--------------------------------------------------------------------	
-ALIGN JUMP_ALIGN
-Initialize_InterruptVectors:
-	call	Initialize_Int13hAnd40h
-	call	Initialize_Int19h
-	jmp		SHORT Initialize_HardwareIrqHandlers
-
-
-;--------------------------------------------------------------------
-; Initializes INT 13h and 40h handlers for Hard Disk and
-; Floppy Drive BIOS functions.
-;
-; Initialize_Int13hAnd40h
-;	Parameters:
-;		DS:		RAMVARS segment
-;		ES:		BDA and Interrupt Vector segment (zero)
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		AX, BX, CX, DX, SI, DI
-;--------------------------------------------------------------------	
-ALIGN JUMP_ALIGN
-Initialize_Int13hAnd40h:
-	mov		ax, [es:INTV_DISK_FUNC*4]			; Load old INT 13h offset
-	mov		dx, [es:INTV_DISK_FUNC*4+2]			; Load old INT 13h segment
-	mov		[RAMVARS.fpOldI13h], ax				; Store old INT 13h offset
-	mov		[RAMVARS.fpOldI13h+2], dx			; Store old INT 13h segment
-	mov		bx, INTV_DISK_FUNC					; INT 13h interrupt vector offset
-	mov		si, Int13h_DiskFunctions			; Interrupt handler offset
-	call	Initialize_InstallInterruptHandler
-
-	; Only store INT 13h handler to 40h if 40h is not already installed.
-	; At least AMI BIOS for 286 stores 40h handler by itself and calls
-	; 40h from 13h. That system locks to infinite loop if we copy 13h to 40h.
-	call	FloppyDrive_IsInt40hInstalled
-	jnc		SHORT Initialize_Int40h
-	ret
-
-;--------------------------------------------------------------------
-; Initializes INT 40h handler for Floppy Drive BIOS functions.
-;
-; Initialize_Int40h
-;	Parameters:
-;		DX:AX:	Ptr to old INT 13h handler
-;		ES:		BDA and Interrupt Vector segment (zero)
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		Nothing
-;--------------------------------------------------------------------	
-ALIGN JUMP_ALIGN
-Initialize_Int40h:
-	mov		[es:INTV_FLOPPY_FUNC*4], ax		; Store offset
-	mov		[es:INTV_FLOPPY_FUNC*4+2], dx	; Store segment
-	ret
-
-
-;--------------------------------------------------------------------
-; Initializes INT 19h handler for boot loader.
-;
-; Initialize_Int19h
-;	Parameters:
-;		DS:		RAMVARS segment
-;		ES:		BDA and Interrupt Vector segment (zero)
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		BX, SI
-;--------------------------------------------------------------------	
-ALIGN JUMP_ALIGN
-Initialize_Int19h:
-	eMOVZX	bx, [cs:ROMVARS.bBootLdrType]	; Load boot loader type
-	mov		si, INTV_BOOTSTRAP				; 19h
-	xchg	bx, si							; SI=Loader type, BX=19h
-	jmp		[cs:si+.rgwSetupBootLoader]		; Jump to install selected loader
-ALIGN WORD_ALIGN
-.rgwSetupBootLoader:
-	dw		.SetupBootMenuLoader		; BOOTLOADER_TYPE_MENU
-	dw		.SetupSimpleLoader			; BOOTLOADER_TYPE_SIMPLE
-	dw		.SetupBootMenuLoader		; reserved
-	dw		.NoBootLoader				; BOOTLOADER_TYPE_NONE
-
-ALIGN JUMP_ALIGN
-.NoBootLoader:
-	test	BYTE [cs:ROMVARS.wFlags], FLG_ROMVARS_LATE
-	jnz		SHORT .SetupSimpleLoader	; Boot loader required for late initialization
-	ret
-ALIGN JUMP_ALIGN
-.SetupSimpleLoader:
-	mov		si, Int19h_SimpleBootLoader
-	jmp		Initialize_InstallInterruptHandler
-ALIGN JUMP_ALIGN
-.SetupBootMenuLoader:
-	mov		si, Int19hMenu_BootLoader
-	jmp		Initialize_InstallInterruptHandler
-
-
-;--------------------------------------------------------------------
-; Initializes hardware IRQ handlers.
-;
-; Initialize_HardwareIrqHandlers
-;	Parameters:
-;		ES:		BDA and Interrupt Vector segment (zero)
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		AX, BX, CX, DX, SI, DI
-;--------------------------------------------------------------------		
-ALIGN JUMP_ALIGN
-Initialize_HardwareIrqHandlers:
-	mov		di, ROMVARS.ideVars0			; CS:SI points to first IDEVARS
-	call	Initialize_GetIdeControllerCountToCX
-ALIGN JUMP_ALIGN
-.IdeControllerLoop:
-	mov		al, [cs:di+IDEVARS.bIRQ]		; Load IRQ number
-	add		di, BYTE IDEVARS_size			; Increment to next controller
-	call	Initialize_LowOrHighIrqHandler
-	loop	.IdeControllerLoop
-	ret
-
-;--------------------------------------------------------------------
-; Returns number of IDE controllers handled by our BIOS.
-;
-; Initialize_GetIdeControllerCountToCX
-;	Parameters:
-;		Nothing
-;	Returns:
-;		CX:		Number of IDE controllers to handle
-;	Corrupts registers:
-;		Nothing
-;--------------------------------------------------------------------	
-ALIGN JUMP_ALIGN
-Initialize_GetIdeControllerCountToCX:
-	mov		cx, 1					; Assume lite mode (one controller)
-	test	BYTE [cs:ROMVARS.wFlags], FLG_ROMVARS_FULLMODE
-	jz		SHORT .Return
-	mov		cl, [cs:ROMVARS.bIdeCnt]
-ALIGN JUMP_ALIGN
-.Return:
-	ret
-
-;--------------------------------------------------------------------
-; Initializes hardware IRQ handler for specific IRQ.
-;
-; Initialize_LowOrHighIrqHandler
-;	Parameters:
-;		AL:		IRQ number, 0 if IRQ disabled
-;		ES:		BDA and Interrupt Vector segment (zero)
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		AX, BX, DX, SI
-;--------------------------------------------------------------------		
-ALIGN JUMP_ALIGN
-Initialize_LowOrHighIrqHandler:
-	test	al, al							; IRQ disabled?
-	jz		SHORT .Return					;  If so, return
-	eMOVZX	bx, al							; Copy IRQ number to BX
-	cmp		al, 8							; High IRQ? (AT systems only)
-	jae		SHORT Initialize_HighIrqHandler
-	jmp		SHORT Initialize_LowIrqHandler
-ALIGN JUMP_ALIGN
-.Return:
-	ret
-
-
-;--------------------------------------------------------------------
-; Initializes handler for high IRQ (8...15).
-;
-; Initialize_HighIrqHandler
-;	Parameters:
-;		AL,BX:	IRQ number (8...15)
-;		ES:		BDA and Interrupt Vector segment (zero)
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		AX, BX, DX, SI
-;--------------------------------------------------------------------		
-ALIGN JUMP_ALIGN
-Initialize_HighIrqHandler:
-	add		bx, BYTE INTV_IRQ8 - 8			; Interrupt vector number
-	mov		si, HIRQ_InterruptServiceRoutineForIrqs8to15
-	call	Initialize_InstallInterruptHandler
-	; Fall to Initialize_UnmaskHighIrqController
-
-;--------------------------------------------------------------------
-; Unmasks interrupt from Slave 8259 interrupt controller (IRQs 8...15)
-;
-; Initialize_HighIrqHandler
-;	Parameters:
-;		AL:		IRQ number (8...15)
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		AX, DX
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-Initialize_UnmaskHighIrqController:
-	sub		al, 8				; Slave interrupt number
-	mov		dx, PORT_8259SL_IMR	; Load Slave Mask Register address
-	call	Initialize_ClearBitFrom8259MaskRegister
-	mov		al, 2				; Master IRQ 2 to allow slave IRQs
-	jmp		SHORT Initialize_UnmaskLowIrqController
-
-
-;--------------------------------------------------------------------
-; Initializes handler for low IRQ (0...7).
-;
-; Initialize_LowIrqHandler
-;	Parameters:
-;		AL,BX:	IRQ number (0...7)
-;		ES:		BDA and Interrupt Vector segment (zero)
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		AX, BX, DX, SI
-;--------------------------------------------------------------------		
-ALIGN JUMP_ALIGN
-Initialize_LowIrqHandler:
-	add		bx, BYTE INTV_IRQ0				; Interrupt vector number
-	mov		si, HIRQ_InterruptServiceRoutineForIrqs2to7
-	call	Initialize_InstallInterruptHandler
-	; Fall to Initialize_UnmaskLowIrqController
-
-;--------------------------------------------------------------------
-; Unmasks interrupt from Master 8259 interrupt controller (IRQs 0...7)
-;
-; Initialize_UnmaskLowIrqController
-;	Parameters:
-;		AL:		IRQ number (0...7)
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		AX, DX
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-Initialize_UnmaskLowIrqController:
-	mov		dx, PORT_8259MA_IMR	; Load Mask Register address
-	; Fall to Initialize_ClearBitFrom8259MaskRegister
-
-;--------------------------------------------------------------------
-; Unmasks interrupt from Master or Slave 8259 Interrupt Controller.
-;
-; Initialize_ClearBitFrom8259MaskRegister
-;	Parameters:
-;		AL:		8259 interrupt index (0...7)
-;		DX:		Port address to Interrupt Mask Register
-;	Returns:
-;		Nothing
-;	Corrupts registers:
-;		AX
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-Initialize_ClearBitFrom8259MaskRegister:
-	push	cx
-	xchg	ax, cx				; IRQ index to CL
-	mov		ch, 1				; Load 1 to be shifted
-	shl		ch, cl				; Shift bit to correct position
-	not		ch					; Invert to create bit mask for clearing
-	in		al, dx				; Read Interrupt Mask Register
-	and		al, ch				; Clear wanted bit
-	out		dx, al				; Write modified Interrupt Mask Register
-	pop		cx
-	ret
-
+	; Fall to .ResetDetectedDrives
 
 ;--------------------------------------------------------------------
@@ -416,5 +118,5 @@
 ;		AX, BX, CX, DX, DI
 ;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-Initialize_ResetDetectedDrives:
+;ALIGN JUMP_ALIGN
+.ResetDetectedDrives:
 	jmp		AH0h_ResetHardDisksHandledByOurBIOS
Index: trunk/XTIDE_Universal_BIOS/Src/Initialization/Interrupts.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Initialization/Interrupts.asm	(revision 33)
+++ trunk/XTIDE_Universal_BIOS/Src/Initialization/Interrupts.asm	(revision 33)
@@ -0,0 +1,272 @@
+; File name		:	Interrupts.asm
+; Project name	:	IDE BIOS
+; Created date	:	23.8.2010
+; Last update	:	23.8.2010
+; Author		:	Tomi Tilli
+; Description	:	Functions for initializing the BIOS.
+
+; Section containing code
+SECTION .text
+
+;--------------------------------------------------------------------
+; Interrupts_InitializeInterruptVectors
+;	Parameters:
+;		DS:		RAMVARS segment
+;		ES:		BDA and Interrupt Vector segment (zero)
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		All except segments
+;--------------------------------------------------------------------	
+ALIGN JUMP_ALIGN
+Interrupts_InitializeInterruptVectors:
+	call	Interrupts_InitializeInt13hAnd40h
+	call	Interrupts_InitializeInt19h
+	jmp		SHORT Interrupts_InitializeHardwareIrqHandlers
+
+
+;--------------------------------------------------------------------
+; Interrupts_Int13hAnd40h
+;	Parameters:
+;		DS:		RAMVARS segment
+;		ES:		BDA and Interrupt Vector segment (zero)
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		AX, BX, CX, DX, SI, DI
+;--------------------------------------------------------------------	
+ALIGN JUMP_ALIGN
+Interrupts_InitializeInt13hAnd40h:
+	mov		ax, [es:INTV_DISK_FUNC*4]			; Load old INT 13h offset
+	mov		dx, [es:INTV_DISK_FUNC*4+2]			; Load old INT 13h segment
+	mov		[RAMVARS.fpOldI13h], ax				; Store old INT 13h offset
+	mov		[RAMVARS.fpOldI13h+2], dx			; Store old INT 13h segment
+	mov		bx, INTV_DISK_FUNC					; INT 13h interrupt vector offset
+	mov		si, Int13h_DiskFunctions			; Interrupt handler offset
+	call	Interrupts_InstallHandlerToVectorInBXFromCSSI
+
+	; Only store INT 13h handler to 40h if 40h is not already installed.
+	; At least AMI BIOS for 286 stores 40h handler by itself and calls
+	; 40h from 13h. That system locks to infinite loop if we copy 13h to 40h.
+	call	FloppyDrive_IsInt40hInstalled
+	jnc		SHORT .InitializeInt40h
+	ret
+
+;--------------------------------------------------------------------
+; .InitializeInt40h
+;	Parameters:
+;		DX:AX:	Ptr to old INT 13h handler
+;		ES:		BDA and Interrupt Vector segment (zero)
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		Nothing
+;--------------------------------------------------------------------	
+ALIGN JUMP_ALIGN
+.InitializeInt40h:
+	mov		[es:INTV_FLOPPY_FUNC*4], ax		; Store offset
+	mov		[es:INTV_FLOPPY_FUNC*4+2], dx	; Store segment
+	ret
+
+
+;--------------------------------------------------------------------
+; Interrupts_InitializeInt19h
+;	Parameters:
+;		DS:		RAMVARS segment
+;		ES:		BDA and Interrupt Vector segment (zero)
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		BX, SI
+;--------------------------------------------------------------------	
+ALIGN JUMP_ALIGN
+Interrupts_InitializeInt19h:
+	eMOVZX	bx, [cs:ROMVARS.bBootLdrType]	; Load boot loader type
+	mov		si, INTV_BOOTSTRAP				; 19h
+	xchg	bx, si							; SI=Loader type, BX=19h
+	jmp		[cs:si+.rgwSetupBootLoader]		; Jump to install selected loader
+ALIGN WORD_ALIGN
+.rgwSetupBootLoader:
+	dw		.SetupBootMenuLoader		; BOOTLOADER_TYPE_MENU
+	dw		.SetupSimpleLoader			; BOOTLOADER_TYPE_SIMPLE
+	dw		.SetupBootMenuLoader		; reserved
+	dw		.NoBootLoader				; BOOTLOADER_TYPE_NONE
+
+ALIGN JUMP_ALIGN
+.NoBootLoader:
+	test	BYTE [cs:ROMVARS.wFlags], FLG_ROMVARS_LATE
+	jnz		SHORT .SetupSimpleLoader	; Boot loader required for late initialization
+	ret
+ALIGN JUMP_ALIGN
+.SetupSimpleLoader:
+	mov		si, Int19h_SimpleBootLoader
+	jmp		Interrupts_InstallHandlerToVectorInBXFromCSSI
+ALIGN JUMP_ALIGN
+.SetupBootMenuLoader:
+	mov		si, Int19hMenu_BootLoader
+	jmp		Interrupts_InstallHandlerToVectorInBXFromCSSI
+
+
+;--------------------------------------------------------------------
+; Interrupts_InitializeHardwareIrqHandlers
+;	Parameters:
+;		ES:		BDA and Interrupt Vector segment (zero)
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		BX, CX, DX, SI, DI
+;--------------------------------------------------------------------		
+ALIGN JUMP_ALIGN
+Interrupts_InitializeHardwareIrqHandlers:
+	call	RamVars_GetIdeControllerCountToCX
+	mov		di, ROMVARS.ideVars0			; CS:SI points to first IDEVARS
+ALIGN JUMP_ALIGN
+.IdeControllerLoop:
+	eMOVZX	bx, BYTE [cs:di+IDEVARS.bIRQ]
+	add		di, BYTE IDEVARS_size			; Increment to next controller
+	call	.InstallLowOrHighIrqHandler
+	loop	.IdeControllerLoop
+	ret
+
+;--------------------------------------------------------------------
+; .InstallLowOrHighIrqHandler
+;	Parameters:
+;		BX:		IRQ number, 0 if IRQ disabled
+;		ES:		BDA and Interrupt Vector segment (zero)
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		BX, SI
+;--------------------------------------------------------------------		
+ALIGN JUMP_ALIGN
+.InstallLowOrHighIrqHandler:
+	cmp		bl, 8
+	jae		SHORT .InstallHighIrqHandler
+	test	bl, bl
+	jnz		SHORT .InstallLowIrqHandler
+	ret		; IRQ not used
+
+;--------------------------------------------------------------------
+; .InstallHighIrqHandler
+;	Parameters:
+;		BX:		IRQ number (8...15)
+;		ES:		BDA and Interrupt Vector segment (zero)
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		BX, SI
+;--------------------------------------------------------------------		
+ALIGN JUMP_ALIGN
+.InstallHighIrqHandler:
+	add		bx, BYTE INTV_IRQ8 - 8			; Interrupt vector number
+	mov		si, HIRQ_InterruptServiceRoutineForIrqs8to15
+	jmp		SHORT Interrupts_InstallHandlerToVectorInBXFromCSSI
+
+;--------------------------------------------------------------------
+; .InstallLowIrqHandler
+;	Parameters:
+;		BX:		IRQ number (0...7)
+;		ES:		BDA and Interrupt Vector segment (zero)
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		BX, SI
+;--------------------------------------------------------------------		
+ALIGN JUMP_ALIGN
+.InstallLowIrqHandler:
+	add		bx, BYTE INTV_IRQ0				; Interrupt vector number
+	mov		si, HIRQ_InterruptServiceRoutineForIrqs2to7
+	; Fall to Interrupts_InstallHandlerToVectorInBXFromCSSI
+
+
+;--------------------------------------------------------------------
+; Interrupts_InstallHandlerToVectorInBXFromCSSI
+;	Parameters:
+;		BX:		Interrupt vector number (for example 13h)
+;		ES:		BDA and Interrupt Vector segment (zero)
+;		CS:SI:	Ptr to interrupt handler
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		BX
+;--------------------------------------------------------------------	
+ALIGN JUMP_ALIGN
+Interrupts_InstallHandlerToVectorInBXFromCSSI:
+	eSHL_IM	bx, 2					; Shift for DWORD offset
+	mov		[es:bx], si				; Store offset
+	mov		[es:bx+2], cs			; Store segment
+	ret
+
+
+;--------------------------------------------------------------------
+; Interrupts_UnmaskInterruptControllerForDriveInDSDI
+;	Parameters:
+;		DS:DI:	Ptr to DPT
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		AX, BX, DX
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+Interrupts_UnmaskInterruptControllerForDriveInDSDI:
+	eMOVZX	bx, BYTE [di+DPT.bIdeOff]
+	mov		al, [cs:bx+IDEVARS.bIRQ]
+	cmp		al, 8
+	jae		SHORT .UnmaskHighIrqController
+	test	al, al
+	jnz		SHORT .UnmaskLowIrqController
+	ret		; Interrupts disabled
+
+;--------------------------------------------------------------------
+; .UnmaskHighIrqController
+;	Parameters:
+;		AL:		IRQ number (8...15)
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		AX, DX
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+.UnmaskHighIrqController:
+	sub		al, 8				; Slave interrupt number
+	mov		dx, PORT_8259SL_IMR	; Load Slave Mask Register address
+	call	.ClearBitFrom8259MaskRegister
+	mov		al, 2				; Master IRQ 2 to allow slave IRQs
+	; Fall to .UnmaskLowIrqController
+
+;--------------------------------------------------------------------
+; .UnmaskLowIrqController
+;	Parameters:
+;		AL:		IRQ number (0...7)
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		AX, DX
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+.UnmaskLowIrqController:
+	mov		dx, PORT_8259MA_IMR	; Load Mask Register address
+	; Fall to .ClearBitFrom8259MaskRegister
+
+;--------------------------------------------------------------------
+; .ClearBitFrom8259MaskRegister
+;	Parameters:
+;		AL:		8259 interrupt index (0...7)
+;		DX:		Port address to Interrupt Mask Register
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		AX
+;--------------------------------------------------------------------
+;ALIGN JUMP_ALIGN
+.ClearBitFrom8259MaskRegister:
+	push	cx
+	xchg	ax, cx				; IRQ index to CL
+	mov		ch, 1				; Load 1 to be shifted
+	shl		ch, cl				; Shift bit to correct position
+	not		ch					; Invert to create bit mask for clearing
+	in		al, dx				; Read Interrupt Mask Register
+	and		al, ch				; Clear wanted bit
+	out		dx, al				; Write modified Interrupt Mask Register
+	pop		cx
+	ret
Index: trunk/XTIDE_Universal_BIOS/Src/Main.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Main.asm	(revision 32)
+++ trunk/XTIDE_Universal_BIOS/Src/Main.asm	(revision 33)
@@ -38,5 +38,5 @@
 	at	ROMVARS.bRomSize,	db	CNT_ROM_BLOCKS	; ROM size in 512B blocks
 	at	ROMVARS.rgbJump, 	jmp	Initialize_FromMainBiosRomSearch
-	at	ROMVARS.rgbDate,	db	"08/03/10"		; Build data (mm/dd/yy)
+	at	ROMVARS.rgbDate,	db	"08/23/10"		; Build data (mm/dd/yy)
 	at	ROMVARS.rgbSign,	db	"XTIDE110"		; Signature for flash program
 	at	ROMVARS.szTitle
@@ -49,5 +49,5 @@
 		db	" (XT)=-",STOP
 %endif
-	at	ROMVARS.szVersion,	db	"v1.1.3 (08/03/10)",STOP
+	at	ROMVARS.szVersion,	db	"v1.1.3 (08/23/10)",STOP
 
 ;---------------------------;
@@ -119,4 +119,5 @@
 ; Include .asm files (Initialization and drive detection)
 %include "Initialize.asm"		; For BIOS initialization
+%include "Interrupts.asm"		; For Interrupt initialization
 %include "RamVars.asm"			; For RAMVARS initialization and access
 %include "CreateDPT.asm"		; For creating DPTs
Index: trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/RamVars.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/RamVars.asm	(revision 32)
+++ trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/RamVars.asm	(revision 33)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	14.3.2010
-; Last update	:	3.8.2010
+; Last update	:	23.8.2010
 ; Author		:	Tomi Tilli
 ; Description	:	Functions for accessings RAMVARS.
@@ -23,13 +23,10 @@
 ALIGN JUMP_ALIGN
 RamVars_Initialize:
-	call	RamVars_StealBaseMemory	; Get RAMVARS segment to DS even if no stealing
-	call	RamVars_ClearToZero
+	call	.StealMemoryForRAMVARS	; Get RAMVARS segment to DS even if no stealing
+	call	.ClearRamvarsFromDS
 	jmp		DriveXlate_Reset
 
-
-;--------------------------------------------------------------------
-; Steals base memory to be used for RAMVARS.
-;
-; RamVars_StealBaseMemory
+;--------------------------------------------------------------------
+; .StealMemoryForRAMVARS
 ;	Parameters:
 ;		Nothing
@@ -40,57 +37,18 @@
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
-RamVars_StealBaseMemory:
+.StealMemoryForRAMVARS:
 	test	BYTE [cs:ROMVARS.wFlags], FLG_ROMVARS_FULLMODE
-	jz		SHORT RamVars_GetSegmentToDS	; No need to steal
-
-	LOAD_BDA_SEGMENT_TO	ds, ax				; Zero AX
+	jz		SHORT RamVars_GetSegmentToDS
+
+	LOAD_BDA_SEGMENT_TO	ds, ax		; Zero AX
 	mov		al, [cs:ROMVARS.bStealSize]
-	sub		[BDA.wBaseMem], ax				; Steal one or more kilobytes
-	mov		ax, [BDA.wBaseMem]				; Load base memory left
-	eSHL_IM	ax, 6							; Shift for segment address
-	mov		ds, ax							; Copy RAMVARS segment to DS
-	ret
-
-
-;--------------------------------------------------------------------
-; Returns segment to RAMVARS.
-; RAMVARS might be located at the top of interrupt vectors (0030:0000h)
-; or at the top of system base RAM.
-;
-; RamVars_GetSegmentToDS
-;	Parameters:
-;		Nothing
-;	Returns:
-;		DS:		RAMVARS segment
-;	Corrupts registers:
-;		DI
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-RamVars_GetSegmentToDS:
-	test	BYTE [cs:ROMVARS.wFlags], FLG_ROMVARS_FULLMODE
-	jnz		SHORT RamVars_GetStolenSegmentToDS
-	mov		di, SEGMENT_RAMVARS_TOP_OF_INTERRUPT_VECTORS
-	mov		ds, di
-	ret
-
-ALIGN JUMP_ALIGN
-RamVars_GetStolenSegmentToDS:
-	LOAD_BDA_SEGMENT_TO	ds, di
-	mov		di, [BDA.wBaseMem]		; Load available base memory size in kB
-	eSHL_IM	di, 6					; Segment to first stolen kB
-ALIGN JUMP_ALIGN
-.LoopStolenKBs:
-	mov		ds, di					; EBDA segment to DS
-	add		di, BYTE 64				; DI to next stolen kB
-	cmp		WORD [FULLRAMVARS.wSign], W_SIGN_FULLRAMVARS
-	jne		SHORT .LoopStolenKBs	; Loop until sign found (always found eventually)
-	ret
-
-
-;--------------------------------------------------------------------
-; Clears RAMVARS variables to zero.
-; FULLRAMVARS sign will be saved if available.
-;
-; RamVars_ClearToZero
+	sub		[BDA.wBaseMem], ax
+	mov		ax, [BDA.wBaseMem]
+	eSHL_IM	ax, 6					; Segment to first stolen kB (*=40h)
+	mov		ds, ax
+	ret
+
+;--------------------------------------------------------------------
+; .ClearRamvarsFromDS
 ;	Parameters:
 ;		DS:		RAMVARS segment
@@ -101,5 +59,5 @@
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
-RamVars_ClearToZero:
+.ClearRamvarsFromDS:
 	call	FindDPT_PointToFirstDPT	; Get RAMVARS/FULLRAMVARS size to DI
 	mov		cx, di					; Copy byte count to CX
@@ -110,4 +68,39 @@
 	rep stosb
 	mov		WORD [FULLRAMVARS.wSign], W_SIGN_FULLRAMVARS
+	ret
+
+
+;--------------------------------------------------------------------
+; Returns segment to RAMVARS.
+; RAMVARS might be located at the top of interrupt vectors (0030:0000h)
+; or at the top of system base RAM.
+;
+; RamVars_GetSegmentToDS
+;	Parameters:
+;		Nothing
+;	Returns:
+;		DS:		RAMVARS segment
+;	Corrupts registers:
+;		DI
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+RamVars_GetSegmentToDS:
+	test	BYTE [cs:ROMVARS.wFlags], FLG_ROMVARS_FULLMODE
+	jnz		SHORT .GetStolenSegmentToDS
+	mov		di, SEGMENT_RAMVARS_TOP_OF_INTERRUPT_VECTORS
+	mov		ds, di
+	ret
+
+ALIGN JUMP_ALIGN
+.GetStolenSegmentToDS:
+	LOAD_BDA_SEGMENT_TO	ds, di
+	mov		di, [BDA.wBaseMem]		; Load available base memory size in kB
+	eSHL_IM	di, 6					; Segment to first stolen kB (*=40h)
+ALIGN JUMP_ALIGN
+.LoopStolenKBs:
+	mov		ds, di					; EBDA segment to DS
+	add		di, BYTE 64				; DI to next stolen kB
+	cmp		WORD [FULLRAMVARS.wSign], W_SIGN_FULLRAMVARS
+	jne		SHORT .LoopStolenKBs	; Loop until sign found (always found eventually)
 	ret
 
@@ -234,2 +227,22 @@
 	and		dl, 7Fh						; Clear HD bit for drive count
 	ret
+
+
+;--------------------------------------------------------------------
+; RamVars_GetIdeControllerCountToCX
+;	Parameters:
+;		Nothing
+;	Returns:
+;		CX:		Number of IDE controllers to handle
+;	Corrupts registers:
+;		Nothing
+;--------------------------------------------------------------------	
+ALIGN JUMP_ALIGN
+RamVars_GetIdeControllerCountToCX:
+	mov		cx, 1					; Assume lite mode (one controller)
+	test	BYTE [cs:ROMVARS.wFlags], FLG_ROMVARS_FULLMODE
+	jz		SHORT .Return
+	mov		cl, [cs:ROMVARS.bIdeCnt]
+ALIGN JUMP_ALIGN, ret
+.Return:
+	ret
