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.

191 lines
5.5KB

  1. ; Keyboard matrix reading (CIA 1). Port A selects columns (a 0 bit drives that
  2. ; column), port B returns the rows (a 0 bit = key down). Positions checked
  3. ; against the KERNAL decode table at $EB81 (keycode = column * 8 + row):
  4. ; column 0: CRSR RIGHT row 2 (keycode 2), CRSR DOWN row 7 (keycode 7)
  5. ; column 1: left SHIFT row 7 (keycode 15)
  6. ; column 6: right SHIFT row 4 (keycode 52)
  7. ; column 5: + row 0 (keycode 40), P row 1 (keycode 41), - row 3 (keycode 43)
  8. ; column 7: SPACE row 4 (keycode 60)
  9. ; SHIFT reverses the cursor keys: SHIFT + CRSR RIGHT = left, SHIFT + CRSR DOWN
  10. ; = up. VICE maps the PC arrow keys to these combinations.
  11. CIA1_PRA = $DC00
  12. CIA1_PRB = $DC01
  13. CIA1_ICR = $DC0D
  14. KEY_COL_CURSOR = %11111110
  15. KEY_COL_LSHIFT = %11111101
  16. KEY_COL_RSHIFT = %10111111
  17. KEY_COL_SPACE = %01111111
  18. KEY_COL_P = %11011111
  19. KEY_ROW_RIGHT = %00000100
  20. KEY_ROW_DOWN = %10000000
  21. KEY_ROW_LSHIFT = %10000000
  22. KEY_ROW_RSHIFT = %00010000
  23. KEY_ROW_SPACE = %00010000
  24. KEY_ROW_P = %00000010
  25. KEY_ROW_PLUS = %00000001
  26. KEY_ROW_MINUS = %00001000
  27. REPEAT_DELAY = 30 ; + and - held: repeat after 30 frames,
  28. REPEAT_RATE = 4 ; then every 5th frame
  29. .export key_row_cursor, key_row_lshift, key_row_rshift, key_row_space, key_row_p
  30. .export scroll_dx
  31. .segment "CODE"
  32. ; Stop the KERNAL timer interrupt: nothing uses it any more, its keyboard scan
  33. ; would fight read_keys over port A, and it delayed the buffer flip. CIA 1
  34. ; timer A keeps running, so it still seeds the generator.
  35. stop_kernal_irq:
  36. lda #%01111111 ; clear all CIA 1 interrupt enables
  37. sta CIA1_ICR
  38. lda CIA1_ICR ; acknowledge anything pending
  39. lda #1
  40. sta space_down ; SPACE must be released before it counts
  41. sta p_down
  42. lda #0
  43. sta key_held
  44. sta key_held+1
  45. rts
  46. ; Read the keys once per frame. Sets scroll_dx and scroll_dy to -1, 0 or +1
  47. ; for as long as a cursor key is held. A new press of P shows the next
  48. ; person's scores (R8; held P counts once). + adds a person and - removes
  49. ; the last one (M7.3; held, they repeat). Returns carry set when SPACE has
  50. ; just been pressed (held SPACE counts once).
  51. read_keys:
  52. lda #KEY_COL_CURSOR
  53. sta CIA1_PRA
  54. lda CIA1_PRB
  55. key_row_cursor: ; check hook: tools/check-keys.ps1 fakes rows here
  56. sta keys_cursor
  57. lda #KEY_COL_LSHIFT
  58. sta CIA1_PRA
  59. lda CIA1_PRB
  60. key_row_lshift:
  61. sta keys_lshift
  62. lda #KEY_COL_RSHIFT
  63. sta CIA1_PRA
  64. lda CIA1_PRB
  65. key_row_rshift:
  66. sta keys_rshift
  67. lda #KEY_COL_SPACE
  68. sta CIA1_PRA
  69. lda CIA1_PRB
  70. key_row_space:
  71. sta keys_space
  72. lda #KEY_COL_P
  73. sta CIA1_PRA
  74. lda CIA1_PRB
  75. key_row_p:
  76. sta keys_p
  77. lda #$FF ; select no column
  78. sta CIA1_PRA
  79. ldy #1 ; unshifted: right / down
  80. lda keys_lshift
  81. and #KEY_ROW_LSHIFT
  82. beq @shifted
  83. lda keys_rshift
  84. and #KEY_ROW_RSHIFT
  85. bne @unshifted
  86. @shifted:
  87. ldy #$FF ; shifted: left / up
  88. @unshifted:
  89. lda #0
  90. sta scroll_dx
  91. sta scroll_dy
  92. lda keys_cursor
  93. and #KEY_ROW_RIGHT
  94. bne @no_horizontal
  95. sty scroll_dx
  96. @no_horizontal:
  97. lda keys_cursor
  98. and #KEY_ROW_DOWN
  99. bne @no_vertical
  100. sty scroll_dy
  101. @no_vertical:
  102. ldx #0
  103. lda keys_p
  104. and #KEY_ROW_P
  105. bne @p_up
  106. inx ; X = 1: P is down
  107. @p_up: lda p_down
  108. stx p_down
  109. bne @p_done ; already down last frame (or up now: X = 0)
  110. cpx #1
  111. bne @p_done
  112. ldx shown_person ; next person, the last wraps to the first
  113. inx
  114. cpx people_count
  115. bcc @p_next
  116. ldx #0
  117. @p_next:
  118. stx shown_person
  119. lda #1
  120. sta shown_dirty
  121. @p_done:
  122. ldx #0 ; + : add a person
  123. lda keys_p
  124. and #KEY_ROW_PLUS
  125. jsr key_repeat
  126. bcc @no_plus
  127. jsr add_person
  128. @no_plus:
  129. ldx #1 ; - : remove the last person
  130. lda keys_p
  131. and #KEY_ROW_MINUS
  132. jsr key_repeat
  133. bcc @no_minus
  134. jsr remove_person
  135. @no_minus:
  136. ldx #0
  137. lda keys_space
  138. and #KEY_ROW_SPACE
  139. bne @space_up
  140. inx ; X = 1: SPACE is down
  141. @space_up:
  142. lda space_down
  143. stx space_down
  144. bne @no_press ; already down last frame
  145. cpx #1 ; carry set only for a new press
  146. rts
  147. @no_press:
  148. clc
  149. rts
  150. ; A = the key's row bits (0 = down; Z flag set from them), X = 0 for +, 1 for -. Carry set when
  151. ; the key acts this frame: a new press, then while held every REPEAT_RATE + 1
  152. ; frames after REPEAT_DELAY frames.
  153. key_repeat:
  154. bne @up
  155. lda key_held,x
  156. beq @first
  157. cmp #REPEAT_DELAY
  158. bcc @count
  159. lda #REPEAT_DELAY - REPEAT_RATE
  160. sta key_held,x
  161. sec
  162. rts
  163. @first: inc key_held,x
  164. sec
  165. rts
  166. @count: inc key_held,x
  167. clc
  168. rts
  169. @up: lda #0
  170. sta key_held,x
  171. clc
  172. rts
  173. .segment "BSS"
  174. key_held: .res 2 ; frames + and - have been held
  175. scroll_dx: .res 1 ; keep scroll_dx and scroll_dy together
  176. scroll_dy: .res 1
  177. keys_cursor: .res 1
  178. keys_lshift: .res 1
  179. keys_rshift: .res 1
  180. keys_space: .res 1
  181. space_down: .res 1
  182. keys_p: .res 1
  183. p_down: .res 1

Powered by TurnKey Linux.