% Generira Goldovo kodo za GPS satelite

function gc=gold_code(prn)

lfsr=1023;%0x3ff
lfsr2=1023;%0x3ff
a=1;
b=1;

for n=2:1023
    p1=xor(bitand(lfsr,1),bitand(lfsr,8)); %x^3+1
    p2=xor(bitand(lfsr2,1),bitand(lfsr,8)); %x^3+1
    p2=xor(p2,bitand(lfsr2,4));%x^2
    p2=xor(p2,bitand(lfsr2,64));%x^6
    p2=xor(p2,bitand(lfsr2,256));%x^8
    p2=xor(p2,bitand(lfsr2,512));%x^9
    %bitshift(lfsr,-1)
    %p1
    %bitshift(p1*1,9)
    lfsr=bitor(bitshift(lfsr,-1),bitshift(p1*1,9));
    lfsr2=bitor(bitshift(lfsr2,-1),bitshift(p2*1,9));
    a=[a bitand(p1,1)];
    b=[b bitand(p2,1)];
end;

m=prn;
n=1;
gc=xor(a(n),b(m));
for n=2:1023
    m=m+1;
    if m>1023
        m=1;
    end;
    gc=[gc xor(a(n),b(m))];
end;

gc=gc*2-1;