;
; ROUTING DATABASE MACROS
;
; DATE OF LAST UPDATE:  6/8/87
;
;  6/8/87- Modified Node macro to add save buffer for network 
;          connect request messages.
; 		D.J.L.
;  3/4/87- Modified Node macro to add LOG parameter to support 
;	   logical node names.
;		D.J.L.
;  2/5/87- Modified Node macro to change default access control
; 	   string to MA_ROUTER instead of <TASK=MA_ROUTER>.
;	   	E.D.L.
;
; AUTHOR:  Earl Lakia
;
;
; Main routing macro, defines all others and header
; of the routing database
;
;
.MACRO	RTRDEF,MXSZ,MXOT,MXRS
;
; RTRDEF MACRO
; This macro is responsible for the following:
; 	1) Defining additional macros for the node and route
;	   definitions.
;	2) Defining the largest outbound message buffer size
;	   (default is 512).
;	3) Defining the number of outbound messages to
; 	   be outstanding at one time (default=4).
;	4) Defining the number of response buffers for
;	   acknowleging inbound messages (default=
;	   MXOT*2)
;
;	MXSZ= Largest outbound message size in bytes
;	MXOT= Number of outstanding outbound messages
;	MXRS= Number of response message buffers.
;
.IIF DF,RT$DEF,.ERROR ;MULTIPLE CALL OF THE RTRDEF MACFRO
;
; SET DEFAULTS IF BLANK ARGUMENTS
;
.IF B MXOT
	MXOTX=4	  	; DEFAULT NUMBER OUTPUT BUFFERS
.IFF 
	MXOTX=MXOT
.ENDC
;
.IF B MXSZ
	MXSZX=512.	; DEFAULT SIZE OF OUTPUT BUFFER
.IFF 
	MXSZX=MXSZ
.ENDC
;
.IF B MXRS
	MXRSX=MXOTX*2 	; DEFAULT NUMBER OF RESPONSE BUFFERS
.IFF 
	MXRSX=MXRS
.ENDC
;
.MCALL	CONB$$		; CONNECT BLOCK MACRO
.MCALL  NETDF$		; NETWORK OFFSETS
.MCALL	NRBDEF		; INVOKE NRB MACRO TO GET LOGICAL NAME 
			;   STATUS BIT VALUE
.MCALL	MGHDEF,HDRDEF	; EVOKE MACROS TO GET ROUTER AND QUEUE HEADER SIZES
.NLIST	ME
NETDF$
MGHDEF
HDRDEF
NRBDEF
.LIST	ME
;
NDCNT=0		; NUMBER OF NODES DEFINED SO FAR.
TIDCNT=0	; NUMBER OF MSG ID'S DEFINED (ALL NODES)
MXSZX=MXSZX+MG.SIZ+H.SIZ   ; ADD IN ROUTER AND QUEUE HEADER SIZES 
RSPSIZ=44.	; SIZE OF A RESPONSE BUFFER
;
; HEADER OF THE WHOLE ROUTING DATABASE
;
.PSECT ARTRHD,RW,D,OVR,REL,GBL
;
HD.NOD:	.ASCII	/******/	; OUR NODE NAME
HD.ID:	.BLKW	1	; WILL CONTAIN UNIQUE IDENTIFIER
			; SO CAN TELL IF DATABASE CHANGES
HD.STR:	.WORD	0	; WILL CONTAIN '1' AFTER ROUTER STARTED SO THAT
			; ROUTER CAN'T BE RESTARTED WITHOUT REINSTALLING
			; DATABASE
HD.NRB:	.WORD	NODE0	; NRB BEGINNING THREAD
HD.NBL:	.WORD	NODE0	; LAST SCANNED NRB, INIT TO ZERO
HD.NCT:	.WORD	0	; NUMBER OF NRBS (NODES)
HD.NRT:	.WORD	0	; NOT ROUTABLE TIMER FLAG (1= TIMER ACTIVE)
HD.CTM:	.WORD	0	; CONNECT TIMER FLAG (1= CONNECT TIMER ACTIVE)
HD.OBF:	.WORD	OUTBUF	; POINTER TO OUTPUT BUFFERS
HD.BLS: .WORD	OUTBUF	; POINTER TO NEXT POSSIBLE AVAILABLE OUTPUT BUFFER
HD.OCT:	.WORD	MXOTX	; TOTAL NUMBER OF OUTPUT BUFFERS
HD.BFR: .WORD	MXOTX	; NUMBER OF FREE OUTPUT BUFFERS
HD.OSZ:	.WORD	MXSZX	; SIZE OF OUTPUT BUFFER
HD.RBF:	.WORD	RSPBUF	; POINTER TO RESPONSE BUFFERS
HD.RLS: .WORD	RSPBUF	; POINTER TO NEXT POSSIBLE AVAILABLE RESPONSE BUFFER
HD.RCT:	.WORD	MXRSX	; TOTAL NUMBER OF RESPONSE BUFFERS
HD.RFR: .WORD	MXRSX	; NUMBER OF FREE RESPONSE BUFFERS
HD.RSZ:	.WORD	RSPSIZ	; SIZE OF RESPONSE BUFFER
;
; OUTPUT BUFFERS
;
.PSECT OUTBUF,D,RW,REL,OVR,GBL
;
OUTBUF:
I=1
.REPT	MXOTX
.IF EQ <I-MXOTX>
	.WORD	OUTBUF	; NO MORE BUFFERS, POINT BACK TO FIRST BUFFER
	.WORD	0,0,0	; NRB, AND STATUS BLOCK OF NODE USING THIS BUFFER
