
;****** Main ************************************************
;
;	$DOC	Main
;
;  STARDATE
;	07-Oct-94 16:47:07
;
;  NAME
;	Main -- how to start a program in a decent way
;
;  ASSEMBLER
;	Devpac is the best, I think it also works with a compatible
;	assembler with SLINK, BLINK or other linker.
;
;  SYNOPSIS
;	Tons of software (included 99% of Cd32 titles) starts only	
;	from CLI , if you try to start them from Workbench the code
;	immediately crash the system.
;	This main.asm its a good way to start, it opens all the
;	system libraries, get system messages if started from Workbench,
;	and closes at end in a clear way. It permits to kill the
;	operating system and restart again when you finish.
;
;  NOTE
;	This code it isn't all my work, I cut from everywhere and
;	pasted together a lot of examples left around from nice guys
;	as the same way I left this software to you. If you continue
;	the chain all Amiga programmers will continue to be happy
;	to not loose programming time to do boring system routines.
;
;  COPYRIGHT
;	This piece of code is written by
;
;		Gerardo Iula
;		via l.da Vinci 23/U
;		20062 Cassano d'Adda
;		(MI) Italy
;
;	You can use in your code freely, this source is free-ware
;	If you like to mention my name in the credits I will very happy
;
;	$END
;********************************************************************


;----- INCLUDE all of them you don't use ,but not loose assembly time

	incdir	"INCLUDE:"
	IFND	DOS_DOS_I
	include	"dos/dosextens.i"
	ENDC

	IFND	EXEC_MACROS_I
	include	"exec/macros.i"
	ENDC
	
	IFND	EXEC_MEMORY_I
	include	"exec/memory.i"			;alloc memory
	ENDC

	IFND	EXEC_STRINGS_I	
	include	"exec/strings.i"
	ENDC

	IFND	GRAPHICS_GFX_I
	include	"graphics/gfx.i"		;Bitmap structure
	ENDC

        IFND    GRAPHICS_VIEW_I
	include	"graphics/view.i"
	ENDC
	
	IFND	GRAPHICS_RASTPORT_I
	include	"graphics/rastport.i"
	ENDC


;----- INCLUDE from the same dir some useful macros

 	include	"main.i"

;------ OS external calls (the linker put all pices together)	

	XREF	OpenLibrary		;exec
	XREF	CloseLibrary		;exec
	XREF	Input			;dos
	XREF	Output			;dos
	XREF	FindTask		;exec
	XREF	WaitPort		;exec
	XREF	GetMsg			;exec
	XREF	ReplyMsg		;exec
	XREF	CurrentDir		;dos

