**********************************************************************
		incdir	"sys:include/"
		include	"exec/exec_lib.i"
		include	"hardware/custom.i"
**********************************************************************
CALL		MACRO
		jsr	_LVO\1(a6)
		ENDM
**********************************************************************
		rsset	-4
PixelMask	rs.w	1	; mask to "OR" with bitplanes
WordsInRaster	rs.w	1	; number of words left in current raster
InitialX	rs.w	1	; inital x
InitialY	rs.w	1	; inital y
LinesLeft	rs.w	1	; number of lines left to draw
**********************************************************************
		movea.l	4.w,a6
		bra.b	BootBlock+12

BootBlock:	dc.b	"DOS",0
		dc.l	"CKSM"
		dc.l	$00000370

		movem.l	d1-d7/a0-a6,-(a7)

		moveq	#1,d0
		swap	d0			; 64k
		moveq	#2,d1
		CALL	AllocMem
		tst.l	d0
		beq.w	NoMem

		move.l	d0,-(a7)

		CALL	Forbid
		CALL	Disable
		lea	$dff000,a4
		bsr.w	WaitVBlank
		movea.l	(a7),a5
		adda.l	#256*40*5,a5
		move.l	a5,cop2lc(a4)
		clr.w	copjmp2(a4)
		move.w	#$07e0,dmacon(a4)
		move.w	#$8240,dmacon(a4)

		lea	Copper(pc),a6
		moveq	#((Palette-Copper)/4)-1,d7
L2		move.l	(a6)+,(a5)+
		dbf	d7,L2

		move.w	#$0180,d0		; color 0
		moveq	#31,d7
L3		move.w	d0,(a5)+
		move.w	(a6)+,(a5)+
		addq.w	#2,d0
		dbf	d7,L3

		move.w	#$00e2,d0		; init bitplane pointers
		move.l	(a7),d1
		moveq	#8,d7
L4		move.w	d0,(a5)+		; low word first
		subq.w	#2,d0		
		move.w	d1,(a5)+
		move.w	d0,(a5)+
		addq.w	#6,d0
		swap	d1
		move.w	d1,(a5)+
		swap	d1
		addi.l	#256*40,d1
		subq.l	#2,d7
		bpl.b	L4

		move.l	d7,(a5)+		; $fffffffe

		move.l	#$80000014,(a5)+	; mask & word per raster count

StartJulia:	movea.l	(a7),a0			; get pointer to bitplanes
.1		btst.b	#6,dmaconr(a4)
		bne.b	.1
		move.l	a0,bltdpt(a4)
		move.l	#$01000000,bltcon0(a4)
		clr.w	bltdmod(a4)
		move.w	#(640*64)+40,bltsize(a4)	; 256*40*5 bytes

		move.l	(a6)+,(a5)
		bne.b	NoReset
		lea	JuliaData(pc),a6
		move.l	(a6)+,(a5)

NoReset:	lea	Plot00+2(pc),a1
		move.w	(a5),(RasterInit-Plot00)(a1)	; initial m
		move.w	(a6),(a1)			; pixel step
		move.w	(a6)+,(RasterAdd-Plot00)(a1)	; raster step
		move.l	(a6)+,(BootBlock-(Plot00+2))(a1)  ; initial c1 and c2
		move.l	a6,((BootBlock+4)-(Plot00+2))(a1) ; save julia pointer
		move.w	#256,LinesLeft(a5)		; vertical height
		lea	256*40(a0),a1
		lea	256*40(a1),a2
		lea	256*40(a2),a3
		lea	$04000000,a6		; for magnitude test

.1		btst.b	#6,dmaconr(a4)
		bne.b	.1

		bsr.w	WaitVBlank
		move.w	#$8380,dmacon(a4)
		lea	256*40(a3),a4

