   .if .not .def _CIO_
_CIO_ = 1

      .if .not .def _SYSTEM_
         .include #system
      .endif
      .if .not .def _MACROS_
         .include #macros
      .endif

@READ     = 4
@WRITE    = 8
@APPEND   = 9
@UPDATE   = @WRITE + @READ

@S_TXTWIN    = 16
@S_NOCLEAR   = 32

; -------------------------------------------------------------
; For the macro...
;              CH_X  channel[,select]
; will get us
;     channel in X
; -------------------------------------------------------------
   .macro ch_x
      .if %0 = 1 .or .not [%2 & @special]  ;; this works as in `C`
         .if %0 = 2 .and .not [%2 & @p1]
            ldx   #%1
            lda   _op_tab,x
            tax
         .else
            .if %1 >= 8
               ldx   #%1
            .else
               ldx   #%1*$10
            .endif
         .endif
      .endif
   .endm

; -------------------------------------------------------------
; For the macro...
;              OPEN  channel,aux1,aux2,filespec[,select]
; will get us
;     channel in X
; -------------------------------------------------------------
   .macro open
@3    .= @p1 ! @p2 ! @p3 ! @p4 ! @s4
      .if %0 = 5
@3       .= %5
      .endif
      ch_x  %1,@3
      @moke_x  %2,icax1,[@3 & @p2] / @p2
      @moke_x  %3,icax2,[@3 & @p3] / @p3
      .if @3 & @s4
         jmp   *+%4+4
         .byte %$4,0
         @dmoad *-%4-1,@p1
      .else
         @dmoad %4,[@3 & @p4] / @p4
      .endif
      jsr   open
   .endm

; ------------------------------------------------------------
; For the macro...
;            CLOSE  channel[,select]
; will get us
;   channel in X
; ------------------------------------------------------------
   .macro close
@3    .= @p1 ! @special
      .if %0 = 2
@3       .= %2
      .endif
      ch_x  %1,@3
      jsr   close
   .endm

; -------------------------------------------------------------
; Generic macro for calls of the form
;        channel,buffer,len[,select]
; Like INPUT PRINT BPUT BGET
; Usage: IO channel,buffer,len,select,jumplabel
; Default assumption: channel in X everything POKEd
; -------------------------------------------------------------
   .macro   _io
       ch_x  %1,%4
       @dmoke_x %3,icbll,[%4 & @p3] / @p3
       @dmoad   %2,[%4 & @p2] / @p2
      jsr   %5
   .endm

; -------------------------------------------------------------
; For the macros...
;              BPUT  channel,buffer,len[,select]]
;              BGET  channel,buffer,len[,select]
;              PRINT channel,buffer[,len[,select]]
;              INPUT channel,buffer,len[,select]
; The ommission of len on PRINT is only recommended
; for quick'n dirty hacks, since your wasting 3 bytes.
; Assumption:
;     channel in X everything POKEd
; -------------------------------------------------------------
   .macro bput
@3    .= @p1 ! @p2 ! @p3
      .if %0 = 4
@3       .= %4
      .endif
      _io %1,%2,%3,@3,bput
   .endm

   .macro bget
@3    .= @p1 ! @p2 ! @p3
      .if %0 = 4
@3       .= %4
      .endif
      _io %1,%2,%3,@3,bget
   .endm

   .macro print
      .if %0=1
         putc  %1,155
      .else
@3       .= @p1 ! @p2 ! @p3
         .if %0 > 3
@3          .= %4
         .else
            .if %0 = 2
@3             .= @3 ! @s2
            .endif
         .endif
         .if @3 & @s2
            jmp  *+%2+3
            .byte %$2
            _io %1,*-%2,%2,[@3 & @special] ! [@3 & @p1] ! @p2 ! @p3,print
         .else
            _io %1,%2,%3,@3,print
         .endif
      .endif
   .endm

   .macro input
@3    .= @p1 ! @p2 ! @p3
      .if %0 = 4
@3       .= %4
      .endif
      _io %1,%2,%3,@3,input
   .endm

; -------------------------------------------------------------
; For the macro...
;              XIO  command,channel,aux1,aux2,buffer[,select]
; will get us
;     channel in X
; -------------------------------------------------------------
   .macro xio
@3    .= @p1 + @p2 + @p3 + @p4 + @p5
      .if %0 = 6
@3       .= %6
      .endif
      ch_x %2,[@3 & @p2]/@p2 ! [@3 & @special]
      @moke_x  %1,iccom, @3
      @moke_x  %3,icax1,[@3 & @p3] / @p3
      @moke_x  %4,icax2,[@3 & @p3] / @p4
      @dmoad   %5,[@3 & @p5] / @p5
      jsr   ciov
   .endm


   .macro putc
@3    .= @p1 + @p2
      .if %0 = 3
@3       .= %3
      .endif
      ch_x     %1, @3
      @moad    %2,[@3 & @p2] / @p2,@y
      jsr      putc
   .endm

   .macro getc
@3    .= @p1
      .if %0 = 2
@3       .= %2
      .endif
      ch_x     %1, @3
      jsr      getc
   .endm

   .endif


