;
; cpuclr.asm
; ----------
;
; replaces BltClear() with a CPU based function for speed on 68030-systems
; compatible to V36's new "fill" feature 
;
; (C) 1991 Oliver Wagner
;

call	MACRO
	xref	_LVO\1
	jsr	_LVO\1(a6)
	ENDM


	section	"init",code

	xref	_LVOBltClear

;
; entry function
;

entry:
	move.l	4,a6
	lea	gfxname(pc),a1
	call	OldOpenLibrary
	move.l	d0,a2
	lea	dosname(pc),a1
	call	OldOpenLibrary
	move.l	d0,a3
	move.l	d0,a6
	call	Output
	move.l	d0,d7
	move.l	d0,d1
	lea	head(pc),a0
	move.l	a0,d2
	moveq	#37,d3
	call	Write

	move.l	_LVOBltClear+2(a2),a5
	cmp.l	#'CPUC',-8(a5)
	bne.s	install

	move.l	4,a6
	move.l	-4(a5),d0
	move.w	#_LVOBltClear,a0
	move.l	a2,a1
	call	SetFunction
	lea	-12(a5),a0
	move.l	a0,d1
	ror.l	#2,d1
	move.l	a3,a6
	call	UnLoadSeg
	lea	rem(pc),a0
	bra.s	exit

install:
	lea	entry-4(pc),a5
	move.l	(a5),d0
	addq.l	#3,d0
	add.l	d0,d0
	add.l	d0,d0
	move.l	d0,a4	; pointer to new func
	clr.l	(a5)	; kill segment pointer
	move.l	a2,a1
	move.w	#_LVOBltClear,a0
	move.l	4,a6
	call	SetFunction
	move.l	d0,-4(a4)

	lea	ins(pc),a0
exit:
	move.l	d7,d1
	move.l	a3,a6
	move.l	a0,d2
	moveq	#10,d3
	call	Write
	moveq	#0,d0
	rts

gfxname:
	dc.b	"graphics.library",0
dosname:
	dc.b	"dos.library",0
head:
	dc.b	$9b,"1mCPUCLR",$9b,"0m (C) 1991 Oliver Wagner: "
ins:
	dc.b	"installed",10
rem:
	dc.b	"removed  ",10

	section	"patch",data

id:	dc.l	'CPUC'
oldfunc:
	dc.l	'(-:]'
;
; this is the new bltclear() routine entry
;
; a0 = block
; d0 = size
; d1 = flags

newclear:
	move.l	d2,a0
	btst	#1,d1
	beq.s	bytesize
; must calc size
	move.l	d0,d2
	and.l	#$ffff,d0
	clr.w	d2
	swap	d2
	mulu	d2,d0
bytesize:
	moveq	#0,d2
	btst	#2,d1
	beq.s	dataok
	move.l	d1,d2
	swap	d2
dataok:
	btst	#1,d0
	subq.l	#2,d0
loop1:
	move.w	d2,(a1,d0.l)
	subq.l	#2,d0
	bpl.s	loop1
	move.l	a0,d2
	rts
;
; more efficent clear
; if size%4 == 0
;

doloop2:
	subq.l	#4,d0
	move.w	d2,d1
	swap	d2
	move.w	d1,d2
	swap	d2
loop2:
	move.l	d2,(a1,d0.l)
	subq.l	#4,d0
	bpl.s	loop2
	move.l	a0,d2
	rts

	END
