; UAEGFX V1.0 - reassembled an reworked by Holger.Hippenstiel.org - lynxx@uni.de
; Don't ask me how it works - i don't know !
; If you think it's slower than V0.9
; Make your Benchmark again with the old version, and another time
; with the new version ... You see, the speed differs,
; regardless which test you use.
; if everything fails, optimize it yourself - or use the old version.

	INCDIR	I:
	INCLUDE "exec/exec.i"
	INCLUDE "exec/exec_lib.i"
	INCLUDE "exec/execbase.i"
	INCLUDE "exec/initializers.i"
	INCLUDE "exec/resident.i"
	INCLUDE "exec/nodes.i"
	INCLUDE "exec/types.i"
	INCLUDE "exec/libraries.i"
	INCLUDE "exec/lists.i"
	INCLUDE "libraries/expansion_lib.i"

picasso_FindCard		=16;
picasso_FillRect		=17;
picasso_SetSwitch		=18;
picasso_SetColorArray		=19;
picasso_SetDAC			=20;
picasso_SetGC			=21;
picasso_SetPanning		=22;
picasso_CalculateBytesPerRow	=23;
picasso_BlitPlanar2Chunky	=24;
picasso_BlitRect		=25;
picasso_SetDisplay		=26;
picasso_BlitTemplate		=27;
picasso_BlitRectNoMaskComplete	=28;
picasso_InitCard		=29;
picasso_BlitPattern		=30;
picasso_InvertRect		=31;
picasso_BlitPlanar2Direct	=32;
picasso_WaitVerticalSync	=34;
allocated_gfxmem		=35;	;UNUSED
picasso_SetSprite		=36;	;UNUSED
picasso_SetSpritePosition	=37;	;UNUSED
picasso_SetSpriteImage		=38;	;UNUSED
picasso_SetSpriteColor		=39;	;UNUSED
picasso_DrawLine		=40;	;UNUSED

CALL_U:			MACRO
			movem.l	d2-d7/a2-a6,-(sp)
			pea	(\1).w
			ENDM

CALL_C:			MACRO
			movem.l	d0-d7/a0-a6,-(sp)
			move.l	UaeBase(pc),a5
			pea	(\1).w
			jsr	(a5)

			addq	#4,sp
			subq.l	#1,d0
			movem.l	(sp)+,d0-d7/a0-a6
			ENDM

SYS:			MACRO
			jsr	_LVO\1(a6)

			ENDM

			SECTION	UAEGfx,CODE


_LVOprv_TaggedOpenLibrary=-810
;1=Gfx,2=Layer,3=Int,4=Dos,5=Icon,6=Exp,7=Util,8=Keymap,9=Gadt,10=wb

ProgStart:		moveq	#-1,d0
			rts

ResidentTag:		dc.w	RTC_MATCHWORD
			dc.l	ResidentTag
			dc.l	ResidentEnd
			dc.b	2^RTB_AUTOINIT		;RT_FLAGS
			dc.b	1			;Version
			dc.b	NT_LIBRARY		;Type
			dc.b	0			;Pri
			dc.l	DriverName
			dc.l	DriverDesc
			dc.l	LibTab
Driver:			dc.b	'uaegfx',0
DriverName:		dc.b	'uaegfx.card',0
			dc.b	"$VER: "
DriverDesc:		dc.b	'uaegfx V1.0 © Holger.Hippenstiel.org (10.12.2001) - lynxx@uni.de',$D,$A,0
			even
	
LibTab:			dc.l	$34			;Postive Size of Lib
			dc.l	Functions
			dc.l	InitTab
			dc.l	InitFunc

Functions:		dc.l	_OpenLib
			dc.l	_CloseLib
			dc.l	_ExpungeLib
			dc.l	Null
			dc.l	FindCard
			dc.l	AddCard
			dc.l	-1

InitTab:		INITBYTE	LN_TYPE,NT_LIBRARY
			INITBYTE	LN_PRI,206
			INITLONG	LN_NAME,DriverName
			INITBYTE	LIB_FLAGS,LIBF_SUMUSED!LIBF_CHANGED
			INITWORD	LIB_VERSION,1
			INITWORD	LIB_REVISION,0
			INITLONG	LIB_IDSTRING,DriverDesc
			INITLONG	$30,Driver			;???
			dc.l	0

InitFunc:		move.l	d0,a1
			move.l	a0,($2C,a1)
			tst.l	d0
			rts

_OpenLib:		addq	#1,LIB_OPENCNT(a6)
			bclr	#LIBB_DELEXP,($22,a6)
			move.l	a6,d0
Return:			rts

_CloseLib:		moveq	#0,d0
			subq	#1,LIB_OPENCNT(a6)
			bne.b	Return

			btst	#LIBB_DELEXP,($22,a6)
			beq.b	Return

_ExpungeLib:		movem.l	d2/a5/a6,-(sp)
			move.l	a6,a5
			tst	LIB_OPENCNT(a5)
			beq.b	.ExpungeIt

			bset	#LIBB_DELEXP,($22,a5)
			moveq	#0,d0
			bra.b	.ExpungeSoon

.ExpungeIt:		move.l	($2C,a5),d2
			move.l	a5,a1
			move.l	4.w,a6
			SYS	Remove

			moveq	#0,d0
			move.l	a5,a1
			move	LIB_NEGSIZE(a5),d0
			sub.l	d0,a1
			add	LIB_POSSIZE(a5),d0
			SYS	FreeMem

			move.l	d2,d0
.ExpungeSoon:
			movem.l	(sp)+,d2/a5/a6
			rts

Null:			moveq	#0,d0
NoUAE:			rts

FindCard:		movem.l	a2/a3/a6,-(sp)
			move.l	a0,a2
			moveq	#6,d0			;Exp
			move.l	4.w,a6
			SYS	prv_TaggedOpenLibrary

			move.l	d0,a6			;Exp_Base
			sub.l	a0,a0
			move.l	#2011,d0
			moveq	#96,d1
			SYS	FindConfigDev

			move.l	a2,a0
			tst.l	d0
			movem.l	(sp)+,a2/a3/a6
			beq.b	NoUAE

			CALL_U	picasso_FindCard
			bra.w	DoUAE

AddCard:		movem.l	d6/d7/a2/a5/a6,-(sp)
			sub.l	a1,a1
			move.l	a0,a2
			move.l	a1,($B2,a2)
			move.l	a1,($AE,a2)
			addq	#1,a1
			move.l	a1,($AA,a2)
			lea	(Driver,pc),a1
			move.l	a1,($10,a2)
			or.l	#$8002,($BA,a2)
			bsr.w	InitCard
			moveq	#-1,d0
			moveq	#1,d6
			lea	$fe(a2),a6
			lea	$ca(a2),a5
			moveq	#4,d7
.l0			move.l	d6,(a6)+
			move.l	d0,(a5)+
			dbf	d7,.l0
			lea	(Nothing,pc),a5
			lea	$11a(a2),a6
			lea	(SetSwitch,pc),a1
			move.l	a1,(a6)+
			lea	(SetColorArray,pc),a1
			move.l	a1,(a6)+
			lea	(SetDAC,pc),a1
			move.l	a1,(a6)+
			lea	(SetGC,pc),a1
			move.l	a1,(a6)+
			lea	(SetPanning,pc),a1
			move.l	a1,(a6)+
			lea	(CalculateBytesPerRow,pc),a1
			move.l	a1,(a6)+
			lea	(UseSoftware,pc),a1
			move.l	a1,(a6)+
			lea	(NotSupported,pc),a1
			move.l	a1,(a6)+
			lea	(SetDisplay,pc),a1
			move.l	a1,(a6)+
			lea	(HardwareDirect,pc),a1
			move.l	a1,(a6)+
			lea	(Secret,pc),a1
			move.l	a1,(a6)+

			moveq	#4,d7
.l1			move.l	a5,(a6)+			;Not supported
			dbf	d7,.l1

			lea	(WaitVerticalSync,pc),a1
			move.l	a1,(a6)
			addq	#8,a6
			move.l	a5,(a6)				;Not supported
			addq	#8,a6
			move.l	4(a6),(a6)			;Always use Software
			addq	#8,a6
			move.l	4(a6),(a6)			;Always use Software
			addq	#8,a6
			lea	(BlitPlanar2Chunky,pc),a1
			move.l	a1,(a6)
			addq	#8,a6
			lea	(FillRect,pc),a1
			move.l	a1,(a6)
			addq	#8,a6
			lea	(InvertRect,pc),a1
			move.l	a1,(a6)
			addq	#8,a6
			lea	(BlitRect,pc),a1
			move.l	a1,(a6)
			addq	#8,a6
			lea	(BlitTemplate,pc),a1
			move.l	a1,(a6)
			addq	#8,a6
			lea	(BlitPattern,pc),a1
			move.l	a1,(a6)
			addq	#8,a6
			move.l	4(a6),(a6)			;Always use Software
			addq	#8,a6
			lea	(BlitRectNoMaskComplete,pc),a1
			move.l	a1,(a6)
			addq	#8,a6
			lea	(BlitPlanar2Direct,pc),a1
			move.l	a1,(a6)
			movem.l	(sp)+,d6/d7/a2/a5/a6
			rts

InitCard:		CALL_U	picasso_InitCard
DoUAE:			move.l	UaeBase(pc),a5
			jsr	(a5)

			addq	#4,sp
			movem.l	(sp)+,d2-d7/a2-a6
			rts

SetSwitch:		CALL_U	picasso_SetSwitch
			bra.b	DoUAE

SetColorArray:		CALL_U	picasso_SetColorArray
			bra.b	DoUAE

SetDAC:			CALL_U	picasso_SetDAC
			bra.b	DoUAE

SetGC:			move.l	a1,($22E,a0)
			move	d0,($23C,a0)
			CALL_U	picasso_SetGC
			bra.b	DoUAE

SetPanning:		move	d1,($236,a0)
			move	d2,($238,a0)
			move.l	a1,d4
			sub.l	(4,a0),d4
			CALL_U	picasso_SetPanning
			bra.b	DoUAE

CalculateBytesPerRow:	CALL_U	picasso_CalculateBytesPerRow
			bra.b	DoUAE

SetDisplay:		CALL_U	picasso_SetDisplay
			bra.b	DoUAE

WaitVerticalSync:	CALL_U	picasso_WaitVerticalSync
			bra.b	DoUAE

FillRect:		CALL_C	picasso_FillRect
			beq.b	.NoEmulation

			move.l	$182(a0),-(sp)		;Hardware can't do it - emulate it
.NoEmulation:		rts

BlitPlanar2Chunky:	CALL_C	picasso_BlitPlanar2Chunky
			beq.b	.NoEmulation

			move.l	$17a(a0),-(sp)		;Hardware can't do it - emulate it
.NoEmulation:		rts

BlitRect:		CALL_C	picasso_BlitRect
			beq.b	.NoEmulation

			move.l	$192(a0),-(sp)		;Hardware can't do it - emulate it
.NoEmulation:		rts

BlitTemplate:		CALL_C	picasso_BlitTemplate
			beq.b	.NoEmulation

			move.l	$19a(a0),-(sp)		;Hardware can't do it - emulate it
.NoEmulation:		rts

BlitRectNoMaskComplete:	CALL_C	picasso_BlitRectNoMaskComplete
			beq.b	.NoEmulation

			move.l	$1b2(a0),-(sp)		;Hardware can't do it - emulate it
.NoEmulation:		rts

BlitPattern:		CALL_C	picasso_BlitPattern
			beq.b	.NoEmulation

			move.l	$1a2(a0),-(sp)		;Hardware can't do it - emulate it
.NoEmulation:		rts

InvertRect:		CALL_C	picasso_InvertRect
			beq.b	.NoEmulation

			move.l	$18a(a0),-(sp)		;Hardware can't do it - emulate it
.NoEmulation:		rts

BlitPlanar2Direct:	CALL_C	picasso_BlitPlanar2Direct
			beq.b	.NoEmulation

			move.l	$1ba(a0),-(sp)		;Hardware can't do it - emulate it
.NoEmulation:		rts

NotSupported:		moveq	#-2,d0
			rts

UseSoftware:		move.l	a1,d0
			rts

HardwareDirect:		move	#$620E,(42,a1)
			move.l	d0,(44,a1)
			moveq	#0,d0
			rts

Secret:			move.l	#$5F958BC,d0
Nothing:		rts


UaeBase:		dc.l	$F0FF60
ResidentEnd:
