13

I'm writing a hobby OS and the first thing I want to do is access PS/2 devices (it's a somewhat legacy, 32 bit OS, so I figured it's relevant to ask here?).

I can see my PS/2 devices and I want to read data from them. If a device is sending several bytes, what happens when I read one? I understand that there is only one place in memory, 0x60, where these bytes show up. So is the following true:

If I read a byte from 0x60 and the device has more to send, then the next byte is stored in 0x60 just because I read there? If that isn't true, how does the device know when to send more data, and how do I know it has sent the next byte so I can read it?

19

On standard PCs, the main CPU and PS/2 controller use a handshake mechanism in the status register at port 0x64:

Main CPU wants to read (probably because it received a keyboard interrupt):

  1. Read port 0x64 until bit 0 is set (peripheral has something)
  2. Read port 0x60 (contains the data byte from PS/2 port). This read also re-sets bit 0 in the status register to tell the PS/2 controller it can send the next byte (if any)

If you want to write something to the PS/2 controller, you use the same port 0x64, but wait for bit 1 to be clear, which tells you the controller is ready to read a byte from the data port at 0x60 or command port at 0x64

15

There is not only one place in memory, and it is not even memory.

The port 0x60 is an IO port in the CPU IO address space for accessing the keyboard controller data port. It is used to access a lot of stuff but only one byte at a time.

And the bytes that keyboard sends are not by default directly available for reception in an identical fashion, because the keyboard controller does a scancode translation from newer PS/2 scancodes to older PC scancodes. Therefore the keyboard controller must have enough buffer inside it to receive PS/2 scancodes and convert them to PC scancodes. There are different modes that can be selected.

If there are multiple bytes for reading, when you read the first byte, the keyboard controller knows it has been read and the next byte appears some time later for reading. The keyboard controller has a status port which you can read to determine when there is something available for reading on data port.

However, when data becomes available, the keyboard controller would signal an interrupt (IRQ1). So if interrupts are used to receive data, you can read the data port in the interrupt when it triggers.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.