;
;	 OPERATOR INTERFACE LOADER
;	 (LINK WITH DOS PROPER)
;
;	02.20 07/??/80 krs
;	02.35 10/19/81 dew Z80 & add IOVECT for GENDEV
;	02.36 12/30/81 rlr C3 special
;	02.37 02/??/82 krs master version
;	02.40 08/05/82 sbw Put BYE back, BOOTNM, HL save area
;       02.41 12/07/82 blc ROMable version.
;
	icl	pdef		;02.39
	icl	df		;02.39
	df.dos			;02.39
	df.cpu			;02.39
	df.sys			;02.39
;
CFCB	EQU	100H	; PUT CHAIN FCB AT 100H
WKSTK	EQU	80H	; WORKING STACK
;
	ENT	CPLOAD
;
CPLOAD:
;
c3ver	equ	0	; 0 = not c3, 1 = c3
most    equ     1       ; 02.41 - 
                        ; 0 = not MOST
                        ; 1 = MOST.
;
;       ENT	DOSEP,INITDOS	; FOR BOOT - 02.41
        ent     initdos         ; 02.41 - for boot.
        ent     opi1            ; 02.41
	ENT	CONPRY		; FOR RE-LOAD
	ENT	VERSION,OSTYPE	; FOR SYSTEM
;	ENT	OPLINK		; OPI LINKAGE TABLE - 02.41
	ent	dirlst					;02.20
;
    ifc SYSUCB=SINGLE
	EXT	?USTK,?UOPT,?UDEV
    elsec
	EXT	RDUCB,SVCRG
    endc
;
	EXT	INIT,CINIT,LINIT
	EXT	PINIT,RINIT
	ext	c0stat,c1stat,c0ind,c1ind,c0outd,c1outd	;02.37
	ext	r0in,r1in,p0out,p1out,l0out,l1out	;02.37
	ext	l0rdy,l1rdy				;02.37
	EXT	DOS,DOSEND,LOGVC
	EXT	SYSBOT,WBOOT,opires			;02.20
	EXT	BYE,ERMSG,BOOTNM			;02.20
	ext	move,dosb,SDVT			;02.20	;02.35
	EXT	PGMDSK					;02.37
        ext     oplink,opcall                   ;02.41
        ext     opdisk,batdsk,opicl,dirlsx      ;02.41
        ext     batfcb,opihl                    ;02.41
        ext     neghl                           ;02.41
        ext     dosep,xdosep,xendlbl            ;02.41
;
CR	EQU	13
LF	EQU	10
;
	ICL	VERSMG		; FOR VERSION AND OSTYPE
;
        ifc     most            ; 02.41
        jmp     initdos
initdos:
        in      062h            ; release boot.
        elsec
INITDOS:
        endc
;
	LXI	SP,WKSTK	; STARTUP STACK
        call    initram         ; 02.41
;
; INITIALIZE SYSTEM
;
	CALL	IOVECT		;initialize GENDEV table	 02.35
	CALL	INIT
	CALL	CINIT	; CONSOLE
	CALL	LINIT	; LIST
	CALL	PINIT	; PUNCH
	CALL	RINIT	; READER
;
	lxi	d,-1		; start out with ^c lockout	 02.37
	mvi	c,82h						;02.37
	call	dos						;02.37
	MVI	C,13	; RESET DOS
	CALL	DOS
	LXI	H,PGMDSK				;02.37
	MOV	A,L					;02.37
	STA	OPDISK					;02.37
	STA	BATDSK					;02.37
	ADRUCB	DEV					;02.37
	DCR	A					;02.37
	MOV	M,A					;02.37
;
;	SIGN-ON MESSAGE IN OPI				 02.20
	JMP	conpry					;02.20
;
	icl	copyrt
;
;02.35  IOVECT: Generate Internal Vector Table
;		for GENDEV: JMP XXX
;
        ram             ; 02.41
VECTORS: DW	0	;temp storage
;
        rog
IOVECT:
	LXI	H,CINIT	;<HL>->CINIT vector
	SHLD	VECTORS
