Im trying to make pseudo-random sequence generator that works just line Linear Feedback Shift Register. Im doing it in JavaScript because its the only language that I know and im using HTML to create GUI. User should type in initial value and get schematic diagram and pseudo-random sequence itself. Here is my JavaScript code:
var UserInput = document.getElementById('ulaz');
var Output = document.getElementById('izlaz');
//variable `data` is an array of objects which I used to store pictures of circuits
// and [taps][3] necessary for shift registers to give max possible length output
// before going into loop which is 2^n-1, where n (`bit` in my code) is number of
//register blocks and number of digits in input value.
function pss (){
var data = [
{
slika:"pic/2bit.png",
tap:[0,1]
},
{
slika:"pic/3bit.png",
tap:[0,2]
},
{
slika:"pic/4bit.png",
tap:[0,3]
},
{
slika:"pic/5bit.png",
tap:[1,4]
},
{
slika:"pic/6bit.png",
tap:[0,5]
},
{
slika:"pic/7bit.png",
tap:[0,6]
},
{
slika:"pic/8bit.png",
tap:[1,2,3,7]
},
{
slika:"pic/9bit.png",
tap:[3,8]
},
{
slika:"pic/10bit.png",
tap:[2,9]
},
{
slika:"pic/11bit.png",
tap:[1,10]
},
{
slika:"pic/12bit.png",
tap:[0,3,5,11]
},
{
slika:"pic/13bit.png",
tap:[0,2,3,12]
},
{
slika:"pic/14bit.png",
tap:[0,2,4,13]
},
{
slika:"pic/15bit.png",
tap:[0,14]
},
{
slika:"pic/16bit.png",
tap:[1,2,4,15]
},
{
slika:"pic/17bit.png",
tap:[2,16]
},
{
slika:"pic/18bit.png",
tap:[6,17]
},
{
slika:"pic/19bit.png",
tap:[0,1,4,18]
},
{
slika:"pic/20bit.png",
tap:[2,19]
},
{
slika:"pic/21bit.png",
tap:[1,20]
},
{
slika:"pic/22bit.png",
tap:[0,21]
},
{
slika:"pic/23bit.png",
tap:[4,22]
},
{
slika:"pic/24bit.png",
tap:[0,2,3,23]
},
{
slika:"pic/25bit.png",
tap:[2,24]
},
{
slika:"pic/26bit.png",
tap:[0,1,5,25]
},
{
slika:"pic/27bit.png",
tap:[0,1,4,26]
},
{
slika:"pic/28bit.png",
tap:[2,27]
},
{
slika:"pic/29bit.png",
tap:[0,28]
},
{
slika:"pic/30bit.png",
tap:[0,3,5,29]
},
{
slika:"pic/31bit.png",
tap:[2,30]
},
{
slika:"pic/32bit.png",
tap:[1,5,6,31]
}
];
var first = UserInput.value.split("");
for (k=0;k<first.length;k++) first[k] = +first[k];
//first is just UserInput separated in one char strings than parsed to integers
var bit = first.length - 2;
// I subtracted 2 here so I can access objects from data
var matrix = [first];
var t = 0;
var between;
var z;
for (i=1; i<Math.pow(2, bit+2)-1; i++){ //here is that 2^n-1. +2 is because i had -2 before. For loop is starting from 1 and ending with <2^n-1 because i already have first array of matrix
for (j=0; j<data[bit].tap.length; j++){
z = data[bit].tap[j];
t = t ^ matrix[i-1][z];
} // this for makes "t" which is all taps XOR-ed. If user input was 101, tap would be [0,2] and t would be 1xor1=0
between = matrix[i-1];
console.log(between);
between.unshift(t);
between.pop();
matrix[i] = between;
t=0; // here Im "shifting registers" or just placing t in front of last generated row and removing its last digit, thus generating new row
}
console.log(matrix);
}
and here is HTML so you can run it.
variable data
is an array of objects which I used to store pictures of circuits and taps necessary for shift registers to give max possible length output before going into loop which is 2^n-1, where n (bit
in my code) is number of register blocks and number of digits in input value.
So problem is: console.log(between);
which logs last generated row is all correct except, ofc, there is no last row because it shows last generated, but than console.log(matrix)
which should log complete matrix , shows all rows overwritten by last one.
So for user input 101, matrix should be
101
010
001
100
110
111
011
but is just
011
011
011 ...
I cant figure out what is wrong with it if part before console.log(between);
is all fine...
P.S. Code is not finished it wont display solution in HTML, and there still needs to be part of function that makes an array from last column of matrix, which is pseudo-random sequence.