; A Level Computing Science Project: Car Burglar Alarm.
;
; Program to test the numeric keypad, designed to run on a
; BBC Micro, Model B.  Note the change of VIA addresses
; and the addition of OSWRCH.  This is a routine in the MOS
; to print the character in the Accumulator on the screen.
; The subroutine phex was borrowed from my on-going long
; term project, a 65C02 machine code monitor.
;
; (C) 1987, 2026 Rich Teer.


temp	*		&0070			; Temporary storage.

v1iorb	*		&FE60			; VIA 1 I/O Register B.
v1iora	*		&FE61			; VIA 1 I/O Register A.
v1ddrb	*		&FE62			; VIA 1 Data Direction Register B.
v1ddra	*		&FE63			; VIA 1 Data Direction Register A.
v1t1cl	*		&FE64			; VIA 1 Timer1 Counter Low.
v1t1ch	*		&FE65			; VIA 1 Timer1 Counter High.
v1t2cl	*		&FE68			; VIA 1 Timer2 Counter Low.
v1t2ch	*		&FE69			; VIA 1 Timer2 Counter High.
v1acr	*		&FE6B			; VIA 1 Auxiliary Control Register.
v1ifr	*		&FE6D			; VIA 1 Interrupt Flag Register.
v1ier	*		&FE6E			; VIA 1 Interrupt Enable Register.
oswrch	*		&FFEE			; OSWRCH.


		ORG		&2000			; Assemebled code to start at &2000.

		LDAIM	&0C				; Load Accumulator with 12,
		JSR		oswrch			; and clear screen.

keypad	LDAIM	&E0				; Load Accumulator with 31, and
		STA		v1ddrb			; change DDRB's mask.
		LDAIM	&00				; Load Accumulator with 0,
		STA		v1iorb			; and make all outputs low.
		LDA		v1iorb			; Load Accumulator with I/O Register B.
		ANDIM	&1F				; Mask out output bits.
		CMPIM	&1F				; Compare to 31.
		BEQ		keypad			; If all bits are 1, check again.
		STAZ	temp			; Store Accumulator in temp.
		LDAIM	&1F				; Load Accumulator with 224, and
		STA		v1ddrb			; change DDRB's mask.
		LDAIM	&00				; Load Accumulator with 0,
		STA		v1iorb			; and make all outputs low.
		LDA		v1iorb			; Load Accumulator with I/O Register B.
		ANDIM	&E0				; Mask out output bits.
		CMPIM	&E0				; Compare to 224.
		BEQ		keypad			; If all bits are 1, check again.
		CLC						; Clear Carry.
		ADCZ	temp			; Add temp to the Accumulator.
		JSR		output			; Print number on screen,
		JMP		keypad			; and do again.

output	PHA						; Preserve Accumulator.
		LDAIM	&1A				; Load Accumulator with 26, i.e., Cursor Home,
		JSR		oswrch			; and print on screen.
		PLA						; Restore Accumulator.
		JSR		phex			; Print Accumulator as a 2 digit hex number,
		RTS						; and return.

phex	PHA						; Preserve Accumulator.
		LSRA					; This bit
		LSRA					; shifts the MSN
		LSRA					; into
		LSRA					; the LSN.
		JSR		conv			; Convert to ASCII.
		PLA						; Restore Accumulator.
		ANDIM	&0F				; Mask out MSN.
conv	CMPIM	&0A				; If the Accumulator is greater than 10,
		BCS		letter			; then digit is a letter.
		CLC						; Else clear the Carry flag,
		ADCIM	&30				; add 48,
		JSR		oswrch			; print on screen,
		RTS						; and return.
letter	ADCIM	&36				; A letter so add 54,
		JSR		oswrch			; print on screen,
		RTS						; and return.

		END
