;SAVKEY: Saves key assignments from memory to disk
;
;	Usage:   SAVKEY <fileref>
; Fileref is optional and should omit .TYP as the 
;filetype is automatically set to .KEY
; If Fileref is not entered, program will ask for
;the drive and filename
;
;	Saves the arrow and function key settings 
;	in memory to a file with the name specified
;	by the user and filetype .KEY
;
;	(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
		;To move char set also use
		; TBL8 EQU 2015h
NEWORG	EQU	5000H
LRL	EQU	128	;Logical record length
		;To move char set also use
		; LRL EQU 256
STAK	EQU	6000H
UCAS	EQU	05FH    ;AND with char
;
;------MACROS----------------------------
;
PRINT	MACRO	MSG	;;Print string @ MSG
	LXI	D,MSG
	CALL	PSTR
	ENDM
;
LDIR	MACRO
	DB	0EDH,0B0H	;z80 LDIR
	ENDM
;
DJNZ	MACRO	TO
	DB	10H,TO-$-1
	ENDM
;
ONE	EQU	01H
SEVEN	EQU	41H
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
	LXI	D,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
;
PARSE	LXI	D,PFCB		;Parse string DBuff
	MVI	C,98H		; to FCB filespec
	CALL	BDOS
	RET
PFCB	DW	DBUFF
	DW	FCB
;
OPEN	LXI	D,FCB		;Open existing file
	MVI	C,15		; named at fcb1
	CALL 	BDOS
	RET
;
MAKEF	MVI	A,0		;Extent 0
	STA	fcbEX	
	LXI	D,FCB	
	MVI	C,22		;Create file  
	CALL    BDOS		; from FCB 
	RET
;
RENAME	LXI	D,FCB
	MVI	C,23		;Rename file
	CALL	BDOS		; in FCB
	RET
;
CLOSE	MVI	C,16		;Close file
	LXI	D,FCB		; @ FCB
	CALL	BDOS
	RET
;
WRITE	LXI	D,FCB		;;Write sector in 
	MVI	C,21		;; in buffer
	CALL	BDOS		;; to file in fcb1
	RET
;
SETBUFF	MVI	C,26		;;Set File buffer
	CALL	BDOS		;; to (DE)   
	RET
;
;Changes string pointed to by HL to upper case
;->HL=string adr    B=string length
;<-HL=string end+1  B=0  Uses AF
;
UPCASE	MOV	A,M
	ANI	05FH
	INX	H
	DJNZ	UPCASE
	RET
;
;
;==========MAINLINE=============================
;
; Relocate the block move above the bank 8 screen and
; execute it to move the key codes up to KEYtbl1 also
; above the bank 8 screen,
;
START	BLKMOVE	RSEG1,NEWORG,RSEG2-RSEG1
	CALL	NEWORG	;Move and do relocateble code
;
; Check command tail for fileref
;
	LDA	FCB+1
	CPI	'A'
	JNC	ISFILE		;Fileref in tail
;
; No fileref in tail - so get the drive and file
; in which to write the the key table
;
NOFILE	PRINT	MSG1		;Save in file ?
	BLKMOVE DFCB,FCB,36
	BLKMOVE BLANK,DBUFF,14
	REPLY	DBUFF-2,14
	CALL	PARSE
;
; Assure filename is in upper case
;
	LXI	H,FCBNAME
	MVI	B,08
	CALL	UPCASE
;
; If the named file does not exist create it.  If it 
;exists, overwrite or make backup copy - then open
;
ISFILE	BLKMOVE KTYPE,FCBTYPE,3 ; Type = .KEY
	CALL	OPEN		;Open file
	INR	A		;FF -> 0
	JZ	ISFIL$3		;No file: create
ISFIL$2	PRINT	MSG2		;BACKUP OR OVERWRITE
	CALL	GETC
	CPI	'O'
	JZ	MVTBL		;GO overwrite
	CPI	'B'
	JNZ	ISFIL$2
	BLKMOVE FCB,NEWDR,16	;rename to .BAK
	BLKMOVE BAKTYP,NEWTYPE,3
	CALL	RENAME
ISFIL$3	CALL	MAKEF
	CALL	OPEN
;
; Write the keytable in the file, close file 
;and quit.
;
MVTBL	BUFFER  NEWORG+TBL1-RSEG1; Points to keytable 
	CALL	WRITE 		;  Write from keytable
;To save char set also add:
;	buffer  neworg+tbl1-rseg1+128
;	call	write
	CALL	CLOSE		
	JMP	BOOT
;
MSG1	DB	CR,LF,'Write keys in file of type KEY and name?  :$'
MSG2 	DB	CR,LF,'File exists.  Backup or Overwrite (B/O)?  :$'
DFCB    DB      0,'        '
KTYPE	DB	'KEY'
	DW	0,0,0,0,0,0,0,0,0,0,0,0
BAKTYP	DB	'BAK'
BLANK	DB	'              '
;
;Relocatable segment.  This moves the tables from bank 8
; to tbl1 in bank 1;
;
RSEG1	BANK	eight
	BLKMOVE TBL8,NEWORG+TBL1-RSEG1,LRL
	BANK	one
	RET
RSEG2	NOP
TBL1	DS	128
		;To move char set also use 
		; TBL1 DS 256
TOP	DB	0
	END	100H
