;************************************************************************** ; ; Filename: w_noise.asm ; ; Author: Ronald Bazillion ; ; Module Description: ; ; This module generates random noise uniformly distributed ; between -1 and +1. The random number generator implements ; Knuth's algorithm for random number generation which is: ; ; x(n) = ((a+c)*x(n-1)) mod m ; ; a =3 - multiplier; c =2^8 - increment; m =2^15 - modulus. ; The initial condition x(-1) = $1415. This algorithm ; generates random numbers between 0 and +1. To have it ; generate random numbers between -1 and +1 we shift it ; using this formula: ; ; y(n) = 2*(x(n) - .5) where .5 is the mean of x(n) ; ; The implementation of the mod function is: ; ; x - y * (floor(x/m) ; ; Since the DSP56307 real number format places the integer ; portion in the MSP of the accumulator and the fractional ; portion in the LSP, we simply implement the mod function ; by extracting the fractional portion in the LSP. This ; fractional portion is unsigned and MUST be converted to ; real number fractional notation before any procesing is ; done. ; ; ;************************************************************************** SEED equ $1415 org x: x_prev ds 1 org p: RNG move #(@CVI(@POW(2.0,15.0))),y0 ; m move #(@CVI(@POW(2.0,8.0)+3)),y1 ; (a+c) move #((@POW(2.0,-15.0))),x1 ; 1/m move x:x_prev,x0 mpy x0,y1,a asr a ; (a+c)*x(n-1) nop move a0,x0 mpy x0,x1,a ; ((a+c)*x(n-1)) / m ; a1 contains integer part ; a0 contains unsigned fractional part lsl a #.5,y0 ; convert to signed fractional form asr a move a0,a1 ; get signed fractional part nop move a1,b ; store x(n-1)/2^8 signed fractional form rep #8 ; shift b1 8 bits to the right lsr b ; to extract x(n-1) from fractional value. nop sub y0,a b1,x:x_prev ; x(n) - .5; store x(n-1) = x(n) asl a ; 2(x(n) - .5) rts ;----------------------------------------------------------------------- ; ; Function Name: RNG_INIT ; ; Description: This function is used to initialize the RNG routine. ; ;----------------------------------------------------------------------- RNG_INIT move #SEED,x0 ;Load initial condition x(-1) move x0,x:x_prev rts