LOADER PROC
			
; AsmIIGS directives to set equivalent 6502/65C02 MODE.
	MACHINE	M65C02
	LONGA	OFF
	LONGI	OFF

************************************************************
*                                                          *
*          ProDOS / Applesoft Command Interpreter          *
*                                                          *
*         Copyright Apple Computer, Inc.,  1983-90         *
*                                                          *
************************************************************


*******************  REVISION HISTORY  ************************************************
*
*  NOTE: With each change made, VERDATE in the TABLES file and this
*        history must be updated.  With each revision, the version
*        number in the LOADER file must be updated.
*
*  The following changes have been made:
*
*  Rev 1.0 to Rev 1.0.1a01 - Dale M. Arends - 2/13/84
*
*  1.  Bug in VARPACKER fixed.  Used when CHAINing & Var storage.
*        (File: CI.BUFMGR)
*  2.  Bug in BSAVE when BSAVEing a non-BINARY file fixed.
*        (File: CMDS1) NOTE: This section is still being debated!
*  3.  The Loader has had the STARTUP filename moved to the 4th
*        byte (preceeded by a JMP) so that a Selector can start a 
*        different program by name.  (File: LOADER)
*
*  Rev 1.0.1A01 to Rev 1.0.1A02 - Dale M. Arends - 4/11/84
*
*  1.  Bug in BSAVE when BSAVEing a non-BINARY file fixed.  System
*        returned to original design operation. (File: CMDS1)
*  2.  Bug fixed in FIELD parameter of READ routine.  Bad buffer
*        address.  (File: CMDS3)
*  3.  RUNTIME support removed.
*
*  Rev 1.0.1A02 to Rev 1.0.1A03 - Dale M. Arends - 4/20/84
*
*  1.  Bug in APPEND fixed.  (After OPENing & READing from a random
*        access file, then APPENDing to another file, the record length
*        of the RA file was used in mark positioning.) (File: CMDS3)
*
*  Rev 1.0.1A03 to Rev 1.1XA01 - Dale M. Arends - 4/26/84
*
*  1.  BYE Command added.
*        (Files: CMDS2, TABLES)
*  2.  VERDATE removed and replaced with VERNUM.  Loader MODified
*        to put VERNUM into MLI Global Page location IVERSION.
*        (Files: LOADER, CI.EQU, TABLES)
*
*  Rev 1.1XA01 renamed to Rev 1.1 - Dale M. Arends - 4/26/84
*
*  Rev 1.2 - Grady Ward - 10/87
*
*  (1) Changed CATALOG in <CMDS2> to ignore TOTENT when enumerating a
*      directory; network servers were dying
*
* 1.2 Version without comma after Apple in splash
*  (2) Changed CATALOG further so control s pause won't be 
*     prematurely terminated
*
* Rev 1.3 - Lee Collings - 1/89
*
* (1) Changed Version number to 1.3 in Loader
* (2) Changed VERNUM to $03 in Tables
* (3) FIX1 in BSAVE at $AE4A to jump to Patch in Main code module
* (4) FIX2 in BSAVE at $AEC9 to jump to Patch in Main code module
* (5) Patched Chain/Store in Main code module
* (6) Added MTR command in Tables to go from BASIC to Monitor
* (7) Converted Source code to MAX/MPW
*
* Rev 1.4 - Lee Collings - 6/89
*
* (1) Added RTS at the end of the BSAVE/BLOAD patch in "MAIN" to return to calling routine
* (2) Changed Loader to add this comment and to change version to 1.4
* (3) Changed VerNum in Main code section to $04
*
* Rev 1.4.1 - Lee Collings - 6/90
*
* (1) Added a LDA and STA in "Main" BSave routine to correct B-Parm bug
* (2) Changed Version to 1.4.1 in Loader
* (3) Changed VerNum in Main code section to $04
* (4) Changed copyright notice to include 1990
*
* Rev 1.5 - Lee Collings - 7/91
* (1) Added change to Main to fix Flash bug in If/Then statement
* (2) Changed revision level to 1.5 in loader
* (3) Changed copyright notice to include 1992
* (4) Fixed bug in Main section of BSAVE routine to eliminate AuxFile type problem
* (5) Fixed bug in Reading Catalog from Slot 3 RAM disk
* (6) Removed "RNFLG" DC.B from MAIN (token never used)
* (7) Added S16 File support to catalog routine (to properly display file types)
* (8) Added the ability to Launch S16 (GS/OS) Applications from BASIC
************************************************************************************

			INCLUDE 'EQUATES'
			
			IMPORT	DEFSLT
			IMPORT	DEFDRV
			IMPORT  RSHIMEM
			IMPORT  VERNUM
			

			JMP	CILDR
			DCB.B	2,$EE			;Marker to show Catalyst there is a buffer
			DC.B	$41	 			;65 char buffer
NAMEHEL		DC.B	$7	 			;Length of the STARTUP program name
			DC.B	'STARTUP'		;Startup program filename
