Code: Select all
mov dx, 0060h
in al, dx
I'm a major n00b, so help would be appreciated greatly!
Code: Select all
mov dx, 0060h
in al, dx
That would read the latest byte from the keyboard; but you won't know if it's old data that you already read or new data.kendfrey wrote:How do I read key presses from the keyboard without using BIOS? I suspect the first part would beCode: Select all
mov dx, 0060h in al, dx
It is even messier as the keyboard in the USB-world is called a "HID" (a Human Interface Device), which have a specification compatible with a interpreter. Luckily, there is still legacy keyboard support for USB keyboards, but it is largely depreciated.Brendan wrote: After that, the next step would be realising that most computers use USB keyboards, and that the "legacy PS2 emulation" done by the BIOS isn't very reliable. This means USB support, which is a whole complicated mess (starting from scanning PCI buses to detect any USB controllers, providing drivers for each type of USB controller, then writing a driver for USB keyboards).
Code: Select all
getscancodefromps2keyboard:
push ecx
;waiting for data with timeout
xor ecx, ecx
mov cx, 1000
xor eax, eax
.waitkey: in al, 64h
dec cl
jnz @f
xor eax, eax
stc
jmp .nothing
@@: and al, 01h
jz .waitkey
;read the scancode
in al, 60h
mov cl, al
;acknowledge
in al, 61h
out 61h, al
mov al, cl
clc
.nothing: pop ecx
;scancode in al, or zero (and carry set) on communication error
Code: Select all
in al, 61h
out 61h, al
I'm not too sure, but that (incorrect) code may have been derived from code to handle an ancient/obsolete XT keyboard; where you had to disable then reenable the keyboard on every scancode. The correct code for this "disable then re-enable" should look like this:egos wrote:Wow! It's extraneous code for keyboard controller. A first reading tells that data has received.Code: Select all
in al, 61h out 61h, al
Code: Select all
in al,0x61 ;Save keyboard status
mov ah,al
or al,0x80
out 0x61,al ;Disable keyboard
mov al,ah
out 0x61,al ;Restore/enable original state
You're right, it's an obsolete piece of code that's not necessary, but it does not hurt, so I was too lazy to investigate if it can be removed or not... The first machine I was tested my first os (i dunno, ca. 10 years ago a 386DX) it definitely required to refresh the value on port 61h, else no more interrupt fired. Could be a firmware problem, I'm not sure. Anyway thanks, I removed it.Brendan wrote: This is needed for XT keyboards (for both polling and IRQ driven code), but XT keyboards are extremely rare and any computer that supports protected mode won't support XT keyboards (they were obsolete well before 80386 was introduced).
For AT keyboards it's unnecessary (and code that does it wrong might seem like it works because it wasn't necessary to begin with ).