# Calculate the IRNSS G2 offset table for lm_catab.c
from mutils import *
from group_mp_slope import hex2array, lfsr

g2_taps = [2,3,6,8,9,10]

# This table was copied straight from the ICD.  Columns are:
# PRN L5_Initial_G2 L5_First_10_chips S1_Initial_G2 S1_First_10_chips
ICD_irnss_table = """
1 1110100111 0130 0011101111 1420
2 0000100110 1731 0101111101 1202
3 1000110100 0713 1000110001 0716
4 0101110010 1215 0010101011 1524
5 1110110000 0117 1010010001 0556
6 0001101011 1624 0100101100 1323
7 0000010100 1753 0010001110 1561
8 0100110000 1317 0100100110 1331
9 0010011000 1547 1100001110 0361
10 1101100100 0233 1010111110 0501
11 0001001100 1663 1110010001 0156
12 1101111100 0203 1101101001 0226
13 1011010010 0455 0101000101 1272
14 0111101010 1025 0100001101 1362
"""

for line in ICD_irnss_table.split('\n'):
    if len(line) == 0:
        continue
    words = line.split()
    sv = int(words[0])
    desired_state = int(words[1],2)

    n_bits = 10
    desired_state = hex2array( desired_state, n_bits )
    g2 = hex2array(0x3ff, n_bits)
    count = 0
    while True:
        state = lfsr( g2_taps, g2 )
        count += 1
        if (state == desired_state).all():
            break
    print('  1023-%d,       /* SV #%d - IRNSS L band */'%(1023-count,sv))

print('')
for line in ICD_irnss_table.split('\n'):
    if len(line) == 0:
        continue
    words = line.split()
    sv = int(words[0])
    desired_state = int(words[3],2)

    n_bits = 10
    desired_state = hex2array( desired_state, n_bits )
    g2 = hex2array(0x3ff, n_bits)
    count = 0
    while True:
        state = lfsr( g2_taps, g2 )
        count += 1
        if (state == desired_state).all():
            break
    print('  1023-%d,       /* SV #%d - IRNSS S band */'%(1023-count,sv))
