	nolist
	include 'exec/types.i'
	include 'exec/memory.i'
	include 'intuition/intuition.i'
;	include 'intuition.i'
	list

;	Calc.asm
;	Version 1.0.001
;
;	These routines take care of handling all intuition input
;	and all window activies. It also handles the allocating
;	and deallocating of gadgets in chip memory.
;

execute	MACRO
	XREF	_LVO\1
	jsr	_LVO\1(a6)
	ENDM

	xref	Gadg,HEX,Decimal,Octal,hp,ocp,dp
	xref	BIN,_CLR,Bp,Cp
	xref	Zero,One,Two,Three,zp,op,twop,thrp
	xref	Four,Five,Six,Seven,fp,fip,sip,sep
	xref	Eight,Nine,Ten,Eleven,ep,np,tp,elp
	xref	Twelve,Thrten,Forten,Fiften,twp,thp,fop,fifp
	xref	Plus,Minus,Multi,Divide,Plp,Mip,Mup,Dip
	xref	Equal,_Title,Eqp,Tip
	xref	GadT,num,Clear,Cl,Funct,Itext0,Iok,ReqNam
	xref	Igt,ReqNam1,HItext0
	xref	Display,VarPtr
	xdef	Finevnt,Input

*	A0 contains a pointer to the CLI input command.
*	D0 contains the length of that string.

ExecB	Equ	4

*	Window.asm

start	move.l	ExecB,a6
	move.w	#0,d0		; Hex Default Input Method
	lea	Input,a0
	move.w	d0,0(a0)

	lea	Table,a4	; Memory allocation routine
	moveq	#0,d1
aloc	move.l	0(a4,d1),d0
	tst.l	d0
	beq.s	cont
	move.l	4(a4,d1),a2
	jsr	Amem(pc)
	tst.l	d0
	beq.l 	A_Err
	move.l	d0,8(a4,d1)
	move.l	12(a4,d1),a1
	move.l	d0,(a1)
	addi.l	#16,d1
	jmp	aloc(pc)


cont	lea	intuit,a1
	moveq	#0,d0
	execute	OpenLibrary
	tst.l	d0
	beq.l	e_int

	lea	Libstg,a3
	move.l	d0,0(a3)

	lea	graph,a1
	moveq	#0,d0
	execute	OpenLibrary
	tst.l	d0
	beq.l	e_int

	lea	Libstg,a3
	move.l	d0,4(a3)

	move.l	0(a3),a6
	lea	MyWin,a0
	execute	OpenWindow
	move.l	d0,a4			;Window Pointer in A4

	lea	Libstg,a3
	move.l	4(a3),a6

	move.l	wd_RPort(a4),a1
	moveq	#1,d0
	execute	SetAPen

	move.l	wd_RPort(a4),a1
	moveq	#15,d0
	moveq	#14,d1
	execute	Move

	move.l	wd_RPort(a4),a1
	move.l	#276,d0
	moveq	#14,d1
	execute	Draw

	move.l	wd_RPort(a4),a1
	move.l	#276,d0
	move.l	#54,d1
	execute	Draw

	move.l	wd_RPort(a4),a1
	move.l	#15,d0
	move.l	#54,d1
	execute	Draw

	move.l	wd_RPort(a4),a1
	move.l	#15,d0
	move.l	#14,d1
	execute	Draw

	move.l	wd_RPort(a4),a1
	move.l	#15,d0
	move.l	#24,d1
	execute	Draw

	move.l	wd_RPort(a4),a1
	move.l	#276,d0
	move.l	#24,d1
	execute	Draw

	move.l	wd_RPort(a4),a1
	move.l	#276,d0
	move.l	#34,d1
	execute	Draw

	move.l	wd_RPort(a4),a1
	move.l	#15,d0
	move.l	#34,d1
	execute	Draw

	move.l	wd_RPort(a4),a1
	move.l	#15,d0
	move.l	#44,d1
	execute	Draw

	move.l	wd_RPort(a4),a1
	move.l	#276,d0
	move.l	#44,d1
	execute	Draw

;	Main

	lea	Libstg,a3
	move.l	0(a3),a6
	jsr	Cl
	move.l	ExecB,a6
gmsg	move.l	wd_UserPort(a4),a0
	execute	WaitPort
clfifo	move.l	wd_UserPort(a4),a0
	execute	GetMsg
	tst.l	d0
	beq.s	gmsg
	move.l	d0,d6
	move.l	d0,a1
	move.l	wd_UserPort(a4),a0
	execute	ReplyMsg
	move.l	d6,a0
	move.l	im_Class(a0),d0
	cmpi.l	#CLOSEWINDOW,d0
	beq.l	close
	cmpi.l	#RAWKEY,d0
	beq.l	Key
	move.l	im_IAddress(a0),a0
	moveq	#0,d0
	move.w	gg_GadgetID(a0),d0
	move.l	d0,d1
	cmpi.l	#26,d0
	bgt.s	Finevnt
	lea	jptbl,a1
	mulu	#4,d0
	move.l	0(a1,d0.w),a0
	lea	Libstg,a3
	move.l	0(a3),a6
	jmp	0(a0)

