	in al, 0x61		; get the port 0x61 "kbd status byte"
	and al, 0x7f		; force the "disable" bit clear
	mov dh, al		; Save keyboard status byte

ADDY20:

	mov cx, 2		; may need to test twice on standard method
.cwait1:			; wait for kbd controller ready to accept command
	in al, 0x64
	test al, 2
	jne short .cwait1

	mov al, 0xD0		; request controller "status"
	out 0x64, al

.dwait1:			; wait for data to be available (on port 0x60)
	in AL, 0x64		; bit #0 (value=1) of port 0x64 is the flag
	test al, 1
	je short .dwait1

	in al, 0x60		; ding! Data is ready. Read it from port 0x60.
	mov ah, al		; and store it for later use

	mov al, dh		; ack receipt of the byte to the controller (probably not needed)
	or al, 0x80		; Disable
	out 0x61, al
	mov al, dh		; Enable
	out 0x61, al

	test ah, 2		; check the A20 bit
	jne near .done		; If bit is on, A20 is already on.

.cwait2:			; wait for controller "ready" again
	in al, 0x64
	test al, 2
	jne short .cwait2

	mov al, 0xD1		; command to write a new status byte (with A20 bit!)
	out 0x64, al

.dwait2:			; wait until controller can accept the byte
	in al, 0x64
	test al, 2		; if bit #1 (value=2) not clear, cannot write to either port
	jne short .dwait2

	mov al, ah		; reload the saved current status byte
	or al, 2		; flip on the A20 enable bit
	out 0x60, AL		; and ram it back into the controller

	loop .cwait1		; Go back and check if it worked.


	mov cx, 2		; Standard method didn't work. Try "Compaq status" method.

.cwait3:			; wait for controller "ready" again
	in al, 0x64
	test al, 2
	jne short .cwait3
	mov al, 0x20		; request "Compaq status byte"
	out 0x64, al

.dwait3:			; wait for data to be available (on port 0x60)
	in AL, 0x64		; bit #0 (value=1) of port 0x64 is the flag
	test al, 1
	je short .dwait3

	in al, 0x60		; W00t! Data!
	mov ah, al		; store for later use

	mov al, dh		; ack receipt of the byte to the controller (probably not needed)
	or al, 0x80		; Disable
	out 0x61, al
	mov al, dh		; Enable
	out 0x61, al

	test ah, 1		; check the A20 bit (bit #0 in Compaq status byte)
	jne short .done		; If bit is on, A20 is already on.

.cwait4:			; wait for controller "ready" again
	in al, 0x64
	test al, 2
	jne short .cwait4

	mov al, 0x60		; command to write a new Compaq status byte
	out 0x64, al

.dwait4:			; wait until controller can accept the byte
	in al, 0x64
	test al, 2		; if bit #1 (value=2) not clear, cannot write to either port
	jne short .dwait4

	mov al, ah		; reload the saved current status byte
	or al, 1		; flip on the A20 enable bit
	out 0x60, AL		; and ram it back into the controller

	loop .cwait3		; Go back and check if it worked.

		; Compaq status method didn't work. Try the HP Vectra method.
.cwait5:			; wait for controller "ready" again
	in al, 0x64
	test al, 2
	jne short .cwait5

	mov al, 0xDF		; send "enable A20" command
	out 0x64, al

; verify that HP Vectra method worked

.cwait6:			; wait for controller "ready" again
	in al, 0x64
	test al, 2
	jne short .cwait6

	mov AL, 0xD0		; request status
	out 0x64, AL

.dwait5:
	in al, 0x64		; wait for the data
	test al, 1
	je short .dwait5

	in al, 0x60		; Yay! More data
	mov ah, al

	mov al, dh		; ack receipt of the byte to the controller
	or al, 0x80		;Disable
	out 0x61, al
	mov al, dh		;Enable
	out 0x61, al

	test ah, 2		; check the "A20 enabled" bit
	jne short .done

	jmp near ADDY20		; start over -- no other choice, except a halt
.done:
	; Continue!
