

	include 'lvo/exec_lib.i'
	include 'lvo/graphics_lib.i'
	include 'lvo/dos_lib.i'
	include 'lvo/layers_lib.i'
	include 'exec/execbase.i'
	include 'exec/memory.i'
	include 'exec/libraries.i'
	include 'graphics/gfx.i'
	include 'graphics/clip.i'
	include 'graphics/rastport.i'
	include 'utility/hooks.i'
	include 'blazec2p.i'

	;1.45/1.5
	; - added more efficient 16bit swap
	; - fixed masked 4bit rp routine
	; - made patch install more multitasking friendly
	;
	;1.55
	; - combined temp/dest bitmap build loops
	;
	;1.56
	; - Added conversion code just for non-aligned stuff.
	; - This gets rid of the temp planes on the stack and
	;     all the startup code that went into building them.
	; - Still have to leave it in tho, for masked crap
	;     but now non-aligned blits go to 11 ;)
	; - Fixed masked rp code since combining temp/dest loops
	;     broke it.
	;
	;1.65
	; - Fixed new non-aligned code. It read everything from the bitmap,masked
	;   or'd and then wrote back into the bitmap which doesn't work on interleaved
	;   bitmaps that are 16 pixels wide or smaller (doh!!!!)
	;
	;1.67
	; - streamlined convertion loops for all combinations of F/M/L conversions.
	; - inlined DoHookClipRects(), should now work from OS 2.04 on.
	;
	;1.7
	; - reworked stack/reg usage, eeked a bit more speed out
	; - reworked merge ops for 1/2/3/5/6/7 plane c2p to drop unused data
	;   instead of preserving it like the original merges did.
	;
	;1.73
	; - Moved code from the main loops to the BlitChunky routine, saved some space
	;   from redundant code.
	; - Patch now only builds a temp bitmap on the stack when operating on a masked rp.
	;   This saves 64 bytes of stack that gets wasted 99% of the time since it's not used.
	; - Now regular code stores data in parts of the stack only used by masked rp routine
	;   which saves having to save/restore registers, which is a little faster.
	; - steamlined some code to make it a little faster.
	;
	;1.74
	; - Now checks to see if unaligned blits are 32 pixels or smaller and allows them
	;   to stay unaligned if possible.  This is more efficient on small, non-aligned
	;   blits since it's the same # of chip-ram accesses but only 1 c2p instead of 2.
	;   Only tested on a 030 but should hold true for all 020+

	MACHINE 68030
	OPT 3


	SECTION text,code

	lea	(execbase,pc),a1
	movea.l 4.w,a6
	move.l	a6,(a1)
	btst	#AFB_68020,(AttnFlags+1,a6)
	beq.w	no020

	move.l	#endpatch-patch,d0
	move.l	#MEMF_PUBLIC|MEMF_FAST|MEMF_REVERSE,d1
	jsr	(_LVOAllocMem,a6)
	lea	(newloc,pc),a1
	move.l	d0,(a1)
	beq.w	nomem


	moveq	#39,d0
	lea	(gfxname,pc),a1
	jsr	(_LVOOpenLibrary,a6)	      ; OpenLibrary
	lea	(gfxbase,pc),a0
	move.l	d0,(a0)
	beq.b	nolibs

	move.l	#endpatch-patch,d0
	lea	(patch,pc),a0
	move.l	(newloc,pc),a1
	jsr	(_LVOCopyMem,a6)

	jsr	(_LVOForbid,a6)

	movea.l (gfxbase,pc),a1
	move.w	#_LVOWritePixelLine8,a0
	move.l	(newloc,pc),d0
	addq.l	#4,d0
	jsr	(_LVOSetFunction,a6)

	movea.l (gfxbase,pc),a1
	move.w	#_LVOWritePixelArray8,a0
	move.l	(newloc,pc),d0
	add.l	#NewWritePixelArray8-patch,d0
	jsr	(_LVOSetFunction,a6)

	movea.l (gfxbase,pc),a1
	cmp.w	#40,(LIB_VERSION,a1)
	blt.b	noV40
	move.w	#_LVOWriteChunkyPixels,a0
	move.l	(newloc,pc),d0
	add.l	#NewWriteChunkyPixels-patch,d0
	jsr	(_LVOSetFunction,a6)

noV40	jsr	(_LVOCacheClearU,a6)
	jsr	(_LVOPermit,a6)
	moveq	#0,d0
	rts
	cnop	0,4



no020	moveq	#20,d0
	rts

nomem	moveq	#10,d0
	rts

nolibs	move.l	(newloc,pc),a1
	move.l	#endpatch-patch,d0
	jsr	(_LVOFreeMem,a6)
	moveq	#30,d0
	rts
	cnop	0,4


gfxname:
	dc.b	'graphics.library',0
	dc.b	'$VER: BlazeWCP 1.74 (22.05.2002) by Rick Pratt',0
	cnop	0,4
newloc
	dc.l	0

patch
	; d0.w - xstart
	; d1.w - ystart
	; d2.w - width
	; a0.l - rp
	; a1.l - temprp
	; a2.l - array
	;
	; returns pixels ploted in d0
	;
	dc.b	'BWCP'

NewWritePixelLine8
	movem.l d2-d7/a2-a5,-(sp)
	move.w	d2,d4
	move.w	d1,d3
	add.w	d0,d2
	addi.w	#15,d4
	subq.w	#1,d2
	andi.w	#-16,d4
	bra.b	WPx8
	cnop	0,4


	; d0.w - xstart
	; d1.w - ystart
	; d2.w - xstop
	; d3.w - ystop
	; a0.l - rp
	; a1.l - temprp
	; a2.l - array
	;
	; returns pixels plotted in d0
	;
	dc.b	'BWCP'

NewWritePixelArray8
	movem.l d2-d7/a2-a5,-(sp)
	move.w	d2,d4
	moveq	#-16,d5
	sub.w	d0,d4
	sub.w	d5,d4	; d4-(-16)=d4+16  or (xstop-xstart+1)+15
	and.w	d5,d4
	bra.b	WPx8
	cnop	0,4


	; d0 - xstart
	; d1 - ystart
	; d2 - xstop
	; d3 - ystop
	; d4 - bytesperrow
	; a0 - rp
	; a2 - array
	;
	; returns nothing
	;
	dc.b	'BWCP'

NewWriteChunkyPixels
	movem.l d2-d7/a2-a5,-(sp)
	;
	; Make Rectangle on stack (see graphics/gfx.h for structure)
	; save ystop
	; save xstop
	; save ystart
	; save xstart	{ (sp) = Rect }
	;
WPx8	movem.w d0-d3,-(sp)

	move.l	sp,d6

	sub.w	d0,d2
	clr.l	-(sp)
	addq.w	#1,d2
	movem.w d0/d1/d2/d4,-(sp)
	move.l	a2,-(sp)	  ; save array	  { (sp) = data, (12,sp) = Rect }
	;12 - # pixels plotted
	;10 - width of chunky data
	;8  - xstop-xstart+1	   ; width  of blit
	;6  - ystart
	;4  - xstart
	;0  - array
	;
	movea.l a0,a1		; get rastport

	move.l	sp,d7	     ; get h_Data
	movea.l (a1),d4      ; d4 = rp->rp_Layer
	BNE.B	have_layer   ; nope, branch

	moveq	#0,d0
	movea.l d6,a4	     ; rect
	MOVE	(2,a4),d0    ;
	move.l	d0,-(sp)     ; offsety
	movea.l a1,a2	     ; rp
	MOVE	(a4),d0      ;
	move.l	d0,-(sp)     ; offsetx
	move.l	(4,a4),-(sp) ; bounds
	move.l	(a4),-(sp)   ; bounds
	move.l	d4,-(sp)     ; layer
	movea.l sp,a1	     ; pass message
	bsr.w	HookFunc
	move.l	(32,sp),d0
	lea	(44,sp),sp
	move.l	(gfxbase,pc),a6    ; get GfxBase
	movem.l (sp)+,d2-d7/a2-a5
	rts
	cnop	0,4

gfxbase     dc.l    0
execbase    dc.l    0


