;*************************************************************************
;*                                                                       *
;*                      ERSET COMMAND PROGRAM - Ver. 1.0                 *
;*                                                                       *
;*************************************************************************
;
;                                   NOTICE
;
;All    rights   reserved.  This software is the property of UDISCO LTD.  and
;the material contained herein is  the   proprietary   property   and   trade
;secrets    of    UDISCO  LTD.,  embodying  substantial  creative efforts and
;confidential information, ideas and expressions, no part of  which  may   be
;reproduced   or  transmitted  in  any  form  or  by  any  means, electronic,
;mechanical,  or  otherwise,  including  photocopying  or  input  into    any
;information   storage  or  retrieval  system  without  the  express  written
;permission of UDISCO LTD.
;
;Permission to copy and use is granted to AMUS members for non commercial
;purposes only.
;
;
;       ERSET:          Make ersatz name assignments in global system ersatz
;               table once system is up and running.
;
;       Syntax: ERSET   /r:<total table entries>
;                       /r:+<additional empty table entries>
;                       <name>[:][/d                                        ]
;                              [=[<cpu>-][<dev><unit>:][<file>.<ext>][PPN]]
;
;               Where:  <name> is the ersatz entry name to define or replace,
;
;                       /d indicates that the named entry is to be removed,
;
;                       <cpu> is a CPU number,
;                       <dev> is a device name,
;                       <unit> is a unit number,
;                       <file> is a file name,
;                       <ext> is a file extention,
;                       <PPN> is a project-programmer number.
;
;               Deleted entries have names that start with "  $$$$".
;
;       Author:         Rene S. Hollan
;
;       COPYRIGHT (C) - 1989 - UDISCO LTD.
;
; Edit History
; ------------
;
; [103] 3 January 1990
;       Reserving entries during boot now works. /RSH
;
; [102] 28 December 1989
;       Work starts on reserving entries during boot. /RSH
;
; [101] 28 December 1989
;       Catch errors in command line fspec. /RSH
;
; [100] 22 December 1989
;       Coding starts. /RSH
;
;

       SEARCH  SYS
       SEARCH  SYSSYM

EZ.LEN  =       20.                     ; Length of ersatz table entry

;       Impure area

OFINI
OFDEF   ER.NAM, 4                       ; Ersatz entry name
OFDEF   ER.ENT, EZ.LEN                  ; Ersatz table entry
OFDEF   ER.DDB, D.DDB                   ; DDB for file spec
OFDEF   ER.NUM, 4                       ; number of entries to add/reserve
OFDEF   ER.FLG, 1                       ; Flag
ERF$DL  =       0                       ;    delete entry
ERF$MD  =       1                       ;    modify entry
ERF$TS  =       2                       ;    set total space
ERF$AS  =       3                       ;    add more space
ERF$NA  =       4                       ;    name specified
OFSIZ   ER.LEN                          ; size of ER impure area


VMAJOR=1                                ; major version
VMINOR=0                                ; minor version
VSUB=0                                  ; sub version
VEDIT=103.                              ; edit version

ERSET:  PHDR    -1,PV$RSM!PV$WSM,PH$REU!PH$REE  ; program header
       GETIMP  ER.LEN,A0               ; get impure memory
       BYP                             ; skip blanks
       TRM                             ; end of line?
       JNE     10$                     ; nope

       TTYI
       ASCII   .ERSET name/d.
       BYTE    13.
       ASCII   .ERSET /r:<total table size>.
       BYTE    13.
       ASCII   .ERSET /r:+<additional empty table entries>.
       BYTE    13.,0
       EVEN
       TYPECR  <ERSET name[:]=[cpu-][dev unit:][file.ext][PPN]>
       EXIT

10$:    CALL    PARSE                   ; parse command tail
       BTST    #ERF$NA,ER.FLG(A0)      ; is there a name to delete/modify?
       BNE     40$                     ; yup!

       CALL    ADJUST                  ; nope - make adjustment
       EXIT

40$:    CALL    FIND                    ; find matching entry
       MOV     ER.NAM(A0),D1           ; D1 = name to match

       BTST    #ERF$DL,ER.FLG(A0)      ; are we trying to delete entry?
       BEQ     20$                     ; nope

       MOV     A1,D7                   ; was there an entry?
       BEQ     11$                     ; nope!

       CMP     D1,@A1                  ; was the entry found?
       BEQ     12$                     ; yup!

11$:    TYPECR  <ERSET: entry not found in system ersatz table>
       EXIT

12$:    MOV     #<[$$$]+33_16.>,D1      ; wipe out entry
       MOV     D1,@A1                  ; ...
       MOV     #0,D1                   ; ...
       MOV     D1,EZ.CPU(A1)           ; ...
       MOVW    D1,EZ.DEV(A1)           ; ...
       MOVW    D1,EZ.UNT(A1)           ; ...
       MOV     D1,EZ.FIL(A1)           ; ...
       MOVW    D1,EZ.EXT(A1)           ; ...
       MOVW    D1,EZ.PPN(A1)           ; ...
       EXIT

20$:    MOV     A1,D1                   ; is there an entry to overwrite?
       BNE     22$                     ; yup!

       TYPECR  <ERSET: no more room in system ersatz table for entry>
       EXIT

22$:    LEA     A2,ER.ENT(A0)           ; A2 -> replacement
       MOV     #EZ.LEN,D1

30$:    MOVB    (A2)+,D0                ; move a byte
       MOVB    D0,(A1)+                ; ...
       SOB     D1,30$                  ; and repeat

       EXIT


; PARSE - parse options from command line
;
; Entry:        A2 -> command line tail
;               A0 -> impure area
;
; Exit:         A2 -> past command line tail
;
; Uses:         A1, D1
;
PARSE:  MOVB    #0,ER.FLG(A0)           ; clear flags
       CMPB    @A2,#'/                 ; is there a name?
       BEQ     11$                     ; nope

       LEA     A1,ER.NAM(A0)           ; A1 -> packed table name
       PACK                            ; pack entry name
       PACK                            ; ...
       BSET    #ERF$NA,ER.FLG(A0)      ; got a name

       CMPB    @A2,#':                 ; is there a colon?
       BNE     1$                      ; nope

       INC     A2                      ; yup - skip over it

1$:     CMPB    @A2,#'/                 ; is it a slash?
       JNE     10$                     ; nope

11$:    CMPB    1(A2),#'D               ; is it a /D?
       JEQ     5$                      ; yup

       CMPB    1(A2),#'R               ; is it a /R?
       BNE     2$                      ; nope - error

       BTST    #ERF$NA,ER.FLG(A0)      ; name specified?
       BEQ     12$                     ; nope

       TTYI
       ASCII   .ERSET: can't specify /R option with a name.
       BYTE    13.,0
       EVEN
       BR      21$

12$:    LEA     A2,2(A2)                ; skip over /R
       CMPB    @A2,#':                 ; is there a colon
       BNE     20$                     ; nope

       INC     A2                      ; yup - skip over it

20$:    CMPB    @A2,#'+                 ; add entries?
       BNE     22$                     ; nope

       INC     A2                      ; skip over +
       BSET    #ERF$AS,ER.FLG(A0)      ; set add entries flag
       BR      23$

22$:    BSET    #ERF$TS,ER.FLG(A0)      ; set total entries flag

23$:    GTDEC                           ; get number of entries
       MOV     D1,ER.NUM(A0)           ; ...
       RTN

2$:     TYPE    <ERSET: syntax error>
21$:    TYPECR  < - type just ERSET for help>
       EXIT

5$:     BTST    #ERF$NA,ER.FLG(A0)      ; name specified?
       BNE     52$                     ; yup!

51$:    TYPE    <ERSET: no ersatz table entry specified>
       BR      21$

52$:    BSET    #ERF$DL,ER.FLG(A0)      ; set delete entry flag
       RTN

10$:    CMPB    @A2,#':                 ; is there an optional colon?
       BNE     15$                     ; nope

       INC     A2                      ; skip over optional colon

15$:    CMPB    @A2,#'=                 ; is there an =?
       JNE     2$                      ; nope - syntax error

       INC     A2                      ; skip over =

       BTST    #ERF$NA,ER.FLG(A0)      ; name specified?
       BEQ     51$                     ; nope

       FSPEC   ER.DDB(A0)              ; fill in DDB
       BYP                             ; skip whitespace
       LIN                             ; at end of line?
       JNE     2$                      ; nope - error

       MOV     ER.NAM(A0),D1           ; move entry name
       MOV     D1,ER.ENT+EZ.NAM(A0)    ; ...

       MOV     ER.DDB+D.CPU(A0),D1     ; move cpu number
       MOV     D1,ER.ENT+EZ.CPU(A0)    ; ...

       MOVW    ER.DDB+D.DEV(A0),D1     ; copy device
       MOVW    D1,ER.ENT+EZ.DEV(A0)    ; ...

       MOVW    ER.DDB+D.DRV(A0),D1     ; copy unit
       MOVW    D1,ER.ENT+EZ.UNT(A0)    ; ...

       MOV     ER.DDB+D.FIL(A0),D1     ; copy file name
       MOV     D1,ER.ENT+EZ.FIL(A0)    ; ...

       MOVW    ER.DDB+D.EXT(A0),D1     ; copy extension
       MOVW    D1,ER.ENT+EZ.EXT(A0)    ; ...

       MOVW    ER.DDB+D.PPN(A0),D1     ; copy PPN
       MOVW    D1,ER.ENT+EZ.PPN(A0)    ; ...

       BSET    #ERF$MD,ER.FLG(A0)      ; set modify flag

       RTN


