************************************************************
*                                                          *
*  GPL routine >23 - File/subdir rename                    *
*                                                          *
*  Inputs:                                                 *
*     >834C - SCSI device number and buffer bit            *
*     >834E - Pointer to new name                          *
*     >8350 - Pointer to old name                          *
*                                                          *
* Outputs:                                                 *
*    >8350 - Error code                                    *
* Errors returned                                          *
*     Old file does not exist - 7 file error               *
*     Old file is protected   - 1                          *
*     Another file with new name already exists - 7        *
*                                                          *
* Modifications:                                           *
*   Feb 28, 1997 - Had to completely rewrite routine       *
*     because it was taking too much space in memory       *
*                                                          *
************************************************************

RENAM0 ANDI R12,>FF00
       AI   R12,24

* Lets start off by copying the old name and new name
* to a buffer in the SCSI card memory
*
       LI   R2,10
       LI   R3,RNOLD
       LDCR @B02,4
       MOVB @>834C,R0        See if filename is in CPU or VDP RAM
       ANDI R0,>8000
       JEQ  RENAM3

       MOV  @>8350,R4        Get pointer to old name in CPU memory
RENAM1 MOVB *R4+,*R3+
       DEC  R2
       JNE  RENAM1

       MOV  @>834E,R4        Get pointer to new name in CPU memory
       LI   R3,RNNEW
       LI   R2,10
RENAM2 MOVB *R4+,*R3+
       DEC  R2
       JNE  RENAM1
       JMP  RENAM6

RENAM3 MOV  @>8350,R4        Get pointer to old name in VDP memory
       SWPB R4               Set VDP read address
       MOVB R4,@VDPWA
       SWPB R4
       MOVB R4,@VDPWA
       NOP
RENAM4 MOVB @VDPRD,*R3+
       DEC  R2
       JNE  RENAM4

 MOVB @>834F,@VDPWA
 NOP
 MOVB @>834E,@VDPWA

       LI   R3,RNNEW
       LI   R2,10
RENAM5 MOVB @VDPRD,*R3+
       DEC  R2
       JNE  RENAM5
RENAM6

* Now we check to see if the parent directory exists
*
       BL   @MAKEFN
       BL   @GETDDR

       CI   R5,0             Check that directory exists
       JNE  RENM7

       MOVB @B07,@>8350      Set error code
       LDCR @ZERO,4
       B    @DSRRT

RENM7  LDCR @B03,4
       MOVB @22(R5),R1       Get # of files in directory
       JEQ  RENM20

* Read the FDIR from disk
*
       MOV  @24(R5),R7       Get AU of FDIR
       LDCR @B02,4
       MOV  R7,@RNFDIR       Save AU of FDIR for later

       LDCR @B04,4
       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       MPY  @SAUTBL(R3),R7

       LI   R5,SECBUF
       SRL  R8,1
       JNC  RENM8
       AI   R5,>100
RENM8  SRL  R7,1
       JNC  RENM9
       ORI  R8,>8000
RENM9  LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JEQ  RENM10

RENM9A MOVB @B06,@>8350      Hardware error
       B    @DSRRT

RENM10 LI   R2,DFDR          Copy the FDIR to the DFDR
       LI   R3,256           buffer
RENM11 LDCR @ZERO,4
       MOV  *R5+,R4
       LDCR @B02,4
       MOV  R4,*R2+
       DECT R3
       JNE  RENM11

       LI   R2,>0200         R2 has the bank
       LI   R3,DFDR          R3 pointer to the list of AUs

       BL   @RNDOIT          Let's rename a file!

* If it successfully renamed the file, we have to write
* the FDIR back out to disk.

       CI   R9,0
       JEQ  RENM20
       CI   R9,1
       JEQ  RENM15

       LDCR @B02,4
       MOV  @RNFDIR,R7
       LI   R5,DFDR
       LI   R4,>0200         Bank of DFDR

**
*
* Here we write the FDIR or DDR back out to disk.  At this
* point in the program, the following registers must be set.
*
* R4 = Bank # with the DDR or FDIR
* R5 = Pointer to the DDR or FDIR in memory
* R6 = SCSI ID of drive we're writing to
* R7 = AU of DDR or FDIR
*
**

RN11A  LDCR @B04,4
       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       MPY  @SAUTBL(R3),R7
       LI   R1,SECBUF
       SRL  R8,1
       JNC  RENM12
       AI   R1,>100
