* Procedure FDDR - Find Directory Descriptor Re Ord
*
* This procedure will search the subdirectory name cache for
* the name of the directory in the name compare buffer.  The
* caller's R2 specifies the end of the directory name by
* pointing to one past the last character in the name.
*
* If the directory is found in the cache, the caller's equal
* bit is set and R4 will point to the directory cache entry.
*
*
* Note: RAM bank 4 must be enabled before calling this procedure
*

FDDR   DATA FDDRWS
       DATA FDDR0

FDDR0  MOV  @4(R13),R2   Get the caller's R2
       LI   R4,DNCASH

FDDR1  MOV  *R4,R0       Is this a valid entry?
       JEQ  FDDR3        If not, try next one

       CB   *R4,@12(R13) Does the SCSI ID match the caller's R6
       JNE  FDDR3        If not, try next one

       MOV  R4,R5
       AI   R5,4         R5 points to directory name in cache entry
       CI   R2,NCB-1     See if We're looking for root directory
       JEQ  FDDR2A
       LI   R3,NCB
FDDR2  C    R3,R2
       JEQ  FDDR2A
       CB   *R3+,*R5+    Compare the two names
       JEQ  FDDR2
       JMP  FDDR3

FDDR2A CB   *R5,@SPACE   Make sure name ends here
       JNE  FDDR3

       MOV  R4,@8(R13)   Name matches, store in caller's R4
       SOC  @EQBIT,R15   Set caller's equal bit
       RTWP              and return

FDDR3  AI   R4,48        Here we go to next one
       CI   R4,>5FFE-48
       JL   FDDR1

       SZC  @EQBIT,R15   No matches, clear caller's equal bit
       RTWP              and return


************************************************************
*                                                          *
* Procedure RESDCE - Reserve Data Cache Entry              *
*                                                          *
*  This procedure will search the directory cache entries  *
*  for an entry that is either unused or so old, it's      *
*  ready to be replaced                                    *
*                                                          *
*  It will return a pointer to the entry in the caller's   *
*  R4.                                                     *
*                                                          *
*  When this procedure finishes, it transfers control to   *
*  the the procedure FREDCE which will get a DDR cache     *
*  entry.                                                  *
*                                                          *
*                                                          *
************************************************************

RESDCE DATA FDDRWS
       DATA RESD1

RESD1  SETO R3
       LI   R4,DNCASH

RESD2  MOV  *R4,R5       Has this entry ever been used?
       JEQ  RESD4        If not, use it.

       MOV  @2(R4),R5    Does this entry have it's DDR in RAM?
       JNE  RESD3        If so, do not use it

       C    R3,@46(R4)   Is this entry the oldest?
       JL   RESD3        If not, try another one

       MOV  @46(R4),R3   Save age of oldest entry
       MOV  R4,R6        Save pointer to oldest entry

RESD3  AI   R4,48        Go to next entry
       CI   R4,>5FFE-48  Did we check them all?
       JL   RESD2        If not, go back and check some more

       MOV  R6,R4
RESD4  INC  @>5FFE       Increment our age number
       MOV  @>5FFE,@46(R4)
       MOV  R4,@8(R13)   Save pointer in caller's R4
       CLR  *R4+
       CLR  *R4+
       LI   R3,40        Blank out the name
RESD5  MOV  @SPACE,*R4+
       DECT R3
       JNE  RESD5

       JMP  FREDC1       Now go free a DDR cache entry

************************************************************
*                                                          *
*  Procedure FREDCE - Free DDR cache entry                 *
*                                                          *
*  This procedure will check to see if all the DDR cache   *
*  entries are in use.  If not, it will take the first     *
*  unused entry and return it to the caller.  If they      *
*  are all in use, it will take the oldest entry and take  *
*  it away from the Directory Name cache entry             *
*                                                          *
*  It returns a pointer to the DDR cache address in the    *
*  caller's R5.                                            *
*                                                          *
************************************************************

FREDCE DATA FDDRWS
       DATA FREDC1

FREDC1 LI   R1,DNCASH
       SETO R2
       LI   R3,>4F00     R3 will keep track of the highest used DDR entry

FREDC2 MOV  @2(R1),R4    Does this entry have it's DDR in cache?
       JEQ  FREDC4       If not, try another one
       C    R3,R4
       JH   FREDC3
       MOV  R4,R3

FREDC3 C    @46(R1),R2   Check age of this entry
       JH   FREDC4
       MOV  R1,R5        Save this address since it's the oldest
       MOV  @46(R1),R2

FREDC4 AI   R1,48        Go to next entry
       CI   R1,>5FFE-48  Check to see if all entries have been checked
       JL   FREDC2       if not, go back

       CI   R3,>5F00     See if all DDR entries have been used
       JEQ  FREDC5       If so, swap out an old one
       AI   R3,>100
       MOV  R3,@10(R13)  Otherwise, return the unused one
       RTWP

FREDC5 MOV  @2(R5),@10(R13) Save pointer in caller's R5
       CLR  @2(R5)          swap out old one
       RTWP                 and return