;
	LXI	H,SDVT		;<HL>->SDVT,(0)
	CALL	DO.5	;con
	CALL	DO.3	;term
	CALL	DO.3	;rdr
	CALL	DO.3	;pun
	CALL	DO.3	;lst
	CALL	DO.2	;clock				 02.37
	lhld	c0stat					;02.37
	shld	c1stat					;02.37
	lhld	c0ind					;02.37
	shld	c1ind					;02.37
	lhld	c0outd					;02.37
	shld	c1outd					;02.37
	lhld	r0in					;02.37
	shld	r1in					;02.37
	lhld	p0out					;02.37
	shld	p1out					;02.37
	lhld	l0rdy					;02.37
	shld	l1rdy					;02.37
	lhld	l0out					;02.37
	shld	l1out					;02.37
	ret						;02.37
DO.2:
	MVI	B,2
	BR	DO.TABLE
DO.5:
	MVI	B,5
	BR	DO.TABLE
DO.3:
	MVI	B,3
DO.TABLE:		;<HL>->SDVT(x)
	MOV	E,M
	INX	H
	MOV	D,M
	INX	H
	PUSH	H
	LHLD	VECTORS
LD.TABLE:
		;<DE>->IO table
		;<HL>->Vectors
		;<B>= # of elements
	MVI	M,0C3H		;(HL)=JMP
	INX	H
	LDAX	D		;(DE)->(HL)
	MOV	M,A
	INX	H
	INX	D
	LDAX	D
	MOV	M,A
	INX	H
	INX	D
	DBRNZ	LD.TABLE	;<B>times
;
	SHLD	VECTORS
	POP	H
	RET	
;
; ===== beginning of new codes for rev. 02.41 ====
;       This routine initializes the RAM.
;
        ext     initsdt,sdt
;
initram:
; initialize DOSEP, USER REGISTERS, OPLINK TABLE,
; CP/M DISK DESCRIPTOR CODE AND JMP TABLE.
;
        lxi     h,dosep
        lxi     d,cpload
        push    h
        call    neghl
        dad     d
        push    h
        pop     b             ;bc <-- @cpload - @dosep.
        pop     h
initlp:
        xra     a             ; ram <-- 0.
        mov     m,a
        inx     h
        dcx     b
        mov     a,c
        ora     b
        jnz     initlp
;
        lxi     d,xendlbl
        lxi     h,xdosep
        call    neghl
        dad     d
        push    h
        pop     b             ; bc <-- @xendlbl - @xdosep.
        lxi     h,xdosep
        lxi     d,dosep
        ldir
;
; initialize the SDT table.
;
        lxi     h,initsdt
        lxi     d,sdt
        mov     b,m
        mvi     c,0ffh
        ldi                   ; #-of-disks field.
sdtloop:
        push    b             ;copy a SDT entry.
        mvi     b,28
sdtlp1:
        ldi
        djnz    sdtlp1
        pop     b
;
        djnz    sdtloop
;
        ifc     most
;
; check a hole between 20th K and 43th K memory
; in a 4K increment.
; if a memory hole is found, then the SYSBOT
; is readjusted to the page below the hole.
;
        mvi     b,6
        lxi     h,05000h
        lxi     d,01000h
ckhole:
        mov     a,m
        mov     c,a
        cma
        mov     m,a
        mov     a,m
        cmp     c
        jreq    hole
;
        dad     d
        djnz    ckhole
        ret
hole:
        dcr     h
        shld    sysbot
        xchg
        lxi     h,dosep
        lxi     b,0009
        ldir
        endc
        ret
; ===== end of new codes for rev. 02.41 =========
        eject
; ----------------------------------------------------
;	OPI RESTART ENTRY
;
CONPRY:	LXI	SP,WKSTK
	ADRUCB	OPT				;02.37
	MOV	A,M				;02.37
	ANI	0A3H				;02.37
	MOV	M,A				;02.37
;
; SET UP SYSTEM JUMPS
;
	MVI	A,0C3H
	STA	0
	STA	5
	STA	38H
