mirror of
https://github.com/laanwj/k210-sdk-stuff.git
synced 2024-11-29 04:43:31 +04:00
25 lines
476 B
Rust
25 lines
476 B
Rust
/** 32-bit LFSR for "random" output */
|
|
pub struct LFSR {
|
|
state: u32,
|
|
feedback: u32,
|
|
}
|
|
|
|
impl LFSR {
|
|
pub fn new() -> LFSR {
|
|
LFSR {
|
|
state: 0x12345678,
|
|
feedback: 0xf00f00f0, // LFSR period 0xf7ffffe0
|
|
}
|
|
}
|
|
|
|
pub fn next(&mut self) -> u32 {
|
|
let rv = self.state;
|
|
let lsb = (self.state & 1) != 0;
|
|
self.state >>= 1;
|
|
if lsb {
|
|
self.state ^= self.feedback;
|
|
}
|
|
rv
|
|
}
|
|
}
|