	TITLE	INIT - FORTRAN-80 RUNTIME INITIALIZATION

	.8080

	ENTRY	$INIT,$EC,$IOFLG,$CPMVN,$CPMWF,$CPMRF
	EXTRN	$CLSFL

GTVRSF	EQU	12		;GET CP/M VERSION FUNCTION
.READS	EQU	20		;READ SEQUENTIAL FUNCTION (1.X)
.WRITS	EQU	21		;WRITE SEQUENTIAL FUNCTION (1.X)
.READR	EQU	33		;READ RANDOM FUNCTION (2.X)
.WRITR	EQU	34		;WRITE RANDOM FUNCTION (2.X)

	DSEG

$CPMVN:	DS	1		;0FFH if CP/M 1.X, 00 if 2.X
$CPMRF:	DS	1		;CP/M Read function held here
$CPMWF:	DS	1		;CP/M WRITE FUNCTION HELD HERE
$EC:	DS	1		;ERROR COUNT - MAX 20 NON-FATAL ERRS
$IOFLG:	DS	1		;FLAG WHETHER I/O INIT HAS BEEN DONE

	CSEG

CPMENT	SET	5

$INIT:	XRA	A
	STA	$EC		;INITIALIZE ERROR COUNT TO 0
	STA	$IOFLG		;INITIALIZE I/O FLAG
	LXI	H,RETINS	;INITIALIZE $CLSFL TO POINT TO "RET"
	SHLD	$CLSFL
	
	LHLD	CPMENT+1	;INITIALIZE STACK TO TOP OF MEMORY-1
	DCX	H
	SPHL

	PUSH	B		;PUT RETURN ADDRESS ON STACK
	MVI	C,GTVRSF
	CALL	CPMENT		;GET CP/M VERSION NUMBER
	SUI	20H-1		;SET $CPMVN SUCH THAT		
	SBB	A		;2.X = 00 AND
	STA	$CPMVN		;1.X = 0FFH..

	LXI	H,(.WRITS SHL 8)+.READS	
	JNZ	SETVF		;1.X USES SEQUENTIAL I/O CALLS
	LXI	H,(.WRITR SHL 8)+.READR	
SETVF:	SHLD	$CPMRF		;2.X USES RANDOM I/O CALLS

RETINS:	RET

	END
