	nolist
	include 'exec/types.i'
	include 'exec/memory.i'
	include 'intuition/intuition.i'
	include 'graphics/rastport.i'
	list

;	Version 1.0
;	Input.asm
;
;	These routines handle the actual actions made from the user
;		(the meat!)
;

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

	xref	Finevnt,Input
	xdef	num,Clear,Cl,Funct
	xdef	Display,VarPtr

ExecB	Equ	4

;	D1 is the gadget number pressed (0-15)
;	A4 is the pointer to the window

num	lea	Err,a0
	move.l	0(a0),d3
	cmpi.l	#1,d3
	beq.l	NODO
	lea	Const,a0
	move.l	0(a0),d0
	beq.s	numgo
	moveq	#0,d2
	move.l	d2,0(a0)
	lea	VarPtr,a0
	move.l	0(a0),d0
	move.l	d2,4(a0,d0.w)
numgo	lea	Input,a0
	move.w	0(a0),d0
	cmpi.w	#3,d0
	bne.s	Npt1

;	BIN Input
	cmpi.l	#1,d1
	bgt.l	Finevnt
	lea	VarPtr,a0
	move.l	0(a0),d0
	move.l	4(a0,d0.w),d2	; get current variable
	lsl.l	#1,d2
	or.l	d1,d2
	move.l	d2,4(a0,d0.w)
	jmp	Npt

Npt1	lea	Input,a0
	move.w	0(a0),d0
	cmpi.w	#2,d0
	bne.s	Npt2

;	Oct Input
	cmpi.l	#7,d1
	bgt.l	Finevnt
	lea	VarPtr,a0
	move.l	0(a0),d0
	move.l	4(a0,d0.w),d2	; get current variable
	lsl.l	#3,d2
	or.l	d1,d2
	move.l	d2,4(a0,d0.w)
	jmp	Npt

Npt2	lea	Input,a0
	move.w	0(a0),d0
	cmpi.w	#0,d0
	bne.s	Npt3

;	Hex Input
	lea	VarPtr,a0
	move.l	0(a0),d0
	move.l	4(a0,d0.w),d2	; get current variable
	lsl.l	#4,d2
	or.l	d1,d2
	move.l	d2,4(a0,d0.w)
	jmp	Npt

Npt3	lea	Input,a0
	move.w	0(a0),d0
	cmpi.w	#1,d0
	bne.s	Npt

;	Dec Input
	cmpi.l	#9,d1
	bgt.l	Finevnt
	lea	VarPtr,a0
	move.l	0(a0),d0
	move.l	4(a0,d0.w),d2	; get current variable
	moveq	#0,d4
	move.l	d2,d3
	lsl.l	#1,d3
	add.l	d3,d4
	lsl.l	#2,d3
	add.l	d3,d4
	add.l	d1,d4
	move.l	d4,4(a0,d0.w)

Npt	jsr	Display
	jmp	Finevnt

;
;	This routine prints the value in the current variable
;	position
;

Display	lea	VarPtr,a0
	move.l	0(a0),d0
	move.l	4(a0,d0.w),d2	; get current variable
	cmpi.l	#0,d2
	bne.s	Ncont
	jsr	Cl		; cheap but effective way to eliminate
	rts			; total leading zeros elimination on 0
Ncont	jsr	BtoA
	jsr	HtoA
	jsr	OtoA
	jsr	BtoB
	jsr	print
	rts

print	move.l	wd_RPort(a4),a0
	lea	ItextH,a1
	moveq	#17,d0
	moveq	#16,d1
	execute	PrintIText
	move.l	wd_RPort(a4),a0
	lea	ItextO,a1
	moveq	#17,d0
	moveq	#26,d1
	execute	PrintIText
	move.l	wd_RPort(a4),a0
	lea	ItextB,a1
	moveq	#17,d0
	moveq	#36,d1
	execute	PrintIText
	move.l	wd_RPort(a4),a0
	lea	ItextD,a1
	moveq	#17,d0
	moveq	#46,d1
	execute	PrintIText
	rts

Cl	lea	CBufB,a0
	lea	BufferB,a1
	moveq	#0,d0
