lfsr.c
Go to the documentation of this file.
1 /******************************************************************************/
38 /******************************************************************************/
39 
40 #include "lfsr.h"
41 
42 void
44  uint16_t seed,
45  lfsr_t *instance
46 ) {
47  instance->start_state = seed;
48  instance->lfsr_value = seed;
49 }
50 
54 uint16_t
56  lfsr_t *instance
57 ) {
58  unsigned lsb = instance->lfsr_value & 1;/* Get LSB (i.e., the output bit). */
59 
60  instance->lfsr_value >>= 1; /* Shift register */
61 
62  if (lsb == 1) {
63  /* Only apply toggle mask if output bit is 1 */
64  instance->lfsr_value ^= 0xB400u;/* Apply toggle mask, value has 1 at bits corresponding
65  * to taps, 0 elsewhere. */
66  }
67 
68  return instance->lfsr_value;
69 }
70 
74 void
76  lfsr_t *instance
77 ) {
78  instance->lfsr_value = instance->start_state;
79 }
void lfsr_init_start_state(uint16_t seed, lfsr_t *instance)
Initialize the seed for the random number generator.
Definition: lfsr.c:43
uint16_t lfsr_get_next(lfsr_t *instance)
Generate the next random number.
Definition: lfsr.c:55
void lfsr_reset(lfsr_t *instance)
Reset the random number generator to it's initial state.
Definition: lfsr.c:75
uint16_t lfsr_value
Definition: lfsr.h:54
An instance type for the random number generator.
Definition: lfsr.h:52
uint16_t start_state
Definition: lfsr.h:53
A linear-feedback shift register pseudo-random number generator.