keypad.asm 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. XDEF Keypad
  2. XREF __SEG_END_SSTACK,Debounce,KVS,pressed_key,pressed_num,PORT_U,SEQUENCE,PORT_S,await_key_release,keypad_timeout,await_key_press
  3. Variables: Section
  4. Constants: Section
  5. Code: Section
  6. Keypad: ;push register and accumulator values for retention
  7. PSHA
  8. PSHB
  9. PSHD
  10. PSHX
  11. PSHY
  12. ;do not go to the task at the end of the array
  13. ;when first starting i.e. ckip ArrayEnd for now
  14. BRA InitSequence
  15. ArrayEnd: ;check the flag to await or not (await for passwords)
  16. LDAB await_key_press
  17. CMPB #$00 ;await is false
  18. BEQ Return ;don't do anything else (do not await a keypress)
  19. ;otherwise re-initialze the sequence
  20. InitSequence:
  21. LDX #SEQUENCE ;load the sequence into the X register
  22. ReadKeypress:
  23. ;check wether or not to await
  24. LDAA 1,X+ ;load the value at address in reg X to A
  25. CMPA #$00 ;check the value of A to zero
  26. BEQ ArrayEnd ;this is the end of the array
  27. ;
  28. ;
  29. ;
  30. ;given in instructions
  31. STAA PORT_U ;output to port U
  32. JSR Debounce ;delay 1 ms
  33. LDAA PORT_U ;read port U back into acc. A
  34. STAA pressed_key ;save the value of acc. A into memory (the value of the KVS array)
  35. ANDA #$0F ;mask the upper nibble
  36. CMPA #$0F ;check if masked A == #$0F (no key pressed)
  37. BEQ ReadKeypress ;send the next item in the sequence
  38. ;
  39. ;
  40. ;
  41. ;skip the release wait if the variable is set
  42. LDAA await_key_release
  43. CMPA #$00
  44. BEQ InitLookup
  45. AwaitRelease: ;a key is pressed
  46. LDAA PORT_U ;read port U
  47. ANDA #$0F ;mask the upper nibble
  48. CMPA #$0F ;check if masked A == #$0F (no key pressed)
  49. BNE AwaitRelease ;wait for the key to be released
  50. InitLookup: ;check for a match in the lookup table
  51. LDY #KVS ;load the initital value of KVS array
  52. LDAB #$00 ;iteration counter
  53. Lookup:
  54. LDAA 1,Y+ ;load a value from the lookup table into A
  55. CMPA pressed_key ;compare the LUT value to the pressed key
  56. BEQ Found ;pressed key reached/found within LUT
  57. INCB ;increment the counter
  58. BRA Lookup ;check the value at the next index
  59. Found:
  60. STAB pressed_num ;the index of the PRESSED value (the numeral of the pressed key) in the KVS/LUT from B -> memory
  61. ;AwaitPress:
  62. ; LDAB await_key_press
  63. ; CMPB #$FF
  64. ; BEQ InitSequence
  65. Return:
  66. ;clear the keypad timeout
  67. ;CLR keypad_timeout
  68. ;restore registers and accumulators
  69. PULY
  70. PULX
  71. PULD
  72. PULB
  73. PULA
  74. RTS