have_layer
	move.l	a1,d5	    ; save rp
	CLR.L	(a1)+	    ; clear rp->rp_layer
	move.l	(a1),-(sp)  ; save rp->rp_BitMap
	move.l	d4,a2	    ;
	SUBQ.L	#8,sp	    ; reserve space
	move.l	(execbase,pc),a6

	lea	(lr_Lock,a2),a0
	jsr	(_LVOObtainSemaphore,a6)
	MOVE	(lr_MinX,a2),d2      ;lr_MinX
	SUB	(lr_Scroll_X,a2),d2  ;lr_Scroll_X
	MOVE	(lr_MinY,a2),d3      ;lr_MinY
	SUB	(lr_Scroll_Y,a2),d3  ;lr_Scroll_Y
	movea.l sp,a1
	movea.l d6,a0
	move.l	d6,-(sp)	; is rect NULL ?
	BEQ.B	NRec		; yep, branch
	move.l	a1,d6
	MOVE	(a0)+,d0
	ADD	d2,d0	   ; ra_MinX + boundsX
	MOVE	d0,(a1)+
	MOVE	(a0)+,d0
	ADD	d3,d0	   ; ra_MinY + boundsY
	MOVE	d0,(a1)+
	MOVE	(a0)+,d0
	ADD	d2,d0	   ; ra_MaxX + boundsX
	MOVE	d0,(a1)+
	MOVE	(a0)+,d0
	ADD	d3,d0	   ; ra_MaxY + boundsY
	MOVE	d0,(a1)+
NRec	LEA	(lr_ClipRect,a2),a5  ; lr_ClipRect
	SUBQ.L	#8,sp


	; for BitMaps/ClipRects
	;
	;
	;
	;
Rend_cr move.l	(a5),d0       ; lr_ClipRect = 0
	BEQ	Rend_Super_cr ; yep, try SuperClipRects
	movea.l d0,a5
	LEA	(cr_MinX,a5),a4 ; cr_MinX
	TST.L	d6
	BEQ.B	cr_NoRect
	movea.l d6,a1
	movea.l sp,a2
	move.l	(a4)+,d0   ; cr_MinX/cr_MinY
	move.l	(a1)+,d1   ; rect minX/MinY
	cmp.l	d0,d1
	ble.b	10$
	exg	d0,d1
10$	cmp.w	d0,d1
	ble.b	20$
	move.w	d1,d0
20$	move.l	d0,(a2)+
	move.l	(a4)+,d0   ; cr_MaxX/cr_MaxY
	move.l	(a1)+,d1   ; rect maxX/maxY
	cmp.l	d0,d1
	bge.b	30$
	exg	d0,d1
30$	cmp.w	d0,d1
	bge.b	40$
	move.w	d1,d0
40$	move.l	d0,(a2)
	move.l	-(a2),d1
	CMP	d1,d0
	BLT.B	Rend_cr
	CMP.L	d1,d0
	BLT.B	Rend_cr
	movea.l sp,a4

cr_NoRect
	movea.l d5,a2	     ; RastPort
	TST.L	(cr_lobs,a5) ; cr_lobs
	BNE.B	cr_Lobs
	moveq	#0,d0
	MOVE	(2,a4),d0    ;
	SUB	d3,d0	     ;
	move.l	d0,-(sp)     ; offsety
	MOVE	(a4),d0      ;
	SUB	d2,d0	     ;
	move.l	d0,-(sp)     ; offsetx
	move.l	(4,a4),-(sp) ; bounds
	move.l	(a4),-(sp)   ; bounds
	move.l	d4,-(sp)     ; layer
	movea.l sp,a1	     ; pass message
	movem.l d2-d7/a5,-(sp)
	bsr.w	HookFunc
	movem.l (sp)+,d2-d7/a5
	LEA	(20,sp),sp
	bra.b	Rend_cr
	cnop	0,4



      ; for ClipRects with lobs
      ;
      ; a0 = Hook
      ; a1 = layer
      ; a2 = RastPort
      ; a4 = current rect
      ; a5 = ClipRect
      ;
cr_Lobs TST.L	(cr_BitMap,a5)	       ; cr_BitMap
	BEQ.B	Rend_cr 	 ; cliprect has no bitmap
	moveq	#15,d1
	move.w	(cr_MinX,a5),d0  ; cr_MinX
	and.w	d0,d1		 ; 15 & cr_MinX
	sub.w	d0,d1		 ; (15 & cr_MinX)-cr_MinX
	move.l	(4,a4),d0	 ; MaxX,MaxY
	swap	d1
	add.l	d1,d0		 ; MaxX+((15 & cr_MinX)-cr_MinX) , MaxY
	sub.w	(cr_MinY,a5),d0  ; MaxX+((15 & cr_MinX)-cr_MinX) , MaxY-cr_MinY
	move.l	d0,-(sp)
	add.l	(a4),d1 	 ; MinX+((15 & cr_MinX)-cr_MinX) , MinY
	SUB	(cr_MinY,a5),d1  ; MinX+((15 & cr_MinX)-cr_MinX) , MinY-cr_MinY
	move.l	d1,-(sp)
	movea.l sp,a3
	move.l	(rp_BitMap,a2),a6	      ; save rp_BitMap
	move.l	(cr_BitMap,a5),(rp_BitMap,a2) ; cr_BitMap -> rp_BitMap
	moveq	#0,d0
	MOVE	(2,a4),d0    ; MinY
	SUB	d3,d0	     ;
	move.l	d0,-(sp)     ; offsety
	MOVE	(a4),d0      ; MinX
	SUB	d2,d0	     ;
	move.l	d0,-(sp)     ; offsetx
	move.l	(4,a3),-(sp) ; bounds MaxX+((15 & cr_MinX)-cr_MinX) ,MaxY-cr_MaxY
	move.l	(a3),-(sp)   ; bounds MinX+((15 & cr_MinX)-cr_MinX) ,MinY-cr_MinY
	move.l	d4,-(sp)     ;
	movea.l sp,a1	     ; pass message
	movem.l d2-d7/a2/a5-a6,-(sp)
	bsr.w	HookFunc
	movem.l (sp)+,d2-d7/a2/a5-a6
	LEA	(28,sp),sp
	move.l	a6,(rp_BitMap,a2)    ; restore rp_BitMap
	BRA	Rend_cr
	cnop	0,4

QuitDHCR:
	ADDQ.L	#8,sp
	movea.l d5,a0
	move.l	(sp)+,(rp_BitMap,a0)  ; rp_BitMap
	move.l	d4,(a0)
	movea.l d4,a0
	move.l	(execbase,pc),a6
	lea	(lr_Lock,a0),a0
	jsr	(_LVOReleaseSemaphore,a6)
	move.l	(32-20,sp),d0
	lea	(44-20,sp),sp
	move.l	(gfxbase,pc),a6    ; get GfxBase
	movem.l (sp)+,d2-d7/a2-a5
	rts
	cnop	0,4


	; for SuperBitMaps/SuperClipRects
	;
	;
	; a2 = Layer
	; a5 = ClipRect
	;
Rend_Super_cr
	ADDQ.L	#8,sp
	move.l	(sp)+,d6
	movea.l d4,a2	    ; restore layer to a2.
	move.l	(lr_SuperBitMap,a2),d0	; lr_SuperBitMap
	BEQ.B	QuitDHCR    ; nope, quit
	movea.l d5,a0	    ; restore rastport
	move.l	d0,(rp_BitMap,a0)   ; lr_SuperBitMap -> rp_BitMap
	LEA	(lr_SuperClipRect,a2),a5  ; lr_SuperClipRect


	;
	; This section loops thru SuperClipRects, calls the hook
	;  if needed and quits when done.

Super_cr
	move.l	(a5),d0     ; lr_SuperClipRect = null
	BEQ.B	QuitDHCR    ; yep, quit
	movea.l d0,a5
	LEA	(cr_MinX,a5),a4  ; cr_MinX
	TST.L	d6	    ; rect = null
	BEQ.B	50$	    ; yep, so just go do it
	movea.l d6,a1
	movea.l sp,a2
	move.l	(a4)+,d0    ; This is the SuperClipRects rect
	move.l	(a1)+,d1    ; This is what we passed.
	cmp.l	d0,d1
	ble.b	10$
	exg	d0,d1
10$	cmp.w	d0,d1
	ble.b	20$
	move.w	d1,d0
20$	move.l	d0,(a2)+
	move.l	(a4)+,d0
	move.l	(a1)+,d1
	cmp.l	d0,d1
	bge.b	30$
	exg	d0,d1
30$	cmp.w	d0,d1
	bge.b	40$
	move.w	d1,d0
40$	move.l	d0,(a2)
	move.l	-(a2),d1
	CMP	d1,d0
	BLT.B	Super_cr
	CMP.l	d1,d0
	BLT.B	Super_cr
	movea.l sp,a4
50$	movea.l d5,a2
	moveq	#0,d0	     ; this does the hook
	MOVE	(2,a4),d0    ;
	move.l	d0,-(sp)     ; offsety
	MOVE	(a4),d0      ;
	move.l	d0,-(sp)     ; offsetx
	move.l	(4,a4),-(sp) ; bounds
	move.l	(a4),-(sp)   ; bounds
	move.l	d4,-(sp)     ; layer
	movea.l sp,a1	     ; pass message
	movem.l d4-d7/a5,-(sp)
	bsr.b	HookFunc
	movem.l (sp)+,d4-d7/a5
	LEA	(20,sp),sp
	bra.b	Super_cr
	cnop	0,4





HookFunc:   ; a0 = Hook, a1 = message [layer, bounds, offsetx, offsety], a2 = RastPort
	moveq	#stacked,d1
	suba.l	d1,sp
	movea.l d7,a6		   ;(h_Data,a0),a6  ; a6 = Hook->h_Data (16)
	movea.l (rp_BitMap,a2),a4	; a4 = BitMap
	move.l	a2,a5
	move.b	(bm_Depth,a4),d5	; d5 = BitMap->Depth
	beq.w	crap_c2p
	cmpi.b	#9,d5
	bge.w	crap_c2p
	move.w	(a4),d1   ;(bm_BytesPerRow,a4),d1
	move.l	d1,(DBPR,sp)
	extb.l	d5
	moveq	#31,d7


	;
	;
	; This code generates height, width area and chunky displacement
	;
	;
	movea.l a1,a3	    ; a3 = message
	move.w	(4,a6),a2   ; a2 = xstart	  4,a6
	move.l	(12,a1),d1  ; d1 = offsetx
	sub.l	a2,d1	    ;	 = offsetx-xstart
	move.w	(6,a6),a2   ; a2 = ystart	  6,a6
	move.l	(16,a1),d2  ; d2 = offsety
	sub.l	a2,d2	    ;	 = offsety-ystart
	muls.w	(10,a6),d2  ; d2 = (chunky BytesPerRow)*(offsety-ystart) 10,a6
	movea.l (a6),a2     ; a2 = pointer to array
	adda.l	d2,a2	    ; a2 = array + ((chunky BytesPerRow)*(offsety-ystart))
	adda.l	d1,a2	    ; a2 = (array+((chunky BytesPerRow)*(offsety-ystart))) + (xoffset-xstart)
	addq.l	#4,a1
	;
	;	d2 = Actual # of pixel wide
	;
	move.w	(a1)+,d0    ; d0 = MinX
	moveq	#0,d2
	move.w	(8,a3),d2   ; d2 = MaxX
	sub.w	d0,d2	    ; d2 = MaxX-MinX
	addq.w	#1,d2	    ; d2 = (MaxX-MinX)+1
	;
	;	d1 = Actual # of pixels high
	;
	moveq	#0,d1
	move.w	(10,a3),d1  ; d1 = MaxY
	sub.w	(a1)+,d1    ; d1 = MaxY-MinY
	addq.w	#1,d1	    ; d1 = (MaxY-MinY)+1


	move.l	d2,d3
	muls.w	d1,d3
	add.l	d3,(12,a6)  ; this WILL make it give the correct # of pixels plotted
	;
	;
	; The code below calculates how the bitmap is aligned and builds masks, etc...
	;
	;
	move.w	d1,(Lines,sp)
	moveq	#32,d4		; d4 = 32
	sub.w	d0,d4		; d4 = 32-MinX
	and.l	d7,d4		; d4 = (32-MinX) & $0000001f  shift into 1st LW
	move.w	(a1),d0 	; d0 = MaxX
	addq.w	#1,d0		; d0 = MaxX+1
	and.l	d7,d0		; d0 = (MaxX+1) & $0000001f shift into last LW
	moveq	#32,d1		; d1 = 32
	sub.l	d4,d1		; d1 = 32-( (32-MinX)&31 ) amount of data in 1st LW
	moveq	#-1,d3
	lsr.l	d1,d3		; d3 = -1>>32-((32-MinX)&31) start mask
	moveq	#32,d1		; d1 = 32
	sub.l	d0,d1		; d1 = 32-((MaxX+1) & 31)
	move.l	d3,(FLWmask,sp) ; firstword mask save d3
	moveq	#-1,d3
	lsl.l	d1,d3		; d1 = -1 << by 32-((MaxX+1) & 31)
	moveq	#0,d6
	move.w	(10,a6),a6	;(HData,sp)
	move.w	(6,a3),d6
	add.w	(4,a3),d7
	muls.w	(a4),d6
	lsr.l	#5,d7
	lsl.l	#2,d7
	add.l	d7,d6

	move.l	d5,d7
	subq.l	#1,d7
	;
	;
	;
	;	Code builds inital pointers into temp/dest bitmap
	;
DispBuild
	movea.l (8,a4,d7.l*4),a1
	adda.l	d6,a1
	move.l	a1,(DP0point,sp,d7.l*4)
	dbf	d7,DispBuild


	moveq	#-1,d7
	lsl.b	d5,d7	      ; mask for how many planes we are using
	not.l	d7
	move.b	(rp_Mask,a5),d6 ; rp_Mask & #plane mask gives mask for this op
	and.b	d7,d6	      ; just in case the mask has more bits then planes
	subq.w	#1,d5
	cmp.b	d7,d6	      ; we need
	bne.w	Masked_rp


	;d2 = (MaxX-MinX)+1	blitwidth
	;d4 = 32-(32-MinX)&31)	amount of data in 1st LW
NotMasked
	move.w	d5,(NLWblit,sp)
	cmp.w	d4,d2	       ; blitwidth-(amount in 1st LW)
	bgt.b	multiLW        ; convertion larger then then 1 lw, blit goes beyond 1st lw
	move.l	(FLWmask,sp),d6
	tst	d0	       ; end of convertion bit
	beq.b	blF	       ; ends at 32bit bound
	and.l	d3,d6	       ; blit smaller then 1st lw

blF
	move.w	#-4,a5
blF2	lea	(-32,a2,d4.l),a0
	lea	(-32,a6),a6
	bra.b	30$
	cnop	0,4

10$	lea	(DBPR,sp),a3
	move.w	d5,d7
	move.l	(a3)+,d0
20$	add.l	d0,(a3)+
	dbf	d7,20$
	adda.w	a6,a0
30$	bsr.w	AlignChunky
	subq.w	#1,(Lines,sp)
	bgt.b	10$
	lea	(stacked,sp),sp
	rts
	cnop	0,4


multiLW
	sub.w	d4,d2
	sub.w	d0,d2		; (width-start)-end=remaining conversion size
	andi.w	#-32,d2
	bne.w	LW1
	tst.w	d4		; no middle  x0x
	bne.b	blFL
	suba.l	a5,a5	       ; last LW
	lea	(-32,a6),a6
	move.l	a2,a0
	move.l	d3,d6
	bra.b	30$
	cnop	0,4

10$	lea	(DBPR,sp),a3
	move.w	d5,d7
	move.l	(a3)+,d0
20$	add.l	d0,(a3)+
	dbf	d7,20$
	adda.w	a6,a0
30$	bsr.w	AlignChunky
	subq.w	#1,(Lines,sp)
	bgt.b	10$
	lea	(stacked,sp),sp
	rts
	cnop	0,4

blFL	moveq	#16,d2
	cmp.l	d2,d4	  ;head
	bgt.b	5$
	cmp.l	d2,d0	  ;tail
	bgt.b	5$
	suba.l	a5,a5
	add.l	d2,d4
	or.l	(FLWmask,sp),d3
	subq.l	#2,a5
	swap	d3
	move.l	d3,d6
	bra.b	blF2
	cnop	0,4

5$	add.l	d2,d2
	moveq	#-8,d1
	move.l	d4,d7
	addq.l	#7,d0
	and.l	d1,d0	;tail
	add.l	d0,d7
	cmp.l	d2,d7
	bgt.b	6$
	add.l	d0,d4
	or.l	(FLWmask,sp),d3
	rol.l	d0,d3
	lsr.l	#3,d0
	move.l	d0,a5
	subq.l	#4,a5
	move.l	d3,d6
	bra.b	blF2
	cnop	0,4



6$	lea	(-32,a2,d4.l),a0
	lea	(-64,a6),a6
	move.w	a6,(sp)
	move.l	d3,a6		;llwm
	move.l	(FLWmask,sp),d6 ;flwm
	move.w	#-4,a5
	bra.b	30$
	cnop	0,4

10$	lea	(DBPR,sp),a3
	move.w	d5,d7
	move.l	(a3)+,d0
20$	add.l	d0,(a3)+
	dbf	d7,20$
	adda.w	(sp),a0 ;(HData,sp),a0
	exg	a6,d6
	subq.l	#4,a5
30$	bsr.w	AlignChunky
	exg	a6,d6
	suba.l	a5,a5
	bsr.w	AlignChunky
	subq.w	#1,(Lines,sp)
	bgt.b	10$
	lea	(stacked,sp),sp
	rts
	cnop	0,4


LW1	sub.w	d2,a6
	move.w	d2,(BWidth,sp)
	tst.w	d4		; middle  x1x
	bne.b	LW3
	move.l	a2,a0
	tst.w	d0		; no head, middle 01x
	bne.b	blML		; no head, middle,tail 011
	move.w	a6,(sp)
	bra.b	30$
	cnop	0,4

10$	lea	(DBPR,sp),a3
	move.w	d5,d7
	move.l	(a3)+,d0