NAMEHEL1	EQU	*-NAMEHEL
			DS.B	$41-NAMEHEL1
CILDR		EQU *
			LDA	#CIDST				;Load CI into Destination
			STA	3
			LDA	#CIADR				;Address of CI
			STA	1
			LDA	#0
			STA	0
			STA	2
			LDX	#CILEN				;Length of CI
			TAY
			JSR	MVPGE				;Begin to move CI to proper location
			LDX	#1
			LDA	#CIGLB				;Load CI Globals
			STA	3
			JSR	MVPGE				;goto Move routine

			LDA	#$15	 			;CHR$(21) CTRL-Q
			JSR	$FDED	 			;COUT to turn off 80 col firmware
			JSR	$FE84	 			;Set Normal Video
			JSR	$FB2F	 			;SET DEFAULT MONITOR STUFF
			JSR	HOME
			LDX	#$17	 			;CLEAR MEMTABLES
			LDA	#0
CLMENT		STA	MEMTABL,X
			DEX
			BNE	CLMENT

*  Set up Memory Map in MLI Globals

			LDA	#$CF
			STA	MEMTABL
			LDA	#$3F
			STA	MEMTABL+$13
			LDA	#$FF
			STA	MEMTABL+$14
			STA	MEMTABL+$15
			STA	MEMTABL+$16
			LDA	#$C3
			STA	MEMTABL+$17
			LDA	APSOFT	 			;is AppleSoft in system?
			CMP	#$4C
			BNE	ERRORS
			LDA	MACHID	 			;is this a 64K system?
			AND	#$20
			BEQ	ERRORS
	
			LDX	#3
MVSPOOK		LDA	SPOOK,X
			STA	CSWL,X
			DEX
			BPL	MVSPOOK
			JMP	APSOFT	 			;GO Initialize APPLESOFT.

ERRORS		LDX	#$27
PRERRMES	LDA	ERRMES,X
			STA	$628,X	 			;Put Err Msg right on screen
			DEX
			BPL	PRERRMES
			STA	$3F4
			STA	$3F3
ALWAYS		BMI	ALWAYS

MVPGE		DEY
			LDA	(0),Y	 			;Move a page from pointer 0
			STA	(2),Y	 			; to pointer 2
			TYA
			BNE	MVPGE
			INC	1	 				;Adjust pointers to next page
			INC	3
			DEX						;One less page to do
			BNE	MVPGE
MVPGX		RTS

RESTART		EQU	*
			CMP	#$DD	 			;IS IT PROMPT?
			BNE	MVPGX
START1		LDA	OUT0	 			;RESTORE OUTPUT HOOK TO NORMAL I/O
			STA	CSWL
			LDA	OUT0+1
			STA	CSWH

			LDA	DVNUM	 			;SET DEFAULT SLOT/DRIVE.
			STA	BUNIT
			LDX	#1	 				;ASSUME DRIVE 1
			ASL	A
			BCC	BRNCH1
			INX						;UNLESS IT'S DRIVE 2.
BRNCH1		STX	DEFDRV
			ASL	A
			ROL	A
			ROL	A
			ROL	A
			AND	#7
			STA	DEFSLT
			LDX	#$C1	 			;NOW ATTACH ALL FOUND I/O DEVICES.
			LDY	#2
			LDA	SLTBYT
			LSR	A
SETSLOT		LSR	A
			PHA
			BCC	NOROM
			LDA	#0
			STA	OUT0,Y
			STA	IN0,Y
			TXA
			STA	OUT0+1,Y
			STA	IN0+1,Y
NOROM		INY
			INY
			INX
			PLA
			BNE	SETSLOT	 			;LOOP UNTIL ALL DEVICES ID'ED

			LDA	#CIMEM	 			;SET HIMEM
			STA	$74
			STA	$70
			STA	RSHIMEM

			LDA	PFIXPTR	 			;Is there a PREFIX set?
			BEQ	NOPREFIX	 		;If 0 then No Prefix is set
ISPRFX		JSR	GOPRO
			DC.B	GPFIX	 		;Get the prefix
			DC.W	PRPARM	 		;  and put it in PBUF
			BNE	NOGOOD
			LDX	PBUF	 			;Length of prefix
			LDA	#$2F	 			;Target char for scan
SCAN10		CMP	PBUF,X	 			;Scan & count the slashes
			BNE	SCAN11
			INC	TEMPOR	 			;1 more slash
SCAN11		DEX
			BNE	SCAN10
			LDA	TEMPOR	 			;Get # slashes in prefix
			CMP	#3	 				;3 or more indicate prefix is a subdir
			BCS	ISHEL0	 			;  so don't Null it out
			JSR	GOPRO	 			;SET PREFIX TO NUL.
			DC.B	SPFIX
			DC.W	NULPFX

* NOW SET PREFIX TO BOOT DEVICE.

