*------------------------------------------------------------------
* math.asm: ZC library interface to Motorola fast floating point
* library supplied by Commodore.
* Maintenance Notes:
* 06Jun89 - Created by Jal
* 27May90 - Modifications by TETISOFT
* 16Jan91 - TetiSoft commented out saving of A6 with ';'
*           since HCC does not use it as local stack pointer.
*           Changed Alert Code, since we have Intuition open,
*           we can use text instead of numbers.
*
*
*------------------------------------------------------------------

;AG_OpenLib	equ	$00030000
;AO_MathLib	equ	$00008005

bias	SET	-30

libref	MACRO
_LVO\1	EQU	bias
bias	SET	bias-6
	ENDM

*------------------------------------------------------------------
* Note: these *Must* be in the same order as the mathbas_lib.fd
* file.
*------------------------------------------------------------------

	libref	SPFix
	libref	SPFlt
	libref	SPCmp
	libref	SPTst
	libref	SPAbs
	libref	SPNeg
	libref	SPAdd
	libref	SPSub
	libref	SPMul
	libref	SPDiv
	libref	SPFloor	; needs release 1.2+
	libref	SPCeil	; needs release 1.2+

;	XREF	_Alert
	XREF	_DisplayAlert

	XREF	_exit
	XREF	_OpenLibrary
	XREF	_CloseLibrary
	XREF	__MathBaseClose

	XDEF	_MathBase

*------------------------------------------------------------------

	DATA

MATHName	DC.B	'mathffp.library',0

	EVEN
AlertString:
	DC.W	120	; x-Offset
	DC.B	20	; y-Offset
	DC.B	'Could not open mathffp.library.',0
	DC.B	1	; another Text follows...
	DC.B	0,120	; x
	DC.B	40	; y
	DC.B	'Press a mouse button to exit.',0,0

	BSS

MathBase	DS.L	1
_MathBase	DS.L	1

	CODE

*
*	Called at exit if we opened the MathBase library.
*

MathBaseClose:
	move.l	MathBase,-(sp)
	jsr	_CloseLibrary
	addq	#4,sp
	rts

*
*	Make sure the MathBase is loaded correctly
*

.flopen:
	movem.l	D0-D1/A0-A1,-(sp)	; save temporary registers
	pea	0			; any version will do....
	pea	MATHName		; Load the name of library
	jsr	_OpenLibrary		; Open the library
	addq	#8,sp
	move.l	D0,_MathBase		; Save the return code
	bne.s	1$			; If non-zero then OK

;	pea	0			; Parameter for Alert.
;	pea	AG_OpenLib!AO_MathLib	; Signal an error.
;	jsr	_Alert
;	addq	#8,sp

	pea	60		; Height
	pea	AlertString
	pea	0		; RECOVERY_ALERT
	jsr	_DisplayAlert
	lea	12(sp),sp

	pea	100			; push return code
	jsr	_exit			; and exit
1$:
	move.l	D0,MathBase		; A private copy
	move.l	#MathBaseClose,__MathBaseClose
	movem.l	(sp)+,D0-D1/A0-A1	; restore temporaries
	rts

*-------------------------------------------------------------------
* Do a one or two parameter MathBase function.
*-------------------------------------------------------------------
.fldo2:
	move.l	12(sp),D1
.fldo1:
	move.l	8(sp),D0
	tst.l	_MathBase
	bne.s	1$
	jsr	.flopen
1$:
;	movem.l	A6,-(sp)
	move.l	_MathBase,A6	

; TetiSoft We changed the stack frame...
;	add.l	4(sp),A6
	add.l	(sp),A6

	jsr	(A6)
;	movem.l	(sp)+,A6
	addq	#4,sp		; unstack the function offset.
	rts

*********************************************************************
*
*	multiply
*
	XDEF	fpmul
	XDEF	_fpmul
fpmul:
_fpmul:
	pea	_LVOSPMul
	jmp	.fldo2

*********************************************************************
*
*	divide
*
	XDEF	fpdiv
	XDEF	_fpdiv
fpdiv:
_fpdiv:
	pea	_LVOSPDiv
	jmp	.fldo2

*********************************************************************
*
*	add
*
	XDEF	fpadd
	XDEF	_fpadd
fpadd:
_fpadd:
	pea	_LVOSPAdd
	jmp	.fldo2

*********************************************************************
*
*	subtract
*
	XDEF	fpsub
	XDEF	_fpsub
fpsub:
_fpsub:
	pea	_LVOSPSub
	jmp	.fldo2

*********************************************************************
*
*	negate
*
	XDEF	fpneg
	XDEF	_fpneg
fpneg:
_fpneg:
	pea	_LVOSPNeg
	jmp	.fldo1

*********************************************************************
*
*	compare
*
	XDEF	fpcmp
	XDEF	_fpcmp
fpcmp:
_fpcmp:
	pea	_LVOSPCmp
	jmp	.fldo2

*********************************************************************
*
*	convert long to float
*
	XDEF	_fpltof
_fpltof:
	pea	_LVOSPFlt
	jmp	.fldo1

*********************************************************************
*
*	Convert float to long
*
	XDEF	_fpftol
_fpftol:
	pea	_LVOSPFix
	jmp	.fldo1

*********************************************************************
*
*	Implement the fabs function.:
*
	XDEF	_fabs
_fabs:
	pea	_LVOSPAbs
	jmp	.fldo1

*********************************************************************
*
*	Implement the floor function.:
*
	XDEF	_floor
_floor:
	pea	_LVOSPFloor
	jmp	.fldo1

*********************************************************************
*
*	Implement the ceil function.:
*
	XDEF	_ceil
_ceil:
	pea	_LVOSPCeil
	jmp	.fldo1

	END