20$	add.l	d0,(a3)+
	dbf	d7,20$
	adda.w	(sp),a0
30$	bsr	BlitChunky
	subq.w	#1,(Lines,sp)
	bgt.b	10$
	lea	(stacked,sp),sp
	rts
	cnop	0,4

blML	lea	(-32,a6),a6
	lsr.w	#3,d2
	move.l	d3,(LLWmask,sp)
	move.w	d2,(EndBit,sp)
	move.w	a6,(sp)
	bra.b	30$
	cnop	0,4

10$	lea	(DBPR,sp),a3
	move.w	d5,d7
	move.l	(a3)+,d0
20$	add.l	d0,(a3)+
	dbf	d7,20$
	adda.w	(sp),a0
30$	bsr	BlitChunky
	move.l	(LLWmask,sp),d6
	move.w	(EndBit,sp),a5
	bsr.w	AlignChunky
	subq.w	#1,(Lines,sp)
	bgt.b	10$
	lea	(stacked,sp),sp
	rts
	cnop	0,4



LW3
	lea	(-32,a2,d4.l),a0
	tst.w	d0		; head, middle	  11x
	beq.b	blFM		; head, middle, tail 111
	lea	(-64,a6),a6	; FML, whole line
	lsr.w	#3,d2
	move.l	d3,(LLWmask,sp)
	move.w	d2,(EndBit,sp)
	move.w	a6,(sp)
	bra.b	30$
	cnop	0,4

10$	lea	(DBPR,sp),a3
	move.w	d5,d7
	move.l	(a3)+,d0
20$	add.l	d0,(a3)+
	dbf	d7,20$
	adda.w	(sp),a0
30$	move.w	#-4,a5
	move.l	(FLWmask,sp),d6
	bsr.w	AlignChunky
	bsr.b	BlitChunky
	move.l	(LLWmask,sp),d6
	move.w	(EndBit,sp),a5
	bsr.w	AlignChunky
	subq.w	#1,(Lines,sp)
	bgt.b	10$

crap_c2p
	lea	(stacked,sp),sp
	rts
	cnop	0,4

blFM	lea	(-32,a6),a6
	move.w	a6,(sp)
	bra.b	30$
	cnop	0,4

10$	lea	(DBPR,sp),a3
	move.w	d5,d7
	move.l	(a3)+,d0
20$	add.l	d0,(a3)+
	dbf	d7,20$
	adda.w	(sp),a0
30$	move.w	#-4,a5
	move.l	(FLWmask,sp),d6
	bsr.w	AlignChunky
	bsr.b	BlitChunky
	subq.w	#1,(Lines,sp)
	bgt.b	10$
	lea	(stacked,sp),sp
	rts
	cnop	0,4







	;
	;
	; This is the heart of the patch, the C2P routines
	;
	;
	; all C2P code after this

BlitChunky:
	;  d0-d7/a1-a6 trashed
	; d0 = width/32
	; d5 = depth
	; a0 = chunky src
	;
	;
	move.w	(BWidth+4,sp),a5
	add.l	a0,a5
	move.w	(.jmp,pc,d5.w*2),d0
	move.l	#$33333333,d5
	move.l	#$55555555,d6
	jmp	(.jmp,pc,d0.w)
	cnop	0,4
.jmp
	dc.w	con1p32-.jmp   ; 1 plane
	dc.w	con2p32-.jmp   ; 2 planes
	dc.w	con3p32-.jmp   ; 3 planes
	dc.w	con4p32-.jmp   ; 4 planes
	dc.w	con5p32-.jmp   ; 5 planes
	dc.w	con6p32-.jmp   ; 6 planes
	dc.w	con7p32-.jmp   ; 7 planes
	dc.w	con8p32-.jmp   ; 8 planes
	cnop	0,4


con4p32
	movem.l (DP0point+4,sp),a1-a4
	move.l	#$0F0F0F0F,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3

	and.l	d4,d0
	and.l	d4,d1
	lsl.l	#4,d0
	lsl.l	#4,d1
	and.l	d4,d2
	and.l	d4,d3
	or.l	d2,d0
	or.l	d3,d1

	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsl.l	#4,d2
	lsl.l	#4,d3
	or.l	d4,d2
	or.l	d7,d3

	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3
	bra.b	c4p32_2
	cnop	0,4


c4p32_1
	move.l	d2,a6
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3

	move.l	d4,(a1)+ ;p0

	move.l	#$0F0F0F0F,d4
	and.l	d4,d0
	and.l	d4,d1
	lsl.l	#4,d0
	lsl.l	#4,d1
	and.l	d4,d2
	and.l	d4,d3
	or.l	d2,d0
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3

	and.l  (a0)+,d2
	and.l  (a0)+,d3
	lsl.l	#4,d2
	or.l	d3,d2
	move.l	d4,d3
	and.l  (a0)+,d3
	and.l  (a0)+,d4

	move.l	d7,(a3)+ ;p2

	lsl.l	#4,d3
	or.l	d4,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3

	move.l	a6,(a2)+ ;p1

c4p32_2 move.l	d2,d4
	move.l	d3,d7
	lsr.l	#2,d4
	lsr.l	#2,d7
	eor.l	d0,d4
	eor.l	d1,d7
	and.l	d5,d4
	and.l	d5,d7
	eor.l	d4,d0
	eor.l	d7,d1
	lsl.l	#2,d4
	lsl.l	#2,d7
	eor.l	d4,d2
	eor.l	d7,d3


	move.l	#$00ff00ff,d4


	move.l	d1,d7
	lsr.l	#8,d7
	eor.l	d0,d7
	and.l	d4,d7
	eor.l	d7,d0
	lsl.l	#8,d7
	eor.l	d7,d1


	move.l	d1,d7
	lsr.l	#1,d7
	eor.l	d0,d7
	and.l	d6,d7
	eor.l	d7,d0
	add.l	d7,d7
	eor.l	d1,d7


	move.l	d0,(a4)+ ;p3


	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	d4,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3


	move.l	d3,d4
	lsr.l	#1,d4
	eor.l	d2,d4
	and.l	d6,d4
	eor.l	d4,d2
	add.l	d4,d4
	eor.l	d3,d4


	cmpa.l	a0,a5
	bgt.w	c4p32_1

	move.w	(NLWblit+4,sp),d5
	move.l	d2,(a2)  ;p1
	move.l	d7,(a3)  ;p2
	move.l	d4,(a1)  ;p0

	rts
	cnop	0,4


con3p32

	movem.l (DP0point+4,sp),a1-a3
	move.l	#$0F0F0F0F,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3

	and.l	d4,d0
	and.l	d4,d1
	lsl.l	#4,d0
	lsl.l	#4,d1
	and.l	d4,d2
	and.l	d4,d3
	or.l	d2,d0
	or.l	d3,d1

	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsl.l	#4,d2
	lsl.l	#4,d3
	or.l	d4,d2
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3
	bra.b	c3p32_2
	cnop	0,4


c3p32_1
	movea.l d2,a4
	movea.l d3,a6
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	move.l	#$0F0F0F0F,d4
	and.l	d4,d0
	and.l	d4,d1
	lsl.l	#4,d0
	lsl.l	#4,d1
	and.l	d4,d2
	and.l	d4,d3
	or.l	d2,d0
	or.l	d3,d1

	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7

	move.l	a4,(a2)+ ;p1

	lsl.l	#4,d2
	or.l	d4,d2
	lsl.l	#4,d3
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3

	move.l	a6,(a1)+ ;p0


c3p32_2
	move.l	d2,d7
	move.l	d3,d4
	lsr.l	#2,d7
	lsr.l	#2,d4
	eor.l	d0,d7
	eor.l	d1,d4
	and.l	d5,d7
	and.l	d5,d4
	eor.l	d7,d0
	eor.l	d4,d1
	lsl.l	#2,d7
	lsl.l	#2,d4
	eor.l	d7,d2
	eor.l	d4,d3

	move.l	d1,d7
	move.l	#$00ff00ff,d4
	lsr.l	#8,d7
	eor.l	d0,d7
	and.l	d4,d7
	eor.l	d7,d0
	lsl.l	#8,d7
	eor.l	d1,d7

	and.l	d6,d0
	and.l	d6,d7
	add.l	d0,d0
	or.l	d0,d7

	move.l	d7,(a3)+ ;p2

	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	d4,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	move.l	d3,d1
	lsr.l	#1,d1
	eor.l	d2,d1
	and.l	d6,d1
	eor.l	d1,d2
	add.l	d1,d1
	eor.l	d1,d3


	cmpa.l	a0,a5
	bgt.w	c3p32_1

	move.w	(NLWblit+4,sp),d5
	move.l	d2,(a2)  ;p1
	move.l	d3,(a1)  ;p0

	rts
	cnop	0,4


