**
*
*  OPEN INPUT
*
* Here we open a file for reading.  We must first make
* sure that the file exists.  Then we make sure that the
* attributes correspond.  Then and only then will we
* open up the file.
*
**

OPENIN

* Check to see if we're opening a file or directory.
* a directory specification will either be blank or
* end with a period.
*

       LDCR @B02,4           Select RAM bank 2
       LI   R1,NCB+39

OIN0   CB   *R1,@PERIOD
       JEQ  OIN0A
       CB   *R1,@SPACE
       JNE  OIN0B
       DEC  R1
       CI   R1,NCB-1
       JNE  OIN0
OIN0A  B    @OIN20

* See if the file exists
*
OIN0B  BL   @GETFDR
       CI   R5,0
       JNE  OIN1

       BL   @DSRERR
       DATA >0700        File error
OIN1

* We must first make sure the attributes match
*
       LDCR @ZERO,4           Select RAM bank 0
       MOVB @PABBUF+4,R2      Get record length
       JEQ  OIN3              If zero, it's OK
       CB   R2,@17(R5)
       JEQ  OIN3A
OIN2   BL   @DSRERR
       DATA >0200             Bad open attribute

* Store record length in caller's PAB
*
OIN3   MOV  @PABADR,R1
       AI   R1,4
       ORI  R1,>4000
       SWPB R1
       MOVB R1,@VDPWA
       SWPB R1
       MOVB R1,@VDPWA
       MOVB @17(R5),@PABBUF+4
       MOVB @17(R5),@VDPWD

OIN3A  MOVB @12(R5),R2        Get file flags
       ANDI R2,>0100          Make sure it's not a program file
       JNE  OIN2
       MOVB @12(R5),R2
       MOV  @PABBUF,R1        Check for Fixed/Variable
       ANDI R1,>0010
       MOV  R2,R3
       ANDI R2,>8000
       SRL  R2,11
       C    R1,R2
       JNE  OIN2

       MOV  @PABBUF,R1        Check for Display/Internal
       ANDI R1,>0008
       ANDI R3,>0200
       SRL  R3,6
       C    R1,R3
       JNE  OIN2

* Check to see if we're opening the file for relative
* or sequential access.
*
* For now, we are just handling sequential access.  If it's
* relative, then the record type must be FIXED.
*
       MOV  @PABBUF,R1
       ANDI R1,>0001         Check for relative file
       JEQ  OIN10

       MOV  @PABBUF,R1        Check for Fixed/Variable
       ANDI R1,>0010
       JEQ  OIN10
       BL   @DSRERR           Relative access - error
       DATA >0300

* Everything is OK so far.  Set up the cache entry
*
OIN10  BL   @OSCASH

* Lets copy the FDR to its buffer
*
       LI   R0,256
       MOV  @4(R4),R3        Get FDR address
       MOVB @2(R4),R2
OIN11  LDCR @ZERO,4
       MOV  *R5+,R1
       LDCR R2,4
       MOV  R1,*R3+
       DECT R0
       JNE  OIN11
       AI   R3,-256       Restore R3

       LDCR @ZERO,4
       B    @DSRRT            Hooray!  We're done

**
*
* Here we are opening a directory for reading.  If the
* directory exists, then set up an entry in the open file
* cache.
*
**

* Make sure we are using fixed length records
*
OIN20  LDCR @ZERO,4
       MOVB @PABBUF+1,R1
       ANDI R1,>1000
       JEQ  OIN20A

OIN20Z BL   @DSRERR
       DATA >0700
OIN20A

* Make sure the record length is set to 38 bytes
*
       MOVB @PABBUF+4,R1
       JEQ  OIN20C
       CB   R1,@B38
       JNE  OIN20Z
OIN20B BL   @GETDDR
       CI   R5,0
       JEQ  OIN20Z

OIN21  BL   @OSCASH
       MOVB @B05,@56(R4)     Set file open attribute

* Now store the DDR in the buffer
*
OIN24
       LI   R0,256
       MOVB @2(R4),R2        DDR bank
       MOV  @4(R4),R3        DDR Address

OIN25  LDCR @B03,4           Copy DDR to buffer
       MOV  *R5+,R1
       LDCR R2,4
       MOV  R1,*R3+
       DECT R0
       JNE  OIN25

       LDCR @ZERO,4
       B    @DSRRT

* We will store the record length in the PAB to see if
* it makes BASIC happy

OIN20C MOVB @B38,@PABBUF+4
       MOV  @PABADR,R1
       AI   R1,4
       ORI  R1,>4000
       SWPB R1
       MOVB R1,@VDPWA
       SWPB R1
       MOVB R1,@VDPWA
       NOP
       MOVB @B38,@VDPWD
       JMP  OIN20B

B38    BYTE 38
