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.

176 lines
4.4KB

  1. ; kbd
  2. ;
  3. ; Control TI-84+'s keyboard.
  4. ;
  5. ; *** Constants ***
  6. .equ KBD_PORT 0x01
  7. ; Keys that have a special meaning in GetC. All >= 0x80. They are interpreted
  8. ; by GetC directly and are never returned as-is.
  9. .equ KBD_KEY_ALPHA 0x80
  10. .equ KBD_KEY_2ND 0x81
  11. ; *** Variables ***
  12. ; active long-term modifiers, such as a-lock
  13. ; bit 0: A-Lock
  14. .equ KBD_MODS KBD_RAMSTART
  15. .equ KBD_RAMEND @+1
  16. ; *** Code ***
  17. kbdInit:
  18. ld a, 1 ; begin with A-Lock on
  19. ld (KBD_MODS), a
  20. ret
  21. ; Wait for a digit to be pressed and sets the A register ASCII value
  22. ; corresponding to that key press.
  23. ;
  24. ; This routine waits for a key to be pressed, but before that, it waits for
  25. ; all keys to be de-pressed. It does that to ensure that two calls to
  26. ; waitForKey only go through after two actual key presses (otherwise, the user
  27. ; doesn't have enough time to de-press the button before the next waitForKey
  28. ; routine registers the same key press as a second one).
  29. ;
  30. ; Sending 0xff to the port resets the keyboard, and then we have to send groups
  31. ; we want to "listen" to, with a 0 in the group bit. Thus, to know if *any* key
  32. ; is pressed, we send 0xff to reset the keypad, then 0x00 to select all groups,
  33. ; if the result isn't 0xff, at least one key is pressed.
  34. kbdGetC:
  35. push bc
  36. push hl
  37. ; During this GetC loop, register C holds the modificators
  38. ; bit 0: Alpha
  39. ; bit 1: 2nd
  40. ; Initial value should be zero, but if A-Lock is on, it's 1
  41. ld a, (KBD_MODS)
  42. and 1
  43. ld c, a
  44. ; loop until a digit is pressed
  45. .loop:
  46. ld hl, .dtbl
  47. ; we go through the 7 rows of the table
  48. ld b, 7
  49. ; is alpha mod enabled?
  50. bit 0, c
  51. jr z, .inner ; unset? skip next
  52. ld hl, .atbl ; set? we're in alpha mode
  53. .inner:
  54. ld a, (hl) ; group mask
  55. call .get
  56. cp 0xff
  57. jr nz, .something
  58. ; nothing for that group, let's scan the next group
  59. ld a, 9
  60. call addHL ; go to next row
  61. djnz .inner
  62. ; found nothing, loop
  63. jr .loop
  64. .something:
  65. ; We have something on that row! Let's find out which char. Register A
  66. ; currently contains a mask with the pressed char bit unset.
  67. ld b, 8
  68. inc hl
  69. .findchar:
  70. rrca ; is next bit unset?
  71. jr nc, .gotit ; yes? we have our char!
  72. inc hl
  73. djnz .findchar
  74. .gotit:
  75. ld a, (hl)
  76. or a ; is char 0?
  77. jr z, .loop ; yes? unsupported. loop.
  78. call .debounce
  79. cp KBD_KEY_ALPHA
  80. jr c, .result ; A < 0x80? valid char, return it.
  81. jr z, .handleAlpha
  82. cp KBD_KEY_2ND
  83. jr z, .handle2nd
  84. jp .loop
  85. .handleAlpha:
  86. ; Toggle Alpha bit in C. Also, if 2ND bit is set, toggle A-Lock mod.
  87. ld a, 1 ; mask for Alpha
  88. xor c
  89. ld c, a
  90. bit 1, c ; 2nd set?
  91. jp z, .loop ; unset? loop
  92. ; we've just hit Alpha with 2nd set. Toggle A-Lock and set Alpha to
  93. ; the value A-Lock has.
  94. ld a, (KBD_MODS)
  95. xor 1
  96. ld (KBD_MODS), a
  97. ld c, a
  98. jp .loop
  99. .handle2nd:
  100. ; toggle 2ND bit in C
  101. ld a, 2 ; mask for 2ND
  102. xor c
  103. ld c, a
  104. jp .loop
  105. .result:
  106. ; We have our result in A, *almost* time to return it. One last thing:
  107. ; Are in in both Alpha and 2nd mode? If yes, then it means that we
  108. ; should return the upcase version of our letter (if it's a letter).
  109. bit 0, c
  110. jr z, .end ; nope
  111. bit 1, c
  112. jr z, .end ; nope
  113. ; yup, we have Alpha + 2nd. Upcase!
  114. call upcase
  115. .end:
  116. pop hl
  117. pop bc
  118. ret
  119. .get:
  120. ex af, af'
  121. ld a, 0xff
  122. di
  123. out (KBD_PORT), a
  124. ex af, af'
  125. out (KBD_PORT), a
  126. in a, (KBD_PORT)
  127. ei
  128. ret
  129. .debounce:
  130. ; wait until all keys are de-pressed
  131. ; To avoid repeat keys, we require 64 subsequent polls to indicate all
  132. ; depressed keys
  133. push af ; --> lvl 1
  134. push bc ; --> lvl 2
  135. .pressed:
  136. ld b, 64
  137. .wait:
  138. xor a
  139. call .get
  140. inc a ; if a was 0xff, will become 0 (nz test)
  141. jr nz, .pressed ; non-zero? something is pressed
  142. djnz .wait
  143. pop bc ; <-- lvl 2
  144. pop af ; <-- lvl 1
  145. ret
  146. ; digits table. each row represents a group. first item is group mask.
  147. ; 0 means unsupported. no group 7 because it has no keys.
  148. .dtbl:
  149. .db 0xfe, 0, 0, 0, 0, 0, 0, 0, 0
  150. .db 0xfd, 0x0d, '+' ,'-' ,'*', '/', '^', 0, 0
  151. .db 0xfb, 0, '3', '6', '9', ')', 0, 0, 0
  152. .db 0xf7, '.', '2', '5', '8', '(', 0, 0, 0
  153. .db 0xef, '0', '1', '4', '7', ',', 0, 0, 0
  154. .db 0xdf, 0, 0, 0, 0, 0, 0, 0, KBD_KEY_ALPHA
  155. .db 0xbf, 0, 0, 0, 0, 0, KBD_KEY_2ND, 0, 0x7f
  156. ; alpha table. same as .dtbl, for when we're in alpha mode.
  157. .atbl:
  158. .db 0xfe, 0, 0, 0, 0, 0, 0, 0, 0
  159. .db 0xfd, 0x0d, '"' ,'w' ,'r', 'm', 'h', 0, 0
  160. .db 0xfb, '?', 0, 'v', 'q', 'l', 'g', 0, 0
  161. .db 0xf7, ':', 'z', 'u', 'p', 'k', 'f', 'c', 0
  162. .db 0xef, ' ', 'y', 't', 'o', 'j', 'e', 'b', 0
  163. .db 0xdf, 0, 'x', 's', 'n', 'i', 'd', 'a', KBD_KEY_ALPHA
  164. .db 0xbf, 0, 0, 0, 0, 0, KBD_KEY_2ND, 0, 0x7f