Fast PRNG for MSP430

Discussion in 'Programmer's Corner' started by tom66, Jul 31, 2010.

  1. tom66

    Thread Starter Senior Member

    May 9, 2009
    2,613
    214
    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!
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    PRNG = Pseudo Random Number Generator.

    hgmjr
     
  3. tom66

    Thread Starter Senior Member

    May 9, 2009
    2,613
    214
    Correct... what is your point?
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I was concerned that some of our less knowledgeable members would not recognize the acronym.

    hgmjr
     
  5. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Did you see this application?

    hgmjr
     
  6. tom66

    Thread Starter Senior Member

    May 9, 2009
    2,613
    214
    I gave it a shot, and I wrote a basic LFSR:

    Code ( (Unknown Language)):
    1.  
    2. LFSR_Rand:      ; LFSR random number generator
    3.                 ; This LFSR uses taps 16, 14, 13 and 11.
    4.                 ; Grab LSB and put it in R4.
    5.                 mov     RandSeed, R4
    6.                 mov     R4, R5
    7.                 and     #1, R4
    8.                 ; Shift LFSR right
    9.                 rla     R5
    10.                 ; XOR with poly 0xB400u.
    11.                 mov     #0xB400, R6
    12.                 xor     R5, R6
    13.                 mov     R5, RandSeed
    14.                 ret
    15.  
    16. GenRand:        ; Generate a PRNG result 0-255 from the LFSR and seed the LFSR
    17.                 ; Result pushed to stack and in R4
    18.                 ; Clobbers R4, R5 and R6
    19.                 mov     RandSeed, R4
    20.                 mov     #0x00FF, R5
    21.                 and     R4, R5
    22.                 ; push twice
    23.                 push    R4
    24.                 push    R4
    25.                 ; seed the LFSR, clobbers R4..R6
    26.                 call    LFSR_Rand
    27.                 pop     R4
    28.                 ret
    29.  
    30. DelayRand:      ; Delay a random amount of time
    31.                 ; Clobbers R4, R5 and R6
    32.                 call    GenRand         ; get a random number 0-255
    33.                 ; Multiply the number 8x
    34.                 rra     R4
    35.                 rra     R4
    36.                 rra     R4
    37.                 mov     #256, R5
    38.                 mov     R5, R6
    39. DelayLoop:      dec     R4
    40.                 mov     R6, R5
    41.                 ; Loop 256 times inside inner loop
    42. DelayInner:     cmp     R6, #0
    43.                 dec     R6
    44.                 jnz     DelayILoop
    45.                 ; Jump back to beginning if not 0
    46.                 cmp     R4, #0
    47.                 jnz     DelayLoop
    48.  
    Not tested yet!! But it should work.
     
    Last edited: Jul 31, 2010
  7. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Here is the definition of the acronym LFSR.

    hgmjr
     
Loading...