; maintest.asm: A program for testing libraries etc.
;
J_main
		call F_PIOinit
		call F_LCDinit

		ld HL, STR_Init
		call F_print

		; Set interrupt vector, go into interrupt mode 2 and enable
		; interrupts.
;		ld A, JTABLE_KeypadInt / 256
;		ld I, A
;		im 2
;		ei

WaitLoop
		ld HL, BUF_keytest
		ld B, 10
		call F_getkpstring
		call F_LCDinit
		jr WaitLoop

;-----------------------------------------------------------------------
; F_getkpstring
; Gets a string from the keypad
; Entry: B = length of buffer to store result
;       HL = pointer to buffer to store result
; Exit: HL = pointer to null terminator at the end of the string
F_getkpstring
		push AF
		push BC
F_getkpstring.Poll
		rst #30
		jr c, F_getkpstring.Poll ; Carry set = nothing pressed

		ld (HL), A		; store result
		inc HL
		call F_printhex4	; print the result

		; Keep polling the keyboard, looking for key up
F_getkpstring.keyup
		rst #30
		jr nc, F_getkpstring.keyup
		djnz F_getkpstring.Poll
		ld A, 0
		ld (HL), A
		pop BC
		pop AF
		ret
		
;-----------------------------------------------------------------------
; F_print - Display a null-terminated string.
; HL = pointer to null terminated string to print.
F_print
		ld A, (HL)
		cp 0
		ret z
		call F_LCDputchar
		inc HL
		jr F_print

;-----------------------------------------------------------------------
; F_printbin - Convert A into an ASCII representation of its binary and
; print it.
F_printbin
		push AF
		push BC
		ld B, 8
		ld C, A
F_printbin.loop
		bit 7, C
		jr z, F_printbin.zero
		ld A, '1'
		call F_LCDputchar
		jr F_printbin.Next
F_printbin.zero
		ld A, '0'
		call F_LCDputchar
F_printbin.Next
		rl c
		djnz F_printbin.loop
		ld A, ' '
		call F_LCDputchar
		pop BC
		pop AF
		ret

;-----------------------------------------------------------------------
; F_printhex4 - Convert low order nibble of A into ASCII hex representation
F_printhex4
		push AF
		push BC
		and #0F		; clear high order nibble
		cp 10		; Less than 10?
		jp m, F_printhex4.zerotonine
		ld B, 'A' - 10	; A-F
		jr F_printhex4.exit
F_printhex4.zerotonine
		ld B, '0'	; 0-9
F_printhex4.exit
		add B
		call F_LCDputchar
		pop BC
		pop AF
		ret
		 
;-----------------------------------------------------------------------
; F_PIOinit: Sets up the PIO ports for our devices.
; Port B = keypad: rows on B0-B3, columns on B4-B7
F_PIOinit
		push AF			; preserve caller's registers
		ld A, C_PIO_Output	; Port A - Output mode
		out (C_PIO_PortA_CMD), A
		ld A, C_PIO_Control	; Port B - Control mode (for keypad)
		out (C_PIO_PortB_CMD), A
		ld A, C_PIO_Keypad	; Set each line direction
		out (C_PIO_PortB_CMD), A
		
		pop AF
		ret

;-----------------------------------------------------------------------
; Set up port B interrupts etc.
;F_PIOPortBintr
;		; Set up the port interrupts. Send the interrupt enable
;		; instruction then the bit mask for the keypad (row inputs)
;		ld A, JTABLE_KeypadInt % 256
;		out (C_PIO_PortB_CMD), A
;		ld A, C_PIO_Interrupt
;		out (C_PIO_PortB_CMD), A
;		ld A, C_PIO_KeypadMask
;		out (C_PIO_PortB_CMD), A
;
;		; Set the first scan bit
;		ld A, #80
;		ld (V_PIO_Keyscan), A
;		ret
;
;------------------------------------------------------------------------

; Strings
STR_Init	defb	"Waiting for keypress...", 0
STR_Row		defb	"Row:", 0
STR_Col		defb	" Col:", 0
STR_spaces	defb	"            -", 0

; Buffers
BUF_keytest	BLOCK	160, 0