con2p32

	movem.l (DP0point+4,sp),a1-a2
	move.l	#$03030303,d4
	move.l	d4,a6
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsl.l	#4,d0
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsl.l	#4,d1
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsl.l	#4,d2
	or.l	d4,d2
	lsl.l	#4,d3
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3
	bra.b	c2p32_2
	cnop	0,4


c2p32_1
	movea.l d2,a4
	movea.l d3,a3
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	move.l	a6,d4
	and.l	d4,d0
	and.l	d4,d2
	lsl.l	#4,d0
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsl.l	#4,d1
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7

	move.l	a4,(a2)+      ;p1

	lsl.l	#4,d2
	or.l	d4,d2
	lsl.l	#4,d3
	or.l	d7,d3

	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3

	move.l	a3,(a1)+ ;p0


c2p32_2
	lsl.l	#2,d0
	lsl.l	#2,d1
	or.l	d0,d2
	or.l	d1,d3

	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	#$00ff00ff,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	move.l	d3,d1
	lsr.l	#1,d1
	eor.l	d2,d1
	and.l	d6,d1
	eor.l	d1,d2
	add.l	d1,d1
	eor.l	d1,d3
	cmpa.l	a0,a5
	bgt.b	c2p32_1

	move.w	(NLWblit+4,sp),d5
	move.l	d2,(a2)  ;p1
	move.l	d3,(a1)  ;p0

	rts
	cnop	0,4



con1p32

	move.l	(DP0point+4,sp),a1
	move.l	#$01010101,a3
	move.l	#$00ff00ff,a2
	move.l	a3,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsl.l	#4,d0
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsl.l	#4,d1
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	bra.b	c1p32_2
	cnop	0,4


c1p32_1
	movea.l d3,a4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	move.l	a3,d4
	and.l	d4,d0
	and.l	d4,d2
	lsl.l	#4,d0
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsl.l	#4,d1
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7

	move.l	a4,(a1)+ ;p0


c1p32_2
	lsl.l	#4,d2
	or.l	d4,d2
	lsl.l	#4,d3
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3

	lsl.l	#2,d0
	lsl.l	#2,d1
	or.l	d0,d2
	or.l	d1,d3

	move.l	d3,d1
	move.l	a2,d4
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	d4,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	add.l	d2,d2
	or.l	d2,d3

	cmpa.l	a0,a5
	bgt.b	c1p32_1
	move.w	(NLWblit+4,sp),d5
	move.l	 d3,(a1)


	rts
	cnop	0,4




con8p32
	movem.l (DP4point+4,sp),a1-a4
	move.l	#$F0F0F0F0,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3

	and.l	d4,d2
	and.l	d4,d3
	lsr.l	#4,d2
	lsr.l	#4,d3
	and.l	d4,d0
	and.l	d4,d1
	or.l	d2,d0
	or.l	d3,d1

	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsr.l	#4,d4
	or.l	d4,d2
	lsr.l	#4,d7
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3
	bra.b	c8p32_2
	cnop	0,4


c8p32_1
	movea.l d2,a6
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3

	move.l	d4,(a1)+ ;p4

	move.l	#$F0F0F0F0,d4
	and.l	d4,d2
	and.l	d4,d3
	lsr.l	#4,d2
	lsr.l	#4,d3
	and.l	d4,d0
	and.l	d4,d1
	or.l	d2,d0
	or.l	d3,d1

	move.l	d4,d2
	move.l	d4,d3
	and.l  (a0)+,d2
	and.l  (a0)+,d3
	lsr.l	#4,d3
	or.l	d3,d2
	move.l	d4,d3
	and.l  (a0)+,d3
	and.l  (a0)+,d4

	move.l	d7,(a3)+ ;p6

	lsr.l	#4,d4
	or.l	d4,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3

	move.l	a6,(a2)+ ;p5


c8p32_2 move.l	d2,d7
	move.l	d3,d4
	lsr.l	#2,d7
	lsr.l	#2,d4
	eor.l	d0,d7
	eor.l	d1,d4
	and.l	d5,d7
	and.l	d5,d4
	eor.l	d7,d0
	eor.l	d4,d1
	lsl.l	#2,d7
	lsl.l	#2,d4
	eor.l	d7,d2
	eor.l	d4,d3

	move.l	d1,d7
	move.l	#$00ff00ff,d4
	lsr.l	#8,d7
	eor.l	d0,d7
	and.l	d4,d7
	eor.l	d7,d0
	lsl.l	#8,d7
	eor.l	d7,d1

	move.l	d1,d7
	lsr.l	#1,d7
	eor.l	d0,d7
	and.l	d6,d7
	eor.l	d7,d0

	move.l	d0,(a4)+ ;p7

	add.l	d7,d7
	eor.l	d1,d7

	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	d4,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	move.l	d3,d4
	lsr.l	#1,d4
	eor.l	d2,d4
	and.l	d6,d4
	eor.l	d4,d2
	add.l	d4,d4
	eor.l	d3,d4

	cmpa.l	a0,a5
	bgt.w	c8p32_1

	sub.w	(BWidth+4,sp),a0

	move.l	d7,(a3)  ;p6

	move.l	d4,(a1)  ;p4
	move.l	d2,(a2)  ;p5

	bra.w	con4p32
	cnop	0,4

con7p32
	movem.l (DP4point+4,sp),a1-a3
	move.l	#$F0F0F0F0,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsr.l	#4,d4
	or.l	d4,d2
	lsr.l	#4,d7
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3
	bra.b	c7p32_2
	cnop	0,4


c7p32_1
	movea.l d2,a4
	movea.l d3,a6
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	move.l	#$F0F0F0F0,d4
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7

	move.l	a4,(a2)+ ;p5

	lsr.l	#4,d4
	or.l	d4,d2
	lsr.l	#4,d7
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3

	move.l	a6,(a1)+ ;p4


c7p32_2 move.l	d2,d7
	move.l	d3,d4
	lsr.l	#2,d7
	lsr.l	#2,d4
	eor.l	d0,d7
	eor.l	d1,d4
	and.l	d5,d7
	and.l	d5,d4
	eor.l	d7,d0
	eor.l	d4,d1
	lsl.l	#2,d7
	lsl.l	#2,d4
	eor.l	d7,d2
	eor.l	d4,d3

	move.l	d1,d7
	move.l	#$00ff00ff,d4
	lsr.l	#8,d7
	eor.l	d0,d7
	and.l	d4,d7
	eor.l	d7,d0
	lsl.l	#8,d7
	eor.l	d1,d7

	and.l	d6,d0
	and.l	d6,d7
	add.l	d0,d0
	or.l	d0,d7

	move.l	d7,(a3)+ ;p6

	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	d4,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	move.l	d3,d1
	lsr.l	#1,d1
	eor.l	d2,d1
	and.l	d6,d1
	eor.l	d1,d2
	add.l	d1,d1
	eor.l	d1,d3
	cmpa.l	a0,a5
	bgt.w	c7p32_1

	sub.w	(BWidth+4,sp),a0
	move.l	d3,(a1)  ;p4
	move.l	d2,(a2)  ;p5

	bra.w	con4p32
	cnop	0,4

con6p32
	movem.l (DP4point+4,sp),a1-a2
	move.l	#$30303030,d4
	move.l	d4,a6
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsr.l	#4,d4
	or.l	d4,d2
	lsr.l	#4,d7
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3
	bra.b	c6p32_2
	cnop	0,4


c6p32_1
	movea.l d2,a3
	movea.l d3,a4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	move.l	a6,d4
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7

	move.l	a3,(a2)+ ;p5

	lsr.l	#4,d4
	or.l	d4,d2
	lsr.l	#4,d7
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3

	move.l	a4,(a1)+ ;p4


c6p32_2
	lsl.l	#2,d0
	lsl.l	#2,d1
	or.l	d0,d2
	or.l	d1,d3

	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	#$00ff00ff,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	move.l	d3,d1
	lsr.l	#1,d1
	eor.l	d2,d1
	and.l	d6,d1
	eor.l	d1,d2
	add.l	d1,d1
	eor.l	d1,d3
	cmpa.l	a0,a5
	bgt.b	c6p32_1

	sub.w	(BWidth+4,sp),a0
	move.l	d3,(a1)  ;p4
	move.l	d2,(a2)  ;p5

	bra.w	con4p32
	cnop	0,4


con5p32
	move.l	(DP4point+4,sp),a1
	move.l	#$10101010,a3
	move.l	#$00ff00ff,a4
	move.l	a3,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	bra.b	c5p32_2
	cnop	0,4


c5p32_1
	movea.l d3,a2
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3

	move.l	a3,d4
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7

	move.l	a2,(a1)+ ;p4



