************************************************************
*                                                          *
*  MKFDR                                                   *
*                                                          *
*  This procedure will take in file information and        *
*  create an FDR on disk and insert it into the parent     *
*  directory.  If the file exists, its flags are checked   *
*  for compatibility.                                      *
*                                                          *
*  Inputs:                                                 *
*     MKFL3 - # of level 3 records in the file             *
*     MKFRL - Record length                                *
*     MKFRPS - # of records per sector                     *
*     MKFEOF - End of File offset                          *
*     MKFLAG - File flags                                  *
*     MKFSZ  - Size of file in sectors                     *
*                                !                         *
*  Output:                                                 *
*     R1 = status (0=success)                              *
*                                                          *
************************************************************


************************************************************
*                                                          *
*  Revision history -                                      *
*                                                          *
*  10-19-95 - I am completely rewriting this routine       *
*  because I didn't like how the old routine would have    *
*  to scan the directory twice if a file didn't exist.     *
*  I no longer call GETFDR in this routine.                *
*                                                          *
************************************************************

MKFDR

* OK - first we will see if the file exists or not.
*
       LDCR @B02,4           Save return address
       MOV  R11,@MKSR11

* See if the parent directory exists
*
       BL   @GETDDR
       CI   R5,0
       JNE  MKF1

       LI   R1,7             File Error
       B    @MKFRT

MKF1
       LDCR @B03,4
       MOVB @22(R5),R1       Get # of files in directory
* Lets read the FDIR
*
       MOV  @24(R5),R7
       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       LDCR @B02,4
       MOV  R7,@SFDRAU       Save AU of FDIR
       LDCR @B04,4
       MPY  @SAUTBL(R3),R7
       LI   R2,SECBUF

       SRL  R8,1
       JNC  MKF3
       AI   R2,>100
MKF3   SRL  R7,1
       JNC  MKF4
       AI   R8,>8000
MKF4   LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JNE  MKFERR

* Move the FDIR to another buffer
*
       LI   R4,DFDR
       LI   R0,256
MKF5   MOV  *R2+,R3
       LDCR @B02,4
       MOV  R3,*R4+
       LDCR @ZERO,4
       DECT R0
       JNE  MKF5

* Copy the filename to FDIR
*
       LI   R2,NCB+39
       LI   R3,FNCB
       LDCR @B02,4
MKF6   CB   *R2,@PERIOD
       JEQ  MKF7
       DEC  R2
       CI   R2,NCB-1
       JNE  MKF6

MKF7   INC  R2
MKF7A  MOVB *R2+,R4
       LDCR @ZERO,4
       MOVB R4,*R3+
       LDCR @B02,4
       CB   *R2,@SPACE
       JEQ  MKF8
       CI   R2,NCB+40
       JNE  MKF7A

MKF8   LDCR @ZERO,4
MKF8A  CI   R3,FNCB+10
       JEQ  MKF9
       MOVB @SPACE,*R3+
       JMP  MKF8A

MKF9

* Now set the bounds of our search.  If the directory is
* empty, we can skip this part
*
       CLR  R0
       SRL  R1,8
       CI   R1,0
       JEQ  MKF17

       DEC  R1

MKF10  MOV  R0,R10           Compute the midpoint
       A    R1,R10
       ANDI R10,>FFFE

       LDCR @B02,4
       MOV  @DFDR(R10),R7

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

       SRL  R8,1
       JNC  MKF11
       AI   R2,>100
MKF11  SRL  R7,1
       JNC  MKF12
       AI   R8,>8000
MKF12  LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JNE  MKFERR

* OK, We got the FDR in the buffer.  Compare file names
*
       LI   R3,FNCB
       LI   R9,10
MKF13  C    *R2+,*R3+
       JL   MKF14
       JH   MKF15
       DECT R9
       JNE  MKF13

* The file exists!  Branch to a part of code that checks
* the file type and protection.

       LDCR @B02,4
*      MOV  @DFDR(R10),R10   Save AU of FDR
       MOV  @DFDR(R10),@MKFAU1
       B    @MKF30

MKF14  C    R0,R1
       JEQ  MKF16
       SRL  R10,1
       MOV  R10,R0
       INC  R0
       JMP  MKF10

MKF15  C    R0,R1
       JEQ  MKF17
       SRL  R10,1
       MOV  R10,R1
       C    R0,R1
       JEQ  MKF17
       DEC  R1
       JMP  MKF10

