*
* These are the disk routines from Blahtune, split out from
* the file diskio.w.s; they use the custom kernal routines
* contained in kobb.s, the other half of diskio.w.s
*
* (Ignore the ENTs)


*
* Disk routines.  This is a common disk access routine
* for the music composer.  A string address is passed in
* in (.X,.A) = (lo,hi) which is PRINTed to the screen
* (presumably it includes a screen clear!).  The
* appropriate load and save addresses must be set up
* ahead of time as well.  The program then prints a
* short menu and handles any disk accesses.
*
* New addition: a 2-byte identifier must also be
* specified.  If greater than $FF00 the byte check is
* skipped, otherwise these two-bytes are compared with
* the first two data bytes in the file, and if they
* do not match an incorrect file type is assumed.
*

READST   = $FFB7
SETLFS   = $FFBA
SETNAM   = $FFBD
OPEN     = $FFC0
CLOSE    = $FFC3
CHKIN    = $FFC6
CHKOUT   = $FFC9
CLRCHN   = $FFCC
CHRIN    = $FFCF
CHROUT   = $FFD2
LOAD     = $FFD5
SAVE     = $FFD8
STOP     = $FFE1
GETIN    = $FFE4
CLALL    = $FFE7

FAQ2     = $69
ACC      = FAQ2
AUX      = ACC+2
EXT      = AUX+2
BLNSW    = $CC
BLNON    = $CF
BLNCT    = $CD


LOADID   ENT
         DA 00            ;2-byte identifier
LOADADDR ENT
         DA 00            ;Load target address
SAVESTRT ENT
         DA 00            ;Beginning of block to save
SAVEEND  ENT
         DA 00            ;End of block to save
SAVEFLAG ENT
         DFB 00           ;Save disable
NEWDATA  ENT
         DFB 00           ;Was new data loaded?

STRADDR  DA 00

DISKIO   ENT
         STX STRADDR
         STA STRADDR+1
         LDA #00
         STA NEWDATA

MAIN     LDX STRADDR
         LDA STRADDR+1
         JSR PRINT
         JSR STROUT
         DFB 159          ;Cyan
         DFB 13,13,13
         DFB 32,32
         TXT 'l - Load file'
         DFB 13,32,32
         TXT 's - Save file'
         DFB 13,32,32
         TXT 'd - Directory'
         DFB 13,32,32
         TXT '@ - Send disk command'
         DFB 13,32,32
         TXT 'c - Change drive no.'
         DFB 13,32,32
         TXT 'x - Exit'
         DFB 13,32,32
         TXT 'Current drive = '
         DFB 00

         LDA $BA
         CMP #10
         BCC :PRINT
         LDA #'1'
         JSR CHROUT
         LDA $BA
         SEC
         SBC #10
:PRINT   ORA #48
         JSR CHROUT
         LDA #13
         JSR CHROUT

:WAIT    JSR STOP
         BEQ :EXIT
         JSR GETIN
         BEQ :WAIT

         CMP #'l'
         BEQ :LOAD
         CMP #'s'
         BEQ :SAVE
         CMP #'d'
         BEQ :DIR
         CMP #'@'
         BEQ :CMD
         CMP #'c'
         BEQ :CHDR
         CMP #'x'
         BNE :WAIT

:EXIT    RTS
:LOAD    JSR LOADFILE
         BCS :WAITJMP
         JMP WAITKEY
:SAVE    JSR SAVEFILE
         BCS :WAITJMP
         JMP WAITKEY
:DIR     JSR PRINTDIR
         JSR WAITKEY
         JMP MAIN
:CMD     JSR SENDCMD
         JSR GETERR
:WAITJMP JSR WAITKEY
         JMP MAIN
:CHDR    
         LDA #13
         JSR CHROUT
         LDA #'#'
         JSR CHROUT
         JSR GETBYTE
         BEQ :DONE
         CMP #20
         BCS :DONE
         STA $BA
:DONE    JMP MAIN

GETBYTE  
         LDX #3
         JSR INPUT
         BEQ :RTS
         LDX #>STRBUF
         LDA #<STRBUF
         LDY #10
         JSR ASCTONUM
         BCS :RTS
         LDA ACC
:RTS     RTS

WAITKEY  ENT
         JSR STROUT
         DFB 13,13
         TXT 'Press a key to continue...',00
         LDA $0286        ;Set blink color to
         STA $0287        ;current color.
         LDA #00
         STA BLNSW
:WAIT    JSR GETIN
         BEQ :WAIT
         INC BLNSW
         LDA #32          ;Erase reversed char
         JMP CHROUT

*
* Disk routines
*
* PRINTDIR reads the directory from the current device
* and prints it to the screen.
*
PRINTDIR 
         LDA #01          ;File no.
         LDX $BA          ;Current device number
         LDY #00          ;Secondary address
         JSR SETLFS
         LDA #1
         LDX #<:DOLLAR
         LDY #>:DOLLAR
         JSR SETNAM
         JSR OPEN
         LDX #01
         JSR CHKIN
         BCS :END         ;Error if carry set

         JSR CHRIN        ;Grab load address
         JSR CHRIN
:LOOP1   LDA #13
         JSR CHROUT
         JSR CHRIN        ;Line link
         JSR CHRIN
         JSR CHRIN        ;Line number (file size)
         TAY
         JSR CHRIN
         TAX
         JSR READST
         BNE :END
         TYA
         LDY #10          ;Base
         JSR PRINTNUM     ;Print out the number in X,Y
         LDA #32
         JSR CHROUT       ;Add a space to look nice
:LOOP2   JSR CHRIN
         TAX
         BEQ :LOOP1
         JSR CHROUT
         BNE :LOOP2
:END     LDA #01
         JSR CLOSE
         JMP CLRCHN
:DOLLAR  TXT '$'

*
* SAVEFILE, whose purpose is awfully self-evident.
*
SAVEFILE 
         LDA SAVEFLAG
         BNE :NOSAVE
         LDY #00
:LOOP    LDA :TEXT,Y
         BEQ :CONT
         JSR CHROUT
         INY
         BNE :LOOP
:CONT    
         LDX #16          ;Number of chars max
         JSR INPUT
         BEQ :ABORT       ;Zero set means aborted input
                          ;Otherwise A and Y contain # of
                          ;chars entered
         LDX #<STRBUF     ;See INPUT for more info
         LDY #>STRBUF
         JSR SETNAM
         LDA #08          ;File no.
         LDX $BA          ;Current device number
         LDY #01          ;Secondary address
         JSR SETLFS
         LDA SAVESTRT
         STA FAQ2
         LDA SAVESTRT+1
         STA FAQ2+1
         LDX SAVEEND
         LDY SAVEEND+1
         LDA #FAQ2
         JSR SAVE
         BCS :ERROR
         JSR CLRCHN
         LDA #13
         JSR CHROUT
         CLC
         RTS

:NOSAVE  LDY #00
:L3      LDA :TEXT3,Y
         BEQ :END
         JSR CHROUT
         INY
         BNE :L3

:ABORT   LDY #00
:LOOP2   LDA :TEXT2,Y
         BEQ :END
         JSR CHROUT
         INY
         BNE :LOOP2

:ERROR   LDY #00
:L1      LDA :ERTEXT,Y
         BEQ :END
         JSR CHROUT
         INY
         BNE :L1
:END     JSR GETERR
         SEC
         RTS

:TEXT    DFB 13
         TXT 'save file:'
         HEX 00
:ERTEXT  DFB 13
         TXT '*** save error ***'
:TEXT2   DFB 13
         TXT 'save aborted'
         DFB 13,00

:TEXT3   DFB 13
         TXT 'old versions are load only'
         DFB 13,00

*
* LOADFILE - remarkably similar to SAVEFILE
*
LOADFILE 
         LDY #00
:LOOP    LDA :TEXT,Y
         BEQ :DONE
         JSR CHROUT
         INY
         BNE :LOOP
