// File: jkingan[P]ladyheather/heathgps.cpp
// Function: x72_discipline_state()
void x72_discipline_state()
{
static int change_count;
double slope;
int val;
int i1, i2;
int flag;
double x;
double dds_change;

   // discipline the output frequency 

   if(pps_enabled == 0) {  // should never happen
      x72_start_discipline();
      return;
   }
   if(x72_tc_val <= 0) {
      x72_tc_val = X72_TIME_CONST;
       x72_reset_filter();
   }
   if(x72_jamthresh_val > X72_JAMSYNC_THRESH) x72_jamthresh_val = X72_JAMSYNC_THRESH;
   if(x72_jamthresh_val <= 0) x72_jamthresh_val = X72_JAMSYNC_THRESH;

   val = x72_pps;
   val = fix_x72_tics(val);
if(show_debug_info) sprintf(debug_text, "disciplining!  interval:%d   ival:%d  last:%d  sum:%d mcv:%g   dds:%g   x72_pps:%d -> %d   ppo:%g", x72_pps_interval, x72_ival,last_ival,x72_dis_sum,x72_pps_stats(0),  last_x72_dds, x72_pps, val, pps_offset);
   val = IABS(val);

   if((val > (int) (x72_jamthresh_val*1.0E-9*x72_osc))) {  // PPS is off by a large amount, do an immediate jamsync
if(show_debug_info) sprintf(debug_text5, "disipline holdover pps limit!!! %d", x72_pps);
      x72_start_discipline();
// !!!!!!!! if DDS is too far off and drift exceeds this rate, we'll never start disciplining
//x72_pps_stats(0);
//slope = x72_pps_trend;
//slope /= (double) x72_pps_interval;
//goto jamit_damnit;
      return;
   }

   if(x72_pps_interval == 0) last_dis_ival = x72_ival;
   i1 = fix_x72_tics(x72_ival);           // fixup possible wrap near 0
   i2 = fix_x72_tics(last_dis_ival);
   last_dis_ival = x72_ival;

   x72_update_pps_list(i1);      // add fixed up ival to pps list

   x72_dis_sum += (i1 - i2);     // accumulate difference between the last two PPS values
   if(IABS(x72_dis_sum) >= 2) ++x72_sum_sum;   // error sum exceeds threshold, see for how long
   else                       x72_sum_sum = 0; // error sum is back in allowed range

   flag = 0;
#define FORCE_TIC 3  // 3 -> 50 ns   was 6
   if((IABS(x72_dis_sum) >= FORCE_TIC) && (x72_pps_interval >= (LONG_INTERVAL*2))) {   // was *1
      flag = 1;   // accumulated PPS error is large over a long interval
   }
   else if(1 && (IABS(x72_dis_sum) >= (FORCE_TIC*3))) {  // was *2 - bigger error triggers an immediate DDS change
      flag = 2;
   }

   if(IABS(x72_sum_sum) >= 5) {  // PPS is off by 2 or more tics for over 5 consecutive seconds
      if((x72_pps_interval >= x72_tc_val) || (flag == 2)) { // we have enough data to evaluate
         x = x72_pps_stats(1);
         if(x || flag) { // PPS most common value is not zero or large PPS error
            // update DDS based on the interval average PPS error
            if(1 && (x72_pps_interval < LONG_INTERVAL)) { // qwerty drift rate is a bit large, scale average value betwen 1.0 and 2.0 times the average
               slope = x72_pps_avg * (1.0 + (((double) (x72_pps_interval - x72_tc_val)) / (2.0*(double) x72_tc_val)) );
            }
            else {  // very slow drift rate
               slope = x72_pps_trend;
            }
            slope /= (double) x72_pps_interval;
         }
         else slope = 0.0;

//jamit_damnit:
         if(slope) {  // adjust the DDS frequency to cancel the average PPS drift during the interval
            slope /= (double) x72_osc;
            last_interval_dds = interval_start_dds;
            interval_start_dds = last_x72_dds + slope;
interval_start_dds = x72_avg_dds(interval_start_dds);
            ++change_count;
            dds_change = interval_start_dds-last_x72_dds;
if(show_debug_info) {
   BEEP(516);
   sprintf(plot_title, "New dds %d!  %g olddds:%g  change:%g  interval:%d  ival:%d  lastiv:%d  sum:%d  mcv:%g  avg:%g  flag:%d", change_count, interval_start_dds,last_x72_dds,dds_change, x72_pps_interval, x72_ival, last_dis_ival, x72_dis_sum, x72_pps_stats(0), x72_pps_avg, flag);
   mark_q_entry[9] = plot_q_in;
}
if(debug_file) fprintf(debug_file, "New dds %d!  %g olddds:%g  change:%g  interval:%d  ival:%d  lastiv:%d  sum:%d mcv:%g  avg:%g  flag:%d\n", change_count,interval_start_dds,last_x72_dds,dds_change, x72_pps_interval, x72_ival, last_dis_ival, x72_dis_sum, x72_pps_stats(0), x72_pps_avg, flag);

            set_x72_dds(interval_start_dds);
            have_x72_pps = 0;
            last_dis_ival = x72_ival;
            new_x72_interval(3);

            update_x72_tic(last_x72_pps, 3);  // re-align PPS out to PPS in
            x72_jamsync_ticks = 0;   // we just changed the TIC register to move the PPS output
            x72_jamsync_flag = 0;    // now wait for the change to take effect
            x72_user_state = X72_STATE_JAM2;
         }
      }
   }
   else {  // PID is not currently used
//      update_x72_pid();
   }
update_x72_pid();

   return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
leaq 0x26275e(%rip), %rax    # 0x36cf00
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x10a7b4
callq 0xc4ec0
jmp 0x10ae34
leaq 0x16f829(%rip), %rax    # 0x279fe4
cmpl $0x0, (%rax)
jg 0x10a7d2
leaq 0x16f81d(%rip), %rax    # 0x279fe4
movl $0x190, (%rax)          # imm = 0x190
callq 0xc4f60
leaq 0x16f827(%rip), %rax    # 0x27a000
movsd (%rax), %xmm0
movsd 0x649eb(%rip), %xmm1    # 0x16f1d0
ucomisd %xmm1, %xmm0
jbe 0x10a7fe
leaq 0x16f80e(%rip), %rax    # 0x27a000
movsd 0x649d6(%rip), %xmm0    # 0x16f1d0
movsd %xmm0, (%rax)
xorps %xmm0, %xmm0
leaq 0x16f7f8(%rip), %rax    # 0x27a000
ucomisd (%rax), %xmm0
jb 0x10a821
leaq 0x16f7eb(%rip), %rax    # 0x27a000
movsd 0x649b3(%rip), %xmm0    # 0x16f1d0
movsd %xmm0, (%rax)
leaq 0x16f7b4(%rip), %rax    # 0x279fdc
movl (%rax), %eax
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %edi
callq 0xc4890
movl %eax, -0xc(%rbp)
leaq 0x16d265(%rip), %rax    # 0x277aa4
cmpb $0x0, (%rax)
je 0x10a8c3
leaq 0x16f8b9(%rip), %rax    # 0x27a104
movl (%rax), %eax
movl %eax, -0x38(%rbp)
leaq 0x16f8c5(%rip), %rax    # 0x27a11c
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movl 0x2c6822(%rip), %eax    # 0x3d1084
movl %eax, -0x30(%rbp)
movl 0x2caff9(%rip), %eax    # 0x3d5864
movl %eax, -0x2c(%rbp)
xorl %edi, %edi
callq 0x109c20
movl -0x38(%rbp), %edx
movl -0x34(%rbp), %ecx
movl -0x30(%rbp), %r8d
movl -0x2c(%rbp), %r9d
leaq 0x16f836(%rip), %rax    # 0x27a0c0
movsd (%rax), %xmm1
leaq 0x16f747(%rip), %rax    # 0x279fdc
movl (%rax), %r10d
movl -0xc(%rbp), %eax
leaq 0x2610f6(%rip), %rsi    # 0x36b998
movsd (%rsi), %xmm2
leaq 0x2932d3(%rip), %rdi    # 0x39db80
leaq 0x83895(%rip), %rsi     # 0x18e149
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x3, %al
callq 0x9060
cmpl $0x0, -0xc(%rbp)
jge 0x10a8d3
xorl %eax, %eax
subl -0xc(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x10a8d9
movl -0xc(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
movsd 0x6487e(%rip), %xmm0    # 0x16f168
leaq 0x16f70f(%rip), %rcx    # 0x27a000
mulsd (%rcx), %xmm0
leaq 0x16f54c(%rip), %rcx    # 0x279e48
mulsd (%rcx), %xmm0
cvttsd2si %xmm0, %ecx
cmpl %ecx, %eax
jle 0x10a93c
leaq 0x16d195(%rip), %rax    # 0x277aa4
cmpb $0x0, (%rax)
je 0x10a932
leaq 0x16f6c1(%rip), %rax    # 0x279fdc
movl (%rax), %edx
leaq 0x29379c(%rip), %rdi    # 0x39e0c0
leaq 0x83880(%rip), %rsi     # 0x18e1ab
movb $0x0, %al
callq 0x9060
callq 0xc4ec0
jmp 0x10ae34
leaq 0x16f7c1(%rip), %rax    # 0x27a104
cmpl $0x0, (%rax)
jne 0x10a957
leaq 0x16f7cd(%rip), %rax    # 0x27a11c
movl (%rax), %eax
movl %eax, 0x2c6731(%rip)    # 0x3d1088
leaq 0x16f7be(%rip), %rax    # 0x27a11c
movl (%rax), %edi
callq 0xc4890
movl %eax, -0x10(%rbp)
movl 0x2c671a(%rip), %edi    # 0x3d1088
callq 0xc4890
movl %eax, -0x14(%rbp)
leaq 0x16f79f(%rip), %rax    # 0x27a11c
movl (%rax), %eax
movl %eax, 0x2c6703(%rip)    # 0x3d1088
movl -0x10(%rbp), %edi
callq 0x10a170
movl -0x10(%rbp), %eax
subl -0x14(%rbp), %eax
addl 0x2caecb(%rip), %eax    # 0x3d5864
movl %eax, 0x2caec5(%rip)    # 0x3d5864
cmpl $0x0, 0x2caebe(%rip)    # 0x3d5864
jge 0x10a9b5
xorl %eax, %eax
subl 0x2caeb4(%rip), %eax    # 0x3d5864
movl %eax, -0x40(%rbp)
jmp 0x10a9be
movl 0x2caea9(%rip), %eax    # 0x3d5864
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl $0x2, %eax
jl 0x10a9d7
movl 0x2cae9c(%rip), %eax    # 0x3d5868
addl $0x1, %eax
movl %eax, 0x2cae93(%rip)    # 0x3d5868
jmp 0x10a9e1
movl $0x0, 0x2cae87(%rip)    # 0x3d5868
movl $0x0, -0x18(%rbp)
cmpl $0x0, 0x2cae75(%rip)    # 0x3d5864
jge 0x10a9fe
xorl %eax, %eax
subl 0x2cae6b(%rip), %eax    # 0x3d5864
movl %eax, -0x44(%rbp)
jmp 0x10aa07
movl 0x2cae60(%rip), %eax    # 0x3d5864
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl $0x3, %eax
jl 0x10aa27
leaq 0x16f6ee(%rip), %rax    # 0x27a104
cmpl $0x960, (%rax)          # imm = 0x960
jl 0x10aa27
movl $0x1, -0x18(%rbp)
jmp 0x10aa57
cmpl $0x0, 0x2cae36(%rip)    # 0x3d5864
jge 0x10aa3d
xorl %eax, %eax
subl 0x2cae2c(%rip), %eax    # 0x3d5864
movl %eax, -0x48(%rbp)
jmp 0x10aa46
movl 0x2cae21(%rip), %eax    # 0x3d5864
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl $0x9, %eax
jl 0x10aa55
movl $0x2, -0x18(%rbp)
jmp 0x10aa57
cmpl $0x0, 0x2cae0a(%rip)    # 0x3d5868
jge 0x10aa6d
xorl %eax, %eax
subl 0x2cae00(%rip), %eax    # 0x3d5868
movl %eax, -0x4c(%rbp)
jmp 0x10aa76
movl 0x2cadf5(%rip), %eax    # 0x3d5868
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl $0x5, %eax
jl 0x10ae2d
leaq 0x16f67b(%rip), %rax    # 0x27a104
movl (%rax), %eax
leaq 0x16f552(%rip), %rcx    # 0x279fe4
cmpl (%rcx), %eax
jge 0x10aaa0
cmpl $0x2, -0x18(%rbp)
jne 0x10ae2b
movl $0x1, %edi
callq 0x109c20
movsd %xmm0, -0x20(%rbp)
movsd -0x20(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x10aac9
jp 0x10aac9
cmpl $0x0, -0x18(%rbp)
je 0x10ab53
leaq 0x16f634(%rip), %rax    # 0x27a104
cmpl $0x4b0, (%rax)          # imm = 0x4B0
jge 0x10ab28
movsd 0x2cad90(%rip), %xmm0   # 0x3d5870
leaq 0x16f61d(%rip), %rax    # 0x27a104
movl (%rax), %eax
leaq 0x16f4f4(%rip), %rcx    # 0x279fe4
subl (%rcx), %eax
cvtsi2sd %eax, %xmm2
leaq 0x16f4e7(%rip), %rax    # 0x279fe4
cvtsi2sdl (%rax), %xmm3
movsd 0x64647(%rip), %xmm1    # 0x16f150
mulsd %xmm3, %xmm1
divsd %xmm1, %xmm2
movsd 0x6454f(%rip), %xmm1    # 0x16f068
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x10ab38
leaq 0x16f5e1(%rip), %rax    # 0x27a110
movsd (%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
leaq 0x16f5c5(%rip), %rax    # 0x27a104
cvtsi2sdl (%rax), %xmm1
movsd -0x8(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x10ab5b
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x10ab70
jp 0x10ab70
jmp 0x10ae29
leaq 0x16f2d1(%rip), %rax    # 0x279e48
movsd (%rax), %xmm1
movsd -0x8(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
movsd 0x2cace7(%rip), %xmm0   # 0x3d5878
movsd %xmm0, 0x2cace7(%rip)   # 0x3d5880
leaq 0x16f520(%rip), %rax    # 0x27a0c0
movsd (%rax), %xmm0
addsd -0x8(%rbp), %xmm0
movsd %xmm0, 0x2cacc7(%rip)   # 0x3d5878
movsd 0x2cacbf(%rip), %xmm0   # 0x3d5878
callq 0x10a060
movsd %xmm0, 0x2cacb2(%rip)   # 0x3d5878
movl 0x2ce3bc(%rip), %eax    # 0x3d8f88
addl $0x1, %eax
movl %eax, 0x2ce3b3(%rip)    # 0x3d8f88
movsd 0x2cac9b(%rip), %xmm0   # 0x3d5878
leaq 0x16f4dc(%rip), %rax    # 0x27a0c0
subsd (%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
leaq 0x16ceb0(%rip), %rax    # 0x277aa4
cmpb $0x0, (%rax)
je 0x10acc9
movl $0x204, %edi            # imm = 0x204
callq 0x9ec0
movl 0x2ce37b(%rip), %eax    # 0x3d8f88
movl %eax, -0x7c(%rbp)
movsd 0x2cac60(%rip), %xmm0   # 0x3d5878
movsd %xmm0, -0x58(%rbp)
leaq 0x16f49c(%rip), %rax    # 0x27a0c0
movsd (%rax), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
leaq 0x16f4c6(%rip), %rax    # 0x27a104
movl (%rax), %eax
movl %eax, -0x68(%rbp)
leaq 0x16f4d2(%rip), %rax    # 0x27a11c
movl (%rax), %eax
movl %eax, -0x64(%rbp)
movl 0x2c6433(%rip), %eax    # 0x3d1088
movl %eax, -0x60(%rbp)
movl 0x2cac06(%rip), %eax    # 0x3d5864
movl %eax, -0x5c(%rbp)
xorl %edi, %edi
callq 0x109c20
movl -0x7c(%rbp), %edx
movsd -0x78(%rbp), %xmm1
movsd -0x70(%rbp), %xmm2
movl -0x68(%rbp), %ecx
movl -0x64(%rbp), %r8d
movl -0x60(%rbp), %r9d
movl -0x5c(%rbp), %r10d
movaps %xmm0, %xmm3
movsd -0x58(%rbp), %xmm0
movsd 0x2cabdc(%rip), %xmm4   # 0x3d5870
movl -0x18(%rbp), %eax
leaq 0x292d92(%rip), %rdi    # 0x39da30
leaq 0x83529(%rip), %rsi     # 0x18e1ce
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x5, %al
callq 0x9060
leaq 0x16f84d(%rip), %rax    # 0x27a508
movq (%rax), %rcx
leaq 0x16f7cb(%rip), %rax    # 0x27a490
movq %rcx, 0x48(%rax)
leaq 0x1928a0(%rip), %rax    # 0x29d570
cmpq $0x0, (%rax)
je 0x10adc8
leaq 0x19288f(%rip), %rax    # 0x29d570
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
movl 0x2ce297(%rip), %eax    # 0x3d8f88
movl %eax, -0xac(%rbp)
movsd 0x2cab79(%rip), %xmm0   # 0x3d5878
movsd %xmm0, -0x88(%rbp)
leaq 0x16f3b2(%rip), %rax    # 0x27a0c0
movsd (%rax), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0x28(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
leaq 0x16f3d6(%rip), %rax    # 0x27a104
movl (%rax), %eax
movl %eax, -0x98(%rbp)
leaq 0x16f3df(%rip), %rax    # 0x27a11c
movl (%rax), %eax
movl %eax, -0x94(%rbp)
movl 0x2c633d(%rip), %eax    # 0x3d1088
movl %eax, -0x90(%rbp)
movl 0x2cab0d(%rip), %eax    # 0x3d5864
movl %eax, -0x8c(%rbp)
xorl %edi, %edi
callq 0x109c20
movq -0xb8(%rbp), %rdi
movl -0xac(%rbp), %edx
movsd -0xa8(%rbp), %xmm1
movsd -0xa0(%rbp), %xmm2
movl -0x98(%rbp), %ecx
movl -0x94(%rbp), %r8d
movl -0x90(%rbp), %r9d
movl -0x8c(%rbp), %r10d
movaps %xmm0, %xmm3
movsd -0x88(%rbp), %xmm0
movsd 0x2caac1(%rip), %xmm4   # 0x3d5870
movl -0x18(%rbp), %eax
leaq 0x8347c(%rip), %rsi     # 0x18e235
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x5, %al
callq 0x9550
movsd 0x2caaa8(%rip), %xmm0   # 0x3d5878
callq 0x9a4f0
leaq 0x16f0d4(%rip), %rax    # 0x279eb0
movl $0x0, (%rax)
leaq 0x16f333(%rip), %rax    # 0x27a11c
movl (%rax), %eax
movl %eax, 0x2c6297(%rip)    # 0x3d1088
movl $0x3, %edi
callq 0x10a090
movl 0x2c0fb7(%rip), %edi    # 0x3cbdb8
movl $0x3, %esi
callq 0x95a50
movl $0x0, 0x2caa47(%rip)    # 0x3d585c
movl $0x0, 0x2caa39(%rip)    # 0x3d5858
movl $0x5, 0x2c0f8b(%rip)    # 0x3cbdb4
jmp 0x10ae2b
jmp 0x10ae2f
jmp 0x10ae2f
callq 0x10a080
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)