/////////////////////////////////////////////////////////////////////////////////////////////// // Homemade GPS Receiver // Copyright (c) Andrew Holme 2011 // http://www.holmea.demon.co.uk/GPS/Main.htm /////////////////////////////////////////////////////////////////////////////////////////////// `define MW 10 // MASH width in BCD digits excluding most significant 2-bits `define FW 7 // F width in BCD digits excluding MS2 module Frac5 ( input VCO_P, input VCO_N, input XCO_P, input XCO_N, input EXT_P, input EXT_N, output PD2_P, output PD2_N, output PD1_P, output PD1_N, input L22P, input L22N, output L23P, output L23N, output L24P, output L24N); function [7:0] bcd2bin; input [8:0] bcd; begin bcd2bin = 8'd100*bcd[8] + 8'd10*bcd[7:4] + bcd[3:0]; // 000 ... 199 end endfunction wire [6:0] M; wire [7:0] N; wire [`FW*4-1:0] F; wire [31:0] vco; assign M = 7'd99; assign N = bcd2bin(vco[28:20]); assign F = {vco[19:0], 8'h00}; wire [5:0] dlyd_N; // Integer part of VCO frequency / 40MHz wire [4:0] dN; // MASH output (signed) reg [5:0] dither; // dlyd_N + dN wire vco_clk, vco_phase, mash_slot; wire xco_clk, ref_phase, osc_phase; wire ext_clk, ext_phase; wire vco_ref_pd, osc_ext_pd; reg [5:0] vco_div; reg [7:0] xco_div; reg [6:0] ext_div; reg msh_gate, vco_gate, ref_gate, osc_gate, ext_gate; wire gps_clk; reg gps_gate; reg [2:0] gps_div; ////////////////////////////////////////////////////////////////////////////////////////////////////////// wire limiter, debug; IBUFDS if_ibufds(.I(L22P), .IB(L22N), .O(limiter)); OBUFDS if_obufds(.O(L24P), .OB(L24N), .I(debug)); GPS gps1(.clk(gps_clk), .vco(vco), .limiter(limiter), .debug_out({L23P,L23N,debug})); ////////////////////////////////////////////////////////////////////////////////////////////////////////// wire [5:0] vco_cy, vco_nxt; FULL_ADDER vco_fa [5:0] (.s(vco_nxt), .co(vco_cy), .ci({vco_cy[4:0], 1'b1}), .a(vco_div), .b(vco_gate? dither : 6'b0)); always @ (posedge vco_clk) vco_div <= vco_nxt; always @ (posedge xco_clk) xco_div <= xco_div? xco_div-1'b1 : 8'd159; // 80MHz/160 = 500KHz always @ (posedge ext_clk) ext_div <= ext_div? ext_div-1'b1 : M; always @ (posedge ext_clk) gps_div <= gps_div? gps_div-1'b1 : 3'd4; always @ (posedge vco_clk) begin vco_gate <= vco_cy[5]; msh_gate <= vco_gate; // Clock MASH one VCO period after VCO PFD edge end always @ (posedge xco_clk) begin osc_gate <= (xco_div==8'd2); // OSC/EXT clocks traverse FPGA 2/80e6 = 25ns before REF ref_gate <= (xco_div[3:0]==4'b0); // REF/VCO PFD rate is 80/16 = 5 MHz end always @ (posedge ext_clk) begin ext_gate <= (ext_div==5'd0); gps_gate <= (gps_div==3'd0); end // Reference frequency = integer step = 5 MHz at FPGA = 40 MHz at VCO // 40 MHz in 1 Hz steps = 7 BCD decades (F) plus 2 most signifcant bits (MS2) MASH noise_shaper(.clk(mash_slot), .MS2(N[1:0]), .F(F), .dN(dN)); // Top 6 bits of N delayed FW+3 clocks to coincide with pipelined MASH output SHIFTER #(.LENGTH(`FW+3)) sr_N[7:2] (.clk(mash_slot), .d(N[7:2]), .q(dlyd_N)); always @ (posedge mash_slot) dither <= -(dlyd_N + {dN[4],dN[4:0]}); // LVDS clock inputs IBUFGDS vco_ibufgds(.I(VCO_P), .IB(VCO_N), .O(vco_clk)); // VCO divided by 8 (via HMC363 prescaler) IBUFGDS xco_ibufgds(.I(XCO_P), .IB(XCO_N), .O(xco_clk)); // 80MHz crystal oscillator phase locked to EXT IBUFGDS ext_ibufgds(.I(EXT_P), .IB(EXT_N), .O(ext_clk)); // 10MHz or 50MHz external standard // Gated global clcoks BUFGMUX vco_bufgce(.I0(1'b0), .I1(vco_clk), .S( vco_gate), .O(vco_phase)); BUFGMUX msh_bufgce(.I1(1'b0), .I0(vco_clk), .S(~msh_gate), .O(mash_slot)); BUFGMUX ref_bufgce(.I0(1'b0), .I1(xco_clk), .S( ref_gate), .O(ref_phase)); BUFGMUX osc_bufgce(.I1(1'b0), .I0(xco_clk), .S(~osc_gate), .O(osc_phase)); BUFGMUX gps_bufgce(.I0(1'b0), .I1(ext_clk), .S( gps_gate), .O(gps_clk)); BUFGMUX ext_bufgce(.I1(1'b0), .I0(ext_clk), .S(~ext_gate), .O(ext_phase)); // BUFGCE ext_bufgce(.I(ext_clk), .CE(ext_gate), .O(ext_phase)); // Phase detectors AD9901 pfd_2(.vco(vco_phase), .ref(ref_phase), .pd(vco_ref_pd)); AD9901 pfd_1(.vco(osc_phase), .ref(ext_phase), .pd(osc_ext_pd)); // LVDS phase detector outputs OBUFDS obufds_2(.I(vco_ref_pd), .O(PD2_P), .OB(PD2_N)); OBUFDS obufds_1(.I(osc_ext_pd), .O(PD1_P), .OB(PD1_N)); endmodule module AD9901 ( input vco, input ref, output pd); wire ref_tff_q, ref_dff_q; wire vco_tff_q, vco_dff_q; FD ref_tff(.Q(ref_tff_q), .D(~ref_tff_q), .C(ref)); FD vco_tff(.Q(vco_tff_q), .D(~vco_tff_q), .C(vco)); wire xout = ref_tff_q ^ vco_tff_q; FDC ref_dff(.Q(ref_dff_q), .D(xout), .C(ref), .CLR(~vco_dff_q)); FDP vco_dff(.Q(vco_dff_q), .D(xout), .C(vco), .PRE(ref_dff_q)); assign pd = (xout & vco_dff_q) | ref_dff_q; endmodule module MASH ( input clk, input [1:0] MS2, input [`FW*4-1:0] F, output [4:0] dN); wire [1:0] dlyd_MS2; // Most sigificant 2-bits delayed FW-1 clocks wire [`FW*4-1:0] dlyd_F; // BCD 10^(FW-1) ... 10^0 delayed FW-1 ... 0 clocks to merge with vertical pipeline delays wire [(`MW*4+2)*4-1:0] cmd, acc; // MW BCD digits plus 2 bits / by 4 stages long - commands & accumulators wire [19:0] dci, dco; // 5 bits wide / by 4 stages long - differentiator chain MASH_STAGE cascade [3:0] (.clk(clk), .cmd(cmd), .acc(acc), .dci(dci), .dco(dco)); defparam cascade[0].DELAY=4; // Delays required between accumulator carry-outs and differentiator defparam cascade[1].DELAY=3; // chain to compensate for inter-stage horizontal pipeline delays. defparam cascade[2].DELAY=2; defparam cascade[3].DELAY=1; // Total MASH pipeline delay from {MS2,F} to dN is FW+3 = 10 clocks (2us) genvar i; generate for (i=0; i<`FW*4; i=i+4) begin : dly SHIFTER #(.LENGTH(i/4)) sr_F[3:0] (.clk(clk), .d(F[i+3:i]), .q(dlyd_F[i+3:i])); // BCD decade 10^(i/4) delayed (i/4) clocks end endgenerate SHIFTER #(.LENGTH(`FW-1)) sr_MS2[1:0] (.clk(clk), .d(MS2), .q(dlyd_MS2)); // Same delay as top BCD decade assign cmd = { // Command inputs to stages 3, 2, 1: acc[(`MW*4+2)*3-1:1], // Accumulator outputs of stages 2, 1, 0 (LSB not used) 1'b1, // Force LSB=1 for good spreading // Command input to first stage: dlyd_MS2[1:0], // 0, 10, 20 or 30 MHz dlyd_F[`FW*4-1:0], // 7 BCD decades: 0 000 000 - 9 999 999 Hz {`MW-`FW{4'd0}}}; // 000 mHz assign {dci, dN} = {5'b0, dco}; // Differentiator chain endmodule module MASH_STAGE #(parameter DELAY=1) ( input clk, input [`MW*4+1:0] cmd, // MW BCD digits plus 2 bits - command input from previous stage, or F output reg [`MW*4+1:0] acc, // Accumulator. Registered outputs create horizontal pipeline delay input [4:0] dci, // Differentiator chain input from next stage output [4:0] dco); // Output to previous stage (data flow direction is reverse of cmd/acc) wire [`MW-1:0] co; // Carry output to decade above reg [`MW-1:0] ci; // Registering carry between decades creates vertical pipeline delay wire [`MW*4+1:0] sum; // MW BCD digits plus 2 bits reg [4:0] dcd; // z^-1 delays in differentiator chain wire dlyd_ovc, ovc; // Overflow carry from accumulator to differentiator chain BCD_ADDER ba [`MW-1:0] (.a(acc[`MW*4-1:0]), .b(cmd[`MW*4-1:0]), .sum(sum[`MW*4-1:0]), .ci(ci), .co(co)); assign {ovc, sum[`MW*4+1:`MW*4]} = acc[`MW*4+1:`MW*4] + cmd[`MW*4+1:`MW*4] + co[`MW-1]; // Binary add. Carry from top BCD decade not registered SHIFTER #(.LENGTH(DELAY)) sr (.clk(clk), .d(ovc), .q(dlyd_ovc)); // Compensate for horizontal pipelining assign dco = dlyd_ovc + dci - dcd; // Differentiator chain always @ (posedge clk) begin acc <= sum; dcd <= dci; ci <= {co, 1'b0}; // Registering ci creates vertical pipeline delay between BCD decades end endmodule module BCD_ADDER ( input [3:0] a, input [3:0] b, input ci, output co, output [3:0] sum); wire [3:0] raw_co, raw_sum; wire [3:0] adj_co, adj_sum; FULL_ADDER raw_fa [3:0] (.s(raw_sum), .co(raw_co), .ci({raw_co[2:0], ci}), .a(a), .b(b)); FULL_ADDER adj_fa [3:0] (.s(adj_sum), .co(adj_co), .ci({adj_co[2:0], 1'b0}), .a(raw_sum), .b(4'd6)); MUXCY cymux (.S(~adj_co[3]), .DI(1'b1), .CI(raw_co[3]), .O(co)); // Uses fast carry chain if ci not registered assign sum = co? adj_sum : raw_sum; endmodule module FULL_ADDER ( input ci, input a, input b, output s, output co ); wire d = a ^ b; XORCY xorcy ( .LI(d), .CI(ci), .O(s)); MUXCY muxcy (.S(d), .DI(a), .CI(ci), .O(co)); endmodule module SHIFTER #(parameter LENGTH=1) ( input clk, input d, output q); generate if (LENGTH==0) begin : dly assign q=d; end else begin : dly reg [LENGTH:1] sr; assign q = sr[LENGTH]; always @ (posedge clk) sr <= {sr, d}; end endgenerate endmodule