Page 1 of 1
BOCHS Mouse and Keyboard interrupt problem...
Posted: Thu Apr 29, 2004 9:32 am
by Cemre
My Keyboard Handler and mouse interrupt handler reads both from port 0x60. keyboard handler reads 1 byte ( or 2 if extended code ) keycode from port 0x60. the mouse handler reads 3 bytes standart mouse packet from AGAIN port 0x60.
in bochs, if i move the mouse and at the same time if i press a key, the data read from port 0x60 gets corrupted. i tried this on real hardware and it corrupted the data too. the mouse unexpectedly moves to the edges of the screen and triggers unexpected button events.
i think this error is not related with bochs. how can i resolve it? have you ever encountered the same problem?
Re:BOCHS Mouse and Keybpard interrupt problem...
Posted: Thu Apr 29, 2004 9:47 am
by Pype.Clicker
you should not try to read 3 bytes in the mouse interrupt handler but "read bytes as long as the controller reports bytes to be present for reading" ...
for your title problem: just edit your initial post
Re:BOCHS Mouse and Keyboard interrupt problem...
Posted: Thu Apr 29, 2004 10:17 am
by Cemre
you should not try to read 3 bytes in the mouse interrupt handler but "read bytes as long as the controller reports bytes to be present for reading" ...
I am waiting for data_ready...
---------
call KB_DataWait
in al , 0x60
mov bl , al
call KB_DataWait
in al , 0x60
mov bh , al
call KB_DataWait
in al , 0x60
...etc
KB_Data_Wait:
in al, 64h ; read status bit "0"
test al, 00000001b
jz KB_Data_Wait
ret
Re:BOCHS Mouse and Keyboard interrupt problem...
Posted: Thu Apr 29, 2004 10:54 am
by DennisCGc
I thought a ps/2 mouse uses irq12 to indicate when the mouse moves, so if this's true, you have to read it when irq12 is called, the mouse bytes.
For the keyboard, just wait for IRQ1.
Re:BOCHS Mouse and Keyboard interrupt problem...
Posted: Thu Apr 29, 2004 11:30 am
by Pype.Clicker
okay. The mouse sends 3 bytes to the controller at each single move. the controller receives them one by one through a quite slow serial line and as soon as it receives the *first* of them completely, it reports it through a IRQ12. The second one may take time to arrive, and the controller will issue a second IRQ12 at that time ...
So imho, the proper code would be
Code: Select all
mouse_handler()
static i=0;
static mouse_byte[3]
while (inb(0x64)&1) {
mouse_byte[i]=inb(0x60);
i++;
if (i==3) {
report_mouse_event(&mouse_byte);
i=0;
}
}
i saw nothing nowhere that forces the 8042 to defer delivery of keyboard bytes because a 'mouse packet' has started. Since the keyboard is prioritary over the mouse (considering the PIC point of view), it wouldn't even be interresting to do it so
Re:BOCHS Mouse and Keyboard interrupt problem...
Posted: Thu Apr 29, 2004 1:58 pm
by Cemre
Thank you... I understand... I must program something like a state machine... I did it as the following
invoke KB_DataWait
InPortB 0x60
mov ebx , dword [ MouseState ]
mov byte [ MouseStateTempData + ebx ] , al
.if dword [ MouseState ] , eq , 0
mov dword [ MouseState ] , 2
and dword [ MouseStateTempData ] , 0xFFFFFF
jmp DoMouseNow
.endif
dec dword [ MouseState ]
jmp MouseDataNotComplete
....
MouseState dd 2 ; initial state
MouseStateTempData dd 0
the problem was that i tried to read the whole packet with just one interrupt, in fact i had to read one byte per interrupt and wait until all three bytes are complete...
thank you again... your advise has worked... now it doesn't corrupt the data
Re:BOCHS Mouse and Keyboard interrupt problem...
Posted: Thu Apr 29, 2004 2:02 pm
by Cemre
one more thing...
in my keyboard handler, if i detect an extended keycode signature ( "0xE0" ), i read the second keycode immediately...
do you think i should also do something like i did in the mouse and wait for a second irq1 and read the remaining byte then?
irq1 has higher priority on irq12, so is it needed?
current code is this:
IRQ_1_KB_Data_Wait: ; Wait for data to be ready
in AL, IRQ_1_KB_Status_Port
bt eax, 0 ; is KB ready for a data?,
jnc IRQ_1_KB_Data_Wait ; wait until is.
xor eax,eax
in al, IRQ_1_KB_Data_Port
mov [ LastKeyboardScanCode ] , eax
.if al , eq , 0xE0 ; Extended key?
IRQ_1_KB_Data_Wait_EX: ; Wait for data to be ready
in AL, IRQ_1_KB_Status_Port
bt eax, 0 ; is KB ready for a data?,
jnc IRQ_1_KB_Data_Wait_EX ; wait until is.
in al, IRQ_1_KB_Data_Port
shl dword [ LastKeyboardScanCode ] , 8
mov byte [ LastKeyboardScanCode ] , al
.endif
Re:BOCHS Mouse and Keyboard interrupt problem...
Posted: Fri Apr 30, 2004 1:55 am
by Pype.Clicker
Imho, yes: you should read only one byte at a time in kbhandler it's less problematic concerning validity of data received, but it'll still make you act like a CPU hog between the two bytes while you could let some user process do useful job until the next interrupt arise ...