DEBUG	EQU 	0		1 = Debugging, 0 = Final Revision
	IFEQ	DEBUG
		OPT	O+,W-,X-
	ELSEIF
		OPT	O+,W-,X+
	ENDC
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
WNX=0				0 if can run as WNX, otherwise not!
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* MACROS -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
object		macro

		move.w	\2,\4
		mulu	#ob_sizeof,\4
		lea	0(\1,\4),\3
		
		endm
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
		RSRESET
ob_next		rs.w	1
ob_head		rs.w	1
ob_tail		rs.w	1
ob_type		rs.w	1
ob_flags	rs.w	1
ob_state	rs.w	1
ob_spec		rs.l	1
ob_x		rs.w	1
ob_y		rs.w	1
ob_w		rs.w	1
ob_h		rs.w	1
ob_sizeof	rs.w	0
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
		RSRESET
dta_reserved	rs.b	21
dta_attrib	rs.b	1
dta_time	rs.b	2
dta_date	rs.b	2
dta_size	rs.b	4
dta_name	rs.b	13
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
		RSRESET
MAGIC		rs.w	1

TSIZE		rs.l	1
DSIZE		rs.l	1
BSIZE		rs.l	1
SSIZE		rs.l	1

SELFRESERVED	rs.w	5	
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
		include	d:\wnx_dev\gemmacro.s
		include	d:\wnx_dev\newmacro.s
		include	d:\wnx_dev\xesmacro.s
		*include	d:\wnx_dev\linea.s
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* START *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
START
		lea	START,a5		get the address of this code
		tst.l	-220(a5)		is p_parent = 0?
		cmp.l	#"XES",-216(a5)		is p_reserved = "XES"
		beq.s	start_wnx		no, so do .PRG startup code

		move.l	a0,d0			
		tst.l	d0			is a0 <> 0?
		bne	start_acc		yes, so .ACC!
		bra	start_prg		no, so .PRG...		

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
start_wnx
	IFEQ	WNX = 0
* first, make a call to initialize.  Pass the address of your RSC.
		move.w	#-1,app			app = -1 for WNXes
		xes_init	#parmblk

		move.w	WNX_grafhandle,current_handle

		xes_rsrcinit	#-1,#2,#resource1
		move.l	WNX_out+6,a0		got the address!
		move.l	a0,treeptr
		move.l	(a0),rsrcptr

		xes_genv			get screen settings...
		move.w	WNX_out+12,scalex
		move.w	WNX_out+14,scaley
		
		xes_pwindow			get window settings...
		movem.w	WNX_out+8,d4-d7
		movem.w	d4-d7,minx

		move.w	#%000000,WNX_flags
		*move.w	#0001,WNX_tcount
		*move.l	#maintimer,WNX_troutine
		*move.l	#mainbutton,WNX_broutine
		*move.l	#mainbutton,tempbutton
		*move.l	#mainmessage,WNX_mroutine
		*move.l	#mainredraw,WNX_rroutine
		move.l	#termroutine,WNX_termroutine
		xes_submit
	ELSEIF
		form_alert	#1,#cannotrunaswnx

		move.l	#START-358,-(a7)	ptr to default parmblk
		move.w	#39,-(a7)
		xes_call
		
	ENDC

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
start_prg	

		move.w	#1,app			PRGs,etc... have app = 1
		move.l	4(a7),a3		base page
		move.l	#mystack,a7
		move.l	$c(a3),d0		text len
		add.l	$14(a3),d0		data len
		add.l	$1c(a3),d0		BSS len
		add.l	#$100,d0		basepage

		move.l	d0,-(sp)
		move.l	a3,-(sp)
		clr.w	-(sp)
		move.w	#$4a,-(sp)
		trap	#1			shrink memory
		lea	12(sp),sp

		bra.s	go_prg_or_acc
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
start_acc	
		move.w	#0,app			ACCs have app = 0
		move.l	#mystack,a7		must have a stack!

go_prg_or_acc	dc.w	$a000			line-A init
		move.l	a0,a_init		

		appl_init
		move.w	d0,ap_id		store the application id

		graf_handle
		move.w	d0,current_handle	Desktop's VDI handle

		bsr	get_scalexy

		move.w	#2,rsc_scale_flag
		move.l	#resource1,d1
		bsr	rsrc_include
		move.l	result0,rsrcptr

		tst.w	app			program or accessory?
		bne.s	go_prg			program!
		
		menu_register	ap_id,#mymenu
		jmp	auto_acc
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
go_prg
* start by opening a virtual workstation
		lea	intin,a0
		moveq	#10-1,d0		-1 for DBF
