PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel_kbd.fth

https://github.com/august0815/forthos
Forth | 132 lines | 114 code | 18 blank | 0 comment | 4 complexity | 9b34461fffc0299ef778712d2418a6bb MD5 | raw file
  1. ; program: kernel_kbd
  2. ; Words related to the keyboard driver
  3. ; License: GPL
  4. ; José Dinuncio <jdinunci@uc.edu.ve>, 12/2009.
  5. %include "forth.h"
  6. %include "kernel_words.h"
  7. %include "kernel_video.h"
  8. extern name_hexprint
  9. %undef OLDLINK
  10. %xdefine LINK name_hexprint
  11. extern keymap
  12. %define keymap keymap
  13. [BITS 32]
  14. %define _key_stat_caps 0x01
  15. %define _key_stat_shift 0x02
  16. ; variable: key_status
  17. ; Store the status of caps, shift and CONTROL keys.
  18. defvar "key_status", key_status, 0, 0
  19. ; function: kbd_flags
  20. ; Returns the keyboard status code.
  21. ;
  22. ; Stack:
  23. ; -- kbd_status
  24. : kbd_flags
  25. 0x64 inb
  26. ;
  27. ; function: kbd_buffer_full
  28. ; true if there is a scancode waiting to be readed
  29. ;
  30. ; Stack:
  31. ; -- bool
  32. : kbd_buffer_full
  33. kbd_flags 1 and
  34. ;
  35. ; function: kbd_scancode_now
  36. ; Returns the scancode readed on the keyboard at this moment.
  37. ;
  38. ; Stack:
  39. ; -- scancode
  40. : kbd_scancode_now
  41. 0x60 inb
  42. ;
  43. ; function: kbd_scancode
  44. ; Waits for a key pressed and returns its sacancode.
  45. ;
  46. ; Stack:
  47. ; -- scancode
  48. : kbd_scancode
  49. begin kbd_buffer_full until
  50. kbd_scancode_now 0xFF and
  51. ;
  52. ; function _tx_key_status
  53. ; Test and xor the key_status variable.
  54. ;
  55. ; If the scancode is equal to the given test, makes an xor
  56. ; between key_status and flags.
  57. ;
  58. ; stack:
  59. ; scancode test flag --
  60. : _tx_key_status
  61. -rot =
  62. if
  63. key_status @ xor key_status !
  64. else
  65. drop
  66. then
  67. ;
  68. ; function: _update_key_status
  69. ; Updates the kbd_flags variable according with the scancode given.
  70. ;
  71. ; Stack:
  72. ; scancode --
  73. : _update_key_status
  74. ; TODO - xor could fail in some cases. Set o clear the bit.
  75. dup 58 _key_stat_caps _tx_key_status ; caps down
  76. dup 42 _key_stat_shift _tx_key_status ; lshift down
  77. dup 170 _key_stat_shift _tx_key_status ; lshift up
  78. dup 54 _key_stat_shift _tx_key_status ; rshift down
  79. dup 182 _key_stat_shift _tx_key_status ; rshift up
  80. drop
  81. ;
  82. ; stack:
  83. ; scancode -- bool
  84. : _key_down?
  85. 0x80 and 0=
  86. ;
  87. ; function: sc_to_c (SCANCODE2CHAR)
  88. ; Converts a scancode to an ASCII character.
  89. ;
  90. ; If the scancode correspond to keyup or to a non-character
  91. ; it returns 0
  92. ;
  93. ; stack:
  94. ; scancode -- char
  95. : sc_to_c
  96. dup _key_down? if
  97. 4 * key_status @ + keymap + c@
  98. else drop 0 then
  99. ;
  100. ; function: getchar
  101. ; Waits for a key to be pressed and then returns its ASCII code.
  102. ;
  103. ; Stack:
  104. ; -- c
  105. : getchar
  106. 0
  107. begin
  108. drop
  109. kbd_scancode
  110. dup _update_key_status
  111. sc_to_c dup
  112. until
  113. ;
  114. global name_getchar