; ************************** AMUS Program Label ******************************
; Filename: PPNS.M68                                        Date: 03/25/92
; Category: UTIL         Hash Code: 432-131-151-562      Version: 1.0(103)
; Initials: ULTR/AM      Name: DAVID PALLMANN
; Company: ULTRASOFT CORPORATION                   Telephone #: 5163484848
; Related Files: None
; Min. Op. Sys.: AMOSL 1.0                     Expertise Level: BEG
; Special:
; Description: Displays PPNs and ERSATZ for ppns a screen at a time.
; Now works with 2.x extended devices and traditional devices.
;
; ****************************************************************************
;*; Updated on 25-Mar-92 at 1:38 AM by James A. Jarboe I V; edit time: 1:31:49
;****************************************************************************
;*                                                                          *
;*                                  PPNS                                    *
;*                           PPN display utility                            *
;*                                                                          *
;****************************************************************************
;Copyright (C) 1990 UltraSoft Corporation.  All Rights Reserved.
;
;Written by: David Pallmann
;Modified by: James A. Jarboe I V
;
;Edit History:
;1.0(100)  24-Aug-90  created. /DFP
;1.0(101)  27-Aug-90  fix bug where count got reset if MFD was more than
;                     one block long.  Thanks to Ami Bar-Yadin for finding
;                     and reporting this. /DFP
;1.0(102)  19-Nov-90  fix bug where device wasn't taken into account. /DFP
;1.0(103)  24-Mar-92  Now works on extended disks. /JAJ
;                     Fixed bug where 377,377 was displayed on traditional
;                      format disks on AMOS 2.x which is a delete directory.
;

       VMAJOR  =1
       VMINOR  =0
       VSUB    =0
       VEDIT   =103.
       VWHO    =0

       LONG.SYMBOL.1   =1
       LONG.SYMBOL.2   =2
       IF EQ,LONG.SYMBOL.1-LONG.SYMBOL.2,ASMERP "?Can't assemble - the 2.x assembler is required"

       SEARCH  SYS
       SEARCH  SYSSYM
       SEARCH  TRM

       MAXACT  =1000.                  ; max accounts we can handle

       .OFINI
       .OFDEF  FLAGS,  2               ; flags
               F$OUT   =1              ;   PPNs not in sequence
               F$EXT   =2              ;   Extended directory.         [103]
               F$20X   =4              ;   AMOS 2.x                    [103]
       .OFDEF  ROW,    1               ; cursor row
       .OFDEF  COL,    1               ; cursor column
       .OFDEF  DISK,   D.DDB           ; disk DDB
       .OFDEF  BUFFER, 8.              ; output buffer
       .OFDEF  COUNT,  2               ; count of accounts
       .OFDEF  PPNTBL, 2*MAXACT        ; table of PPNs
       .OFDEF  ERZTBL, 4*MAXACT        ; table of ersatz names
       .OFSIZ  MEMSIZ

DEFINE  CURSOR  ROW,COL
       MOVB    ROW,D1
       ROLW    D1,#8.
       MOVB    COL,D1
       TCRT
       ENDM

DEFINE  CRT     N
       MOVW    #-1_8.+^D<N>,D1
       TCRT
       ENDM

START:  PHDR    -1,0,PH$REE!PH$REU      ; program header
       GETIMP  MEMSIZ,A5               ; allocate memory for variables

CMDLIN: BYP                             ;
       LIN                             ;
       JEQ     USAGE                   ;
       FSPEC   DISK(A5)                ;
       INIT    DISK(A5)                ;
;;;     TSTW    DISK+D.FMT(A5)          ;                               [103]
;;;     JNE     EXTENDED                ;                               [103]
       CMPB    -1(A2),#':              ;
       JNE     CMDERR                  ;
       MOV     2, D2                   ; Get AMOS version number.      [103]
       SWAP    D2                      ; Swap ends.                    [103]
       ROLW    D2, #8.                 ; Get VMAJOR.                   [103]
       CMPB    D2, #2.                 ; Is this 2.x?                  [103]
       BLO     5$                      ; No..never mind.               [103]
       ORW     #F$20X, FLAGS(A5)       ; Yes..set 2.x                  [103]
