* LOADER - FLEX LOADER ROUTINE
*
* COPYRIGHT (C) 1980 BY
* TECHNICAL SYSTEMS CONSULTANTS, INC.
* PO BOX 2570, W.LAFAYETTE, IN 47906
*
* LOADS FLEX FROM DISK. ASSUMES DRIVE IS ALREADY
* SELECTED AND A RESTORE HAS BEEN PERFORMED BY THE
* ROM BOOT AND THAT STARTING TRACK AND SECTOR OF
* FLEX ARE AT $A105 AND $A106.  BEGIN EXECUTION
* BY JUMPING TO LOCATION $A100.  JUMPS TO FLEX
* STARTUP WHEN COMPLETE.
*

* EQUATES

STACK EQU $A07F
SCTBUF EQU $A300 DATA SECTOR BUFFER

* START OF UTILITY

 ORG $A100
LOAD LDS #STACK SETUP STACK
 BRA  LOAD0

TRK FCB 0 FILE START TRACK
SCT FCB 0 FILE START SECTOR
DNS FCB 0 DENSITY FLAG
TADR FDB $A100 TRANSFER ADDRESS
LADR FDB 0 LOAD ADDRESS
SBFPTR FDB 0 SECTOR BUFFER POINTER

LOAD0 LDAA TRK SETUP STARTING TRK & SCT
 STAA SCTBUF
 LDAA SCT
 STAA SCTBUF+1
 LDX #SCTBUF+256
 STX SBFPTR

* PERFORM ACTUAL FILE LOAD

LOAD1 BSR GETCH GET A CHARACTER
 CMPA #$02 DATA RECORRD HEADER?
 BEQ LOAD2 SKIP IF SO
 CMPA #$16 XFR ADDRESS HEADER?
 BNE LOAD1 LOOP IF NEITHER
 BSR GETCH GET TRANSFER ADDRESS
 STAA TADR
 BSR GETCH
 STAA TADR+1
 BRA LOAD1 CONTINUE LOAD
LOAD2 BSR GETCH GET LOAD ADDRESS
 STAA LADR
 BSR GETCH
 STAA LADR+1
 BSR GETCH GET BYTE COUNT
 TAB PUT IN B
 BEQ LOAD1 LOOP IF COUNT=0
LOAD3 PSHB
 BSR GETCH GET A DATA CHARACTER
 PULB
 LDX LADR GET LOAD ADDRESS
 STAA 0,X PUT CHARACTER
 INX
 STX LADR
 DECB END OF DATA IN RECORD?
 BNE LOAD3 LOOP IF NOT
 BRA LOAD1 GET ANOTHER RECORD

* GET CHARACTER ROUTINE - READS A SECTOR IF NECESSARY

GETCH LDX SBFPTR CHECK SECTOR BUFFER POINTER
 CPX #SCTBUF+256 OUT OF DATA?
 BEQ GETCH2 GO READ SECTOR IF SO
GETCH1 LDAA 0,X ELSE, GET A CHARACTER
 INX
 STX SBFPTR UPDATE POINTER
 RTS
GETCH2 LDX #SCTBUF POINT TO BUFFER
 LDAA 0,X GET FORWARD LINK (TRACK)
 BEQ GO IF ZERO, FILE IS LOADED
 LDAB 1,X ELSE, GET SECTOR
 BSR READ READ NEXT SECTOR
 BNE LOAD START OVER IF ERROR
 LDX #SCTBUF+4 POINT PAST LINK
 BRA GETCH1 GO GET A CHARACTER

* FILE IS LOADED, JUMP TO IT

GO LDX TADR GET TRANSFER ADDRESS
 JMP 0,X JUMP THERE

* READ SINGLE SECTOR
*
* THIS ROUTINE MUST READ THE SECTOR WHOSE TRACK
* AND SECTOR ADDRESS ARE IN A AND B ON ENTRY.
* THE DATA FROM THE SECTOR IS TO BE PLACED AT
* THE ADDRESS CONTAINED IN X ON ENTRY.
* IF ERRORS, A NOT-EQUAL CONDITION SHOULD BE
* RETURNED. THIS ROUTINE WILL HAVE TO DO SEEKS.

READ LDAB #$FF MUST BE USER SUPPLIED!
 RTS THIS CODE DISABLES READ!

 END