MKFERR LI   R1,6             Device Error
MKFRT  LDCR @B02,4
       MOV  @MKSR11,R11
       RT


MKF16  INC  R0
MKF17  SLA  R0,1
       AI   R0,DFDR

* Lets make sure we don't already have 127 files in
* this directory before trying to create a new one

       LDCR @B03,4
       CB   @B127,@22(R5)
       JNE  MKF17A

MKF18  LI   R1,4         Out of space error
       JMP  MKFRT
MKF17A

* Let's allocate an AU to store the file's FDR and insert
* it into the parent's FDIR
*

       CLR  R3               Start at beginning
       CLR  R7               No preferred AU
       LI   R8,1             We want 1 AU
       LDCR R3,0         Select RAM bank 0
*      LDCR @ZERO,4
       BLWP @ALLOC
       JNE  MKFERR

       CI   R4,0
       JEQ  MKF18            Out of space error

* Now insert the AU into the FDIR
*
       LDCR @B02,4
       LI   R1,DFDR+252
MKF19  C    R1,R0
       JEQ  MKF20
       MOV  @-2(R1),*R1
       DECT R1
       JMP  MKF19

MKF20  MOV  R5,*R0
       MOV  R5,@MKFAU1       Save AU of FDR

* Now write the FDIR back out to disk.
*
       MOV  @SFDRAU,R7
       LDCR @B04,4
       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       MPY  @SAUTBL(R3),R7

       LI   R2,SECBUF
       SRL  R8,1
       JNC  MKF21
       AI   R2,>100
MKF21  SRL  R7,1
       JNC  MKF22
       AI   R8,>8000
MKF22  LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JNE  MKFERR

       LI   R3,DFDR
       LI   R0,256
MKF23  LDCR @B02,4
       MOV  *R3+,R4
       LDCR @ZERO,4
       MOV  R4,*R2+
       DECT R0
       JNE  MKF23

       BLWP @BANKIT
       DATA SCSIWT
       JNE  MKFERR

* Now that we have the new FDIR written to disk, increment
* the file counter in the parent DDR
*

       BL   @GETDDR
       LDCR @B03,4
       AB   @B01,@22(R5)     Increment cache

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

       LI   R2,SECBUF
       SRL  R8,1
       JNC  MKF24
       AI   R2,>100
MKF24  SRL  R7,1
       JNC  MKF25
       AI   R8,>8000
MKF25  LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JNE  MKFERR

* now increment the file counter

       AB   @B01,@22(R2)

* And write back out to disk

MKF25C BLWP @BANKIT
       DATA SCSIWT
       JNE  MKFERR

* If this was the VIB, we must also write it out
* in the copy

       CI   R8,0
       JNE  MKF25B
       CI   R7,0
       JNE  MKF25B
       AI   R8,16
       JMP  MKF25C
MKF25B
* Now we will create an FDR in the DFDR buffer in bank 2
* Start by filling in the file name

       LI   R1,DFDR
       LI   R2,FNCB
       LI   R0,10
MKF25A LDCR @ZERO,4
       MOV  *R2+,R3
       LDCR @B02,4
       MOV  R3,*R1+
       DECT R0
       JNE  MKF25A

       CLR  *R1+             Extended record length
       MOVB @MKFLAG,*R1+     File flags
       MOVB @MKFRPS,*R1+     Records per sector
       CLR  *R1+             # of sectors allocated
       MOVB @MKFEOF,*R1+     End of file offset
       MOVB @MKFRL,*R1+      Record Length
       MOV  @MKFL3,*R1+      # of level 3 records
       CLR  *R1+             Creation date
       CLR  *R1+
       CLR  *R1+             Update date
       CLR  *R1+
       LI   R0,'FI'
       MOV  R0,*R1+
       CLR  *R1+             Parent FDR
       CLR  *R1+             Offspring AU
       CLR  *R1+             AU's allocated for FDR
       MOV  @SFDRAU,*R1+     Pointer to FDIR
       CLR  *R1+             Extended information
MKF26  CLR  *R1+             Data chain pointers
       CI   R1,DFDR+256
       JNE  MKF26

* Now branch to a section of code that will allocate
* all the AU's we need for the file

       B    @MKF40

************************************************************
*                                                          *
*  The file already exists, so we have to check to make    *
*  sure the file attribute match.  If they do, we will     *
*  make sure we have enough AUs allocated and rewrite      *
*  the FDR back to disk.                                   *
*                                                          *
************************************************************


