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


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


#vsc elf
	.text
	.sdreg	13
	.global	UseLibP2
	.align	4
UseLibP2:
	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	11,MaxCnt@ha
	lwz	24,MaxCnt@l(11)	#MaxCnt in r24.
	lis	11,LPixelBuf@ha
	lwz	25,LPixelBuf@l(11)	#Get Location of LPixelBuf
	lis	11,ColorTable32@ha
	lwz	27,ColorTable32@l(11)	#Start of color table
	lis	11,BPP@ha
	lwz	28,BPP@l(11)             #Number bytes per pixel
	lis	11,Factor@ha
	lfd	25,Factor@l(11)	#Get Factor into f25
	lis	11,Two@ha
	lfd	22,Two@l(11)	#Get Two into f22 as constant
	lis	11,Limit@ha
	lfd	23,Limit@l(11)	#Get Limit into f23 as constant

	li	30,0		#xtemp = 0.
# Convert xtemp to double fp.
	lis	12,17200	#r12 = 0x4330

	lis	11,.l14@ha
	stw	12,.l14@l(11)	#Place in buffer

	xoris	12,30,32768   #XOR with 0x8000

	lis	18,.l15@ha
	stw	12,.l15@l(18)     #Store in high word of buffer


	lis	11,.l14@ha
	lfd	0,.l14@l(11)      #Get back as double

	lis	11,.l13@ha
	lfd	10,.l13@l(11)
	fsub	15,0,10		#Find (double)xtemp
	lis	11,XCenter@ha
	lfd	10,XCenter@l(11)	#f10 = XCenter
	fsub	15,15,10	#xRel = xRel - XCenter
	fdiv	15,15,25	#xRel = xRel/Factor
	lis	11,DX@ha
	lfd	21,DX@l(11)	#Get the real value of Delta x.
	lis	11,RightEdge@ha
	lwz	29,RightEdge@l(11)

Start:
	fmr	18,15		#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,18,18	#Zr2 in f24
	fmul	4,17,17		#Zi2 = Zi*Zi
	fadd	19,24,4
	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 for storage as UWORD
	bl	PlotIt
l9:	b	l1
l6:
	fmul	20,22,17	#Find 2*Zi (Two in f22)
	fsub	10,24,4      #Zr2 = Zr2 - Zi2
	fmul	17,20,18	#Find 2*Zi*Zr (temporary Zi)
	addi	23,23,1       #Increment CurCnt
	fadd	18,15,10	#New Zr = xRel + Zr2
	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 into r3 for PlotIt
	bl	PlotIt
l1:
	fadd	15,15,21	#Find new value of xRel.
	addi	30,30,1		#Increment xtemp.
	cmpw	0,30,29		#See if to RightEdge

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

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

	.section .rodata
	.align	2
	.align	3
.l13:
	.long	0x43300000,0x80000000
.l14:	.word	0
.l15:	.word	0,0,0
