User:Superleaf1995/PCI in assembly

From OSDev Wiki
Jump to navigation Jump to search
; AL = BUS
; BL = SLOT
; CL = FUNC
; DL = OFFSET
; Returns AX as TMP
pciConfigReadWord:
	mov byte [.off], dl
	xor edx, edx
	
	mov byte [.tmp], al
	xor eax, eax ; Only leave AL (assume we are using 386+)
	mov al, byte [.tmp]
	mov dword [.lbus], eax
	
	mov byte [.tmp], bl
	xor ebx, ebx
	mov bl, byte [.tmp]
	mov dword [.lslot], ebx
	
	mov byte [.tmp], cl
	xor ecx, ecx
	mov cl, byte [.tmp]
	mov dword [.lfunc], ecx
	
	; Everything set, now calculate address
	xor eax, eax
	xor ebx, ebx
	xor ecx, ecx
	
	mov ecx, [.lfunc]
	mov ebx, [.lslot]
	mov eax, [.lbus]
	shl eax, 16
	shl ebx, 11
	shl ecx, 8
	mov dl, byte [.off]
	and dl, 0FCh
	
	or eax, ebx
	or eax, ecx
	or eax, edx
	mov ebx, 080000000h
	or eax, ebx
	mov dword [.addr], eax
	
	mov eax, dword [.addr]
	out 0CF8h, eax
	
	in eax, 0CFCh
	mov cl, [.off]
	and cl, 2
	shr eax, cl
	
	mov ebx, 8
	mul ebx
	
	and ax, 0FFFFh
	
	ret

.off	db 0
.addr	dd 0
.lbus	dd 0
.lslot	dd 0
.lfunc	dd 0
.tmp	dw 0

WARNING: Code untested, Ctr+C and Ctr+V at your own risk!