;  SLAVE NETWORK INPUT OUTPUT SYSTEM
;  FOR NOS MODE
;
	TITLE	SLAVE NETWORK INPUT OUTPUT SYSTEM FOR NOS
;
;  EQUATIONS OF SLAVE PROCESSOR ID CODE
;
SLVID	EQU	20H
;
;  EQUATIONS OF NETWORK STATUS BYTE
;
ACTIVE	EQU	10H		;SLAVE LOGGED IN ON NETWORK
RCVERR	EQU	2H		;ERROR IN RECEIVED MESSAGE
SNDERR	EQU	1H		;UNABLE TO SEND MESSAGE
;
;  EQUATIONS OF CODE & DATA
;
SOH	EQU	01H		;START OF HEADER
STX	EQU	02H		;START OF DATA
ETX	EQU	03H		;END OF DATA
EOT	EQU	04H		;END OF TRANSMISSION
ENQ	EQU	05H		;ENQUIRE
ACK	EQU	06H		;ACKNOWLEDGE
ACKS	EQU	07H		;SECOND ACKNOWLEDGE
LF	EQU	0AH		;LINE FEED
CR	EQU	0DH		;CARRIAGRE RETURN
NAK	EQU	15H		;NEGATIVE ACKNOWLEDGE
;
CBUFPR	EQU	9		;PRINT BUFFER BDOS FUNCTION
LOGIN	EQU	64		;LOGIN NDOS FUNCTION NUMBER
;
TMORTY	EQU	100		;TIME OUT RETRY COUNT
MAXRTY	EQU	10		;SEND MESSAGE RETRY COUNT
;
;  EQUATIONS OF INPUT OUTPUT PORT & ADDRESS
;
BDOS	EQU	5		;BDOS ENTRY ADDRESS
;
COMPRT	EQU	0C4H		;COMMUNICATION PORT
STATI	EQU	COMPRT		;INPUT STATUS
DPRTI	EQU	COMPRT+1	;DATA PORT
MSKI	EQU	01H		;INPUT READY BIT
;
STATO	EQU	COMPRT		;OUTPUT STATUS
DPRTO	EQU	COMPRT+1	;DATA PORT
MSKO	EQU	80H		;OUTPUT READY BIT
;
;  JUMP VECTOR FOR SNIOS ENTRY POINTS
;
	JMP	NTWKIN		;NETWORK INITIALIZE
	JMP	NTWKST		;NETWORK STATUS
	JMP	CNTBAD		;RETURN CONFIGRATION TABLE ADDRESS
	JMP	SNDMSG		;SEND MESSAGE
	JMP	RCVMSG		;RECEIVE MESSAGE
	JMP	NTRSET		;NETWORK ERROR RESET
	JMP	NTWKWB		;NETWORK WARM BOOT
;
; SLAVE CONFIGRATION TABLE
;
CNFTBL:
	DB	0		;NETWORK STATUS BYTE
	DB	SLVID		;SLAVE PROCESSOR ID
	DB	81H,0		;A: HOME DISK IS B: OF MASTER 0
	DB	80H,0		;B:		A: OF MASTER 0
	DB	82H,0		;C:		C: OF MASTER 0
	DB	83H,0		;D:		D: OF MASTER 0
	DB	84H,0		;E:		E: OF MASTER 0
	DB	0,0		;F: NO DEVICE
	DB	0,0		;G: NO DEVICE
	DB	0,0		;H: NO DEVICE
	DB	0,0		;I: NO DEVICE
	DB	0,0		;J: NO DEVICE
	DB	0,0		;K: NO DEVICE
	DB	0,0		;L: NO DEVICE
	DB	0,0		;M: NO DEVICE
	DB	0,0		;N: NO DEVICE
	DB	0,0		;O: NO DEVICE
	DB	0,0		;P: NO DEVICE
;
	DB	0,0		;CONSOLE DEVICE
;
	DB	0,0		;LIST DEVICE
	DB	0		;BUFFER INDEX
;
	DB	0		;FMT  MESSAGE BUFFER FOR LISTER
	DB	0		;DID
	DB	SLVID		;SID
	DB	5		;FNC  LIST OUT
	DB	0		;SIZ
	DS	1		;MESSAGE 0  LISTER NUMBER
	DS	128		;MESSAGE 1 - 128  OUTPUT DATA
;
;  INITIALIZE NETWROK
;
NTWKIN:
	CALL	NTRSET		;RESET COMMUNICATION INTERFACE
	XRA	A		;RETURN CODE 0 IS SUCCESS END
	RET