.fill		move.w	#1,(a0)+		most params are 1
		dbf	d0,.fill
		move.w	#2,(a0)+		use RC system

		v_opnvwk			open it
		
		wind_get	#0,#4		find working area...
		movem.w	int_out+2,d0-d3
		movem.w	d0-d3,minx

* gets the resolution of the system!
		_getrez
		move.w	d0,origmode

		_physbase
		move.l	d0,orig1
		
		_logbase
		move.l	d0,orig2

* set the mouse to an arrow
		graf_mouse	#0
		jmp	PROGRAM
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- MAIN *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
auto_acc
* Any autoboot DA code goes here...
* We conclude by getting into the accessory loop in wait4event...
* load the RSC
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
wait4event
* We wait for GEM to say this DA is open...
		evnt_mesag	#messagebuf	
		move.l		#messagebuf,a0
		move.w		(a0),d0
		cmp.w		#40,d0
		beq		BEGIN

* check for other GEM messages here...
		bra.s		wait4event
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
get_scalexy
		vqt_fontinfo
		movem.w	int_out+2,d0-d1

* fixes MultiDesk incompatibility by sensing when 6x6 font is in!
._h		divu.w	#8,d0
		bne	._w

		moveq	#1,d0
		
._w		divu.w	#8,d1
		bne	._done
		
		moveq	#1,d1
		
._done		movem.w	d0-d1,scalex

		rts
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
rsc_scale_flag	dc.l	0
result0		dc.l	0
result1		dc.l	0
result2		dc.l	0
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
rsrc_include
* fixes up a resource tree using mystic arts.  Actually fixes multiple
* resource trees within one .RSC file!
* in:	d1 = ptr to binary .RSC file in memory.
*
* out:	result0 = ptr to first tree in resource.
*	result1	= ptr to tree list.
*	result2 = number of trees in resource.
*
* assume to smash all registers.
*

		tst.l	d1
		beq.w	rsrc_null
		
		move.l	d1,a2			a2 = ptr to binary RSC
		move.l	d1,d7			d7 = a2
		
		move.w	20(a2),d2		d2 = number of objects in RSC
		subq.w	#1,d2			d2 = d2 - 1

		moveq	#0,d0		
		move.w	2(a2),d0		d0 = offset to first tree
		add.l	d0,a2			a2 = ptr to first tree...

		move.l	a2,result0		result0 = ptr to first tree |X|
		
fixloop
		object	a2,d2,a0,d0
		move.w	ob_type(a0),d0		d0 = object type
		move.l	ob_spec(a0),d1		d1 = object spec field.
		
		cmp.w	#20,d0
		beq.s	endfixloop
		cmp.w	#25,d0
		beq.s	endfixloop
		cmp.w	#27,d0
		beq.s	endfixloop
		
		add.l	d7,d1
		move.l	d1,ob_spec(a0)		fixes up ob_spec entry
		
		move.l	d1,a1
		add.l	d7,(a1)+		fixes up a field
		
		cmp.w	#23,d0
		beq.s	endfixloop
		cmp.w	#24,d0
		beq.s	endfixloop
		
		add.l	d7,(a1)+		fixes up two fields...
		add.l	d7,(a1)+

endfixloop
		lea	16(a0),a0		point at X field
		
		move.w	scalex,d0		scale of x to 8x8 font
		move.w	scaley,d1		scale of y to 8x8 font

		move.w	#1,d6			fix (1+1)*2 entries (X,Y,W,H)
		
fix_XYWH		
.XW		moveq	#0,d4
		moveq	#0,d5
		move.b	(a0),d4			d4 = 0-7 pixel coord
		
		tst.w	rsc_scale_flag		scale pixel coords?
		beq.s	.skip1			no.

		mulu	d0,d4			d4 = d4 * d0 if flag set.
		
.skip1		move.b	1(a0),d5		d5 = char coord
		mulu	#8,d5
		
		cmp.w	#2,rsc_scale_flag	scale char coords?
		bne.s	.skip1b			no.
		
		mulu	d0,d5			d5 = d5 * d0 if flag set.
		
.skip1b		add.w	d4,d5
		move.w	d5,(a0)+


.YH		moveq	#0,d4
		moveq	#0,d5
		move.b	(a0),d4			0-7 pixel coord
		
		tst.w	rsc_scale_flag		if not set, no conversion
		beq.s	.skip2			

		mulu	d1,d4			d4 = d4 * d0 if flag set
		
.skip2		move.b	1(a0),d5		d5 = char coord
		mulu	#8,d5

		cmp.w	#2,rsc_scale_flag	scale char coords?
		bne.s	.skip2b			no.
	
		mulu	d1,d5

.skip2b		add.w	d4,d5
		
		move.w	d5,(a0)+
		
		dbra.w	d6,fix_XYWH
		
		dbra.w	d2,fixloop		keep fixin' 'em!
		
fixtrees	move.l	d7,a0			a0 = ptr to binary resource
		move.w	22(a0),d6		d6 = number of trees
		move.w	d6,result2		result2.W = number of trees |X|
		
		move.l	d7,a1			a1 = d7 = a0
		moveq	#0,d0
		move.w	18(a0),d0		d0 = offset to tree entries
		add.l	d0,a1			a1 = ptr to tree entries
		move.l	a1,result1		result1 = ptr to tree list |X|
		
		subq.w	#1,d6			d6 = d6 - 1
		
.treeloop	add.l	d7,(a1)+
		dbra.w	d6,.treeloop
		
		move.w	#0,d0			okay...
		rts
		
rsrc_null
		move.w	#-1,d0			not okay!
		rts	


*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
PROGRAM
* special code needed for PRGs starts here.
		
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
BEGIN
* this is where we go when we do our main code, irregardless of type
* (i.e., ACC, PRG, or WNX)
		bra.s	termn8
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
termroutine
* routine to run at end of a WNX...
		rts
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
* STOP HERE.......
termn8
		tst.w	app
		bmi	termroutine		if WNX, goto termroutine
		beq	wait4event		if ACC, go back to wait4event

PRG_TERMN8
		_setscreen	origmode,orig1,orig2,
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
terminate
* show mouse if not being shown

		v_show_c #0

* then closing the virtual workstation

		v_clsvwk			close it

		appl_exit			tell GEM I've finished
					
* now quit to the desktop
		p_term	#0

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
		SECTION	DATA
* global constants
		EVEN
resource1 	incbin	d:\wnx_dev\wercs\color_x.rsc
		EVEN

crlf		dc.b	13,10,0			values for a cr/lf 
mymenu		dc.b	"  Flip's Test ACC  ",0,0
cannotrunaswnx	dc.b	"[3][| I Cannot run as .WNX| I will now terminate properly.][My Humble Apologies...]",0
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
		SECTION	BSS

* Used in the application's "startup" sequence....vroom!
app		ds.w	1

ap_id		ds.w 	1

		ds.b	508		stack space
mystack		ds.l	1		(stacks go backwards)
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
* Used for m_alloc and m_free calls.  I define as many of these as I think
* I will need.....3 is about the maximum for what I plan to do!
m_start		ds.l	16
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
* Now, some variables for file handling.......
* I don't think I'll personally use more than 6 files at once...
f_handx		ds.w	1		word for file handle
filesize	ds.l	1		file size

path		ds.b	224		path name from fsel_input
filename	ds.b	14		12 bytes terminated with a null word
fullname	ds.b	256	
		EVEN
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-		
* These are custmized variables for various uses...
dummy		ds.l	1		dummy variable
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-		
* variables for use with the screen...
orig1		ds.l	1
orig2		ds.l	1
origmode	ds.w	1

a_init		ds.l	1		ptr to Line-A init

screen1		ds.l	1		address of screen1
screen2		ds.l	1		address of screen2

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	IFEQ	WNX = 0
* This is a parameter block for a WNX...
		include	d:\wnx_dev\parmblk.s
	ENDC
	
rsrcptr		ds.l	1
treeptr		ds.l	1

scalex		ds.w	1		scale of screen font to 8x8
scaley		ds.w	1

fx		ds.w	1
fy		ds.w	1
fw		ds.w	1
fh		ds.w	1

minx		ds.w	1		screen x coords, etc...
miny		ds.w	1
maxx		ds.w	1
maxy		ds.w	1
	
messagebuf	ds.b	16


* if not linking then include the run-times

	include	d:\wnx_dev\aeslib.s
	include	d:\wnx_dev\vdilib.s


