Fast PRNG for MSP430

Thread Starter

tom66

Joined May 9, 2009
2,595
I am looking for a fast PRNG for the MSP430x2x01 series, low power 16-bit microcontrollers.

The PRNG doesn't need to be especially random, just good enough. It will be used to stagger the startup of large devices (refrigerators, heaters, A/C, etc.) when first turned on, to reduce inrush current. The PRNG will be seeded with a guaranteed random number from a preprogrammed EEPROM. (It needs to be random every time, so I will be writing the seed back.)

I've had a look at LCGs but I'm not sure how to implement one in assembly.

Any help appreciated!
 

Thread Starter

tom66

Joined May 9, 2009
2,595
I gave it a shot, and I wrote a basic LFSR:

Rich (BB code):
LFSR_Rand:      ; LFSR random number generator
                ; This LFSR uses taps 16, 14, 13 and 11.
                ; Grab LSB and put it in R4.
                mov     RandSeed, R4
                mov     R4, R5
                and     #1, R4
                ; Shift LFSR right
                rla     R5
                ; XOR with poly 0xB400u.
                mov     #0xB400, R6
                xor     R5, R6
                mov     R5, RandSeed
                ret

GenRand:        ; Generate a PRNG result 0-255 from the LFSR and seed the LFSR
                ; Result pushed to stack and in R4
                ; Clobbers R4, R5 and R6
                mov     RandSeed, R4
                mov     #0x00FF, R5
                and     R4, R5
                ; push twice
                push    R4
                push    R4
                ; seed the LFSR, clobbers R4..R6
                call    LFSR_Rand
                pop     R4
                ret

DelayRand:      ; Delay a random amount of time
                ; Clobbers R4, R5 and R6
                call    GenRand         ; get a random number 0-255
                ; Multiply the number 8x
                rra     R4
                rra     R4
                rra     R4
                mov     #256, R5
                mov     R5, R6
DelayLoop:      dec     R4
                mov     R6, R5
                ; Loop 256 times inside inner loop
DelayInner:     cmp     R6, #0
                dec     R6
                jnz     DelayILoop
                ; Jump back to beginning if not 0
                cmp     R4, #0
                jnz     DelayLoop
Not tested yet!! But it should work.
 
Last edited:
Top