Index: trunk/XTIDE_Universal_BIOS/Src/Device/MemoryMappedIDE/MemIdeTransfer.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Device/MemoryMappedIDE/MemIdeTransfer.asm	(revision 238)
+++ trunk/XTIDE_Universal_BIOS/Src/Device/MemoryMappedIDE/MemIdeTransfer.asm	(revision 242)
@@ -8,5 +8,6 @@
 	.wWordsLeft				resb	2	; 2, WORDs left to transfer
 	.wWordsDone				resb	2	; 4, Number of sectors xferred
-							resb	1	; 6, 
+	; TODO: The above word vars could just as well be byte vars?
+							resb	1	; 6,
 							resb	1	; 7, IDEPACK.bDeviceControl
 	.fpDPT					resb	4	; 8, Far pointer to DPT
@@ -34,44 +35,28 @@
 ALIGN JUMP_ALIGN
 MemIdeTransfer_StartWithCommandInAL:
+	push	cs	; We push CS here (segment of SAW) and later pop it to DS (reads) or ES (writes)
+
 	; Initialize MEMPIOVARS
-	xchg	cx, ax								; IDE command to CL
-	xor		al, al
-	mov		ah, [bp+IDEPACK.bSectorCount]
-	mov		[bp+MEMPIOVARS.wWordsLeft], ax
-	cbw
-	mov		[bp+MEMPIOVARS.wWordsDone], ax		; Zero
-	mov		ah, [di+DPT_ATA.bSetBlock]
-	mov		[bp+MEMPIOVARS.wWordsInBlock], ax
+	xor		cx, cx
+	mov		[bp+MEMPIOVARS.wWordsDone], cx
+	mov		ch, [bp+IDEPACK.bSectorCount]
+	mov		[bp+MEMPIOVARS.wWordsLeft], cx
+	mov		ch, [di+DPT_ATA.bSetBlock]
+	mov		[bp+MEMPIOVARS.wWordsInBlock], cx
 	mov		[bp+MEMPIOVARS.fpDPT], di
 	mov		[bp+MEMPIOVARS.fpDPT+2], ds
 
 	; Are we reading or writing?
-	test	cl, 16	; Bit 4 is cleared on all the read commands but set on 3 of the 4 write commands
-	jnz		SHORT .PrepareToWriteDataFromESSI
-	cmp		cl, COMMAND_WRITE_MULTIPLE
-	je		SHORT .PrepareToWriteDataFromESSI
-
-	; Prepare to read data to ES:DI
-	mov		di, si
-	push	cs
-	pop		ds
-	mov		si, JRIDE_SECTOR_ACCESS_WINDOW_OFFSET
-	jmp		SHORT ReadFromSectorAccessWindowInDSSItoESDI
-
-ALIGN JUMP_ALIGN
-.PrepareToWriteDataFromESSI:
-	push	es
-	pop		ds
-	push	cs
-	pop		es
-	mov		di, JRIDE_SECTOR_ACCESS_WINDOW_OFFSET
-	; Fall to WriteToSectorAccessWindowInESDIfromDSSI
-
-
-;--------------------------------------------------------------------
-; WriteToSectorAccessWindowInESDIfromDSSI
-;	Parameters:
-;		DS:SI:	Normalized ptr to buffer containing data
-;		ES:DI:	Ptr to Sector Access Window
+	test	al, 16	; Bit 4 is cleared on all the read commands but set on 3 of the 4 write commands
+	jnz		SHORT WriteToSectorAccessWindow
+	cmp		al, COMMAND_WRITE_MULTIPLE
+	je		SHORT WriteToSectorAccessWindow
+	; Fall to ReadFromSectorAccessWindow
+
+;--------------------------------------------------------------------
+; ReadFromSectorAccessWindow
+;	Parameters:
+;		Stack:	Segment part of ptr to Sector Access Window
+;		ES:SI:	Normalized ptr to buffer to receive data
 ;		SS:BP:	Ptr to MEMPIOVARS
 ;	Returns:
@@ -84,85 +69,18 @@
 ;		AL, BX, DX, SI, ES
 ;--------------------------------------------------------------------