lpCl	move.w	0(a0,d0),d1
	move.w	d1,0(a1,d0)
	addi.w	#2,d0
	cmpi.w	#136,d0
	bne.s	lpCl
	jsr	print
	lea	VarPtr,a0
	rts


Init	moveq	#0,d0		; Init clear values
	move.l	d0,0(a0)	; Var 1,2 and pointer
	move.l	d0,4(a0)
	move.l	d0,8(a0)
	move.l	d0,12(a0)	; Operation
	lea	Const,a0	; Constant
	move.l	d0,0(a0)
	lea	Err,a0
	move.l	d0,0(a0)
	rts

Clear	jsr	Cl
	jsr	Init
	jmp	Finevnt

;***************************************************
;*		ClearT				   *
;*	Sets T Buffer variable equal to spaces     *
;***************************************************

ClearT	lea	BufferT,a0
	move.b	#$20,d0
	moveq	#$0,d1
Cllp	move.b	d0,0(a0,d1.w)
	addi.l	#1,d1
	cmpi.l	#$32,d1
	blt.s	Cllp
	rts

;***************************************************
;*		HtoA				   *
;*	Converts variable pointed to by VarPtr to  *
;* 	a 32 byte HEX ascii string for displaying. *
;***************************************************

HtoA	jsr	ClearT
	lea	VarPtr,a0
	move.l	0(a0),d0
	move.l	4(a0,d0.w),d0	; get current varible
	lea	BufferT,a1
	move.l	#29,d3
HtoAtp	move.l	d0,d2
	andi.l	#%00000000000000000000000000001111,d2
	addi.l	#$30,d2		; ascii offset for num
	cmpi.l	#$39,d2
	ble.s	HtoAct
	addi.l	#$7,d2		; ascii offset for alpha
HtoAct	move.b	d2,0(a1,d3.w)
	lsr.l	#4,d0
	subi.l	#4,d3
	cmpi.l	#1,d3
	bge.s	HtoAtp

	lea	BufferT,a1
	lea	BufferH,a0
	jsr	Rmzero
	rts


;***************************************************
;*		OtoA				   *
;*	Converts variable pointed to by VarPtr to  *
;* 	a 32 byte Oct ascii string for displaying. *
;***************************************************

OtoA	jsr	ClearT
	lea	VarPtr,a0
	move.l	0(a0),d0
	move.l	4(a0,d0.w),d0	; get current variable
	lea	BufferT,a1
	move.l	#30,d3
OtoAtp	move.l	d0,d2
	andi.l	#%00000000000000000000000000000111,d2
	addi.l	#$30,d2		; ascii offset for num
	move.b	d2,0(a1,d3.w)
	lsr.l	#3,d0
	subi.l	#3,d3
	cmpi.l	#0,d3
	bge.s	OtoAtp

	lea	BufferT,a1
	lea	BufferO,a0
	jsr	Rmzero
	rts



;***************************************************
;*		BtoA				   *
;*	Converts variable pointed to by VarPtr to  *
;* 	a 32 byte BIN ascii string for displaying. *
;***************************************************

BtoA	jsr	ClearT
	lea	VarPtr,a0
	move.l	0(a0),d0
	move.l	4(a0,d0.w),d0	; get current variable
	lea	BufferT,a1
	move.b	#$30,d2
	move.b	#$31,d3
	move.l	#31,d1
	moveq	#0,d4
BtoAlp	btst.l	d1,d0
	beq.s	BZero
	move.b	d3,0(a1,d4)
	jmp	BtoAf
BZero	move.b	d2,0(a1,d4)
BtoAf	addi.l	#1,d4
	subi.l	#1,d1
	bpl.s	BtoAlp

	lea	BufferT,a1
	lea	BufferB,a0
	jsr	Rmzero
	rts

;******************************************************************
;*		Rmzero		Kill leading zeros		  *
;*	Inputs:	a0 pointer to destination buffer		  *
;*		a1 pointer to source				  *
;*	Bad:	d0,d1,d3,d4					  *
;******************************************************************

Rmzero	move.l	#$20,d3		; remove leading zeros
	move.l	d3,d4
	move.l	#0,d1
