Z80 Routines:Input:GetCSC

From WikiTI
Revision as of 17:57, 27 September 2009 by Dr. D'nar (Talk | contribs)

Jump to: navigation, search

This is a replacement for the GetCSC routine. Its returns are exactly the same. You need to have a variable called lastKey so keys won't repeat. Unless you want that. Feel free to add versions with fun stuff like controlled repeating.

;====== GetCSC clone ===========================================================
; This routine is a replacement for the GetCSC bcall.  Its returns are the same.
; Inputs:
;  - None
; Outputs:
;  - A: Keycode
; Destroys:
;  - AF, BC

; To do: Add debouncing

GetCloneSC:
	ld	c, 0bfh
	ld	b, 7
getCSCloop:
	ld	a, c
	out	(1), a
	nop
	nop
	nop
	rrca
	ld	c, a
	in	a, (1)
	cp	0ffh
	jr	nz, getCSCgotCSC
	djnz	getCSCloop
	xor	a
	ld	(lastKey), a
	ret
getCSCgotCSC:
	dec	b
	ld	c, b
	call	getResetBit
	ld	a, b
	sla	c
	sla	c
	sla	c
	add	a, c
	ld	b, a		; This dance ensures that
	ld	a, (lastKey)	; the keycode is returned in A
	ld	c, a
	ld	a, b
	cp	c
	ld	(lastKey), a
	jr	nz, getCSCgoodCSC
	xor	a
getCSCgoodCSC:
	ret
			
getResetBit:
	cp	0ffh
	ret	z
	ld	b, 0
getResetBitLoop:
	rrca
	inc	b
	jr	c, getResetBitLoop
	ret