; File testmath.asm
; generated by the Basic DSP compiler Ver 0.10
; 1999/9/23, 18:14:4.72

; standard program header for the C31 DSK
	.start ".text", 0x809802 ; RAM block 0
	.start ".data", 0x809C00 ; RAM block 1
ten	.set	10	; const ten 10


; start of the data section
	.data
y	.float	0	; var y float
z	.float	0	; var z float
k	.word	0	; var k int
maxcounter	.word	100000	; var maxcounter int     100000
histogram	; var histogram  int(10)      0
	.loop	10
	.word	0
	.endloop
fmaxminsrda	.float	0,0,0,0	; var fmaxminsrda  float 0,0,0,0
imaxminsrda	.word	0,0,0,0	; var imaxminsrda  int   0,0,0,0
fsqrtsrda	.float	0,0,0	; var fsqrtsrda float 0,0,0
finvsrda	.float	0,0	; var finvsrda  float 0,0
finvmask	.word	0xff7fffff	; var finvmask  int 0xff7fffff
idivsrda	.word	0, 0, 0	; var idivsrda int 0, 0, 0
flog2srda	.float	0,0	; var flog2srda float 0,0
flog2msk1	.word	0xff000000	; var flog2msk1 int 0xff000000
flog2msk2	.word	0x00fe0000	; var flog2msk2 int 0x00fe0000
flog2msk3	.word	0x0001fc00	; var flog2msk3 int 0x0001fc00
flog2msk4	.word	0x000003ff	; var flog2msk4 int 0x000003ff
flog2sc	.float	pow(2.0,-24.0)	; var flog2sc   float pow(2.0,-24.0)
polyvalsrda	.float	10, 0, 6	; var  polyvalsrda float 10, 0, 6
polycoeffs	.float	1,2,3,4,5,6	; var ~polycoeffs  float 1,2,3,4,5,6
fsinsrda	.float	1,0	; var fsinsrda float 1,0
fcossrda	.float	1,0	; var fcossrda float 1,0
pi	.float	+3.14159265358e0	; var pi      float  +3.14159265358e0
dpi	.float	+6.28318530718e0	; var dpi     float  +6.28318530718e0
pisd	.float	+1.57079632679e0	; var pisd    float  +1.57079632679e0
zu	.float	+0.06250000000e0	; var zu      float  +0.06250000000e0
polysinsrda	.float	0.0, 0.0, 6	; var  polysinsrda  float  0.0, 0.0, 6
polysinsrda5	.float	-2.626903960237294e-2	; var ~polysinsrda5 float -2.626903960237294e-2
polysinsrda4	.float	1.805996472663140e-1	; var ~polysinsrda4 float  1.805996472663140e-1
polysinsrda3	.float	-8.126984126984127e-1	; var ~polysinsrda3 float -8.126984126984127e-1
polysinsrda2	.float	2.133333333333333e+0	; var ~polysinsrda2 float  2.133333333333333e+0
polysinsrda1	.float	-2.666666666666667e+0	; var ~polysinsrda1 float -2.666666666666667e+0
polysinsrda0	.float	1.000000000000000e+0	; var ~polysinsrda0 float  1.000000000000000e+0
irandsrda	.word	0, 0x0107465, 0x0234567	; var irandsrda int 0, 0x0107465, 0x0234567
iprbssrda	.word	0x7e521603	; var iprbssrda int 0x7e521603
frandjasrda	.float	0.0, 1.229	; var frandjasrda float 0.0, 1.229

; start of the data address section
_pbase	.word	0x808000 ; memory mapped registers
_y	.word	y	; address of y
_z	.word	z	; address of z
_k	.word	k	; address of k
_maxcounter	.word	maxcounter	; address of maxcounter
_histogram	.word	histogram	; address of histogram
_fmaxminsrda	.word	fmaxminsrda	; address of fmaxminsrda
_imaxminsrda	.word	imaxminsrda	; address of imaxminsrda
_fsqrtsrda	.word	fsqrtsrda	; address of fsqrtsrda
_finvsrda	.word	finvsrda	; address of finvsrda
_finvmask	.word	finvmask	; address of finvmask
_idivsrda	.word	idivsrda	; address of idivsrda
_flog2srda	.word	flog2srda	; address of flog2srda
_flog2msk1	.word	flog2msk1	; address of flog2msk1
_flog2msk2	.word	flog2msk2	; address of flog2msk2
_flog2msk3	.word	flog2msk3	; address of flog2msk3
_flog2msk4	.word	flog2msk4	; address of flog2msk4
_flog2sc	.word	flog2sc	; address of flog2sc
_polyvalsrda	.word	polyvalsrda	; address of polyvalsrda
_fsinsrda	.word	fsinsrda	; address of fsinsrda
_fcossrda	.word	fcossrda	; address of fcossrda
_pi	.word	pi	; address of pi
_dpi	.word	dpi	; address of dpi
_pisd	.word	pisd	; address of pisd
_zu	.word	zu	; address of zu
_polysinsrda	.word	polysinsrda	; address of polysinsrda
_irandsrda	.word	irandsrda	; address of irandsrda
_iprbssrda	.word	iprbssrda	; address of iprbssrda
_frandjasrda	.word	frandjasrda	; address of frandjasrda

_stack	.word stack	; address of stack
stack	.word $		; beginning of the stack
; end of the data section

; start of the program section
	.entry BEGIN
	.text
BEGIN	ldp	@.data     ; init the data page
	ldi	@stack, sp ; init the stack pointer
main	; :main
	ldi	0, R6	; i2=0

programend	; :programend
	br	programend	; goto programend

test_frandja	; :test_frandja
	push	ar0	; gosub frandja
	ldi	@_frandjasrda, ar0
	call	frandja
	pop	ar0

	ldi	1, R0	; i2+=1
	addi3	R0, R6, R6

	ldi	@_frandjasrda, AR1	; x0=frandjasrda(0)
	ldf	*AR1, R0
	ldf	R0, R4

	ldf	10, R0	; x0*=10
	mpyf3	R0, R4, R4

	ldf	R4, R0	; i1=x0
	fix	R0, R0
	ldi	R0, R5

	ldi	@_histogram, AR1	; histogram(i1)+=1
	ldi	R5, IR0
	ldi	1, R0
	addi3	R0, *+AR1(IR0), R0
	sti	R0, *+AR1(IR0)

	ldi	R6, R0	; if i2<maxcounter thengoto test_frandja
	ldi	@_maxcounter, AR1
	ldi	*AR1, R1
	cmpi	R1, R0
	blt	test_frandja

	br	programend	; goto programend

test_idiv	; :test_idiv
	ldi	@_idivsrda, AR1	; idivsrda(0)=100
	ldi	100, R0
	sti	R0, *AR1

	ldi	@_idivsrda, AR1	; idivsrda(1)=15
	ldi	1, IR0
	ldi	15, R0
	sti	R0, *+AR1(IR0)

	push	ar0	; gosub idiv
	ldi	@_idivsrda, ar0
	call	idiv
	pop	ar0

	ldi	@_idivsrda, AR1	; idivsrda(0)=100
	ldi	100, R0
	sti	R0, *AR1

	ldi	@_idivsrda, AR1	; idivsrda(1)=-15
	ldi	1, IR0
	ldi	-15, R0
	sti	R0, *+AR1(IR0)

	push	ar0	; gosub idiv
	ldi	@_idivsrda, ar0
	call	idiv
	pop	ar0

	ldi	@_idivsrda, AR1	; idivsrda(0)= 10
	ldi	10, R0
	sti	R0, *AR1

	ldi	@_idivsrda, AR1	; idivsrda(1)=15
	ldi	1, IR0
	ldi	15, R0
	sti	R0, *+AR1(IR0)

	push	ar0	; gosub idiv
	ldi	@_idivsrda, ar0
	call	idiv
	pop	ar0

	ldi	@_idivsrda, AR1	; idivsrda(0)=100
	ldi	100, R0
	sti	R0, *AR1

	ldi	@_idivsrda, AR1	; idivsrda(1)=0
	ldi	1, IR0
	ldi	0, R0
	sti	R0, *+AR1(IR0)

	push	ar0	; gosub idiv
	ldi	@_idivsrda, ar0
	call	idiv
	pop	ar0

	ldi	@_idivsrda, AR1	; idivsrda(0)=5432
	ldi	5432, R0
	sti	R0, *AR1

	ldi	@_idivsrda, AR1	; idivsrda(1)=23
	ldi	1, IR0
	ldi	23, R0
	sti	R0, *+AR1(IR0)

	push	ar0	; gosub idiv
	ldi	@_idivsrda, ar0
	call	idiv
	pop	ar0

	ldi	@_idivsrda, AR1	; finvsrda(0)=idivsrda(1)
	ldi	1, IR0
	ldi	*+AR1(IR0), R0
	float	R0, R0
	ldi	@_finvsrda, AR1
	stf	R0, *AR1

	push	ar0	; gosub finv
	ldi	@_finvsrda, ar0
	call	finv
	pop	ar0

	ldi	@_finvsrda, AR1	; k=finvsrda(1)*idivsrda(0)
	ldi	1, IR0
	ldf	*+AR1(IR0), R0
	ldi	@_idivsrda, AR1
	ldi	*AR1, R1
	float	R1, R1
	mpyf3	R1, R0, R0
	fix	R0, R0
	ldi	@_k, AR1
	sti	R0, *AR1

	br	programend	; goto programend