RENM12 SRL  R7,1
       JNC  RENM13
       ORI  R8,>8000
RENM13 LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JNE  RENM9A           Error routine
       LI   R0,256           Copy the FDIR into the buffer
RENM14 LDCR R4,4
       MOV  *R5+,R2
       LDCR @ZERO,4
       MOV  R2,*R1+
       DECT R0
       JNE  RENM14

       BLWP @BANKIT
       DATA SCSIWT
       JNE  RENM9A

RENM15 LDCR @ZERO,4
       MOVB @ZERO,@>8356
       B    @DSRRT

* If we get to here, then there is no filename with the same
* name as what we ant to rename.  Check to see if there is a
* directory with the name and rename that
*
RENM20 BL   @GETDDR
       LDCR @B03,4
       MOVB @23(R5),R1   Get number of subdirectories
       JEQ  RENM21

       LI   R2,>0300     R2 = bank of the DDR
       MOV  R5,R3
       AI   R3,28
       BL   @RNDOIT
       CI   R9,0
       JNE  RENM22

RENM21 LDCR @ZERO,4      File not found error
       MOVB @B07,@>8350
       B    @DSRRT

RENM22 CI   R9,1
       JEQ  RENM15

* We have to write the DDR back out to disk
*
       BL   @GETDDR          Get pointer to DDR in R5
       LI   R4,>0300         Bank of DDR in memory
       LDCR @B04,4
       MOV  @SAVEAU,R7
       JMP  RN11A

************************************************************
*                                                          *
*  PROCEDURE: RNDOIT (REname DO IT)                        *
*                                                          *
*  This procedure has been written so that it can rename   *
*  either a file or a directory.  All it needs is a list   *
*  of AUs from either an FDIR or a DDR and will modify     *
*  the file name in the header of the FDR or DDR and       *
*  resequence the list of AUs.  It it the duty of the      *
*  caller to write the list of AUs back out to disk.       *
*                                                          *
*  Inputs:                                                 *
*     RNOLD - Contains the old name of the file or dir     *
*     RNNEW - Contains the new name of the file or dir     *
*     R1 - Number of AUs in the list (MSB)                 *
*     R2 - Bank number of list (MSB)                       *
*     R3 - Points to list of AUs                           *
*                                                          *
*  Output:                                                 *
*     R9 = 0 - File not found                              *
*          1 - File renamed, no need to write list di disk *
*          2 - File renames, write index back to disk      *
*                                                          *
************************************************************

RNDOIT LDCR @B02,4
       SRL  R1,8
       MOV  R1,@RNHIGH
       DEC  R1
       CLR  R0

       CLR  @RNLOW
       CLR  @LONAM
       SETO @HINAM

* Start by copying the old name to the file name compare
* buffer in bank 0

       LI   R4,10
       LI   R5,FNCB
       LI   R7,RNOLD
RNDO1  LDCR @B02,4
       MOV  *R7+,R8
       LDCR @ZERO,4
       MOV  R8,*R5+
       DECT R4
       JNE  RNDO1

RNDO2  MOV  R0,R4            Compute the midpoint
       A    R1,R4
       ANDI R4,>FFFE
       A    R3,R4
       LDCR R2,4
       MOV  *R4,R7           Get AU
       LDCR @B02,4
       MOV  R7,@RENMAU       Save AU in case this is the one

       LDCR @B04,4
       MOV  R6,R4
       SRL  R4,8
       SLA  R4,1
       MPY  @SAUTBL(R4),R7

       LI   R4,SECBUF
       SRL  R8,1
       JNC  RNDO3
       AI   R4,>100
RNDO3  SRL  R7,1
       JNC  RNDO4
       ORI  R8,>8000
RNDO4  LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JEQ  RNDO4A

       MOVB @B06,@>8350      Device error
       B    @DSRRT

* Here we compare the name to the new filename.  This will
* help narrow down the search for where the file will go
* after it has been renamed.
*
RNDO4A
       LI   R7,RNNEW
       MOV  R4,R8
       LI   R5,10
RNDO4B LDCR @ZERO,4
       MOV  *R8+,R9
       LDCR @B02,4
       C    R9,*R7+
       JH   RNDO4D
       JL   RNDO4G
       DECT R5
       JNE  RNDO4B

* This matched the new name.  We have to set everything
*
       MOV  R0,R7            Recalculate midpoint
       A    R1,R7
       SRL  R7,1
       MOV  R7,@RNLOW
       MOV  R7,@RNHIGH

       LI   R7,LONAM
       MOV  R4,R8
       LI   R5,10
RNDO4C LDCR @ZERO,4
       MOV  *R8+,R9
       LDCR @B02,4
       MOV  R9,@10(R7)
       MOV  R9,*R7+
       DECT R5
       JNE  RNDO4C
       JMP  RNDO5

RNDO4D LI   R7,HINAM
       MOV  R4,R8
       LI   R5,10
RNDO4E LDCR @ZERO,4
       MOV  *R8+,R9
       LDCR @B02,4
       C    R9,*R7+
       JH   RNDO5
       JL   RNDO4F
       DECT R5
       JNE  RNDO4E

* This name is higher than the new name, but lower
* than HINAM.  We need to set the new HINAM and RNHIGH
*
RNDO4F LI   R7,HINAM
       MOV  R4,R8
       LI   R5,10
RNDO4K LDCR @ZERO,4
       MOV  *R8+,R9
       LDCR @B02,4
       MOV  R9,*R7+
       DECT R5
       JNE  RNDO4K

       MOV  R0,R7            Recalculate midpoint
       A    R1,R7
       SRL  R7,1
       MOV  R7,@RNHIGH
       JMP  RNDO5

**

RNDO4G LI   R7,LONAM
       MOV  R4,R8
       LI   R5,10
RNDO4H LDCR @ZERO,4
       MOV  *R8+,R9
       LDCR @B02,4
       C    R9,*R7+
       JH   RNDO4I
       JL   RNDO5
       DECT R5
       JNE  RNDO4H

* This name is lower than the new name, but higher
* than LONAM.  We need to set the new LONAM and RNLOW
*
RNDO4I LI   R7,LONAM
       MOV  R4,R8
       LI   R5,10
RNDO4J LDCR @ZERO,4
       MOV  *R8+,R9
       LDCR @B02,4
       MOV  R9,*R7+
       DECT R5
       JNE  RNDO4J

       MOV  R0,R7            Recalculate midpoint
       A    R1,R7
       SRL  R7,1
       MOV  R7,@RNLOW

* Compare the names to see if we have a match
*
RNDO5  LDCR @ZERO,4
       LI   R5,FNCB
RNDO5A C    *R4+,*R5+
       JL   RNDO6
       JH   RNDO7
       CI   R5,FNCB+10
       JNE  RNDO5A

* We have a match !!!

       AI   R4,-10

* If this is an FDR, see if it is protected.  If so, abort
* here.  If it is not protected, or if this is a DDR,
* continue on.

       MOV  @28(R4),R1
       CI   R1,'FI'
       JNE  RNDO10
       MOV  @12(R4),R1
       COC  @PROBIT,R1
       JNE  RNDO10

       MOVB @B01,@>8350      Set error - file protected
       B    @DSRRT

* The name didn't match.  Adjust the bounds of our search
*
RNDO6  C    R0,R1            Anything left to check
       JEQ  RNDO8
       MOV  R0,R4            Adjust lower bound of search
       A    R1,R4
       SRL  R4,1
       MOV  R4,R0
       INC  R0
       B    @RNDO2

RNDO7  C    R0,R1
       JEQ  RNDO8
       MOV  R0,R4            Adjust upper bound of search
       A    R1,R4
       SRL  R4,1
       C    R4,R0
       JEQ  RNDO8
       MOV  R4,R1
       DEC  R1
       B    @RNDO2

* The name didn't exist - return to the caller
*
RNDO8  CLR  R9
       RT

* The filename existed and wasn't protected.  Now we have to
* see if the name we want to rename it to already exists
*
RNDO10 LI   R4,10            Copy the new name to the FNCB
       LI   R5,FNCB
       LI   R7,RNNEW
RNDO11 LDCR @B02,4
       MOV  *R7+,R8
       LDCR @ZERO,4
       MOV  R8,*R5+
       DECT R4
       JNE  RNDO11

       LDCR @B02,4
       MOV  @RNLOW,R0        Get bounds of search
       MOV  @RNHIGH,R1
       C    R0,R1            If equal, new filename
       JNE  RND11A           exists already
       C    @LONAM,@ZERO
       JNE  RN15A
RND11A

* Here are some check to see if we have to scan the
* directory again

* Check 1 - If the name we want to rename to comes before
*           the first file in the directory, we can skip
*           processing

       CI   R1,0
       JEQ  RNDO19

* Check 2 - If R0 and R1 are off by only 1, we know the new
*           file will go between them.

       DEC  R1
       C    R1,R0
       JNE  RND11B
       C    @LONAM,@ZERO
       JNE  RNDO18
RND11B
       C    @LONAM,@ZERO
       JEQ  RNDO12
       INC  R0
RNDO12
       MOV  R0,R4            Calculate the midpoint
       A    R1,R4
       ANDI R4,>FFFE
       A    R3,R4
       LDCR R2,4
       MOV  *R4,R7           Get AU

       LDCR @B04,4
       MOV  R6,R4
       SRL  R4,8
       SLA  R4,1
       MPY  @SAUTBL(R4),R7

       LI   R4,SECBUF
       SRL  R8,1
       JNC  RNDO13
       AI   R4,>100
RNDO13 SRL  R7,1
       JNC  RNDO14
       ORI  R8,>8000
RNDO14 LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JNE  RNDER6

* Compare the names to see if we have a match
*
RNDO15 LI   R5,FNCB
RN15B  C    *R4+,*R5+
       JL   RNDO16
       JH   RNDO17
       CI   R5,FNCB+10
       JNE  RN15B

* The new name exists - error!!!
*
RN15A  LDCR @ZERO,4
       MOVB @B07,@>8350
       B    @DSRRT

RNDO16 C    R0,R1            Is there anything left to check?
       JEQ  RNDO18
       MOV  R0,R4
       A    R1,R4
       SRL  R4,1
       MOV  R4,R0
       INC  R0
       JMP  RNDO12

RNDO17 C    R0,R1            Is there anything left to check?
       JEQ  RNDO19
       MOV  R0,R4
       A    R1,R4
       SRL  R4,1
       C    R4,R0
       JEQ  RNDO19
       MOV  R4,R1
       DEC  R1
       JMP  RNDO12

**
*
* Here is where we go if there is an error doing
* I/O to the SCSI drive
*
**

RNDER6 MOVB @B06,@>8350
       B    @DSRRT

*******************

RNDO18 INC  R0
RNDO19

* We now have the index where the file will go.  Based on
* where it was, we wither have to move the pointers forward,
* backward, or not at all.

       SLA  R0,1

       LI   R1,-2
       LDCR @B02,4
       MOV  @RENMAU,R7
       LDCR R2,4
       MOV  R3,R5
RNDO20 INCT R1
       C    *R5+,R7
       JNE  RNDO20

       A    R3,R0
       A    R3,R1

       LI   R10,1
       C    R1,R0            See which way we move
       JEQ  RNDO24           No shuffling of list required!!
       JH   RNDO22

* OK, We have to move pointers
*
       DECT R0
       C    R0,R1
       JEQ  RNDO24
RNDO21 MOV  @2(R1),*R1
       INCT R1
       C    R1,R0
       JNE  RNDO21
       JMP  RNDO23

* We have to move pointers the other way
RNDO22 MOV  @-2(R1),*R1
       DECT R1
       C    R1,R0
       JNE  RNDO22

RNDO23 MOV  R7,*R1
       LI   R10,2
* Now lets overwrite the name in the FDR or DDR
*
RNDO24 LDCR @B04,4
       MOV  R6,R5
       SRL  R5,8
       SLA  R5,1
       MPY  @SAUTBL(R5),R7

       LI   R5,SECBUF
       SRL  R8,1
       JNC  RNDO25
       AI   R5,>100
RNDO25 SRL  R7,1
       JNC  RNDO26
       ORI  R8,>8000
RNDO26 LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JNE  RNDER6

* Now copy the filename over the old one
* Lucky for us, it's still in the FNCB

       LI   R4,10
       LI   R9,FNCB
RNDO27 MOV  *R9+,*R5+
       DECT R4
       JNE  RNDO27

* Now write it out back to disk
*
       BLWP @BANKIT
       DATA SCSIWT
       JNE  RNDER6

* We are done - Success!!

       MOV  R10,R9
       RT
