         DEF SEGLD:
SEGLD:   EQU      %
*        704763   SIGMA 5/7         BPM M:SEGLD
         SYSTEM   UTS
         DEF      SEGLD,SEGLDSZ
*
*                                   SEGLD
         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 USED BY MONITOR.
K0       EQU      0
K1       EQU      1
K2       EQU      2
K3       EQU      3
K4       EQU      4
K5       EQU      5
KA       EQU      10
KB       EQU      11
KF       EQU      15
         PAGE
TREEBAD  EQU      1
CIRCTREE EQU      2
BIGD     EQU      3
BIGP     EQU      4
BADB     EQU      5
SANSPAGE EQU      6
SADPAGE  EQU      7
         REF      UH:FLG
         REF      J:TREE,M17,MSRRDWT,Y4,Y8,Y2,Y6,T:ABORTM
         REF      SPPBASE,DELTAGO,XN2
         REF      YC
         REF      J:ASSIGN
         REF      TRAPEXIT,OPNSEG
         REF      J:RNST
         REF      MASKS
*
         REF      YFFF
         REF      S:CUN
         REF      XFFFF,J:DUL,J:DLL,J:PUL,J:PLL,J:BUP,JB:PCP
         REF     JB:LMAP,FPMC,T:GNVPI,T:RVPI,JX:CMAP
K11      DATA,2   11,0
*
*
FNDNAM3  AW,6     2
SEGLD    EQU      %
*  FIND SGLD DCB IN NAME TABLE    6= BEG OF TABLE
         AI,6     1
         LB,1     *6                CNT OF NXT NAME OR END OF TABLE
         BEZ      MSEGLDEX          NOT IN TABLE - RETURN TO CAL+1
         LW,2     1
         SLS,2    -2
         AI,2     1
         LB,4     *6,1
         CB,4     NSGLDDCB,1
         BNE      FNDNAM3
         BDR,1    %-3
         LI,10    0                 INITIALIZE SEG COUNT
*  GET SGLD DCB
         PUSH     2,9               MAKE HOLE IN STACK
         LW,6     *2,6
         LW,R1    Y002
         CW,R1    0,R6
         BANZ     NOOPN
         PSW,10   TSTACK            ZAP PARAMETER WORD OF FPT
         LW,R7    TSTACK
         AI,R7    K1
         LCI      6
         LM,D4    OPN
         PSM,D4   TSTACK
         LI,SR1   20
         OVERLAY  OPNSEG,0
         PULL     6,D4
         PLW,7    TSTACK            ADJUST STACK
         LB,SR3   SR3
         BNEZ     EREXIT
*                   MOVE MEMORY OF 'IN' SEGMENTS FROM BIT 0,WD 3 TO
*                   BIT 1,WD3.
NOOPN    EQU      %
         LW,R1    J:TREE
         LW,R2    J:TREE
         AI,R1    K4
         AW,R2    0,R2
         SLD,2    -17               CHECK THAT TREE IS
         LB,2     3                 IN CONTIGUOUS PAGES OF USER CORE
         CW,2     J:PLL             MUST BE IN PROCEDURE
         BLE      %+4               ALL DONE OR NEG SIZE(GETS IT SOON)
         LC       JB:LMAP,2         USER MUST HAVE PAGE BELOW 01
         BCR,15   BADTREE           AND ABOVE X'F'
         BDR,2    %-4
         LI,2     0                 RESTORE END ADRESS
         SLD,2    17
*  RESET BITS 0&1, UNLESS 0 IS SET-THEN RESET 0 & SET 1
*  R1 PTS TO WD3 OF ROOT SEG IN TREE. R2 PTS TO TREE END+1
MEMLOP   INT,D1   0,R1
         BCR,8    %+2
         OR,D2    Y4
         STW,D2   0,R1
         AI,10    1                 COMPUTE #SEGS FOR CIRCTREE CHECKS
         AI,R1    KB
         CW,R1    R2
         BL       MEMLOP