Finevnt	move.l	ExecB,a6
	jmp	clfifo

Key	moveq	#0,d0
	move.w	im_Code(a0),d0
	btst.l	#7,d0
	bne.s	Finevnt

	lea	Libstg,a3
	move.l	0(a3),a6

	move.l	#0,d3
	lea	KeyConv,a5
Keylp	move.l	0(a5,d3.w),d1
	cmpi.l	#$00FF,d1
	beq.s	Finevnt		; disable this to show keys
;	beq.s	gotoit		; enable this to show keys
	cmp.b	d0,d1
	beq.s	gotoit
	addi.l	#12,d3
	jmp	Keylp

gotoit	move.l	8(a5,d3.w),d1
	move.l	4(a5,d3.w),a5
	jmp	0(a5)		;disable this to show keys

				;enable the following to show keys
;	lea	VarPtr,a0
;	move.l	d0,4(a0)	; Display the key on screen
;	jsr	Display
;	jmp	Finevnt

close	lea	Libstg,a3
	move.l	0(a3),a6

	move.l	a4,a0
	execute	CloseWindow

done	lea	Libstg,a3
	move.l	0(a3),d0
	beq.s	done2
	move.l	d0,a1
	move.l	ExecB,a6
	execute	CloseLibrary

done2	lea	Libstg,a3
	move.l	4(a3),d0
	beq.s	done3
	move.l	d0,a1
	move.l	ExecB,a6
	execute	CloseLibrary

done3	lea	BOT,a4	; Memory de-allocation routine
	moveq	#0,d1
A_Err	subi.l	#16,d1
daloc	move.l	0(a4,d1),d0
	tst.l	d0
	beq.s	fini
	move.l	8(a4,d1),a1
	movem.l	d1,-(a7)
	execute FreeMem
	movem.l	(a7)+,d1
	subi.l	#16,d1
	jmp	daloc(pc)

fini	moveq	#0,d0
	rts

e_int	lea	dos,a1
	moveq	#0,d0
	execute	OpenLibrary
	tst.l	d0
	beq.s	e_dos
	move.l	d0,a6
	execute	Output
	move.l	d0,d1
	lea	Int_Err,a2
	move.l	a2,d2
	execute	Write
	move.l	a6,a1
	move.l	ExecB,a6
	execute	CloseLibrary
	moveq	#20,d0
e_dos	jmp	done(pc)

;********************************************************************
;			Amem					    *
;	INPUTS:	A6	=	Exec Library			    *
;		D0	=	Number of Bytes			    *
;		A2	=	Location of Source		    *
;								    *
;	OUTPUT:	D0	=	Pointer to Destination in CHIP MEM  *
;								    *
;********************************************************************


Amem	movem.l	a1/a4/d1,-(a7)
	move.l	#MEMF_CHIP,d1		; MEMF_CHIP
	move.l	d0,d5
	execute	AllocMem
	tst.l	d0
	beq.s	bad(pc)
	move.l	d0,a3
	moveq	#0,d0
lp2	move.l	(a2)+,0(a3,d0)
	addi.l	#4,d0
	cmp.l	d5,d0
	blt.s	lp2
	move.l	a3,d0
	movem.l	(a7)+,a1/a4/d1
bad	rts


;*******************************************************
;*             Gadget Enables                          *
;*               2-F  (2-15)                           *
;*             a4 = window pointer                     *
;*             d4 = start			       *
;*	       d5 = end				       *
;*******************************************************

GadEn	addi.l	#4,d5
	move.l	#0,a2
	lea	GadT,a3
cir	move.l	0(a3,d4.w),a0
	move.l	a4,a1
	execute	OnGadget
	addi.l	#4,d4
	cmp.l	d5,d4
	bne.s	cir
	rts

;*******************************************************
;*             Gadget Disables                         *
;*               2-F  (2-15)                           *
;*             a4 = window pointer                     *
;*             d4 = start			       *
;*	       d5 = end				       *
;*******************************************************

GadDi	move.l	#0,a2
	lea	GadT,a3
cird	move.l	0(a3,d4.w),a0
	move.l	a4,a1
	execute	OffGadget
	subi.l	#4,d4
	cmp.l	d5,d4
	bne.s	cird
	rts

