* GPL Routine >24 - Access Direct Input File
*
* This procedure gets file information from the disk
* and returns it to the caller
*
* Inputs:
*    >834C - SCSI device number and buffer bit
*    >834D - Access code
*    >834E - Pointer to file name
*    >8350 - Additional info
*
* Outputs:
*    >8350 - Error code
*              0 = success
*              non 0 = error
*
GPL24
       ANDI R12,>FF00
       AI   R12,24

* Store the scratch pad RAM in a buffer for now

       BL   @SAVPAD

       BL   @MAKEFN
       BL   @GETFDR

* Restore the scratch pad RAM
*
       BL   @RESPAD

       CI   R5,0             See if file exists
       JEQ  G24ERR

* First check the access code to see if we're transfering
* data or file parameters.
*
       MOVB @>834D,R1
       JNE  GPL24A

* We're just transferring file parameters
*

       MOVB @>8350,R2        Get pointer to additional info
       SRL  R2,8
       AI   R2,>8300+2
       MOVB @14(R5),*R2+  # of sectors
       MOVB @15(R5),*R2+
       MOVB @12(R5),*R2+  File flags
       MOVB @13(R5),*R2+  # of rec/sec
       MOVB @16(R5),*R2+  EOF offset
       MOVB @17(R5),*R2+  Logical record size
       MOVB @18(R5),*R2+  # of level 3 records
       MOVB @19(R5),*R2+
       MOVB @ZERO,*R2+       MSB of other crap
       MOVB @ZERO,*R2+
       MOVB @ZERO,*R2+       Extended Record Length
       MOVB @ZERO,*R2+

       MOVB @ZERO,@>8350     Set success code
       B    @DSRRT

* Here we have to read actual data from the file and
* transfer it to the buffer
*

GPL24A

* Let's start off by copying the FDR to a buffer in another bank
*
       LI   R4,DFDR
       LI   R0,256
GPL24B MOV  *R5+,R3
       LDCR @B02,4
       MOV  R3,*R4+
       LDCR @ZERO,4
       DECT R0
       JNE  GPL24B

* Register Usage throughout the rest of the subprogram
*
* R1 - # of sectors left to be read
* R4 - Current AU within file
* R5 - Current sector within AU
* R6 - SCSI ID
* R7, R8 - SCSI sector number to read
* R10 - Pointer to buffer (if CPU RAM)

       SRL  R1,8
       MOVB @>834C,R6
       ANDI R6,>0F00
       AI   R6,->0100

       MOVB @>8350,R2        Get pointer to additional info
       SRL  R2,8
       AI   R2,>8300

       MOV  *R2,R10

       MOV  @2(R2),R5        Get sector number
       CLR  R4

       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       LDCR @B04,4
       DIV  @SAUTBL(R3),R4

* Now search through the FDR for actual AU on disk.
*
       LDCR @B02,4
       LI   R3,DFDR+40

GPL24D MOV  *R3+,R2
       CI   R4,0
       JEQ  GPL24F
G24DD  C    R2,*R3
       JNE  GPL24E
       INCT R3
       DEC  R4
       JMP  GPL24D

G24ERR
       MOVB @B01,@>8350      Set an error code
       B    @DSRRT

GPL24E INC  R2
       DEC  R4
       JNE  G24DD

GPL24F MOV  R2,R4            R4 now has actual AU

* Now lets read the data from disk
*
GPL24H LI   R2,SECBUF
       MOV  R4,R7
       LDCR @B04,4
       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       MOV  @SAUTBL(R3),R3
       MPY  R3,R7

       A    R5,R8
       JNC  GPL24I
       INC  R7
GPL24I
       SRL  R8,1
       JNC  GPL24J
       AI   R2,>100
GPL24J SRL  R7,1
       JNC  GPL24K
       AI   R8,>8000
GPL24K

* Save the contents of scratch pad RAM before
* SCSIRD trashes it

       BL   @SAVPAD

* If we can transfer all 512 bytes of the SCSI sector, then
* we can use sector I/O to transfer it.
*
       CI   R2,SECBUF
       JNE  GPL24L
       CI   R1,1
       JEQ  GPL24L
       DEC  R3
       C    R3,R5
       JNE  GPL24T
GPL24L
       LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JNE  G24ERR

       BL   @RESPAD

* Now we transfer it either to buffer.  The buffer can be in VDP
* RAM or CPU RAM.

       LI   R0,256
       MOVB @>834C,R3
       ANDI R3,>8000
       JNE  GPL24N

* The buffer is in VDP RAM

       ORI  R10,>4000
       SWPB R10
       MOVB R10,@VDPWA
       SWPB R10
       MOVB R10,@VDPWA
       ANDI R10,>3FFF

GPL24M MOVB *R2+,@VDPWD
       DEC  R0
       JNE  GPL24M
       AI   R10,256
       JMP  GPL24O

GPL24N MOVB *R2+,*R10+
       DEC  R0
       JNE  GPL24N

GPL24O INC  R5
       LDCR @B04,4
       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       C    R5,@SAUTBL(R3)
       JNE  GPL24Q

       CLR  R5

* We now loop through the FDR to see if we go to next AU
       LDCR @B02,4
       LI   R3,DFDR+42
GPL24P C    *R3,R4
       JEQ  GPL24S
       AI   R3,4
       CI   R3,DFDR+>100
       JL   GPL24P

       INC  R4
GPL24Q

       DEC  R1
       JNE  GPL24H
       MOVB R1,@>834D        I don't know if this is right
       MOVB @ZERO,@>8350     Set success code
       LDCR @ZERO,4
       B    @DSRRT

GPL24S MOV  @2(R3),R4
       JMP  GPL24Q

**
*
*  Here we are going to use SCSI sector I/O to read the data
*
**

GPL24T LI   R0,1             Read
       MOVB R6,R0            Set drive number
       ORI  R0,>4000         Use absolute sector size
       MOVB @>834C,R3
       ANDI R3,>8000
       SOC  R3,R0            Set VDP/CPU RAM flag
       MOV  R0,@>834C
       MOV  R10,@>834E       Set buffer address
       MOV  R8,@>8350        Set 32 bit SCSI sector
       MOV  R7,@>8352

       LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSI20

       BL   @RESPAD

       AI   R10,512          Set new buffer address
       INC  R5
       DEC  R1
       JMP  GPL24O

* SAVPAD - Save the contents of scratch pad RAM to
* a buffer in bank 2

SAVPAD LDCR @B02,4
       MOV  R10,@POSIAU
       LI   R9,>8300
       LI   R10,G24BUF
SAVPD2 LI   R0,>70
SAVPD1 MOV  *R9+,*R10+
       DECT R0
       JNE  SAVPD1
       MOV  @POSIAU,R10
       LDCR @ZERO,4
       RT

* RESPAD - Restore the contents of scratch pad RAM

RESPAD LDCR @B02,4
       MOV  R10,@POSIAU
       LI   R10,>8300
       LI   R9,G24BUF
       JMP  SAVPD2
