Index: trunk/XTIDE_Universal_BIOS/Src/Device/IDE/IdeTransfer.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Device/IDE/IdeTransfer.asm	(revision 361)
+++ trunk/XTIDE_Universal_BIOS/Src/Device/IDE/IdeTransfer.asm	(revision 363)
@@ -5,11 +5,10 @@
 ; This struct must not be larger than IDEPACK without INTPACK.
 struc PIOVARS
-	.wWordsInBlock			resb	2	; 0, Block size in WORDs
-	.wWordsLeft				resb	2	; 2, WORDs left to transfer
-	.wWordsDone				resb	2	; 4, Number of sectors xferred
-							resb	1	; 6,
+	.wDataPort				resb	2	; 0, IDE Data Port
+	.fnXfer					resb	2	; 2, Offset to transfer function
+	.wSectorsInBlock		resb	2	; 4, Block size in sectors
+	.bSectorsLeft			resb	1	; 6, Sectors left to transfer
 							resb	1	; 7, IDEPACK.bDeviceControl
-	.wDataPort				resb	2	; 8, IDE Data Port
-	.fnXfer					resb	2	; 10, Offset to transfer function
+	.bSectorsDone			resb	1	; 8, Number of sectors xferred
 endstruc
 
@@ -69,10 +68,10 @@
 	xchg	si, di								; ES:DI now points buffer
 
-	mov		cx, [bp+PIOVARS.wWordsInBlock]
+	mov		cx, [bp+PIOVARS.wSectorsInBlock]	; Max 128
 
 ALIGN JUMP_ALIGN
 .ReadNextBlockFromDrive:
 	mov		dx, [bp+PIOVARS.wDataPort]
-	cmp		[bp+PIOVARS.wWordsLeft], cx
+	cmp		[bp+PIOVARS.bSectorsLeft], cl
 	jbe		SHORT .ReadLastBlockFromDrive
 	call	[bp+PIOVARS.fnXfer]
@@ -84,13 +83,13 @@
 	xchg	si, di								; ES:DI now points buffer
 
-	; Increment number of successfully read WORDs
-	mov		cx, [bp+PIOVARS.wWordsInBlock]
-	sub		[bp+PIOVARS.wWordsLeft], cx
-	add		[bp+PIOVARS.wWordsDone], cx
+	; Increment number of successfully read sectors
+	mov		cx, [bp+PIOVARS.wSectorsInBlock]
+	sub		[bp+PIOVARS.bSectorsLeft], cl
+	add		[bp+PIOVARS.bSectorsDone], cl
 	jmp		SHORT .ReadNextBlockFromDrive
 
 ALIGN JUMP_ALIGN
 .ReadLastBlockFromDrive:
-	mov		cx, [bp+PIOVARS.wWordsLeft]
+	mov		cl, [bp+PIOVARS.bSectorsLeft]		; CH is already zero
 	call	[bp+PIOVARS.fnXfer]					; Transfer possibly partial block
 
@@ -103,9 +102,6 @@
 	; Return number of successfully read sectors
 ReturnWithTransferErrorInAH:
-	mov		cx, [bp+PIOVARS.wWordsDone]
-	jc		SHORT .ConvertTransferredWordsInCXtoSectors
-	add		cx, [bp+PIOVARS.wWordsLeft]			; Never sets CF
-.ConvertTransferredWordsInCXtoSectors:
-	xchg	cl, ch
+	mov		cl, [bp+PIOVARS.bSectorsDone]
+	mov		ch, 0								; Preserve CF
 	ret
 
@@ -137,10 +133,10 @@
 	jc		SHORT ReturnWithTransferErrorInAH
 
-	mov		cx, [bp+PIOVARS.wWordsInBlock]
+	mov		cx, [bp+PIOVARS.wSectorsInBlock]	; Max 128
 
 ALIGN JUMP_ALIGN
 .WriteNextBlockToDrive:
 	mov		dx, [bp+PIOVARS.wDataPort]
-	cmp		[bp+PIOVARS.wWordsLeft], cx
+	cmp		[bp+PIOVARS.bSectorsLeft], cl
 	jbe		SHORT .WriteLastBlockToDrive
 	call	[bp+PIOVARS.fnXfer]
@@ -150,13 +146,13 @@
 	jc		SHORT ReturnWithTransferErrorInAH
 
-	; Increment number of successfully written WORDs
-	mov		cx, [bp+PIOVARS.wWordsInBlock]
-	sub		[bp+PIOVARS.wWordsLeft], cx
-	add		[bp+PIOVARS.wWordsDone], cx
+	; Increment number of successfully written sectors
+	mov		cx, [bp+PIOVARS.wSectorsInBlock]
+	sub		[bp+PIOVARS.bSectorsLeft], cl
+	add		[bp+PIOVARS.bSectorsDone], cl
 	jmp		SHORT .WriteNextBlockToDrive
 
 ALIGN JUMP_ALIGN
 .WriteLastBlockToDrive:
-	mov		cx, [bp+PIOVARS.wWordsLeft]
+	mov		cl, [bp+PIOVARS.bSectorsLeft]		; CH is already zero
 %ifdef USE_186
 	push	CheckErrorsAfterTransferringLastBlock