.IFF
	.WORD	.+MXSZX+10 ; LINK TO NEXT BUFFER HEADER
	.WORD	0,0,0	; NRB, AND STATUS BLOCK OF NODE USING THIS BUFFER
.ENDC
	.BLKB	MXSZX	; ALLOCATE THE BUFFER
	I=I+1
.ENDR
;
; RESPONSE BUFFERS
;
.PSECT RSPBUF,D,RW,REL,OVR,GBL
RSPBUF:
I=1
.REPT	MXRSX
.IF EQ <I-MXRSX>
	.WORD	RSPBUF	; NO MORE BUFFERS, POINT BACK TO FIRST BUFFER
	.WORD	0,0,0	; NRB, AND STATUS BLOCK OF NODE USING THIS BUFFER
.IFF
	.WORD	.+RSPSIZ+10 ; LINK TO NEXT BUFFER HEADER
	.WORD	0,0,0	; NRB, AND STATUS BLOCK OF NODE USING THIS BUFFER
.ENDC
	.BLKB	RSPSIZ	; ALLOCATE THE RESPONSE BUFFER
	I=I+1
.ENDR
.PAGE
; 
; UTILITY MACROS
;
; MACRO TO CREATE A LABEL
; 
.MACRO LABEL,NAME
NODE'NAME:
.ENDM
;
; MACRO TO GENERATE A LABEL TO POINT BACK TO THE START OF A THREADED LIST
;
.MACRO ELABEL,NAME
NODE'NAME = NODE0	; ENDING LINK
.ENDM
;
; MACRO TO GENERATE A LINK TO NEXT NRB
;
.MACRO FLINK,LNK
	.WORD	NODE'LNK	; LINK TO NEXT NRB
.ENDM
;
; MACRO TO GENERATE A INPUT BUFFER AND GENERATE LINKS
;
.MACRO	UNSBUF,NODE,SIZ
	.WORD	USBF'NODE	; LINK TO UNSOLICITED INPUT BUFFER
	.SAVE
.PSECT	UNSBUF,D,RW,REL,OVR,GBL
	.EVEN
USBF'NODE:
	.WORD	SIZ	; SIZE OF THE BUFFER
	.BLKB	SIZ	; ACTUAL BUFFER
	.RESTORE
.ENDM