;
;  OUTPUT STATUS OF NETWORK
;
NTWKST:
	LDA	CNFTBL		;GET NETWORK STATUS
	MOV	B,A
	ANI	NOT (RCVERR+SNDERR)
	STA	CNFTBL		;OFF ERROR BITS
	MOV	A,B
	RET
;
;  RETURN CONFIGRATION TABLE ADDRESS
;
CNTBAD:
	LXI	H,CNFTBL
	RET
;
;  SEND MESSAGE ON NETWORK
;  INPUT
;  BC:MESSAGE ADDRESS
;
SNDMSG:
	MOV	H,B
	MOV	L,C
	SHLD	MSGADR		;SAVE MESSAGE ADDRESS
SNDMSR:				;RETRY LOOP
	MVI	A,MAXRTY
	STA	RTYCNT		;INITIALIE RETRY COUNT
SEND:
	CALL	NTRSET		;RESET BUFFER
SENDS:
	LHLD	MSGADR		;GET MESSAGE TOP
	MVI	D,TMORTY	;TIME OUT RETRY COUNT
SENDWT:				;ENQUIRE RESPONSE WAIT LOOP
	MVI	C,ENQ
	CALL	CHROUT		;SEND ENQUIRE TO MASTER
	CALL	CHARIN		;WAIT RESPONSE
	JNC	SENDGT		;GET RESPONSE
	DCR	D		;TIME OVER
	JNZ	SENDWT		;SEND ENQUIRE ONCE MORE
	JMP	CHINTO		;TIME OVER FOR LINK
;
SENDGT:				;GET RESPONSE
	CPI	ENQ		;CHECK SEND - SEND MODE
	JZ	SNDRCV		;CRASH SEND SO TO PASS RECEIVE MODE
	CALL	GETAK0		;GET RESPONSE OF ENQ
	MVI	C,SOH
	MVI	E,5		;HEADER LENGTH COUNT
	CALL	MSGOUT		;SEND SOH,FMT,DID,SID,FNC,SIZ
	XRA	A
	SUB	D
	MOV	C,A
	CALL	NETOUT		;SEND HCS (HEADER CHECK SUM)
	CALL	CHARIW		;GET RESPONSE
	JC	GETATS		;TIME OVER
	CPI	ACK
	JNZ	$+9		;NORMAL MODE
	CALL	CHARIW		;MAYBE BUFFER LEFT ACK, SO IGNORE
	JC	GETATS		;TIME OVER
	SUI	ACKS
	JNZ	GETATS		;MISS MATCH OF RESPONSE
	DCX	H
	MOV	E,M
	INX	H
	INR	E
	MVI	C,STX
	CALL	MSGOUT		;SEND STX,DB0,DB1,....
	MVI	C,ETX
	CALL	NETOUT		;SEND ETX
	XRA	A
	SUB	D
	MOV	C,A
	CALL	NETOUT		;SEND DATA CHECK SUM
	MVI	C,EOT
	CALL	CHROUT		;SEND END OF TRNASMISSION
	CALL	GETAKS		;********NEED CALL ******* FOR ERROR RETRY
	RET
;
;  PASS RECEIVE FOR SEND & SEND MODE
;
SNDRCV:
	CALL	NTRSET		;CLEAR CURRENT BUFFER
	MVI	D,TMORTY
SNDRCW:				;WAIT FOR ANOTHER ENQ
	CALL	CHARIN
	JNC	SNDRCG		;GET DATA
	DCR	D		;TIME OVER
	JNZ	SNDRCW		;WAIT
	JMP	SEND		;MAYBE MISS LINK, SO TO SEND
;
SNDRCG:				;GET DATA
	CPI	ENQ
	JNZ	SEND		;MAYBE MISS LINK
	LXI	H,SNDRCV
	PUSH	H		;SAVE RETRY LOOP
	CALL	SNDACK		;SEND ACKNOWLEDGE
	CALL	CHARIW
	RC			;NO RESPONSE, SO RETRY
	CPI	SOH
	RNZ
	LXI	H,DHEADR	;DUMMY HEADER BUFFER
	MOV	D,A
	MVI	E,5
	CALL	MSGIN		;GET HEADER
	CNC	NETIN		;GET CHECK SUM
	RC			;TIME OUT RETRY
	JNZ	SNDNAK		;CHECK SUM ERROR
	CALL	SNDAKS		;SEND RESPONSE
	CALL	CHARIW
	RC
	CPI	STX		;CHECK START OF TEXT
	RNZ			;RETRY
	MOV	D,A		;INITIALIZE CHECK SUM
	DCX	H
	MOV	E,M
	INR	E
	INR	E		;GET COUNT FOR READ
