// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
leaq 0x24c80c(%rip), %rax    # 0x30dd20
cmpb $0x0, (%rax)
je 0xc168f
leaq 0x1598e0(%rip), %rax    # 0x21ae04
cmpl $0x0, (%rax)
jg 0xc155b
movl $0x190, (%rax)          # imm = 0x190
leaq 0x1599f2(%rip), %rax    # 0x21af28
xorl %ecx, %ecx
movl %ecx, (%rax)
movq %rcx, 0x2b51ef(%rip)    # 0x376730
movl %ecx, 0x2b87e9(%rip)    # 0x379d30
movl %ecx, 0x2b87e7(%rip)    # 0x379d34
movq %rcx, 0x2b87d4(%rip)    # 0x379d28
movq %rcx, 0x2b87dd(%rip)    # 0x379d38
leaq 0x1598be(%rip), %rdi    # 0x21ae20
movsd (%rdi), %xmm0
ucomisd 0x4a3b2(%rip), %xmm0    # 0x10b920
movabsq $0x4072c00000000000, %rax # imm = 0x4072C00000000000
jbe 0xc157d
movq %rax, (%rdi)
xorpd %xmm0, %xmm0
ucomisd (%rdi), %xmm0
jb 0xc158a
movq %rax, (%rdi)
leaq 0x15986b(%rip), %r8     # 0x21adfc
movl (%r8), %r13d
leaq 0x1596cd(%rip), %rsi    # 0x21ac68
cvttsd2si (%rsi), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
xorl %edx, %edx
cmpl %r13d, %ecx
cmovll %eax, %edx
subl %edx, %r13d
leaq 0x15730a(%rip), %rax    # 0x2188c4
cmpb $0x0, (%rax)
je 0xc1646
leaq 0x15995a(%rip), %rax    # 0x21af24
movl (%rax), %ebx
leaq 0x159969(%rip), %rax    # 0x21af3c
movl (%rax), %r14d
movl 0x2b08c3(%rip), %r15d   # 0x371ea0
movl 0x2b5050(%rip), %r12d   # 0x376634
callq 0xc0dcd
leaq 0x1598f0(%rip), %rax    # 0x21aee0
movsd (%rax), %xmm1
leaq 0x159801(%rip), %rax    # 0x21adfc
movl (%rax), %r10d
leaq 0x24b1b3(%rip), %rax    # 0x30c7b8
movsd (%rax), %xmm2
leaq 0x27d390(%rip), %rdi    # 0x33e9a0
leaq 0x6965c(%rip), %rsi     # 0x12ac73
movl %ebx, %edx
movl %r14d, %ecx
movl %r15d, %r8d
movl %r12d, %r9d
movb $0x3, %al
pushq %r13
pushq %r10
callq 0x9060
leaq 0x1597c8(%rip), %r8     # 0x21adfc
leaq 0x1597e5(%rip), %rdi    # 0x21ae20
leaq 0x159626(%rip), %rsi    # 0x21ac68
addq $0x10, %rsp
movl %r13d, %eax
negl %eax
cmovsl %r13d, %eax
movsd (%rdi), %xmm1
mulsd 0x5cb95(%rip), %xmm1    # 0x11e1f0
movsd (%rsi), %xmm0
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
cmpl %ecx, %eax
jle 0xc16a2
leaq 0x157252(%rip), %rax    # 0x2188c4
cmpb $0x0, (%rax)
je 0xc168f
movl (%r8), %edx
leaq 0x27d85f(%rip), %rdi    # 0x33eee0
leaq 0x6964d(%rip), %rsi     # 0x12acd5
xorl %eax, %eax
callq 0x9060
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8fcc8
leaq 0x15987b(%rip), %r12    # 0x21af24
cmpl $0x0, (%r12)
jne 0xc16bf
leaq 0x159885(%rip), %rax    # 0x21af3c
movl (%rax), %eax
movl %eax, 0x2b07e5(%rip)    # 0x371ea4
leaq 0x159876(%rip), %r13    # 0x21af3c
movl (%r13), %ebx
cvttsd2si %xmm0, %r14d
movl %r14d, %eax
shrl $0x1f, %eax
addl %r14d, %eax
sarl %eax
xorl %r15d, %r15d
cmpl %ebx, %eax
movl $0x0, %ecx
cmovll %r14d, %ecx
movl 0x2b07b6(%rip), %edx    # 0x371ea4
movl %ebx, 0x2b07b0(%rip)    # 0x371ea4
subl %ecx, %ebx
cmpl %edx, %eax
cmovgel %r15d, %r14d
subl %edx, %r14d
movl %ebx, %edi
callq 0xc10ac
addl %ebx, %r14d
addl 0x2b4f24(%rip), %r14d   # 0x376634
movl %r14d, %edx
negl %edx
cmovsl %r14d, %edx
movl 0x2b4f19(%rip), %eax    # 0x376638
incl %eax
cmpl $0x2, %edx
cmovbl %r15d, %eax
movl %r14d, 0x2b4f05(%rip)   # 0x376634
movl %eax, 0x2b4f03(%rip)    # 0x376638
seta %sil
movl (%r12), %ecx
cmpl $0x960, %ecx            # imm = 0x960
setge %dil
testb %dil, %sil
je 0xc1758
movl $0x1, %r15d
movb $0x1, %bl
xorl %edx, %edx
jmp 0xc176d
cmpl $0x9, %edx
jb 0xc1769
movl $0x2, %r15d
movb $0x1, %dl
movb $0x1, %bl
jmp 0xc176d
xorl %edx, %edx
xorl %ebx, %ebx
movl %eax, %esi
negl %esi
cmovsl %eax, %esi
cmpl $0x5, %esi
jb 0xc1a15
leaq 0x159680(%rip), %rax    # 0x21ae04
cmpl (%rax), %ecx
setl %al
notb %dl
testb %al, %dl
jne 0xc1a15
callq 0xc0dcd
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
setnp %al
sete %cl
andb %al, %cl
notb %bl
xorpd %xmm0, %xmm0
testb %cl, %bl
jne 0xc1805
movl (%r12), %eax
cmpl $0x4af, %eax            # imm = 0x4AF
jg 0xc17ef
leaq 0x159640(%rip), %rcx    # 0x21ae04
movl (%rcx), %ecx
movl %eax, %edx
subl %ecx, %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
cvtsi2sd %ecx, %xmm2
addsd %xmm2, %xmm2
divsd %xmm2, %xmm0
addsd 0x4788b(%rip), %xmm0    # 0x109070
mulsd 0x2b4e53(%rip), %xmm0   # 0x376640
jmp 0xc17fa
leaq 0x15973a(%rip), %rcx    # 0x21af30
movsd (%rcx), %xmm0
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
divsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jne 0xc1811
jnp 0xc1a15
leaq 0x159450(%rip), %rax    # 0x21ac68
divsd (%rax), %xmm0
movsd 0x2b4e24(%rip), %xmm1   # 0x376648
movsd %xmm1, 0x2b4e24(%rip)   # 0x376650
leaq 0x1596ad(%rip), %r14    # 0x21aee0
addsd (%r14), %xmm0
movsd %xmm0, 0x2b4e08(%rip)   # 0x376648
callq 0xc0f29
movsd %xmm0, 0x2b4dfb(%rip)   # 0x376648
incl 0x2b84f9(%rip)          # 0x379d4c
subsd (%r14), %xmm0
leaq 0x157065(%rip), %rax    # 0x2188c4
cmpb $0x0, (%rax)
movsd %xmm0, -0x50(%rbp)
je 0xc1921
movl $0x204, %edi            # imm = 0x204
callq 0x9d80
movl 0x2b84cf(%rip), %ebx    # 0x379d4c
movsd 0x2b4dc3(%rip), %xmm0   # 0x376648
movsd %xmm0, -0x40(%rbp)
movsd (%r14), %xmm0
movsd %xmm0, -0x38(%rbp)
movl (%r12), %r14d
leaq 0x15969d(%rip), %rax    # 0x21af3c
movl (%rax), %r13d
movl 0x2b05fc(%rip), %eax    # 0x371ea4
movl %eax, -0x2c(%rbp)
movl 0x2b4d83(%rip), %eax    # 0x376634
movq %rax, -0x48(%rbp)
callq 0xc0dcd
movaps %xmm0, %xmm3
movsd 0x2b4d7b(%rip), %xmm4   # 0x376640
leaq 0x27cf84(%rip), %rdi    # 0x33e850
leaq 0x69425(%rip), %rsi     # 0x12acf8
movl %ebx, %edx
movsd -0x40(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movsd -0x50(%rbp), %xmm2
movl %r14d, %ecx
leaq 0x1595f2(%rip), %r14    # 0x21aee0
movl %r13d, %r8d
leaq 0x159644(%rip), %r13    # 0x21af3c
movl -0x2c(%rbp), %r9d
movb $0x5, %al
pushq %r15
pushq -0x48(%rbp)
callq 0x9060
addq $0x10, %rsp
leaq 0x159a15(%rip), %rax    # 0x21b328
movq (%rax), %rax
leaq 0x159993(%rip), %rcx    # 0x21b2b0
movq %rax, 0x48(%rcx)
leaq 0x17ca68(%rip), %rax    # 0x23e390
movq (%rax), %rbx
testq %rbx, %rbx
je 0xc19bc
movq %r14, %rax
movl 0x2b840e(%rip), %r14d   # 0x379d4c
movsd 0x2b4d02(%rip), %xmm0   # 0x376648
movsd %xmm0, -0x40(%rbp)
movsd (%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movl (%r12), %eax
movl %eax, -0x2c(%rbp)
movl (%r13), %r12d
movl 0x2b053e(%rip), %r13d   # 0x371ea4
movl 0x2b4cc8(%rip), %eax    # 0x376634
movq %rax, -0x48(%rbp)
callq 0xc0dcd
movaps %xmm0, %xmm3
movsd 0x2b4cc0(%rip), %xmm4   # 0x376640
leaq 0x693d8(%rip), %rsi     # 0x12ad5f
movq %rbx, %rdi
movl %r14d, %edx
movsd -0x40(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movsd -0x50(%rbp), %xmm2
movl -0x2c(%rbp), %ecx
movl %r12d, %r8d
movl %r13d, %r9d
leaq 0x159590(%rip), %r13    # 0x21af3c
movb $0x5, %al
pushq %r15
pushq -0x48(%rbp)
callq 0x9560
addq $0x10, %rsp
movsd 0x2b4c84(%rip), %xmm0   # 0x376648
callq 0x70f27
leaq 0x159300(%rip), %rax    # 0x21acd0
xorl %ebx, %ebx
movl %ebx, (%rax)
movl (%r13), %eax
movl %eax, 0x2b04c6(%rip)    # 0x371ea4
movl $0x3, %edi
callq 0xc1019
movl 0x2ab1fa(%rip), %edi    # 0x36cbe8
incl 0x2b834c(%rip)          # 0x379d40
callq 0x8fb74
movl %ebx, 0x2b4c5d(%rip)    # 0x37665c
movl %ebx, 0x2b4c27(%rip)    # 0x37662c
movl %ebx, 0x2b4c1d(%rip)    # 0x376628
movl $0x5, 0x2ab1cf(%rip)    # 0x36cbe4
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq