      .include #system
      .include #macros
      .include #cio
      .include #os
      .include #16bit

_string1 == $F0
_value   == $F2
_tmp1    == $F4

maxsecs = 32
:more   = @special + @s2 + @p1 + @p2 + @p3   ; len will be ignored

;; This is more or less an example, not written for beauty or
;; anything. Maybe you can pick up some ideas about using STDIO

main:
      print       0,"SD sector reader -- 1991 Natrlich!"
      print       0,"Input diskette in drive 1",0,:more  ; same IOCB chan.
      print       0,"Press  to continue",0,:more   ; so we can use
      getc        0,@special                             ; @special to save
:again1
      bput        0,p_start,p_end-p_start    ; some bytes
      input       0,filename,4               ; get 4 chars into filename
      dpoke       _string1,filename          ; convert &filename number
      jsr         atoi                       ; try to convert ATASCII->16bit
      bcc         :again1                    ; CC: failure
      dmove       _value,startsec            ; get atoi val

:again2
      bput        0,p_end,p_output-p_end     ; some bytes
      input       0,filename,4
      dpoke       _string1,filename
      jsr         atoi
      bcc         :again2
      cmp.w       maxsecs+1,_value,@p1
      bcc         :alrite
      jsr         :toomany
      jmp         :again2     

:alrite
      move        _value,size+1
      tay                                    ; get #sectors in Y
      lsr.w       size                       ; size = value * $80

      setup_sio   1,SIOREAD,startsec,buffer,@p1+@p2+@p4
:loop
      jsr         sd_secs
      bpl         :fine
      jmp         :failure

:fine
      bput        0,p_output,size-p_output,@p1+@p2+@p3
      input       0,filename,buffer-filename,@p1+@p2+@p3+@special
      open        1,8,0,filename,@p1+@p2+@p3+@p4
      bmi         :noopen
      bput        1,buffer,size,@p1+@p2+@special
      php
      close       1,@p1+@special
      plp
      bpl         :ok
      jsr         :diskfull
      jmp         :fine

:ok
      print       0,"Done"
      rts


:failure:
      print       0,"Couldn't read sectors"
      rts

:noopen:
      print       0,"Couldn't open output file"
      rts

:diskfull:
      print       0,"Disk full ? Retry with another..."
      rts

:toomany
      print       0,"Too many sectors requested"
      rts

p_start:    .byte "Enter first sector no: "
p_end:      .byte "      Sectors to read: "
p_output:   .byte "Output file name: "

size:       .word 0
endsec:     .ds   2
startsec:   .ds   2
filename:   .ds   64
buffer:     .ds   128*maxsecs

