Index: trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/CreateDPT.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/CreateDPT.asm	(revision 259)
+++ trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/CreateDPT.asm	(revision 262)
@@ -200,4 +200,8 @@
 	call	Device_FinalizeDPT
 
+;----------------------------------------------------------------------
+; Update drive counts (hard and floppy)
+;----------------------------------------------------------------------	
+		
 %ifdef MODULE_SERIAL_FLOPPY
 ;
@@ -223,31 +227,7 @@
 %endif
 
-	; Fall to .StoreDriveNumberAndUpdateDriveCount
-
-;--------------------------------------------------------------------
-; .StoreDriveNumberAndUpdateDriveCount
-;	Parameters:
-;		DS:DI:	Ptr to Disk Parameter Table
-;		ES:SI:	Ptr to 512-byte ATA information read from the drive
-;		CS:BP:	Ptr to IDEVARS for the controller
-;		ES:		BDA Segment
-;	Returns:
-;		DL:		Drive number for new drive
-;		CF:		Always cleared
-;	Corrupts registers:
-;		Nothing
-;--------------------------------------------------------------------
-.StoreDriveNumberAndUpdateDriveCount:
-	mov		dl, [es:BDA.bHDCount]
-	or		dl, 80h						; Set bit 7 since hard disk
-
 	inc		BYTE [RAMVARS.bDrvCnt]		; Increment drive count to RAMVARS
-	inc		BYTE [es:BDA.bHDCount]		; Increment drive count to BDA
-
-	cmp		BYTE [RAMVARS.bFirstDrv], 0	; First drive set?
-	ja		SHORT .AllDone				;  If so, return
-	mov		[RAMVARS.bFirstDrv], dl		; Store first drive number
-
-.AllDone:
+		
+.AllDone:				
 	clc
 	ret
Index: trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/FindDPT.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/FindDPT.asm	(revision 259)
+++ trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/FindDPT.asm	(revision 262)
@@ -6,41 +6,75 @@
 
 ;--------------------------------------------------------------------
-; Finds pointer to first unused Disk Parameter Table.
-;
-; FindDPT_ForNewDriveToDSDI
-;	Parameters:
-;		DS:		RAMVARS segment
-;	Returns:
-;		DS:DI:	Ptr to first unused DPT
-;	Corrupts registers:
-;		DX
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-FindDPT_ForNewDriveToDSDI:
-	mov		ax, [RAMVARS.wDrvCntAndFirst]
-	add		al, ah
+; Checks if drive is handled by this BIOS, and return DPT pointer.
+;
+; FindDPT_ForDriveNumberInDL		
+;	Parameters:
+;		DL:		Drive number
+;		DS:		RAMVARS segment
+;	Returns:
+;		CF:		Cleared if drive is handled by this BIOS
+;				Set if drive belongs to some other BIOS
+;		DI:		DPT Pointer if drive is handled by this BIOS
+;				Zero if drive belongs to some other BIOS
+;	Corrupts registers:
+;		Nothing
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+FindDPT_ForDriveNumberInDL:		
+	xchg	di, ax								; Save the contents of AX in DI
+
+; 
+; Check Our Hard Disks
+;
+	mov		ax, [RAMVARS.wDrvCntAndFirst]		; Drive count to AH, First number to AL
+	add		ah, al								; One past last drive to AH
+
 %ifdef MODULE_SERIAL_FLOPPY
-	add		al, [RAMVARS.xlateVars+XLATEVARS.bFlopCreateCnt]
+	cmp		dl, ah								; Above last supported?
+	jae		SHORT .HardDiskNotHandledByThisBIOS
+		
+	cmp		dl, al								; Below first supported?
+	jae		SHORT .CalcDPTForDriveNumber
+
+ALIGN JUMP_ALIGN				
+.HardDiskNotHandledByThisBIOS:	
+;
+; Check Our Floppy Disks
+; 
+	call	RamVars_UnpackFlopCntAndFirstToAL
+	cbw											; normally 0h, could be ffh if no drives present
+	adc		ah, al								; if no drives present, still ffh (ffh + ffh + 1 = ffh)
+	js		SHORT .DiskIsNotHandledByThisBIOS
+	cmp		ah, dl								; Check second drive if two, first drive if only one
+	jz		SHORT .CalcDPTForDriveNumber
+	cmp		al, dl								; Check first drive in all cases, redundant but OK to repeat
+	jnz		SHORT .DiskIsNotHandledByThisBIOS			
+%else
+	cmp		dl, ah								; Above last supported?		
+	jae		SHORT .DiskIsNotHandledByThisBIOS
+		
+	cmp		dl, al								; Below first supported?
+	jb		SHORT .DiskIsNotHandledByThisBIOS			
 %endif
