
#    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 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)

#	3.00 in f14
#	Zr in f15.  It changes as Zr is recalculated.
#	Use f16	as value of yRel.  This is constant for each line.
#	Use f17	Zi.  It's initially yRel, but changes within loop.
#	Use f18	as xRel. Only changes as DX is added for the next pixel.
#	Use f19 as |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
#	Use f26 as Zr3.


	.text
	.sdreg	13
	.global	UseLibP5
	.align	4
UseLibP5:
	mflr	11
	stw	11,4(1)	#Save return address
	stwu	1,-80(1)      #Save stack pointer
	fmr	16,1		#Transfer yrel from f1 to f16
	lis	4,MaxCnt@ha
	lwz	24,MaxCnt@l(4)		#MaxCnt in r24.
	lis	4,LPixelBuf@ha
	lwz	25,LPixelBuf@l(4)	#Get Location of LPixelBuf
	lis	4,ColorTable32@ha
	lwz	27,ColorTable32@l(4)	#Start of color table

	lis	4,BPP@ha
	lwz	28,BPP@l(4)             #Number bytes per pixel
	lis	4,Factor@ha
	lfd	25,Factor@l(4)	#Get Factor into f25
	lis	4,Two@ha
	lfd	22,Two@l(4)	#Get Two into f22 as constant
	lis	4,Limit@ha
	lfd	23,Limit@l(4)	#Get Limit into f23 as constant

	fadd	14,22,22	#f14 is 4.
	fadd	14,14,22	#f14 is 6.
	fdiv	14,14,22	#f14 is 3.

#The above could have been done with a global definition of 3.000.

	li	30,0		#xtemp = 0.
	fsub	15,22,22	#(float)xtemp = 0.
	lis	4,XCenter@ha
	lfd	10,XCenter@l(4)	#f10 = XCenter
	fsub	15,15,10	#xRel = xRel - XCenter
	fdiv	18,15,25	#xRel = xRel/Factor

	lis	4,DX@ha
	lfd	21,DX@l(4)	#Get the real value of Delta x.
	lis	4,RightEdge@ha
	lwz	29,RightEdge@l(4)

Start:
	fmr	15,18		#Initial value of Zr is xRel

#  The following line was moved before rather than after fmr f17,f16
#  to give the fpu a break.
	li	23,1		#Initialize CurCnt = 1
	fmr	17,16		#yRel into f17 as initial Zi.
	b	l3		#Check to see if to MaxCnt
l2:
	fmul	24,15,15	#Zr2 in f24
	fmul	4,17,17	#Zi2 = Zi*Zi
	fadd	19,24,4	#Zr2 + Zi2 -> f19
	fcmpu	0,19,23	#Compare with Limit in f23.
	ble	0,l6

#The following section is not very time-critical as it is only reached once.
#for each pixel
	cmpwi	0,28,2	#See if BPP < 2
	blt	0,l8
l7:
	slwi	10,23,2	#4 times CurCnt for offset in ColorTable32
	add	26,27,10	#Find location of pixel color in ColorTable32
	lwz	11,0(26)	#Get ARGB from ColorTable
	stw	11,0(25)	#Store color in LPixelBuf
	addi	25,25,4	#Point to next location in LPixelBuf
	b	l1

l8:	andi.	3,23,65535	#Prep Color as UWORD for PlotIt
	bl	PlotIt
l9:	b	l1
l6:

	fsub	27,24,4	#Zr2 - Zi2 -> f27
	fmul	20,4,14	#Zi2*3 -> f20
	fmul	28,24,14	#3*Zr2 -> f28
	fsub	20,24,20	#Zr2 - 3*Zi2 -> f20

	fsub	28,4,28	#Zi2 - 3*Zr2 -> f28

	fmul	29,28,15	#Zr*(Zi2 - 3Zr2) -> f29
	fmul	30,27,20	#(Zr2 - 3Zi2)*(Zr2 - Zi2) -> f30
	fmul	29,29,4	#Zi2*Zr*(Zi2 - 3Zr2) -> f29
	fmul	30,30,15	#Zr*(Zr2 - Z3*Zi2)*(Zr2 - Zi2) -> f30
	fmul	29,29,22	#2*Zi2*Zr(Zi2 - 3Zr2) -> f29

	fmul	31,28,27	#(Zi2 - 3Zr2)(Zr2 - Zi2) -> f31
	fadd	30,30,29	#Zr3 -> f30

	fmul	29,20,24	#Zr2*(Zr2 - 3Zi2) -> f29
	fmul	31,31,17	#Times Zi
	fmul	29,29,17	#Zi*Zr2*(Zr2 - 3Zi2) -> f29

	fadd	15,18,30	#New Zr = xRel + Zr2

	fmul	29,29,22	#times 2.0 -> f29

	fsub	17,29,31	#New Zi
#	fmr	24,30       #New Zr




	addi	23,23,1	#Increment CurCnt
	fadd	17,16,17	#Zi = Zi + yRel

l3:
	cmpw	0,23,24	#See if CurCnt < MaxCnt
	blt	0,l2		#Repeat until MaxCnt reached

	cmpwi	0,28,2	#See if BPP < 2
	blt	0,l11
l10:

	lis	12,4
	addi	12,12,-4
	add	26,27,12	#Find location of pixel color
	lwz	11,0(26)      #Get color

	stw	11,0(25)	#Store color in LPixelBuf
	addi	25,25,4	#Point to next pixel in buffer
	b	l1		#Was l12
l11:
	lis	11,1
	addi	3,11,-1	#ffff for PlotIt
	bl	PlotIt
l1:
	fadd	18,18,21	#Find new value of xRel (xRel = xRel + DX).
	addi	30,30,1	#Increment xtemp.
	cmpw	0,30,29	#See if to RightEdge

	blt	0,Start	#Repeat until row is done.
	lis	4,LPixelBuf@ha
	stw	25,LPixelBuf@l(4)  #New location in long pixel buffer
	addi	1,1,80        #Balance Stack pointer
	lwz	11,4(1)	#Get return address
	mtlr	11

	blr
	.type	UseLibP5,@function
	.size	UseLibP5,$-UseLibP5

	.globl	MaxCnt
	.globl	RightEdge
	.globl	XCenter
	.globl	Factor
	.globl	Limit
	.globl	Two
	.globl	LPixelBuf
	.globl	ColorTable32
	.globl	BPP
	.globl	DX
	.globl	PlotIt