@@ -183,10 +179,8 @@
 InitializePiovarsInSSBPwithSectorCountInAH:
 	; Store sizes
-	xor		al, al
-	mov		[bp+PIOVARS.wWordsLeft], ax
-	mov		ah, [di+DPT_ATA.bSetBlock]
-	mov		[bp+PIOVARS.wWordsInBlock], ax
-	cbw
-	mov		[bp+PIOVARS.wWordsDone], ax			; Zero
+	mov		[bp+PIOVARS.bSectorsLeft], ah
+	eMOVZX	ax, BYTE [di+DPT_ATA.bSetBlock]
+	mov		[bp+PIOVARS.wSectorsInBlock], ax
+	mov		[bp+PIOVARS.bSectorsDone], ah		; Zero
 
 	; Get transfer function based on bus type
@@ -194,6 +188,11 @@
 	mov		bl, [di+DPT.bIdevarsOffset]			; CS:BX now points to IDEVARS
 	mov		dx, [cs:bx+IDEVARS.wPort]			; Load IDE Data port address
+%ifdef MODULE_ADVANCED_ATA
+	mov		bl, [di+DPT_ATA.bDevice]
+%else
 	mov		bl, [cs:bx+IDEVARS.bDevice]			; Load device type to BX
+%endif
 	add		bx, ax
+
 	mov		[bp+PIOVARS.wDataPort], dx
 	mov		ax, [cs:bx]							; Load offset to transfer function
@@ -208,5 +207,5 @@
 ; ReadBlockFrom32bitDataPort	VLB/PCI 32-bit IDE
 ;	Parameters:
-;		CX:		Block size in WORDs
+;		CX:		Block size in 512 byte sectors
 ;		DX:		IDE Data port address
 ;		ES:DI:	Normalized ptr to buffer to receive data
@@ -218,5 +217,5 @@
 ALIGN JUMP_ALIGN
 ReadBlockFromXtideRev1:
-	eSHR_IM	cx, 2		; Loop unrolling
+	UNROLL_SECTORS_IN_CX_TO_QWORDS
 	mov		bx, 8		; Bit mask for toggling data low/high reg
 ALIGN JUMP_ALIGN
@@ -233,5 +232,5 @@
 ALIGN JUMP_ALIGN
 ReadBlockFromXtideRev2:
-	times 2 shr	cx, 1	; WORD count to QWORD count
+	UNROLL_SECTORS_IN_CX_TO_QWORDS
 ALIGN JUMP_ALIGN
 .ReadNextQword:
@@ -251,4 +250,5 @@
 ALIGN JUMP_ALIGN
 ReadBlockFrom16bitDataPort:
+	xchg	cl, ch		; Sectors to WORDs
 	rep
 	db		6Dh			; INSW (we want this in XT build)
@@ -258,5 +258,7 @@
 ALIGN JUMP_ALIGN
 ReadBlockFrom32bitDataPort:
-	shr		cx, 1		; WORD count to DWORD count
+	db		0C1h		; SHL
+	db		0E1h		; CX
+	db		7			; 7	(Sectors to DWORDs)
 	rep
 	db		66h			; Override operand size to 32-bit
@@ -272,5 +274,5 @@
 ; WriteBlockTo32bitDataPort		VLB/PCI 32-bit IDE
 ;	Parameters:
-;		CX:		Block size in WORDs
+;		CX:		Block size in 512-byte sectors
 ;		DX:		IDE Data port address
 ;		ES:SI:	Normalized ptr to buffer containing data
@@ -284,5 +286,5 @@
 	push	ds
 	push	bx
-	eSHR_IM	cx, 2		; Loop unrolling
+	UNROLL_SECTORS_IN_CX_TO_QWORDS
 	mov		bx, 8		; Bit mask for toggling data low/high reg
 	push	es			; Copy ES...
@@ -302,6 +304,6 @@
 ALIGN JUMP_ALIGN
 WriteBlockToXtideRev2:
+	UNROLL_SECTORS_IN_CX_TO_QWORDS
 	push	ds
-	eSHR_IM	cx, 2		; Loop unrolling
 	push	es			; Copy ES...
 	pop		ds			; ...to DS
@@ -320,5 +322,5 @@
 ALIGN JUMP_ALIGN
 WriteBlockToFastXtide:
-	times 2 shr	cx, 1	; WORD count to QWORD count
+	UNROLL_SECTORS_IN_CX_TO_QWORDS
 	push	ds
 	push	es
@@ -342,4 +344,5 @@
 ALIGN JUMP_ALIGN
 WriteBlockTo16bitDataPort:
+	xchg	cl, ch		; Sectors to WORDs
 	es					; Source is ES segment
 	rep
@@ -350,5 +353,7 @@
 ALIGN JUMP_ALIGN
 WriteBlockTo32bitDataPort:
-	shr		cx, 1		; WORD count to DWORD count
+	db		0C1h		; SHL
+	db		0E1h		; CX
+	db		7			; 7	(Sectors to DWORDs)
 	es					; Source is ES segment
 	rep
