// Downlink PN Code
module downlink_pn( I, Q, clk, reset);
output I, Q;
input clk, reset;

reg [17:0] a, b;     // a is the upper register
                     // b is the lower
reg c, d, e, f;      // c is the upper register feedback
                     // e is the upper register forword XOR
                     // d is the lower register feedback
                     // f is the lower register forword XOR
reg I, Q;            // in pahse and quadrature components

always @ (posedge clk)
if (!reset)
   begin
      a = 18'b11_1111_1111_1111_1111;
      b = 18'b11_1111_1111_1111_1111;
      c = 1'b0;    d = 1'b0;
      e = 1'b0;    f = 1'b0;
      I = 1'b0;    Q = 1'b0; 
   end
else
    begin  
       I = a[0] ^ b[0];
      Q = e ^ f;

       c = a[0] ^ a[7];
       e = a[4] ^ a[6] ^ a[15];

       d = b[0] ^ b[5] ^ b[7] ^ b[10];
        f = b[5] ^ b[6] ^ b[8] ^ b[9] ^ b[10] ^ b[11] ^ b[12] ^ b[13] ^ b[14] ^ b[15];

       a = { c, a[17:1] };
       b = { d, b[17:1] };
    end  
endmodule
