****	sozoinit.s - sozobon program/accessory startup	****
****	by samuel streeper 900411						****

*
* Base page definitions
*
codelen	equ 12			* Code segment length
datalen equ 20			* Data segment length
bsslen	equ 28			* Bss  segment length
par_bp	equ 36			* Parent basepage

*
* external references
*
.globl __main
.globl _main
.globl __base
.globl __app
.globl __progsize

.bss
.even
__base:					* points to base page of program
	.ds.l	1
__app:					* $ff=program is app, 0=program is accessory
	.ds.w	1
__progsize:
	.ds.l	1
saveret:
	.ds.l	1

	.ds.b	6144		* 6K stack size
_stackbottom:
	.ds.l	1



.text

__main:
		lea		__main, a5		* Compute basepage address
		suba.w	#$100, a5		* subtract size of basepage
		lea		_stackbottom,sp	* set up our stack

		moveq	#0,d5
		tst.l   par_bp(a5)		* Parent basepage pointer clear if ACC
		sne		d5				* D5 set if application
		move	d5,__app
		beq     cont			* is an accessory

	app:
		move.l	codelen(a5),d0
		add.l	datalen(a5),d0
		add.l	bsslen(a5),d0
		addi.l	#$100,d0

		move.l	d0,__progsize

		move.l	d0,-(sp)		* amount of memory to keep
		move.l	a5,-(sp)		* starting at basepage
		clr		-(sp)
		move	#$4a,-(sp)		* Mshrink() - return excess storage
		trap	#1
		lea		12(sp),sp

	cont:
		move.l	a5, __base		* so user progs can find basepage

		jsr		_main			* call application

		clr		-(sp)			* Pterm0()
		trap	#1


	************************************
	* Bindings for gemdos, bios, xbios *
	* These may be moved into dlibs in *
	* a later version                  *
	************************************

         .globl _gemdos 
   _gemdos: 
         move.l    (sp)+,saveret 
         trap      #1 
         move.l    saveret,-(sp) 
         rts 

         .globl _bios 
   _bios: 
         move.l    (sp)+,saveret 
         trap      #13 
         move.l    saveret,-(sp) 
         rts 

         .globl _xbios 
   _xbios: 
         move.l    (sp)+,saveret 
         trap      #14 
         move.l    saveret,-(sp) 
         rts
