amiref	macro
	xref _LVO\1
	endm

libcall macro	;library, routine
	move.l \1,a6
	jsr _LVO\2(A6)
	endm

execlib macro	;routine
	libcall execbase,\1
	endm

doslib macro	;routine
	libcall dosbase,\1
	endm

gfxlib macro	;routine[,*rastport]
	ifnc '\2',''
	move.l \2,a1
	endc
	libcall gfxbase,\1
	endm

intlib macro	;routine
	libcall intbase,\1
	endm

oplib macro ; library name,pointer to base address
	move.l	execbase,a6
	lea	\1,a1		;library name
	move.l	#0,d0
	execlib	OpenLibrary
	move.l	d0,\2		;save pointer to library base address
	endm

clolib macro ; library base address - must test if open before call
	movea.l	\1,a1
	execlib	CloseLibrary
	endm

evenpc macro			;used to word-align the program counter
	ds.w 0
	endm

zero macro
	moveq #0,\1
	endm

zera macro
	suba.l \1,\1
	endm

pushreg macro	;register
	movem.l \1,-(sp)
	endm

pullreg macro
	movem.l (sp)+,\1
	endm

pause macro		;#secs
	move.l	\1,d1
	zero	d0
	move.w	#TICKS_PER_SECOND,d0
	mulu	d0,d1
	doslib	Delay
	endm

getmem macro		;(#)size in bytes,(#)type,*memblock
	move.l	\1,d0
	move.l	\2,d1
	execlib	AllocMem
	move.l	d0,\3
	endm

byemem macro		;(#)size in bytes,*memblock
	move.l	\1,d0
	ifnc	'\2',''
	move.l	\2,a1
	endc
	execlib	FreeMem
	endm

MakeTask macro		;stacksize,*stack,*task,*name,*codeseg

	move.l	\1,d0		;this rounds the stack size to the
	addq.l	#3,d0		;nearest long word
	move.l	#$FFFFFFFC,d1
	and.l	d1,d0
	move.l	d0,\1

	move.l	#MEMF_CLEAR,d1	;allocate mem for stack (not public)
	execlib	AllocMem
	move.l	d0,\2
	beq	1$

	getmem	#TC_SIZE,#MEMF_CLEAR!MEMF_PUBLIC,\3	;get mem for
	beq	1$					;task control block

	move.l	\3,a0			;task addr in a0
	move.l	\2,a1			;stack addr in a1
	move.l	a1,TC_SPLOWER(a0)	;put stack addr in task struct

	move.l	\1,d0			;stacksize in d0
	add.l	a1,d0			;add stackaddr to it to get to upper
	move.l	d0,TC_SPUPPER(a0)	;put this value in task struct
	move.l	d0,TC_SPREG(a0)		;set the stack pointer to the top

	
	move.b	#NT_TASK,LN_TYPE(a0)	;set type in node struct
	lea	\4,a1
	move.l	a1,LN_NAME(a0)		;load the task name into struct

	pushreg	a2-a3
	move.l	\3,a1
	lea	\5,a2
	zera	a3
	execlib	AddTask			;returns nothing,hope all goes well
	pullreg	a2-a3
1$
	endm
;************************************************************************
ByeTask	macro		;stacksize,*stack,*task

	move.l	\3,d0
	beq.s	1$
	move.l	\3,a1
	execlib	RemTask		;remove the task
	byemem	#TC_SIZE,\3	;get back tcb memory
1$
	move.l	\2,d0		;get back the stack mem
	beq.s	2$
	byemem	\1,\2
2$
	endm
;************************************************************************
CreatePort macro		;sigbit,*port,*name,*task - priority always zero

	zero	d0
	move.b	#-1,d0
	execlib	AllocSignal	;get signal for the port
	move.b	d0,\1
	beq.s	1$

	zero	d0
	getmem	#MP_SIZE,#MEMF_CLEAR!MEMF_PUBLIC,\2	;get mem for
	beq.s	1$					;port structure

	move.l	\2,a0
	lea	\3,a1
	move.l	a1,LN_NAME(a0)		;load port name into port struct
	move.b	#NT_MSGPORT,LN_TYPE(a0)	;load type into the node struct

	move.b	#PA_SIGNAL,MP_FLAGS(a0)	;we want a signal
	move.b	\1,MP_SIGBIT(a0)	;give it the signal bit we just got
	move.l	\4,MP_SIGTASK(a0)	;tell it which task to signal

	move.l	\2,a1
	execlib	AddPort		;hope all goes well
1$
	endm
;************************************************************************
DeletePort macro	;*port,sigbit

	move.l	\1,d0
	beq.s	1$
	move.l	\1,a1
	execlib	RemPort		;remove the port
	byemem	#MP_SIZE,\1	;recover the memory for the port struct
1$
	zero	d0
	move.b	\2,d0
	beq.s	2$
	execlib	FreeSignal	;give back the signal we allocated for
2$				;this port
	endm
;************************************************************************
opencon macro			;*console_spec_str,*fhandle(bcpl)
	lea	\1,a0
	move.l	a0,d1
	pushreg	d2
	move.l	#MODE_NEWFILE,d2
	doslib	Open
	move.l	d0,\2
	pullreg	d2
	tst.l	d0
	beq	getevent
	endm
;****************************************************************************
closecon macro			;*fhandle(BCPL)
	move.l	\1,d1
	doslib	Close
	move.l	#0,\1
	endm
;***************************************************************************
conout macro			;*fh,*bufbegin,*bufend
	move.l	\1,d1
	pushreg	d2-d3
	move.l	#\2,d2
	move.l	#\3-\2,d3
	doslib	Write
	pullreg	d2-d3
	endm
;**************************************************************************
