;LAST UPDATE: 05.05.2011 savelij
KEYBOARDS PUSH HL
PUSH DE
PUSH BC
PUSH AF
CALL KEYBOARD
POP AF
POP BC
POP DE
POP HL
EI
EMPTY RET
KEY_SCAN LD L,0X2F
LD DE,0XFFFF
LD BC,0XFEFE
KEY_LINE IN A,(C)
CPL
AND 0X1F
JR Z,KEY_DONE
LD H,A
LD A,L
KEY_3KEYS INC D
RET NZ
KEY_BITS SUB 8
SRL H
JR NC,KEY_BITS
LD D,E
LD E,A
JR NZ,KEY_3KEYS
KEY_DONE DEC L
RLC B
JR C,KEY_LINE
LD A,D
INC A
RET Z
CP 0X28
RET Z
CP 0X19
RET Z
LD A,E
LD E,D
LD D,A
CP 0X18
RET
K_TEST LD B,D
LD D,0
LD A,E
CP 0X27
RET NC
CP 0X18
JR NZ,K_MAIN
BIT 7,B
RET NZ
K_MAIN LD HL,TABL_KEYS
ADD HL,DE
LD A,(HL)
SCF
RET
K_DECODE LD A,E
CP 0X3A
JR C,K_DIGIT
DEC C
JP M,K_KLC_LET
ADD A,0X4F
RET
K_LOOK_UP LD D,0
ADD HL,DE
LD A,(HL)
RET
K_KLC_LET LD HL,TABL_KEYD
BIT 0,B
JR Z,K_LOOK_UP
PUSH HL
LD HL,FLAGS_KEY1
BIT 3,(HL)
POP HL
RET NZ
INC B
RET NZ
ADD A,0X20
RET
K_DIGIT CP 0X30
RET C
DEC C
K_KLC_DGT INC B
RET Z
BIT 5,B
LD HL,TABL_KEYC-0X30
JR NZ,K_LOOK_UP
SUB 0X10
CP 0X22
JR Z,K_GAW_CHAR
CP 0X20
RET NZ
LD A,0X5F
RET
K_GAW_CHAR LD A,0X40
RET
KEYBOARD CALL KEY_SCAN
RET NZ
LD HL,KSTATE0
K_ST_LOOP BIT 7,(HL)
JR NZ,K_CH_SET
INC HL
DEC (HL)
DEC HL
JR NZ,K_CH_SET
LD (HL),0XFF
K_CH_SET LD A,L
LD HL,KSTATE4
CP L
JR NZ,K_ST_LOOP
CALL K_TEST
RET NC
LD HL,KSTATE0
CP (HL)
JR Z,K_REPEAT
EX DE,HL
LD HL,KSTATE4
CP (HL)
JR Z,K_REPEAT
BIT 7,(HL)
JR NZ,K_NEW
EX DE,HL
BIT 7,(HL)
RET Z
K_NEW LD E,A
LD (HL),A
INC HL
LD (HL),5
INC HL
LD A,(REPDEL)
LD (HL),A
INC HL
LD C,0 ;(MODE)
LD A,(FLAGS_KEY)
LD D,A
PUSH HL
CALL K_DECODE
POP HL
LD (HL),A
K_END LD (LAST_K),A
PUSH HL
LD HL,FLAGS_KEY
SET 5,(HL)
POP HL
RET
K_REPEAT INC HL
LD (HL),5
INC HL
DEC (HL)
RET NZ
LD A,(REPPER)
LD (HL),A
INC HL
LD A,(HL)
JR K_END
TABL_KEYS DB 0X42,0X48,0X59,0X36,0X35,0X54,0X47,0X56 ; B H Y 6 5 T G V
DB 0X4E,0X4A,0X55,0X37,0X34,0X52,0X46,0X43 ; N J U 7 4 R F C
DB 0X4D,0X4B,0X49,0X38,0X33,0X45,0X44,0X58 ; M K I 8 3 E D X
DB 0X0E,0X4C,0X4F,0X39,0X32,0X57,0X53,0X5A ; SS L 0 9 2 W S Z
DB 0X20,0X0D,0X50,0X30,0X31,0X51,0X41 ;SPACE ENTER P 0 1 Q A
TABL_KEYD EQU $-3
TABL_KEYC DB 0X0C,0X07,0X06,0X04,0X05
DB 0X08,0X0A,0X0B,0X09,0X0F