k210-sdk-stuff/rust/k210-console/src/lfsr.rs
Wladimir J. van der Laan d9453e8a52 Fun with rust
2019-05-11 10:24:59 +00:00

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
}
}