; Some Subroutines and Macros to ease the use of the filing system
; Include this file AFTER MACS.ASM
; Routines to Use:
; Macro  OPEN_IN  Number,filename         LEA Fnam,A0  / OPEN_IN  1,A0
; Macro  OPEN_NEW Number,filename         LEA Fnam2,A1 / OPEN_NEW 2,A1
; Macro  OPEN_RW  Number,filename         LEA Fnam3,A2 / OPEN_RW  3,A2
; Macro  FCLOSE   Number                  FCLOSE 3
; Macro  FSIZE    Number,Rn               FSIZE 1,D0
; Macro  LOAD     Number,An               LOAD  2,A5
;
; ------------------------------------------------
;  Open file for read only, store handle in CHNRn
; ------------------------------------------------
OPEN_IN: MACRO    $\1,$\2
      MOVE.L      \2,-(A7)
      BSR         _OPEN1
      LEA         CHNR\1,A6
      MOVE.L      (A7)+,(A6)
      ENDM

; ---------------------------------------
;  Open new file , store handle in CHNRn
; ---------------------------------------
OPEN_NEW: MACRO    $\1,$\2
      MOVE.L      \2,-(A7)
      BSR         _OPEN2
      LEA         CHNR\1,A6
      MOVE.L      (A7)+,(A6)
      ENDM

; -----------------------------------------------------
;  Open old file for read\write, store handle in CHNRn
; -----------------------------------------------------
OPEN_RW: MACRO    $\1,$\2
      MOVE.L      \2,-(A7)
      BSR         _OPEN3
      LEA         CHNR\1,A6
      MOVE.L      (A7)+,(A6)
      ENDM

; -------------------------------------
; close file with given channel number
; -------------------------------------
FCLOSE: MACRO $\1
      MOVE.L      CHNR\1(PC),-(A7)
      BSR         _FCLOSE
      ENDM

; -------------------------------------------
; get size of file with given channel number
; -------------------------------------------
FSIZE: MACRO $\1,$\2
      MOVE.L      CHNR\1(PC),-(A7)
      BSR         _FSIZE
      MOVE.L      (A7)+,\2
      ENDM

; -----------------------------------------------
; Load file with given channel number into memory
; -----------------------------------------------
LOAD: MACRO $\1,$\2
      MOVE.L      \2,-(A7)
      MOVE.L      CHNR\1(PC),-(A7)
      BSR         _LOAD
      ENDM

      BRA            _end_File_sbrs
; -------------------------------------------------
; Channel number table. Maximum channel number is 9
; -------------------------------------------------
CHNR0:   DS.L  1
CHNR1:   DS.L  1
CHNR2:   DS.L  1
CHNR3:   DS.L  1
CHNR4:   DS.L  1
CHNR5:   DS.L  1
CHNR6:   DS.L  1
CHNR7:   DS.L  1
CHNR8:   DS.L  1
CHNR9:   DS.L  1
; -------------------------------------------------------------------------
; open file with pointer to name on stack 1 as 1005 / 2 as 1006 / 3 as 1004
; -------------------------------------------------------------------------
_OPEN1:  ; open file for Input only
      MOVEM.L        D0-D3/A0,-(A7)
      MOVE.L         #1005,D2
      BRA.S          _DO_OPEN
_OPEN2:  ; open new file for output
      MOVEM.L        D0-D3/A0,-(A7)
      MOVE.L         #1006,D2
      BRA.S          _DO_OPEN
_OPEN3:  ; open file for read / write
      MOVEM.L        D0-D3/A0,-(A7)
      MOVE.L         #1004,D2
_DO_OPEN:
      MOVE.L         24(A7),D1 ; get pointer to file name
      MOVE.L         dos(PC),A6
      JSR            _LVOOpen(A6)
       TST.L         D0
       BNE.S         _Open_ok
       BSR           PRINT
       DC.B          'can not open file',10,0
      NOP
_Open_ok:
      MOVE.L         D0,24(A7) ; store handle
      MOVEM.L        (A7)+,D0-D3/A0
      RTS

; --------------------------------
; close file with handle on stack
; --------------------------------
_FCLOSE:
      MOVEM.L        D0-D3/A0,-(A7)
      MOVE.L         24(A7),D1   ; get handle of file
      BEQ.S          _CLOSEX
      MOVE.L         dos(PC),A6
      JSR            _LVOClose(A6)   ; and close file
_CLOSEX:
      MOVEM.L        (A7)+,D0-D3/A0
      MOVE.L         (A7),4(A7)  ; Tidy up stack
      ADDQ.L         #4,A7
      RTS

; ----------------------------------------
; get length of file with handle on stack
; ----------------------------------------
_FSIZE:
      MOVEM.L        D0-D3/A0,-(A7)
      MOVE.L         24(A7),D1   ; get handle of file
      BEQ.S          _FSIZEX
       MOVEQ         #0,D2    ; no distance
       MOVEQ         #1,D3    ; to end of file
       MOVE.L        dos(PC),A6
       JSR           _LVOSeek(A6)
       MOVE.L        24(A7),D1   ; get handle of file
       MOVEQ         #0,D2
       MOVEQ         #0,D3    ; relative to current position
       JSR           _LVOSeek(A6) ; get filepointer = length of file
      MOVE.L         24(A7),D1   ; get handle of file
      MOVE.L         D0,24(A7)   ; store file length

; go  back to start of file

       MOVEQ         #0,D2
       MOVEQ         #-1,D3       ; relative to start of file
       JSR           _LVOSeek(A6)
_FSIZEX:
      MOVEM.L        (A7)+,D0-D3/A0
      RTS

; -----------------------------------------------
; now load File with supplied handle into memory
; -----------------------------------------------
_LOAD:
      MOVEM.L        D0-D3/A0,-(A7)
       MOVE.L        24(A7),D1    ; handle of file
      BEQ.S          _LOADX
      MOVE.L         D1,-(A7)
      BSR            _FSIZE
      MOVE.L         (A7)+,D3     ; number of bytes to read
      MOVE.L         28(A7),D2    ; start of buffer
      MOVE.L         dos(PC),A6
      JSR            _LVORead(A6)     ; load file
_LOADX:
      MOVEM.L        (A7)+,D0-D3/A0
      MOVE.L         (A7),8(A7)   ; tidy up stack
      ADDQ.L         #8,A7
      RTS

_end_File_sbrs:
      NOP