-	xchg	ax, dx
-	; fall-through to FindDPT_ForDriveNumber
+	; fall-through to CalcDPTForDriveNumber
 
 ;--------------------------------------------------------------------
 ; Finds Disk Parameter Table for drive number.
-; IDE Base Port address will be stored to RAMVARS if correct DPT is found.
-;
-; FindDPT_ForDriveNumber
+; Note intended to be called except by FindDPT_ForDriveNumber
+;
+; CalcDPTForDriveNumber
 ;	Parameters:
 ;		DL:		Drive number
 ;		DS:		RAMVARS segment
+;       DI:     Saved copy of AX from entry at FindDPT_ForDriveNumber
 ;	Returns:
 ;		DS:DI:	Ptr to DPT
+;       CF:     Clear
 ;	Corrupts registers:
 ;		Nothing
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
-FindDPT_ForDriveNumber:
+.CalcDPTForDriveNumber:
 	push	dx
-	xchg	di, ax	; Save the contents of AX in DI
 
 %ifdef MODULE_SERIAL_FLOPPY
@@ -52,4 +86,6 @@
 	call	RamVars_UnpackFlopCntAndFirstToAL
 	add		dl, ah						; add in end of hard disk DPT list, floppies start immediately after
+		
+ALIGN JUMP_ALIGN				
 .harddisk:
 	sub		dl, al						; subtract off beginning of either hard disk or floppy list (as appropriate)
@@ -57,105 +93,51 @@
 	sub		dl, [RAMVARS.bFirstDrv]		; subtract off beginning of hard disk list
 %endif
-		
+
+.CalcDPTForNewDrive:				
 	mov		al, LARGEST_DPT_SIZE
 		
 	mul		dl
-	add		ax, BYTE RAMVARS_size
-
-	xchg	di, ax						; Restore AX and put result in DI
+	add		ax, BYTE RAMVARS_size		; Clears CF (will not oveflow)
+
 	pop		dx
-		
-	ret
-
-;--------------------------------------------------------------------
-; Consolidator for checking the result from RamVars_IsDriveHandledByThisBIOS
-; and then if it is our drive, getting the DPT with FindDPT_ForDriveNumber
-;
-; RamVars_IsDriveHandledByThisBIOS_And_FindDPT_ForDriveNumber
-;	Parameters:
-;		DL:		Drive number
-;		DS:		RAMVARS segment
-;	Returns:
-;		DS:DI:	Ptr to DPT, if it is our drive
-;       CF:     Set if not our drive, clear if it is our drive
-;	Corrupts registers:
-;		Nothing
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-RamVars_IsDriveHandledByThisBIOS_And_FindDPT_ForDriveNumber:
-	call	RamVars_IsDriveHandledByThisBIOS
-	jnc		FindDPT_ForDriveNumber
-	ret
-
-
-;--------------------------------------------------------------------
-; Finds Disk Parameter Table for
-; Master or Slave drive at wanted port.
-;
-; FindDPT_ToDSDIForIdeMasterAtPortDX
-; FindDPT_ToDSDIForIdeSlaveAtPortDX
-;	Parameters:
-;		DX:		IDE Base Port address
-;		DS:		RAMVARS segment
-;	Returns:
-;		DL:		Drive number (if DPT found)
-;		DS:DI:	Ptr to DPT
-;		CF:		Set if wanted DPT found
-;				Cleared if DPT not found
-;	Corrupts registers:
-;		SI
-;
-; Converted to macros since there is only once call site for each of these
-;
-;--------------------------------------------------------------------
-	
-%macro FindDPT_ToDSDIForIdeMasterAtPortDX 0
-	mov		si, IterateToMasterAtPortCallback
-	call	IterateAllDPTs
-%endmacro
-
-%macro FindDPT_ToDSDIForIdeSlaveAtPortDX 0
-	mov		si, IterateToSlaveAtPortCallback
-	call	IterateAllDPTs
-%endmacro
-
-		
-;--------------------------------------------------------------------
-; Iteration callback for finding DPT using
-; IDE base port for Master or Slave drive.
-;
-; IterateToSlaveAtPortCallback
-; IterateToMasterAtPortCallback
-;	Parameters:
-;		DX:		IDE Base Port address
-;		DS:DI:	Ptr to DPT to examine
-;	Returns:
-;		CF:		Set if wanted DPT found
-;				Cleared if wrong DPT
-;	Corrupts registers:
-;		Nothing
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-IterateToSlaveAtPortCallback:
-	test	BYTE [di+DPT.bFlagsLow], FLGL_DPT_SLAVE	; Clears CF
-	jnz		SHORT CompareBasePortAddress
-	ret		; Wrong DPT
-
-ALIGN JUMP_ALIGN
-IterateToMasterAtPortCallback:
-	test	BYTE [di+DPT.bFlagsLow], FLGL_DPT_SLAVE
-	jnz		SHORT ReturnWrongDPT				; Return if slave drive
-
-CompareBasePortAddress:
-	push	bx
-	eMOVZX	bx, BYTE [di+DPT.bIdevarsOffset]	; CS:BX now points to IDEVARS
-	cmp		dx, [cs:bx+IDEVARS.wPort]			; Wanted port?
-	pop		bx
-	jne		SHORT ReturnWrongDPT
-
-ReturnRightDPT:
-	stc											; Set CF since wanted DPT
-	ret
-
+
+	xchg	di, ax						; Restore AX from entry at FindDPT_ForDriveNumber, put DPT pointer in DI
+	ret
+
+ALIGN JUMP_ALIGN		
+.DiskIsNotHandledByThisBIOS:			
+;
+; Drive not found...
+;
+	xor		ax, ax								; Clear DPT pointer
+	stc											; Is not supported by our BIOS		
+		
+	xchg	di, ax								; Restore AX from save at top
+	ret
+
+;--------------------------------------------------------------------
+; Finds pointer to first unused Disk Parameter Table.
+; Should only be used before DetectDrives is complete (not valid after this time).
+;
+; FindDPT_ForNewDriveToDSDI
+;	Parameters:
+;		DS:		RAMVARS segment
+;	Returns:
+;		DS:DI:	Ptr to first unused DPT
+;	Corrupts registers:
+;		AX
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+FindDPT_ForNewDriveToDSDI:
+	push	dx
+		
+%ifdef MODULE_SERIAL_FLOPPY
+	mov		dx, [RAMVARS.wDrvCntAndFlopCnt]
+	add		dl, dh
+%else
+	mov		dl, [RAMVARS.bDrvCnt]
+%endif
+		
+	jmp		short FindDPT_ForDriveNumberInDL.CalcDPTForNewDrive
 
 ;--------------------------------------------------------------------