PixelLoop:	move.w	(a5),d1			; initial x
		move.w	InitialY(a5),d0		; initial y
		moveq	#30,d7
		movem.w	BootBlock(pc),d4-d5	; initial c1 and c2
		move.w	d0,d2
		move.w	d1,d3
		bra.b	CheckMagnitude

IterateJulia:	sub.l	d3,d2		; x^2 - y^2
		lsl.l	#4,d2		; fix decimal point
		swap	d2		; ...
		add.w	d4,d2		; x1 = x^2 - y^2 + c1

		move.w	d1,d3		; y
		muls	d0,d3		; x * y
		lsl.l	#5,d3		; fix decimal point and multiply by 2
		swap	d3		; ...
		add.w	d5,d3		; y1 = 2 * x * y + c2

		move.w	d2,d0		; x = x1
		move.w	d3,d1		; y = y1

CheckMagnitude:	muls	d2,d2		; x^2
		muls	d3,d3		; y^2
		move.l	d2,d6
		add.l	d3,d6		; z = x^2 + y^2
		cmp.l	a6,d6		; escaped yet?
		dbhi	d7,IterateJulia
		move.w	PixelMask(a5),d6
		moveq	#0,d5
		move.b	JumpTable+1(pc,d7.w),d5
		jmp	JumpTable(pc,d5.w)

JumpTable:	dc.b	Plot00-JumpTable,Plot31-JumpTable
		dc.b	Plot30-JumpTable,Plot29-JumpTable
		dc.b	Plot28-JumpTable,Plot27-JumpTable
		dc.b	Plot26-JumpTable,Plot25-JumpTable
		dc.b	Plot24-JumpTable,Plot23-JumpTable
		dc.b	Plot22-JumpTable,Plot21-JumpTable
		dc.b	Plot20-JumpTable,Plot19-JumpTable
		dc.b	Plot18-JumpTable,Plot01-JumpTable
		dc.b	Plot16-JumpTable,Plot15-JumpTable
		dc.b	Plot14-JumpTable,Plot13-JumpTable
		dc.b	Plot12-JumpTable,Plot11-JumpTable
		dc.b	Plot10-JumpTable,Plot09-JumpTable
		dc.b	Plot08-JumpTable,Plot07-JumpTable
		dc.b	Plot06-JumpTable,Plot05-JumpTable
		dc.b	Plot04-JumpTable,Plot03-JumpTable
		dc.b	Plot02-JumpTable,Plot17-JumpTable

Plot22:		or.w	d6,(a4)
		or.w	d6,(a2)
		or.w	d6,(a1)
		bra.b	Plot00

Plot21:		or.w	d6,(a4)
		or.w	d6,(a2)
		or.w	d6,(a0)
		bra.b	Plot00

Plot20:		or.w	d6,(a4)
		or.w	d6,(a2)
		bra.b	Plot00

Plot18:		or.w	d6,(a4)
		or.w	d6,(a1)
		bra.b	Plot00

Plot26:		or.w	d6,(a4)
Plot10:		or.w	d6,(a3)
		or.w	d6,(a1)
		bra.b	Plot00

Plot23:		or.w	d6,(a4)
		or.w	d6,(a2)
		or.w	d6,(a1)
		or.w	d6,(a0)
		bra.b	Plot00

Plot19:		or.w	d6,(a4)
		or.w	d6,(a1)
		or.w	d6,(a0)
		bra.b	Plot00

Plot27:		or.w	d6,(a4)
Plot11:		or.w	d6,(a3)
		or.w	d6,(a1)
		or.w	d6,(a0)
		bra.b	Plot00

Plot17:		or.w	d6,(a4)
		or.w	d6,(a0)
		bra.b	Plot00

Plot25:		or.w	d6,(a4)
Plot09:		or.w	d6,(a3)
		or.w	d6,(a0)
		bra.b	Plot00

Plot29:		or.w	d6,(a4)
Plot13:		or.w	d6,(a3)
Plot05:		or.w	d6,(a2)
		or.w	d6,(a0)
		bra.b	Plot00