NOPREFIX	JSR	GOPRO	 			; Do ONLINE call
			DC.B	ONLINE
			DC.W	OLPARM
			BNE	NOGOOD	 			;BRANCH IF PROBLEMS.
			LDA	PBUF+1	 			;GET VOLUME NAME LENGTH.
			AND	#$F	 				;STRIP DEVNUM.
			BEQ	NOGOOD	 			;BRANCH IF ERROR.
			TAX						;ADD 1 FOR LEADING '/'
			INX
			STX	PBUF	 			;SAVE PREFIX LENGTH.
			LDA	#$2F	 			;PLACE LEADING '/' IN PBUF.
			STA	PBUF+1
			JSR	GOPRO	 			;SET PREFIX.
			DC.B	SPFIX
			DC.W	PRPARM
			BNE	NOGOOD	 			;BRANCH IF PROBLEMS.

ISHEL0		JSR	GOPRO	 			;FIND OUT IF 'STARTUP' EXISTS.
			DC.B	GTINFO
			DC.W	STARTUP
			BNE	NOSTARTUP	 		;BRANCH IF NO STARTUPS..
			LDX	NAMEHEL	 			;MOVE '-STARTUP' TO INPUT BUFFER.
			STX	RTNX

MVHEL		LDA	NAMEHEL,X
			STA	INBUF,X
			DEX
			BNE	MVHEL
			LDA	#$AD	 			;STORE '-' IN FRONT OF NAME
			STA	INBUF
NOGOOD		EQU	*
NOSTARTUP	EQU	*
			LDA	TEMPOR	 			;Get # slashes in prefix
			CMP	#3	 				;3 or more indicate prefix is a subdir
			BCS	DONTNUL
			JSR	GOPRO	 			;SET PREFIX TO NUL.
			DC.B	SPFIX
			DC.W	NULPFX
DONTNUL		LDA	RTNX	 			;WAS THERE A 'STARTUP'?
			BNE	GOCI
			JSR	STRTSTR
			LDX	#COPRIT-VERID-1	 	;Length of Version Identification
PRVERID		LDA	VERID,X
			STA	$400,X
			DEX
			BPL	PRVERID
			LDX	#ZLDREND-COPRIT-1 	;Length of Copyright Notice
PRCOPRT		LDA	COPRIT,X
			STA	$480,X
			DEX
			BPL	PRCOPRT
			LDA	#$8D
			JSR	$FDF0
			JSR	$FDF0
			JSR	$FDF0
GOCI		EQU	*
			LDX	#2
GOCI1		LDA	THREEDOG,X
			STA	$3D0,X	 			;3DOG VECTOR
			STA	$3D3,X	 			;COLD RESTART OF BASIC
			STA	$3F8,X	 			;CTRL-Y VECTOR
			LDA	AMPHOOK,X
			STA	$3F5,X	 			;Set up & hooks for SYNTAX Error
			DEX
			BPL	GOCI1
			LDA	A2MONITOR
			STA	$3F0	 			;BREAK VECTOR
			LDA	A2MONITOR+1
			STA	$3F1
			LDA	CTRLRST
			STA	$3F2	 			;CTRL-RESET VECTOR
			LDA	CTRLRST+1
			STA	$3F3
			EOR	#$A5	 			;Exclusive OR $A5 with high byte of
			STA	$3F4	 			;  RESET Vector for Power-up byte.
			LDA	#$A5
			STA	ZTRACE
			LDA	VERNUM	 			;Get current Revision number
			STA	IVERSION	 		; and put it in MLI global page.
THREEDOG	JMP	CIENT	 			;DO IT!
A2MONITOR	DC.W 	$FA59
CTRLRST		DC.W	$BE00	 		;CI ENTRY
AMPHOOK		JMP	$BE03	 			;DOSCMD entry to CI

STRTSTR		LDA	IN0
			STA	KSWL
			LDA	IN0+1
			STA	KSWH
			LDX	NAMEHEL
			INX
			LDA	#$8D
			RTS

SPOOK		DC.W	RESTART
			DC.W	STRTSTR

STARTUP		DC.B	$A
			DC.W	NAMEHEL
			DS.B	$10				;Placeholder for Startup program info

NULPFX		DC.B	$1
			DC.W	NOPFX

NOPFX		DC.B	$1
			DC.B	'/'

RTNX		DC.B	$00

OLPARM		DC.B	$02
BUNIT		DC.B	$70
			DC.W	PBUF+1

PRPARM		DC.B	$01
			DC.W	PBUF

PBUF		EQU	INBUF+$80
TEMPOR		DC.B	0

			MSB	ON
ERRMES		DC.B	'***  UNABLE TO EXECUTE BASIC SYSTEM  ***'
VERID		DC.B	'            PRODOS BASIC 2.0 ';
COPRIT		DC.B	'        COPYRIGHT APPLE  1983-2026'	 ;34 CHARACTERS
ZLDRLEFT	EQU		$03FF-*	 			;# bytes free
ZLDREND		EQU		*
			DCB.B	$400-(zldrend-loader),0		;this fills in to the end of the segment ($2400)

			END
