;SETKEY: Sets keycodes to those in a disk file
; 
;	Usage: SETKEY <fileame>
; Filename is optional.  If used should include
;drive (if not default) but not .TYP which will
;be KEY.  If no filename used, the program will
;ask for one.
;
;	Sets the arrow and functions keys in effect
;	to those found in the file whose name is
;	specified and of type .KEY   The new key
;	key setting take effect immediately.
;
;	(C) Gordon Wilk
;	    6706 Springpark AV
;	    Los Angeles CA 90056
;
;------	EQUATES -------------------------
;
BOOT	EQU	00	;warmboot
BDOS	EQU	05	;BDOS call
CR	EQU	0DH	;carraige return
LF	EQU	0Ah	;line feed
FCB	EQU	05CH	;adr of default fcb 
fcbDR	EQU	FCB
fcbNAME EQU	FCB+1
fcbTYPE EQU	FCB+9
fcbEX	EQU	FCB+0CH
fcbCR	EQU	FCB+20H
NEWDR	EQU	FCB+10H	;For rename
NEWNAME EQU	FCB+11H
NEWTYPE EQU	FCB+19H
DBUFF	EQU	080H	;adr of default dma 
TBL8	EQU	2155H	;key table in bank 8
NEWORG	EQU	5000H
LRL	EQU	128	;Logical record length
	;To set char set also use
	;LRL EQU 256
STAK	EQU	6000H
;
;------MACROS----------------------------
;
PRINT	MACRO	MSG	;;Print string @ MSG
	LXI	D,MSG
	CALL	PSTR
	ENDM
;
LDIR	MACRO
	DB	0EDH,0B0H	;z80 LDIR
	ENDM
;
ONE	EQU	01H
EIGHT	EQU	81H
BANK	MACRO	NUMBER		;Enable bank #
	MVI	A,NUMBER
	OUT	0
	ENDM
;
BLKMOVE MACRO	FROM,TO,LENGTH  ;;Block move
	LXI	H,FROM
	LXI	D,TO
	LXI	B,LENGTH
	DB	0EDH,0B0H	;;z80 LDIR
	ENDM
;
REPLY	MACRO	BUFFER,LENGTH
	LDA	LENGTH
	STA	BUFFER
	LXI	D,BUFFER
	CALL	GETS
	ENDM
;
BUFFER	MACRO	BUFADR		;Sets buffer address
	LXI 	D,BUFADR	; to BUFADR
	CALL	SETBUFF
	ENDM
;
;-------PROLOG---------------------------------------
;
	ORG	100H
	LXI	SP,STAK
	JMP	START
;
;----------------------------------------------------
;
;			SUBROUTINES
;
GETC	MVI	C,01	 	;get char into A
	CALL	BDOS	
	cpi	03		;Abort
	JZ 	boot
	ANI	05FH		;Uppercase
	RET		
;
GETS	MVI	C,0AH		;get string into
	CALL	BDOS		;adr pointed to by DE
	RET		
;
PSTR	MVI	C,09		;display string pointed 
	CALL	BDOS		;to by DE
	RET
;
OPEN	LXI	D,FCB		;Open existing file
	MVI	C,15		; named at fcb1
	CALL 	BDOS
	RET
;
PARSE	LXI	D,PFCB		;Parse filename in DBuff
	MVI	C,152		; to filespex in FCB
	CALL	BDOS
	RET
PFCB	DW	DBUFF  
	DW	FCB
;
READ	LXI	D,FCB		;;Read sector to 
	MVI	C,20		;; buffer
	CALL	BDOS		
	RET
;
SETBUFF	MVI	C,26		;;Set File buffer
	CALL	BDOS		;; to (DE)   
	RET
;
;
;==========MAINLINE=============================
;
;Move relocatable segment up above bank 8
;
START	BLKMOVE	RSEG1,NEWORG,RSEG2-RSEG1
;
;Check command tail for filespec
; If filespec given go check validity and open
;
	LDA	FCB+1
	CPI	'A' 
	JNC	ISFILE
;
; If no filename given, ask name
;
	BLKMOVE BLANK,DBUFF,15
ASKF	PRINT	MSG1		;FILENAME? 
	REPLY	DBUFF-2,14
	CALL	PARSE
;
; If the named file does not exist ask again. If it 
;exists, open it.
;
ISFILE  BLKMOVE KTYPE,FCBtype,26
	CALL	OPEN		;Open file
	INR	A		;FF -> 0
	JNZ	MVTBL		;If there
	PRINT	MSG2		;If not
	JMP	ASKF		;say so and ask again
;
; Read the keytable in the file into TBL1					
;
MVTBL	BUFFER	NEWORG+TBL1-RSEG1; Points to keytable 
;	To set char set also add
;	mvi	e,2		;set multisector
;	mvi	c,44		;count = 2
;	call	bdos
 	CALL	READ 		;Read into TBL1
	JMP	NEWORG		;To relocated segment
;
MSG1	DB	CR,LF,'KEY file name is? :$'	
MSG2 	DB	CR,LF,'No file by that name$',CR,LF
BLANK   DB      '              ',0
KTYPE	DB	'KEY'
	DS	22
;
;Relocatable segment
;Moves the table from bank 1 to bank 8
;
RSEG1	BANK	EIGHT
	BLKMOVE NEWORG+TBL1-RSEG1,TBL8,LRL
	BANK	ONE
	JMP	BOOT
RSEG2	NOP
TBL1	DS	128	;Key table in bank 1
		;To set char set also use
		;TBL1 DS 256
TOP	DB	0
	END	100H
