gpt4 book ai didi

无法接收来 self 的 PS/2 鼠标驱动程序的输出

转载 作者:行者123 更新时间:2023-12-04 14:55:18 26 4
gpt4 key购买 nike

我正在关注 this关于 PS/2 鼠标驱动程序的 OSDev Wiki 教程,我正在尝试编写自己的驱动程序以便将其添加到我的 C 内核(在保护模式下)。
首先,我将命令字节 0x60 发送到端口 0x64,然后将此状态字节发送到端口 0x60:

                       0    1    1    1    0    0    1    1
| | | | | | | |
| | | | | | | |
UN TRAN ME KE IGKL SF MIE KIE

地点:

  • UN:未使用(总是0)
  • TRAN : 翻译键盘扫描码
  • ME : 鼠标启用
  • KE : 键盘启用
  • IGKL:忽略键盘锁(PS/2 未使用,因此为 0)
  • SF : 系统标志
  • MIE : 鼠标中断使能(缓冲区满时发送 IRQ12)
  • KIE : 键盘中断使能(缓冲区满时发送IRQ1)

这样,我想我启用了鼠标和键盘。正确的? r.r..rr..对吧..?
通过简单地检查状态寄存器的第一位,就可以得到输出缓冲区的状态。如果已设置(输出缓冲区已满),则检查第 5 位。如果已设置,则输出来自鼠标。如果不是,它来自键盘。

这是我的代码:(我可以使用内联汇编将它添加到内核):

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. =;

我希望收到鼠标的输出,因为它会发送数据包来传达鼠标移动,但什么也没发生。然而,这工作正常(使用键盘):

   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. =;

(在 QEMU 上测试,在 Ubuntu 上运行。所以,也许 QEMU 不支持 PS/2?)。

注意:我在 SO 上看到过一些像这样的问题,但没有一个能回答我的问题。

最佳答案

您没有遵循 the linked article 中给出的建议.

Waiting to Send Bytes to Port 0x60 and 0x64

All output to port 0x60 or 0x64 must be preceded by waiting for bit 1 of port 0x64 to become clear. Similarly, bytes cannot be read from port 0x60 until bit 0 of port 0x64 is set.

0xD4 Byte, Command Byte, Data Byte

Sending a command or data byte to the mouse (to port 0x60) must be preceded by sending a 0xD4 byte to port 0x64 (with appropriate waits on port 0x64, bit 1, before sending each output byte). Note: this 0xD4 byte does not generate any ACK, from either the keyboard or mouse.

Wait for ACK from Mouse

It is required to wait until the mouse sends back the 0xFA acknowledgement byte after each command or data byte before sending the next byte. A few commands require an additional data byte, and both bytes will generate an ACK.

; The loop avoids waiting forever and allows adding error processing. Remove at will.
WaitForOutput:
xor cx, cx
.again:
in al, 0x64
test al, 2
loopnz .again
ret

WaitForInput:
xor cx, cx
.again:
in al, 0x64
test al, 1
loopz .again
ret

WaitForACK:
call WaitForInput
in al, 0x60 ; AL = 0xFA
ret

Initializing a PS2 Mouse

You need to send the command byte 0x20 to the PS2 controller on port 0x64. This command does not generate a 0xFA ACK byte. The very next byte returned should be the Status byte. After you get the Status byte, you need to set bit number 1 (value=2, Enable IRQ12), and clear bit number 5 (value=0x20, Disable Mouse Clock). Then send command byte 0x60 to port 0x64, followed by the modified Status byte to port 0x60. This might generate a 0xFA ACK byte from the keyboard.
Send the Enable Auxiliary Device command (0xA8) to port 0x64. This will generate an ACK response from the keyboard, which you must wait to receive.

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)

上面启用鼠标的代码过于简单,您发送到 0x60 的值应该按照本文清除第 5 位。 (您不是在“重新编程”鼠标,因此您不需要通过设置状态字节的第 5 位来禁用“主鼠标时钟”)。此外,很多时候,在处理这些位 vector 时,使用硬编码值并不是一个好主意,例如 mov al, 01110011b。始终只修改您感兴趣的位。

  call    WaitForOutput
mov al, 0x20 ; PS2.GetStatusByte (no ACK)
out 0x64, al

call WaitForInput
in al, 0x60
or al, 00000010b ; Set bit 1 (Enable IRQ12)
and al, 11011111b ; Clear bit 5 (Disable Mouse Clock)
push ax ; (1)

call WaitForOutput
mov al, 0x60 ; PS2.SetStatusByte (ACK)
out 0x64, al
call WaitForACK

call WaitForOutput
mov al, 0xD4 ; PS2.WriteToMouseInsteadOfKeyboard (no ACK)
out 0x64, al

call WaitForOutput
pop ax ; (1)
out 0x60, al ; SendToMouse (ACK)
call WaitForACK

call WaitForOutput
mov al, 0xA8 ; PS2.EnableMousePort (ACK)
out 0x64, al
call WaitForACK

Again:
in al, 0x64
and al, 00100001b
cmp al, 00100001b
jne Again
call WaitForInput
in al, 0x60
; draw ...

关于无法接收来 self 的 PS/2 鼠标驱动程序的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68167549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com