Last modified on 1 September 2014, at 03:44

Programming:Keyboard redefinition

Key redefinition library

This is a little library to allow you to scan keyboard, test keys and redefine them.Functions are explained in source code.

Keycodes returned by GetPressed and used for IsPressed/GetMask are the same than firmware keycodes (you know the one you have on disc plate on CPC 664/6128)

For performance , it is possible to compute directly offset and mask in keyboard buffer with GetMask function.This is usefull for game as IsPressed function can be considered as slow.

As SHIFT or CONTROL status can be a problem , you can inhibate them using IgnoreSHITFCTRL.

;simple keyboard module
;no extra functions , just scan and locate a key
;no char buffer , no antibounce, ect...
;SJASM syntax

MODULE key
		
;do a complete key scan and store it at (HL)
;altered : AF - BC - DE

Scan
 push HL
 ld BC,#F782
 out (C),C
 ld BC,#F40e
 ld E,B
 out (C),C
 ld BC,#F6c0
 ld D,B
 out (C),C
 ld C,0 
 out (C),C
 ld BC,#F792
 out (C),C
 ld A,#40
 ld C,#4A
.loop
 ld B,D
 out (C),A
 ld B,E
 ini
 inc A
 cp C
 jr c,.loop
 ld BC,#F782
 out (C),C 
 pop HL
 ret

;check if there is a key is down at (HL)
;only the first key found is returned
;return carry is a key is down
;return key code in A : key code = (line*8)+bit
;returned code is the same than system
;altered : AF - BC

GetPressed		
 push HL		
 ld C,10
.loop_col		
 ld B,8		
 ld A,(HL++)
.loop_bit		
 rla		
 jr nc,.pressed		
 djnz .loop_bit				
 dec C		
 jr nz,.loop_col		
 or A ;no key found !		
 pop HL		
 ret
.pressed		
 dec C		
 ld A,9		
 sub C		
 add A	;*2		
 add A	;*4		
 add A	;*8		
 dec B		
 or B	;add bit		
 scf	;key found !		
 pop HL		
 ret	

;check if a key is pressed
;A = key code		
;HL = 10 byte array scanned with Scan function
;return key state in carry
;altered AF - DE - B

IsPressed	
 push HL	
 push AF ; keep bit	
 and %01111000	
 rrca	
 rrca	
 rrca	
 ld E,A	
 ld D,0
 add HL,DE
;so we get the address
 pop AF
 and 7
 inc A
 ld B,A
 ld A,(HL)
;get line
.loop
 rra ;get bit in carry
 djnz .loop
 ccf	;reverse bit
 pop HL
 ret	

;compute offset and mask for a key
;A = key code
;return offset in C and mask in B
;altered AF - BC

GetMask	
 ld B,A
;get offset	
 and %01111000	
 rrca	
 rrca	
 rrca	
 ld C,A 
;get mask	
 ld A,B	
 and %00000111	
 ld B,A
 ld A,1	
 jr z,.done
.loop	
 add A	
 djnz .loop
.done	
 ld B,A	
 ret	


;erase SHIFT and CONTROL status
;used when that can be a problem , like with IsPressed
;HL = 10 byte array scanned with Scan function
;altered : AF

IgnoreSHITFCTRL		
 push HL	
 inc HL	
 inc HL	
 ld A,(HL)	
 or %10100000	
 ld (HL),A	
 pop HL	
 ret				


 ENDMODULE