Rmzlp1	move.b	0(a1,d1.w),d0
	cmpi.b	#$30,d0
	beq.s	Rmzzr
	cmpi.b	#$20,d0
	beq.s	Rmzlp2
	move.l	#$30,d3
	move.b	d0,0(a0,d1)
	jmp	Rmzze

Rmzlp2	move.b	d4,0(a0,d1)
	jmp	Rmzze

Rmzzr	move.b	d3,0(a0,d1)
Rmzze	addi.l	#1,d1
	cmpi.l	#32,d1
	bne.s	Rmzlp1
	rts

;**************************************************
;*		BIN to BCD			  *
;**************************************************

BtoB	lea	DecTemp,a0	; Clear Working buff
	moveq	#0,d0
	move.w	d0,0(a0)
	move.w	d0,2(a0)
	move.w	d0,4(a0)

	lea	VarPtr,a0
	move.l	0(a0),d0
	move.l	4(a0,d0.w),d0	; get current variable
	moveq	#0,d3		; constant pointer
	lea	DecCon2,a2
	moveq	#8,d4
BtoBtp	move.l	d0,d2
	andi.l	#%00000000000000000000000000001111,d2
BtoBlp1	beq.s 	BtoBdn
	lea	DecCon1,a0	; Temp pointer
	andi.b	#$EF,ccr
	move.l	a2,a1
	adda.l	d3,a1		; current pointer to con
BtoBlp2	abcd	-(a1),-(a0)
	cmpa.l	#DecTemp,a0
	bne.s	BtoBlp2
	subi.l	#1,d2
	jmp	BtoBlp1

BtoBdn	lsr.l	#4,d0
	addi.l	#6,d3
	subi.l	#1,d4
	bne.s	BtoBtp

	lea	DecTemp,a0	; change buffer to ascii
	lea	AscTmp,a1
	moveq	#0,d0		; input buf
	moveq	#0,d3		; out buf
BtoBlp3	move.b	0(a0,d0),d2
	move.b	d2,d1
	lsr.b	#4,d1
	andi.b	#$0F,d1
	addi.b	#$30,d1
	move.b	d1,0(a1,d3)
	move.b	d2,d1
	andi.b	#$0F,d1
	addi.b	#$30,d1
	move.b	d1,1(a1,d3)
	addi.l	#2,d3
	addi.l	#1,d0
	cmpi.l	#7,d0
	bne.s	BtoBlp3

	lea	BufDec,a1
	lea	BufferD,a0
	jsr	Rmzero

	rts

;
;	Function: This routine handles all process of
;		 function keys.
;

Funct	lea	Err,a0
	move.l	0(a0),d3
	cmpi.l	#1,d3
	beq.l	NODO
	subi.l	#15,d1		; d1=0-5 (nop,+,-,*,/,=)
	cmpi	#5,d1
	beq.s	equal
	lea	Const,a0
	move.l	0(a0),d0
	bne.l	per		; =1

equal	lea	VarPtr,a0
	move.l	12(a0),d3	;Operation
	beq.l	noper

	cmpi.l	#1,d3
	bne.s	Sub0

	move.l	0(a0),d4
	beq.s	Fzer0
	move.l	4(a0,d4.w),d6
	add.l	d6,0(a0,d4.w)
	jmp	FDis
Fzer0	move.l	0(a0,d4.w),d6
	add.l	d6,4(a0,d4.w)
FDis	cmpi	#5,d1
	beq.s	nodo
	move.l	d1,12(a0)
nodo	jsr	Fswap
	jsr	Display
	jsr	Fswap
	moveq	#1,d0
	lea	Const,a0
	move.l	d0,0(a0)
NODO	jmp	Finevnt

Sub0	cmpi.l	#2,d3
	bne.s	Mul0

	move.l	0(a0),d4
	beq.s	Szer0
	move.l	4(a0,d4.w),d6
	sub.l	d6,0(a0,d4.w)
	jmp	SDis
Szer0	move.l	0(a0,d4.w),d6
	sub.l	d6,4(a0,d4.w)
SDis	cmpi.l	#5,d1
	beq.s	nodo1
	move.l	d1,12(a0)
nodo1	jsr	Fswap
	jsr	Display
	jsr	Fswap
	moveq	#1,d0
	lea	Const,a0
	move.l	d0,0(a0)
	jmp	Finevnt

Mul0	cmpi.l	#3,d3
	bne.l	Div0

	move.l	0(a0),d4
	beq.s	Mzer0
	move.l	4(a0,d4.w),d6
	move.l	0(a0,d4.w),d7

	moveq	#0,d3
	cmpi.l	#0,d6
	beq.l	MulE1
	cmpi.l	#0,d7
	beq.l	MulE1

;	Mult
;	input d6,d7
;	output d3
;	distroyed d4,d5

Mult	moveq	#31,d5
mlp1	btst.l	d5,d6
	dbne	d5,mlp1

	addi.l	#1,d5
	moveq	#0,d4
	moveq	#0,d3
mlp2	lsr.l	#1,d6
	bcc.s	Mnoadd
	add.l	d7,d3
	bvs.l	error
Mnoadd	lsl.l	#1,d7
	bcs.l	error
	addi.l	#1,d4
	cmp.l	d4,d5
	bne.s	mlp2

MulE1	move.l	0(a0),d4
	move.l	d3,0(a0,d4.w)
	jmp	Mdis

Mzer0	move.l	0(a0,d4.w),d6
	move.l	4(a0,d4.w),d7

	moveq	#0,d3
	cmpi.l	#0,d6
	beq.l	MulE2
	cmpi.l	#0,d7
	beq.l	MulE2

;	Mult
;	input d6,d7
;	output d3
;	distroyed d4,d5

Mult1	moveq	#31,d5
mlp11	btst.l	d5,d6
	dbne	d5,mlp11

	addi.l	#1,d5
	moveq	#0,d4
	moveq	#0,d3
mlp21	lsr.l	#1,d6
	bcc.s	Mnoadd1
	add.l	d7,d3
	bvs.l	error
Mnoadd1	lsl.l	#1,d7
	bcs.l	error
	addi.l	#1,d4
	cmp.l	d4,d5
	bne.s	mlp21

MulE2	move.l	0(a0),d4
	move.l	d3,4(a0,d4.w)

Mdis	cmpi.l	#5,d1
	beq.s	nodo2
	move.l	d1,12(a0)
nodo2	jsr	Fswap
	jsr	Display
	jsr	Fswap
	moveq	#1,d0
	lea	Const,a0
	move.l	d0,0(a0)
	jmp	Finevnt

Div0	cmpi.l	#4,d3
	bne.l	nopcnt

	move.l	0(a0),d4
	beq.s	Dzer0
	move.l	4(a0,d4.w),d7
	move.l	0(a0,d4.w),d6

;	Divide
;	input d6,d7
;	output d3
;	distroyed d2,d4,d5

	moveq	#31,d5
	tst.l	d7
lenlp	bmi.s	endl
	sub.l	#1,d5
	bcs.l	error	; this means there are no ones in divisor
	lsl.l	#1,d7
	jmp	lenlp

;	Left justified divisor

endl	add.l	#1,d5	;Length

	moveq	#0,d3
	moveq	#31,d4
	sub.l	d5,d4
	add.l	#1,d4	; Starting bit position in Answer

dlp2	move.l	d6,d5

	sub.l	d7,d5	; d7-d2 to d6
	bcs.s	divsk

	bset.l	d4,d3	; set answer position to 1
	move.l	d5,d6	; save result

divsk	sub.l	#1,d4
	bmi.s 	DivE1
	lsr.l	#1,d7
	jmp	dlp2

DivE1	move.l	0(a0),d4
	move.l	d3,0(a0,d4.w)
	jmp	Ddis

Dzer0	move.l	0(a0,d4.w),d7
	move.l	4(a0,d4.w),d6

;	Divide
;	input d6,d7
;	output d3
;	distroyed d2,d4,d5

	moveq	#31,d5
	tst.l	d7
lenlp1	bmi.s	endl1
	sub.l	#1,d5
	bcs.l	error	; this means there are no ones in divisor
	lsl.l	#1,d7
	jmp	lenlp1

;	Left justified divisor

endl1	add.l	#1,d5	;Length

	moveq	#0,d3
	moveq	#31,d4
	sub.l	d5,d4
	add.l	#1,d4	; Starting bit position in Answer

dlp21	move.l	d6,d5

	sub.l	d7,d5	; d7-d2 to d6
	bcs.s	divsk1

	bset.l	d4,d3	; set answer position to 1
	move.l	d5,d6	; save result

divsk1	sub.l	#1,d4
	bmi.s 	DivE2
	lsr.l	#1,d7
	jmp	dlp21

DivE2	move.l	0(a0),d4
	move.l	d3,4(a0,d4.w)

Ddis	cmpi.l	#5,d1
	beq.s	nodo3
	move.l	d1,12(a0)
nodo3	jsr	Fswap
	jsr	Display
	jsr	Fswap
	moveq	#1,d0
	lea	Const,a0
	move.l	d0,0(a0)
	jmp	Finevnt

nopcnt	jsr	Display
	jsr	Fswap
	jmp	Finevnt


noper	cmpi.l	#5,d1
	beq.s	equnper
	lea	VarPtr,a0
	move.l	d1,12(a0)
	jsr	Fswap
equnper	jmp	Finevnt

per	lea	VarPtr,a0	; if a valid operation and constant
	move.l	d1,12(a0)	; then store operation new operation
	jmp	Finevnt		; only.

;
;	Move to other input varable
;

Fswap	lea	VarPtr,a0
	move.l	0(a0),d3
	beq.s	Fswze
	moveq	#0,d4
	move.l	d4,0(a0)
	jmp	Fswend
Fswze	moveq	#4,d4
	move.l	d4,0(a0)
Fswend	rts


error	lea	EBufB,a0
	lea	BufferB,a1
	moveq	#0,d0
ElpCl	move.w	0(a0,d0),d1
	move.w	d1,0(a1,d0)
	addi.w	#2,d0
	cmpi.w	#136,d0
	bne.s	ElpCl
	jsr	print
	moveq	#1,d0
	lea	Err,a0
	move.l	d0,0(a0)
	jmp	equnper


ItextB	dc.b	1,0
	dc.b	RP_JAM2
	dc.w	0,0
	dc.l	0
	dc.l	BufferB
	dc.l	0

ItextO	dc.b	1,0
	dc.b	RP_JAM2
	dc.w	0,0
	dc.l	0
	dc.l	BufferO
	dc.l	0

ItextH	dc.b	1,0
	dc.b	RP_JAM2
	dc.w	0,0
	dc.l	0
	dc.l	BufferH
	dc.l	0

ItextD	dc.b	1,0
	dc.b	RP_JAM2
	dc.w	0,0
	dc.l	0
	dc.l	BufferD
	dc.l	0

BufferB	dc.b	'00011100011100011100011100011100',0,0
BufferO	dc.b	'0  3  4  3  4  3  4  3  4  3  4 ',0,0
BufferH	dc.b	' 1   C   7   1   C   7   1   C  ',0,0
BufferD	dc.b	'                       208783132',0,0
BufferT	dc.b	'                                ',0,0

	ds.b	2

CBufB	dc.b	'                               0',0,0
CBufO	dc.b	'                              0 ',0,0
CBufH	dc.b	'                             0  ',0,0
CBufD	dc.b	'                               0',0,0

	ds.b	2

EBufB	dc.b	'   ********** Error **********  ',0,0
EBufO	dc.b	'   ********** Error **********  ',0,0
EBufH	dc.b	'   ********** Error **********  ',0,0
EBufD	dc.b	'   ********** Error **********  ',0,0

	ds.b	2

VarPtr	ds.l	1
Var	ds.l	1
	ds.l	1

Oper	dc.l	0	; 0,1,2,3,4 none,add,sub,Mult,Div

Const	dc.l	0
Err	dc.l	0
BufDec	dc.b	'                    '
AscTmp	ds.b	12
DecTemp	ds.w	3
DecCon1	dc.w	$0000,$0000,$0001
DecCon2	dc.w	$0000,$0000,$0016
DecCon3	dc.w	$0000,$0000,$0256
DecCon4	dc.w	$0000,$0000,$4096
DecCon5	dc.w	$0000,$0006,$5536
DecCon6	dc.w	$0000,$0104,$8576
DecCon7	dc.w	$0000,$1677,$7216
DecCon8	dc.w	$0002,$6843,$5456
DecConE	dc.w	$0000,$0000,$0000

	end
