Index: trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/FindDPT.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/FindDPT.asm	(revision 3)
+++ trunk/XTIDE_Universal_BIOS/Src/VariablesAndDPTs/FindDPT.asm	(revision 150)
@@ -1,7 +1,3 @@
-; File name		:	FindDPT.asm
-; Project name	:	IDE BIOS
-; Created date	:	14.3.2010
-; Last update	:	12.4.2010
-; Author		:	Tomi Tilli
+; Project name	:	XTIDE Universal BIOS
 ; Description	:	Functions for finding Disk Parameter Table.
 
@@ -12,5 +8,5 @@
 ; Finds pointer to first unused Disk Parameter Table.
 ;
-; FindDPT_ForNewDrive
+; FindDPT_ForNewDriveToDSDI
 ;	Parameters:
 ;		DS:		RAMVARS segment
@@ -18,86 +14,11 @@
 ;		DS:DI:	Ptr to first unused DPT
 ;	Corrupts registers:
-;		Nothing
+;		DL
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
-FindDPT_ForNewDrive:
-	push	si
-	mov		si, FindDPT_ReturnWrongDPT
-	jmp		SHORT FindDPT_StartIterationAndReturnAfterDone
-
-
-;--------------------------------------------------------------------
-; Finds Disk Parameter Table for
-; Master or Slave drive at wanted port.
-;
-; FindDPT_ForIdeSlaveAtPort
-; FindDPT_ForIdeMasterAtPort
-;	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:
-;		Nothing
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-FindDPT_ForIdeSlaveAtPort:
-	push	si
-	mov		si, FindDPT_IterateToSlaveAtPortCallback
-	jmp		SHORT FindDPT_StartIterationAndReturnAfterDone
-
-ALIGN JUMP_ALIGN
-FindDPT_ForIdeMasterAtPort:
-	push	si
-	mov		si, FindDPT_IterateToMasterAtPortCallback
-	jmp		SHORT FindDPT_StartIterationAndReturnAfterDone
-
-;--------------------------------------------------------------------
-; Iteration callback for finding DPT using
-; IDE base port for Master or Slave drive.
-;
-; FindDPT_IterateToSlaveAtPortCallback
-; FindDPT_IterateToMasterAtPortCallback
-;	Parameters:
-;		DX:		IDE Base Port address
-;		DS:DI:	Ptr to DPT to examine
-;	Returns:
-;		DL:		Drive number if correct DPT
-;		CF:		Set if wanted DPT found
-;				Cleared if wrong DPT
-;	Corrupts registers:
-;		Nothing
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-FindDPT_IterateToSlaveAtPortCallback:
-	test	BYTE [di+DPT.bDrvSel], FLG_IDE_DRVHD_DRV
-	jnz		SHORT FindDPT_IterateToMasterOrSlaveAtPortCallback
-	jmp		SHORT FindDPT_ReturnWrongDPT	; Return if master drive
-
-ALIGN JUMP_ALIGN
-FindDPT_IterateToMasterAtPortCallback:
-	test	BYTE [di+DPT.bDrvSel], FLG_IDE_DRVHD_DRV
-	jnz		SHORT FindDPT_ReturnWrongDPT	; Return if slave drive
-
-	; If BIOS partitioned, ignore all but first partition
-ALIGN JUMP_ALIGN
-FindDPT_IterateToMasterOrSlaveAtPortCallback:
-	test	BYTE [di+DPT.bFlags], FLG_DPT_PARTITION
-	jz		SHORT .CompareBasePortAddress
-	test	BYTE [di+DPT.bFlags], FLG_DPT_FIRSTPART
-	jz		SHORT FindDPT_ReturnWrongDPT
-ALIGN JUMP_ALIGN
-.CompareBasePortAddress:
-	push	bx
-	eMOVZX	bx, BYTE [di+DPT.bIdeOff]		; CS:BX now points to IDEVARS
-	cmp		dx, [cs:bx+IDEVARS.wPort]		; Wanted port?
-	pop		bx
-	jne		SHORT FindDPT_ReturnWrongDPT
-	mov		dl, [di+DPT.bDrvNum]			; Load drive number
-	stc										; Set CF since wanted DPT
-	ret
+FindDPT_ForNewDriveToDSDI:
+	mov		dl, [RAMVARS.bFirstDrv]
+	add		dl, [RAMVARS.bDrvCnt]
+	; Fall to FindDPT_ForDriveNumber
 
 
@@ -112,6 +33,4 @@
 ;	Returns:
 ;		DS:DI:	Ptr to DPT
-;		CF:		Set if wanted DPT found
-;				Cleared if DPT not found
 ;	Corrupts registers:
 ;		Nothing
@@ -119,19 +38,57 @@
 ALIGN JUMP_ALIGN
 FindDPT_ForDriveNumber:
-	push	si
-	mov		si, FindDPT_IterateToDriveNumberCallback
-FindDPT_StartIterationAndReturnAfterDone:
-	call	FindDPT_IterateAllDPTs
-	pop		si
+	push	dx
+	push	ax
+
+	mov		al, LARGEST_DPT_SIZE
+	sub		dl, [RAMVARS.bFirstDrv]
+	mul		dl
+	add		ax, BYTE RAMVARS_size
+	xchg	di, ax
+
+	pop		ax
+	pop		dx
 	ret
 
