
	globl rc_intersect

g_x 		= 0
g_y 		= 2
g_w 		= 4
g_h 		= 6

xmin1		= -16
ymin1		= -14
xmin2		= -12
ymin2		= -10
xmax1		= -8
ymax1		= -6
xmax2		= -4
ymax2		= -2

			TEXT

MODULE rc_intersect
			link	a6,#-16
			move.w	g_y(a0),d0
			cmp.w	g_y(a1),d0
			bge.b	skip

			move.l	g_x(a0),xmin1(a6)

			move.w	g_x(a0),d0
			add.w	g_w(a0),d0
			move.w	d0,xmax1(a6)

			move.w	g_y(a0),d0
			add.w	g_h(a0),d0
			move.w	d0,ymax1(a6)
			
			move.l	g_x(a1),xmin2(a6)

			move.w	g_x(a1),d0
			add.w	g_w(a1),d0
			move.w	d0,xmax2(a6)

			move.w	g_y(a1),d0
			add.w	g_h(a1),d0
			move.w	d0,ymax2(a6)
			bra.b	skp

skip:		move.l	g_x(a0),xmin2(a6)

			move.w	g_x(a0),d0
			add.w	g_w(a0),d0
			move.w	d0,xmax2(a6)

			move.w	g_y(a0),d0
			add.w	g_h(a0),d0
			move.w	d0,ymax2(a6)

			move.l	g_x(a1),xmin1(a6)

			move.w	g_x(a1),d0
			add.w	g_w(a1),d0
			move.w	d0,xmax1(a6)

			move.w	g_y(a1),d0
			add.w	g_h(a1),d0
			move.w	d0,ymax1(a6)

skp:		move.w	ymin2(a6),d0
			cmp.w	ymax1(a6),d0
			bge.b	ret

			move.w	xmin1(a6),d0
			cmp.w	xmax2(a6),d0
			bge.b	ret

			move.w	xmin2(a6),d0
			cmp.w	xmax1(a6),d0
			bge.b	ret

			move.w	xmin1(a6),d0
			move.w	xmin2(a6),d1
			cmp.w	d1,d0
			bge.b	(*)+4

			move.w	d1,d0
			move.w	d0,g_x(a1)
			
			move.w	ymin1(a6),d0
			move.w	ymin2(a6),d1
			cmp.w	d1,d0
			bge.b	(*)+4
			
			move.w	d1,d0
			move.w	d0,g_y(a1)
			
			move.w	xmax1(a6),d0
			move.w	xmax2(a6),d1
			cmp.w	d1,d0
			blt.b	(*)+4
			
			move.w	d1,d0
			move.w	d0,g_w(a1)
			
			move.w	ymax1(a6),d0
			move.w	ymax2(a6),d1
			cmp.w	d1,d0
			blt.b	(*)+4

			move.w	d1,d0
			move.w	d0,g_h(a1)

			move.w	g_x(a1),d0
			sub.w	d0,g_w(a1)
			move.w	g_y(a1),d0
			sub.w	d0,g_h(a1)
			
			moveq.l	#1,d0
			unlk	a6
			rts

ret:		moveq.l	#0,d0
			unlk	a6
			rts
ENDMOD
