function codeChips = codeGenerateL5Inphase( prn )

%==============================================================================%
%                                                                              %
%   codeChips = codeGenerateL5Inphase( prn )                                   %
%                                                                              %
%   Generate 10230 chips of the L5-Inphase code sequence                       %
%   David S. De Lorenzo and Grace Gao                                          %
%   22 February 2007                                                           %
%                                                                              %
%------------------------------------------------------------------------------%
%                                                                              %
%   Reference IS-GPS-705D dated 24 September 2013                              %
%     -- pages 5-7 for code phase assignments GPS PRN signals 1-63             %
%     -- pages 29-33 for additional code phase assignments PRN 38-210          %
%                                                                              %
%   WAAS/L5 satellites are as follows:                                         %
%             PRN 131 - Eutelsat 117 West B                                    %
%             PRN 135 - Galaxy 15                                              %
%             PRN 138 - Anik F1R                                               %
%             PRN 133 - SES-15                                                 %
%                                                                              %
%==============================================================================%
%                       GPS Software Receiver in Matlab                        %
%     Copyright (c) 2003-2017 David S. De Lorenzo / 2018-2023 Trimble Inc.     %
%==============================================================================%

% XB code advance - chips
if ( prn >= 1 ) && ( prn <= 210 )
    XB_advanceVec = ...
        [ 266,  365,  804, 1138, 1509, 1559, 1756, 2084, 2170, 2303, ...
         2527, 2687, 2930, 3471, 3940, 4132, 4332, 4924, 5343, 5443, ...
         5641, 5816, 5898, 5918, 5955, 6243, 6345, 6477, 6518, 6875, ...
         7168, 7187, 7329, 7577, 7720, 7777, 8057, 5358, 3550, 3412, ...
          819, 4608, 3698,  962, 3001, 4441, 4937, 3717, 4730, 7291, ...
         2279, 7613, 5723, 7030, 1475, 2593, 2904, 2056, 2757, 3756, ...
         6205, 5053, 6437, 7789, 2311, 7432, 5155, 1593, 5841, 5014, ...
         1545, 3016, 4875, 2119,  229, 7634, 1406, 4506, 1819, 7580, ...
         5446, 6053, 7958, 5267, 2956, 3544, 1277, 2996, 1758, 3360, ...
         2718, 3754, 7440, 2781, 6756, 7314,  208, 5252,  696,  527, ...
         1399, 5879, 6868,  217, 7681, 3788, 1337, 2424, 4243, 5686, ...
         1955, 4791,  492, 1518, 6566, 5349,  506,  113, 1953, 2797, ...
          934, 3023, 3632, 1330, 4909, 4867, 1183, 3990, 6217, 1224, ...
         1733, 2319, 3928, 2380,  841, 5049, 7027, 1197, 7208, 8000, ...
          152, 6762, 3745, 4723, 5502, 4796,  123, 8142, 5091, 7875, ...
          330, 5272, 4912,  374, 2045, 6616, 6321, 7605, 2570, 2419, ...
         1234, 1922, 4317, 5110,  825,  958, 1089, 7813, 6058, 7703, ...
         6702, 1714, 6371, 2281, 1986, 6282, 3201, 3760, 1056, 6233, ...
         1150, 2823, 6250,  645, 2401, 1639, 2946, 7091,  923, 7045, ...
         6493, 1706, 5836,  926, 6086,  950, 5905, 3240, 6675, 3197, ...
         1555, 3589, 4555, 5671, 6948, 4664, 2086, 5950, 5521, 1515];
    XB_advance = XB_advanceVec(prn);
else
    error( '***  Invalid PRN Code!!  ***' );
end

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

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

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

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

% Concatenate G1 & G2
g1 = [ g1(1:8190), g1(1:10230-8190) ];
g2 = [ g2(1:8191), g2(1:10230-8191) ];

% Advance the G2 code
%   The idea: g2 = concatenate[ g2_right_part, g2_left_part ]
g2 = [ g2(XB_advance+1:10230), g2(1:XB_advance) ];

% 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
%===============================================================================