5$:
       INIT    DISK(A5)                ;
       MOV     #1,DISK+D.REC(A5)       ;

       CLRW    COUNT(A5)               ;                               [101]
       LEA     A3,PPNTBL(A5)           ;                               [101]
       LEA     A4,ERZTBL(A5)           ;                               [101]

       TSTW    DISK+D.FMT(A5)          ; Is this an extended device?   [103]
       BEQ     10$                     ; No..read traditional.         [103]
       CALL    EXTENDED                ; Yes..read extended format.    [103]
       BR      DISPLAY                 ; Now display data.             [103]
10$:    CALL    REDMFD                  ; No..read traditional style.   [103]


DISPLAY:
       CLR     D5                      ;
       MOVW    COUNT(A5),D5            ;
       BNE     10$                     ;
       TYPECR  %Disk is empty and contains no accounts
       CRLF                            ;
       EXIT                            ;
10$:    CRT     0                       ;
       CRT     11.
       TYPESP  Accounts on             ;
       CRT     12.
       LEA     A1,DISK+D.DEV(A5)       ;
       LEA     A2,BUFFER(A5)           ;
       UNPACK                          ;
       CLR     D1                      ;
       MOVW    DISK+D.DRV(A5),D1       ;
       DCVT    0,OT$MEM                ;
       MOVB    #':,(A2)+               ;
       CLRB    @A2                     ;
       TTYL    BUFFER(A5)              ;
       MOVB    #3,ROW(A5)              ;
       MOVB    #1,COL(A5)              ;

       LEA     A3,PPNTBL(A5)           ;
       LEA     A4,ERZTBL(A5)           ;

DI.PPN: CMPW    D5,COUNT(A5)            ;
       BEQ     10$                     ;
       CMMB    1(A3),-1(A3)            ;
       BEQ     10$                     ;
       CALL    NEXT.ROW                ;

10$:    CURSOR  ROW(A5),COL(A5)         ;
       CALL    DIM
       TYPE    [                       ;
       CALL    BRIGHT
       PRPPN   @A3                     ;
       CALL    DIM
       TYPE    ]                       ;
       CALL    BRIGHT
       ADD     #2,A3                   ;

DI.ERZ: MOV     (A4)+,D4                ;
       BEQ     DI.NXT                  ;

       MOVB    COL(A5),D0              ;
       ADD     #10.,D0                 ;
       CURSOR  ROW(A5),D0              ;

       LEA     A1,-4(A4)               ;
       LEA     A2,BUFFER(A5)           ;
       UNPACK                          ;
       UNPACK                          ;
50$:    CMPB    -(A2),#40               ;
       BEQ     50$                     ;
       INC     A2                      ;
       MOVB    #':,(A2)+               ;
       CLRB    @A2                     ;
       TTYL    BUFFER(A5)              ;

DI.NXT: CALL    NEXT.ROW                ;
       SOB     D5,DI.PPN               ;

NOTES:
       MOVW    FLAGS(A5), D7           ; Set flags.                    [103]
       ANDW    #F$EXT, D7              ; Extended directory?           [103]
       BEQ     10$                     ;  No..never mind.              [103]
       CURSOR  #1, #56.                ; Set cursor.                   [103]
       TYPE    <Extended Directory>    ; Tell em.                      [103]
       BR      20$                     ; Then quit..                   [103]
                                       ;  Extendeds are always sorted. [103]

; Traditional directories can be out of sequence. They are not sorted
; using the PSORT routine. The thought is, if the traditional directory
; is out of sequence, inform the user so that they can do a MFDSEQ on that
; device if it is out of sequence. Extended devices ppns can be found even
; if not sequenced thus we sort them if they are out of sequence for good
; display.

10$:
       MOVW    FLAGS(A5),D7            ;
       ANDW    #F$OUT,D7               ;
       BEQ     20$                     ;
       CURSOR  #1,#56.                 ;
       TYPE    PPNs are not in sequence
20$:
       CURSOR  #24.,#1                 ;
       EXIT                            ;

REDMFD: READ    DISK(A5)                ;

       MOV     DISK+D.BUF(A5),A0       ;

       MOV     #63.,D0                 ;