-WriteToSectorAccessWindowInESDIfromDSSI:
-	; Always poll when writing first block (IRQs are generated for following blocks)
-	call	WaitUntilReadyToTransferNextBlock
-	jc		SHORT ReturnWithMemoryIOtransferErrorInAH
-
-ALIGN JUMP_ALIGN
-.WriteNextBlockToDrive:
-	mov		cx, [bp+PIOVARS.wWordsInBlock]
-	cmp		[bp+PIOVARS.wWordsLeft], cx
-	jbe		SHORT .WriteLastBlockToDrive
-	eMOVZX	dx, ch								; DX = Sectors in block
-	call	WriteSingleBlockFromDSSIToSectorAccessWindowInESDI
-	call	WaitUntilReadyToTransferNextBlock
-	jc		SHORT ReturnWithMemoryIOtransferErrorInAH
-
-	; Increment number of successfully written WORDs
-	mov		ax, [bp+PIOVARS.wWordsInBlock]
-	sub		[bp+PIOVARS.wWordsLeft], ax
-	add		[bp+PIOVARS.wWordsDone], ax
-	jmp		SHORT .WriteNextBlockToDrive
-
-ALIGN JUMP_ALIGN
-.WriteLastBlockToDrive:
-	eMOVZX	dx, BYTE [bp+PIOVARS.wWordsLeft+1]	; Sectors left
-%ifdef USE_186
-	push	CheckErrorsAfterTransferringLastMemoryMappedBlock
-	jmp		WriteSingleBlockFromDSSIToSectorAccessWindowInESDI
-%else
-	call	WriteSingleBlockFromDSSIToSectorAccessWindowInESDI
-	jmp		SHORT CheckErrorsAfterTransferringLastMemoryMappedBlock
-%endif
-
-
-;--------------------------------------------------------------------
-; WriteSingleBlockFromDSSIToSectorAccessWindowInESDI
-;	Parameters:
-;		DX:		Number of sectors in block
-;		DS:SI:	Normalized ptr to source buffer
-;		ES:DI:	Ptr to Sector Access Window
-;	Returns:
-;		CX, DX:	Zero
-;		SI:		Updated
-;	Corrupts registers:
-;		Nothing
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-WriteSingleBlockFromDSSIToSectorAccessWindowInESDI:
-	mov		cx, JRIDE_SECTOR_ACCESS_WINDOW_SIZE / 2
-	rep movsw
-	sub		di, JRIDE_SECTOR_ACCESS_WINDOW_SIZE	; Reset for next sector
-	dec		dx
-	jnz		SHORT WriteSingleBlockFromDSSIToSectorAccessWindowInESDI
-	ret
-
-
-;--------------------------------------------------------------------
-; ReadFromSectorAccessWindowInDSSItoESDI
-;	Parameters:
-;		ES:DI:	Normalized ptr to buffer to recieve data
-;		DS:SI:	Ptr to Sector Access Window
-;		SS:BP:	Ptr to MEMPIOVARS
-;	Returns:
-;		DS:DI:	Ptr to DPT (in RAMVARS segment)
-;		AH:		BIOS Error code
-;		CX:		Number of successfully transferred sectors
-;		CF:		0 if transfer succesfull
-;				1 if any error
-;	Corrupts registers:
-;		AL, BX, DX, SI, ES
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-ReadFromSectorAccessWindowInDSSItoESDI:
-	call	WaitUntilReadyToTransferNextBlock
-	jc		SHORT ReturnWithMemoryIOtransferErrorInAH
+ReadFromSectorAccessWindow:
+	pop		ds	; CS -> DS
+	mov		di, si
+	mov		si, JRIDE_SECTOR_ACCESS_WINDOW_OFFSET
+
+	call	WaitUntilReadyToTransferNextBlock
+	jc		SHORT ReturnWithMemoryIOtransferErrorInAH
+
+	mov		cx, [bp+PIOVARS.wWordsInBlock]
 
 ALIGN JUMP_ALIGN
 .ReadNextBlockFromDrive:
-	mov		cx, [bp+PIOVARS.wWordsInBlock]
 	cmp		[bp+PIOVARS.wWordsLeft], cx
 	jbe		SHORT .ReadLastBlockFromDrive
-	eMOVZX	dx, ch								; DX = Sectors in block
 	call	ReadSingleBlockFromSectorAccessWindowInDSSItoESDI
 	call	WaitUntilReadyToTransferNextBlock
@@ -170,12 +88,12 @@
 
 	; Increment number of successfully read WORDs
-	mov		ax, [bp+PIOVARS.wWordsInBlock]
-	sub		[bp+PIOVARS.wWordsLeft], ax
-	add		[bp+PIOVARS.wWordsDone], ax
+	mov		cx, [bp+PIOVARS.wWordsInBlock]
+	sub		[bp+PIOVARS.wWordsLeft], cx
+	add		[bp+PIOVARS.wWordsDone], cx
 	jmp		SHORT .ReadNextBlockFromDrive
 
 ALIGN JUMP_ALIGN
 .ReadLastBlockFromDrive:
-	eMOVZX	dx, BYTE [bp+PIOVARS.wWordsLeft+1]	; Sectors left
+	mov		ch, [bp+PIOVARS.wWordsLeft+1]		; Sectors left
 	call	ReadSingleBlockFromSectorAccessWindowInDSSItoESDI
 
@@ -186,5 +104,5 @@
 	call	IDEDEVICE%+Wait_PollStatusFlagInBLwithTimeoutInBH
 
-	; Return number of successfully read sectors
+	; Return number of successfully transferred sectors
 ReturnWithMemoryIOtransferErrorInAH:
 	lds		di, [bp+MEMPIOVARS.fpDPT]			; DPT now in DS:DI
@@ -198,8 +116,88 @@
 
 ;--------------------------------------------------------------------