MKF30

* Start by copying the FDR to the DFDR buffer in bank 2

       LI   R1,DFDR
       AI   R2,-10           It still points to FDR
       LI   R0,256
MKF31  LDCR @ZERO,4
       MOV  *R2+,R3
       LDCR @B02,4
       MOV  R3,*R1+
       DECT R0
       JNE  MKF31

* Check to see if the old file was protected
*
       MOVB @DFDR+12,R1      Get file flags
       COC  @PROBIT,R1
       JNE  MKF32

       LI   R1,1             File protect error
       B    @MKFRT

MKF32

* If it is a program file, that is all we have to check.
* Otherwise we must check DISPLAY/INTERNAL, FIXED/VARIABLE
* and record length

       ANDI R1,>8000
       JNE  MKF34A

* It's not a program file.  Make sure attributes match
*
       MOVB @DFDR+12,R1
       ANDI R1,>C100
       MOVB @MKFLAG,R2
       ANDI R2,>C100
       C    R1,R2
       JEQ  MKF34

MKF33  LI   R1,7             File error
       B    @MKFRT

MKF34

* Now check record length

       CB   @DFDR+17,@MKFRL
       JNE  MKF33

* Everything checks out.  Lets rewrite a couple fields and
* allocate some AUs!
MKF34A
       MOVB @MKFLAG,@DFDR+12     File flags
       MOVB @MKFEOF,@DFDR+16     End Of File offset
       MOV  @MKFL3,@DFDR+18      # of level 3 records

**
*
* Here we will allocate AU's if we need to
*
**

MKF40  MOV  @MKFSZ,R1        Get # of sectors wanted
       LDCR @B04,4
       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       MOV  @SAUTBL(R3),R3
       A    R3,R1
       DEC  R1
       CLR  R0
       DIV  R3,R0


       MOV  R0,R8            R8 has # of AU's needed
       LDCR @B02,4
       C    R8,@DFDR+34      See if we need any more
       JL   MKF44
       JEQ  MKF44

* Ok - We need to allocate more AU's for this FDR!
*
       S    @DFDR+34,R8

MKF40A LI   R3,256*8         AU Boundry
       CLR  R7               No preferred starting AU
       LDCR @ZERO,4
       BLWP @ALLOC
*      JNE  MKFERR

       CI   R4,0
       JNE  MKF41

       LI   R1,4             Out Of Space Error
       B    @MKFRT

MKF41
* Fill in the next cluster of the FDR
       LI   R3,DFDR+40
       LDCR @B02,4
MKF42  C    *R3,@ZERO
       JEQ  MKF43
       AI   R3,4
       JMP  MKF42

MKF43  MOV  R5,*R3+
       MOV  R5,*R3
       A    R4,*R3
       DEC  *R3

       A    R4,@DFDR+34      Add to # of AUs allocated

       S    R4,R8            See if we need to get any more
       JNE  MKF40A

MKF44

* Now lets fill in the # of sectors currently allocated.
* For program files, this will be whatever the user wanted.
* For data files, it is equal to the number of AU's
* allocated time the number of sectors per AU.
*
       MOVB @DFDR+12,R1      Get file flags
       ANDI R1,>0100
       JNE  MKF45

       MOV  @DFDR+34,R1
       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       LDCR @B04,4
       MPY  @SAUTBL(R3),R1
       LDCR @B02,4
       MOV  R2,@DFDR+14
       JMP  MKF46

MKF45  MOV  @MKFSZ,@DFDR+14
MKF46

* All we have to do now is write the FDR back out to
* disk and we're done!

       LDCR @B02,4
       MOV  @MKFAU1,R7       Restore AU of FDR

       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       LDCR @B04,4
       MPY  @SAUTBL(R3),R7
       LI   R2,SECBUF
       SRL  R8,1
       JNC  MKF47
       AI   R2,>100
MKF47  SRL  R7,1
       JNC  MKF48
       AI   R8,>8000
MKF48  LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD           Read old block
*      JNE  MKFERR

       LI   R1,DFDR
       LI   R0,256

MKF49  LDCR @B02,4
       MOV  *R1+,R4
       LDCR @ZERO,4
       MOV  R4,*R2+
       DECT R0
       JNE  MKF49

       BLWP @BANKIT          Write it back out
       DATA SCSIWT
*      JNE  MKFERR

       CLR  R1               Success!
       B    @MKFRT
