-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLFSR_MS.java
More file actions
65 lines (54 loc) · 1.74 KB
/
LFSR_MS.java
File metadata and controls
65 lines (54 loc) · 1.74 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
import java.util.ArrayList;
/*
@Milos Seskar
@9/20/18
This program creates and LFSR and has methods to simulate a step in the LFSR
*/
public class LFSR_MS{
private String reg;
private int N;
private int tapPos;
public LFSR_MS(String seed, int tap){
reg = seed;
tapPos = tap;
N = seed.length();
}
// simulates one step of this LFSR and returns the new bit (as 0 or 1)
public int step(){
String oldReg = reg;
int tapBit = ithConv(oldReg, N-tapPos -1);//right most bit, -1 to stay in bounds
int fstBit = ithConv(oldReg, 0);//left most bit
int curLastBit = tapBit ^ fstBit; //XOR the left most bit with the tap
char lastChar;
if(curLastBit == 0)
lastChar = '0';
else
lastChar = '1';
reg = oldReg.substring(1) + lastChar;//excludes left most bit and adds the lastChar to the right
return curLastBit;
}
//simulate k steps in the lfsr
public int generate(int k){
int sum = 0;
for(int i = 0; i < k; i++){
int curBit = step();
sum = (sum << 1) + curBit; //binary shift operator, skips need for string, adds the last bit generated by the step
}
return sum;
}
// returns the ith bit of this LFSR (as 0 or 1)
public int ithConv(String str, int i) {
return (int)(str.charAt(i) - '0');//takes char at i in string register, then casts to int and returns
}
public String toString(){
return reg;
}
public static void main(String [] args){
LFSR_MS sr = new LFSR_MS("01101000010", 8);
System.out.println(sr);
for(int i = 0; i < 10; i++){
int r = sr.generate(5);
System.out.println(sr + " " + r);
}
}
}