// ===========================================================
// ClockComparator:  The comparator controlling frequency
//                   of the PLL VFO.
//
// The small, v. 1.1 6-bit counter assumes that the frequency 
// difference will never exceed a factor of 63, which would
// possibly cause an overflow.
// ------------------------------------------------------------
// 2010-03-24 jmw: v. 1.1 Used cts assign to AdjustFreq (not reg).
// 2006-05-17 jmw: v. 1.0 Copied from Lab21.
// ============================================================
//
module ClockComparator (output[1:0] AdjustFreq
                       , input ClockIn, CounterClock, Reset
                       );
reg[1:0] VarClockCount, AdjustFreqR;
//
assign AdjustFreq = AdjustFreqR;
//
// The count from the previous edge is used for the
// comparison:
always@(ClockIn, Reset)
  if (Reset==1'b1)
       begin
       AdjustFreqR   = 2'b01;
       VarClockCount = 2'b01;
       end
  else if (CounterClock==1'b1)
            VarClockCount = VarClockCount + 2'b01;
       else begin
            case (VarClockCount)
                2'b00: AdjustFreqR = 2'b11; // Better speed it up.
                2'b01: AdjustFreqR = 2'b01; // Seems matched.
              default: AdjustFreqR = 2'b00; // Must be too fast.
            endcase
            VarClockCount = 2'b00; // Initialize for next ClockIn edge.
            end
endmodule // ClockComparator.