;
	LXI	H,WBOOT	; @0/ JMP WBOOT
	SHLD	1
	LHLD	SYSBOT
	SHLD	6	; @5/ JMP SYSBOT
	LXI	H,CRASH
	SHLD	39H	; @38/ JMP CRASH
;
; SHOW NO CMD LINE AND CALLING OPI
;
	XRA	A
	STA	80H	; SHOW NO COMMAND LINE
	CMA	A
	STA	OPCALL	; AND CALLING OPI
;
	LXI	D,5CH	; NULL BOTH USER FCB'S
	LXI	H,NONAME
	MVI	C,86H
	CALL	DOS
	LXI	D,6CH
	lxi	h,noname
	mvi	c,86h
	CALL	DOS
	lhld	opicl	; address of conproc OR opi/cp load 02.20
	pchl		; go to right place		    02.20
;-------------------------------------------------------
;  LOOP UNTIL OPI IS RE-LOADED
;
opi1:	LDA	OPDISK
    .DO
	PUSH	AF
	LXI	H,BOOTNM ; GET NAME ADDR PTR
	LXI	D,CFCB
	MVI	C,86H	; SET UP CHAIN FCB WITH NAME
	CALL	DOS
	POP	AF
	STA	CFCB	; REQUEST DISK
;
	LXI	D,CFCB	; CHAIN(opi or conproc)			02.20
	MVI	C,88H
	CALL	DOS
; OPI GOTTA BE THERE
     .NEXT		;02.36
	LXI	H,NO$OP1
	CALL	ERMSG
	LXI	H,BOOTNM
	CALL	ERMSG	; <NAME>
	LXI	H,NO$OP2
	CALL	ERMSG	; WHICH DISK? 
	LXI	H,NO$OP3
	CALL	ERMSG	; WHICH DISK? 
	MVI	C,1
	CALL	DOS	; GET A CHAR
	CPI	'@'	;02.36 CHECK FOR NON-LETTER
	BRLT	.NEXT	;02.36
	ANI	15	; A=DISK TO GET FROM
	push	af	 ;02.33
	LXI	H,NO$OP1 ;02.33 force new line
	CALL	ERMSG	 ;02.33
	pop	af	 ;02.33
	BR	.DO	;02.36
    .UNTIL
;
NO$OP2:	DB	' not found'
no$op1:	db	CR,LF,0
no$op3:	db	'Enter disk: ',0
;
;	ADDRESSING FAULT OR INTERRUPT
;	  CAUSING JUMP TO 38H
;
CRASH:	POP	H
	DCX	H	; BACK UP TO RST 7 ADR
	SHLD	CRMG1
	LXI	SP,80H	; RESTORE SP
	LXI	H,CRMG
	CALL	ERMSG
	JMP	CONPRY
;
CRMG:	DB	'Invalid jump to location ',81H
      	DW	crmg1   ;       
	DB	CR,LF
noname: db	0,0					;02.20
        ram
crmg1:  dw      0       ; put address here
;
        rog
dirlst:		; cdos directory call (9C)		 02.20
	lda	oplink	; is opi resident?		 02.20
	ani	1	; 1 if yes, 0 if no		 02.20
	BRz	nodirl					;02.20	;02.35
	lxi	h,5ch	; ufcb1				 02.20
	lxi	b,16					;02.20
	call	move					;02.20
	LHLUCB	stk	; get user stack ptr		 02.20	;02.35
	xchg		; to de				 02.20
	lxi	b,dirls1				;02.20
	lhld	dirlsx	;get address			 02.20
	pchl		; go do it			 02.20
dirls1:	SDEUCB	STK					;02.20	;02.35
	JMP	dosb	; go back			 02.20	;02.35
;
nodirl	lxi	h,nodmsg				;02.20
	call	ermsg	; say SORRY			 02.20
	ret						;02.20
;
nodmsg:	db	cr,lf,'DIR function not active',cr,lf,0	;02.20
;
; Rev 02.41
; Operator Interface Linkage Table is
; moved to the module 'IOSROM'.
;
	END
