;:ts=8
;
; Initial startup routine for Aztec 'C' and ARP using RESIDENT.
; NB: This should allow access to all Aztec Features, math, etc.
;     Could be made smaller if only for CLI, only for Workbench,
;     Could be made smaller if only this or that.
;
; Created 02-18-88 by -+=SDB+=- from arpcrt0.s
; Copyright (c) 1988 by Scott Ballantyne, may be freely
; used by Arp Supporters/users.
; Use at your own risk.
;
; NB: References to the ___fromdisk__ long have to be handled carefully,
;     since they are somewhat shizophrenic, sometimes referring to copied
;     data, sometimes not.  Note that they should fine for the C code without
;     special handling as long as small model is used.
;
; May 9, 1988:	Move OpenLibrary() call into _main() to fix WBench Bug.
;		Add AhOhNoARP() callout for better user interface.
;
; 10-Mar-90:	Put together from resgeta4.s & arprescrt0.s for
;		Aztec 5.0 release.
;
;		Note:	define ARPONLY to create startup code for Arp
;			tool, the default is standard Aztec startup code.
;			The reason for this is: Arp opens dos.library in
;			_main() while the standard Aztec startup code
;			opens it before entering _main().
;

        INCLUDE "exec/types.i"
        INCLUDE "exec/exec_lib.i"
        INCLUDE "exec/execbase.i"
        INCLUDE "exec/memory.i"
        INCLUDE "exec/tasks.i"
        INCLUDE "exec/alerts.i"

        INCLUDE "libraries/arpbase.i"

ARPONLY	EQU	1	; This is the arp startup code

        mc68881
        entry   .begin
        public  .begin
        public  _geta4

*       If you need more or less stack, change the first number below
*       For example, if your program only needs 4000 bytes of stack, the
*       line below would read:
*       RESIDENT 4000,__H2_end-__H1_org,loaded
*

.begin
        RESIDENT 10240,__H2_end-__H1_org,loaded
        moveq.l #0,d7                   ; flag resident code
        move.l  a4,a3                   ; copy base
        add.l   #32766,a4               ; bias
        bra.s   clonedata               ; and copy the data segment only
loaded: moveq.l #-1,d7                  ; flag not resident (start if from disk)
        far data
        move.l  d7,___fromdisk__        ; needs to be set for _geta4
        near data
        bsr     _geta4
isresident:
        lea     __H1_end,a1
        lea     __H2_org,a2
        cmp.l   a1,a2                   ;check if BSS and DATA together
        bne     normal                  ; this would actually be an error.
        move.w  #((__H2_end-__H2_org)/4)-1,d1
        bmi     normal
        move.l  #0,d2
loop
        move.l  d2,(a1)+                ;clear out memory
        dbra    d1,loop
        bra.s   normal
clonedata:
        move.l  #((__H1_end-__H1_org)/4)-1,d1
        bmi.s   normal
        far     data
        lea     __H1_end,a1
        lea     __H2_org,a2
        cmp.l   a1,a2
        beq.s   1$
        move.l  #500,d0                 ; error, must be same hunk, so scram
        rts
1$:     lea.l   __H1_org,a1             ; clone data and bss
        near    data
0$      move.l  (a1)+,(a3)+             ; a3 set to point to base
        dbra    d1,0$
normal: move.l  sp,__savsp              ; save stack pointer
        move.l  4,a6                    ; get Exec's library base pointer
        move.l  a6,_SysBase             ; put where we can get it
        movem.l d0/a0,-(sp)             ; save CLI command parameters

        btst.b  #4,$129(a6)             ;check for 68881 flag in AttnFlags
        beq     1$                      ;skip if not
        lea     2$,a5
        jsr     -30(a6)                 ;do it in supervisor mode
        bra     1$
2$
        clr.l   -(sp)
        frestore (sp)+                  ;reset the ffp stuff
        rte                             ;and return
1$
	IFND ARPONLY
	lea	dos_name,a1		;get name of dos library
	moveq	#0,d0			;any version
	jsr	-$228(a6)		;open the library
	move.l	d0,_DOSBase		;set it up
	bne	3$			;skip if okay

  	move.l  #$38007,d7		;AG_OpenLib | AO_DOSLib
	jsr     -108(a6)		;Alert
	bra	4$
3$
	jsr	__main			;call the startup stuff
	tst.l	_DOSBase		;lib still open?
	beq.s	4$			;nope
	move.l	_DOSBase,a1
	move.l	4,a6
	jsr	-$19e(a6)		;close it then
4$
	add.w 	#8,sp			;pop args
	rts				;and return

dos_name:
	dc.b	'dos.library',0
	ENDC

	IFD ARPONLY
        jsr     __main                  ;call the startup stuff
        add.w   #8,sp                   ;pop args
        rts                             ;and return
	ENDC

_geta4:
	far	data
	movem.l	d0-d1/a0-a1/a6,-(sp)	; max safety here...
	tst.l	___fromdisk__
	bne.s	1$
	move.l	4,a6
	move.l	ThisTask(a6),a0
	lea.l	TC_MEMENTRY(a0),a0
	lea.l	PMEM(pc),a1
	SYSCALL	FindName
	move.l	d0,a0
	cmp.w	#2,pm_Num(a0)		; check for data
	bne.s	1$			; resident, but no data allocated.
	move.l	pm_Data(a0),a4		; get data pointer
	add.l	#32766,a4		; bias 
	bra.s	2$
1$:	lea.l	__H1_org+32766,a4
2$:	movem.l	(sp)+,d0-d1/a0-a1/a6
	rts

PMEM	PMEM_NAME

        public  __main,__H0_org

        dseg
        
        public  _SysBase,_DOSBase,__savsp,___fromdisk__,___sloppy__
        public  __H1_org,__H1_end,__H2_org,__H2_end

	near	data

	even

___fromdisk__:
	dc.l	0	; default is resident
___sloppy__:
	IFND ARPONLY
	dc.l	0
	ENDC

	IFD ARPONLY
	dc.l	1
	ENDC
