	title	Report key pressed
	name	('KEYTYPE')

; Copyright (c)	Werner Cirsovius
;		Hohe Weide 44
;		D-2000 Hamburg 20
;		Federal Republic of Germany
;		Tel.: 040/4223247

; KEYTYPE reads character from keyboard and reports it in different number bases

; System addresses
;
OS	equ	0000h
BDOS	equ	0005h
TPATOP	equ	BDOS+1
;
; BIOS function numbers
;
_CIN	equ	3		; Keyboard data to Accu
_COT	equ	4		; Reg C to keyboard
;
; BIOS offsets
;
CIN	equ	(_CIN-1)*3
COT	equ	(_COT-1)*3
;
; Characters
;
null	equ	00h
lf	equ	0ah
cr	equ	0dh

	ld	sp,(TPATOP)	; Load stack
	ld	hl,$SYNC
	call	INPUT		; Ask for sync character
	ld	(SYNC),a	; Save it
LOOP:
	ld	hl,$INPUT
	call	INPUT		; Read character
	push	af
	ld	e,a		; Upack character
	call	toChar		; Convert to character
	call	toDec		; Convert to decimal
	call	toHex		; Convert to hex
	call	toOct		; Convert to octal
	call	toBin		; Convert to binary
	ld	hl,$CHAR
	call	string		; Tell result
	pop	af
	ld	hl,SYNC
	cp	(hl)		; Test end
	jr	nz,LOOP		; Get next if not
	jp	OS		; Yeap
;
; "Convert" character to character
;
toChar:
	ld	ix,$CH
	ld	(ix+0),' '	; Set normal one
	ld	(ix+1),e	; Store it
	ld	a,e
	cp	' '		; Test control
	ret	nc		; Nope
	add	a,'@'		; Fix character
	ld	(ix+1),a
	ld	(ix+0),'^'	; Indicate control
	ret
;
; Convert character to decimal
;
toDec:
	ld	ix,$DEC
	set	0,c		; Init leading zero bit
	ld	d,e		; Get character
	ld	b,100		; Set divisor
	call	div		; Divide by 100
	ld	b,10
	call	div		; Divide by 10
	ld	a,'0'
	add	a,d		; Get last digit
	ld	(ix),a
	ret
;
; Build ASCII quotient
;
div:
	ld	(ix),'0'-1	; Init digit
	ld	a,d		; Get number
divon:
	inc	(ix)		; Bump quotient
	sub	b		; Divide
	jr	nc,divon	; Until < 0
	add	a,b		; Make >= 0
	ld	d,a
	ld	a,(ix)		; Get result
	inc	ix
	cp	'0'		; Test result
	jr	nz,nolead0	; Ok
	bit	0,c		; Test leading zero
	ret	z		; Nope
	ld	(ix-1),' '	; Blank leading zero
	ret
nolead0:
	res	0,c		; Indicate non zero digit
	ret
;
; Convert character to hex
;
toHex:
	ld	hl,$HEX
	ld	a,e
        rrca                    ; Get upper bits
        rrca
        rrca
        rrca
	call    ascnib		; Convert it
	ld	a,e
ascnib:
        and     00001111b	; Mask bits
        add     a,090h		; Convert to hex
        daa
        adc     a,'0'+16
        daa
	ld	(hl),a		; Save result
	inc	hl
	ret
;
; Convert character to octal
;
toOct:
	ld	hl,$OCT
	ld	a,e		; Get character
	rlca			; Get upper bits
	rlca
	res	2,a
	call	stOct		; Store it as ASCII
	ld	a,e
	rrca			; Get middle bits
	rrca
	rrca
	call	stOct
	ld	a,e		; Final lower bits
stOct:
	and	00000111b	; Mask bits
	add	a,'0'		; Make ASCII
	ld	(hl),a		; Store it
	inc	hl
	ret
;
; Convert character to binary
;
toBin:
	ld	hl,$BIN
	ld	b,8		; Set bit count
BinSet:
	rlc	e		; Get MSB into LSB
	ld	a,00000001b	; Set mask
	and	e		; Mask bit
	add	a,'0'		; Make ASCII
	ld	(hl),a		; Store it
	inc	hl
	djnz	BinSet
	ret
;
; Read character from console
;
conin:
	ld	de,CIN		; Set offset for read
	call	OScall		; Read it
OSret:
	ret
;
; Put character to console
;
conout:
	ld	de,COT		; Set offset for output
	ld	c,a		; Get character
OScall:
	ld	hl,OSret
	push	hl		; Set return
	ld	hl,(OS+1)	; Fetch BIOS vector
	add	hl,de		; Calculate entry address
	jp	(hl)		; Execute
;
; Request input
;
INPUT:
	call	string		; Tell what we expect
	call	conin		; Read it
	push	af
	call	crlf		; Close line
	pop	af
	ret
;
; Close line
;
crlf:
	ld	hl,$CRLF
;
; Put string ^HL to console
;
string:
	ld	a,(hl)		; Get character
	or	a		; Test end
	ret	z		; Yeap
	push	hl
	call	conout		; Print it
	pop	hl
	inc	hl
	jr	string
;
$SYNC:
	db	'Input character to end with: ',null
$INPUT:
	db	'-> ',null
$CHAR:
	db	'Read: '
$CH:
	db	'cc, decimal='
$DEC:
	db	'ddd, hex='
$HEX:
	db	'xx, octal='
$OCT:
	db	'ooo, binary='
$BIN:
	db	'bbbbbbbb'
$CRLF:
	db	cr,lf,null
SYNC:
	ds	1
	
	end

