// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathgps.cpp
// Function: restore_x72_state()
void restore_x72_state()
{
unsigned i;
double val;

   // restore last known settings from disk file since STUPID Symmetricom 
   // does not save them in EEPROM

   if(x72_file) {
      fclose(x72_file);
      x72_file = 0;
   }
   x72_state_set = 1;

   sprintf(x72_name, "%s%s", dump_prefix, SYM_EEP_FILE);
   x72_file = fopen(x72_name, "r");
   if(x72_file == 0) goto open_x72_for_write;

   while(fgets(out, sizeof out, x72_file) != NULL) {  // get the last saved settings
      if(out[0] == 0) continue;

      for(i=0; i<strlen(out); i++) {   // skip leading white space
         if(out[i] == ' ') continue;
         else if(out[i] == '\t') continue;
         else break;
      }


      if(out[i] == '.') ;          // first thing on the line should be a number
      else if(out[i] == '+') ;
      else if(out[i] == '-') ;
      else if(isdigit(out[i])) ;
      else continue;               // treat anything else as a comment

      strupr(out);
if(debug_file) fprintf(debug_file, "restore:%s\n", out);  //burp

      if(strstr(out, "DIVIDER")) {
         last_x72_acmos_freq = atoi(&out[i]);
         user_set_x72_acmos_freq = 1;
      }
      else if(strstr(out, "DDS")) {
         last_x72_dds = atof(&out[i]);
         user_set_x72_dds = 1;
      }
      else if(strstr(out, "OSC")) {
         val = atof(&out[i]);
         if(val > 0.0) {
            last_x72_osc = x72_osc = atof(&out[i]);
            user_set_x72_osc = 1;
         }
      }
      else if(strstr(out, "EFC")) {
         last_x72_efc = atoi(&out[i]);
         user_set_x72_efc = 1;
      }
      else if(strstr(out, "PPS")) {
         last_x72_pps_enable = atoi(&out[i]);
         user_set_x72_pps_enable = 1;
      }
      else if(strstr(out, "TIC")) {
         last_x72_tic = atoi(&out[i]);
         user_set_x72_tic = 1;
      }
      else if(strstr(out, "SRVC")) {
         last_x72_srvc = atoi(&out[i]);
         user_set_x72_srvc = 1;
      }

      else if(strstr(out, "DISCIPLINE")) { // discipling parameters
         last_x72_dmode = atoi(&out[i]);
         user_set_x72_dmode = 1;
      }
      else if(strstr(out, "DMODE")) {      // old name for this param
         last_x72_dmode = atoi(&out[i]);
         user_set_x72_dmode = 1;
      }
      else if(strstr(out, "TC")) { 
         last_x72_tc = atoi(&out[i]);
         user_set_x72_tc = 1;
      }
      else if(strstr(out, "DAMPING")) {
         last_x72_damping = atof(&out[i]);
         user_set_x72_damping = 1;
      }
      else if(strstr(out, "JAMTHRESH")) {
         last_x72_jamthresh = atof(&out[i]);
         user_set_x72_jamthresh = 1;
      }
      else if(strstr(out, "HOLDOVER")) {
         last_x72_holdover = atoi(&out[i]);
         user_set_x72_holdover = 1;
      }
      else if(strstr(out, "EOF")) {
         break;
      }
   }

   if(x72_file) {
      fclose(x72_file);
      x72_file = 0;
   }

   open_x72_for_write:
   x72_file = fopen(x72_name, "w");

   // send the last known settings values to the device and re-write the values to the settings file
   if(user_set_x72_dmode) set_x72_discipline(last_x72_dmode);
   if((last_x72_dmode == 1) || (last_x72_dmode == 2)) {  // firmware disciplining enabled
      // don't set TC or DAMPING values... this messes up X72 firmware disciplining
   }
   else {
      if(user_set_x72_dds)     set_x72_dds(last_x72_dds);
      if(user_set_x72_tic)     set_x72_tic(last_x72_tic);
      if(user_set_x72_tc)      set_x72_tc(last_x72_tc);
      if(user_set_x72_damping) set_x72_damping(last_x72_damping);

      // software disciplining parameters
      if(user_set_x72_jamthresh)  set_x72_jamthresh(last_x72_jamthresh);
      if(user_set_x72_holdover)   set_x72_holdover(last_x72_holdover);
   }

   if(user_set_x72_acmos_freq) set_x72_acmos_freq(last_x72_acmos_freq);
   if(user_set_x72_efc)        set_x72_efc(last_x72_efc);
   if(user_set_x72_srvc)       set_x72_srvc(last_x72_srvc);
   if(user_set_x72_pps_enable) {
      user_pps_enable = last_x72_pps_enable;
      set_pps(user_pps_enable, pps_polarity,  delay_value, pps1_delay,  pps_threshold, 4);
   }

   x72_restore_timeout = 10; // pause disciplining for 10 seconds after a settings restoration
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0x1a6610(%rip), %r15    # 0x2283b8
movq (%r15), %rdi
testq %rdi, %rdi
je 0x81db9
callq 0x9360
andq $0x0, (%r15)
leaq 0x182f50(%rip), %rax    # 0x204d10
movl $0x1, (%rax)
leaq 0x1a5a93(%rip), %rbx    # 0x227860
leaq 0x73841(%rip), %rsi     # 0xf5615
leaq 0x2763e5(%rip), %rdx    # 0x2f81c0
leaq 0x8c144(%rip), %rcx     # 0x10df26
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
leaq 0x88ee2(%rip), %rsi     # 0x10acd5
movq %rbx, %rdi
callq 0x9430
movq %rax, (%r15)
testq %rax, %rax
je 0x8215f
movq %rax, %rdx
leaq 0x2a440f(%rip), %rbx    # 0x326220
leaq 0x8c125(%rip), %r14     # 0x10df3d
movabsq $0x680000000000, %r13   # imm = 0x680000000000
movq %rbx, %rdi
movl $0x201, %esi            # imm = 0x201
callq 0x9600
testq %rax, %rax
je 0x8214e
cmpb $0x0, (%rbx)
je 0x81f0a
movq %rbx, %rdi
callq 0x9120
xorl %r12d, %r12d
movl %r12d, %r12d
movsbl (%rbx,%r12), %ecx
cmpq %r12, %rax
jbe 0x81e6b
cmpb $0x20, %cl
je 0x81e66
movzbl %cl, %edx
cmpl $0x9, %edx
jne 0x81e6b
incl %r12d
jmp 0x81e4c
cmpb $0x2e, %cl
ja 0x81f12
movzbl %cl, %eax
btq %rax, %r13
jae 0x81f12
movq %rbx, %rdi
callq 0x988f
leaq 0x1a6500(%rip), %rax    # 0x228390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x81ea9
leaq 0x8c092(%rip), %rsi     # 0x10df31
movq %rbx, %rdx
xorl %eax, %eax
callq 0x9560
addq %rbx, %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9090
testq %rax, %rax
je 0x81ed6
movq %r12, %rdi
callq 0x95e0
leaq 0x183001(%rip), %rcx    # 0x204ecc
movl %eax, (%rcx)
leaq 0x182fe4(%rip), %rax    # 0x204eb8
jmp 0x81f04
movq %rbx, %rdi
leaq 0x86098(%rip), %rsi     # 0x107f78
callq 0x9090
testq %rax, %rax
je 0x81f20
movq %r12, %rdi
callq 0x9470
leaq 0x182fe7(%rip), %rax    # 0x204ee0
movsd %xmm0, (%rax)
leaq 0x182fac(%rip), %rax    # 0x204eb0
movl $0x1, (%rax)
movq (%r15), %rdx
jmp 0x81e22
addl $-0x30, %ecx
cmpl $0x9, %ecx
jbe 0x81e81
jmp 0x81f0a
movq %rbx, %rdi
leaq 0x71718(%rip), %rsi     # 0xf3642
callq 0x9090
testq %rax, %rax
je 0x81f65
movq %r12, %rdi
callq 0x9470
ucomisd 0x711d4(%rip), %xmm0    # 0xf3118
jbe 0x81f0a
leaq 0x182d1b(%rip), %rax    # 0x204c68
movsd %xmm0, (%rax)
leaq 0x182f90(%rip), %rax    # 0x204ee8
movsd %xmm0, (%rax)
leaq 0x182f51(%rip), %rax    # 0x204eb4
jmp 0x81f04
movq %rbx, %rdi
leaq 0x85f21(%rip), %rsi     # 0x107e90
callq 0x9090
testq %rax, %rax
je 0x81f96
movq %r12, %rdi
callq 0x95e0
leaq 0x182f48(%rip), %rcx    # 0x204ed0
movl %eax, (%rcx)
leaq 0x182f33(%rip), %rax    # 0x204ec4
jmp 0x81f04
movq %rbx, %rdi
leaq 0x8e338(%rip), %rsi     # 0x1102d8
callq 0x9090
testq %rax, %rax
je 0x81fc7
movq %r12, %rdi
callq 0x95e0
leaq 0x182f1b(%rip), %rcx    # 0x204ed4
movl %eax, (%rcx)
leaq 0x182efa(%rip), %rax    # 0x204ebc
jmp 0x81f04
movq %rbx, %rdi
leaq 0x958bf(%rip), %rsi     # 0x117890
callq 0x9090
testq %rax, %rax
je 0x81ff8
movq %r12, %rdi
callq 0x95e0
leaq 0x182eee(%rip), %rcx    # 0x204ed8
movl %eax, (%rcx)
leaq 0x182ecd(%rip), %rax    # 0x204ec0
jmp 0x81f04
movq %rbx, %rdi
leaq 0x8bf43(%rip), %rsi     # 0x10df45
callq 0x9090
testq %rax, %rax
je 0x82029
movq %r12, %rdi
callq 0x95e0
leaq 0x182ec1(%rip), %rcx    # 0x204edc
movl %eax, (%rcx)
leaq 0x182ea4(%rip), %rax    # 0x204ec8
jmp 0x81f04
movq %rbx, %rdi
leaq 0x8bf17(%rip), %rsi     # 0x10df4a
callq 0x9090
testq %rax, %rax
jne 0x82051
movq %rbx, %rdi
leaq 0x8bf0e(%rip), %rsi     # 0x10df55
callq 0x9090
testq %rax, %rax
je 0x8206e
movq %r12, %rdi
callq 0x95e0
leaq 0x182ec0(%rip), %rcx    # 0x204f20
movl %eax, (%rcx)
leaq 0x182e9b(%rip), %rax    # 0x204f04
jmp 0x81f04
movq %rbx, %rdi
leaq 0x9baf2(%rip), %rsi     # 0x11db6a
callq 0x9090
testq %rax, %rax
je 0x8209f
movq %r12, %rdi
callq 0x95e0
leaq 0x182e8b(%rip), %rcx    # 0x204f1c
movl %eax, (%rcx)
leaq 0x182e5a(%rip), %rax    # 0x204ef4
jmp 0x81f04
movq %rbx, %rdi
leaq 0x8beb2(%rip), %rsi     # 0x10df5b
callq 0x9090
testq %rax, %rax
je 0x820d2
movq %r12, %rdi
callq 0x9470
leaq 0x182e46(%rip), %rax    # 0x204f08
movsd %xmm0, (%rax)
leaq 0x182e2b(%rip), %rax    # 0x204ef8
jmp 0x81f04
movq %rbx, %rdi
leaq 0x8be87(%rip), %rsi     # 0x10df63
callq 0x9090
testq %rax, %rax
je 0x82105
movq %r12, %rdi
callq 0x9470
leaq 0x182e1b(%rip), %rax    # 0x204f10
movsd %xmm0, (%rax)
leaq 0x182dfc(%rip), %rax    # 0x204efc
jmp 0x81f04
movq %rbx, %rdi
leaq 0x8bf88(%rip), %rsi     # 0x10e097
callq 0x9090
testq %rax, %rax
je 0x82136
movq %r12, %rdi
callq 0x95e0
leaq 0x182df0(%rip), %rcx    # 0x204f18
movl %eax, (%rcx)
leaq 0x182dcf(%rip), %rax    # 0x204f00
jmp 0x81f04
movq %rbx, %rdi
leaq 0x8be2d(%rip), %rsi     # 0x10df6d
callq 0x9090
testq %rax, %rax
je 0x81f0a
movq (%r15), %rdi
testq %rdi, %rdi
je 0x8215f
callq 0x9360
andq $0x0, (%r15)
leaq 0x1a56fa(%rip), %rdi    # 0x227860
leaq 0x89196(%rip), %rsi     # 0x10b303
callq 0x9430
movq %rax, (%r15)
leaq 0x182d88(%rip), %rax    # 0x204f04
cmpl $0x0, (%rax)
je 0x8218f
leaq 0x182d98(%rip), %rax    # 0x204f20
movl (%rax), %edi
callq 0x647f2
leaq 0x182d8a(%rip), %rax    # 0x204f20
movl (%rax), %eax
decl %eax
cmpl $0x2, %eax
jb 0x82245
leaq 0x182d06(%rip), %rax    # 0x204eb0
cmpl $0x0, (%rax)
je 0x821bf
leaq 0x182d2a(%rip), %rax    # 0x204ee0
movsd (%rax), %xmm0
callq 0x6779b
leaq 0x182cfa(%rip), %rax    # 0x204ec0
cmpl $0x0, (%rax)
je 0x821d9
leaq 0x182d06(%rip), %rax    # 0x204ed8
movl (%rax), %edi
callq 0x82eb3
leaq 0x182d14(%rip), %rax    # 0x204ef4
cmpl $0x0, (%rax)
je 0x821f3
leaq 0x182d30(%rip), %rax    # 0x204f1c
movl (%rax), %edi
callq 0x67965
leaq 0x182cfe(%rip), %rax    # 0x204ef8
cmpl $0x0, (%rax)
je 0x8220f
leaq 0x182d02(%rip), %rax    # 0x204f08
movsd (%rax), %xmm0
callq 0x67a48
leaq 0x182ce6(%rip), %rax    # 0x204efc
cmpl $0x0, (%rax)
je 0x8222b
leaq 0x182cee(%rip), %rax    # 0x204f10
movsd (%rax), %xmm0
callq 0x678da
leaq 0x182cce(%rip), %rax    # 0x204f00
cmpl $0x0, (%rax)
je 0x82245
leaq 0x182cda(%rip), %rax    # 0x204f18
movl (%rax), %edi
callq 0x67ae4
leaq 0x182c6c(%rip), %rax    # 0x204eb8
cmpl $0x0, (%rax)
je 0x8225f
leaq 0x182c74(%rip), %rax    # 0x204ecc
movl (%rax), %edi
callq 0x82f4a
leaq 0x182c5e(%rip), %rax    # 0x204ec4
cmpl $0x0, (%rax)
je 0x82279
leaq 0x182c5e(%rip), %rax    # 0x204ed0
movl (%rax), %edi
callq 0x82e54
leaq 0x182c48(%rip), %rax    # 0x204ec8
cmpl $0x0, (%rax)
je 0x82293
leaq 0x182c50(%rip), %rax    # 0x204edc
movl (%rax), %edi
callq 0x82fa6
leaq 0x182c22(%rip), %rax    # 0x204ebc
cmpl $0x0, (%rax)
je 0x822e6
leaq 0x182c2e(%rip), %rax    # 0x204ed4
movzbl (%rax), %edi
leaq 0x275a71(%rip), %rax    # 0x2f7d21
movb %dil, (%rax)
leaq 0x275a5e(%rip), %rax    # 0x2f7d18
movzbl (%rax), %esi
leaq 0x275afc(%rip), %rax    # 0x2f7dc0
movsd (%rax), %xmm0
leaq 0x275b09(%rip), %rax    # 0x2f7dd8
movsd (%rax), %xmm1
leaq 0x275a42(%rip), %rax    # 0x2f7d1c
movss (%rax), %xmm2
pushq $0x4
popq %rdx
callq 0x61e97
movl $0xa, 0x2d48e4(%rip)    # 0x356bd4
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq