-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit: Code from January 2011
Working implementation of SLAA338 and an LCG, with a very simple test.
- Loading branch information
0 parents
commit d1e10ba
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |