Difference between revisions of "Z80 Routines:Input:GetCSC"

From WikiTI
Jump to: navigation, search
(added another routine)
 
(One intermediate revision by the same user not shown)
Line 17: Line 17:
 
GetCloneSC:
 
GetCloneSC:
 
; push bc ; uncomment for preserving bc
 
; push bc ; uncomment for preserving bc
ld c, 0BFh
+
ld c, 0BFh
ld b, 7
+
ld b, 7
 
getCSCloop:
 
getCSCloop:
ld a, c
+
ld a, c
out (1), a
+
out (1), a
nop
+
nop
nop
+
nop
nop
+
nop
rrca
+
rrca
ld c, a
+
ld c, a
in a, (1)
+
in a, (1)
cp 0ffh
+
cp 0ffh
jr nz, getCSCgotCSC
+
jr nz, getCSCgotCSC
djnz getCSCloop
+
djnz getCSCloop
xor a
+
xor a
ld (lastKey), a
+
ld (lastKey), a
 +
; pop bc
 
ret
 
ret
 
getCSCgotCSC:
 
getCSCgotCSC:
dec b
+
dec b
ld c, b
+
ld c, b
call getResetBit
+
call getResetBit
ld a, b
+
ld a, b
sla c
+
sla c
sla c
+
sla c
sla c
+
sla c
add a, c
+
add a, c
ld b, a ; This dance ensures that
+
ld b, a ; This dance ensures that
ld a, (lastKey) ; the keycode is returned in A
+
ld a, (lastKey) ; the keycode is returned in A
ld c, a
+
ld c, a
ld a, b
+
ld a, b
cp c
+
cp c
ld (lastKey), a
+
ld (lastKey), a
jr nz, getCSCgoodCSC
+
jr nz, getCSCgoodCSC
xor a
+
xor a
 
getCSCgoodCSC:
 
getCSCgoodCSC:
 
; pop bc
 
; pop bc
Line 77: Line 78:
 
gsGetCSC:
 
gsGetCSC:
 
push hl
 
push hl
push de
+
push de
push bc
+
push bc
ld e,$fe ;frist group
+
ld e,$fe ;frist group
ld c,$01 ;key port
+
ld c,$01 ;key port
ld l,0 ;l holds key pressed
+
ld l,0 ;l holds key pressed
 
cscloop:
 
cscloop:
ld a,$ff ;For some reason emulator really wants it in the loop
+
ld a,$ff ;For some reason emulator really wants it in the loop
out (1),a ;reset keyport
+
out (1),a ;reset keyport
ld h,$fe
+
ld h,$fe
out (c),e ;set keygroup
+
out (c),e ;set keygroup
ld b,8 ;loop, Delay needed when work with key driver
+
ld b,8 ;loop, Delay needed when work with key driver
in a,(c) ;read key
+
in a,(c) ;read key
 
cscbit:
 
cscbit:
inc l ;inc to get key pressed
+
inc l ;inc to get key pressed
rra ; if key pressed done
+
rra ; if key pressed done
jp nc,donecsc
+
jp nc,donecsc
rlc h
+
rlc h
djnz cscbit ;loop 8
+
djnz cscbit ;loop 8
rlc e ;next key group
+
rlc e ;next key group
jp m,cscloop ;if bit 7 set loop
+
jp m,cscloop ;if bit 7 set loop
ld l,0 ;if no key pressed 0
+
ld l,0 ;if no key pressed 0
 
donecsc:
 
donecsc:
ld a,$ff
+
ld a,$ff
out (1),a
+
out (1),a
ld a,e
+
ld a,e
cpl
+
cpl
out (1),a
+
out (1),a
nop
+
nop
nop
+
nop
in a,(1)
+
in a,(1)
inc a
+
inc a
jp z,nootherkeypressed
+
jp z,nootherkeypressed
ld l,0
+
ld l,0
 
nootherkeypressed:
 
nootherkeypressed:
ld a,$ff
+
ld a,$ff
out (1),a
+
out (1),a
nop
+
nop
ld a,e
+
ld a,e
out (1),a
+
out (1),a
nop
+
nop
nop
+
nop
in a,(1)
+
in a,(1)
cp h
+
cp h
jr z,only1key
+
jr z,only1key
ld l,0
+
ld l,0
 
only1key:
 
only1key:
ld a,l ;
+
ld a,l ;
or a
+
or a
ld (gs_keymem),a
+
ld (gs_keymem),a
pop bc
+
pop bc
pop de
+
pop de
 
pop hl
 
pop hl
 
ret</nowiki>
 
ret</nowiki>

Latest revision as of 13:44, 20 August 2017


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