; FIND - find matching ersatz table entry
;
; Entry:        ER.NAM(A0) = packed name to search for
;
; Exit:         A1 -> matching entry, if found, else
;                  -> "  $$$$', if empty entry available, else
;                  = 0, if no match and no empty entry
;
FIND:   SAVE    A2,D0-D1
       MOV     ERSATZ,A2               ; A2 -> base of system ersatz table
       MOV     #0,A1                   ; assume no match or empty entry found
       MOV     A2,D1                   ; is there an ersatz table?
       BEQ     90$                     ; nope

       MOV     ER.NAM(A0),D1           ; D1 = name to match
       MOV     #<[$$$]+33_16.>,D0      ; D0 = empty entry

10$:    TST     EZ.NAM(A2)              ; end of table?
       BEQ     90$                     ; yup

       CMP     D1,EZ.NAM(A2)           ; do entry names match?
       BNE     20$                     ; nope

       MOV     A2,A1                   ; A1 -> matching entry
       BR      90$

20$:    CMP     D0,EZ.NAM(A2)           ; is this an empty entry?
       BNE     80$                     ; nope

       MOV     A2,A1                   ; A1 -> empty entry

80$:    LEA     A2,EZ.LEN(A2)           ; A2 -> next table entry
       BR      10$                     ; and loop for more

90$:    REST    A2,D0-D1
       RTN

; ADJUST - adjust size of ersatz table
;
; Entry:        A0 -> impure area
;
ADJUST: CALL    CHKMEM                  ; ersatz table continuous?
       JEQ     10$                     ; o.k.

       TTYI
       ASCII   .ERSET: R option can only be used during system.
       BYTE    13.,0
       EVEN
       TYPECR  <       initialization before programs are loaded>
       TYPECR  <       into system memory.>
       EXIT

10$:    BTST    #ERF$AS,ER.FLG(A0)      ; add space?
       BEQ     15$                     ; nope, make up total

       MOV     ER.NUM(A0),D2           ; D2 = space to add
       BR      18$                     ; add them

15$:    MOV     A4,D1                   ; compute entries in table
       MOV     A3,D7                   ; ...
       SUB     D7,D1                   ; ...
       DIV     D1,#EZ.LEN              ; ...
       SUB     D0,D1                   ; D1 = entries in table

       MOV     ER.NUM(A0),D2           ; compute entries to add
       SUB     D1,D2                   ; D2 = entries to add
       BMI     90$                     ; already too much

       SUB     D0,D2                   ; subtract already deleted entries
       BMI     90$                     ; too much

18$:    BEQ     92$                     ; nothing to do

20$:    MOV     #EZ.LEN,D1              ; clear out entry

30$:    CLRB    (A4)+                   ; ...
       SOB     D1,30$                  ; ....

                                       ; mark as deleted
       MOV     #<[$$$]+33_16.>,EZ.NAM-EZ.LEN(A4) ; ...
       SOB     D2,20$                  ; repeat for next entry

       CLR     @A4                     ; terminate ersatz table
       LEA     A4,4(A4)                ; set MEMBAS
       MOV     A4,MEMBAS               ; ...

92$:    RTN

90$:    TYPECR  <ERSET: more than specified total entries already>
       TYPECR  <       in system ersatz table.>
       RTN


; CHKMEM - check if ersatz memory is contiguous
;
; Exit:         Zero flag set if ersatz table memory is contiguous
;               If so, A3 -> start of ersatz memory table,
;                      A4 -> end of ersatz memory table,
;                      D0 = number of deleted entries.
;
CHKMEM: MOV     SYSTEM,D7               ; is system up?
       AND     #SY$UP,D7               ; ...
       BEQ     5$                      ; nope!

       RTN                             ; system already up, sorry!

5$:     MOV     #0,D0                   ; clear deleted entries counter
       MOV     ERSATZ,D7               ; anything in system memory?
       BEQ     90$                     ; nope

       MOV     D7,A3                   ; A3 -> start of ersatz area
       MOV     D7,A4                   ; A4 -> current end of ersatz area

10$:    MOV     @A4,D7                  ; end of table?
       BEQ     20$                     ; yup

       TSTW    EZ.NAM(A4)              ; deleted entry?
       BNE     12$                     ; nope

       INC     D0                      ; increment deleted entry count

12$:    ADD     #EZ.LEN,A4              ; A4 -> next entry
       BR      10$

20$:    MOV     A4,D7                   ; is ersatz table contiguous?
       MOV     MEMBAS,D6               ; ...
       SUB     #4,D6                   ; ...
       CMP     D6,D7                   ; ...
       RTN                             ; ... return with status

90$:    MOV     MEMBAS,A3               ; nothing in memory
       MOV     A3,A4                   ; A3,A4 -> start of ersatz table
       LCC     #PS.Z                   ; set zero flag
       RTN

       END