Skip to content

Commit

Permalink
Initial commit: Code from January 2011
Browse files Browse the repository at this point in the history
Working implementation of SLAA338 and an LCG, with a very simple test.
  • Loading branch information
0 committed Apr 3, 2011
0 parents commit d1e10ba
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CC = msp430-gcc
CFLAGS = -Wall -Wno-main -Os -g -mmcu=msp430x2211

OBJS=rand.o

all: $(OBJS)
$(CC) $(CFLAGS) -o main.elf $(OBJS)

%.o: %.c
$(CC) $(CFLAGS) -c $<

clean:
rm -f main.elf $(OBJS)
89 changes: 89 additions & 0 deletions rand.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <io.h>

/**
* Random number generator.
*
* NOTE: This affects Timer A.
*
* Algorithm from TI SLAA338:
* http://www.ti.com/sc/docs/psheets/abstract/apps/slaa338.htm
*
* @return 16 random bits generated from a hardware source.
*/
unsigned int rand() {
int i, j;
unsigned int result = 0;

TACCTL0 = CAP | CM_1 | CCIS_1; // Capture mode, positive edge
TACTL = TASSEL_2 | MC_2; // SMCLK, continuous up

for (i = 0; i < 16; i++) {
unsigned int ones = 0;

for (j = 0; j < 5; j++) {
while (!(CCIFG & TACCTL0)); // Wait for interrupt

TACCTL0 &= ~CCIFG; // Clear interrupt

if (1 & TACCR0) // If LSB set, count it
ones++;
}

result >>= 1; // Save previous bits

if (ones >= 3) // Best out of 5
result |= 0x8000; // Set MSB
}

return result;
}

#define M 49381 // Multiplier
#define I 8643 // Increment

/**
* Pseudo-random number generator.
*
* 16-bit linear congruential generator.
* NOTE: Only treat the upper byte of the return value as random.
*
* @param state Previous state of the generator.
* @return Next state of the generator.
*/
unsigned int prand(unsigned int state) {
return (M * state + I); // Generate the next state of the LCG
}

/*
* Test:
*/

#define TIMES 32

/**
* Red light on => Waiting for rand() to return
* Green light on => Done
*/
void main() {
volatile unsigned int result[TIMES];
unsigned int s;
int i;

P1DIR |= BIT0 | BIT6;
P1OUT |= BIT0; // R
P1OUT &= ~BIT6; // !G

s = rand();
result[0] = s;

P1OUT &= ~BIT0; // !R

for (i = 1; i < TIMES; i++) {
s = prand(s);
result[i] = s;
}

P1OUT |= BIT6; // G

asm("mov %0, r8" : : "r" (&result)); // Manually check TIMES words starting at address in r8
}

0 comments on commit d1e10ba

Please sign in to comment.