;=======T=======T========================T==========================================;
;gms.s
;-----
;Compile this file and store it in SC:LIB/ if you have SAS/C.  If you have
;DiceC then store it in dcc:dlib/.
;
;Link it to your C programs with the following:
;
;  1> sc <source.c> link startup=LIB:gms.o data=far

	INCDIR	"INCLUDES:"
	INCLUDE	"games/games_lib.i"

CALL	MACRO
	jsr	_LVO\1(a6)
	ENDM

	xdef	_GMSBase
	xdef	__XCEXIT

	xref	_main
	xref	_PREFSNAME

__XCEXIT =	0

	SECTION	gms,CODE

;===================================================================================;
;                            SPECIAL STARTUP CODE
;===================================================================================;

Start:	MOVEM.L	D0-D7/A0-A6,-(SP)
	cmp.l	#"GMSP",d0
	bne.s	.StartFromDOS
	move.l	a1,_GMSBase
	move.l	a1,a6	;a6 = GMSBase.
	move.l	_PREFSNAME,a0
	CALL	SetUserPrefs
	tst.l	d0
	bne.s	.gexit
	jsr	_main
.gexit	move.l	_GMSBase(pc),a6
	CALL	CloseGMS
	MOVEM.L	(SP)+,D0-D7/A0-A6
	moveq	#$00,d0
	rts

.StartFromDOS
	move.l	($4).w,a6	;a6 = ExecBase
	lea	GMSName(pc),a1	;a1 = Library name.
	moveq	#$00,d0	;d0 = Any version.
	jsr	-552(a6)	;>> = OpenLibrary()
	move.l	d0,_GMSBase	;ma = Save base.
	beq.s	.errgms	;>> = Error, exit.

	move.l	d0,a6	;a6 = GMSBase.
	lea	.exit(pc),a0	;a0 = Pointer to SelfDestruct() cleanup.
	move.l	a7,a1	;a1 = Stack pointer.
	CALL	InitDestruct	;>> = Initialise the call.

	move.l	_PREFSNAME,a0
	CALL	SetUserPrefs
	tst.l	d0
	bne.s	.exit

	jsr	_main

.exit	move.l	_GMSBase(pc),a6
	CALL	CloseGMS
.errgms	MOVEM.L	(SP)+,D0-D7/A0-A6
	moveq	#$00,d0
	rts

_GMSBase dc.l	0

GMSName	 dc.b	"GMS:libs/dpkernal.library",0
	 even

