*        704727   SIGMA 5/7         BPM M:INIT2
***********************************************************************
*M*      IN2      INITIALIZATION FOR THE LOADER'S SECOND PASS.
***********************************************************************
*P*      NAME:    IN2
*P*      PURPOSE: ENTRY/EXIT FROM LDR.
*P*               ALLOCATES THE WORK SPACE FOR THE SECOND PASS,
*P*                    DETERMINING IF EXTENDED MEMORY MODE IS NECESSARY
*P*      DESCRIPTION: IN2 CONTAINS THE LOGIC WHICH PARTITIONS MEMORY
*P*               FOR PS2 USAGE. IT ALSO DETERMINES THE SIZE OF EACH
*P*               PROTECTION TYPE AREA FOR THE FINAL LOAD MODULE.
*P*               FIRST,THE SIZE OF THE TCB,LIBRARY ERROR TABLES AND
*P*               DCB NAME TABLE IS ACCOUNTED FOR. THEN THE LARGEST
*P*               PROTECTION TYPE FOR A SINGLE OVERLAY SEGMENT IS
*P*               RETAINED IN MAX00,01 AND 10. ALLMEM IS CALLED ONCE
*P*               TO ALLOCATE BUFFERS FOR THE CORE IMAGE AND RELOCTION
*P*               DICTIONARY(UNLESS ABSOLUTE) OF THE ROOT SEGMENT,AND
*P*               AGAIN WITH THE VALUES MAX00,01 AND 10 FOR CURRENT
*P*               SEGMENT LOADING.
*P*      REFERENCE: THE OVERLAY LOADER TECHNICAL MANUAL.
***********************************************************************
         SYSTEM   SIG7FDP
MODE     EQU      1
         DO       MODE=1
SD       EQU      1
         ELSE
SD       EQU      0
         FIN
CFU      EQU      1
MREF     EQU      1
BPMLIB   EQU      0                 1=BUILD BPM LIB UNDER CP-V
*BPMLIB=1 MUST BE USED ONLY WITH MODE=1 AND THEN JUST TO
*FORM A LOADER TO USE IN BUILDING BPM LIBRARIES UNDER CP-V.
         CSECT    1
         DEF      MCI2
MCI2     EQU      %
         REF      M15
         REF      M16
         REF      M17
         REF      MN1
         REF      MN4
         REF      MN8
         REF      MN9
         REF      Y000B
         REF      Y4
         REF      Y00FF
         REF      YFF00FFFF
         REF      M11
         DEF      INIT2
         REF      RFDFSTK
         REF      RFDFBAS
         REF      EXPRSTK
         REF      EXPRSTK1
         REF      EXPRBAS
         REF      BSEG2
         REF      CSEG1
         REF      BUF
         REF      TEMPPTR
         REF      FCOUNT
         REF      ERRTAB
         REF      ERRSTK
         REF      TCBSIZE
         REF      TCBBLNK
         REF      RSEG00
         REF      RSEG10
         REF      RREL00
         REF      RREL01
         REF      RREL10
         REF      CSEG00
         REF      CREL00
         REF      MAX00
         REF      MAX01
         REF      MAX10
         REF      DLOC
         REF      PLOC
         REF      SLOC
         REF      LOCCT
         REF      LOADBAS
         REF      MAXRFDF
         REF      MAXEXPR
         REF      TOPOMEM
         REF      PRINT,USID,LOCWD
         REF      NXTAVPG
         REF      SBLNKDIS
         REF      00DIS
         REF      01DIS
         REF      10DIS
         REF      BIASDIS
         REF      LOWLIMDS
         REF      ERRDIS
         REF      FCOMSIZ
         REF      M:LM
         REF      XMEM3X,XMKEY
         REF      XMRKEY
         REF      RFLDMODS,BREFBIT,RFLDTBSZ
         REF      DOREFPTR,TXS:OVRL
ER9      EQU      9
ER15     EQU      X'15'
ER17     EQU      X'17'
ER18     EQU      X'18'
         REF      FASTSRCH          FLAG = 0 IF USING BINARY STACK SEARCH
         REF      RDSYMS            COUNT OF NO. OF ENTRIES IN REF/DEF
*,*                                 POINTER TABLE (RDPTRS).
         REF      RDPTRS            POINTER TO START OF ORDERED LIST
*,*                                 OF POINTERS INTO THE REF/DEF STACK
         REF      Y05               MASK
         DO       MODE=1
         REF      CHKLM
         REF      CORELIB
         REF      M:FVP
         FIN
         REF      CODE
         REF      CFUPTR
         DO       SD=1
         REF      SYMBOLTB,SYMTOP
         FIN
         PAGE
         REF      MESSAGE
BGEZ     EQU      X'681'
BE       EQU      X'683'
BNEZ     EQU      X'693'
BGE      EQU      X'681'
BL       EQU      X'691'
BAZ      EQU      X'684'
BCR8     EQU      X'688'
BCR10    EQU      X'68A'
BLE      EQU      X'682'
BEZ      EQU      X'683'
*        QUIT     ERROR,CONDITION
QUIT     CNAME
         PROC
LF       EQU      %
         DO       NUM(AF)=3
         DO       AF(2)~=0
         GEN,12,20  AF(2),%+4+MODE
         FIN
         STW,AF(3) CODE
         ELSE
         DO       AF(2)~=0
         GEN,12,20  AF(2),%+3+MODE
         FIN
         FIN
         LI,R3    AF(1)
         DO       MODE=1
         LI,R4    AF(3)=SR3
         FIN
         B        MESSAGE
         PEND
*        SNAP     FROM,TO,TEXT1,TEXT2
SNAP     CNAME
         PROC
LF       CAL1,3   %+1
         DATA     0,AF(1),AF(2),AF(3),AF(4),X'02000000'
         PEND
         PAGE
*                 SYMBOLIC REGISTER DEFINITIONS.
R0       EQU      0
R1       EQU      1
R2       EQU      2
R3       EQU      3
R4       EQU      4
R5       EQU      5
R6       EQU      6
R7       EQU      7
SR1      EQU      8
SR2      EQU      9
SR3      EQU      10
SR4      EQU      11
D1       EQU      12
D2       EQU      13
D3       EQU      14
D4       EQU      15
         PAGE
*                 IMMEDIATE CONSTANTS FOR LOADER.
K0       EQU      0
K1       EQU      1
K2       EQU      2
K3       EQU      3
K4       EQU      4
K5       EQU      5
K6       EQU      6
K7       EQU      7
K8       EQU      8
K9       EQU      9
KA       EQU      X'A'
KB       EQU      X'B'
KC       EQU      X'C'
KD       EQU      X'D'
KE       EQU      X'E'
KF       EQU      X'F'
K10      EQU      X'10'
K11      EQU      X'11'
K12      EQU      X'12'
K16      EQU      X'16'
K17      EQU      X'17'
K19      EQU      X'19'
K1C      EQU      X'1C'
K1E      EQU      X'1E'
K1F      EQU      X'1F'
K20      EQU      X'20'
K22      EQU         X'22'
K24      EQU      X'24'
K26      EQU      X'26'
K39      EQU      X'39'
K3C      EQU      X'3C'
K3F      EQU      X'3F'
K40      EQU      X'40'
K43      EQU      X'43'
K6C      EQU      X'6C'
K78      EQU      X'78'
K80      EQU      X'80'
KF0      EQU      X'F0'
KF1      EQU      X'F1'
KF2      EQU      X'F2'
KFF      EQU      X'FF'
K100     EQU      X'100'
K120     EQU      X'120'
K1FF     EQU      X'1FF'
K200     EQU      X'200'
K202     EQU      X'202'
K220     EQU      X'220'                                                727
K256     EQU      X'256'
K400     EQU      X'400'
K800     EQU      X'800'
KC00     EQU      X'C00'
K1000    EQU      X'1000'
K2000    EQU      X'2000'
K3FFF    EQU      X'3FFF'
K4000    EQU      X'4000'
K4100    EQU      X'4100'
K4110    EQU      X'4110'
K7FFF    EQU      X'7FFF'
KFF00   EQU      X'FF00'
K8000    EQU      X'8000'
K1FFFF   EQU      X'1FFFF'
K20000   EQU      X'20000'
KN1      EQU      -1
KN2      EQU      -2
KN3      EQU      -3
KN4      EQU      -4
KN8      EQU      -8
KN16     EQU      -16
KN100    EQU      -100
KN200    EQU      -X'200'
KN400    EQU      -X'400'
KTE      EQU      'E'
KTR      EQU      'R'
         PAGE
*                                   INIT2 CALCULATES THE SIZE OF THE
*                                   LOAD MODULE AND LAYS OUT STACK
*                                   AND BUFFER AREAS FOR PASS2.
INIT2    EQU      %
         PSW,SR4  *R0
         DO       MODE=1
*                                RELEASE ALL COMMON PAGES, IF ANY.
*                                (THESE USED TO BE EXPRESSION
*                                STACK AREA).
         LW,7     TOPOMEM
         AW,7     M:FVP
         CAL1,8   7
         BCS,8    %+3
         AI,7     -X'200'
         B        %-3
         FIN
         LI,R4    K0
         STW,R4   CSEG1
CALCSIZE EQU      %
*                                   CALCULATES THE SIZE OF THE TCB AND
*                                   ASSOCIATED TABLES.
         LW,R4    LOADBAS           IF ZERO BIAS, NO BUILT STUFF
         BEZ      FINDLGSTPATH
         LW,R4    LOCWD
         CI,R4    K2000
         BANZ     CALCSIZ2
         LI,SR1   TCBBLNK+14
         LW,R4    LOCCT
         AI,R4    ERRDIS
         INT,R6   *R4
         LH,R6    *R4
         STW,R6   ERRSTK
         STW,R7   ERRTAB
         AW,SR1   R6
         AW,SR1   R7
         STW,SR1  TEMPPTR
         AI,R4    8-7               TMPSZDIS-ERRDIS
         LH,R6    *R4
         AW,SR1   R6
         LW,R3    SR1
         LW,D4    LOCWD
         CI,D4    K40
         BAZ      XNOM100
         AI,SR1   KFF
         AND,SR1  MN8
         B        TCBFIX
XNOM100  EQU      %
         CI,D4    K20
         BAZ      SETTCBSIZE
         AI,SR1   X'F'
         AND,SR1  MN4
TCBFIX   EQU      %
         LW,R7    SR1
         SW,SR1   R3
         AI,SR1   1
         AND,SR1  MN1
         SLS,SR1  15
         AWM,SR1  00DIS,R1
         LW,SR1   R7
SETTCBSIZE EQU    %
         STW,SR1  TCBSIZE
         PAGE
*                                   ACCOUNT FOR DCB TABLE SIZE
*                                   IN 01 IF BPM OR 10 IF CP-V.
CALCSIZ2 EQU      %
         LI,SR1   0
         LI,R3    0
         LW,R6    LOCWD
         CI,R6    K10               IS IT LIBRARY
         BANZ     FINDLGSTPATH
         LW,SR1   FCOUNT            NO.
         DO       MODE=0[BPMLIB=1
         BNEZ     YESDCBS
         ELSE
         BEZ      NODCBS
         FIN
         DO       CFU=1
         LW,D4    CFUPTR            CFUPTR>0 & FCOUNT>2
         BEZ      YESDCBS           IMPLIES DCBS.
         CI,SR1   K2
         BG       YESDCBS
         LW,D4    RFDFBAS
         LI,R6    K0
         LW,R7    Y0401             M:* IS THE ONLY DCB & MUST
         STW,R7   *D4               BE REMOVED (CHANGE TO SREF)
         AI,D4    K3
         STW,R6   FCOUNT              FCOUNT=0,
         STW,R6   10DIS,R1            AND 10 SIZE IN ROOT'S TREE=0.
         LW,R6    LOCWD
         B        NOTREE
NODCBS   EQU      %
         FIN
         DO       MODE=1
         STW,SR1  10DIS,R1
         FIN
NOTREE   AI,R3    X'C'            NO DCBS IMPLIES ROOT ONLY.
         B        BOUNDM1XX
YESDCBS  EQU      %
*                                   IN R3 WE ACCUMULATE THE SIZE OF THE
*                                   01 PROTECTION TYPE IN CASE WE HAVE
*                                   TO ACCOUNT FOR A BOUND OF M10 ORM100
         AI,SR1   4
         AND,SR1  MN1
         SLS,SR1  15
         DO       MODE=0[BPMLIB=1
         AWM,SR1  01DIS,R1
         LW,R3    SR1
         SLS,R3   -15
         ELSE
         AW,SR1   10DIS,R1
         AW,SR1   Y00FF
         AND,SR1  YFF00FFFF
         STW,SR1  10DIS,R1
         FIN
         LW,R7    -1,R1
         SW,R7    R1
         AW,R3    R7
         AI,R3    2
         CI,R7    KC
         BLE      BOUNDM1XX         NO OVERLAY IF ONLY ROOT.
         CW,R6    RFLDMODS          IS REF OR BREF SPECIFIED
         BAZ      BOUNDM1XX         BRANCH IF NO REF LOAD REQUIREMENTS.
         SLS,R7   -1                APPROX REF LD ENTRIES = 5.5/SEGMENT
         MTW,0    RFLDTBSZ
         BEZ      %+2               USE SPECIFIED VALUE IF SUPPLIED
         LW,R7    RFLDTBSZ
         SLS,R7   2                 ASSUME 4-WDS PER ENTRY IN RFLDTABLE
         CW,R6    BREFBIT
         BAZ      %+2
         SLS,R7   -1                RFLDTABLE SIZE IS 2 WDS PER ENTRY
         AI,R7    X'16'
         STW,R7   RFLDTBSZ          SET SIZE OF REF LOADING
         AI,R7    X'A'
         SLS,R7   15
         AWM,R7   01DIS,R1
         SLS,R7   -15
         AW,R3    R7
         LI,R7    K100
         STS,R7   LOCWD             *  *  *   FORCE LMN 'ABS'   *  *  *
BOUNDM1XX EQU     %
         LW,SR1   R3
         LW,R7    LOCWD
         CI,R7    X'20'
         BAZ      NOTM10
         AI,SR1   X'F'
         AND,SR1  MN4
         B        ADJUST01
NOTM10   CI,R7    X'40'
         BAZ      FINDLGSTPATH
         AI,SR1   X'FF'
         AND,SR1  MN8
ADJUST01 EQU      %
         SW,SR1   R3                COMPUTE DIFFERENCE AND AUGMENT 01.
         AI,SR1   1
         AND,SR1  MN1
         SLS,SR1  15
         AWM,SR1  01DIS,R1
*                                   THIS ROUTINE GOES THROUGH THE TREE
*                                   STRUCTURE FINDING THE LONGEST PATH
*                                   OF EACH PROTECTION TYPE.
FINDLGSTPATH EQU   %
         LI,R7    K0
         LCI      K8
         STM,R7   SR1
         LW,D4    R1
         BAL,SR4  GETSIZ
ADDSIZ   AW,D1    R5
         AW,D2    R6
         AW,D3    R7
         CW,D4    R1
         BNE      ADDSIZ2
         BAL,SR4  COMLNK
         B        ADDSIZ1
ADDSIZ2  CW,R5    MAX00
         BLE      %+2
         STW,R5   MAX00
         CW,R6    MAX01
         BLE      %+2
         STW,R6   MAX01
         CW,R7    MAX10
         BLE      %+2
         STW,R7   MAX10
ADDSIZ1  AI,D4    SBLNKDIS
         LH,R4    *D4
         BNEZ     SBLNK
         LW,R4    *D4
         AND,R4   M16
         BNEZ     OVLNK
         AI,D4    KN1
BKLNK    BAL,SR4  COMLNK
         LW,R4    *D4                                                   727
         AND,R4   M16
         BEZ      CALCSIZ3
BKLNK2   SW,D1    R5
         SW,D2    R6
         SW,D3    R7
         LW,D4    R4
         AW,D4    R1
         BAL,SR4  GETSIZ
         AI,D4    SBLNKDIS
         LW,R4    *D4
         AND,R4   M16
         BNEZ     SUBSIZ
         AI,D4    3-4               ROM1DIS-SBLNKDIS
         B        BKLNK
SBLNK    LW,D4    R4
         AW,D4    R1
         BAL,SR4  GETSIZ
         B        ADDSIZ
COMLNK   CW,SR1   D1
         BGE      %+2
         STW,D1   SR1
         CW,SR2   D2
         BGE      %+2
         STW,D2   SR2
         CW,SR3   D3
         BGE      %+2
         STW,D3   SR3
         B        *SR4
OVLNK    BAL,SR4  COMLNK
SUBSIZ   SW,D1    R5
         SW,D2    R6
         SW,D3    R7
         B        SBLNK
GETSIZ   AI,D4    00DIS
         LW,R5    *D4
         SLS,R5   -15
         AI,D4    7-5               01DIS-00DIS
         LW,R6    *D4
         SLS,R6   -15
         AI,D4    9-7               10DIS,01DIS
         LW,R7    *D4
         SLS,R7   -15
         AI,D4    -9                -10DIS
         B        *SR4
*                                   THIS SECTION ROUNDS THE SIZES UP
*                                   TO FULL PAGES. IT ALSO PICKS UP
*                                   THE BIAS OR USES BACKGROUND
*                                   LOWER LIMIT AND CHECKS TO SEE
*                                   IF PROGRAM CAN FIT IN ANY
*                                   MACHINE. CONTROL SIZE TO SETBIAS
*                                   OR CALSIZ4.
CALCSIZ3 LI,R5    K3
         DO       MODE=0
         LW,R7    LOCWD
         CI,R7    X'80'
         BANZ     %+2
         FIN
         AW,SR1   FCOMSIZ
         DO       MODE+BPMLIB=1
         LI,R6    0
         LW,R7    Y4
         STS,R6   CORELIB
         LH,R7    CORELIB
         BLEZ     %+3
         SLS,R7   1
         AW,SR1   R7
         FIN
         LI,R7    K0
         LI,R4    DLOC-1
CALCSIZ5 LW,R6    SR1-1,R5
         AI,R6    K1FF
         AND,R6   MN9
         STW,R6   *R4,R5
         DO       MODE=0[BPMLIB=1
         AW,R7    R6
         FIN
         BDR,R5   CALCSIZ5
         DO       MODE=0[BPMLIB=1
         LW,R5    LOCCT
         LW,R4    BIASDIS,R5
         FIN
         DO       MODE=0
         BNEZ     %+2
         LW,R4    LOWLIMDS,R5
         AND,R4   MN9
         STW,R4   BIASDIS,R5
         STW,R4   LOADBAS
         CI,R7    K1FFFF
         BLE      CALCSIZ4
         QUIT     ER9,,R4
         ELSE
         B        CALCSIZ4
         FIN
ALLMEM   LI,R4    K3
*                                   ALLMEM EXPECTS THE SIZE OF THE
*                                   THESE SECTIONS IN R5, 6 AND 7. IT
*                                   ALLOCATES BUFFERS DOWN FROM THE
*                                   ADDRESS IN D4 IN THE ORDER 10,
*                                   01, 00. IT THEN ALLOCATES REL. DICT.
*                                   BUFFERS IN THE SAME ORDER IF
*                                   NECESSARY.
ALLMEM1  SW,D4    R4,R4
          STW,D4  *D3,R4
         SLS,D4   2
         XW,D4    *D3,R4
         BDR,R4   ALLMEM1
         AI,D3    46-43
         LW,R4    LOCWD
         CI,R4    K100
         BANZ     *SR4
         LI,R4    K3
ALLMEM2  LW,D2    R4,R4
         AI,D2    K7
         SLS,D2   -3
         SW,D4    D2
         STW,D4   *D3,R4
          SLS,D4   2
         XW,D4     *D3,R4
         BDR,R4   ALLMEM2
         B        *SR4
*                                   DLOC, PLOC & SLOC. THE THREE LOCATION
*                                   COUNTERS ARE INITIALIZED AND BUFFERS
*                                   ARE ESTABLISHED FOR LOADING THE
*                                   ROOT SEGMENT AND THE LONGEST
*                                   OTHER SEGMENT FROM THE TOP OF
*                                   MEMORY DOWNWARD.
CALCSIZ4 LW,R4    LOADBAS
         LCI      3
         LM,R5    DLOC              SAVE SIZES FOR GENERAL
         STM,R5   BUF               ALLOCATION SUMMARY.
         XW,R4    DLOC
         AW,R4    DLOC
         DO       MODE+BPMLIB=1
         XW,R4    SLOC              DCB NAME TABLE FOLLOWS
         AW,R4    SLOC                00 CSECTS FOR CP-V
         FIN
         XW,R4    PLOC
         AW,R4    PLOC
         DO       MODE+BPMLIB=1
         STW,R4   NXTAVPG
         FIN
         DO       MODE=0[BPMLIB=1
         XW,R4    SLOC
         AW,R4    SLOC
         STW,R4   NXTAVPG
         FIN
         LCI      3
         LM,R5    DLOC              SAVE BASE ADDRESSES FOR GENERSL
         STM,R5   BUF+3             ALLOCATION SUMMARY(OUTPUT BY 'ALL').
         LW,D4    R1
         BAL,SR4  GETSIZ
         LW,D4    TOPOMEM
         LI,D3    RSEG00-1
         BAL,SR4  ALLMEM
         CI,D4    0
         BGEZ     ALLCATE
         LCW,D4   D4
         QUIT     ER15,,D4
*                                   SPACE IS ALLOCATED FOR THE MAXIMUM
*                                   REF/DEF AND EXPR STACK FROM LOW
*                                   MEMORY UP, AND THE DOUBLE WORDS
*                                   ARE CALCULATED.
ALLCATE  EQU      %
         LW,R5    MAX00
         LW,R6    MAX01
         LW,R7    MAX10
         AI,D3    49-46             CSEG00-RREL00
         BAL,SR4  ALLMEM
         INT,R5   RFDFSTK+1
         AND,R5   M15
         LCW,R5   R5
         MSP,R5   RFDFSTK
         LW,R5    RFDFSTK
         AND,R5   M17
         STW,R5   RFDFSTK
         LW,R5    MAXRFDF
         AW,R5    RFDFBAS
         CI,R5    K1
         BANZ     %+2
         AI,R5    K1
         STW,R5   EXPRSTK
         AI,R5    K1
         STW,R5   EXPRBAS
         DO       MODE=1
         LW,R5    YFFFF8
         STW,R5   EXPRSTK+1
         LW,R5    RFDFBAS
         LW,R6    EXPRBAS
         AW,R6    MAXEXPR
         AI,R6    3                 MAKE ROOM FOR A NEW EXPRESSION
         CW,R6    RDPTRS            WILL WE CLOBBER THE SORT TABLE
         BL       %+2               NO
         DATA     FASTSRCH          YES, TURN IT OFF
         SW,R6    R5
         SLS,R6   2
         BAL,SR4  CHKLM
         FIN
         LW,R6    LOCWD
         CI,R6    K100
         BAZ      %+3
         LW,R5    CSEG00
         B        %+2
         LW,R5    CREL00
         DO       MODE=1
         SLS,R5   -2                WORD ADDRESS
         LW,R7    RDPTRS            WOULD WE RUN INTO THE SOT TABLE
         AW,R7    RDSYMS
         CW,R7    R5
         BG       EXMEM0            YUP
         LW,R7    TOPOMEM           DO WE HAV THE CORE
         SW,R7    R5
         SLS,R7   -9                THIS IS ONE SHORT
         CAL1,8   M:GDDL
         CW,R7    SR3
         BGE      EXMEM0            CANT DO IT
         CI,R6    X'4000'           WAS PAGE OPTION SPECIFIED
         BANZ     EXMEM0
         LW,R3    LOCCT
         LCI      2
         LM,R4    10,R3
         CD,R4    TXM:MON           FORCE EXTENDED MEMORY MODE IF
         BNE      EXMEM6            LOADING THE CP-V MONITOR
EXMEM0   EQU      %
         DO       MREF=1
         LI,D3    X'4000'           SET TREE'S 00LOC FOR
         LW,D4    M16               EVERY SUBLINK
         LW,R7    R1                OF ROOT = X'4000'
         INT,D1   SBLNKDIS,R7
         CI,D1    K0                SO CALCLOC IN EVL
         BEZ      EXMEM4            WILL USE PROPER
         AW,R7    D1                SEG BASE
EXMEM2   STS,D3   00DIS,R7          FOR INITIALIZING
         INT,D2   SBLNKDIS,R7       BIAS, LOC, & FBIAS.
         LW,R7    R1
         AW,R7    D2
         CI,D2    K0
         BNE      EXMEM2
         FIN
EXMEM4   BAL,SR4  XMEM
EXMEM6   EQU      %
         LW,R6    LOCWD
         ELSE
         SLS,R5   -2
         SW,R5    EXPRBAS
         CW,R5    M15
         BLE      %+2
         LW,R5    M15
         CW,R5    MAXEXPR
         BG       EXSTKPTR
*                                   IF NOT ENOUGH ROOM FOR THE EXPR.
*                                   STACK GO INTO EXTENDED MEMORY
*                                   MODE.
         BAL,SR4  XMEM
         LW,R6    LOCWD
         LI,R5    KN400              ASSUME TWO PAGES NEEDED FOR XMEM
         CI,R6    K100              IS THE 'ABS' BIT ON
         BAZ      %+2               NO
         LI,R5    KN200             YES, THEN ONLY ONE PAGE IS NEEDED
         AW,R5    TOPOMEM
         SW,R5    EXPRBAS
         CW,R5    M15
         BLE      %+2
         LW,R5    M15
EXSTKPTR AI,R5    K8000
         SLS,R5   16
         AI,R5    K8000
         STW,R5   EXPRSTK1
         FIN
*                                   TRACE REF/DEF STACK FOR S:OVRLY
*                                   IF IN BREF MODE
         CW,R6    BREFBIT
         DO       SD=0
         BAZ      IN2EXIT
         ELSE
         BAZ      SDKRD16
         FIN
         LW,R6    TXS:OVRL
         LW,R7    TXS:OVRL+1
         LW,R5    RFDFBAS
NEXTREF  LB,D2    *R5
         AW,R5    D2
         CW,R6    -2,R5
         BNE      NEXTREF
         CW,R7    -1,R5
         BNE      NEXTREF
         STW,R5   DOREFPTR
         DO       SD=1
SDKRD16  RES
         LCI      2
         PSM,R1   *R0
         LW,R2    SYMBOLTB          DO WE WANT INT SYMS
         BLZ      SDKRD9            NO
         LW,R1    LOCWD
         CI,R1    X'4000'           XMEM MODE
         BANZ     SDKRD20           YES
         LW,R2    CREL00
         CI,R1    X'100'            ABS
         BAZ      %+2               NO
         LW,R2    CSEG00
         SLS,R2   -2                FIRST NON AVIAL WORD NON XMEM
         B        SDKRD9
SDKRD20  LW,R2    TOPOMEM
         AI,R2    -X'200'
         DO       MODE=0
         CI,R1    X'100'            ABS
         BANZ     %+2               YES-JUST ONE PAGE XMEM BUFFER
         AI,R2    -X'200'           NO-TWO PAGE XMEM BUFFER
         FIN
SDKRD9   STW,R2   SYMTOP            FIRST NOW AVIALABLE WORD
         STW,R2   SYMBOLTB          INTIALIZE IST BUFFER ADDRESS
         STW,R2   BSEG2
         LCI      2
         PLM,R1   *R0
         FIN
IN2EXIT  EQU      %
         PLW,SR4  *R0
         B        *SR4
         PAGE
*                                   EXTENDED MEMORY MODE CHECKS TO
*                                   SEE THAT THERE IS AT LEAST 1 PAGE
*                                   (2 IF NOT ABS) ABOVE THE EXPRSTACK.
*                                   IF SO IT OPENS M:DIC  TO THE
*                                   EXTENDED MEMORY FILE "ID X". IT
*                                   THEN REALLOCATES THE BUFFERS TO
*                                   YIELD ONLY THOSE WITH EACH EQUAL
*                                   TO THE GREATER OF THE TWO PREVIOUSLY
*                                   ALLOCATED FOR EACH PROTECTION TYPE
*                                   (ROOT AND OTHER SEGMENT). THIS IS
*                                   TO ALLOW PUTTING THE SEGMENTS
*                                   TOGETHER AT THE END OF LOADING.
XMEM     PSW,SR4  *R0
         DO       MODE=1
         CI,R6    X'10'
*E*      ERROR:   0200-2D
*E*      MESSAGE: LIB. LOAD MODULE TOO BIG - CANNOT USE EXTENDED
*E*               MEMORY.
*E*      DESCRIPTION: EXTENDED MEMORY MODE HAS BEEN ENTERED BECAUSE
*E*               THE LOAD MODULE IS TOO LARGE TO BE FORMED IN ONE PIECE.
*E*               THIS CANNOT BE DONE FOR LIBRARY LOAD MODULES.
         QUIT     X'2D',BAZ         CANNOT FORM LIB LMN IN XMEM
         LW,R6    LOCCT
         LW,D4    3,R6              ROMT POINTER
         SW,D4    2,R6              TREET POINTER
         CI,D4    X'B00'            TREE SIZE FOR 256 SEGS
         BLE      XMEM1
         DH,D4    Y000B
*E*      ERROR:   0200-30
*E*      MESSAGE: PAGED LMN MUST NOT HAVE MORE THAN 256 SEGMENTS.
*E*      DESCRIPTION: THE LOAD MODULE IS SO BIG THAT IT WOULD REQUIRE
*E*               MORE THAN 256 SEGMENTS TO BUILD IT IN EXTENDED MEMORY
*E*               MODE.
*E*      REGISTERS: R15 CONTAINS THE NECESSARY NUMBER OF SEGMENTS.
         QUIT     X'30',,D4         NO MORE THAN 256 SEGS ALLOWED
XMEM1    EQU      %
         CAL1,1   SETXERR
         INT,D4   00DIS,R1          BASE ADDR OF ROOT'S DATA FOR CP-V
         SLS,D4   -8
         ELSE
         LW,D4    TOPOMEM
         SLS,D4   -9
         FIN
         AW,D4    Y03
         STW,D4   XMKEY             KEY OF INITIAL CORE IMAGE RECORD
         DO       MODE=0
         STW,D4   XMRKEY
         FIN
         LW,R6    TOPOMEM
         DO       MODE=1
         AI,R6    1
         LI,R7    KN200
         LI,D4    0
         STW,D4   *R6,R7            CLEAR FIRST PAGE IN XMEM BUFFER
         BIR,R7   %-1
         ELSE
         AI,R6    XMPL-XMPLX        ALLOW ROOM FOR M:DIC OPEN FPT
         LW,R7    EXPRBAS
         AW,R7    MAXEXPR
         CW,R6    R7                 WILL EXPRESSION STACK FIT
         BGE      XMEM0
         SW,R7    R6
         QUIT     ER17,,R7
XMEM0    EQU      %
         FIN
         DO       MODE~=0
         LI,R7    X'4100'           SET XMEM AND ABS BITS
         STS,R7   LOCWD
         LW,R6    LOCCT
         STS,R7   *R6
         ELSE
         LI,R7    X'4000'
         STS,R7   LOCWD
         LI,R7    XMPLX-XMPL
         LW,R4    XMPL-1,R7
         STW,R4   *R6,R7
         BDR,R7   %-2
         LW,R7    USID
         STS,R7   10,R6
         CAL1,1   1,R6
         FIN
         LI,R5    XMESS
         CAL1,2   PRINT
         LW,D4    R1
         BAL,SR4  GETSIZ
         CW,R5    MAX00
         BGE      %+2
         LW,R5    MAX00
         CW,R6    MAX01
         BGE      %+2
         LW,R6    MAX01
         CW,R7    MAX10
         BGE      %+2
         LW,R7    MAX10
         LW,D4    TOPOMEM
         LI,D3    RSEG00-1
         BAL,SR4  ALLMEM
         CI,D4    0
         BGEZ     XMEM2
         LCW,D4   D4
*E*      ERROR:   0200-18
*E*      MESSAGE: NOT ENOUGH ROOM TO CONCATENATE XMEM PAGES
*E*      REGISTERS: R15 HOLDS THE NUMBER OF WORDS EXCEEDING AVAILABLE
*E*               CORE.
         QUIT     ER18,,D4
XMEM2    EQU      %
         DO       MODE=0
         AW,R6    R7
         AW,R6    R5
         CI,R6    K1000        ASSURE UNIQUE PAGES BETWEEN RELDIC AND LM
*                                   IF SUM OF MAX SIZES<8K,NO OVERLAP
*                                   POSSIBLE FOR REL.DIC. PAGE.
         BGE      XMEM1
         LW,R4    RSEG10
         AND,R4   M11
         AI,R4    4
         LCW,R4   R4
         AWM,R4   RREL00
         AWM,R4   RREL01
         AWM,R4   RREL10
XMEM1    LCI      6
         ELSE
         LCI      3
         FIN
         LM,R5    RSEG00
         STM,R5   CSEG00
         PLW,SR4  *R0
         B        *SR4
         DO       MODE=0
         REF      XMEMEOFX
XMPL     GEN,8,24 X'14',M:DIC
         DATA     X'CF000001'                                           727
         DATA     XMEM3X,XMEMEOFX,10,2,2,8
         DATA     X'01000101',X'030000E7',X'02010002'                   727
         RES      2
XMPLX    EQU      %
         FIN
Y03      DATA     X'03000000'
Y0401    DATA     X'04010000'
         DO       MODE=1
YFFFF8   DATA     X'FFFF8000'
M:GDDL   GEN,8,24 X'1B',0
         BOUND    8
TXM:MON  TEXTC    'M:MON'
SETXERR  GEN,8,24 6,M:LM
         DATA     X'80000000'
         DATA     XMEM3X
         FIN
XMESS    TEXTC    ' EXTENDED MEMORY MODE.'
         END