DHex	move.w	#0,d0		; Hex Input Method
	lea	Input,a0

	move.w	0(a0),d4
	move.w	d0,0(a0)
	cmpi.w	#0,d4
	beq.l	Finevnt

	lea	InTbl,a0
	move.l	0(a0),d5
	mulu	#4,d4
	move.l	0(a0,d4.w),d4

	jsr	GadEn

	lea	Tb,a0
	move.b	#'H',0(a0)
	move.b	#'e',1(a0)
	move.b	#'x',2(a0)

	move.l	a4,a0
	lea	Titl,a1
	move.l	#-1,a2
	execute	SetWindowTitles
	jmp	Finevnt

DDec	move.w	#1,d0		; Dec Input Method
	lea	Input,a0

	move.w	0(a0),d4
	move.w	d0,0(a0)
	cmpi.w	#1,d4
	beq.l	Finevnt
	blt.s	didec

	lea	InTbl,a0
	move.l	4(a0),d5
	mulu	#4,d4
	move.l	0(a0,d4.w),d4
	jsr	GadEn
	jmp	decf(pc)

didec	lea	InTbl,a0
	move.l	4(a0),d5
	mulu	#4,d4
	move.l	0(a0,d4.w),d4
	jsr	GadDi

decf	lea	Tb,a0
	move.b	#'D',0(a0)
	move.b	#'e',1(a0)
	move.b	#'c',2(a0)

	move.l	a4,a0
	lea	Titl,a1
	move.l	#-1,a2
	execute	SetWindowTitles
	jmp	Finevnt

DOct	move.w	#2,d0		; Oct Input Method
	lea	Input,a0

	move.w	0(a0),d4
	move.w	d0,0(a0)
	cmpi.w	#2,d4
	beq.l	Finevnt
	blt.s	dioct

	lea	InTbl,a0
	move.l	8(a0),d5
	mulu	#4,d4
	move.l	0(a0,d4.w),d4
	jsr	GadEn
	jmp	octf(pc)

dioct	lea	InTbl,a0
	move.l	8(a0),d5
	mulu	#4,d4
	move.l	0(a0,d4.w),d4
	jsr	GadDi

octf	lea	Tb,a0
	move.b	#'O',0(a0)
	move.b	#'c',1(a0)
	move.b	#'t',2(a0)

	move.l	a4,a0
	lea	Titl,a1
	move.l	#-1,a2
	execute	SetWindowTitles
	jmp	Finevnt

DBin	move.w	#3,d0		; Bin Input Method
	lea	Input,a0

	move.w	0(a0),d4
	move.w	d0,0(a0)
	cmpi.w	#3,d4
	beq.l	Finevnt

	lea	InTbl,a0
	move.l	12(a0),d5
	mulu	#4,d4
	move.l	0(a0,d4.w),d4
	jsr	GadDi

binf	lea	Tb,a0
	move.b	#'B',0(a0)
	move.b	#'i',1(a0)
	move.b	#'n',2(a0)

	move.l	a4,a0
	lea	Titl,a1
	move.l	#-1,a2
	execute	SetWindowTitles
	jmp	Finevnt


TitlR	move.l	a4,a0
	lea	ReqNam,a1
	move.l	#-1,a2
	execute	SetWindowTitles
	move.l	a4,a0
	lea	Itext0,a1
	move.l	#0,a2
	lea	Iok,a3
	moveq	#0,d0
	move.l	#0,d1
	move.l	#350,d2
	move.l	#125,d3
	execute	AutoRequest
	move.l	a4,a0
	lea	Titl,a1
	move.l	#-1,a2
	execute	SetWindowTitles
	jmp	Finevnt

TitlH	move.l	a4,a0
	lea	ReqNam1,a1
	move.l	#-1,a2
	execute	SetWindowTitles
	move.l	a4,a0
	lea	HItext0,a1
	move.l	#0,a2
	lea	Igt,a3
	moveq	#0,d0
	move.l	#0,d1
	move.l	#440,d2
	move.l	#145,d3
	execute	AutoRequest
	move.l	a4,a0
	lea	Titl,a1
	move.l	#-1,a2
	execute	SetWindowTitles
	jmp	Finevnt



;	Memory allocation Table for all chip destined data
;		Bytes,Source,Destination(CHIP),Install Address

	dc.l	0,0,0,0     	; so we can find the top
