************************************************************
*                                                          *
*  SCSI Sector Allocation Routine                          *
*                                                          *
*  This routine will search for free AUs in the bitmap.    *
*                                                          *
*  Inputs:                                                 *
*     R3 - AU boundry                                      *
*     R6 - SCSI ID in high byte                            *
*     R7 - Preferred starting AU                           *
*     R8 - # of AUs wanted                                 *
*                                                          *
*  Outputs:                                                *
*     R4 - Actual number of AUs allocated.                 *
*     R5 - Starting AU                                     *
*                                                          *
************************************************************

ALLOC  DATA ALWS
       DATA ALOC0

ALOC0  SOC  @EQBIT,R15       Set caller's equal bit

       MOV  @12(R13),R6      Get SCSI ID
       MOV  @14(R13),R5      Get starting AU
       JEQ  ALOC50           Branch if no starting AU given

       CLR  R7
       AI   R5,256*8         VIB take up 1st 256 bytes of disk

       MOV  R5,R0
*      ANDI R0,>000F         Mask out bit position
       MOV  R5,R1            R1 will contain byte offset
       ANDI R1,>0FF0         within the sector
       SRL  R1,3
       AI   R1,SECBUF
       MOV  R5,R8
       SRL  R8,12
       LI   R4,>8000
       SRC  R4,0
       CLR  R7
       BLWP @BANKIT          Read section of bitmap
       DATA SCSIRD
       JEQ  ALOC1
       AI   R8,16            If read wasn't successful, try
       BLWP @BANKIT          reading the copy of the bitmap
       DATA SCSIRD
       JNE  ALOCER           Couldn't read either copy - Error
       AI   R8,-16
ALOC1
       MOV  *R1,R10
       CZC  R4,R10           See if AU is free
       JNE  ALOC50           If not, get 1st available AU
       MOV  @14(R13),@10(R13)                  Set starting AU
ALOC1A SOC  R4,*R1           Mark AU as allocated
       LI   R2,1             R2 = # of AU's we allocate

ALOC2  C    R2,@16(R13)       See if we've allocated enough
       JEQ  ALOC10
       SRC  R4,1
       JOC  ALOC3
ALOC2A CZC  R4,R10
       JNE  ALOC10
       SOC  R4,*R1           Mark AU as allocated
       INC  R2               Increment AU's allocated counter
       JMP  ALOC2

ALOC3  INCT R1
       CI   R1,SECBUF+>200   See if we've reached the end
       JEQ  ALOC10
       MOV  *R1,R10
       JMP  ALOC2A

ALOC10 BLWP @BANKIT
       DATA SCSIWT
       AI   R8,16
       BLWP @BANKIT
       DATA SCSIWT
       MOV  R2,@8(R13)       Save # of AU's in caller's R4
       RTWP


* Allocation error - clear the caller's EQ bit and return
*
ALOCER SZC  @EQBIT,R15
       RTWP

* We have to search the bitmap for the 1st free AU
*
* For now, we will just look for the 1st available AU and return
* it.  In a future better algorithm, we will look for contiguous
* sectors and return as many as the user wants.
*

ALOC50 MOV  @6(R13),R5       Get AU boundry
       JGT  ALOC51
       CLR  R5               Start at beginning of disk

ALOC51 AI   R5,256*8         Account for VIB at beginning of disk
       MOV  R5,R0
*      ANDI R0,>000F         Mask out bit position
       MOV  R5,R1            R1 is offset withing sector buffer
       ANDI R1,>0FF0
       SRL  R1,3
       AI   R1,SECBUF

       LI   R4,>8000
       SRC  R4,0

       MOV  R5,R8            R8 is disk sector number
       SRL  R8,12
       CLR  R7
ALOC54 BLWP @BANKIT          Read a sector of the bitmap
       DATA SCSIRD

       JEQ  ALOC53
       AI   R8,16            If we had an error reading the bitmap,
       BLWP @BANKIT          then try reading the other copy
       DATA SCSIRD

       JNE  ALOCER
       AI   R8,-16

ALOC53 MOV  *R1,R10          See if AU is free
ALO53A CZC  R4,R10
       JEQ  ALOC55
       INC  R5
       SRC  R4,1             Not free, try next one
       JNC  ALO53A
       INCT R1
       CI   R1,SECBUF+>200   See if we've reached the end of the buffer
       JNE  ALOC53
       INC  R8
       LI   R1,SECBUF
       CI   R8,16            See if disk is full
       JL   ALOC54
       CLR  @8(R13)
       RTWP

ALOC55

       AI   R5,-256*8
       MOV  R5,@10(R13)      Save AU in caller's R5
       JMP  ALOC1A