c5p32_2

	lsr.l	#4,d4
	or.l	d4,d2
	lsr.l	#4,d7
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3

	lsl.l	#2,d0
	lsl.l	#2,d1
	or.l	d0,d2
	or.l	d1,d3

	move.l	d3,d1
	move.l	a4,d4
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	d4,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	add.l	d2,d2
	or.l	d2,d3

	cmpa.l	a0,a5
	bgt.b	c5p32_1

	sub.w	(BWidth+4,sp),a0
	move.l	d3,(a1)  ;p4

	bra.w	con4p32
	cnop	0,4




AlignChunky:
	; d0-d7/a1-a5 trashed
	; a6 not used
	; d5 = depth
	; a0 = chunky src
	;
	;
	move.w	(.jmp,pc,d5.w*2),d0
	jmp	(.jmp,pc,d0.w)
	cnop	0,4

.jmp
	dc.w	a1p-.jmp   ; 1 plane
	dc.w	a2p-.jmp   ; 2 planes
	dc.w	a3p-.jmp   ; 3 planes
	dc.w	a4p-.jmp   ; 4 planes
	dc.w	a5p-.jmp   ; 5 planes
	dc.w	a6p-.jmp   ; 6 planes
	dc.w	a7p-.jmp   ; 7 planes
	dc.w	a8p-.jmp   ; 8 planes
	cnop	0,4


a4p

	movem.l (DP0point+4,sp),a1-a4
	add.l	a5,a1
	add.l	a5,a2
	add.l	a5,a3
	add.l	a5,a4

	move.l	#$0F0F0F0F,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsl.l	#4,d0
	and.l	d4,d1
	or.l	d2,d0
	lsl.l	#4,d1
	and.l	d4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsl.l	#4,d2
	lsl.l	#4,d3
	or.l	d4,d2
	or.l	d7,d3

	move.l	d2,d5
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d5,d2
	move.w	d4,d3

	move.l	#$33333333,d5

	move.l	d2,d7
	move.l	d3,d4
	lsr.l	#2,d7
	lsr.l	#2,d4
	eor.l	d0,d7
	eor.l	d1,d4
	and.l	d5,d7
	and.l	d5,d4
	eor.l	d7,d0
	eor.l	d4,d1
	lsl.l	#2,d7
	lsl.l	#2,d4
	eor.l	d7,d2
	eor.l	d4,d3

	move.l	#$00ff00ff,d4

	move.l	d1,d7
	move.l	d3,d5
	lsr.l	#8,d7
	lsr.l	#8,d5
	eor.l	d0,d7
	eor.l	d2,d5
	and.l	d4,d7
	and.l	d4,d5
	eor.l	d7,d0
	eor.l	d5,d2
	lsl.l	#8,d7
	lsl.l	#8,d5
	eor.l	d7,d1
	eor.l	d5,d3

	move.l	#$55555555,d4

	move.l	d1,d7
	move.l	d3,d5
	lsr.l	#1,d7
	lsr.l	#1,d5
	eor.l	d0,d7
	eor.l	d2,d5
	and.l	d4,d7
	and.l	d4,d5
	eor.l	d7,d0
	eor.l	d5,d2
	add.l	d7,d7
	add.l	d5,d5
	eor.l	d1,d7
	eor.l	d5,d3

	and.l	d6,d0
	and.l	d6,d2
	and.l	d6,d3
	and.l	d6,d7

	not.l	d6


	move.l	d6,d1
	and.l	(a4),d1
	or.l	d1,d0
	move.l	d0,(a4)  ;p3

	move.l	d6,d4
	and.l	(a2),d4
	or.l	d4,d2
	move.l	d2,(a2)  ;p1

	move.l	d6,d5
	and.l	(a1),d5
	or.l	d5,d3
	move.l	d6,d1
	move.l	d3,(a1)  ;p0

	and.l	(a3),d1
	or.l	d1,d7
	move.w	(NLWblit+4,sp),d5
	move.l	d7,(a3)  ;p2
	not.l	d6

	rts
	cnop	0,4



a3p	movem.l (DP0point+4,sp),a1-a3
	add.l	a5,a1
	add.l	a5,a2
	add.l	a5,a3

	move.l	#$0F0F0F0F,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsl.l	#4,d0
	and.l	d4,d1
	or.l	d2,d0
	lsl.l	#4,d1
	and.l	d4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsl.l	#4,d2
	lsl.l	#4,d3
	or.l	d4,d2
	or.l	d7,d3

	move.l	d2,d4
	move.l	d3,d7
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d4,d2
	move.w	d7,d3


	move.l	#$33333333,d5
	move.l	d2,d7
	move.l	d3,d4
	lsr.l	#2,d7
	lsr.l	#2,d4
	eor.l	d0,d7
	eor.l	d1,d4
	and.l	d5,d7
	and.l	d5,d4
	eor.l	d7,d0
	eor.l	d4,d1
	lsl.l	#2,d7
	lsl.l	#2,d4
	eor.l	d7,d2
	eor.l	d4,d3

	move.l	#$00ff00ff,d4
	move.l	d1,d7
	move.l	d3,d5
	lsr.l	#8,d7
	lsr.l	#8,d5
	eor.l	d0,d7
	eor.l	d2,d5
	and.l	d4,d7
	and.l	d4,d5
	eor.l	d7,d0
	eor.l	d5,d2
	lsl.l	#8,d7
	lsl.l	#8,d5
	eor.l	d1,d7
	eor.l	d5,d3

	move.l	#$55555555,d4
	and.l	d4,d0
	and.l	d4,d7
	add.l	d0,d0
	or.l	d0,d7

	move.l	d3,d5
	lsr.l	#1,d5
	eor.l	d2,d5
	and.l	d4,d5
	eor.l	d5,d2
	add.l	d5,d5
	eor.l	d5,d3

	and.l	d6,d7
	and.l	d6,d2
	move.l	d6,d1
	and.l	d6,d3

	not.l	d1

	move.l	d1,d4
	and.l	(a2),d4
	or.l	d4,d2
	move.l	d2,(a2)  ;p1

	move.l	d1,d5
	and.l	(a1),d5
	or.l	d5,d3
	move.l	d3,(a1)  ;p0

	and.l	(a3),d1
	or.l	d1,d7
	move.w	(NLWblit+4,sp),d5
	move.l	d7,(a3)  ;p2


	rts
	cnop	0,4




a2p	movem.l (DP0point+4,sp),a1-a2
	add.l	a5,a1
	add.l	a5,a2
	move.l	#$03030303,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsl.l	#4,d0
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsl.l	#4,d1
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsl.l	#4,d2
	or.l	d4,d2
	lsl.l	#4,d3
	or.l	d7,d3

	move.l	d2,d4
	move.l	d3,d7
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d4,d2
	move.w	d7,d3

	lsl.l	#2,d0
	lsl.l	#2,d1
	or.l	d0,d2
	or.l	d1,d3

	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	#$00ff00ff,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	move.l	d3,d1
	lsr.l	#1,d1
	eor.l	d2,d1
	and.l	#$55555555,d1
	eor.l	d1,d2
	add.l	d1,d1
	eor.l	d1,d3

	and.l	d6,d2
	move.l	d6,d1
	and.l	d6,d3

	not.l	d1

	move.l	d1,d4
	and.l	(a2),d4
	or.l	d4,d2
	move.l	d2,(a2)  ;p1

	and.l	(a1),d1
	or.l	d1,d3
	move.w	(NLWblit+4,sp),d5
	move.l	d3,(a1)  ;p0

	rts
	cnop	0,4




a1p	move.l	(DP0point+4,sp),a1
	add.l	a5,a1
	move.l	#$01010101,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsl.l	#4,d0
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsl.l	#4,d1
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsl.l	#4,d2
	or.l	d4,d2
	lsl.l	#4,d3
	or.l	d7,d3

	move.l	d2,d4
	move.l	d3,d7
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d4,d2
	move.w	d7,d3

	lsl.l	#2,d0
	lsl.l	#2,d1
	or.l	d0,d2
	or.l	d1,d3

	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	#$00ff00ff,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	add.l	d2,d2
	or.l	d2,d3

	move.l	d6,d1
	and.l	d6,d3

	not.l	d1

	and.l	(a1),d1

	or.l	d1,d3
	move.w	(NLWblit+4,sp),d5
	move.l	d3,(a1)  ;p0

	rts
	cnop	0,4