@@ -165,6 +147,6 @@
 ;       BL:		Bit(s) to test in DPT.bFlagsHigh 
 ;	Returns:
-;		CF:		Set if wanted DPT found
-;				Cleared if wrong DPT
+;		CF:		Clear if wanted DPT found
+;				Set if wrong DPT
 ;	Corrupts registers:
 ;		Nothing
@@ -172,10 +154,8 @@
 ALIGN JUMP_ALIGN
 IterateToDptWithFlagsHighInBL:		
-	test	BYTE [di+DPT.bFlagsHigh], bl		; Clears CF (but we need the clc
-												; below anyway for callers above)
-	jnz		SHORT ReturnRightDPT
-
-ReturnWrongDPT:
-	clc										; Clear CF since wrong DPT
+	test	BYTE [di+DPT.bFlagsHigh], bl		; Clears CF
+	jnz		SHORT .ReturnRightDPT
+	stc
+.ReturnRightDPT:		
 	ret
 
@@ -220,10 +200,11 @@
 ;		AX,BX,DX:	Parameters to callback function
 ;		CS:SI:		Ptr to callback function
+;                   Callback routine should return CF=clear if found
 ;		DS:			RAMVARS segment
 ;	Returns:
 ;		DS:DI:		Ptr to wanted DPT (if found)
 ;					If not found, points to first empty DPT
-;		CF:			Set if wanted DPT found
-;					Cleared if DPT not found, or no DPTs present
+;		CF:			Clear if wanted DPT found
+;					Set if DPT not found, or no DPTs present
 ;	Corrupts registers:
 ;		Nothing unless corrupted by callback function
@@ -238,17 +219,19 @@
 	xor		ch, ch						; Clears CF  
 		
-	jcxz	.AllDptsIterated			; Return if no drives, CF will be clear from xor above
+	jcxz	.NotFound					; Return if no drives
 		
 ALIGN JUMP_ALIGN
 .LoopWhileDPTsLeft:
 	call	si							; Is wanted DPT?
-	jc		SHORT .AllDptsIterated		;  If so, return
-	add		di, BYTE LARGEST_DPT_SIZE	; Point to next DPT, clears CF
+	jnc		SHORT .Found				;  If so, return
+	add		di, BYTE LARGEST_DPT_SIZE	; Point to next DPT
 	loop	.LoopWhileDPTsLeft
