1

I am in the process of learning about operating systems and bootloaders. I have found that, to read keyboard keypresses, one can use the in assembly mnemonic with port 60. This works even when writing low-level assembly programs running on a modern machine.

I was reading this article on the topic, in order to better understand this snippet:

keyhandler:
   in al, 0x60   ; get key data
   mov bl, al   ; save it
   mov byte [port60], al
 
   in al, 0x61   ; keybrd control
   mov ah, al
   or al, 0x80   ; disable bit 7
   out 0x61, al   ; send it back
   xchg ah, al   ; get original
   out 0x61, al   ; send that back
 
   mov al, 0x20   ; End of Interrupt
   out 0x20, al   ;
 
   and bl, 0x80   ; key released
   jnz done   ; don't repeat
 
   mov ax, [port60]
   mov  word [reg16], ax
   call printreg16

How does the machine know to route a keyboard plugged into, for example, an arbitrary USB connector to port 60 so it is accessible for the programmer?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Pseudonym Patel
  • 469
  • 7
  • 16
  • 2
    *This works even when writing low-level assembly programs running on a modern machine.* - not when it's booted into a modern OS! In that case, the BIOS's emulation stuff won't still be in place. It uses SMM I think, System Management Mode. The second section of my question about whether VGA was also like this ([Does modern PC video hardware support VGA text mode in HW, or does the BIOS emulate it (with System Management Mode)?](https://stackoverflow.com/q/61521819)) has some info and links, e.g. https://en.wikipedia.org/wiki/System_Management_Mode#Usage – Peter Cordes Mar 02 '22 at 06:08
  • 2
    Also, if you disable the BIOS's USB legacy emulation, or whatever it calls the option, then your code would need USB drivers to talk to USB keyboards. – Peter Cordes Mar 02 '22 at 06:10
  • @PeterCordes SMM should be active even when in protective mode. Think of it as a sort of hypervisor. – fuz Mar 02 '22 at 10:37
  • @fuz: Right, SMM still exists and is used to emulate some legacy IBM-PC stuff across modes, but once you have your own USB drivers running, you don't want rogue SMM code also communicating with the same USB devices. So the firmware needs to un-hook port 60 at some point, doesn't it? Leaving it only connected to an actual keyboard controller for a PS/2 or AT keyboard port, if there is one. (Or possibly to an SMM hook for emulating a PS/2 keyboard on top of whatever the chipset actually has for a non-USB keyboard port?) I don't know all the details, that's why I didn't post an answer :/ – Peter Cordes Mar 02 '22 at 10:44

0 Answers0