function codeChips = codeGenerateL5Quadrature( prn )

%==============================================================================%
%                                                                              %
%   codeChips = codeGenerateL5Quadrature( prn )                                %
%                                                                              %
%   Generate 10230 chips of the L5-Quadrature 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 = ...
        [1701,  323, 5292, 2020, 5429, 7136, 1041, 5947, 4315,  148, ...
          535, 1939, 5206, 5910, 3595, 5135, 6082, 6990, 3546, 1523, ...
         4548, 4484, 1893, 3961, 7106, 5299, 4660,  276, 4389, 3783, ...
         1591, 1601,  749, 1387, 1661, 3210,  708, 4226, 5604, 6375, ...
         3056, 1772, 3662, 4401, 5218, 2838, 6913, 1685, 1194, 6963, ...
         5001, 6694,  991, 7489, 2441,  639, 2097, 2498, 6470, 2399, ...
          242, 3768, 1186, 5246, 4259, 5907, 3870, 3262, 7387, 3069, ...
         2999, 7993, 7849, 4157, 5031, 5986, 4833, 5739, 7846,  898, ...
         2022, 7446, 6404,  155, 7862, 7795, 6121, 4840, 6585,  429, ...
         6020,  200, 1664, 1499, 7298, 1305, 7323, 7544, 4438, 2485, ...
         3387, 7319, 1853, 5781, 1874, 7555, 2132, 6441, 6722, 1192, ...
         2588, 2188,  297, 1540, 4138, 5231, 4789,  659,  871, 6837, ...
         1393, 7383,  611, 4920, 5416, 1611, 2474,  118, 1382, 1092, ...
         7950, 7223, 1769, 4721, 1252, 5147, 2165, 7897, 4054, 3498, ...
         6571, 2858, 8126, 7017, 1901,  181, 1114, 5195, 7479, 4186, ...
         3904, 7128, 1396, 4513, 5967, 2580, 2575, 7961, 2598, 4508, ...
         2090, 3685, 7748,  684,  913, 5558, 2894, 5858, 6432, 3813, ...
         3573, 7523, 5280, 3376, 7424, 2918, 5793, 1747, 7079, 2921, ...
         2490, 4119, 3373,  977,  681, 4273, 5419, 5626, 1266, 5804, ...
         2414, 6444, 4757,  427, 5452, 5182, 6606, 6531, 4268, 3115, ...
         6835,  862, 4856, 2765,   37, 1943, 7977, 2512, 4451, 4071];
    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
%===============================================================================