;
.PAGE
;
; NODE MACRO
;	This macro builds a Node Routing Block (NRB).
;
;  LOG= Flag.. Primary node name is a logical node name
;  PRI= Primary node name (or Blank if terminating macro)
;  SEC= Secondary node name
;  ACC= Access control string (Default= TASK=MA_ROUTER>
;  SIZ= Largest incomming message size in bytes (Default=512 bytes)
;
; calling sequence:
;	NODE LOG,PRI=<B4750A>,SEC=<B4750B>,ACC=<<MA_ROUTER>>,SIZ=1024.
;
.MACRO NODE,LOG,PRI,SEC,ACC,SIZ
IDCNT=0				; NUMBER IDs FOR THIS NODE
.IF B PRI
	ELABEL	\NDCNT		; CREATES NULL FORWARD LINK
	RTRDID==<NDCNT*1000>+<TIDCNT> ; GLOBAL SYMBOL USED BY ROUTER PGM
				      ;   TO CHECK IF IT IS USING CURRENT 
				      ;   ROUTING DATABASE
	.PSECT	ARTRHD
	SAVEPC=.		; SAVE PSECT PROGRAM COUNTER
	.=HD.ID
	.WORD	RTRDID		; REPLACE HD.ID IN HEADER TO IDENTIFY DBS
	.=HD.NCT
	.WORD	NDCNT		; REPLACE HD.NCT IN HEADER
	.=SAVEPC		; RESTORE PSECT PROGRAM COUNTER
	.MEXIT
.ENDC
;
; BUILD A NRB AS WE HAVE A NODE SPECIFIED
;
.PSECT	NRBS,D,OVR,RW,REL,GBL
;
	LABEL	\NDCNT	; DEFINE OURSELVES
	LUN=NDCNT+4	
	.WORD	LUN	; LOGICAL UNIT NUMBER USED TO CONNECT TO NODE
	.IF NB LOG
	.WORD	NS.LND	; SET LOGICAL NODE NAME STATUS BIT
	.IFF
	.WORD	0	; N.STS NODE STATUS
	.ENDC
	NDCNT=NDCNT+1	; INCREMENT NUMBER OF NODES BEING DEFINED
	FLINK	\NDCNT  ; LINK TO NEXT NRB
	.BLKB	6	; ACTUAL CURRENT NODE NAME (FILLED IN WHEN CONNECTED)
LIM=.+6			; ONLY TAKE 6 CHARACTER NODE NAME
	.ASCII	/'PRI/
.=LIM
LIM=.+6			; ONLY TAKE 6 CHARACTER SEC. NODE NAME
	.IF NB LOG 
	.ASCII	/      /
	.IFF 
	.IF B SEC
	.ASCII	/'PRI/	; IF NO SECONDARY, THEN USE PRIMARY
	.IFF
	.ASCII	/'SEC/
	.ENDC
	.ENDC
.=LIM
	.ASCII	/                              /	; INIT ACC TO
	.ASCII	/                              /	;   BLANKS
.=LIM
LIM=.+60.		; ONLY ALLOW 60 CHARACTER ACCESS CONTROL STRING
	.IF B <ACC>
	.ASCII	/MA_ROUTER/	; DEFAULT ACCESS CONTROL STRING
	.IFF
	.ASCII	/'ACC/	; ACCESS CONTROL STRING
	.ENDC
.=LIM
	.NLIST	ME
	.IF B <ACC>
	CONB$$	,0,1,<MA_ROUTER>  ; BUILD DEFAULT NETWORK CONNECT BLOCK
	.IFF
	CONB$$	,0,1,ACC	;BUILD NETWORK CONNECT BLOCK
	.ENDC
	.LIST	ME
NIDCNT=.		; SAVE FOR REPLACEMENT BY ROUTE MACRO
	.WORD	0	; TOTAL ID'S THIS NODE FROM ROUTE MACRO
	.IF B SIZ
	SIZX=512.	; DEFAULT SIZE OF UNSOLICITED INPUT BUFFER
	.IFF 
	SIZX=SIZ
	.ENDC
	SIZX=SIZX+MG.SIZ+H.SIZ	; ADD IN ROUTER AND QUEUE HEADER SIZES
	UNSBUF	\NDCNT-1,SIZX	; LINK TO UNSOLICITED INPUT BUFFER AND 
				; ALSO ALLOCATE BUFFER
	.WORD	.+2	; N.IBSA ADDRESS OF NETWORK RECEIVE I/O STATUS BLOCK
	.WORD	0,0	; N.IOSB NETWORK RECEIVE I/O STATUS BLOCK
	.WORD	.+2	; N.ISAC ADDRESS OF CONNECT/DISCONNECT I/O STATUS BLOCK
	.WORD	0,0	; N.IOSC CONNECT/DISCONNECT I/O STATUS BLOCK
	.WORD	.+2	; N.ISAU ADDRESS OF NETWORK DATA I/O STATUS BLOCK
	.WORD	0,0	; N.IOSU NETWORK DATA I/O STATUS BLOCK
	.WORD	0	; N.SEQ LINK SEQUENCE INBOUND NUMBER
	.WORD	0	; N.OSEQ LINK SEQUENCE OUTBOUND NUMBER
	.WORD	.+4+N.CBL+30	; N.OIDS ADDRESS LAST SCANNED OUTBOUND NRBID 
	.WORD	0	; N.STAM CONNECT/DISCONNECT STATUS FOR WRITE TO QUEUE
	.BLKB	N.CBL+30	; N.MBX SAVE BUFFER FOR NETWORK CONNECT 
				;   REQUEST MESSAGE
.ENDM
.PAGE
;
; The following macro performs the routing function for
; an outbound message id.
;
;	ID= Message Id that is to be output. If less
;	    than 16 characters are specified, it is
;	    padded with Blanks.
;
;	If the ID is Blank, it terminates a list of
;	ROUTE macros for a particular node.
;
; Calling sequence:
;
;	ROUTE  ID=<MESSAGE_ID>
;
.MACRO ROUTE,ID
;
.PSECT	NRBS,D,OVR,RW,REL,GBL
.IF B ID
	.WORD	0		; INDEX FOR QUEUE
	.WORD	0		; RNA
	.WORD	0		; MSG ID STATUS WORD
	.ASCII	/                /  ; END MESSAGE ID
	SAVEPC=.
	.=NIDCNT		    ; RESET PC TO NUMBER OF IDs IN NRB
	.WORD	IDCNT		    ; STORE NUMBER OF IDs
	.=SAVEPC
	TIDCNT=TIDCNT+IDCNT	    ; ADD TO TOTAL NUMBER OF ID'S
	.MEXIT
.ENDC	
;
; Attach the message id to the current node being defined
;
	.WORD	0		; INDEX FOR QUEUE
	.WORD	0		; RNA
	.WORD	0		; MSG ID STATUS WORD
	LIM=.
	.ASCII	/                /	; INIT TO Blanks
	.=LIM	
	LIM=.+16.		; DON'T LET GET BIGGER THAN 16
	.ASCII	/'ID/
	.=LIM
	IDCNT=IDCNT+1		; INCREMENT NUMBER OF MSG ID'S THIS NODE
.ENDM
.ENDM