-	
-	; fall-through: DPT was not found, CF is already clear from ADD di inside the loop
-		
-ALIGN JUMP_ALIGN
-.AllDptsIterated:
+
+ALIGN JUMP_ALIGN
+.NotFound:
+	stc
+
+ALIGN JUMP_ALIGN
+.Found:
 	pop		cx
 	ret
Index: trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/RamVars.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/RamVars.asm	(revision 259)
+++ trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/RamVars.asm	(revision 262)
@@ -115,88 +115,4 @@
 
 ;--------------------------------------------------------------------
-; Checks if INT 13h function is handled by this BIOS.
-;
-; RamVars_IsFunctionHandledByThisBIOS
-;	Parameters:
-;		AH:		INT 13h function number
-;		DL:		Drive number
-;		DS:		RAMVARS segment
-;	Returns:
-;		CF:		Cleared if function is handled by this BIOS
-;				Set if function belongs to some other BIOS
-;	Corrupts registers:
-;		Nothing
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-RamVars_IsFunctionHandledByThisBIOS:
-	test	ah, ah			; Reset for all floppy and hard disk drives?
-	jz		SHORT RamVars_IsDriveHandledByThisBIOS.CFAlreadyClear_IsHandledByOurBIOS
-	cmp		ah, 08h
-%ifdef MODULE_SERIAL_FLOPPY
-; we handle all traffic for function 08h, as we need to wrap both hard disk and floppy drive counts
-	je		SHORT RamVars_IsDriveHandledByThisBIOS.CFAlreadyClear_IsHandledByOurBIOS
-%else
-; we handle all *hard disk* traffic for function 08h, as we need to wrap the hard disk drive count
-	je		SHORT RamVars_IsDriveHandledByThisBIOS.IsDriveAHardDisk
-%endif
-;;; fall-through			
-		
-;--------------------------------------------------------------------
-; Checks if drive is handled by this BIOS.
-;
-; RamVars_IsDriveHandledByThisBIOS
-;	Parameters:
-;		DL:		Drive number
-;		DS:		RAMVARS segment
-;	Returns:
-;		CF:		Cleared if drive is handled by this BIOS
-;				Set if drive belongs to some other BIOS
-;	Corrupts registers:
-;		Nothing
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-RamVars_IsDriveHandledByThisBIOS:
-	push	ax
-
-	mov		ax, [RAMVARS.wDrvCntAndFirst]		; Drive count to AH, First number to AL
-	add		ah, al								; One past last drive to AH
-	cmp		dl, ah								; Above last supported?
-	jae		SHORT .HardDiskIsNotHandledByThisBIOS
-.TestLowLimit:
-	cmp		dl, al								; Below first supported?
-	jae		SHORT .CFAlreadyClear_IsHandledByOurBIOS_PopAX	; note that CF is clear if the branch is taken
-
-.HardDiskIsNotHandledByThisBIOS:
-%ifdef MODULE_SERIAL_FLOPPY
-	call	RamVars_UnpackFlopCntAndFirstToAL
-	cbw											; normally 0h, could be ffh if no drives present
-	adc		ah, al								; if no drives present, still ffh (ffh + ffh + 1 = ffh)
-	js		SHORT .DiskIsNotHandledByThisBIOS
-	cmp		ah, dl
-	jz		SHORT .CFAlreadyClear_IsHandledByOurBIOS_PopAX
-	cmp		al, dl
-	jz		SHORT .CFAlreadyClear_IsHandledByOurBIOS_PopAX
-.DiskIsNotHandledByThisBIOS:			
-%endif
-
-	stc											; Is not supported by our BIOS
-		
-.CFAlreadyClear_IsHandledByOurBIOS_PopAX:				
-	pop		ax
-.CFAlreadyClear_IsHandledByOurBIOS:	
-	ret
-
-%ifndef MODULE_SERIAL_FLOPPY		
-;
-; Note that we could have just checked for the high order bit in dl, but with the needed STC and jumps, 
-; leveraging the code above resulted in space savings.
-;
-.IsDriveAHardDisk:		
-	push	ax									; match stack at the top of routine
-	mov		al, 80h								; to catch all hard disks, lower limit is 80h vs. bFirstDrv
-	jmp		.TestLowLimit						; and there is no need to test a high limit
-%endif
-
-;--------------------------------------------------------------------
 ; RamVars_GetHardDiskCountFromBDAtoAX
 ;	Parameters:
