math.asm
drmortalwombat_plekthora/math.asm

!zone	Math

.expl	!byte	0, 5, 11, 17, 23, 29, 35, 41, 48, 55, 61, 68, 75, 83, 90, 98, 106, 113, 122, 130, 138, 147, 156, 165, 174, 183, 193, 203, 213, 223, 234, 245, 0, 11, 22, 34, 46, 58, 71, 83, 96, 110, 123, 137, 151, 166, 181, 196, 212, 227, 244, 4, 21, 38, 56, 74, 93, 111, 131, 150, 171, 191, 212, 234, 0, 22, 45, 68, 92, 117, 142, 167, 193, 220, 247, 19, 47, 77, 106, 137, 168, 199, 232, 9, 43, 77, 113, 149, 186, 223, 6, 45, 86, 127, 169, 212, 0, 44, 90, 137, 185, 234, 28, 79, 131, 184, 239, 39, 95, 154, 213, 18, 80, 143, 208, 18, 86, 155, 226, 42, 116, 191, 12, 91, 172, 254, 82, 168, 0, 89, 181, 19, 114, 212, 56, 158, 6, 113, 222, 78, 191, 52, 171, 36, 160, 31, 161, 37, 172, 55, 196, 85, 232, 127, 25, 183, 88, 252, 164, 80, 0, 179, 106, 38, 229, 169, 112, 61, 13, 227, 189, 156, 127, 104, 86, 72, 65, 62, 66, 75, 89, 110, 137, 170, 209, 254, 51, 110, 176, 249, 73, 160, 0, 102, 213, 76, 202, 82, 225, 122, 27, 198, 122, 56, 255, 208, 172, 145, 130, 125, 132, 150, 179, 220, 18, 84, 162, 253, 102, 220, 96, 242, 146, 65, 0, 205, 170, 152, 149, 164, 195, 244, 55, 141, 245, 112, 254, 161, 88, 35, 4, 251, 8, 44, 103, 185, 36, 168, 68, 251, 204, 185, 192, 228, 37, 131
.exph	!byte	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 19, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 28, 28, 29, 29, 30, 31, 32, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 56, 57, 58, 59, 61, 62, 64, 65, 66, 68, 69, 71, 72, 74, 76, 77, 79, 81, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 103, 105, 107, 109, 112, 114, 117, 119, 122, 125, 128, 130, 133, 136, 139, 142, 145, 148, 152, 155, 158, 162, 165, 169, 173, 177, 181, 184, 189, 193, 197, 201, 206, 210, 215, 219, 224, 229, 234, 239, 245, 250
.logl	!byte	0, 0, 0, 184, 0, 77, 184, 213, 0, 112, 77, 179, 184, 106, 213, 5, 0, 204, 112, 239, 77, 141, 179, 193, 184, 154, 106, 40, 213, 116, 5, 136, 0, 107, 204, 35, 112, 179, 239, 34, 77, 113, 141, 163, 179, 189, 193, 191, 184, 171, 154, 132, 106, 75, 40, 0, 213, 167, 116, 62, 5, 200, 136, 69, 0, 183, 107, 29, 204, 121, 35, 202, 112, 19, 179, 82, 239, 137, 34, 184, 77, 224, 113, 0, 141, 25, 163, 44, 179, 57, 189, 63, 193, 64, 191, 60, 184, 50, 171, 35, 154, 16, 132, 247, 106, 219, 75, 186, 40, 148, 0, 107, 213, 62, 167, 14, 116, 218, 62, 162, 5, 103, 200, 41, 136, 231, 69, 163, 0, 91, 183, 17, 107, 196, 29, 117, 204, 35, 121, 206, 35, 119, 202, 29, 112, 193, 19, 99, 179, 3, 82, 161, 239, 60, 137, 214, 34, 109, 184, 3, 77, 150, 224, 40, 113, 184, 0, 71, 141, 212, 25, 95, 163, 232, 44, 112, 179, 246, 57, 123, 189, 254, 63, 128, 193, 1, 64, 128, 191, 253, 60, 122, 184, 245, 50, 111, 171, 231, 35, 95, 154, 213, 16, 74, 132, 190, 247, 49, 106, 162, 219, 19, 75, 130, 186, 241, 40, 94, 148, 203, 0, 54, 107, 160, 213, 10, 62, 115, 167, 218, 14, 65, 116, 167, 218, 12, 62, 112, 162, 211, 5, 54, 103, 152, 200, 248, 41, 89, 136, 184, 231, 22, 69, 116, 163, 209
.logh	!byte	0, 0, 32, 50, 64, 74, 82, 89, 96, 101, 106, 110, 114, 118, 121, 125, 128, 130, 133, 135, 138, 140, 142, 144, 146, 148, 150, 152, 153, 155, 157, 158, 160, 161, 162, 164, 165, 166, 167, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 185, 186, 187, 188, 189, 189, 190, 191, 192, 192, 193, 194, 194, 195, 196, 196, 197, 198, 198, 199, 199, 200, 201, 201, 202, 202, 203, 204, 204, 205, 205, 206, 206, 207, 207, 208, 208, 209, 209, 210, 210, 211, 211, 212, 212, 213, 213, 213, 214, 214, 215, 215, 216, 216, 217, 217, 217, 218, 218, 219, 219, 219, 220, 220, 221, 221, 221, 222, 222, 222, 223, 223, 224, 224, 224, 225, 225, 225, 226, 226, 226, 227, 227, 227, 228, 228, 228, 229, 229, 229, 230, 230, 230, 231, 231, 231, 231, 232, 232, 232, 233, 233, 233, 234, 234, 234, 234, 235, 235, 235, 236, 236, 236, 236, 237, 237, 237, 237, 238, 238, 238, 238, 239, 239, 239, 239, 240, 240, 240, 241, 241, 241, 241, 241, 242, 242, 242, 242, 243, 243, 243, 243, 244, 244, 244, 244, 245, 245, 245, 245, 245, 246, 246, 246, 246, 247, 247, 247, 247, 247, 248, 248, 248, 248, 249, 249, 249, 249, 249, 250, 250, 250, 250, 250, 251, 251, 251, 251, 251, 252, 252, 252, 252, 252, 253, 253, 253, 253, 253, 253, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255
;.asr5	!byte	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
;.asl3	!byte	0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248
.sin20	!byte	0, 2, 4, 6, 8, 9, 11, 13, 14, 15, 17, 18, 18, 19, 20, 20, 20, 20, 20, 19, 18, 18, 17, 15, 14, 13, 11, 9, 8, 6, 4, 2, 0, -2, -4, -6, -8, -9, -11, -13, -14, -15, -17, -18, -18, -19, -20, -20, -20, -20, -20, -19, -18, -18, -17, -15, -14, -13, -11, -9, -8, -6, -4, -2