Plot16:		or.w	d6,(a4)
		bra.b	Plot00

Plot24:		or.w	d6,(a4)
Plot08:		or.w	d6,(a3)
		bra.b	Plot00

Plot28:		or.w	d6,(a4)
Plot12:		or.w	d6,(a3)
Plot04:		or.w	d6,(a2)
		bra.b	Plot00

Plot30:		or.w	d6,(a4)
Plot14:		or.w	d6,(a3)
Plot06:		or.w	d6,(a2)
Plot02:		or.w	d6,(a1)
		bra.b	Plot00

Plot31:		or.w	d6,(a4)
Plot15:		or.w	d6,(a3)
Plot07:		or.w	d6,(a2)
Plot03:		or.w	d6,(a1)
Plot01:		or.w	d6,(a0)
Plot00:		addi.w	#$0000,(a5)		; pixel "step"
		ror.w	PixelMask(a5)		; shift mask over
		bpl.w	PixelLoop

		addq.w	#2,a0
		addq.w	#2,a1
		addq.w	#2,a2
		addq.w	#2,a3
		addq.w	#2,a4
		subq.w	#1,WordsInRaster(a5)	; subtract from word counter
		bne.w	PixelLoop

		btst.b	#6,$bfe001
		beq.b	Exit

		move.w	#320/16,WordsInRaster(a5)	; words per raster
		btst.b	#2,$dff016		; new julia?
		beq.b	NewJulia

RasterInit:	move.w	#$0000,(a5)		; inital M value
RasterAdd:	addi.w	#$0000,InitialY(a5)	; raster "step"
		subq.w	#1,LinesLeft(a5)
		bne.w	PixelLoop

WaitMouse:	btst.b	#6,$bfe001
		beq.b	Exit
		btst.b	#2,$dff016
		bne.b	WaitMouse

NewJulia:	movea.l	BootBlock+4(pc),a6
		lea	$dff000,a4
.waitRMBup	btst.b	#2,$0016(a4)
		beq.b	.waitRMBup
		bra.w	StartJulia

Exit:		lea	$dff000,a4
		bsr.b	WaitVBlank
		clr.w	$0100(a4)

		move.l	(a7)+,a1
		moveq	#1,d0
		swap	d0
		movea.l	4.w,a6
		CALL	FreeMem

NoMem:		movem.l	(a7)+,d1-d7/a0-a6
		CALL	Enable
		CALL	Permit
		lea	DosName(pc),a1
		CALL	FindResident
		movea.l	d0,a0
		movea.l	$16(a0),a0
		moveq	#0,d0
		rts	

WaitVBlank:	btst.b	#0,5(a4)
		bne.b	WaitVBlank
.1		btst.b	#0,5(a4)
		beq.b	.1
		rts	

Copper:		dc.l	$01005200,$01020000,$01080000
		dc.l	$008e2981,$009029c1
		dc.l	$00920038,$009400d0

Palette:	dc.w	$0000,$0831,$000e,$000d,$000c,$000b,$000a,$0009
		dc.w	$0008,$0007,$0106,$0205,$0304,$0403,$0502,$0611
		dc.w	$0720,$0fff,$0942,$0a53,$0b64,$0c75,$0d86,$0c97
		dc.w	$0ba8,$0a9a,$098b,$0879,$0767,$0555,$0343,$0131

JuliaData:	dc.l	$f800eb00
		dc.w	$0018,$0100,$0ad0,$ec00
		dc.l	$ef000010
		dc.w	$0600,$0100,$f226,$fd56
		dc.l	$0015ee00
		dc.w	$fb40,$ede2,$f0b2,$001d
		dc.l	$05c0ff00
		dc.w	$ef12,$e812,$001d,$f320
		dc.l	$fd21eeae
		dc.w	$0225,$000d,$f420,$fd43
		dc.l	$00000000

DosName:	dc.b	'dos.library',0

BootLen		equ	*-BootBlock