;------ Export variables (if you link with C programs the _underscore
;			  variables are SET from C part and you have to set
;			  as XREF instead to XDEF (XDEF= export symbol)
;						  (XREF= external symbol)

	XDEF	_IntuitionBase
	XDEF	_GfxBase
	XDEF	_ExecBase
	XDEF	_DiskFontBase
	XDEF	_DosBase
	XDEF	_IconBase
*	XDEF	_LayersBase		;if you use remove comments
*	XDEF	_IeeeBase
*	XDEF	_PPBase
*	XDEF	_GadToolsBase
*	XDEF	_LowLevelBase
*	XDEF	_ReqToolsBase	
	
	XDEF	_stdin	
	XDEF	_stdout	
	XDEF	_stderr	
	XDEF	_task	
	XDEF	DosName
	XDEF	version	

	SECTION	main,CODE

main
				;a0= point to cli arguments
				;if you need after save somewhere!
				
*******************************************************
*	Open Libraries
*******************************************************

	move.l	_ExecBase,a6		;its a fixed value =4
	
	lea	DosName,a1
	moveq	#0,d0			;any version
	JSRLIB OpenLibrary
	moveq	#1,d2			;set error message 
	cmp.l	#0,d0
	beq	error
	move.l  d0,_DosBase

	move.l	_ExecBase,a6
	lea	GraphicsName,a1		;Gfxname nach a1
	moveq	#0,d0			;any version
	JSRLIB	OpenLibrary		;Gfxlibrary oeffnen
	moveq	#2,d2			;set error message 
	cmp.l	#0,d0
	beq	error
	move.l	d0,_GfxBase	;graphic librray 

	move.l	_ExecBase,a6
	lea	IntuitionName,a1
	moveq	#0,d0			;any version
	JSRLIB	OpenLibrary
	moveq	#3,d2			;set error message 
	cmp.l	#0,d0
	beq	error
	move.l	d0,_IntuitionBase	;save address

*	move.l	_ExecBase,a6		;remember to close it at end of code!!
*	lea	DiskFontName,a1		;the close code starts at line 261
*	moveq	#0,d0			;any version
*	JSRLIB	OpenLibrary
*	moveq	#4,d2			;set error message 
*	cmp.l	#0,d0
*	beq	error
*	move.l	d0,_DiskFontBase

;--------------------------------------- get the stdin,etc.

        movea.l _DosBase,a6		;------ get standard input handle:
        JSRLIB Input
        move.l  d0,_stdin

        JSRLIB	Output			;------ get standard output handle:
        move.l  d0,_stdout
        move.l  d0,_stderr

	move.l	_ExecBase,a6
        suba.l  a1,a1       	        ; clear a1
        JSRLIB	FindTask
        move.l  d0,_task           	; 

;------------------------------------------------------------------------

	move.l	_task,a0
	tst.l	pr_CLI(a0)		;were we run by Workbench?
	bne	getcli			;no-jump

;------ get arguments from workbench
	
	move.l	_ExecBase,a6
	lea	pr_MsgPort(a0),a0	;wait for the startup message
	PUSH	a0
	JSRLIB	WaitPort
	POP	a0			;get it
	JSRLIB	GetMsg
	move.l	d0,WbMsg		;save the message

	move.l	_ExecBase,a6
	lea	IconName,a1
	moveq	#0,d0			;any version
	JSRLIB	OpenLibrary
	lea	IconName,a0		;name of error
	moveq	#13,d2			;generic library
	cmp.l	#0,d0
	beq	error
	move.l	d0,_IconBase

;    OldCD =  CurrentDir(WBenchMsg->sm_ArgList->wa_Lock)

	move.l	_DosBase,a6
	move.l	WbMsg,a3	
	move.l	sm_ArgList(a3),a3
	move.l	wa_Lock(a3),D1
	JSRLIB	CurrentDir		(d1=lock)
	move.l	d0,WbLock

;    DiskObj = GetDiskObject(WBenchMsg->sm_ArgList->wa_Name)
	move.l	_IconBase,a6
	move.l	wa_Name(a3),a0
	JSRLIB	GetDiskObject		; (a0=name)
	move.l	d0,DiskObj
	cmp.l	#0,d0
	beq	errorwb			;error
;    ToolTypes = Dobj->do_ToolTypes
	move.l	d0,a0
	move.l	_BaseLib,a5
	move.l	do_ToolTypes(a0),Lib_Tooltype(a5)	;store for user

;    get number of arguments
	move.l	WbMsg,a3	
	move.l	sm_NumArgs(a3),Lib_Toolarg(a5)		; the number of elements in ArgList
	move.l	sm_ArgList(a3),Lib_Toolarglist(a5)	;array of ptr arguments

	bra	endgetarg
	
;------ get arguments from cli
getcli
				;recall the stored pointer at start of code
	
endgetarg
******************************************************************
*	main loop control
******************************************************************

;	move.l	_ExecBase,a6
;	JSRLIB	Forbid		;disable the task no interrupts

ciclo1
;	jsr	MyCode		;that opens screens/windows/and 
;				whatever you want to do

*******************************************************************
*	Restore system and exit
*******************************************************************
endmain

;	move.l	_ExecBase,a6
;	JSRLIB	Permit

;------ return the startup message

	move.l	_task,a0
	tst.l	pr_CLI(a0)		;were we run by Workbench?
	bne	cliskip			;no-jump

	jsr	TooltypePrgFree		;free tooltype if loaded and close libs
					;the code starts at line 373
cliskip

*	move.l	_ExecBase,a6
*	move.l	_DiskFontBase,a1	
*	JSRLIB	CloseLibrary
	
*	move.l	_ExecBase,a6
*	move.l	_GadToolsBase,a1	
*	JSRLIB	CloseLibrary

	move.l	_ExecBase,a6
	move.l	_IconBase,a1	
	cmpa.l	#0,a1
	beq	skipicon
	JSRLIB	CloseLibrary

skipicon

	move.l	_ExecBase,a6
	move.l	_IntuitionBase,a1	;close Intuition
	JSRLIB	CloseLibrary

*	move.l	_ExecBase,a6
*	move.l	_ReqToolsBase,a1
*	JSRLIB	CloseLibrary

*	move.l	_ExecBase,a6
*	move.l	_PPBase,a1
*	JSRLIB	CloseLibrary

*	move.l	_ExecBase,a6
*	move.l	_LowLevelBase,a1
*	cmp.l	#0,a1
*	beq	skiplowclose
*	JSRLIB	CloseLibrary
*skiplowclose

***************************************************************************
*	name of libraries (also some are in the .i includes)
***************************************************************************
	even
DosName
	dc.b "dos.library",0
	even
IntuitionName
	dc.b "intuition.library",0
	even
IconName
	dc.b "icon.library",0
	even
GraphicsName
	dc.b "graphics.library",0
	even
DiskFontName
	dc.b  "diskfont.library",0
	even
ConsoleDevice
	dc.b  "console.device",0
	even
LayersName
	dc.b  "layers.library",0
	even
MathFpName	
	dc.b  "mathffp.library",0
	even
GadToolsName
	dc.b  "gadtools.library",0
	even
LowLevelName
	dc.b  "lowlevel.library",0
	even

	
********************************************************************
*	Name of PD/others libraries (there are examples)
********************************************************************

*	IFD	LIBRARIES_REQTOOLS_I
*	even
*ReqToolsName	REQTOOLSNAME
*	ENDC
*
*	IFD	LIBRARIES_PPBASE_I
*	even
*PpName	PPNAME
*	ENDC
*	
	even

;----- store the pointer of principal libraries

_IntuitionBase	dc.l	0
_GfxBase	dc.l 	0
_ExecBase	EQU	4		;the ONLY fixed address on Amiga!!
_DiskFontBase	dc.l	0
_DosBase	dc.l 	0
_IconBase	dc.l	0
_LayersBase	dc.l	0
_IeeeBase	dc.l	0
_PPBase		dc.l	0
_ReqToolsBase	dc.l	0
_GadToolsBase	dc.l	0
_LowLevelBase	dc.l	0

_stdin		dc.l	0
_stdout		dc.l	0
_stderr		dc.l	0
_task		dc.l	0
_BaseLib	dc.l	0		;structure library

WbMsg		dc.l	0		;startup-message workbench
WbLock		dc.l	0		;lock for icon
DiskObj		dc.l	0

;****** TooltypePrgFree ************************************************
;
;	$DOC	TooltypePrgFree
;  NAME
;	TooltypePrgFree -- free program.info icon
;
;  SYNOPSIS
;	TooltypePrgFree()
;		
;
;  FUNCTION
;	Free program.info icon and parameters
;
;  EXAMPLE
;	jsr	 TooltypePrgFree
;
;  SEE ALSO
;	TooltypeGet()
;	$END
;********************************************************************


TooltypePrgFree
	
;  FreeDiskObject(DiskObj)
	move.l	DiskObj,a0
	move.l	_IconBase,a6
	JSRLIB	FreeDiskObject

	move.l	_DosBase,a6
	move.l	WbLock,d1
	JSRLIB	CurrentDir		(d1=lock)

;    Unlock wb directory
	move.l	_DosBase,a6
	move.l	WbLock,d1
	JSRLIB	UnLock

errorwb
	move.l	_ExecBase,a6
	move.l	_IconBase,a1		;close Iconbase
	JSRLIB	CloseLibrary

	move.l	WbMsg,a1		;return and free the message
	move.l	_ExecBase,a6
	JSRLIB	ReplyMsg

	move.l	#0,WbMsg		;clr
	move.l	#0,_IconBase		;clr Iconbase
	rts
	

	
