-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlfsr.c
More file actions
142 lines (104 loc) · 3.65 KB
/
lfsr.c
File metadata and controls
142 lines (104 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*********************************************
Code for LFSR for pseudo random numbers
Copyright (C) 2016-19 S Combes
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************/
#include "config.h"
#include <avr/interrupt.h>
#include "lfsr.h"
extern uint16_t lfsr;
volatile extern uint8_t timecount;
// -----------------------------------------------------------------------------
void initLFSR(void) {
// Randomly initialises the LFSR
lfsr=(((uint16_t) TCNT0)<<8)|timecount|0x08; // TCNT0 is only 8 bit, timecount perhaps 4
// 0x08 ensures not initialised as 0.
}
// -----------------------------------------------------------------------------
void shuffleLFSR(uint16_t shuffle) {
// Deterministically shuffles the LFSR
if (shuffle!=lfsr) lfsr^=shuffle; // avoid if XOR would create 0.
return;
}
// -----------------------------------------------------------------------------
void shuffleTimeLFSR(void) {
// Stochastically shuffles the LFSR
uint16_t shuffle=(((uint16_t) timecount)<<8)|TCNT0;
shuffleLFSR(shuffle);
return;
}
// -----------------------------------------------------------------------------
uint8_t Rnd12(void) {
// Advances the LFSR and returns a uniformly distributed number in range 0 to 11,
// Galois LFSR, maximum period, example from Wikipedia
uint8_t output;
uint8_t i;
do { // Extract a 4-bit sequence from the LFSR, and keep if it is <12
output=0;
for (i=0;i<4;i++) {
output<<=1;
output|=nextLFSR();
}
} while (output>=12); // reject if not in range 0 to 11
return (output);
}
// -----------------------------------------------------------------------------
uint32_t Rnd32bit(void) {
// Advances the LFSR and returns a uniformly distributed 32 bit number
uint32_t output;
uint8_t i;
output=0;
for (i=0;i<32;i++) {
output<<=1;
output|=nextLFSR();
}
return (output);
}
// -----------------------------------------------------------------------------
uint16_t Rnd16bit(void) {
// Advances the LFSR and returns a uniformly distributed 16 bit number
uint16_t output;
uint8_t i;
// Extract a 16-bit sequence from the LFSR
output=0;
for (i=0;i<16;i++) {
output<<=1;
output|=nextLFSR();
}
return (output);
}
// -----------------------------------------------------------------------------
uint8_t Rnd8bit(void) {
// Advances the LFSR and returns a uniformly distributed 8 bit number
uint8_t output;
uint8_t i;
// Extract a 8-bit sequence from the LFSR
output=0;
for (i=0;i<8;i++) {
output<<=1;
output|=nextLFSR();
}
return (output);
}
// -----------------------------------------------------------------------------
uint8_t nextLFSR(void) {
// Advances the LFSR and returns one bit (i.e. 0 or 1 as uint8),
// Galois LFSR, maximum period, example from Wikipedia
uint8_t output;
#define TOGGLE_MASK (0xB400u)
output=lfsr&1;
lfsr>>=1;
if (output&1)
lfsr^=TOGGLE_MASK;
return (output);
}
// -----------------------------------------------------------------------------