test_rand	; :test_rand
	push	ar0	; gosub irand
	ldi	@_irandsrda, ar0
	call	irand
	pop	ar0

	push	ar0	; gosub irand
	ldi	@_irandsrda, ar0
	call	irand
	pop	ar0

	push	ar0	; gosub irand
	ldi	@_irandsrda, ar0
	call	irand
	pop	ar0

	push	ar0	; gosub iprbs
	ldi	@_iprbssrda, ar0
	call	iprbs
	pop	ar0

	push	ar0	; gosub iprbs
	ldi	@_iprbssrda, ar0
	call	iprbs
	pop	ar0

	push	ar0	; gosub iprbs
	ldi	@_iprbssrda, ar0
	call	iprbs
	pop	ar0

	br	programend	; goto programend

test_math	; :test_math
	ldi	@_fmaxminsrda, AR1	; fmaxminsrda(0)=1
	ldf	1, R0
	stf	R0, *AR1

	ldi	@_fmaxminsrda, AR1	; fmaxminsrda(1)=3
	ldi	1, IR0
	ldf	3, R0
	stf	R0, *+AR1(IR0)

	push	ar0	; gosub fmaxmin
	ldi	@_fmaxminsrda, ar0
	call	fmaxmin
	pop	ar0

	ldi	@_fmaxminsrda, AR1	; y=fmaxminsrda(2)
	ldi	2, IR0
	ldf	*+AR1(IR0), R0
	ldi	@_y, AR1
	stf	R0, *AR1

	ldi	@_fmaxminsrda, AR1	; z=fmaxminsrda(3)
	ldi	3, IR0
	ldf	*+AR1(IR0), R0
	ldi	@_z, AR1
	stf	R0, *AR1

	ldi	@_fsqrtsrda, AR1	; fsqrtsrda(0)=4
	ldf	4, R0
	stf	R0, *AR1

	push	ar0	; gosub fsqrt
	ldi	@_fsqrtsrda, ar0
	call	fsqrt
	pop	ar0

	ldi	@_fsqrtsrda, AR1	; z=fsqrtsrda(1)
	ldi	1, IR0
	ldf	*+AR1(IR0), R0
	ldi	@_z, AR1
	stf	R0, *AR1

	ldi	@_finvsrda, AR1	; finvsrda(0)=10
	ldf	10, R0
	stf	R0, *AR1

	push	ar0	; gosub finv
	ldi	@_finvsrda, ar0
	call	finv
	pop	ar0

	ldi	@_finvsrda, AR1	; z=finvsrda(1)
	ldi	1, IR0
	ldf	*+AR1(IR0), R0
	ldi	@_z, AR1
	stf	R0, *AR1

	ldi	@_flog2srda, AR1	; flog2srda(0)=16
	ldf	16, R0
	stf	R0, *AR1

	push	ar0	; gosub flog2
	ldi	@_flog2srda, ar0
	call	flog2
	pop	ar0

	ldi	@_flog2srda, AR1	; z=flog2srda(1)
	ldi	1, IR0
	ldf	*+AR1(IR0), R0
	ldi	@_z, AR1
	stf	R0, *AR1

	ldi	@_pi, AR1	; fsinsrda(0)=pi
	ldf	*AR1, R0
	ldi	@_fsinsrda, AR1
	stf	R0, *AR1

	ldi	@_fsinsrda, AR1	; fsinsrda(0)*=0.25
	ldf	0.25, R0
	mpyf3	R0,*AR1, R0
	stf	R0, *AR1

	push	ar0	; gosub fsin
	ldi	@_fsinsrda, ar0
	call	fsin
	pop	ar0

	ldi	@_fsinsrda, AR1	; z=fsinsrda(1)
	ldi	1, IR0
	ldf	*+AR1(IR0), R0
	ldi	@_z, AR1
	stf	R0, *AR1

	ldi	@_pi, AR1	; fcossrda(0)=pi
	ldf	*AR1, R0
	ldi	@_fcossrda, AR1
	stf	R0, *AR1

	ldi	@_fcossrda, AR1	; fcossrda(0)*=0.25
	ldf	0.25, R0
	mpyf3	R0,*AR1, R0
	stf	R0, *AR1

	push	ar0	; gosub fcos
	ldi	@_fcossrda, ar0
	call	fcos
	pop	ar0

	ldi	@_fcossrda, AR1	; z=fcossrda(1)
	ldi	1, IR0
	ldf	*+AR1(IR0), R0
	ldi	@_z, AR1
	stf	R0, *AR1

	br	programend	; goto programend