.asr5	=	$ec00
.asl3 	=	$ed00

.Init
	ldx	#0
-
	txa
	asl
	asl
	asl
	sta	.asl3, x
	txa
	clc
	adc	#$80
	lsr
	lsr
	lsr
	lsr
	lsr
	sec
	sbc	#$04
	sta	.asr5, x
	inx
	bne	-
	
	rts

; divide x by y

; mul x by y -> xa

.umul8x8
	cpx	#0
	beq	.mzero
	cpy	#0
	beq	.mzero
	
	clc
	lda	.logl,x
	adc	.logl,y
	lda	.logh,x
	adc	.logh,y
	tay
	bcs	+
	
	lda	.exph,y
	ldx	#0
	rts
	
+
	lda	.expl,y
	ldx	.exph,y
	rts	
	
.mzero	lda	#0
	ldx	#0
	rts
	
; divide x by y -> x,a

.udiv8x8
	cpx	#0
	beq	.dzero
	cpy	#0
	beq	.dinf
	
	sec
	lda	.logl,x
	sbc	.logl,y
	lda	.logh,x
	sbc	.logh,y
	bcc	.mzero
	
	tay
	lda	.expl,y
	ldx	.exph,y
	rts
	
.dinf
	lda	#$ff
	ldx	#$ff
	rts	
	
.dzero	lda	#0
	ldx	#0
	rts

; negate xa
	
.neg16
	eor	#$ff
	tay
	txa
	eor	#$ff
	tax
	
	iny
	bne	+
	inx
+
	tya
	rts
	
; mul x by y -> xa signed


.smul8x8
	txa
	bmi	.nx
	tya
	bmi	.ny
	
	jmp	.umul8x8
	
.nx
	eor	#$ff
	tax
	inx
	
	tya
	bmi	.nxy
	
	jsr	.umul8x8
	jmp	.neg16
	
.ny
	eor	#$ff
	tay
	iny

	jsr	.umul8x8
	jmp	.neg16
	
.nxy
	eor	#$ff
	tay
	iny

	jmp	.umul8x8

; mul x by y divide by a
	
.umul8x8div8
	cpx	#0
	beq	.mdzero
	cpy	#0
	beq	.mdzero
	
	pha

	clc
	lda	.logl,x
	adc	.logl,y
	sta	tmpml
	lda	.logh,x
	adc	.logh,y
	sta	tmpmh
	
	pla
	tax
	
	bcs	.mdupper
	
	sec
	lda	tmpml
	sbc	.logl,x
	lda	tmpmh
	sbc	.logh,x
	
	bcc	.mdzero
	
.mdlower	
	tay
	lda	.exph,y
	ldx	#0
	rts
	
.mdupper
	lda	tmpml
	sbc	.logl,x
	lda	tmpmh
	sbc	.logh,x
	
	bcc	.mdlower

	lda	.expl,y
	ldx	.exph,y
	rts	
	
.mdzero	lda	#0
	ldx	#0
	rts
	
; mul signed x by unsinged y divide by unsigend a

.smul8x8div8
	cpx	#$80
	bcs	.smdnx
	jmp	.umul8x8div8
.smdnx
	pha
	txa
	eor	#$ff
	tax
	inx
	pla
	jsr	.umul8x8div8
	jmp	.neg16




