;
; SYSLIB Module Name:  SFOPEN
; Author:  Richard Conn
; SYSLIB Version Number:  2.0
; Module Version Number:  1.0
; Module Entry Points:
;	F$MOPEN		F$OPEN
; Module External References:
;	BDOS
;

;
;  F$OPEN -- OPEN FILE SPECIFIED BY FCB PTED TO BY DE
;	RETURN W/A=0 AND ZERO FLAG SET (Z) IF NO ERROR
;	RETURN W/A=0FFH AND NZ IF FILE NOT FOUND OR NO ROOM IN DIRECTORY
;  F$MOPEN -- OPEN AND MAKE FILE SPECIFIED BY FCB PTED TO BY DE
;	IF FILE DOES NOT EXIST, CREATE IT FIRST
;	SAME RETURN CODES
;
	EXT	BDOS

PUTRG	MACRO
	PUSH	B	; SAVE BC, DE, HL
	PUSH	D
	PUSH	H
	ENDM
GETRG	MACRO
	POP	H	; RESTORE HL, DE, BC
	POP	D
	POP	B
	ENDM

F$OPEN::
	PUTRG		; SAVE REGISTERS
	MVI	C,B$OPEN	; OPEN FILE
	CALL	BDOS	; OPEN FILE
	CPI	255	; NOT PRESENT
	JNZ	OPENOK	; OK
OPENERR:
	MVI	A,0FFH	; ERROR FLAG
	ORA	A	; SET FLAGS
	JMP	OPENDN
OPENOK:
	XRA	A	; OK FLAG
OPENDN:
	GETRG		; RESTORE REGISTERS
	RET

F$MOPEN::
	PUTRG		; SAVE REGISTERS
	MVI	C,B$OPEN	; TRY TO OPEN FILE
	CALL	BDOS
	CPI	0FFH	; NOT PRESENT?
	JNZ	OPENOK	; OK
	MVI	C,B$CREAT	; TRY TO CREATE FILE
	CALL	BDOS
	CPI	0FFH	; NOT ENOUGH ROOM?
	JNZ	OPENOK	; OK
	JMP	OPENERR	; ERROR RETURN

B$OPEN	EQU	15	; OPEN FILE
B$CREAT	EQU	22	; CREATE FILE

	END
