
#    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 r22 as Color
#	Use r23	as CurCnt
#	Use r24	as MaxCnt (Constant value)
#	Use r25	as LPixelBuf
#	Use r26	as location within ColorTable32
#	Use r27	as base address of ColorTable32
#	Use r28	as BPP (that is, bytes per pixel)

#	r29	as RightEdge
#	r30	as xtemp (integer)

#	Note that the following are constants, and independent of xtemp.

#	First the integers
#	MaxCnt	in r24
#	LPixelBuf in r25. It changes but need only be loaded 1 time.
#	Base address of ColorTable32 in r27
#	BPP in r28

#	Now the floats	(Newly defined)


#	Zr in f15 (changes as DX is added in on each pass)
#	Use f16	as value of yRel.  This is constant for each line.
#	Use f17	as value that was stored in 40(r1).  It's Zi
#	Use f18	as Zr.
#	Use f19 as Zr2 + Zi2 = |Z2|	Only for comparing with Limit.
#	Use f20    This is scratch and used shortly for 2Zi.
#	Delta x	in f21
#	Two	in f22
#	Limit	in f23
#	Use f24	as Zr2.  This is also a temporary value


	.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
	fmr	f16,f1		#Transfer yrel from f1 to f16

	lwz	r24,_MaxCnt(r2)		#MaxCnt in r24.
	lwz	r25,_LPixelBuf(r2)	#Get Location of LPixelBuf
	lwz	r27,_ColorTable32(r2)	#Start of color table
	lwz	r28,_BPP(r2)             #Number bytes per pixel
	lfd	f25,_Factor(r2)	#Get Factor into f25
	lfd	f22,_Two(r2)	#Get Two into f22 as constant
	lfd	f23,_Limit(r2)	#Get Limit into f23 as constant

	li	r30,0		#xtemp = 0.

#	Find real value associated with xtemp = 0
	fsub	f15,f22,f22	#f15 = f22 - f22 = 0.
	lfd	f10,_XCenter(r2)#f10 = XCenter
	fsub	f15,f15,f10	#xRel = xRel - XCenter
	fdiv	f15,f15,f25	#Zr = xRel/Factor
	lfd	f21,_DX(r2)	#Get the real value of Delta x.
	lwz	r29,_RightEdge(r2)

Start:
	fmr	f18,f15		#Zr -> xRel.
#  The following line was moved before rather than after fmr f17,f16
#  to give the fpu a break.
	li	r23,1		#Initialize CurCnt = 1
	fmr	f17,f16		#yRel -> Zi.
	b	l3		#Check to see if to MaxCnt
l2:	fmul	f24,f18,f18	#Zr2 in f24
	fmul	f4,f17,f17	#Zi2 = Zi*Zi
	fadd	f19,f24,f4	#Zr2 + Zi2 for |Z2|
	fcmpu	cr0,f19,f23	#Compare with Limit in f23.
	ble	cr0,l6

#The following section is not very time-critical as it is only reached once.
#for each pixel
	cmpwi	cr0,r28,2	#See if BPP < 2
	blt	cr0,l8
l7:
	slwi	r10,r23,2	#4 times CurCnt for offset in ColorTable32
	add	r26,r27,r10	#Find location of pixel color in ColorTable32
	lwz	r11,0(r26)	#Get ARGB from ColorTable
	stw	r11,0(r25)	#Store color in LPixelBuf
	addi	r25,r25,4	#Point to next location in LPixelBuf
	b	l1
l8:	andi.	r3,r23,65535	#Mask Color into r3 for transfer to PlotIt
	bl	_PlotIt
l9:	b	l1

#Now calcuate new values for Zi and Zr.
l6:
	fmul	f20,f22,f17	#Find 2*Zi (Two in f22)
	fsub	f10,f24,f4      #Zr2 = Zr2 - Zi2
	fmul	f17,f20,f18	#Find 2*Zi*Zr (temporary Zi)

	addi	r23,r23,1       #Increment CurCnt
	fadd	f18,f15,f10	#New Zr = xRel + Zr2
	fadd	f17,f16,f17	#Zi = Zi + yRel
l3:
	cmpw	cr0,r23,r24	#See if CurCnt < MaxCnt
	blt	cr0,l2		#Repeat until MaxCnt reached

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

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

	stw	r11,0(r25)	#Store color in LPixelBuf
	addi	r25,r25,4	#Point to next pixel in buffer
	b	l1		#Was l12
l11:
	lis	r11,1
	addi	r3,r11,-1	#ffff r3 for transfer to PlotIt
	bl	_PlotIt
l1:
	addi	r30,r30,1       #Increment xtemp
	fadd	f15,f15,f21	#Find new value of xRel.
	cmpw	cr0,r30,r29	#See if to RightEdge

	blt	cr0,Start	#Repeat until row is done.
	stw	r25,_LPixelBuf(r2)  #New location in long pixel buffer
	addi	r1,r1,96        #Balance Stack pointer
	lwz	r11,8(r1)	#Get return address
	mtlr	r11
	blr
	.type	_UseLibP2,@function
	.size	_UseLibP2,$-_UseLibP2

	.global	_MaxCnt
	.global	_RightEdge
	.global	_XCenter
	.global	_Factor
	.global	_Limit
	.global	_Two
	.global	_Color
	.global	_LPixelBuf
	.global	_ColorTable32
	.global	_BPP
	.global	_DX
	.global	_PlotIt
	.global	@__PlotIt