10$:    TSTW    @A0                     ;
       BEQ     20$                     ;
       CMPB    1(A0), #^O377           ; Is project number 377?        [103]
       BNE     12$                     ; No..never mind                [103]
       CMPB    @A0, #^O377             ; Is Programmer number 377?     [103]
       BNE     12$                     ; No..never mind.               [103]
       MOVW    FLAGS(A5), D7           ; Get flags.                    [103]
       ANDW    #F$20X, D7              ; Is this AMOS 2.x?             [103]
       BNE     18$                     ; Yes..then bypass 377,377.     [103]

12$:    INCW    COUNT(A5)               ;
       MOVW    @A0,D3                  ;

       CMPW    COUNT(A5),#1            ;
       BEQ     15$                     ;
       CMPW    D3,-2(A3)               ;
       BHIS    15$                     ;
       ORW     #F$OUT,FLAGS(A5)        ;

15$:    MOVW    D3,(A3)+                ;
       CALL    GET.ERSATZ              ;
       MOV     D4,(A4)+                ;
18$:    ADD     #10,A0                  ;
       SOB     D0,10$                  ;

20$:    CLR     D1                      ;
       MOV     DISK+D.BUF(A5),A0       ;
       MOVW    504.+MF.NXT(A0),D1      ;
       BEQ     30$                     ;
       MOV     D1,DISK+D.REC(A5)       ;
       BR      REDMFD                  ;

30$:
       RTN


;**************
;*  EXTENDED  *
;**************
;Function:      Get extended directory PPns.

EXTENDED:
;;;     TYPECR  ?PPNS does not support extended disks                   [103]
;;;     CRLF                            ;                               [103]
;;;     EXIT                            ;                               [103]

       ORW     #F$EXT,FLAGS(A5)        ; Set extended directory.       [103]
       DIRACC  DISK(A5),#DA$INI!DA$NEW ; Get directory access          [103]

10$:
       DIRSCH  DISK(A5),#DS$DIR        ; Search for next PPn.          [103]
       TSTW    D6                      ; End of entries?               [103]
       BMI     30$                     ; Branch if yes                 [103]
       INCW    COUNT(A5)               ; Bump count.                   [103]
       MOVW    D.FIL+DISK(A5), (A3)+   ; Save ppn.                     [103]
       CMPW    COUNT(A5),#1            ; First ppn?                    [103]
       BEQ     10$                     ; Yes..bypass test.             [103]
       CMMW    -2(A3),-4(A3)           ; Greater than last PPN?        [103]
       BHIS    20$                     ; Yes..never mind.              [103]
       ORW     #F$OUT,FLAGS(A5)        ; No..set not sequenced.        [103]
20$:
       BR      10$                     ; Next ppn.                     [103]
30$:
       MOVW    COUNT(A5),D0            ; Get ppn count.                [103]
       BEQ     99$                     ; No ppns.                      [103]
       MOVW    FLAGS(A5), D7           ; Get flags.                    [103]
       ANDW    #F$OUT, D7              ; Any ppns out of sequence?     [103]
       BEQ     40$                     ; No..never mind.               [103]
       CALL    PSORT                   ; Yes..sort PPNs.               [103]
40$:
       LEA     A3, PPNTBL(A5)          ; Index ppn table.              [103]

50$:    MOVW    (A3)+, D3               ; Setup PPN.                    [103]
       BEQ     99$                     ; Quit if no more.              [103]
       CALL    GET.ERSATZ              ; See if ersatz exists.         [103]
       MOV     D4, (A4)+               ; Yes..set it.                  [103]
60$:    SOB     D0, 50$                 ; Do for all PPNs.              [103]

99$:    RTN                             ; Return to caller.             [103]

;**************
;*  PSORT     *
;**************
;Function:      Simple PPN sort for extended device.                    [103]
;
; Extended directories are sorted, traditional directories are not.
; They could be, but then it would defeat the purpose of informing the
; user that at traditional device's ppns are out of sequence.
;
PSORT:
       SAVE    A3,A4, D0               ; Save registers.