a8p
	movem.l (DP4point+4,sp),a1-a4
	move.l	#$F0F0F0F0,d4
	add.l	a5,a1
	add.l	a5,a2
	add.l	a5,a3
	add.l	a5,a4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsr.l	#4,d4
	or.l	d4,d2
	lsr.l	#4,d7
	or.l	d7,d3

	move.l	d2,d4
	move.l	d3,d7
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d4,d2
	move.w	d7,d3

	move.l	#$33333333,d5

	move.l	d2,d7
	move.l	d3,d4
	lsr.l	#2,d7
	lsr.l	#2,d4
	eor.l	d0,d7
	eor.l	d1,d4
	and.l	d5,d7
	and.l	d5,d4
	eor.l	d7,d0
	eor.l	d4,d1
	lsl.l	#2,d7
	lsl.l	#2,d4
	eor.l	d7,d2
	eor.l	d4,d3

	move.l	d1,d7
	move.l	#$00ff00ff,d4
	move.l	d3,d5
	lsr.l	#8,d7
	lsr.l	#8,d5
	eor.l	d0,d7
	eor.l	d2,d5
	and.l	d4,d7
	and.l	d4,d5
	eor.l	d7,d0
	eor.l	d5,d2
	lsl.l	#8,d7
	lsl.l	#8,d5
	eor.l	d7,d1
	eor.l	d5,d3

	move.l	#$55555555,d4

	move.l	d1,d7
	move.l	d3,d5
	lsr.l	#1,d7
	lsr.l	#1,d5
	eor.l	d0,d7
	eor.l	d2,d5
	and.l	d4,d7
	and.l	d4,d5
	eor.l	d7,d0
	eor.l	d5,d2
	add.l	d7,d7
	add.l	d5,d5
	eor.l	d1,d7
	eor.l	d5,d3


	and.l	d6,d0
	and.l	d6,d2
	and.l	d6,d3
	and.l	d6,d7

	move.l	d6,d5
	not.l	d5
	move.l	d5,d1
	and.l	(a4),d1
	or.l	d1,d0
	move.l	d0,(a4)  ;p3




	move.l	d5,d4
	and.l	(a2),d4
	or.l	d4,d2
	move.l	d2,(a2)  ;p1

	move.l	d5,d1
	and.l	(a3),d1
	or.l	d1,d7
	move.l	d7,(a3)  ;p2

	and.l	(a1),d5
	or.l	d5,d3
	move.l	d3,(a1)  ;p0


	lea	(-32,a0),a0
	bra.w	a4p
	cnop	0,4

a7p
	movem.l (DP4point+4,sp),a1-a3
	add.l	a5,a1
	add.l	a5,a2
	add.l	a5,a3
	move.l	#$F0F0F0F0,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsr.l	#4,d4
	or.l	d4,d2
	lsr.l	#4,d7
	or.l	d7,d3

	move.l	d2,d4
	move.l	d3,d7
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d4,d2
	move.w	d7,d3

	move.l	#$33333333,d5
	move.l	d2,d7
	move.l	d3,d4
	lsr.l	#2,d7
	lsr.l	#2,d4
	eor.l	d0,d7
	eor.l	d1,d4
	and.l	d5,d7
	and.l	d5,d4
	eor.l	d7,d0
	eor.l	d4,d1
	lsl.l	#2,d7
	lsl.l	#2,d4
	eor.l	d7,d2
	eor.l	d4,d3

	move.l	#$00ff00ff,d4
	move.l	d1,d7
	move.l	d3,d5
	lsr.l	#8,d7
	lsr.l	#8,d5
	eor.l	d0,d7
	eor.l	d2,d5
	and.l	d4,d7
	and.l	d4,d5
	eor.l	d7,d0
	eor.l	d5,d2
	lsl.l	#8,d7
	lsl.l	#8,d5
	eor.l	d1,d7
	eor.l	d5,d3

	move.l	#$55555555,d4
	and.l	d4,d0
	and.l	d4,d7
	add.l	d0,d0
	or.l	d0,d7

	move.l	d3,d5
	lsr.l	#1,d5
	eor.l	d2,d5
	and.l	d4,d5
	eor.l	d5,d2
	add.l	d5,d5
	eor.l	d5,d3

	and.l	d6,d2
	and.l	d6,d7
	and.l	d6,d3

	move.l	d6,d5
	not.l	d5
	move.l	d5,d4
	and.l	(a2),d4
	or.l	d4,d2
	move.l	d2,(a2)  ;p1

	move.l	d5,d1
	and.l	(a3),d1
	or.l	d1,d7
	move.l	d7,(a3)  ;p2

	and.l	(a1),d5
	or.l	d5,d3
	move.l	d3,(a1)  ;p0


	lea	(-32,a0),a0
	bra.w	a4p
	cnop	0,4

a6p
	movem.l (DP4point+4,sp),a1-a2
	move.l	#$30303030,d4
	add.l	a5,a1
	add.l	a5,a2
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsr.l	#4,d4
	or.l	d4,d2
	lsr.l	#4,d7
	or.l	d7,d3

	move.l	d2,d4
	move.l	d3,d7
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d4,d2
	move.w	d7,d3

	lsl.l	#2,d0
	lsl.l	#2,d1
	or.l	d0,d2
	or.l	d1,d3

	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	#$00ff00ff,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	move.l	d3,d1
	lsr.l	#1,d1
	eor.l	d2,d1
	and.l	#$55555555,d1
	eor.l	d1,d2
	add.l	d1,d1
	eor.l	d1,d3

	move.l	d6,d5
	and.l	d6,d2
	and.l	d6,d3
	not.l	d5
	move.l	d5,d1

	and.l	(a2),d1
	or.l	d1,d2
	move.l	d2,(a2)  ;p5

	and.l	(a1),d5
	or.l	d5,d3
	move.l	d3,(a1)  ;p4


	lea	(-32,a0),a0
	bra.w	a4p
	cnop	0,4


a5p
	move.l	(DP4point+4,sp),a1
	add.l	a5,a1
	move.l	#$10101010,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsr.l	#4,d4
	or.l	d4,d2
	lsr.l	#4,d7
	or.l	d7,d3

	move.l	d2,d4
	move.l	d3,d7
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d4,d2
	move.w	d7,d3

	lsl.l	#2,d0
	lsl.l	#2,d1
	or.l	d0,d2
	or.l	d1,d3

	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	#$00ff00ff,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	add.l	d2,d2
	or.l	d2,d3


	move.l	d6,d5
	not.l	d5
	and.l	(a1),d5


	and.l	d6,d3

	or.l	d5,d3

	move.l	d3,(a1)  ;p4

	lea	(-32,a0),a0
	bra.w	a4p
	cnop	0,4



	; d6 = rp_mask
	; d7 = #planes mask
Masked_rp
	move.l	d3,(LLWmask,sp) ; save d1  ending mask
	move.w	d5,d1	       ; We do this little bit here to see if we can't use
10$	cmp.b	d7,d6	       ; the regular routines since the mask could be for
	beq.w	NotMasked      ; an even spread of planes and the regular c2p
	lsr.b	#1,d7	       ; is almost 2x faster.
	dbf	d5,10$	       ;
	move.w	d1,d5	       ;
	move.w	d2,(BWidth,sp)

	move.w	d0,(EndBit,sp)
	cmp.w	d4,d2	       ; blitwidth-(amount in 1st LW)
	bgt.b	mmultiLW       ; convertion larger then then 1 lw, blit goes beyond 1st lw
	tst	d0	       ; end of convertion bit
	beq.b	moneLW	       ; ends at 32bit bound
	and.l	d3,(FLWmask,sp); blit smaller then 1st lw


moneLW	clr.w	(NLWblit,sp)
	bra.b	smrp
	cnop	0,4

mmultiLW
	sub.w	d4,d2
	sub.w	d0,d2		; (width-start)-end=remaining conversion size
	lsr.w	#5,d2		; size/32
	move.w	d2,(NLWblit,sp) ; # words to convert


smrp	move.w	a6,(sp) ;(HData,sp)
	moveq	#64,d0
	suba.l	d0,sp
	move.b	d6,d1
	lea	(mDP0point,sp),a0
     ;	 lea	 (SP0point,sp),a1
	movea.l sp,a1
	lea	(SP0,sp),a3
	moveq	#8,d2		; need to do this for all 8 possible planes
	bra.b	30$
	cnop	0,4

10$	move.l	a3,(a1)
	lsr.b	#1,d1	    ; we clear masked out plane pointers here
	bcs.b	20$
	clr.l	(a0)
	clr.l	(a1)
20$	addq.l	#4,a0
	addq.l	#4,a1
	addq.l	#4,a3
30$	dbf	d2,10$


Firstmask
	tst.l	d4
	beq.b	Alignedmask
	lea	(-32,a2,d4.l),a0
	move.l	(mFLWmask,sp),d0
	moveq	#-4,d2
	bsr.b	Masked

Alignedmask
	moveq	#0,d0
	move.w	(mNLWblit,sp),d0
	beq.b	Lastmask
	lea	(a2,d4.l),a0
	lea	(mDP0point,sp),a3
	bsr.w	mask8p

Lastmask
	move.w	(mEndBit,sp),d0
	beq.b	Endmask
	movea.l a2,a0
	suba.w	d0,a0
	adda.w	(mBWidth,sp),a0
	moveq	#0,d2
	move.w	(mNLWblit,sp),d2
	lsl.l	#2,d2
	move.l	(mLLWmask,sp),d0
	bsr.b	Masked