+; WriteToSectorAccessWindow
+;	Parameters:
+;		Stack:	Segment part of ptr to Sector Access Window
+;		ES:SI:	Normalized ptr to buffer containing data
+;		SS:BP:	Ptr to MEMPIOVARS
+;	Returns:
+;		DS:DI:	Ptr to DPT (in RAMVARS segment)
+;		AH:		BIOS Error code
+;		CX:		Number of successfully transferred sectors
+;		CF:		0 if transfer succesfull
+;				1 if any error
+;	Corrupts registers:
+;		AL, BX, DX, SI, ES
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+WriteToSectorAccessWindow:
+	push	es
+	pop		ds
+	pop		es	; CS -> ES
+	mov		di, JRIDE_SECTOR_ACCESS_WINDOW_OFFSET
+
+	; Always poll when writing first block (IRQs are generated for following blocks)
+	call	WaitUntilReadyToTransferNextBlock
+	jc		SHORT ReturnWithMemoryIOtransferErrorInAH
+
+	mov		cx, [bp+PIOVARS.wWordsInBlock]
+
+ALIGN JUMP_ALIGN
+.WriteNextBlockToDrive:
+	cmp		[bp+PIOVARS.wWordsLeft], cx
+	jbe		SHORT .WriteLastBlockToDrive
+	call	WriteSingleBlockFromDSSIToSectorAccessWindowInESDI
+	call	WaitUntilReadyToTransferNextBlock
+	jc		SHORT ReturnWithMemoryIOtransferErrorInAH
+
+	; Increment number of successfully written WORDs
+	mov		cx, [bp+PIOVARS.wWordsInBlock]
+	sub		[bp+PIOVARS.wWordsLeft], cx
+	add		[bp+PIOVARS.wWordsDone], cx
+	jmp		SHORT .WriteNextBlockToDrive
+
+ALIGN JUMP_ALIGN
+.WriteLastBlockToDrive:
+	mov		ch, [bp+PIOVARS.wWordsLeft+1]		; Sectors left
+%ifndef USE_186
+	mov		bx, CheckErrorsAfterTransferringLastMemoryMappedBlock
+	push	bx
+%else
+	push	CheckErrorsAfterTransferringLastMemoryMappedBlock
+%endif
+	; Fall to WriteSingleBlockFromDSSIToSectorAccessWindowInESDI
+
+;--------------------------------------------------------------------
+; WriteSingleBlockFromDSSIToSectorAccessWindowInESDI
+;	Parameters:
+;		CH:		Number of sectors in block
+;		DS:SI:	Normalized ptr to source buffer
+;		ES:DI:	Ptr to Sector Access Window
+;	Returns:
+;		CX, DX:	Zero
+;		SI:		Updated
+;	Corrupts registers:
+;		BX
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+WriteSingleBlockFromDSSIToSectorAccessWindowInESDI:
+	mov		bx, di
+	eMOVZX	dx, ch
+	xor		cl, cl
+ALIGN JUMP_ALIGN
+.WriteBlock:
+	mov		ch, JRIDE_SECTOR_ACCESS_WINDOW_SIZE >> 9
+	rep movsw
+	mov		di, bx	; Reset for next sector
+	dec		dx
+	jnz		SHORT .WriteBlock
+	ret
+
+
+;--------------------------------------------------------------------
 ; ReadSingleBlockFromSectorAccessWindowInDSSItoESDI
 ;	Parameters:
-;		DX:		Number of sectors in block
-;		ES:DI:	Normalized ptr to buffer to recieve data (destination)
+;		CH:		Number of sectors in block
+;		ES:DI:	Normalized ptr to buffer to receive data (destination)
 ;		DS:SI:	Ptr to Sector Access Window (source)
 ;	Returns:
@@ -207,13 +205,18 @@
 ;		DI:		Updated
 ;	Corrupts registers:
-;		Nothing
+;		BX
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
 ReadSingleBlockFromSectorAccessWindowInDSSItoESDI:
-	mov		cx, JRIDE_SECTOR_ACCESS_WINDOW_SIZE / 2
+	mov		bx, si
+	eMOVZX	dx, ch
+	xor		cl, cl
+ALIGN JUMP_ALIGN
+.ReadBlock:
+	mov		ch, JRIDE_SECTOR_ACCESS_WINDOW_SIZE >> 9
 	rep movsw
-	sub		si, JRIDE_SECTOR_ACCESS_WINDOW_SIZE	; Reset for next sector
+	mov		si, bx	; Reset for next sector
 	dec		dx
-	jnz		SHORT ReadSingleBlockFromSectorAccessWindowInDSSItoESDI
+	jnz		SHORT .ReadBlock
 	ret
 
@@ -238,2 +241,8 @@
 	pop		ds
 	ret
+
+
+%if JRIDE_SECTOR_ACCESS_WINDOW_SIZE <> 512
+	%error "JRIDE_SECTOR_ACCESS_WINDOW_SIZE is no longer equal to 512. MemIdeTransfer.asm needs changes."
+%endif
+
