* MiniCalcu
* v. 1.0
* par Christophe LARATTE
* pour le Petit Amiga Illustré

Forbid			=	-132
FindTask		=	-294

	move.l	4.w,a6			cette partie permet
	move.l	#0,a1			simplement le
	jsr	FindTask(a6)		chargement depuis le WB
	move.l	d0,a4			(c'est quand même mieux)
	tst.l	$AC(a4)
	bne.s	FromCLI
	lea	$5C(a4),a0
	jsr	WaitPort(a6)
	lea	$5C(a4),a0
	jsr	GetMsg(a6)
	move.l	d0,-(sp)
	bsr	FromCLI
	move.l	4.w,a6
	jsr	Forbid(a6)
	move.l	(sp)+,a1
	jsr	ReplyMsg(a6)
	moveq	#0,d0
	rts

FromCLI	lea	CRTitle(pc),a0
	lea	CRPTitle(pc),a1
	move.l	a0,(a1)
	lea	CRParameters(pc),a0  Appel de la fonction CalculatorRequester
	move.l	#0,a1		     il suffit de passer l'adresse des
	bsr	CalculatorRequester  paramètres et celle de l'écran ou 0.
	moveq	#0,d0
	rts

******************** routines d'affichage de la fenêtre ****************


* Exec.library
AllocMem		=	-198
CloseLibrary		=	-414
FreeMem			=	-210
GetMsg			=	-372
OpenLibrary		=	-552
ReplyMsg		=	-378
WaitPort		=	-384



* Intuition.library
AddGList		=	-438
CloseWindow		=	-72
OpenWindow		=	-204
DrawBorder		=	-108
GetScreenData		=	-426
RefreshGList		=	-432
RemoveGList		=	-444
RefreshWindowFrame	=	-456

Class			=	$14

wd_RPort		=	$32
wd_UserPort		=	$56


* Graphics.library
RectFill		=	-306
PolyDraw		=	-336
Draw			=	-246
Move			=	-240
SetAPen			=	-342


EntrieLength		=	255

MemToAlloc		=	269+EntrieLength
CRWindowBase		=	0
IntuitionBase		=	4
CRUserPort		=	8
CRRastPort		=	12
CRWindow		=	16
Gadget			=	64
StringInfo		=	108
Buffer			=	144
GadgetTitle		=	145+EntrieLength
GfxBase			=	261+EntrieLength
Result			=	265+EntrieLength

; IN :
; a0 : *Parameters
; a1 : *screen or 0 for WB

; OUT :
; d0,d1,a0,a1 altered

CalculatorRequester
	movem.l	d2-d7/a2-a5,-(sp)
	move.l	a6,-(sp)
	moveq	#-1,d7
	move.l	a0,a5
	move.l	a1,a3
	move.l	4.w,a6
	move.l	#MemToAlloc,d0
	move.l	#$10001,d1
	jsr	AllocMem(a6)
	tst.l	d0
	beq	NoMem
	move.l	d0,a4
	move.l	4.w,a6
	lea	IntuitionName(pc),a1
	moveq	#33,d0
	jsr	OpenLibrary(a6)
	move.l	d0,IntuitionBase(a4)
	beq	NoIntuition
	lea	GfxName(pc),a1
	moveq	#33,d0
	jsr	OpenLibrary(a6)
	move.l	d0,GfxBase(a4)
	beq	NoGfx
	move.l	IntuitionBase(a4),a6
	cmp.l	#0,a3
	bne.s	.NoWBSc
	move.w	#1,CRWindow+46(a4)
	link	a3,#-16
	move.l	sp,a0
	moveq	#16,d0
	moveq	#1,d1
	jsr	GetScreenData(a6)
	move.l	sp,a0
	move.w	12(a0),d2
	move.w	14(a0),d3
	unlk	a3
	tst.l	d0
	beq	NoCRWindow
	bra.s	.WBSc
.NoWBSc	move.w	#$F,CRWindow+46(a4)
	move.l	a3,CRWindow+30(a4)
	move.w	12(a3),d2
	move.w	14(a3),d3
.WBSc	move.w	(a5),d0
	move.w	d0,CRWindow+4(a4)
	move.w	2(a5),d1
	cmp.w	#-1,d1
	bne.s	.NoAutX
	sub.w	d0,d2
	lsr.w	d2
	move.w	d2,d1
.NoAutX	move.w	d1,CRWindow(a4)
	moveq	#0,d0
	move.l	6(a5),d1
	btst	#0,d1
	beq.s	.NBit
	addq.w	#1,d0
.NBit	btst	#1,d1
	beq.s	.NBit1
	addq.w	#1,d0
.NBit1	btst	#2,d1
	beq.s	.NBit2
	addq.w	#1,d0
.NBit2	mulu	#12,d0
	add.w	#40,d0
	move.w	d0,CRWindow+6(a4)
	move.w	4(a5),d1
	cmp.w	#-1,d1
	bne.s	.NoAutY
	sub.w	d0,d3
	lsr.w	d3
	move.w	d3,d1
.NoAutY	move.w	d1,CRWindow+2(a4)
	move.b	#1,CRWindow+9(a4)
	move.l	#$200|$40|$800|1,CRWindow+10(a4)
	move.l	#4|8|$1000|$10000,CRWindow+14(a4)
	move.w	8(a5),d0
	btst	#10,d0
	beq.s	.NoZoom
	bset	#5,CRWindow+14(a4)
.NoZoom	btst	#9,d0
	beq.s	.NoDrag
	bset	#1,CRWindow+17(a4)
.NoDrag	btst	#4,d0
	beq.s	.NoSize
	bset	#0,CRWindow+17(a4)
	move.w	#6*8,CRWindow+38(a4)
	btst	#0,d0
	beq.s	.NS1
	move.w	#14*8,CRWindow+38(a4)
.NS1	btst	#2,d0
	beq.s	.NS2
	move.w	#15*8,CRWindow+38(a4)
.NS2	btst	#1,d0
	beq.s	.NS3
	move.w	#38*8,CRWindow+38(a4)
.NS3	move.w	#-1,CRWindow+42(a4)
.NoSize	btst	#8,d0
	beq.s	.Move
	move.w	CRWindow+4(a4),CRWindow+38(a4)
.Move	move.l	10(a5),CRWindow+26(a4)
	lea	Gadget(a4),a0
	move.l	a0,CRWindow+18(a4)
	move.w	(a5),d1
	moveq	#0,d5
	bsr	InitGadgets
	cmp.l	#-1,d1
	beq	NoCRWindow
	lea	CRWindow(a4),a0
	jsr	OpenWindow(a6)
	move.l	d0,CRWindowBase(a4)
	beq	NoCRWindow
	move.l	d0,a0
	move.l	wd_RPort(a0),CRRastPort(a4)
	move.l	wd_UserPort(a0),CRUserPort(a4)
	bsr	DrawBoBo



Attente
	move.l	4.w,a6
	move.l	CRUserPort(a4),a3
	move.l	a3,a0
	jsr	WaitPort(a6)
	move.l	a3,a0
	jsr	GetMsg(a6)
	move.l	d0,a1
	move.l	Class(a1),d2
	jsr	ReplyMsg(a6)
	cmp.l	#$40,d2
	beq	TraiteCalcul
	cmp.l	#1,d2
	beq	RedrawGadgets
	cmp.l	#$200,d2
	beq	End
	bra.s	Attente

RedrawGadgets
	move.l	IntuitionBase(a4),a6
	move.l	CRWindowBase(a4),a0
	lea	Gadget(a4),a1
	moveq	#-1,d0
	jsr	RemoveGList(a6)
	move.l	CRWindowBase(a4),a0
	jsr	RefreshWindowFrame(a6)
	moveq	#0,d0
	bsr	SetPen
	move.l	CRRastPort(a4),a1
	moveq	#4,d0
	moveq	#11,d1
	move.l	CRWindowBase(a4),a0
	move.w	8(a0),d2
	sub.w	#19,d2
	move.w	10(a0),d3 
	subq.w	#3,d3
	jsr	RectFill(a6)
	move.l	CRWindowBase(a4),a0
	move.w	8(a0),d1
	moveq	#0,d5
	btst	#0,8(a5)
	beq.s	.Gna
	moveq	#-1,d5
.Gna	bsr	InitGadgets
	move.l	IntuitionBase(a4),a6
	move.l	CRWindowBase(a4),a0
	lea	Gadget(a4),a1
	move.l	#0,a2
	moveq	#0,d0
	moveq	#-1,d1
	jsr	AddGList(a6)
	bsr	RefreshMyGadget
	bsr	DrawBoBo
	bra	Attente

TraiteCalcul
	lea	Buffer(a4),a0
	bsr	Preprocessor
	move.l	d0,d3
	move.l	d0,Result(a4)
	move.l	Gadget+26(a4),a1
	btst	#0,9(a5)
	beq.s	.NoHexa
	move.l	12(a1),a0
	move.b	#'$',(a0)+
	addq.l	#8,a0
	moveq	#7,d2
.MakeNumber
	move.b	d0,d1
	and.b	#%1111,d1
	add.b	#'0',d1
	cmp.b	#'9',d1
	ble.s	.NumberMade
	add.b	#'A'-'9'-1,d1
.NumberMade
	move.b	d1,-(a0)
	lsr.l	#4,d0
	dbf	d2,.MakeNumber
	move.l	16(a1),a1
.NoHexa	btst	#1,9(a5)
	beq.s	.NoBin
	move.l	d3,d0
	move.l	12(a1),a0
	move.b	#'%',(a0)+
	moveq	#31,d2
.MakeNumber2
	btst	d2,d0
	beq.s	.Zero
	move.b	#'1',(a0)+
	bra.s	.Cont0
.Zero	move.b	#'0',(a0)+
.Cont0	dbf	d2,.MakeNumber2
	move.l	16(a1),a1
.NoBin	btst	#2,9(a5)
	beq.s	.NoDec
	move.l	d3,d1
	move.l	12(a1),a0
	move.b	#' ',(a0)+
	btst	#5,9(a5)
	beq.s	.NoNeg
	tst.l	d1
	bpl.s	.NoNeg
	neg.l	d1
	move.b	#'-',-1(a0)
.NoNeg	add.l	#10,a0
	moveq	#9,d2
.MakeNumber3
	move.l	d1,d0
	move.w	#0,d0
	swap	d0
	beq.s	.NoNeed
	divu	#10,d0
.NoNeed	move.w	d0,-(sp)
	move.w	d1,d0
	tst.l	d0
	beq.s	.NoNee2
	divu	#10,d0
.NoNee2	swap	d0
	add.b	#'0',d0
	move.b	d0,-(a0)
	move.w	(sp)+,d0
	swap	d0
	move.l	d0,d1
	dbf	d2,.MakeNumber3
.NoDec	bsr	RefreshMyGadget
	bra	Attente

RefreshMyGadget
	move.l	IntuitionBase(a4),a6
	lea	Gadget(a4),a0
	move.l	CRWindowBase(a4),a1
	moveq	#-1,d0
	jsr	RefreshGList(a6)
	rts

End	moveq	#0,d7
	move.l	Result(a4),d6
	move.l	IntuitionBase(a4),a6
	move.l	CRWindowBase(a4),a0
	jsr	CloseWindow(a6)
NoCRWindow
	move.l	4.w,a6
	move.l	GfxBase(a4),a1
	jsr	CloseLibrary(a6)
NoGfx	move.l	4.w,a6
	move.l	IntuitionBase(a4),a1
	jsr	CloseLibrary(a6)
NoIntuition
	move.l	4.w,a6
	move.l	a4,a1
	move.l	#MemToAlloc,d0
	jsr	FreeMem(a6)
NoMem	tst.l	d7
	beq.s	.OK
	moveq	#0,d0
	moveq	#-1,d1
	bra.s	.End
.OK	move.l	d6,d0
	moveq	#0,d1
.End	move.l	(sp)+,a6
	movem.l	(sp)+,d2-d7/a2-a5
	rts


**********		datas pour la CR fenetre		**********

IntuitionName	dc.b "intuition.library",0
GfxName		dc.b "graphics.library",0
	even

DrawBoBo
	move.w	Gadget+8(a4),d0
	move.w	Gadget+4(a4),d1
	btst	#3,9(a5)
	beq.s	.TheOther
	bsr	DrawMyBorder
	bra.s	.End
.TheOther
	bsr	DrawMySecondBorder
.End	rts

InitGadgets
	move.b	9(a5),d6
	move.w	d1,d7
	move.w	d1,d0				; IN     d1.w : Width
	sub.w	#50,d1				; d5.l : flag
 	bpl.s	.OK
	moveq	#-1,d1
	bra	.End
.OK	and.w	#%1111111111111000,d1
	sub.w	d1,d0
	lsr.w	d0
	move.w	d0,Gadget+4(a4)
	move.w	d1,Gadget+8(a4)
	move.w	#20,Gadget+6(a4)
	move.w	#8,Gadget+10(a4)
	move.l	#$2010004,Gadget+14(a4)
	lea	StringInfo(a4),a0
	move.l	a0,Gadget+34(a4)
	lea	Buffer(a4),a0
	move.l	a0,StringInfo(a4)
	move.w	#EntrieLength,StringInfo+10(a4)
	lea	GadgetTitle(a4),a2
	lea	Gadget+26(a4),a3
	moveq	#$10,d3
	tst.l	d5
	bne	.End
	btst	#0,d6
	beq.s	.NextLine
	move.l	#$01000100,(a2)
	move.w	d7,d0
	sub.w	#9*8,d0
	bpl.s	.WidthOK
	moveq	#-1,d1
	bra	.End
.WidthOK
	btst	#7,d6
	bne.s	.AtLef
	btst	#6,d6
	bne.s	.AtRig
	lsr.w	d0
	bra.s	.CWOK
.AtLef	moveq	#20,d0
	bra.s	.CWOK
.AtRig	sub.w	#22,d0
.CWOK	sub.w	Gadget+4(a4),d0
	move.w	d0,4(a2)
	move.w	d3,6(a2)
	lea	20(a2),a0
	move.l	a0,12(a2)
	move.l	a2,(a3)
	lea	16(a2),a3
	lea	30(a2),a2
	add.w	#12,d3
.NextLine	
	btst	#1,d6
	beq.s	.NextLine2
	move.l	#$01000100,(a2)
	move.w	d7,d0
	sub.w	#33*8,d0
	bpl.s	.WidthOK2
	moveq	#-1,d1
	bra	.End
.WidthOK2
	btst	#7,d6
	bne.s	.AtLef2
	btst	#6,d6
	bne.s	.AtRig2
	lsr.w	d0
	bra.s	.CWOK2
.AtLef2	moveq	#20,d0
	bra.s	.CWOK2
.AtRig2	sub.w	#22,d0
.CWOK2	sub.w	Gadget+4(a4),d0
	move.w	d0,4(a2)
	move.w	d3,6(a2)
	lea	20(a2),a0
	move.l	a0,12(a2)
	move.l	a2,(a3)
	lea	16(a2),a3
	lea	54(a2),a2
	add.w	#12,d3
.NextLine2
	btst	#2,d6
	beq.s	.NextLine3
	move.l	#$01000100,(a2)
	move.w	d7,d0
	sub.w	#12*8,d0
	bpl.s	.WidthOK3
	moveq	#-1,d1
	bra.s	.End
.WidthOK3
	btst	#7,d6
	bne.s	.AtLef3
	btst	#6,d6
	bne.s	.AtRig3
	lsr.w	d0
	bra.s	.CWOK3
.AtLef3	moveq	#12,d0
	bra.s	.CWOK3
.AtRig3	sub.w	#14,d0
.CWOK3	sub.w	Gadget+4(a4),d0
	move.w	d0,4(a2)
	move.w	d3,6(a2)
	lea	20(a2),a0
	move.l	a0,12(a2)
	move.l	a2,(a3)
.NextLine3
	moveq	#0,d1
.End	rts


DrawMyBorder
	move.w	d0,d2				IN:
	move.w	d1,d3				d0.w : width
	moveq	#29,d4				d1.w : LeftEdge
	moveq	#18,d5
	moveq	#30,d6
	moveq	#17,d7
	move.b	14(a5),d0
	bsr	SetPen
	move.l	CRRastPort(a4),a1
	move.w	d3,d0
	subq.w	#5,d0
	move.w	#18,d1
	jsr	Move(a6)	
	link	a3,#-36
	move.l	sp,a0
	move.w	d5,18(a0)
	move.w	d7,10(a0)
	move.w	d7,14(a0)
	move.w	#19,26(a0)
	move.w	d4,2(a0)
	move.w	d4,22(a0)
	move.w	d4,30(a0)
	move.w	d4,34(a0)
	move.w	d6,6(a0)
	move.w	d3,d0
	subq.w	#6,d0
	move.w	d0,4(a0)
	move.w	d0,8(a0)
	addq.w	#1,d0
	move.w	d0,(a0)
	addq.w	#2,d0
	move.w	d0,32(a0)
	addq.w	#5,d0
	add.w	d2,d0
	move.w	d0,24(a0)
	move.w	d0,28(a0)
	addq.w	#1,d0
	move.w	d0,16(a0)
	move.w	d0,20(a0)
	addq.w	#1,d0
	move.w	d0,12(a0)
	move.l	CRRastPort(a4),a1
	moveq	#9,d0
	jsr	PolyDraw(a6)
	move.b	15(a5),d0
	bsr	SetPen
	move.l	CRRastPort(a4),a1
	move.w	d3,d0
	add.w	d2,d0
	addq.w	#4,d0
	move.w	#29,d1
	jsr	Move(a6)	
	move.l	sp,a0
	move.w	d7,6(a0)
	move.w	d5,2(a0)
	move.w	d5,22(a0)
	move.w	d5,30(a0)
	move.w	d5,34(a0)
	move.w	#28,26(a0)
	move.w	d4,18(a0)
	move.w	d6,10(a0)
	move.w	d6,14(a0)
	subq.w	#5,d3
	move.w	d3,12(a0)
	addq.w	#1,d3
	move.w	d3,16(a0)
	move.w	d3,20(a0)
	addq.w	#1,d3
	move.w	d3,24(a0)
	move.w	d3,28(a0)
	add.w	d2,d3
	addq.w	#5,d3
	move.w	d3,32(a0)
	addq.w	#2,d3
	move.w	d3,(a0)
	addq.w	#1,d3
	move.w	d3,4(a0)
	move.w	d3,8(a0)
	move.l	CRRastPort(a4),a1
	moveq	#9,d0
	jsr	PolyDraw(a6)
	unlk	a3
.No	rts

SetPen
	move.l	GfxBase(a4),a6
	move.l	CRRastPort(a4),a1
	jsr	SetAPen(a6)
	rts

DrawMySecondBorder
	move.w	d0,d2				See DrawMyBorder
	move.w	d1,d3
	moveq	#18,d4
	moveq	#29,d5
	move.b	14(a5),d0
	bsr	SetPen
	move.l	CRRastPort(a4),a1
	move.w	d3,d0
	subq.w	#3,d0
	move.w	d4,d1
	jsr	Move(a6)
	link	a3,#-16
	move.l	sp,a0
	move.w	d4,2(a0)
	move.w	d4,14(a0)
	move.w	d5,6(a0)
	move.w	d5,10(a0)
	subq.w	#3,d3
	move.w	d3,8(a0)
	move.w	d3,12(a0)
	add.w	d2,d3
	addq.w	#5,d3
	move.w	d3,(a0)
	move.w	d3,4(a0)
	move.l	CRRastPort(a4),a1
	moveq	#4,d0
	jsr	PolyDraw(a6)
	unlk	a3
	rts


*************************************

* Preprocessor
* par Christophe LARATTE
* routine devant recevoir une chaine en a0
* et renvoyant la valeur en d0 si d1 contient 0

;AllocMem	=	-198		indique simplement que Preprocessor
;FreeMem	=	-210		utilise ces routines

Operations	=	0
Numbers		=	250
Max		=	1024

;	IN :
; a0 : *text

;	OUT :
; d1 : 0 or -1 or -2
; if 0 then d0 : integer
; else if -1 error
;      if -2 error while allocmem

; a0/a1 altered

mull	MACRO
	IFNC	'2','\<NARG>'
	FAIL	missing parameter !
	MEXIT
	ENDC
	move.l	\2,-(sp)
	mulu	\1,\2
	move.l	#0,-(sp)
	move.l	\2,(sp)
	move.w	4(sp),\2
	mulu	\1,\2
	add.w	\2,(sp)
	swap	\1
	move.w	6(sp),\2
	mulu	\1,\2
	swap	\2
	move.w	#0,\2
	swap	\1
	add.l	(sp)+,\2
	addq.l	#4,sp
	ENDM

Preprocessor
	movem.l	d2-d7/a2-a6,-(sp)
	move.l	a0,a2
	move.l	4.w,a6
	move.l	#Max,d0
	move.l	#$10001,d1
	jsr	AllocMem(a6)
	tst.l	d0
	bne.s	.Cont
	moveq	#0,d0
	moveq	#-2,d1
	bra	EndMem
.Cont	move.l	d0,a6
	move.l	a2,a0
	lea	Operations(a6),a4
	lea	Numbers(a6),a5
	move.b	(a0),d0
	cmp.b	#'-',d0
	beq.s	.Areu
	cmp.b	#'+',d0
	bne.s	Analyse
.Areu	move.l	#0,(a5)+
Analyse	move.b	(a0)+,d0
	beq	EndAnalyse
	cmp.b	#'0',d0
	blt.s	NoDec1
	cmp.b	#'9',d0
	bgt.s	NoDec1
	lea	-1(a0),a0
	bra	ProcessDecimal
NoDec1	cmp.b	#'$',d0
	beq	ProcessHexadecimal
	cmp.b	#'%',d0
	beq	ProcessBinary
	cmp.b	#'@',d0
	beq	ProcessOctal
	cmp.b	#"'",d0
	beq	ProcessString
	cmp.b	#'"',d0
	beq	ProcessString
	cmp.b	#'(',d0
	bne.s	.Cont0
	move.b	#'(',(a4)+
	bra.s	Analyse
.Cont0	cmp.b	#')',d0
	bne.s	.Cont1
	move.b	#')',(a4)+
	bra.s	Analyse
.Cont1	cmp.b	#'+',d0
	bne.s	.Cont2
	cmp.b	#'(',-2(a0)
	bne.s	.Cont11
	move.l	#0,(a5)+
.Cont11	move.b	#'+',(a4)+
	bra.s	Analyse
.Cont2	cmp.b	#'/',d0
	bne.s	.Cont3
	move.b	#'/',(a4)+
	bra.s	Analyse
.Cont3	cmp.b	#'-',d0
	bne.s	.Cont4
	cmp.b	#'(',-2(a0)
	beq.s	.Cont32
   cmp.b #'[',-2(a0)
   beq.s .Cont32
   cmp.b #'{',-2(a0)
   bne.s .Cont33
.Cont32
	move.l	#0,(a5)+
.Cont33	move.b	#'-',(a4)+
	bra	Analyse
.Cont4	cmp.b	#'*',d0
	bne.s	.Cont5
	move.b	#'*',(a4)+
	bra	Analyse
.Cont5	cmp.b	#'[',d0
	bne.s	.Cont6
	move.b	#'(',(a4)+
	bra	Analyse
.Cont6	cmp.b	#']',d0
	bne.s	.Cont7
	move.b	#')',(a4)+
	bra	Analyse
.Cont7	cmp.b	#'{',d0
	bne.s	.Cont8
	move.b	#'(',(a4)+
	bra	Analyse
.Cont8	cmp.b	#'}',d0
	bne.s	.Cont9
	move.b	#')',(a4)+
	bra	Analyse
.Cont9	bra	Error
Cont	move.l	d1,(a5)+
	bra	Analyse
EndAnalyse
	lea	Operations(a6),a2
	move.l	a2,a5
.Boucle	move.b	(a2)+,d0
	beq.s	.End
	cmp.b	#'/',d0
	beq.s	.Boucl0
	cmp.b	#'*',d0
	bne.s	.Boucle
.Boucl0	move.l	a2,a4
	addq.l	#1,a2
	lea	1(a4),a3
	lea	-1(a4),a4
	cmp.l	a4,a5
	beq.s	.Next
	moveq	#0,d2
.Bibi	move.b	-(a4),d0
	cmp.b	#'(',d0
	bne.s	.SNext
	addq.l	#1,d2
.SNext	cmp.b	#')',d0
	bne.s	.PNext
	subq.l	#1,d2
.PNext	tst.l	d2
	bpl.s	.PPNext
	cmp.l	a4,a5
	bne.s	.Bibi
.PPNext	lea	1(a4),a4
.Next	move.b	#'(',d0
.NNext	move.b	(a4),d1
	move.b	d0,(a4)+
	move.b	d1,d0
	bne.s	.NNext
	moveq	#0,d2
.Bebe	move.b	(a3)+,d0
	beq.s	.FNext
	cmp.b	#'(',d0
	bne.s	.FPNext	
	subq.l	#1,d2
.FPNext	cmp.b	#')',d0
	bne.s	.FSNext
	addq.l	#1,d2
.FSNext	tst.l	d2
	bmi.s	.Bebe
.FNext	lea	-1(a3),a3
	move.b	#')',d0
.FFNext	move.b	(a3),d1
	move.b	d0,(a3)+
	move.b	d1,d0
	bne.s	.FFNext
	bra.s	.Boucle
.End	lea	Operations(a6),a4
	lea	Numbers(a6),a5
	moveq	#0,d7
	moveq	#'+',d1
	moveq	#0,d0
	bra.s	Calculate
EndOK	moveq	#0,d1
EndErr	movem.l	d0/d1,-(sp)
	move.l	a6,a1
	move.l	4.w,a6
	move.l	#Max,d0
	jsr	FreeMem(a6)
	movem.l	(sp)+,d0/d1
EndMem	movem.l	(sp)+,d2-d7/a2-a6
	rts

Error0	tst.l	d7
	beq.s	Error
	subq.l	#1,d7
	move.l	(sp)+,d2
	move.w	(sp)+,d1
	bra.s	Error0
Error	moveq	#0,d0
	moveq	#-1,d1
	bra	EndErr

Calculate
	cmp.b	#'(',d1
	bne.s	.PNext
	addq.l	#1,d7
	move.w	#'+',-(sp)
	move.l	#0,-(sp)
	bra	.End0
.PNext	cmp.b	#'(',(a4)
	bne.s	.Next
	addq.l	#1,d7
	move.w	d1,-(sp)
	move.l	d0,-(sp)
	move.l	(a5)+,d0
	lea	1(a4),a4
	bra.s	.End0
.Next	cmp.b	#')',d1
	bne.s	.NNext
	subq.l	#1,d7
	bmi.s	Error
	move.l	(sp)+,d2
	move.w	(sp)+,d1
	exg	d2,d0
	bra.s	.CNext
.NNext	move.l	(a5)+,d2
.CNext	cmp.b	#'+',d1
	bne.s	.NoAdd
	add.l	d2,d0
	bra.s	.End0
.NoAdd	cmp.b	#'-',d1
	bne.s	.NoSub
	sub.l	d2,d0
	bra.s	.End0
.NoSub	cmp.b	#'*',d1
	bne.s	.NoMul
	mull	d2,d0
	bra.s	.End0
.NoMul	cmp.b	#'/',d1
	bne.s	.Well
	tst.l	d2
	beq	Error0
	divs	d2,d0
	ext.l	d0
	bra.s	.End0
.Well	bra	Error0
.End0	move.b	(a4)+,d1
	bne	Calculate
	tst.l	d7
	bne	Error0
	bra	EndOK


ProcessString
	move.b	d0,d1
	move.l	a0,d3
.Boucle	move.b	(a0)+,d0
	bne.s	.Cont
	lea	-1(a0),a0
	bra.s	.End
.Cont	cmp.b	d1,d0
	bne.s	.Boucle
.End	move.l	a0,a1
	lea	-1(a0),a0
	moveq	#0,d1
	cmp.l	a0,d3
	beq	Cont
	moveq	#0,d2
.Calcul	moveq	#0,d0
	move.b	-(a0),d0
	lsl.l	d2,d0
	add.l	d0,d1
	addq.l	#8,d2
	cmp.l	a0,d3
	bne.s	.Calcul
	move.l	a1,a0
	bra	Cont

ProcessBinary
	move.l	a0,d3
.Boucle	move.b	(a0)+,d0
	cmp.b	#'1',d0
	beq.s	.Boucle
	cmp.b	#'0',d0
	beq.s	.Boucle
	lea	-1(a0),a0
	moveq	#0,d1
	cmp.l	a0,d3
	beq	Cont
	move.l	a0,a1
	moveq	#0,d2
.Calcul	move.b	-(a0),d0
	sub.b	#'0',d0
	beq.s	.No
	bset	d2,d1
.No	addq.l	#1,d2
	cmp.l	a0,d3
	bne.s	.Calcul
	move.l	a1,a0
	bra	Cont

ProcessHexadecimal
	move.l	a0,d3
.Boucle	move.b	(a0)+,d0
	cmp.b	#'f',d0
	bgt.s	.End
	cmp.b	#'a',d0
	bge.s	.Boucle
	cmp.b	#'F',d0
	bgt.s	.End
	cmp.b	#'A',d0
	bge.s	.Boucle
	cmp.b	#'9',d0
	bgt.s	.End
	cmp.b	#'0',d0
	bge.s	.Boucle
.End	lea	-1(a0),a0
	moveq	#0,d1
	cmp.l	a0,d3
	beq	Cont
	move.l	a0,a1
	moveq	#0,d2
.Calcul	moveq	#0,d0	
	move.b	-(a0),d0
	sub.b	#'0',d0
	cmp.b	#9,d0
	ble.s	.OK
	subq.b	#'A'-'0'-10,d0
	cmp.b	#15,d0
	ble.s	.OK
	sub.b	#'a'-'A',d0
.OK	lsl.l	d2,d0
	add.l	d0,d1
	addq.l	#4,d2
	cmp.l	a0,d3
	bne.s	.Calcul
	move.l	a1,a0
	bra	Cont

ProcessDecimal
	move.l	a0,d3
.Boucle	move.b	(a0)+,d0
	cmp.b	#'0',d0
	blt.s	.End
	cmp.b	#'9',d0
	ble.s	.Boucle
.End	lea	-1(a0),a0
	moveq	#0,d1
	cmp.l	a0,d3
	beq	Cont
	move.l	a0,a1
	moveq	#1,d2
.Calcul	moveq	#0,d0
	move.b	-(a0),d0
	sub.b	#'0',d0
	mull	d2,d0
	add.l	d0,d1
	move.l	d0,-(sp)
	move.l	d2,d0
	mulu	#10,d2
	swap	d0
	mulu	#10,d0
	swap	d0
	move.w	#0,d0
	add.l	d0,d2
	move.l	(sp)+,d0
	cmp.l	a0,d3
	bne.s	.Calcul
	move.l	a1,a0
	bra	Cont

ProcessOctal
	move.l	a0,d3
.Boucle	move.b	(a0)+,d0
	cmp.b	#'0',d0
	blt.s	.End
	cmp.b	#'7',d0
	ble.s	.Boucle
.End	lea	-1(a0),a0
	moveq	#0,d1
	cmp.l	a0,d3
	beq	Cont
	move.l	a0,a1
	moveq	#0,d2
.Calcul	moveq	#0,d0
	move.b	-(a0),d0
	sub.b	#'0',d0
	lsl.l	d2,d0
	add.l	d0,d1
	addq.l	#3,d2
	cmp.l	a0,d3
	bne.s	.Calcul
	move.l	a1,a0
	bra	Cont




DISPLAYHEXA	=	1<<0
DISPLAYBINARY	=	1<<1
DISPLAYDECIMAL	=	1<<2
WB2FEELING	=	1<<3
GADGSIZE	=	1<<4
SHOWNEGATIVE	=	1<<5
ATRIGHT		=	1<<6
ATLEFT		=	1<<7
NOMOVE		=	1<<8
DRAG		=	1<<9
GADGZOOM	=	1<<10		; under WB2.0


CRParameters	dc.w 400		; Width
		dc.w $FFFF		; LeftEdge ou $FFFF (centrage)
		dc.w $FFFF		; TopEdge ou $FFFF (centrage)
		dc.l DISPLAYDECIMAL|DISPLAYBINARY|DISPLAYHEXA|WB2FEELING|SHOWNEGATIVE|DRAG|GADGZOOM|GADGSIZE
CRPTitle	dc.l 0			; Titre de la fenêtre
		dc.b 2			; couleur 1 du bord
		dc.b 1			; couleur 2 du bord


CRTitle		dc.b "MiniCalcu pour le PAI",0

	END

