Index: trunk/XTIDE_Universal_BIOS_Configurator_v2/Src/Menupages/IdeControllerMenu.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS_Configurator_v2/Src/Menupages/IdeControllerMenu.asm	(revision 589)
+++ trunk/XTIDE_Universal_BIOS_Configurator_v2/Src/Menupages/IdeControllerMenu.asm	(revision 592)
@@ -135,5 +135,5 @@
 	at	MENUITEM.itemValue + ITEM_VALUE.szDialogTitle,				dw	g_szDlgIdeCmdPort
 	at	MENUITEM.itemValue + ITEM_VALUE.wMinValue,					dw	8h
-	at	MENUITEM.itemValue + ITEM_VALUE.wMaxValue,					dw	3f8h
+	at	MENUITEM.itemValue + ITEM_VALUE.wMaxValue,					dw	3F8h
 	at	MENUITEM.itemValue + ITEM_VALUE.fnValueReader,				dw	IdeControllerMenu_SerialReadPort
 	at	MENUITEM.itemValue + ITEM_VALUE.fnValueWriter,				dw	IdeControllerMenu_SerialWritePort
@@ -306,9 +306,19 @@
 	mov		[cs:g_MenuitemIdeControllerDevice+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset], ax
 
+%ifndef CHECK_FOR_UNUSED_ENTRYPOINTS
+%if IDEVARS.wBasePort = 0
+	mov		[cs:g_MenuitemIdeControllerCommandBlockAddress+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset], bx
+%else
 	lea		ax, [bx+IDEVARS.wBasePort]
 	mov		[cs:g_MenuitemIdeControllerCommandBlockAddress+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset], ax
-
+%endif
+
+%if IDEVARS.bSerialPort = 0
+	mov		[cs:g_MenuitemIdeControllerSerialPort+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset], bx
+%else
 	lea		ax, [bx+IDEVARS.bSerialPort]
 	mov		[cs:g_MenuitemIdeControllerSerialPort+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset], ax
+%endif
+%endif
 
 	lea		ax, [bx+IDEVARS.bSerialBaud]
@@ -361,5 +371,5 @@
 ALIGN JUMP_ALIGN
 .EnableOrDisableCommandBlockPort:
-	mov		bx, [cs:g_MenuitemIdeControllerDevice+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
+	mov		bx, [g_MenuitemIdeControllerDevice+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
 	call	Buffers_GetRomvarsValueToAXfromOffsetInBX
 	mov		bx, g_MenuitemIdeControllerCommandBlockAddress
@@ -380,5 +390,5 @@
 ALIGN JUMP_ALIGN
 .EnableOrDisableControlBlockPort:
-	mov		bx, [cs:g_MenuitemIdeControllerDevice+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
+	mov		bx, [g_MenuitemIdeControllerDevice+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
 	call	Buffers_GetRomvarsValueToAXfromOffsetInBX
 	mov		bx, g_MenuitemIdeControllerControlBlockAddress
@@ -404,5 +414,5 @@
 	jz		SHORT .DisableMenuitemFromCSBX
 
-	mov		bx, [cs:g_MenuitemIdeControllerDevice+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
+	mov		bx, [g_MenuitemIdeControllerDevice+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
 	call	Buffers_GetRomvarsValueToAXfromOffsetInBX
 	mov		bx, g_MenuitemIdeControllerEnableInterrupt
@@ -424,5 +434,5 @@
 ALIGN JUMP_ALIGN
 .EnableOrDisableIRQchannelSelection:
-	mov		bx, [cs:g_MenuitemIdeControllerEnableInterrupt+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
+	mov		bx, [g_MenuitemIdeControllerEnableInterrupt+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
 	call	Buffers_GetRomvarsValueToAXfromOffsetInBX
 	mov		bx, g_MenuitemIdeControllerIdeIRQ
@@ -453,4 +463,13 @@
 
 
+;--------------------------------------------------------------------
+; .EnableOrDisableSerial
+;	Parameters:
+;		SS:BP:	Menu handle
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		AX, BX
+;--------------------------------------------------------------------
 .EnableOrDisableSerial:
 	mov		bx, g_MenuitemIdeControllerSerialBaud
@@ -463,8 +482,8 @@
 	call	DisableMenuitemFromCSBX
 
-	mov		bx, [cs:g_MenuitemIdeControllerDevice+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
+	mov		bx, [g_MenuitemIdeControllerDevice+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
 	call	Buffers_GetRomvarsValueToAXfromOffsetInBX
 	cmp		al, DEVICE_SERIAL_PORT
-	jne		.DisableAllSerial
+	jne		SHORT .DisableAllSerial
 
 	mov		bx, g_MenuitemIdeControllerSerialCOM
@@ -474,5 +493,5 @@
 	call	EnableMenuitemFromCSBX
 
-	mov		bx, [cs:g_MenuitemIdeControllerSerialCOM+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
+	mov		bx, [g_MenuitemIdeControllerSerialCOM+MENUITEM.itemValue+ITEM_VALUE.wRomvarsValueOffset]
 	call	Buffers_GetRomvarsValueToAXfromOffsetInBX
 	mov		bx, g_MenuitemIdeControllerSerialPort
@@ -483,4 +502,5 @@
 	ret
 
+
 ;--------------------------------------------------------------------
 ; MENUITEM activation functions (.fnActivate)
@@ -531,47 +551,21 @@
 	jmp		MasterSlaveMenu_EnterMenuOrModifyItemVisibility
 
-;------------------------------------------------------------------------------------------
-;
-; Reader/Writer Routines
-;
-; For serial drives, we pack the port number and baud rate into a single byte, and thus
-; we need to take care to properly read/write just the bits we need.  In addition, since
-; we use the Port/PortCtrl bytes in a special way for serial drives, we need to properly
-; default the values stored in both these words when switching in and out of the Serial
-; device choice.
-;
-; Writers:
-;	Parameters:
-;		AX:		Value that the MENUITEM system was interacting with
-;		ES:DI:	ROMVARS location where the value is to be stored
+
+;--------------------------------------------------------------------
+; IdeControllerMenu_WriteDevice
+;
+; Sets default values to ports and other device dependent stuff
+;
+;	Parameters:
+;		AX:		IDE controller/Device type menu choice index
+;		ES:DI:	Ptr to IDEVARS.bDevice
 ;		DS:SI:	MENUITEM pointer
 ;	Returns:
-;		AX:		Value to actually write to ROMVARS
-;	Corrupts registers:
-;		AX
-;
-; Readers:
-;	Parameters:
-;		AX:		Value read from the ROMVARS location
-;		ES:DI:	ROMVARS location where the value was just read from
-;		DS:SI:	MENUITEM pointer
-;	Returns:
-;		AX:		Value that the MENUITEM system will interact with and display
-;	Corrupts registers:
-;		AX
-;
-ALIGN JUMP_ALIGN
-WriterForXTCFwindow:
-	xor		al, al
-	SKIP2B	f
-ReaderForXTCFwindow:
-	xor		ah, ah
-	xchg	al, ah
-	ret
-
-
+;		AX:		IDE controller/Device type menu choice index
+;	Corrupts registers:
+;		BX, DX
+;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
 IdeControllerMenu_WriteDevice:
-	push	bx
 	push	di
 	push	ax
@@ -581,7 +575,5 @@
 
 	; Note! AL is the choice index, not device code
-	shl		al, 1								; Selection to device code
-
-	; Restore ports and other stuff to default values
+	eSHL_IM	al, 1								; Selection to device code
 	jz		SHORT .StandardIdeDevice			; DEVICE_16BIT_ATA
 
@@ -603,5 +595,5 @@
 	div		bl
 	mov		bx, .rgbLowByteOfStdIdeInterfacePorts
-	xlat
+	xlat										; DS=CS so no segment override needed
 	mov		ah, 1								; DEVICE_ATA_*_PORT >> 8
 	mov		bh, 3								; DEVICE_ATA_*_PORTCTRL >> 8
@@ -622,8 +614,6 @@
 
 .SupportForDeviceNotAvailable:
-	push	dx
 	mov		dx, g_szUnsupportedDevice
 	call	Dialogs_DisplayErrorFromCSDX
-	pop		dx
 
 	; Restore device type to the previous value
@@ -631,5 +621,6 @@
 	mov		al, bl								; Previous device type to AL
 	shr		al, 1								; Device code to choice index
-	jmp		SHORT .DoneWithNoChangeOfDevice
+	pop		di
+	ret
 
 .NotSerialDevice:
@@ -670,11 +661,15 @@
 
 .ChangingToSerial:
-	cmp		bl, DEVICE_SERIAL_PORT
-	je		SHORT .Done							; if we were already serial, nothing to do
-
+;
+; For serial drives, we pack the port number and baud rate into a single byte, and thus
+; we need to take care to properly read/write just the bits we need.  In addition, since
+; we use the Port/PortCtrl bytes in a special way for serial drives, we need to properly
+; default the values stored in both these words when switching in and out of the Serial
+; device choice.
+;
+	mov		al, SERIAL_DEFAULT_COM
 	mov		BYTE [es:di+IDEVARS.bSerialBaud-IDEVARS.wBasePort], SERIAL_DEFAULT_BAUD
-	mov		BYTE [es:di+IDEVARS.bIRQ-IDEVARS.wBasePort], 0	; Clear .bIRQ to keep the boot menu from printing it
-
-	mov		al, SERIAL_DEFAULT_COM
+	mov		[es:di+IDEVARS.bIRQ-IDEVARS.wBasePort], ah	; Clear .bIRQ to keep the boot menu from printing it
+
 	sub		di, IDEVARS.wBasePort - IDEVARS.bSerialCOMPortChar
 	call	IdeControllerMenu_SerialWriteCOM
@@ -683,53 +678,64 @@
 .Done:
 	pop		ax
-.DoneWithNoChangeOfDevice:
-	pop		di			; IDEVARS.bDevice
-	pop		bx
+	pop		di
 	ret
 
-;
-; Doesn't modify COM character (unless it is not recognized, which would be an error case),
-; But does update the port address based on COM port selection
-;
+
+;--------------------------------------------------------------------
+; IdeControllerMenu_SerialWriteCOM
+;
+; Updates the port address based on COM port selection
+;
+;	Parameters:
+;		AL:		COM port
+;		ES:DI:	Ptr to IDEVARS.bSerialCOMPortChar
+;		DS:SI:	MENUITEM pointer
+;	Returns:
+;		Nothing
+;	Corrupts registers:
+;		BX
+;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
 IdeControllerMenu_SerialWriteCOM:
 	push	ax
-	push	bx
 	push	si
 
-	mov		si, g_rgbChoiceToValueLookupForCOM
-	mov		bx, PackedCOMPortAddresses
-
-.loop:
-	mov		ah, [bx]
-
-	cmp		ah, (SERIAL_DEFAULT_CUSTOM_PORT >> 2)
-	je		.notFound
-
-	cmp		al, [si]
-	je		.found
-
+	mov		bx, PackedCOMPortAddresses - 1
+	mov		si, g_rgbChoiceToValueLookupForCOM - 2
+
+.Loop:
+	inc		bx
 	inc		si
 	inc		si
-	inc		bx
-
-	jmp		.loop
-
-.notFound:
-	mov		al, 'x'
-
-.found:
+
+	mov		ah, [bx]
+	cmp		ah, SERIAL_DEFAULT_CUSTOM_PORT >> 2
+	je		SHORT .NotFound
+
+	cmp		al, [si]
+	jne		SHORT .Loop
+
+.NotFound:
 	mov		[es:di+IDEVARS.bSerialPort-IDEVARS.bSerialCOMPortChar], ah
 
 	pop		si
-	pop		bx
 	pop		ax
-
 	ret
 
 
+;--------------------------------------------------------------------
+; IdeControllerMenu_SerialReadPort
 ;
 ; Packed Port (byte) -> Numeric Port (word)
 ;
+;	Parameters:
+;		AX:		Value read from the ROMVARS location
+;		ES:DI:	ROMVARS location where the value was just read from
+;		DS:SI:	MENUITEM pointer
+;	Returns:
+;		AX:		Value that the MENUITEM system will interact with and display
+;	Corrupts registers:
+;		Nothing
+;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
 IdeControllerMenu_SerialReadPort:
@@ -738,39 +744,48 @@
 	ret
 
+
+;--------------------------------------------------------------------
+; IdeControllerMenu_SerialWritePort
 ;
 ; Numeric Port (word) -> Packed Port (byte)
-; And convert from Custom to a defined COM port if we match one of the pre-defined COM port numbers
-;
+;
+; And convert from Custom to a defined COM port if we
+; match one of the pre-defined COM port numbers
+;
+;	Parameters:
+;		AX:		Value that the MENUITEM system was interacting with
+;		ES:DI:	ROMVARS location where the value is to be stored
+;		DS:SI:	MENUITEM pointer
+;	Returns:
+;		AX:		Value to actually write to ROMVARS
+;	Corrupts registers:
+;		BX
+;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
 IdeControllerMenu_SerialWritePort:
-	push	bx
 	push	si
 
 	eSHR_IM	ax, 2
-	and		al, 0feh			; force 8-byte boundary
-
-	mov		si, g_rgbChoiceToValueLookupForCOM
-	mov		bx, PackedCOMPortAddresses			; loop, looking for port address in known COM address list
-
-.loop:
-	mov		ah, [si]
-	cmp		ah, 'x'
-	je		.found
-
-	cmp		al, [bx]
-	je		.found
-
+	and		al, 0FEh							; Force 8-byte boundary
+
+	mov		si, g_rgbChoiceToValueLookupForCOM - 2
+	mov		bx, PackedCOMPortAddresses - 1		; Loop, looking for port address in known COM address list
+
+.Loop:
 	inc		si
 	inc		si
 	inc		bx
 
-	jmp		.loop
-
-.found:
+	mov		ah, [si]
+	cmp		ah, 'x'
+	je		SHORT .Found
+
+	cmp		al, [bx]
+	jne		SHORT .Loop
+
+.Found:
 	mov		[es:di+IDEVARS.bSerialCOMPortChar-IDEVARS.bSerialPort], ah
 
 	pop		si
-	pop		bx
-
 	ret
 
