I am following this OSDev Wiki tutorial about PS/2
Mouse drivers and I'm trying to write mine so I can add it to my C kernel (in protected mode).
First, I sent the command byte 0x60
to the port 0x64
followed by this status byte to the port 0x60
:
0 1 1 1 0 0 1 1
| | | | | | | |
| | | | | | | |
UN TRAN ME KE IGKL SF MIE KIE
Where:
UN
: Unused (always0
)TRAN
: Translate keyboard scancodesME
: Mouse enableKE
: Keyboard enableIGKL
: Ignore keyboard lock (Unused for PS/2, so it's0
)SF
: System flagMIE
: Mouse interrupt enable (send IRQ12 when buffer is full)KIE
: Keyboard interrupt enable (send IRQ1 when buffer is full)
With that, I guess I enabled the mouse and the keyboard. Right? r.r..rr..right..?
By simply checking the first bit of the status register, you can get the status of the output buffer. If it's set (output buffer is full) you check the 5th bit. If it's set, the output is coming from the mouse. If not, it's coming from the keyboard.
Here is my code: (I can add it to the kernel using inline assembly):
mov al, 0x60 ;= send command byte 0x60... =;
out 0x64, al ;= to port 0x64 =;
mov al, 01110011b ;= and send the status byte... =;
out 0x60, al ;= to port 0x60 =;
mov al, 0xA8 ;= double-check! :P =;
out 0x64, al ;= double-checking is great! i...ii..isn't it..? Or...? (SUS) =;
jmp lp ;= do the loop again =;
lp:
in al, 0x64 ;= read the status register =;
bt ax, 0 ;= check the first bit (is the output buffer full?) =;
jnc lp ;= if not, then repeat. =;
bt ax, 5 ;= check the 5th bit (is the output coming from the mouse?) =;
jc draw ;= then.. uhh.. Idk.. maybe draw a pixel ;-)
jmp lp ;= If it's coming from the keyboard, repeat. =;
I expected receiving output from the mouse because it will send packets to communicate mouse movement but nothing happened. However, this works fine (with the keyboard):
bt ax, 5 ;= check the 5th bit (is the output coming from the keyboard?) =;
jnc draw
jmp lp ;= If it's coming from the mouse, repeat. =;
(Tested on QEMU, running on Ubuntu. So, maybe QEMU doesn't support PS/2?).
NOTE: I've seen some questions like this one on SO, but none of them answered mine.