NEW
  AUTO 3,1
*--------------------------------------
MATH			jmp (.1,x)
.1				.DA M16.uMUL
				.DA M16.uDIV
				.DA M16.uMOD
				.DA M16.ADD
				.DA M16.SUB

				.DA M16.SHL
				.DA M16.SHR

				.DA M16.L
				.DA M16.LE
				.DA M16.G
				.DA M16.GE
				.DA M16.E
				.DA M16.NE

				.DA M16.AND
				.DA M16.OR
				.DA M16.XOR

				.DA M16.LAND
				.DA M16.LOR
*--------------------------------------
				.DA M16.iMUL
				.DA M16.iDIV
				.DA M16.iMOD
				.DA M16.ADD
				.DA M16.SUB

				.DA M16.SHL
				.DA M16.SHR

				.DA M16.L
				.DA M16.LE
				.DA M16.G
				.DA M16.GE
				.DA M16.E
				.DA M16.NE

				.DA M16.AND
				.DA M16.OR
				.DA M16.XOR

				.DA M16.LAND
				.DA M16.LOR
*--------------------------------------
				.DA M32.uMUL
				.DA M32.uDIV
				.DA M32.uMOD
				.DA M32.ADD
				.DA M32.SUB

				.DA M32.SHL
				.DA M32.SHR

				.DA M32.L
				.DA M32.LE
				.DA M32.G
				.DA M32.GE
				.DA M32.E
				.DA M32.NE

				.DA M32.AND
				.DA M32.OR
				.DA M32.XOR

				.DA M32.LAND
				.DA M32.LOR
*--------------------------------------
				.DA M32.iMUL
				.DA M32.iDIV
				.DA M32.iMOD
				.DA M32.ADD
				.DA M32.SUB

				.DA M32.SHL
				.DA M32.SHR

				.DA M32.L
				.DA M32.LE
				.DA M32.G
				.DA M32.GE
				.DA M32.E
				.DA M32.NE

				.DA M32.AND
				.DA M32.OR
				.DA M32.XOR

				.DA M32.LAND
				.DA M32.LOR
*--------------------------------------
MATH.A2STR10NP	ldx #3					3 digit max
				ldy #0					Disable Padding

MATH.A2STR10	jsr M32.A2ACC32
				clc						unsigned
*--------------------------------------
* CS = signed long
* CC = unsigned long
* X  = Pad Len
* Y  = Pad Char
*--------------------------------------
MATH.ACC322STR10
				stx .5+1				Pad Len
				sty .81+1				Pad Char

				stz FOUTBuf-1
				bcc .1

				clc
				lda ACC32+3
				bpl .1
				 
				jsr M32.nACC

				sec
.1				ror ACC32.Sign			Save sign

				ldx #4

.2				stz RESULT,x			Clear all 5 bytes
				dex
				bpl .2

				sed						switch to BCD mode

				ldx #32					let's roll 32 bits

.3				jsr MATH.ACC32x2

				ldy #4

.4				lda RESULT,y
				adc RESULT,y
				sta RESULT,y
				dey
				bpl .4

				dex
				bne .3

				cld
		
				ldx .5+1				no padding
				beq .6

				lda #10					starts at 10-padlen
				sec

.5				sbc #$ff				SELF MODIFIED
				tax
	
.6				txa
				lsr
				tay
				lda RESULT,y
				bcs .7

				lsr
				lsr
				lsr
				lsr

.7				and #$0F
				ora #$30

				cmp #'0'
				beq .80

				ldy #'0'				next 0s always printed
				sty .81+1
				bra .82

.80				cpx #9					last char, print always
				beq .82

.81				lda #$ff				SELF MODIFIED	Get Padding char	
				beq .87
	
.82				bit ACC32.Sign			"-" to print ?
				bpl .86

				pha
				lda #'-'
				jsr .88
				stz ACC32.Sign
				pla

.86				jsr .88
.87				inx
				cpx #10
				bne .6

				lda #0

.88				ldy FOUTBuf-1
				inc FOUTBuf-1
				sta FOUTBuf,y
				rts
*--------------------------------------
* Convert Hex int at ZPPtr2 to ACC32
*--------------------------------------
MATH.Hex2ACC32	lda (ZPPtr2)
				beq .9
				
				jsr .80
				bcs .9
				
				jsr M32.A2ACC32
				
				ldy #0
				
.1				iny
				lda (ZPPtr2),y
				beq .8
				
				jsr .80
				bcs .8
				
				pha
				ldx #4
				
.2				jsr MATH.ACC32x2
				dex
				bne .2
				
				pla
				ora ACC32
				sta ACC32
				bra .1
				
.8				clc
				rts

.9				sec
.99				rts
*--------------------------------------
.80				jsr ZP.IsDigit
				bcc .88

				cmp #'A'
				bcc .9

				cmp #'F'+1
				bcc .81

				cmp #'a'
				bcc .9

				cmp #'f'+1
				bcs .99

				eor #$20			
.81
*				clc
				sbc #'A'-11			cc so A->10 (11-CC)
				clc
.88				and #$0F
				rts
*--------------------------------------
* Convert Decimal int at ZPPtr2 to ACC32
*--------------------------------------
MATH.Dec2ACC32	jsr M32.ACC32Z
				clc
				lda (ZPPtr2)
				eor #'-'
				bne .10

				jsr SHARED.NextCP2	skip '-'
				sec
.10				ror .80+1				set pos/neg flag

				ldy #$ff

.1				iny
				lda (ZPPtr2),y
				beq .8
				jsr ZP.IsDigit
				bcs .8

				phy						Save Y, pointing to next char
				jsr .70
				ply
				bcs .9

				lda (ZPPtr2),y
				and #$0F
*				clc

				adc ACC32
				sta ACC32
				bcc .1
				inc ACC32+1
				bne .1
				inc ACC32+2
				bne .1
				inc ACC32+3
				bne .1					if 0, overflow!!!

.9				lda #E.INUM
				sec
				rts

.8				tya						no digit parsed...error
				beq .9

.80				lda #$FF				SELF MODIFIED
				bpl .88

				phy
				jsr M32.nACC
				ply

.88				clc
				rts		
*--------------------------------------
.70				ldx #3

.71				lda ACC32,x				save ACC32 for 4+1
				pha
				dex
				bpl .71

				jsr MATH.ACC32x2		ACC32 * 2 -> ACC32
				jsr MATH.ACC32x2		ACC32 * 4 -> ACC32

				ldx #0
				ldy #4
*				clc

.72				pla						ACC32 * 4 + ACC32 -> ACC32
				adc ACC32,x
				sta ACC32,x
				inx
				dey
				bne .72

MATH.ACC32x2	asl ACC32
				rol ACC32+1
				rol ACC32+2
				rol ACC32+3
				rts
*--------------------------------------
MATH.CMPT		.DA #%100				L
				.DA #%110				LE
				.DA #%001				G
				.DA #%011				GE
				.DA #%010				E
				.DA #%101				NE
*--------------------------------------
MAN
SAVE usr/src/sys/kernel.s.math
LOAD usr/src/sys/kernel.s
ASM
