
#    r0		volatile, may be used by function linkage
#    r1		stack pointer
#    r2		table of contents register
#    r3	.. r4	volatile, pass 1st - 2nd int args, return 1st - 2nd ints
#    r5  .. r10	volatile, pass 3rd - 8th int args
#    r11		volatile, pass static chain if language needs it
#    r12		volatile, used by dyn linker, exception handling
#    r13 .. r31	saved



#    f0		volatile
#    f1  .. f4	volatile, pass 1st - 4th float args, return 1st - 4th floats
#    f5  .. f13	volatile, pass 5th - 13th float args
#    f14 .. f31	saved

#	Use r3	as CurCnt
#	Use r4	as MaxCnt (Constant value)
#	Use r5	as LPixelBuf
#	Use r6	as location in ColorTable32
#	Use r7	as base address of ColorTable32
#	Use r8	as BPP (that is, bytes per pixel)

#	Use f3	as 56(r1)
#	Use f2	as Zr2 (was in 48(r1)
#	Note: Problems were had with f4.  Will have to investigate.
#	Use f5  as xRel (Fixed value).
#	Use f6	as value that was stored in 120(r10) (yRel?)
#	Use f7	as value that was stored in 40(r1).  It's Zi
#	Use f8	as Zr.
#	Use f12 as constant value of Two.
#	Use f13 as constant value of Limit

	.global	@__UseLibP2
	.text
	.sdreg	r2
	.global	_UseLibP2
	.align	4
_UseLibP2:
	mflr	r11
	stw	r11,8(r1)	#Save return address
	stwu	r1,-96(r1)      #Save stack pointer
	stfd	f1,32(r1)	#Save yRel
	lfd	f6,32(r1)	#Get yRel (Doesn't change and stays in f6)
	lwz	r11,_xtemp(r2)  #Get x coordinate within image.
# Convert xtemp to double fp.
	lis	r12,17200
	stw	r12,76(r1)
	xoris	r12,r11,32768
	stw	r12,80(r1)
	lfd	f0,76(r1)
	lfd	f10,l13(r2)
	fsub	f5,f0,f10	#Find (double)xtemp

	lfd	f10,_XCenter(r2)#f10 = XCenter
	fsub	f5,f5,f10	#xRel = xRel - XCenter
	lfd	f10,_Factor(r2)	#f10 = Factor
	fdiv	f5,f5,f10	#xRel = xRel/Factor

	stfd	f5,32(r1)	#Zr = xRel initial value
	lfd	f8,32(r1)       #Zr in f8, while f5 is fixed value of xRel
	stfd	f6,32(r1)	#Store yRel as initial Zi
	lfd	f7,32(r1)	#Zi into f7
	lwz	r4,_MaxCnt(r2)	#MaxCnt in r4.
	li	r3,1		#CurCnt = 1
	lwz	r5,_LPixelBuf(r2)	#Get Location of LPixelBuf
	lwz	r7,_ColorTable32(r2)
	lwz	r8,_BPP(r2)
	lfd	f12,_Two(r2)	#Get Two into f12 as constant
	lfd	f13,_Limit(r2)	#Get Limit into f13 as constant
	b	l3		#Check to see if to MaxCnt
l2:
	fmul	f2,f8,f8	#Zr2 in f2
	fmul	f4,f7,f7	#Zi2 = Zi*Zi
	fadd	f9,f2,f4
	fcmpu	cr0,f9,f13	#Compare with Limit in f13.
	ble	cr0,l6

#The following section is not very time-critical as it is only reached once.
#for each pixel
	cmpwi	cr0,r8,2	#See if BPP < 2
	blt	cr0,l8
l7:
	slwi	r10,r3,2	#4 times CurCnt for offset in ColorTable32

	add	r6,r7,r10	#Find location of pixel color in ColorTable32

	lwz	r11,0(r6)	#Get ARGB from ColorTable
	stw	r11,0(r5)	#Store color in LPixelBuf
	addi	r5,r5,4		#Point to next location in LPixelBuf
	b	l1
l8:	andi.	r0,r3,65535	#Prep for storage as UWORD
	sth	r0,_Color(r2)
	bl	_PlotIt
l9:	b	l1
l6:
	fmul	f11,f12,f7	#Find 2*Zi  (Two is in f12)
	fmul	f7,f11,f8	#Find 2*Zi*Zr (temporary Zi)
	fsub	f10,f2,f4       #Zr2 = Zr2 - Zi2
	fadd	f8,f5,f10	#New Zr = xRel + Zr2
	fadd	f7,f6,f7	#Zi = Zi + yRel
	addi	r3,r3,1
l3:
	cmpw	cr0,r3,r4	#See if CurCnt < MaxCnt
	blt	cr0,l2		#Repeat until MaxCnt reached

	cmpwi	cr0,r8,2	#See if BPP < 2
	blt	cr0,l11
l10:

	lis	r12,4
	addi	r12,r12,-4
	add	r6,r7,r12	#Find location of pixel color
	lwz	r11,0(r6)       #Get color

	stw	r11,0(r5)	#Store color in LPixelBuf
	addi	r5,r5,4         #Point to next pixel in buffer
	b	l12
l11:
	lis	r11,1
	addi	r11,r11,-1
	sth	r11,_Color(r2)
	bl	_PlotIt
l12:
l1:
	stw	r5,_LPixelBuf(r2)
	addi	r1,r1,96        #Balance Stack pointer
	lwz	r11,8(r1)	#Get return address
	mtlr	r11
	blr
	.type	_UseLibP2,@function
	.size	_UseLibP2,$-_UseLibP2
	.global	_xtemp
	.global	_MaxCnt
	.global	_XCenter
	.global	_Factor
	.global	_Limit
	.global	_Two
	.global	_Color
	.global	_LPixelBuf
	.global	_ColorTable32
	.global	_BPP
	.global	_PlotIt
	.global	@__PlotIt
	.tocd
	.align	3
l13:
	.long	0x43300000,0x80000000