:DONE    
         LDX #16          ;Number of chars max
         JSR INPUT
         BEQ :ABORT       ;Zero set means aborted input

         LDX #<STRBUF     ;See INPUT for more info
         LDY #>STRBUF
         JSR SETNAM
         LDA #08          ;File no.
         LDX $BA          ;Current device number
         LDY #00          ;Secondary address
         JSR SETLFS

         LDA LOADID+1     ;ID check
         CMP #$FF
         BEQ :SKIP
         JSR OPEN
         BCS :ERROR
         LDX #$08
         JSR CHKIN

         JSR CHRIN        ;Load address
         JSR CHRIN
         JSR CHRIN
         CMP LOADID
         BNE :IDERR
         JSR CHRIN
         CMP LOADID+1
         BNE :IDERR
         LDA #8
         JSR CLOSE
:SKIP    
         LDA #08          ;File no.
         LDX $BA          ;Current device number
         LDY #00          ;Secondary address
         JSR SETLFS
         LDA #0
         LDX LOADADDR
         LDY LOADADDR+1
         JSR LOAD
         BCS :ERROR

         JSR CLRCHN
         LDA #13
         STA NEWDATA
         JSR CHROUT       ;Put a blank line to look nice
         CLC
         RTS

:ABORT   LDY #00
:LOOP3   LDA :TEXT2,Y
         BEQ :END
         JSR CHROUT
         INY
         BNE :LOOP3

:ERROR   JSR CLALL
         JSR CLRCHN
         LDY #00
:LOOP2   LDA :ERTEXT,Y
         BEQ :END
         JSR CHROUT
         INY
         BNE :LOOP2
:END     JSR GETERR
         SEC
         RTS

:IDERR   JSR CLALL
         JSR CLRCHN
         LDY #00
:L2      LDA :TEXT3,Y
         BEQ :END
         JSR CHROUT
         INY
         BNE :L2


:ERTEXT  DFB 13
         TXT '*** load error ***'
:TEXT2   DFB 13
         TXT 'load aborted.'
         DFB 13,00

:TEXT3   DFB 13
         TXT 'incorrect file type.'
         DFB 13,00

:TEXT    DFB 13           ;Return
         TXT 'load file:'
         HEX 00

*
* SENDCMD sends a command to the current drive
*
SENDCMD  
         LDA #13
         JSR CHROUT
         LDA #'>'
         JSR CHROUT
         LDX #39
         JSR INPUT
         BEQ :ERROR

         LDA #$0F
         LDX $BA
         LDY #$0F
         JSR SETLFS
         LDA #00
         JSR SETNAM
         JSR OPEN
         BCS :ERROR
         LDX #$0F
         JSR CHKOUT
         LDY #00
:LOOP    LDA STRBUF,Y
         BEQ :ERROR
         JSR CHROUT
         INY
         BNE :LOOP

:ERROR   JSR CLRCHN
         LDA #$0F
         JSR CLOSE
         JMP CLRCHN

*
* GETERR prints the error message from the current disk drive.
*
GETERR   
         LDA #13
         JSR CHROUT
*
* This method is a bit faster on output to screen.
*
         LDA #$0F
         LDX $BA
         LDY #$0F
         JSR SETLFS
         LDA #00
         JSR SETNAM
         JSR OPEN
         BCS :ERROR
         LDX #$0F
         JSR CHKIN
:LOOP    JSR CHRIN
         CMP #$0D
         BEQ :EXIT
         JSR CHROUT
         BNE :LOOP
:EXIT    JSR CHROUT
         JSR CHROUT       ;One more to look nice
         LDA #15
         JSR CLOSE
         JMP CLRCHN

:ERROR   PHA
         LDY #00
:LOOP2   LDA :TEXT,Y
         BEQ :OUT
         JSR CHROUT
         INY
         BNE :LOOP2
:OUT     PLA
         CLC
         ADC #48          ;Convert to CHR$
         BNE :EXIT

:TEXT    TXT 'open error #'
         HEX 00


* This method seems a little slower
         DO 0             ;Don't assemble
         LDA $BA
         JSR TALK         ;Command device to talk
         LDA #$6F         ;Channel 15
         STA SA           ;Current secondary address
         JSR TKSA
         LDA #$0D         ;Set for initial CR
:LOOP    JSR CHROUT
         JSR ACPTR
         CMP #$0D
         BNE :LOOP
         JSR CHROUT       ;Do CR
         JMP UNTLK        ;... and exit
         FIN