*                 FILE OPEN, FIND NAMED SEGMENT
         STW,10   *TSTACK           SAVE SEG COUNT ON TREE
         LW,R1    J:TREE
         AI,R1    K1
*  FIND NAME IN TREE  R1= 1ST ENTRY OF TREE WD0  R2= TREE END+1
KEYLOOP  AI,R1    KB
         CW,R1    R2
         BGE      EREXIT
         LB,R4    *R1
         CB,R4    *SR2
         BNE      KEYLOOP
         LB,D1    *R1,R4
         CB,D1    *SR2,R4
         BNE      KEYLOOP
         BDR,R4   %-3
*  SR2 PTS TO NAMED SEGMENT,IE ENTRY IN TREE
         LW,SR2   R1
*                 SEGMENT FOUND, ADDRESS OF NAME IN SR2
*                 CHECK IF SEG. IN, MARK IT IN.
         LW,D2    YC
         CW,D2    3,R1              IS REQ SEG IN CORE
         BAZ      CHKSEG            NO, NEITHER ARE SUBLINK
         LI,3     KA                TREE TABLE
         AI,1     -1
NXTBRNCH STS,D2   4,R1              MARK SEG IN
         BDR,10   %+2
         B        CIRCSEG           CIRCULAR TREE
         LH,R1    *R1,R3            FLINK
         BEZ      CHKSEG1           NONE LEFT
         BLZ      BADTREE
         AW,R1    J:TREE
         CW,1     2                 OFF THE END YET
         BGE      BADTREE           YES
         CW,D2    4,R1
         BANZ     NXTBRNCH
         LI,R3    9
         LH,1     *1,3              BACK LINK TO
         BLZ      BADTREE
         CW,1     2                 END OF TREE CHECK
         BGE      BADTREE           BAD LINK
         AW,R1    J:TREE
         AI,1     1                 POINT TO 1ST WORD OF TREE ENTRY
CHKSEG   EQU      %
*
*        R1 CONTAINS THE POINTER TO THE TREE TABLE ENTRY
*        FOR THE HIGHEST SEGMENT WHICH IS TO REMAIN IN CORE
*
*        *TSTACK HAS SEG# COUNT
*
*        *TSTACK - 1  WILL HAVE TWO HALFWORDS WITH HIGHEST
*                     PAGE OF ALLOCATED PROCEDURE AND DATA
*        IT IS SET UP BY MEMSET0
*
         LI,2     5                 SET UP DATA ALLOCATION
         LI,3     J:DLL             LIMITS DWD
         BAL,11   MEMSET0
         LI,2     7
         LI,3     J:PLL             NOW DO PROCEDURE
         BAL,11   MEMSET0
*
*
*        AT THIS POINT, USER MEMORY IS ALLOCATED UP TO
*        THE HIGHEST PAGE OF DATA AND PP OF THE HIGHEST
*        SEGMENT TO BE READ INTO CORE (OR WHICH IS ALREADY IN)
*
CHKSEG1  EQU      %
         LW,D2    Y8
         LW,R1    SR2
         STS,D2   3,R1
         INT,D2   3,R1
         BCS,4    SEGIN
*  THE ABOVE SETS BIT 0 (OF WD3).  THEN IF BIT 1 IS SET INDICATING
*  SEG IS IN WE SKIP TO & GO FOR NXT BACK PATH. SR2 PTS TO APPRO SEG
         LI,R1    K3
         BAL,SR4  RDSEG
         LI,R1    K5
         BAL,SR4  RDSEG
         LI,0     0
         LW,1     Y2
         STS,0    J:ASSIGN
SEGIN    LW,R1    SR2
         INT,D2   3,R1
         AI,D2    0
         BNE      NXTSEG
         PULL     4,7
         LW,4     S:CUN
         LH,15    UH:FLG,4
         OR,15    X10               SET PPSWAP
         STH,15   UH:FLG,4
         CI,15    DELA              DELTA ASSOCIATED?
*
*
         B        SEGLDX1
*
*CHANGE THIS INSTRUCTION TO 'BAZ   SEGLDX1' WHEN
*DELTA IS READY TO HANDLE BREAK POINTS IN
*OVERLAYED PROGRAMS.     LLF
*
*
         CI,15    TIC               BUT IF IT'S CMD PROC SEGLD-ING,
         BANZ     SEGLDX1           ---> DON'T GO TO DELTA.
         LW,1     TSTACK            BUMP THE USER ADDRESS
         AI,1     -17               IN TSTACK ENVIRONMENT
         AND,1    XN2               TO BE
         MTW,1    0,1               CALL+1.
         LI,10    SPPBASE+X'F'      R10= DELTA'S SEGLD ENTRY POINT.
         DESTRUCT DELTAGO         -----> GO TO DELTA.
SEGLDX1  RES      0
         LI,11    TRAPEXIT
         REF      Y008,DEBUGSEG
         CW,8     Y008              IS THERE A REPLACED INSTRUCTION
         BAZ      T:SELFDESTRUCT
         AI,7     -3                YES, TRUNDLE INTO DEBUG CAL EXIT
         OVERTO   DEBUGSEG,6
NXTSEG   LW,SR2   J:TREE
         AI,SR2   K1
         AW,SR2   D2
         MTW,-1   *TSTACK           COUNT SEGMENTS
         BGZ      CHKSEG1           NO LOOP YET
         B        CIRCSEG           TOO MANY
         SPACE    5
*                 READ IN PROTECTION TYPE AND SET PROTECTION ACCORDINGLY
*                   R1=3, 00  -  R1=5, 01  -  R1=7, 10
RDSEG    EQU      %
         MTB,1    *SR2
         LB,R2    *SR2
         STB,R1   *SR2,R2
         AI,R1    K2
         LW,D3    *SR2,R1
         INT,D4   D3
         SLS,D3   -13               BYTE COUNT
         AND,D3   =X'7FFF8'
         BEZ      RDSEGX1           NOTHING TO READ
         SLS,D4   1                 BUF ADDR
         LW,7     D4
         SLS,7    -9                FORM PAGE NUMBER
*        WORD OF USER CORE LIMITS IS AT *TSTACK-1
*        SO 5=00=>-1 ; 7=01=>-2
         AI,1     -3                00=2,01=4
         SLS,1    -1                00=1,01=2
         LCW,1    1
         CLM,7    J:DLL+2,1         J:DLL=J:PLL+2
         BCS,9    BADBUF            NO, ABORT HIM
         LW,2     14                NOW CHECK END OF BUFFER
         AI,2     -1
         SLS,2    -2
         AW,2     15                LAST WORD OF BUFFER
         SLS,2    -9
         CH,2     *TSTACK,1         CHECK PAGE # TO BE IN USER
         BG       BADBUF
         STH,7    2                 SAVE BUF END PAGE#S IN STACK
         XW,D3    D4
         PUSH     2
RD1      EQU      %
         PUSH     SR4
         LW,R7    TSTACK
         LI,SR1   16
         LW,D2    READ
         LW,R0    SR2
         LW,1     Y6
         STS,1    J:ASSIGN          SUPPRESS BUF LIMIT CHKS
         LI,R1    K0
         PUSH     7,D2
         BAL,SR4  MSRRDWT
         PULL     7,D2
         PULL     SR4
         LB,SR3   SR3
         BEZ      RD2
         CI,SR3   X'43'
         BE       RD4               NO SUCH KEY
         CI,SR3   X'07'
         BNE      EREXIT            LOOKS LIKE A SERIOUS ERROR
         B        RD2               IGNORE LOST DATA
RD4      MTB,0    2
         BNEZ     RD25              MISSING PAGED PAGE
         LH,7     2                 TRY PAGED TYPE; GET 1ST PAGE#
         LW,8     SR2
         AI,8     -1
         SW,8     J:TREE
         DH,8     K11               FORM ENTRY # OF SEGMENT
         CI,8     255
         BG       MANYSEG           TOO MANY PAGED SEGMENTS
         STH,8    7
         SH,2     2                 COMPUTE # OF RECORDS TO READ
         AI,2     1                 =END PAGE - START PAGE + 1
         SLS,2    24                SET FLAG AND CONUNTER
         MTB,3    7
         XW,7     *0
         PUSH     7
         LW,3     14                SAVE LAST PARTIAL SIZE
         SLS,3    2                 BA(START)
         AW,3     15                BA(END)
         AND,3    MASKS+11          DISP INTO PAGE
         BNEZ     RD1
         LI,3     X'800'            0 => FULL PAGE READ
         B        RD1
RD2      MTB,0    2
         BEZ      RD3               NOT AN 85 TYPE MODULE
         AI,2     X'100'            SET READ OCCURRED FLAG
RD25     MTW,1    *0                INC PG NR PORTION OF KEY
         STB,2    J:RNST            CLEAR ERROR FROM 43 ABN
         AI,D3    X'200'            INCR BUF
         AND,D3   =X'1FE00'
         LI,15    2048              SET BUFFER SIZE
         MTB,-1   2                 COUNT DOWN # PAGES TO READ
         LB,7     2                 IF LAST PAGE, USE PARTIAL BUFFER
         BDR,7    RD1               NOT LAST
         STW,3    15                SET LAST READ SIZE
         BGZ      RD1
         AI,2     0                 CHECK IF AT LEAST ONE READ.
         BLE      EREXIT            ERROR IF NOT.
         PULL     2
         STW,2    *0
RD3      EQU      %
         PULL     12                GET RECORD PAGE LIMITS
         LI,2     X'60000'          IF NO DEBUG PAGE, WE ARE DONE
         CW,2     J:ASSIGN
         BAZ      RDSEGX1
         SLD,12   -16               MAKE DWD FOR CLM
         SLS,13   -16
         PUSH     3,5
         LW,7     YFFF
         LI,R2    10
         INT,R2   *SR2,R2
MODLOP   CI,R2    K0
         BLE      RDSEGX
         AI,R2    -2
         LD,D3    *R2,R3
* GO THRU CLOBBER TBL, SAVING REPLACED INST IN PLIST IF DB CAL
         LI,6     X'1FFFF'          EXTRACT ADDRESS
         AND,6    D3                OF CHANGE
         SLS,6    -9                ITS PAGE #
         CLM,6    12                IS IT IN THIS RECORD (MAYBE)
         BCS,9    MODLOP            NO, JUST TRY NEXT ONE
         LW,6     D4
         CS,6     DBCAL
         BNE      MD4
         CW,D4    *D3               HAS INST BEEN REPLACED
         BE       MODLOP
         XW,D4    *D3
         AND,6    M17
MD3      EQU      %
         AI,6     5                 CHECK THAT FPTS ARE IN DEBUG PAGE
         LW,5     6
         SLS,5    -9
         CW,5     J:PUL
         BNE      MODLOP
         STW,15   0,6               SAVE REPLACED INSTRUCTION IN ALL OF THEM
         LW,6     -5,6              FLINK
         AND,6    M17
         BNEZ     MD3
         B        MODLOP
MD4      EQU      %
         STW,D4   *D3
         B        MODLOP
RDSEGX   EQU      %
         PULL     3,5
RDSEGX1  MTB,15   *SR2
         B        *SR4
         SPACE    5
MEMSET0  EQU      %                 GET CTL WD FROM TREE AND SET MEM
         LW,12    *1,2              (2) IS: 00=5,01=7
         SLD,12   -16
         SLS,13   -16
         AW,12    13                END ADDRESS
         AI,12    -1
         SLS,12   -8                END PAGE#
         CLM,12   *3                IS IT LEGAL
         BCS,9    BIG
         AI,2     -3                SET WORD IN STACK
         SLS,2    -1
         LCW,2    2
         STH,12   *TSTACK,2
         LW,2     12                PUT END PAGE IN 2 FOR MEMSET
         DEF      MEMSET
MEMSET   EQU      %
*
*        MEMSET GETS ALL PAGES BETWEEN LOWER LIMIT IN *R3
*        AND THE PAGE SPECIFIED IN R2; RELEASING ALL PAGES
*        BETWEEN (R2)+1 AND THE UPPER LIMIT *R3+1
*
         PSW,11   TSTACK            SAVE RETURN
         LCI      2
         PSM,0    TSTACK
         PSW,6    TSTACK
         LI,6     1                 NUMBER OF PAGES FOR M
         LW,7     *3                LOWER LIMIT
         LI,1     0                 INDICATING GET OPERATION
         BAL,10   MEM1
         LI,1     1                 INDICATING RELEASE
         LW,2     *3,1              GET WHERE TO STOP
         LI,10    X'60000'
         CW,10    J:ASSIGN          IS THERE DEBUG PG
         BCR,4    %+4               NO PAGE
         CI,3     J:PLL             ARE WE DOING PROCEDURE
         BNE      %+2               NO, DEBUG PAGE IS IN PROCEDURE
         AI,2     -1                YES, DONT RELEASE IT
         CW,2     7                 BUMP OVER PAGE YET
         BL       MEMDONE           YEP, QUIT
         BAL,10   MEM1
MEMDONE  EQU      %
         PLW,6    TSTACK
         LCI      2
         PLM,0    TSTACK
         PLW,11   TSTACK
         B        *11               RETURN
*
*
MEM1     EQU      %
        LOAD,15 JX:CMAP,7
         CI,D4    FPMC              HAVE THE PAGE
         EXU      ACTION,1          TREAT APPROPRIATELY
         LCI      3
         PSM,1    TSTACK
         MTB,1    JB:PCP              TO PASS MM MAX PAGES TEST
         BAL,11   GETFREE,1         GET/RELEASE ENTRYS
         BCS,15   NOPAGE            DIDN'T DO OPERATION
         LCI      3
         PLM,1    TSTACK            RESTORE
         MTB,-1   JB:PCP            RESET COUNT
MEM2     EQU      %
         AI,7     1
         CW,7     2                 FINISHED
         BLE      MEM1              NO
         B        *10               YES, RETURN
*
*
ACTION   BNE      CHKSAD
         BE       MEM2
*
GETFREE  B        T:GNVPI
         B        T:RVPI
*
*
*
CHKSAD   EQU      %
         LB,D4    JB:LMAP,7         GOT WITH SAD CAL
         CI,D4    1
         BNE      MEM2              NO, ALL OK
         LI,14    SADPAGE
         B        ABORTIT
*
*
MANYSEG  LI,14    8
         B        ABORTIT           TOO MANY PAGED SEGMENTS
*
*
NOPAGE   EQU      %
         MTB,-1   JB:PCP            RESET COUNT
         LI,14    SANSPAGE
         B        ABORTIT
*
*
CIRCSEG  EQU      %
         LI,14    CIRCTREE
         B        ABORTIT
*
*
BIG      EQU      %
         LI,14    BIGP
         CI,3     J:PLL             IS THIS RIGHT
         BE       ABORTIT           YES
         LI,14    BIGD
         B        ABORTIT
*
*
BADTREE  EQU      %
         LI,14    TREEBAD
         B        ABORTIT
*
*
EREXIT   EQU      %
         LI,14    0
         B        ABORTIT
*
*
MSEGLDEX EQU %
         PULL     6
         PULL     7
         DESTRUCT TRAPEXIT
*
BADBUF   EQU      %
         LI,14    BADB
ABORTIT  EQU      %
         SCS,14   -8
         AI,14    X'B1'
         B        T:ABORTM
*
OPN      DATA     X'F000001',10,2,2,1
         DATA     X'10000'
Y002     DATA     X'200000'
READ     DATA     X'3C000010'
DBCAL    DATA     X'04300000'
         BOUND    8
NSGLDDCB TEXTC    'M:SGLD'
SEGLDSZ  EQU      %-SEGLD
         END

