Z80 Routines:Input:GetCSC

From WikiTI
Revision as of 12:44, 20 August 2017 by Geekboy (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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:
;	push bc			; uncomment for preserving bc
		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
;	pop bc
	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:
;	pop bc
	ret
			
getResetBit:
	cp	$FF
	ret	z
	ld	b, 0
getResetBitLoop:
	rrca
	inc	b
	jr	c, getResetBitLoop
	ret

Alternative Version

This has debouncing?

;Getcsc replacement by James Montelongo 
; Outputs:
;  - A: Keycode
; Destroys:
;  - AF
gsGetK:
gsGetCSC:
	push hl
		push de
			push bc
			ld e,$fe		;frist group
			ld c,$01		;key port
			ld l,0		;l holds key pressed
cscloop:
			ld a,$ff		;For some reason emulator really wants it in the loop
			out (1),a		;reset keyport
			ld h,$fe
			out (c),e		;set keygroup
			ld b,8		;loop, Delay needed when work with key driver
			in a,(c)		;read key
cscbit:
			inc l			;inc to get key pressed
			rra 			; if key pressed done
			jp nc,donecsc
			rlc h
			djnz cscbit 	;loop 8
			rlc e			;next key group
			jp m,cscloop	;if bit 7 set loop
			ld l,0		;if no key pressed 0
donecsc:
			ld a,$ff
			out (1),a
			ld a,e
			cpl
			out (1),a
			nop
			nop
			in a,(1)
			inc a
			jp z,nootherkeypressed
			ld l,0
nootherkeypressed:
			ld a,$ff
			out (1),a
			nop
			ld a,e
			out (1),a
			nop
			nop
			in a,(1)
			cp h
			jr z,only1key
			ld l,0
only1key:
			ld a,l		;
			or a
			ld (gs_keymem),a
			pop bc
		pop de
	pop hl
	ret