SNDRCL:				;DATA PASS RECEIVE LOOP
	CALL	NETIN		;GET ONE DATA
	RC			;TIME OUT RETRY
	DCR	E
	JNZ	SNDRCL		;TO NEXT DATA
	CPI	ETX
	RNZ			;MISS MATCH OF END CODE
	CALL	NETIN		;GET CHECK SUM
	RC
	CALL	CHARIN
	RC
	CPI	EOT		;END OF TRANSMISSION
	RNZ
	MOV	A,D
	ORA	A
	JNZ	SNDNAK		;CHECK SUM ERROR
	CALL	SNDAKS
	POP	H
	JMP	SENDS
;
;  GET ACKNOWLEDGE
;  OVERRIDING RETURN WHEN ERROR
;
GETAK0:				;CHECK ACKNOWLEDGE
	SUI	ACK
	RZ
SNDRTY:
	POP	H		;DISCARD RETURN ADDRESS
GETATS:				;TIME OVER RETRY
	LXI	H,RTYCNT
	DCR	M
	JNZ	SEND		;SEND AGAIN
CHINTO:				;CHARACTER IN TIME OUT
	MVI	A,SNDERR
	CALL	ERETRN
	JMP	SNDMSR
;
GETAKS:				;GET SECOND ACKNOWLEDGE
	CALL	CHARIW
	JC	SNDRTY		;TIME OUT
	SUI	ACKS
	RZ			;OK
	JMP	SNDRTY		;RETRY
;
;  RECEIVE MESSAGE FROM NETWORK
;  INPUT
;  BC:MESSAGE ADDRESS
;
RCVMSG:
	MOV	H,B
	MOV	L,C
	SHLD	MSGADR		;SAVE MESSAGE ADDRESS
RCVMSR:
	MVI	A,MAXRTY
	STA	RTYCNT		;INITIALIZE RETRY COUNT
RECALL:
	CALL	RECEIV
	LXI	H,RTYCNT	;RETURN FROM RECEIV IS ERROR
	DCR	M
	JNZ	RECALL		;RETRY
RCVTOT:				;TIME OUT
	MVI	A,RCVERR
	CALL	ERETRN
	JMP	RCVMSR
;
;  RECEIVE BODDY
;  RETURN OVERRIDING WHEN OK
;  SIMPLE RETURN IS ERROR
;
RECEIV:
	LHLD	MSGADR		;GET MESSAGE ADDRESS
	MVI	D,TMORTY
	CALL	NTRSET		;RESET INPUT BUFFER
RCVFST:				;RECEIVE FIRST CHARACTER LOOP
	CALL	CHARIN
	JNC	RCVGFS		;GET FIRST CHARACTER
	DCR	D		;TIME OVER
	JNZ	RCVFST		;RETRY
	POP	H
	JMP	RCVTOT		;SET TIME OVER ERROR FLAG & RETRY
;
RCVGFS:				;GET FIRST CHARACTER
	CPI	ENQ
	JNZ	RECEIV		;NOT ENQUIRE SO RETRY
	CALL	SNDACK		;RESPONSE TO MASTER
	CALL	CHARIW
	RC			;RETURN TO RECEIVE RETRY
	CPI	SOH
	RNZ			;NOT START OF HEADER
	MOV	D,A		;START OF HEADER
	MVI	E,5		;LENGTH OF HEADER
	CALL	MSGIN		;GET HEADER BLOCK
	CNC	NETIN		;GET HCS (HEADER CHECK SUM)
	RC			;TIME OVER RETRY
	JNZ	SNDNAK		;CHECK SUM ERROR
	CALL	SNDAKS		;SEND RESPONSE
	CALL	CHARIW		;WAIT DATA TOP
	RC
	CPI	STX
	RNZ			;NOT START OF TEXT
	MOV	D,A		;START OF TEXT
	DCX	H
	MOV	E,M		;GET SIZE
	INX	H
	INR	E		;MAKE READ COUNT FOR DATA BLOCK
	CALL	MSGIN		;RECEIVE DATA
	RC
	CALL	CHARIN		;GET END CODE OF DATA BLOCK
	RC
	CPI	ETX
	RNZ			;NOT END OF TEXT SO RETRY
	ADD	D
	MOV	D,A
	CALL	NETIN		;GET HEADER CHECK SUM
	RC
	CALL	CHARIN		;GET LAST CODE
	RC
	CPI	EOT
	RNZ			;NOT END OF TRANSMIT  SO ERROR
	MOV	A,D
	ORA	A
	JNZ	SNDNAK		;CHECK SUM ERROR
	POP	H		;OK ALL PACKETTE IS RECEIVED
	LHLD	MSGADR
	INX	H
	LDA	CNFTBL+1	;GET SLAVE ID
	SUB	M		;CHECK SID
	JZ	$+5
	MVI	A,-1		;SID ERROR
	PUSH	PSW
	CALL	SNDAKS		;SEND ACKNOWLEDGE
	POP	PSW
	RET