fmaxmin	; :fmaxmin
	ldf     *+ar0(0),r0
	|| ldf     *+ar0(1),r1
	cmpf    r1,r0
	bp      fmaxmin1
	stf     r0,*+ar0(3)
	stf     r1,*+ar0(2)
	rets	; return

fmaxmin1	; :fmaxmin1
	stf     r0,*+ar0(2)
	stf     r1,*+ar0(3)
	rets	; return

imaxmin	; :imaxmin
	ldi     *+ar0(0),r0
	|| ldi     *+ar0(1),r1
	cmpi    r1,r0
	bp      imaxmin1
	sti     r0,*+ar0(3)
	sti     r1,*+ar0(2)
	rets	; return

imaxmin1	; :imaxmin1
	sti     r0,*+ar0(2)
	sti     r1,*+ar0(3)
	rets	; return

fsqrt	; :fsqrt
	ldf	*+ar0(0),r0
	cmpf	0.0,r0
	ldflt	-1,r0
	ldfeq	0.0,r0
	stf	r0,*+ar0(1)
	retsle
	ldf     r0,r1
	lsh     1,r1
	pushf   r1
	pop     r1
	not     r1,r1
	ash     -1,r1
	push    r1
	popf    r1
	lsh     -1,r1
	mpyf    0.5,r0
	ldi     5,rc
	rptb    fsqrt2
	mpyf3   r1,r1,r2
	mpyf    r0,r2
	subrf   1.5,r2
fsqrt2	; :fsqrt2
	mpyf    r2,r1
	rnd     r1,r1
	stf     r1,*+ar0(2)
	mpyf    r1,r0
	mpyf    2.0,r0
	stf     r0,*+ar0(1)
	rets	; return

finv	; :finv
	ldf     *+ar0(0),r0
	cmpf    0.0,r0
	beq	finv3
	ldf     r0,r1
	pushf   r1
	pop     r1
	xor     @finvmask,r1
	push    r1
	popf    r1
	ldi     5,rc
	rptb    finv2
	mpyf3   r1,r0,r2
	subrf   2.0,r2
finv2	; :finv2
	mpyf    r2,r1
	ldf     r1,r0
	stf     r0,*+ar0(1)
	rets	; return

finv3	; :finv3
	ldf	0.0, r0
	stf     r0,*+ar0(1)
	rets	; return

idiv	; :idiv
	ldi	*ar0,r0
	|| ldi	*+ar0(1),r1
	xor3	r0,r1,r2
	absi	r0,r0
	absi	r1,r1
	bzd	idiv3
	cmpi	r0,r1
	bgtd	idiv2
	float	r0, r3
	pushf	r3
	pop	ir1
	lsh	-24,ir1
	float	r1, r3
	pushf	r3
	pop	ir0
	lsh	-24,ir0
	subi	ir0,ir1
	lsh	ir1,r1
	rpts	ir1
	subc	r1,r0
	subri	31,ir1
	lsh	ir1,r0
	negi	ir1,ir1
	lsh	ir1,r0
	negi	r0,r1
	ash	-31,r2
	ldinz	r1,r0
	sti	r0,*+ar0(2)
	rets	; return

idiv2	; :idiv2
	ldi	0,r0
	sti	r0,*+ar0(2)
	rets	; return

