function codeChips = codeGenerateCA( prn )

%==============================================================================%
%                                                                              %
%   codeChips = codeGenerateCA( prn )                                          %
%                                                                              %
%   Generate 1023 chips of the C/A-code sequence                               %
%   David S. De Lorenzo and Grace Gao                                          %
%   22 February 2007                                                           %
%                                                                              %
%------------------------------------------------------------------------------%
%                                                                              %
%   Reference IS-GPS-200H dated 24 September 2013                              %
%     -- pages 6-8 for code phase assignments GPS PRN signals 1-63             %
%           C/A Codes for 34 and 37 are identical                              %
%           PRN sequence 33 is reserved for other uses                         %
%           PRNs 38-63 are III and subsequent blocks only                      %
%     -- pages 57-61 for additional code phase assignments PRN 64-210          %
%                                                                              %
%   The code generator can be checked with the following code:                 %
%{                                                                         
     firstTenOctal = ...
         [1440, 1620, 1710, 1744, 1133, 1455, 1131, 1454, 1626, 1504, ...
          1642, 1750, 1764, 1772, 1775, 1776, 1156, 1467, 1633, 1715, ...
          1746, 1763, 1063, 1706, 1743, 1761, 1770, 1774, 1127, 1453, ...
          1625, 1712, 1745, 1713, 1134, 1456, 1713, 1760, 1236,   63, ...
           626,  126, 1674, 1234,  271,  712,  213,  412,  236,  450, ...
            61,  142,  775,  762,  111, 1600,  424, 1351, 1550, 1271, ...
          1441,  444,   32, 1523,  175,  617,  663,  435, 1752,  254, ...
           731, 1373,  332,  723, 1705, 1515, 1700, 1256,  377,  767, ...
           336, 1412, 1507, 1514, 1164, 1500,  215,  103,  664,  532, ...
          1171, 1641, 1521,  227,  543, 1517,  322,  242, 1031,  744, ...
           564, 1067, 1056,   14,   26, 1342, 1042, 1006, 1637, 1666, ...
          1121,  761, 1315,  766, 1225, 1732,  673, 1220, 1413,  671, ...
           536, 1510, 1545,  160,  701,   13, 1060,  245,  527, 1436, ...
          1226, 1257,   46, 1071,  561, 1037,  770, 1327, 1472,  124, ...
           366,  133,  465,  717,  217, 1742, 1422, 1442,  523,  736, ...
          1635,  136,  273, 1026,    3, 1670,  624,  235,  554,   75, ...
          1341,   42,  115,  207,  204, 1576, 1142,   40,  107, 1643, ...
           553,  317,  415,  123, 1267, 1535,  635,  760,  707, 1276, ...
          1322,  211, 1562,  774,  323,  112, 1306,   27, 1470, 1505, ...
          1013,  355,  727,  170,   30,  472, 1237,  414, 1050, 1630, ...
           571,  732, 1301, 1173,   20,  447, 1114,  341, 1024, 1046];
     for prn = 1:210
         octalIn    = firstTenOctal(prn);
         decimalOut = 0;
         % Convert octal to decimal --> avoids call to 'oct2dec'
         for ii = ceil(log10(octalIn)):-1:1
             d = floor(octalIn/10^(ii-1));
             decimalOut = 8*decimalOut + d;
             octalIn = octalIn - d*10^(ii-1);
         end
         firstTenBinary  = dec2bin( decimalOut, 10 );
         codeChips       = codeGenerateCA( prn );
         codeChipsBinary = sprintf('%d', (codeChips+1)/2);
         if any( firstTenBinary ~= codeChipsBinary(1:10) )
             error( 'Code generation error for prn #%d!!', prn );
         end
     end
     disp( 'ALL PASSED!!' );
%}
%                                                                              %
%==============================================================================%
%                       GPS Software Receiver in Matlab                        %
%     Copyright (c) 2003-2017 David S. De Lorenzo / 2018-2023 Trimble Inc.     %
%==============================================================================%

% XB code delay - chips
if ( prn >= 1 ) && ( prn <= 210 )
    XB_delayVec = ...
        [   5,    6,    7,    8,   17,   18,  139,  140,  141,  251, ...
          252,  254,  255,  256,  257,  258,  469,  470,  471,  472, ...
          473,  474,  509,  512,  513,  514,  515,  516,  859,  860, ...
          861,  862,  863,  950,  947,  948,  950,   67,  103,   91, ...
           19,  679,  225,  625,  946,  638,  161, 1001,  554,  280, ...
          710,  709,  775,  864,  558,  220,  397,   55,  898,  759, ...
          367,  299, 1018,  729,  695,  780,  801,  788,  732,   34, ...
          320,  327,  389,  407,  525,  405,  221,  761,  260,  326, ...
          955,  653,  699,  422,  188,  438,  959,  539,  879,  677, ...
          586,  153,  792,  814,  446,  264, 1015,  278,  536,  819, ...
          156,  957,  159,  712,  885,  461,  248,  713,  126,  807, ...
          279,  122,  197,  693,  632,  771,  467,  647,  203,  145, ...
          175,   52,   21,  237,  235,  886,  657,  634,  762,  355, ...
         1012,  176,  603,  130,  359,  595,   68,  386,  797,  456, ...
          499,  883,  307,  127,  211,  121,  118,  163,  628,  853, ...
          484,  289,  811,  202, 1021,  463,  568,  904,  670,  230, ...
          911,  684,  309,  644,  932,   12,  314,  891,  212,  185, ...
          675,  503,  150,  395,  345,  846,  798,  992,  357,  995, ...
          877,  112,  144,  476,  193,  109,  445,  291,   87,  399, ...
          292,  901,  339,  208,  711,  189,  263,  537,  663,  942, ...
          173,  900,   30,  500,  935,  556,  373,   85,  652,  310];
    XB_delay = XB_delayVec(prn);
else
    error( '***  Invalid PRN Code!!  ***' );
end

% Shift register tap points
tap1 = [1 0 0 0 0 0 0 1 0 0];
tap2 = [1 1 1 0 1 0 0 1 1 0];

% Initialize G1 and G2 shift registers
g1reg = ones(1, 10);
g2reg = ones(1, 10);

% Initialize G1 & G2 output
g1 = zeros(1, 1023);
g2 = zeros(1, 1023);
g1(1:10) = g1reg;
g2(1:10) = g2reg;

% Generate all G1 & G2 chips based on the feedback polynomials
for ii = 11:1023
    g1(ii) = mod( g1reg * tap1', 2 );
    g2(ii) = mod( g2reg * tap2', 2 );
    g1reg  = [g1reg(2:10), g1(ii)];
    g2reg  = [g2reg(2:10), g2(ii)];
end

% Delay the G2 code
%   The idea: g2 = concatenate[ g2_right_part, g2_left_part ]
g2 = [ g2(1023-XB_delay+1:1023), g2(1:1023-XB_delay) ];

% Form codeChips by modulo-adding G1 and G2
codeChips = mod( g1 + g2, 2 );

% Shift/scale values back to +1 and -1
codeChips = 2*codeChips - 1;

return
%===============================================================================