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

From WikiTI
Jump to: navigation, search
(Added page)
 
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
[[Category:Z80 Routines:Input|GetCSC]]
 +
[[Category:Z80 Routines|GetCSC]]
 +
 
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.
 
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.
  
Line 13: Line 16:
  
 
GetCloneSC:
 
GetCloneSC:
ld c, 0bfh
+
; push bc ; uncomment for preserving bc
ld b, 7
+
ld c, 0BFh
 +
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
 
ret
 
ret
 
 
 
getResetBit:
 
getResetBit:
cp 0ffh
+
cp $FF
 
ret z
 
ret z
 
ld b, 0
 
ld b, 0
Line 58: Line 64:
 
inc b
 
inc b
 
jr c, getResetBitLoop
 
jr c, getResetBitLoop
 +
ret</nowiki>
 +
 +
==== Alternative Version ====
 +
 +
This has debouncing?
 +
 +
<nowiki>;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</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