;
;  ROUTINES TO SEND RESPONSE CODES
;
SNDACK:				;SEND ACKNOWLEDGE
	MVI	C,ACK
	JMP	CHROUT
;
SNDAKS:				;SEND SECOND ACKNOWLEDGE
	MVI	C,ACKS
	JMP	CHROUT
;
SNDNAK:				;SEND NEGATIVE ACKNOWLEDGE
	MVI	C,NAK
	JMP	CHROUT
;
;  ERROR RETURN
;  INPUT
;   A:ERROR BIT
;  OUTPUT
;   A:-1
;
ERETRN:
	LXI	H,CNFTBL
	ORA	M		;ON ERROR BIT
	MOV	M,A
	CALL	NTRSET		;RESET COMMUNICATION INTERFACE
	MVI	A,-1		;ERROR RETUNR CODE
	RET
;
;  NETWORK ERROR RECOVER ROUTINE    RESET COMMUNICATION INTERFACE
;
NTRSET:
	MVI	A,080H
	OUT	STATO		;RESET
	RET
;
;  BOOTING UP ROUTINE
;
NTWKWB:
	JMP	NTRSET		;RESET INTERFACE
;
;  NETWORK OUTPUT DATA
;  INPUT
;   C:OUTPUT DATA
;   D:CHECK SUM
;
NETOUT:
	MOV	A,D
	ADD	C
	MOV	D,A		;MAKE CHECK SUM
CHROUT:
	IN	STATO
	ANI	MSKO		;GET OUTPUT READY MODE
	JNZ	CHROUT		;NOT READY
	MOV	A,C
	OUT	DPRTO		;OUT DATA
	RET
;
;  INPUT FROM NETWORK
;  OUTPUT
;   A:DATA
;  CF:ON TIME OVER   OFF GET DATA
;
CHARIW:				;LONG WAIT
	MVI	B,0
	JMP	CHARIM
;
CHARIN:
	MVI	B,10		;10 MS = 192 CH / 192K BAUD
CHARIM:				;WAIT LOOP
	MVI	C,5AH		;1 MS WAIT VALUE
CHARIL:				;1 MS WAIT LOOP
	IN	STATI
	ANI	MSKI
	JZ	CHARIG		;READY
	DCR	C
	JNZ	CHARIL		;WAIT OF 1 MS
	DCR	B
	JNZ	CHARIM		;WAIT
	STC			;TIME OVER RETURN
	RET
;
CHARIG:
	IN	DPRTI		;GET DATA
	RET
;
;  MESSAGE INPUT
;  INPUT
;  HL:DESTINATION ADDRESS
;   E:INPUT COUNT
;
MSGIN:
	CALL	NETIN
	RC			;TIME OUT
	MOV	M,A		;GET DATA
	INX	H
	DCR	E
	JNZ	MSGIN		;TO NEXT BYTE
	RET
;
;  NETWORK INPUT
;  INPUT
;   D:CHECK SUM
;  OUTPUT
;   A:DATA
;
NETIN:
	CALL	CHARIN		;RECEIVE BYTE IN BINARY MODE
	RC
	MOV	B,A
	ADD	D		;MAKE CHECK SUM
	MOV	D,A
	ORA	A		;OFF CARRY FLAG
	MOV	A,B
	RET
;
;  MESSAGE OUT
;  INPUT
;  HL:SOURCE ADDRESS
;   E:COUNT OF OUTPUT DATA
;   C:PREAMBLE CODE
;
MSGOUT:
	MVI	D,0		;CLEAR CHECK SUM
	CALL	NETOUT		;OUT PREAMBLE CODE
MSGOTL:				;OUT LOOP
	MOV	C,M		;GET CODE
	INX	H
	CALL	NETOUT		;OUT DATA
	DCR	E
	JNZ	MSGOTL		;TO NEXT DATA
	RET
;
;  DATA AREA
;
MSGADR:	DS	2		;MESSSAGE ADDRESS BUFFER
DHEADR:	DS	5		;DUMMY HEADER BUFFER
RTYCNT:	DB	0		;RETRY COUNT
;
;
	END
