; ;keypad.asm ; XDEF Keypad XREF __SEG_END_SSTACK,Delay,KVS,PRESSED,PRESSED_VAL,PORT_U,SEQUENCE Variables: Section Constants: Section Code: Section Keypad: ;push register and accumulator values for retention PSHA PSHB PSHX PSHY InitSequence: LDX #SEQUENCE ;load the sequence into the X register ReadKeypress: LDAA 1,X+ ;load the value at address in reg X to A CMPA #$00 ;reached the last value in the sequence BEQ InitSequence ;re-initialize X STAA PORT_U ;output to port U JSR Delay ;delay 1 ms LDAA PORT_U ;read port U back into acc. A STAA PRESSED ;save the value of acc. A into memory (the value of the KVS array) ANDA #$0F ;mask the upper nibble CMPA #$0F ;check if masked A == #$0F (no key pressed) BEQ ReadKeypress ;send the next item in the sequence AwaitRelease: ;a key is pressed LDAA PORT_U ;read port U ANDA #$0F ;mask the upper nibble CMPA #$0F ;check if masked A == #$0F (no key pressed) BNE AwaitRelease ;wait for the key to be released InitLookup: ;check for a match in the lookup table LDY #KVS ;load the initital value of KVS array LDAB #$00 ;iteration counter Lookup: LDAA 1,Y+ ;load a value from the lookup table into A CMPA PRESSED ;compare the LUT value to the pressed key BEQ Found ;pressed key reached/found within LUT INCB ;increment the counter BRA Lookup ;check the value at the next index Found: STAB PRESSED_VAL ;the index of the PRESSED value (the numeral of the pressed key) in the KVS/LUT from B -> memory Close: ;restore registers and accumulators PULY PULX PULB PULA RTS