idiv3	; :idiv3
	ash	-31,r2
	ldinz	0x8000,r0
	ldinz	0x0000,r1
	ldiz	0x07ff,r0
	ldiz	0xffff,r1
	lsh	16,r0
	addi	r1,r0
	sti	r0,*+ar0(2)
	rets	; return

flog2	; :flog2
	ldf     *+ar0(0),r0
	cmpf    0.0,r0
	ble	flog2c
	ldi     2,rc
	rptb    flog2b
	pushf   r0
	lde     1.0,r0
	mpyf    r0,r0
	mpyf    r0,r0
	mpyf    r0,r0
	mpyf    r0,r0
	mpyf    r0,r0
	mpyf    r0,r0
flog2b	; :flog2b
	mpyf    r0,r0
	pushf   r0
	pop     r0
	lsh     -21,r0
	and     @flog2msk4,r0
	pop     r1
	lsh     -14,r1
	and     @flog2msk3,r1
	or      r1,r0
	pop     r1
	lsh     -7,r1
	and     @flog2msk2,r1
	or      r1,r0
	pop     r1
	and     @flog2msk1,r1
	or      r1,r0
	float   r0,r0
	mpyf    @flog2sc,r0
	stf     r0,*+ar0(1)
	rets	; return

flog2c	; :flog2c
	ldf	-1,r0
	stf	r0, *+ar0(1)
	rets	; return

fpolyval	; :fpolyval
	ldi	ar0, ar1
	ldf	*++ar1(3), r0
	ldf	*+ar0(2), r1
	fix	r1, r1
	cmpi	1, r1
	bz	fpolyval3
	subi	2, r1
	ldi	r1, rc
	rptb	fpolyval2
	mpyf	*ar0, r0
fpolyval2	; :fpolyval2
	addf	*++ar1, r0
fpolyval3	; :fpolyval3
	stf	r0, *+ar0(1)
	rets	; return

fsin	; :fsin
	ldf     *ar0,r3
	bn      fsin3
fsin2	; :fsin2
	cmpf    @pi,r3
	bn      fsin4
	subf    @dpi,r3
	br      fsin2
fsin3	; :fsin3
	ldf     @pi,r2
	negf    r2,r2
	cmpf    r2,r3
	bp      fsin4
	addf    @dpi,r3
	br      fsin3
fsin4	; :fsin4
	cmpf    @pisd,r3
	bn      fsin5
	ldf     @pi,r2
	subf3   r3,r2,r3
fsin5	; :fsin5
	negf    @pisd,r2
	cmpf    r2,r3
	bp      fsin6
	ldf     @pi,r2
	negf    r2,r2
	subf3   r3,r2,r3
fsin6	; :fsin6
	push    ar0
	ldi     @_polysinsrda,ar0
	mpyf3   r3,r3,r2
	mpyf    @zu,r2
	stf     r2,*+ar0(0)
	call    fpolyval
	ldf     *+ar0(1),r0
	pop     ar0
	mpyf    r3,r0
	stf     r0,*+ar0(1)
	rets	; return

fcos	; :fcos
	push    ar0
	ldf     *+ar0(0),r0
	addf    @pisd,r0
	ldi     @_fsinsrda,ar0
	stf     r0,*+ar0(0)
	call    fsin
	ldf     *+ar0(1),r0
	pop     ar0
	stf     r0,*+ar0(1)
	rets	; return

irand	; :irand
	ldi	ar0, ar1
	ldi     *ar1++, r0
	mpyi    *ar1++, r0
	addi    *ar1, r0
	sti     r0,*ar0
	rets	; return

iprbs	; :iprbs
	ldi	*ar0, r0
	|| ldi	*ar0, r1
	ldi	*ar0, r2
	lsh	-17, r2
	lsh	-28, r1
	xor	r1, r2
	lsh	-2, r1
	xor	r1, r2
	lsh	-1, r1
	xor	r1, r2
	and	1, r2
	lsh	1, r0
	or	r2, r0
	sti	r0, *ar0
	rets	; return

frandja	; :frandja
	ldf	*ar0, r0
	addf	*+ar0(1),r0
	mpyf3	r0,r0,r1
	mpyf3	r1,r1,r0
	mpyf3	r0,r0,r1
	fix	r1,r0
	float	r0,r0
	subf	r0,r1
	stf	r1,*ar0
	rets	; return


; end of the program section
	.end
