;********************************************************
;*							*
;*		 KEYBOARD INPUT PATCH			*
;*							*
;*		  SIMULATED FUNCTION			*
;*    KEYS WITH CPM IN GENERAL; THE SORCERER COMPUTER	*
;*    IN PARTICULAR, USING KIP.HEX AND KID.COM &  DDT	*
;*							*
;*    AS PUBLISHED IN DR DOBBS JOURNAL, ISSUE 55  MAY	*
;*    1981, BY JOHN B PALMER.				*
;*	   P.O. BOX 23					*
;*	   BOONSVILLE, CA, 95415			*
;*	   USA						*
;*							*
;*	See the Dr. Dobbs article for commented code	*
;*							*
;********************************************************
;
;	Keyed in By	John Cepak
;		 	Australia
;
FRERAM	EQU	5C00H
TEXT	EQU	FRERAM+256
KIFUN	EQU	01
TAB	EQU	09
LF	EQU	0AH
CR	EQU	0DH
EOF	EQU	01AH
;
ACTIVE	EQU	0FFH
INACTIV	EQU	0
;
	ORG	FRERAM
;
KIP:
OLDST:	JMP	0
OLDIN:	JMP	0
NEWST:	JMP	STAT
NEWIN:	JMP	INP
;
ASWITCH	DB	0
EOSTR:	DB	0
STRPTR:	DW	0
SAVCHR:	DB	0
;
STAT:
	LDA	SWITCH
	ORA	A
	RNZ
	JMP	OLDST
;
INP:
	LDA	SWITCH
	ORA	A
	JNZ	STRING
	CALL	OLDIN
	CPI	KIFUN
	RNZ
	CALL	OLDIN
	STA	SAVCHR
	ORI	20H
	SUI	'`'
	SUI	16
	CC	OLDIN
	LDA	SAVCHR
SEARCH:
	PUSH	B
	PUSH	H
	CPI	KIFUN
	JZ	PEEK
	MOV	B,A
	LXI	H,TEXT
TRY:
	CALL	IGNORE
	JC	EXIT
	JZ	TRY
	CMP	B
	JZ	FOUND
	CALL	SKIP
	JNC	TRY
EXIT:
	MVI	A,'?'
	POP	H
	POP	B
	RET
;
PEEK:
	MVI	A,CR
	CALL	DIO
	MVI	A,LF
	LXI	H,TEXT
PRINT:
	CALL	DIO
	MOV	A,M
	INX	H
	CPI	EOF
	JNZ	PRINT
	POP	H
	POP	B
	MVI	A,CR
	RET
;
DIO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