;*************************************************************************
;* KEYLCD-D
;*	Simple program to read the Axiom CMD11A8 keypad and echo the keys
;*	to the LCD screen.
;*
;**************************************************************************
       ORG $2000		; start code at 2000h
       JMP  MAIN		; jump over data to start of program

LCDBAS EQU  $B5F0		; LCD port address
KEYBAS EQU  $B5F4		; keypad port address
IO     EQU  $93			; Keypad port control register value
BUF1   RMB  1			; temporary storage data variables
BUF2   RMB  1
BUF3   RMB  1
BUF4   RMB  1
BUF5   RMB  1

TABLE  FCB  $31,$32,$33,$41	; keypad scan lookup table
       FCB  $34,$35,$36,$42
       FCB  $37,$38,$39,$43
       FCB  $2A,$30,$23,$44

; Start of program
MAIN   SEI			; Disable enable interrupts
       JSR  LCDSET            	; Setup LCD
       JSR  KEYSET            	; Setup Keypad

; Main Loop
M1     JSR  KEYIN             	; Get a character from the keypad
       JSR  LCDOUT            	; Display it on LCD
       JMP  M1                  ; go wait for another key

; Keypad input subroutine
KEYIN  LDY  #KEYBAS		; get keypad port address
       CLR  BUF4              	; clear Counter
       LDAA #$10              	; start with First Row

LOP    CLR  $02,Y             	; Clear Scan lines
       LDAB $00,Y             	; Read Keypad
       ANDB #$F0		; mask all but data
       STAB BUF1              	; Save result

       STAA $02,Y             	; send row scan
       LDAB $00,Y             	; Read Keypad
       ANDB #$F0                ; mask result
       CMPB BUF1              	; is there activity on this row?
       BNE  DONE		; branch if yes

       LSLA                   	; no key here, shift for next Row
       ANDA #$F0                ; mask row counter for test
       BEQ  KEYIN             	; if all rows scanned, start over

       INC  BUF4		; incriment index counter
       INC  BUF4
       INC  BUF4
       INC  BUF4
       BRA  LOP			; go scann next row

; here if something is pressed, find the column
DONE   CLR  BUF3              	; clear column counter
       STAB BUF2		; save row data
LL     ANDB #$10              	; get row number
       LDA  BUF1		; get colum number
       ANDA #$10
       CBA                    	; match found?
       BNE  FOUND		; branch if yes
       INC  BUF3              	; Increment counter
       LSR  BUF1                ; shift for next column
       LSR  BUF2		; shift for next row
       LDAB BUF2		; get row data
       BNE  LL                	; go check next
;
FOUND  CMPA BUF5		; same key as pressed last time?
       BEQ  DD			; branch if no
       STAA BUF5		; save key for debounce
       JMP  KEYIN		; go wait for another key

DD     LDD  #TABLE            	; Load look up table
       ADDB BUF3		; add column index
       ADDB BUF4		; add row index
       XGDY                     ; prepare for table read
       LDAA 0,Y			; get the table data at this location

; now the key pressed data is in A
       CMPA #$2A              	; is key pressed '*' ?
       BNE  NEXT		; branch if not
       JSR  LCDSET		; yes, clear lcd screen
       JMP  KEYIN		; go wait for another key
NEXT   LDAB #$02		; delay count
TT     JSR  DELAY		; wait for lines to settle
       DECB			; next count
       BNE  TT			; wait some more
       RTS

; This version adjusts for 4x20 keypad row addressing if you're expecting
; continuous rows 1,2,3,4
LCDOUT
	ldab	LCDBAS		; get current address

	CMPB 	#$13		; at end of first row
	beq     lcd_ad1		; jif yes
	CMPB 	#$53		; at end of 2nd row
	beq     lcd_ad2		; jif yes
	CMPB 	#$27		; at end of 3rd row
	beq     lcd_ad3		; jif yes

	STAA	LCDBAS+1	; write data to lcd output port
	RTS

lcd_ad1
	ldab	#$A8		; set correct address
	bra	lcd_fix		; continue fix
lcd_ad2
	ldab	#$94		; set correct address
	bra	lcd_fix		; continue fix
lcd_ad3
	ldab	#$D4		; set correct address
lcd_fix
	STAA	LCDBAS+1	; write data to lcd output port
	JSR	DELAY
	STAB	LCDBAS		; make address fix
	RTS

; configure LCD screen
LCDSET LDAA  #$3C             	; set 4x20 Display
       STAA  LCDBAS
       JSR   DELAY
       LDAA  #$01             	; Clear display & Home cursor
       STAA  LCDBAS
       JSR   DELAY
       LDAA  #$0F             	; turn on Display
       STAA  LCDBAS
       JSR   DELAY
       LDAA  #$06             	; Cursor shift on
       STAA  LCDBAS
       LDAA  #$14             	; cursor Shift right
       STAA  LCDBAS
       LDAA  #$02             	; move Cursor to Home
       STAA  LCDBAS
       JSR   DELAY
       RTS

; delay loop
DELAY  PSHB 		; save registers
       PSHA
       LDAB  #$F0	; set loop count 1
DEL1   LDAA  #$F0	; set loop count 2
DEL2   DECA             ; decriment counter 2
       BNE   DEL2       ; branch if not 0
       DECB		; decriment counter 1
       BNE   DEL1	; branch if not 0
       PULA		; restore registers
       PULB
       RTS

; configure Keypad
KEYSET LDAA  #IO	; get configuration value
       STAA  KEYBAS+3	; set it
       RTS