Endmask
	lea	(mDP0point,sp),a3
	move.l	d5,d7
	move.l	(mDBPR,sp),d0

10$	move.l	(a3),d1
	beq.b	15$
	add.l	d0,d1
	move.l	d1,(a3)
15$	addq.l	#4,a3
20$	dbf	d7,10$
	adda.w	(mHData,sp),a2
	subq.w	 #1,(mLines,sp)
	bgt.b	Firstmask
	lea	(mstacked,sp),sp
	rts
	cnop	0,4



Masked
	movem.l d4/a2,-(sp)
	move.l	d0,d4		     ; data mask
	lea	(SP0point+12,sp),a3  ; bm_Planes for tmp bitmap on stack
	moveq	#1,d0		     ; only 1 word to do
	bsr.b	mask8p
	move.l	d4,d7		     ; converted data mask
	not.l	d4		     ; dst bitmap mask
	move.l	d5,d6
	lea	(mDP0point+12,sp),a3  ; bm_Planes for dst bitmap  stacked a4
	lea	(SP0+12,sp),a1		 ; load next tmp plane ptr

10$	move.l	(a1)+,d0	   ; we use	   temp read
	move.l	(a3)+,d1	   ; load next dst plane ptr
	beq.b	20$		   ; masked out, so skip
	move.l	d1,a0
	adda.l	d2,a0		   ; move fwd/back in bitmap to align correctly
	move.l	(a0),d1 	   ; 32bit reads   dest read
	and.l	d7,d0		   ; mask out unneeded converted data
	and.l	d4,d1		   ; mask out just where we'll write
	or.l	d0,d1		   ; stick'em together
	move.l	d1,(a0) 	   ; 32bit writes
20$	dbf	d6,10$
	movem.l (sp)+,d4/a2
	rts
	cnop	0,4



mask8p
	movem.l d2/d4/d5/a2,-(sp)
	move.l	a0,a5
	lsl.l	#5,d0
	add.l	d0,a5

	cmpi.b	#4,d5
	blt.w	mask4p	    ; if it's =<4 planes skip the upper 4

	movem.l a0/a3,-(sp)
	movem.l (16,a3),a1-a4
	move.l	#$33333333,d5
	move.l	#$55555555,d6
	move.l	#$F0F0F0F0,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsr.l	#4,d4
	or.l	d4,d2
	lsr.l	#4,d7
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3
	bra.b	m8p_2
	cnop	0,4


m8p_1	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3

	tst.l	a2
	beq.b	10$
	move.l	d4,(a2)+  ;p5

10$	move.l	#$F0F0F0F0,d4
	and.l	d4,d0
	and.l	d4,d2
	lsr.l	#4,d2
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsr.l	#4,d3
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	and.l  (a0)+,d2
	and.l  (a0)+,d3
	lsr.l	#4,d3
	or.l	d3,d2
	move.l	d4,d3
	and.l  (a0)+,d3
	and.l  (a0)+,d4

	tst.l	a3
	beq.b	20$
	move.l	d7,(a3)+  ;p6

20$	lsr.l	#4,d4
	or.l	d4,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3

	tst.l	a1
	beq.b	m8p_2
	move.l	a6,(a1)+  ;p4


m8p_2
	move.l	d2,d4
	move.l	d3,d7
	lsr.l	#2,d4
	lsr.l	#2,d7
	eor.l	d0,d4
	eor.l	d1,d7
	and.l	d5,d4
	and.l	d5,d7
	eor.l	d4,d0
	eor.l	d7,d1
	lsl.l	#2,d4
	lsl.l	#2,d7
	eor.l	d4,d2
	eor.l	d7,d3

	move.l	#$00ff00ff,d4
	move.l	d1,d7
	lsr.l	#8,d7
	eor.l	d0,d7
	and.l	d4,d7
	eor.l	d7,d0
	lsl.l	#8,d7
	eor.l	d7,d1

	move.l	d1,d7
	lsr.l	#1,d7
	eor.l	d0,d7
	and.l	d6,d7
	eor.l	d7,d0

	tst.l	a4
	beq.b	10$
	move.l	d0,(a4)+  ;p7

10$	add.l	d7,d7
	eor.l	d1,d7

	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	d4,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	move.l	d3,d1
	lsr.l	#1,d1
	eor.l	d2,d1
	and.l	d6,d1
	eor.l	d1,d2
	movea.l d2,d4
	add.l	d1,d1
	eor.l	d1,d3
	movea.l d3,a6
	cmpa.l	a0,a5
	bgt	m8p_1

	tst.l	a3
	beq.b	20$
	move.l	d7,(a3)   ;p6

20$	tst.l	a1
	beq.b	30$
	move.l	a6,(a1)   ;p4

30$	tst.l	a2
	beq.b	40$
	move.l	d4,(a2)   ;p5

40$	movem.l (sp)+,a0/a3

mask4p
	movem.l (a3),a1-a4
	;
	; lower 4 planes
	;
	move.l	#$33333333,d5
	move.l	#$55555555,d6
	move.l	#$0F0F0F0F,d4
	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3
	and.l	d4,d0
	and.l	d4,d2
	lsl.l	#4,d0
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsl.l	#4,d1
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	move.l	d4,d7
	and.l  (a0)+,d2
	and.l  (a0)+,d4
	and.l  (a0)+,d3
	and.l  (a0)+,d7
	lsl.l	#4,d2
	or.l	d4,d2
	lsl.l	#4,d3
	or.l	d7,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3
	bra.b	m4p_2
	cnop	0,4


m4p_1	move.l	(a0)+,d0
	move.l	(a0)+,d2
	move.l	(a0)+,d1
	move.l	(a0)+,d3

	tst.l	a2
	beq.b	10$
	move.l	d4,(a2)+      ;p1

10$	move.l	#$0F0F0F0F,d4
	and.l	d4,d0
	and.l	d4,d2
	lsl.l	#4,d0
	or.l	d2,d0
	and.l	d4,d1
	and.l	d4,d3
	lsl.l	#4,d1
	or.l	d3,d1
	move.l	d4,d2
	move.l	d4,d3
	and.l  (a0)+,d2
	and.l  (a0)+,d3
	lsl.l	#4,d2
	or.l	d3,d2
	move.l	d4,d3
	and.l  (a0)+,d3
	and.l  (a0)+,d4

	tst.l	a3
	beq.b	20$
	move.l	d7,(a3)+  ;p2

20$	lsl.l	#4,d3
	or.l	d4,d3
	move.l	d2,d7
	move.l	d3,d4
	move.w	d0,d2
	move.w	d1,d3
	swap	d2
	swap	d3
	move.w	d2,d0
	move.w	d3,d1
	move.w	d7,d2
	move.w	d4,d3

	tst.l	a1
	beq.b	m4p_2
	move.l	a6,(a1)+ ;p0


m4p_2
	move.l	d2,d4
	move.l	d3,d7
	lsr.l	#2,d4
	lsr.l	#2,d7
	eor.l	d0,d4
	eor.l	d1,d7
	and.l	d5,d4
	and.l	d5,d7
	eor.l	d4,d0
	eor.l	d7,d1
	lsl.l	#2,d4
	lsl.l	#2,d7
	eor.l	d4,d2
	eor.l	d7,d3

	move.l	#$00ff00ff,d4
	move.l	d1,d7
	lsr.l	#8,d7
	eor.l	d0,d7
	and.l	d4,d7
	eor.l	d7,d0
	lsl.l	#8,d7
	eor.l	d7,d1

	move.l	d1,d7
	lsr.l	#1,d7
	eor.l	d0,d7
	and.l	d6,d7
	eor.l	d7,d0

	tst.l	a4
	beq.b	10$
	move.l	d0,(a4)+  ;p3

10$	add.l	d7,d7
	eor.l	d1,d7

	move.l	d3,d1
	lsr.l	#8,d1
	eor.l	d2,d1
	and.l	d4,d1
	eor.l	d1,d2
	lsl.l	#8,d1
	eor.l	d1,d3

	move.l	d3,d1
	lsr.l	#1,d1
	eor.l	d2,d1
	and.l	d6,d1
	eor.l	d1,d2
	movea.l d2,d4
	add.l	d1,d1
	eor.l	d1,d3
	movea.l d3,a6
	cmpa.l	a0,a5
	bgt	m4p_1

	tst.l	a3
	beq.b	20$
	move.l	d7,(a3)  ;p2

20$	tst.l	a1
	beq.b	30$
	move.l	a6,(a1)  ;p0

30$	tst.l	a2
	beq.b	40$
	move.l	d4,(a2)  ;p1

40$	movem.l (sp)+,d2/d4/d5/a2
	rts
	cnop	0,4



endpatch