+
 ;--------------------------------------------------------------------
-; Iteration callback for finding DPT for drive number.
+; Finds Disk Parameter Table for
+; Master or Slave drive at wanted port.
 ;
-; FindDPT_IterateToDriveNumberCallback
+; FindDPT_ToDSDIForIdeMasterAtPortDX
+; FindDPT_ToDSDIForIdeSlaveAtPortDX
 ;	Parameters:
-;		DL:		Drive number to search for
+;		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
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+FindDPT_ToDSDIForIdeMasterAtPortDX:
+	mov		si, FindDPT_IterateToMasterAtPortCallback
+	jmp		SHORT IterateAllDPTs
+
+ALIGN JUMP_ALIGN
+FindDPT_ToDSDIForIdeSlaveAtPortDX:
+	mov		si, FindDPT_IterateToSlaveAtPortCallback
+	jmp		SHORT IterateAllDPTs
+
+;--------------------------------------------------------------------
+; Iteration callback for finding DPT using
+; IDE base port for Master or Slave drive.
+;
+; FindDPT_IterateToSlaveAtPortCallback
+; FindDPT_IterateToMasterAtPortCallback
+;	Parameters:
+;		CH:		Drive number
+;		DX:		IDE Base Port address
 ;		DS:DI:	Ptr to DPT to examine
 ;	Returns:
+;		DL:		Drive number if correct DPT
 ;		CF:		Set if wanted DPT found
 ;				Cleared if wrong DPT
@@ -140,18 +97,25 @@
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
-FindDPT_IterateToDriveNumberCallback:
-	cmp		dl, [di+DPT.bDrvNum]			; Wanted DPT found?
-	je		SHORT FindDPT_RightDriveNumber	;  If so, return
-FindDPT_ReturnWrongDPT:
-	clc										; Clear CF since wrong DPT
+FindDPT_IterateToSlaveAtPortCallback:
+	test	BYTE [di+DPT.wFlags], FLG_DPT_SLAVE	; Clears CF
+	jnz		SHORT CompareBasePortAddress
+	ret		; Wrong DPT
+
+ALIGN JUMP_ALIGN
+FindDPT_IterateToMasterAtPortCallback:
+	test	BYTE [di+DPT.wFlags], FLG_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
+	mov		dl, ch								; Return drive number in DL
+	stc											; Set CF since wanted DPT
 	ret
-ALIGN JUMP_ALIGN
-FindDPT_RightDriveNumber:
-	push	bx
-	eMOVZX	bx, BYTE [di+DPT.bIdeOff]		; CS:BX now points to IDEVARS
-	mov		bx, [cs:bx+IDEVARS.wPort]		; Load IDE Base Port address...
-	mov		[RAMVARS.wIdeBase], bx			; ...and store it to RAMVARS
-	pop		bx
-	stc
+ReturnWrongDPT:
+	clc											; Clear CF since wrong DPT
 	ret
 
@@ -160,5 +124,5 @@
 ; Iterates all Disk Parameter Tables.
 ;
-; FindDPT_IterateAllDPTs
+; IterateAllDPTs
 ;	Parameters:
 ;		BX,DX:	Parameters to callback function
@@ -170,26 +134,24 @@
 ;				Cleared if DPT not found
 ;	Corrupts registers:
-;		Nothing, unless corrupted by callback function
+;		Nothing unless corrupted by callback function
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
-FindDPT_IterateAllDPTs:
-	push	ax
+IterateAllDPTs:
 	push	cx
+	mov		cx, [RAMVARS.wDrvCntAndFirst]
+	jcxz	.AllDptsIterated			; Return if no drives
 	call	FindDPT_PointToFirstDPT		; Point DS:DI to first DPT
-	eMOVZX	cx, BYTE [RAMVARS.bDrvCnt]	; Load number of drives
-	xor		ax, ax						; Zero AX for DPT size and clear CF
-	jcxz	.Return						; Return if no drives
 ALIGN JUMP_ALIGN
 .LoopWhileDPTsLeft:
 	call	si							; Is wanted DPT?
-	jc		SHORT .Return				;  If so, return
-	mov		al, [di+DPT.bSize]			; Load DPT size to AX
-	add		di, ax						; Point to next DPT
-	loop	.LoopWhileDPTsLeft			; Check next DPT
+	jc		SHORT .AllDptsIterated		;  If so, return
+	inc		ch							; Increment drive number
+	add		di, BYTE LARGEST_DPT_SIZE	; Point to next DPT
+	dec		cl							; Decrement drives left
+	jnz		SHORT .LoopWhileDPTsLeft
 	clc									; Clear CF since DPT not found
 ALIGN JUMP_ALIGN
-.Return:
+.AllDptsIterated:
 	pop		cx
-	pop		ax
 	ret
 
@@ -209,8 +171,3 @@
 FindDPT_PointToFirstDPT:
 	mov		di, RAMVARS_size
-	test	BYTE [cs:ROMVARS.wFlags], FLG_ROMVARS_FULLMODE
-	jz		SHORT .Return							; RAMVARS used (top of interrupt vectors)
-	add		di, BYTE FULLRAMVARS_size-RAMVARS_size	; FULLRAMVARS used (top of base memory)
-ALIGN JUMP_ALIGN
-.Return:
 	ret