10$:    CLR     D0                      ; Clear PPN counter.
       LEA     A3, PPNTBL(A5)          ; Index PPN table.
       MOV     A3, A4                  ; Duplicate PPN table address.
       ADD     #2, A4                  ; Bump to next PPn entry.
       MOVW    COUNT(A5), D0           ; Set PPn count.
       SUBB    #1, D0                  ; Offset ppn count for compare.
       BEQ     99$                     ; Quit if only 1.
20$:    CMMW    @A4, @A3                ; Next PPn greater than last?
       BHIS    30$                     ; Yes..all ok.
       MOVW    @A3, D7                 ; No..save ppn
       MOVW    @A4, @A3                ; Write it to last place.
       MOVW    D7, @A4                 ; Put larger ppn ahead of previous.
       BR      10$                     ; Start over.
30$:    ADD     #2, A3                  ; Bump previous ppn.
       ADD     #2, A4                  ; Bump current ppn.
       SOB     D0,20$                  ; Do for all ppns.
99$:    REST    A3,A4, D0               ; Restore all registers.
       RTN                             ; Return to caller.


DIM:
       CRT     11.                     ; Set low intensity.            [103]
       RTN                             ; Return to caller.             [103]

BRIGHT:
       CRT     12.                     ; Set normal intensity.         [103]
       RTN                             ; Return to caller.             [103]

CMDERR: TYPECR  ?Command format error   ;
       CRLF                            ;

USAGE:  TYPECR  Copyright (C) 1990 UltraSoft Corporation
       TYPECR  Function: PPNS displays accounts and ersatz names
       TYPECR  Usage:    .PPNS Devn:   ;
       CRLF                            ;
       EXIT                            ;

;**************
;*  NEXT.ROW  *
;**************
;Function:      Advance to next display position

NEXT.ROW:
       INCB    ROW(A5)                 ;
       CMPB    ROW(A5),#23.            ;
       JLT     10$                     ;
       MOVB    #3,ROW(A5)              ;
       ADDB    #20.,COL(A5)            ;

       CMPB    COL(A5),#80.            ;
       BLT     2$                      ;
       TRMRST  D0                      ;
       ORW     #T$IMI!T$ECS,D0         ;
       TRMWST  D0                      ;
       CURSOR  #24.,#27.               ;
       CRT     21                      ;
       TYPE    press any key to continue
       CRT     22                      ;
       TIN                             ;
       CURSOR  #3,#1                   ;
       CRT     10                      ;
       MOVB    #3,ROW(A5)              ;
       MOVB    #1,COL(A5)              ;
       BR      10$                     ;

2$:     CRT     23                      ;
       SAVE    D0,D2                   ;
       MOV     #3,D0                   ;
       CLR     D2                      ;
       MOVB    COL(A5),D2              ;
       SUB     #2,D2                   ;
5$:     CURSOR  D0,D2                   ;
       CRT     47                      ;
       INCB    D0                      ;
       CMPB    D0,#23.                 ;
       BLT     5$                      ;
       REST    D0,D2                   ;
       CRT     24                      ;

10$:    RTN                             ;

;****************
;*  GET.ERSATZ  *
;****************
;Function:      Return ersatz name for an account
;
;Inputs:        D3 - PPN
;
;Outputs:       D4 - ersatz name RAD50 (0 if no ersatz name)

GET.ERSATZ:
       SAVE    A0                      ;
       CLR     D4                      ;
       MOV     ERSATZ,D7               ;
       JEQ     GE.RTN                  ;
       MOV     D7,A0                   ;
10$:    TSTW    @A0                     ;
       BEQ     GE.RTN                  ;
       CMMW    DISK+D.DEV(A5),EZ.DEV(A0) ; right device?               [102]
       BNE     15$                     ;     no                        [102]
       CMMW    DISK+D.DRV(A5),EZ.UNT(A0) ; right drive?                [102]
       BNE     15$                     ;     no                        [102]
       CMPW    D3,EZ.PPN(A0)           ;
       BEQ     20$                     ;
15$:    ADD     #EZ.SIZ,A0              ;
       BR      10$                     ;
20$:    MOV     EZ.NAM(A0),D4           ;
GE.RTN: REST    A0                      ;
       RTN                             ;

       END