Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.7KB

  1. ; kbd - implement GetC for PS/2 keyboard
  2. ;
  3. ; It reads raw key codes from a FetchKC routine and returns, if appropriate,
  4. ; a proper ASCII char to type. See recipes rc2014/ps2 and sms/kbd.
  5. ;
  6. ; *** Defines ***
  7. ; Pointer to a routine that fetches the last typed keyword in A. Should return
  8. ; 0 when nothing was typed.
  9. ; KBD_FETCHKC
  10. ; *** Variables ***
  11. .equ KBD_SKIP_NEXT KBD_RAMSTART
  12. ; Pointer to a routine that fetches the last typed keyword in A. Should return
  13. ; 0 when nothing was typed.
  14. .equ KBD_RAMEND KBD_SKIP_NEXT+1
  15. kbdInit:
  16. xor a
  17. ld (KBD_SKIP_NEXT), a
  18. ret
  19. kbdGetC:
  20. call KBD_FETCHKC
  21. or a
  22. jr z, .nothing
  23. ; scan code not zero, maybe we have something.
  24. ; Do we need to skip it?
  25. push af ; <|
  26. ld a, (KBD_SKIP_NEXT) ;|
  27. or a ; |
  28. jr nz, .skip ; |
  29. pop af ; <|
  30. cp 0x80
  31. jr nc, .outOfBounds
  32. ; No need to skip, code within bounds, we have something! Let's see if
  33. ; there's a ASCII code associated to it.
  34. push hl ; <|
  35. ld hl, kbdScanCodes ; |
  36. call addHL ; |
  37. ld a, (hl) ; |
  38. pop hl ; <|
  39. or a
  40. jp z, unsetZ ; no code. Keep A at 0, but unset Z
  41. ; We have something!
  42. cp a ; ensure Z
  43. ret
  44. .outOfBounds:
  45. ; A scan code over 0x80 is out of bounds. Ignore.
  46. ; If F0 (break code) or E0 (extended code), we also skip the next code
  47. cp 0xf0
  48. jr z, .skipNext
  49. cp 0xe0
  50. jr z, .skipNext
  51. xor a
  52. jp unsetZ
  53. .skipNext:
  54. ld (KBD_SKIP_NEXT), a
  55. xor a
  56. jp unsetZ
  57. .skip:
  58. pop af ; equilibrate stack
  59. xor a
  60. ld (KBD_SKIP_NEXT), a
  61. jp unsetZ
  62. .nothing:
  63. ; We have nothing. Before we go further, we'll wait a bit to give our
  64. ; device the time to "breathe". When we're in a "nothing" loop, the z80
  65. ; hammers the device really fast and continuously generates interrupts
  66. ; on it and it interferes with its other task of reading the keyboard.
  67. push bc
  68. ld b, 0
  69. .wait:
  70. nop
  71. djnz .wait
  72. pop bc
  73. jp unsetZ
  74. ; A list of the values associated with the 0x80 possible scan codes of the set
  75. ; 2 of the PS/2 keyboard specs. 0 means no value. That value is a character than
  76. ; can be read in a GetC routine. No make code in the PS/2 set 2 reaches 0x80.
  77. kbdScanCodes:
  78. ; 0x00 1 2 3 4 5 6 7 8 9 a b c d e f
  79. .db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,'`', 0
  80. ; 0x10 9 = TAB
  81. .db 0, 0, 0, 0, 0,'q','1', 0, 0, 0,'z','s','a','w','2', 0
  82. ; 0x20 32 = SPACE
  83. .db 0,'c','x','d','e','4','3', 0, 0, 32,'v','f','t','r','5', 0
  84. ; 0x30
  85. .db 0,'n','b','h','g','y','6', 0, 0, 0,'m','j','u','7','8', 0
  86. ; 0x40 59 = ;
  87. .db 0,',','k','i','o','0','9', 0, 0,'.','/','l', 59,'p','-', 0
  88. ; 0x50 13 = RETURN 39 = '
  89. .db 0, 0, 39, 0,'[','=', 0, 0, 0, 0, 13,']', 0,'\', 0, 0
  90. ; 0x60 8 = BKSP
  91. .db 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0
  92. ; 0x70 27 = ESC
  93. .db 0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0