Table	dc.l	204,HEX,0,hp
	dc.l	204,Octal,0,ocp
	dc.l	204,Decimal,0,dp
	dc.l	204,BIN,0,Bp
	dc.l	204,_CLR,0,Cp
	dc.l	144,Zero,0,zp
	dc.l	144,One,0,op
	dc.l	144,Two,0,twop
	dc.l	144,Three,0,thrp
	dc.l	144,Four,0,fp
	dc.l	144,Five,0,fip
	dc.l	144,Six,0,sip
	dc.l	144,Seven,0,sep
	dc.l	144,Eight,0,ep
	dc.l	144,Nine,0,np
	dc.l	144,Ten,0,tp
	dc.l	144,Eleven,0,elp
	dc.l	144,Twelve,0,twp
	dc.l	144,Thrten,0,thp
	dc.l	144,Forten,0,fop
	dc.l	144,Fiften,0,fifp
	dc.l	144,Plus,0,Plp
	dc.l	144,Minus,0,Mip
	dc.l	144,Multi,0,Mup
	dc.l	144,Divide,0,Dip
	dc.l	144,Equal,0,Eqp
	dc.l	1260,_Title,0,Tip
BOT	dc.l	0,0,0,0	  	; bottom


KeyConv	dc.l	$17,TitlR,0	;I
	dc.l	$5F,TitlH,0	;HELP
	dc.l	$45,close,0	;ESC

	dc.l	$A,num,0	;0
	dc.l	1,num,1		;1
	dc.l	2,num,2		;2
	dc.l	3,num,3		;3
	dc.l	4,num,4		;4
	dc.l	5,num,5		;5
	dc.l	6,num,6		;6
	dc.l	7,num,7		;7
	dc.l	8,num,8		;8
	dc.l	9,num,9		;9

	dc.l	$F,num,0	;0
	dc.l	$1D,num,1	;1
	dc.l	$1E,num,2	;2
	dc.l	$1F,num,3	;3
	dc.l	$2D,num,4	;4
	dc.l	$2E,num,5	;5
	dc.l	$2F,num,6	;6
	dc.l	$3D,num,7	;7
	dc.l	$3E,num,8	;8
	dc.l	$3F,num,9	;9

	dc.l	$20,num,$A	;A
	dc.l	$35,num,$B	;B
	dc.l	$33,num,$C	;C
	dc.l	$22,num,$D	;D
	dc.l	$12,num,$E	;E
	dc.l	$23,num,$F	;F

	dc.l	$50,num,$A	;F1
	dc.l	$51,num,$B	;F2
	dc.l	$52,num,$C	;F3
	dc.l	$53,num,$D	;F4
	dc.l	$54,num,$E	;F5
	dc.l	$55,num,$F	;F6

	dc.l	$56,DHex,$0	;F7
	dc.l	$57,DOct,$0	;F8
	dc.l	$58,DBin,$0	;F9
	dc.l	$59,DDec,$0	;F10

	dc.l	$46,Clear,26	;DEL

	dc.l	$0C,Funct,16	;+
	dc.l	$0B,Funct,17	;-
	dc.l	$4A,Funct,17	;-
	dc.l	$32,Funct,18	;*
	dc.l	$3A,Funct,19	;/

	dc.l	$44,Funct,20	;=
	dc.l	$43,Funct,20	;=

	dc.l	94,Funct,16	;+ for Amy 500&2000
	dc.l	93,Funct,18	;* for Amy 500&2000
	dc.l	92,Funct,19	;/ for Amy 500&2000

	dc.l	$FF,0		;End of KeyConv table
	dc.l	$FF,0		;End of KeyConv table
	dc.l	$FF,0		;End of KeyConv table
	dc.l	$FF,0		;End of KeyConv table

jptbl	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	num
	dc.l	Funct
	dc.l	Funct
	dc.l	Funct
	dc.l	Funct
	dc.l	Funct
	dc.l	TitlR
;22 is Hex
	dc.l	DHex
	dc.l	DOct
;24 is Dec
	dc.l	DDec
	dc.l	DBin
	dc.l	Clear

	dc.l	Finevnt
	dc.l	Finevnt
	dc.l	Finevnt

MyWin	dc.w	10,10,409,119
	dc.b	0,1
	dc.l	CLOSEWINDOW+GADGETUP+RAWKEY
	dc.l	ACTIVATE+WINDOWDEPTH+WINDOWCLOSE+WINDOWDRAG
	dc.l	Gadg,0
	dc.l	Titl
	dc.l	0,0
	dc.w	20,20,350,100
	dc.w	WBENCHSCREEN

Titl	dc.b	'Compu-Calc  Ver 1.00        Input:'
Tb	dc.b	'Hex',0

Libstg	ds.l	2		; long 0=Intuit 1=graphics

Input	ds.w	1		; 0=Hex 1=Dec 2=Oct 3=Bin
InTbl	dc.l	60,36,28,4

Int_Err	dc.b	'Error Opening A Library',0
intuit	dc.b	'intuition.library',0
dos	dc.b	'dos.library',0
graph	dc.b	'graphics.library',0


	END
