// File: jkingan[P]ladyheather/heathgps.cpp
// Function: fix_rollover()
void fix_rollover()
{
double jd;

   // See if date from the receiver is likely incorrect due to week rollover
   // issues.  Attempt to fix it if it is wrong.

   if(user_set_rollover) ;    // user forced a rollover offset
   else if(fake_time_stamp) ; // faked time stamp (for Symmetricom UCCM-P)
   else if(auto_rollover && (user_set_rollover == 0) && (pri_year >= ROLLOVER_YEAR)) {  // receiver is now sending valid years
      rollover = 0.0;       // cancel the rollover fix
      auto_rollover = 0.0;
      rolled = 0;

      last_log_hours = 1234;  // force new log header write
      sprintf(log_text, "#! automatic week rollover compensation triggered. year:%d", pri_year);
      write_log_comment(1);

      if(1 && (roll_timing_msg < (ROLL_THRESH+5))) {  // reset the plot queue if rollover detection occured at Heather startup
         reset_queues(RESET_ALL_QUEUES, 4598);
      }
   }
   else if(ROLL_FILT && (time_flags & TFLAGS_NO_UTC_OFS)) {  // GPS time is not valid
      roll_filter = 0;
   }
   else if(ROLL_FILT && !have_utc_ofs) {              // no UTC offset seen yet
      roll_filter = 0;
   }
   else if(ROLL_FILT && (pri_year < 1980)) {            // should never happen
      roll_filter = 0;
   }
   else if(0 && (gps_week == 0) && ((pri_year == 1980) || (pri_year == 1999))) {  // years of first two rollover events
      get_clock_time();       // ... probably a receiver in startup mode
      pri_year = clk_year;    // ... so substitute system clock date/time
      pri_month = clk_month;
      pri_day = clk_day;
      pri_hours = clk_hours;
      pri_minutes = clk_minutes;
      pri_seconds = clk_seconds;
      pri_frac = raw_frac = clk_frac;
   }
   else if((pri_year > 0) && (pri_year < ROLLOVER_YEAR)) {   // GPS year looks bogus
      if(ROLL_FILT && (roll_filter < ROLL_THRESH)) {    // we need a few consecutive bogo years
         ++roll_filter;
      }
      else {
         get_clock_time();  // figure out how many times we rolled over from the system clock
         jd = jdate(pri_year,pri_month,pri_day) + jtime(pri_hours,pri_minutes,pri_seconds, pri_frac);
         rolls = (clk_jd -jd) / (19.0*365.0);  // 19.0 is (1024/52) rounded down (to allow for some system clock error)
//sprintf(debug_text3, "clkjd:%.9f rjd:%.9f  diff:%.9f rolls:%f", clk_jd, jd, clk_jd-jd, rolls);
         rolls = (double) (int) rolls;
         if(rolls <= 1.0) rolls = 1.0;
         rollover = rolls * (1024.0 * 7.0 * 24.0*60.0*60.0);  // rollover one 1024 week epoch
         if(rollover != auto_rollover) {  // new rollover value
            auto_rollover = rollover;

            last_log_hours = 1234;  // force log header write
            sprintf(log_text, "#! automatic week rollover compensation triggered. year:%d  rolls:%g", pri_year, rolls);
            write_log_comment(1);

            if(1 && (roll_timing_msg < (ROLL_THRESH+5))) {  // reset the plot queue if rollover detection occured at Heather startup
               reset_queues(RESET_ALL_QUEUES, 4598);
            }
//          setup_calendars(10);
         }
      }
   }
   else roll_filter = 0;

   if(rollover || user_set_rollover) {  // adjust GPS receiver time by xxx seconds
// sprintf(debug_text, "rover:%f  rolled:%d  pyear:%d", rollover, user_set_rollover, pri_year);
      adjust_rcvr_time(rollover);
      if(rolled == 0) rolled = 1;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
leaq 0x1d8689(%rip), %rax    # 0x27a648
cmpl $0x0, (%rax)
je 0xa1fc9
jmp 0xa222e
leaq 0x2ca900(%rip), %rax    # 0x36c8d0
cmpl $0x0, (%rax)
je 0xa1fda
jmp 0xa222c
leaq 0x2ca617(%rip), %rax    # 0x36c5f8
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xa1ff5
jp 0xa1ff5
jmp 0xa2092
leaq 0x1d864c(%rip), %rax    # 0x27a648
cmpl $0x0, (%rax)
jne 0xa2092
leaq 0x2ca6e8(%rip), %rax    # 0x36c6f4
cmpl $0x7e2, (%rax)          # imm = 0x7E2
jl 0xa2092
leaq 0x1d8625(%rip), %rax    # 0x27a640
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x2ca5cf(%rip), %rax    # 0x36c5f8
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x2ca5b9(%rip), %rax    # 0x36c5f0
movl $0x0, (%rax)
leaq 0x2ca8a0(%rip), %rax    # 0x36c8e4
movl $0x4d2, (%rax)          # imm = 0x4D2
leaq 0x2ca6a3(%rip), %rax    # 0x36c6f4
movl (%rax), %edx
leaq 0x1faae6(%rip), %rdi    # 0x29cb40
leaq 0xe335d(%rip), %rsi     # 0x1853be
movb $0x0, %al
callq 0x9060
movl $0x1, %edi
callq 0x10cbe0
leaq 0x2ca5a7(%rip), %rax    # 0x36c620
cmpl $0x14, (%rax)
jae 0xa208d
movl $0x7, %edi
movl $0x11f6, %esi           # imm = 0x11F6
callq 0x144c0
jmp 0xa222a
leaq 0x2ca65b(%rip), %rax    # 0x36c6f4
cmpl $0x0, (%rax)
jle 0xa221e
leaq 0x2ca64b(%rip), %rax    # 0x36c6f4
cmpl $0x7e2, (%rax)          # imm = 0x7E2
jge 0xa221e
callq 0xa750
leaq 0x2ca633(%rip), %rax    # 0x36c6f4
movl (%rax), %edi
leaq 0x2ca626(%rip), %rax    # 0x36c6f0
movl (%rax), %esi
leaq 0x2ca619(%rip), %rax    # 0x36c6ec
movl (%rax), %edx
callq 0x122140
movsd %xmm0, -0x10(%rbp)
leaq 0x2ca602(%rip), %rax    # 0x36c6e8
movl (%rax), %edi
leaq 0x2ca5f5(%rip), %rax    # 0x36c6e4
movl (%rax), %esi
leaq 0x2ca5e8(%rip), %rax    # 0x36c6e0
movl (%rax), %edx
leaq 0x2ca5f7(%rip), %rax    # 0x36c6f8
movsd (%rax), %xmm0
callq 0x122210
movaps %xmm0, %xmm1
movsd -0x10(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
leaq 0x2ca79e(%rip), %rax    # 0x36c8c0
movsd (%rax), %xmm0
subsd -0x8(%rbp), %xmm0
movsd 0xe0b0d(%rip), %xmm1    # 0x182c40
divsd %xmm1, %xmm0
movsd %xmm0, 0x32dd91(%rip)   # 0x3cfed0
cvttsd2si 0x32dd89(%rip), %eax # 0x3cfed0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x32dd7d(%rip)   # 0x3cfed0
movsd 0xccf0d(%rip), %xmm0    # 0x16f068
ucomisd 0x32dd6d(%rip), %xmm0   # 0x3cfed0
jb 0xa2175
movsd 0xccefb(%rip), %xmm0    # 0x16f068
movsd %xmm0, 0x32dd5b(%rip)   # 0x3cfed0
movsd 0xcf77b(%rip), %xmm0    # 0x1718f8
mulsd 0x32dd4b(%rip), %xmm0   # 0x3cfed0
leaq 0x1d84b4(%rip), %rax    # 0x27a640
movsd %xmm0, (%rax)
leaq 0x1d84a9(%rip), %rax    # 0x27a640
movsd (%rax), %xmm0
leaq 0x2ca456(%rip), %rax    # 0x36c5f8
ucomisd (%rax), %xmm0
jne 0xa21ac
jp 0xa21ac
jmp 0xa221c
leaq 0x1d848d(%rip), %rax    # 0x27a640
movsd (%rax), %xmm0
leaq 0x2ca43a(%rip), %rax    # 0x36c5f8
movsd %xmm0, (%rax)
leaq 0x2ca71b(%rip), %rax    # 0x36c8e4
movl $0x4d2, (%rax)          # imm = 0x4D2
leaq 0x2ca51e(%rip), %rax    # 0x36c6f4
movl (%rax), %edx
movsd 0x32dcf0(%rip), %xmm0   # 0x3cfed0
leaq 0x1fa959(%rip), %rdi    # 0x29cb40
leaq 0xe320b(%rip), %rsi     # 0x1853f9
movb $0x1, %al
callq 0x9060
movl $0x1, %edi
callq 0x10cbe0
leaq 0x2ca41a(%rip), %rax    # 0x36c620
cmpl $0x14, (%rax)
jae 0xa221a
movl $0x7, %edi
movl $0x11f6, %esi           # imm = 0x11F6
callq 0x144c0
jmp 0xa221c
jmp 0xa2228
movl $0x0, 0x328c48(%rip)    # 0x3cae70
jmp 0xa222a
jmp 0xa222c
jmp 0xa222e
leaq 0x1d840b(%rip), %rax    # 0x27a640
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xa2250
jp 0xa2250
leaq 0x1d83fd(%rip), %rax    # 0x27a648
cmpl $0x0, (%rax)
je 0xa227b
leaq 0x1d83e9(%rip), %rax    # 0x27a640
movsd (%rax), %xmm0
callq 0x7fbf0
leaq 0x2ca389(%rip), %rax    # 0x36c5f0
cmpl $0x0, (%rax)
jne 0xa2279
leaq 0x2ca37d(%rip), %rax    # 0x36c5f0
movl $0x1, (%rax)
jmp 0xa227b
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)