* Procedure FLUSH
*
*   This procedure will check the open file to see if
*   it's buffer needs to be written to disk.  If it
*   does, it will either read the 512 byte block from
*   disk and overwrite the 256 byte sector or if the
*   buffer is 512 bytes, it will just write out the
*   512 bytes.
*
*   Input:
*      R5 points to the open file cache
*      R12 points to the 1st CRU bit of RAM bank select
*

FLUSH  LDCR @B02,4            Select RAM bank 2
       MOV  @60(R5),R1        Get the modified flags
       JNE  FLUSH1
       RT                     Buffer not modified - Return

FLUSH1 LI   R0,512            Assume a 512 byte sector
       LI   R1,SECBUF
       MOV  @50(R5),R7        Get AU of the buffer
       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       LDCR @B04,4            Select RAM bank 4
       MPY  @SAUTBL(R3),R7
       LDCR @B02,4
       A    @52(R5),R8        Add in sector within AU
       JNC  FLUSH2
       INC  R7
FLUSH2 SRL  R8,1
       JNC  FLUSH3
       AI   R1,>100
FLUSH3 SRL  R7,1
       JNC  FLUSH4
       AI   R8,>8000
FLUSH4 C    @58(R5),@ZERO     Check buffer size
       JNE  FLUSH6            If 512 byte buffer, don't bother reading
       LI   R0,256
       LDCR @ZERO,4           Select RAM bank 0
       BLWP @BANKIT           Read the block from disk
       DATA SCSIRD
       JEQ  FLUSH6
FLUSH5 BL   @DSRERR
       DATA >0600             Device error

FLUSH6 LDCR @B02,4
       MOV  @6(R5),R4         Get buffer address
       MOVB @3(R5),R2         Get buffer bank

* Copy the file buffer to the SCSI I/O buffer and clear
* out the buffer.
*

FLUSH7 LDCR R2,4
       MOV  *R4,R3
       CLR  *R4+
       LDCR @ZERO,4
       MOV  R3,*R1+
       DECT R0
       JNE  FLUSH7

       BLWP @BANKIT           Write back out to disk
       DATA SCSIWT
       JNE  FLUSH5

* Now write the FDR back out to disk
* A later version of the DSR make have two flags.  One
* for buffer modified and one for FDR modified.  For
* now, we write out the buffer and FDR together.
*
       LDCR @B02,4            Select RAM bank 2
       MOV  @48(R5),R7        Get AU of FDR
       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       LDCR @B04,4            Select RAM bank 4
       MPY  @SAUTBL(R3),R7
       LI   R1,SECBUF

       SRL  R8,1
       JNC  FLUSH8
       AI   R4,>100
FLUSH8 SRL  R7,1
       JNC  FLUSH9
       AI   R8,>8000

FLUSH9 LDCR @ZERO,4           Select RAM bank 0
       BLWP @BANKIT           Read old block
       DATA SCSIRD
       JNE  FLUSH5

       LDCR @B02,4            Select RAM bank 2
       MOV  @4(R5),R4         Get FDR address
       MOVB @2(R5),R2         Get FDR bank
       LI   R0,256

* Copy the FDR to the SCSI I/O buffer
*
FLUS10 LDCR R2,4
       MOV  *R4+,R3
       LDCR @ZERO,4
       MOV  R3,*R1+
       DECT R0
       JNE  FLUS10

       BLWP @BANKIT
       DATA SCSIWT
       JNE  FLUSH5

       LDCR @B02,4            Select RAM bank 2
       CLR  @60(R5)           Clear modified flags
       RT
