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.

138 lines
4.2KB

  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. ; *** Consts ***
  11. .equ KBD_KC_BREAK 0xf0
  12. .equ KBD_KC_EXT 0xe0
  13. .equ KBD_KC_LSHIFT 0x12
  14. .equ KBD_KC_RSHIFT 0x59
  15. ; *** Variables ***
  16. ; Set to previously received scan code
  17. .equ KBD_PREV_KC KBD_RAMSTART
  18. ; Whether Shift key is pressed. When not pressed, holds 0. When pressed, holds
  19. ; 0x80. This allows for quick shifting in the glyph table.
  20. .equ KBD_SHIFT_ON @+1
  21. .equ KBD_RAMEND @+1
  22. kbdInit:
  23. xor a
  24. ld (KBD_PREV_KC), a
  25. ld (KBD_SHIFT_ON), a
  26. ret
  27. kbdGetC:
  28. call KBD_FETCHKC
  29. or a
  30. jr z, .nothing
  31. ; scan code not zero, maybe we have something.
  32. ; Do we need to skip it?
  33. ex af, af' ; save fetched KC
  34. ld a, (KBD_PREV_KC)
  35. ; Whatever the KC, the new A becomes our prev. The easiest way to do
  36. ; this is to do it now.
  37. ex af, af' ; restore KC
  38. ld (KBD_PREV_KC), a
  39. ex af, af' ; restore prev KC
  40. ; If F0 (break code) or E0 (extended code), we skip this code
  41. cp KBD_KC_BREAK
  42. jr z, .break
  43. cp KBD_KC_EXT
  44. jr z, .nothing
  45. ex af, af' ; restore saved KC
  46. ; A scan code over 0x80 is out of bounds or prev KC tell us we should
  47. ; skip. Ignore.
  48. cp 0x80
  49. jr nc, .nothing
  50. ; No need to skip, code within bounds, we have something!
  51. call .isShift
  52. jr z, .shiftPressed
  53. ; Let's see if there's a ASCII code associated to it.
  54. push hl ; --> lvl 1
  55. ld hl, KBD_SHIFT_ON
  56. or (hl) ; if shift is on, A now ranges in 0x80-0xff.
  57. ld hl, kbdScanCodes ; no flag changed
  58. call addHL
  59. ld a, (hl)
  60. pop hl ; <-- lvl 1
  61. or a
  62. jr z, kbdGetC ; no code.
  63. ; We have something!
  64. cp a ; ensure Z
  65. ret
  66. .shiftPressed:
  67. ld a, 0x80
  68. ld (KBD_SHIFT_ON), a
  69. jr .nothing ; no actual char to return
  70. .break:
  71. ex af, af' ; restore saved KC
  72. call .isShift
  73. jr nz, .nothing
  74. ; We had a shift break, update status
  75. xor a
  76. ld (KBD_SHIFT_ON), a
  77. ; continue to .nothing
  78. .nothing:
  79. ; We have nothing. Before we go further, we'll wait a bit to give our
  80. ; device the time to "breathe". When we're in a "nothing" loop, the z80
  81. ; hammers the device really fast and continuously generates interrupts
  82. ; on it and it interferes with its other task of reading the keyboard.
  83. xor a
  84. .wait:
  85. inc a
  86. jr nz, .wait
  87. jr kbdGetC
  88. ; Whether KC in A is L or R shift
  89. .isShift:
  90. cp KBD_KC_LSHIFT
  91. ret z
  92. cp KBD_KC_RSHIFT
  93. ret
  94. ; A list of the values associated with the 0x80 possible scan codes of the set
  95. ; 2 of the PS/2 keyboard specs. 0 means no value. That value is a character that
  96. ; can be read in a GetC routine. No make code in the PS/2 set 2 reaches 0x80.
  97. kbdScanCodes:
  98. ; 0x00 1 2 3 4 5 6 7 8 9 a b c d e f
  99. .db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,'`', 0
  100. ; 0x10 9 = TAB
  101. .db 0, 0, 0, 0, 0,'q','1', 0, 0, 0,'z','s','a','w','2', 0
  102. ; 0x20 32 = SPACE
  103. .db 0,'c','x','d','e','4','3', 0, 0, 32,'v','f','t','r','5', 0
  104. ; 0x30
  105. .db 0,'n','b','h','g','y','6', 0, 0, 0,'m','j','u','7','8', 0
  106. ; 0x40 59 = ;
  107. .db 0,',','k','i','o','0','9', 0, 0,'.','/','l', 59,'p','-', 0
  108. ; 0x50 13 = RETURN 39 = '
  109. .db 0, 0, 39, 0,'[','=', 0, 0, 0, 0, 13,']', 0,'\', 0, 0
  110. ; 0x60 8 = BKSP
  111. .db 0, 0, 0, 0, 0, 0, 8, 0, 0,'1', 0,'4','7', 0, 0, 0
  112. ; 0x70 27 = ESC
  113. .db '0','.','2','5','6','8', 27, 0, 0, 0,'3', 0, 0,'9', 0, 0
  114. ; Same values, but shifted, exactly 0x80 bytes after kbdScanCodes
  115. ; 0x00 1 2 3 4 5 6 7 8 9 a b c d e f
  116. .db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,'~', 0
  117. ; 0x10 9 = TAB
  118. .db 0, 0, 0, 0, 0,'Q','!', 0, 0, 0,'Z','S','A','W','@', 0
  119. ; 0x20 32 = SPACE
  120. .db 0,'C','X','D','E','$','#', 0, 0, 32,'V','F','T','R','%', 0
  121. ; 0x30
  122. .db 0,'N','B','H','G','Y','^', 0, 0, 0,'M','J','U','&','*', 0
  123. ; 0x40 59 = ;
  124. .db 0,'<','K','I','O',')','(', 0, 0,'>','?','L',':','P','_', 0
  125. ; 0x50 13 = RETURN
  126. .db 0, 0,'"', 0,'{','+', 0, 0, 0, 0, 13,'}', 0,'|', 0, 0
  127. ; 0x60 8 = BKSP
  128. .db 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0
  129. ; 0x70 27 = ESC
  130. .db 0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0