// File: /workspace/github_cpp_makefile_star2_O1/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 0x1af9f0(%rip), %r15    # 0x23e3b8
movq (%r15), %rdi
testq %rdi, %rdi
je 0x8e9dc
callq 0x9370
movq $0x0, (%r15)
leaq 0x18c32d(%rip), %rax    # 0x21ad10
movl $0x1, (%rax)
leaq 0x1aee70(%rip), %rbx    # 0x23d860
leaq 0x7cb96(%rip), %rsi     # 0x10b58d
leaq 0x27f7c2(%rip), %rdx    # 0x30e1c0
leaq 0x954f9(%rip), %rcx     # 0x123efe
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
leaq 0x92297(%rip), %rsi     # 0x120cad
movq %rbx, %rdi
callq 0x9430
movq %rax, (%r15)
testq %rax, %rax
je 0x8edaa
movq (%r15), %rdx
leaq 0x2ad7ec(%rip), %rbx    # 0x33c220
movq %rbx, %rdi
movl $0x201, %esi            # imm = 0x201
callq 0x9600
testq %rax, %rax
je 0x8ed96
leaq 0x954c4(%rip), %r14     # 0x123f15
movabsq $0x680000000000, %r13   # imm = 0x680000000000
cmpb $0x0, (%rbx)
je 0x8eb39
movq %rbx, %rdi
callq 0x9120
testq %rax, %rax
je 0x8ea94
movl $0x1, %ecx
xorl %r12d, %r12d
movzbl (%rbx,%r12), %edx
cmpl $0x20, %edx
je 0x8ea88
cmpl $0x9, %edx
jne 0x8ea97
movl %ecx, %r12d
incl %ecx
cmpq %r12, %rax
ja 0x8ea79
jmp 0x8ea97
xorl %r12d, %r12d
movzbl (%rbx,%r12), %eax
cmpq $0x2e, %rax
ja 0x8eb57
btq %rax, %r13
jae 0x8eb57
movq %rbx, %rdi
callq 0x9899
leaq 0x1af8d1(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x8ead8
leaq 0x9543b(%rip), %rsi     # 0x123f09
movq %rbx, %rdx
xorl %eax, %eax
callq 0x9560
addq %rbx, %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9090
testq %rax, %rax
je 0x8eb05
movq %r12, %rdi
callq 0x95e0
leaq 0x18c3d2(%rip), %rcx    # 0x21aecc
movl %eax, (%rcx)
leaq 0x18c3b5(%rip), %rax    # 0x21aeb8
jmp 0x8eb33
movq %rbx, %rdi
leaq 0x8f355(%rip), %rsi     # 0x11de64
callq 0x9090
testq %rax, %rax
je 0x8eb68
movq %r12, %rdi
callq 0x9470
leaq 0x18c3b8(%rip), %rax    # 0x21aee0
movsd %xmm0, (%rax)
leaq 0x18c37d(%rip), %rax    # 0x21aeb0
movl $0x1, (%rax)
movq (%r15), %rdx
movq %rbx, %rdi
movl $0x201, %esi            # imm = 0x201
callq 0x9600
testq %rax, %rax
jne 0x8ea5b
jmp 0x8ed96
movsbl %al, %eax
addl $-0x30, %eax
cmpl $0x9, %eax
jbe 0x8eab0
jmp 0x8eb39
movq %rbx, %rdi
leaq 0x7aa70(%rip), %rsi     # 0x1095e2
callq 0x9090
testq %rax, %rax
je 0x8ebad
movq %r12, %rdi
callq 0x9470
ucomisd 0x7a58c(%rip), %xmm0    # 0x109118
jbe 0x8eb39
leaq 0x18c0d3(%rip), %rax    # 0x21ac68
movsd %xmm0, (%rax)
leaq 0x18c348(%rip), %rax    # 0x21aee8
movsd %xmm0, (%rax)
leaq 0x18c309(%rip), %rax    # 0x21aeb4
jmp 0x8eb33
movq %rbx, %rdi
leaq 0x8f1c5(%rip), %rsi     # 0x11dd7c
callq 0x9090
testq %rax, %rax
je 0x8ebde
movq %r12, %rdi
callq 0x95e0
leaq 0x18c300(%rip), %rcx    # 0x21aed0
movl %eax, (%rcx)
leaq 0x18c2eb(%rip), %rax    # 0x21aec4
jmp 0x8eb33
movq %rbx, %rdi
leaq 0x976cb(%rip), %rsi     # 0x1262b3
callq 0x9090
testq %rax, %rax
je 0x8ec0f
movq %r12, %rdi
callq 0x95e0
leaq 0x18c2d3(%rip), %rcx    # 0x21aed4
movl %eax, (%rcx)
leaq 0x18c2b2(%rip), %rax    # 0x21aebc
jmp 0x8eb33
movq %rbx, %rdi
leaq 0x9ec67(%rip), %rsi     # 0x12d880
callq 0x9090
testq %rax, %rax
je 0x8ec40
movq %r12, %rdi
callq 0x95e0
leaq 0x18c2a6(%rip), %rcx    # 0x21aed8
movl %eax, (%rcx)
leaq 0x18c285(%rip), %rax    # 0x21aec0
jmp 0x8eb33
movq %rbx, %rdi
leaq 0x952d3(%rip), %rsi     # 0x123f1d
callq 0x9090
testq %rax, %rax
je 0x8ec71
movq %r12, %rdi
callq 0x95e0
leaq 0x18c279(%rip), %rcx    # 0x21aedc
movl %eax, (%rcx)
leaq 0x18c25c(%rip), %rax    # 0x21aec8
jmp 0x8eb33
movq %rbx, %rdi
leaq 0x952a7(%rip), %rsi     # 0x123f22
callq 0x9090
testq %rax, %rax
jne 0x8ec99
movq %rbx, %rdi
leaq 0x9529e(%rip), %rsi     # 0x123f2d
callq 0x9090
testq %rax, %rax
je 0x8ecb6
movq %r12, %rdi
callq 0x95e0
leaq 0x18c278(%rip), %rcx    # 0x21af20
movl %eax, (%rcx)
leaq 0x18c253(%rip), %rax    # 0x21af04
jmp 0x8eb33
movq %rbx, %rdi
leaq 0xa4e9a(%rip), %rsi     # 0x133b5a
callq 0x9090
testq %rax, %rax
je 0x8ece7
movq %r12, %rdi
callq 0x95e0
leaq 0x18c243(%rip), %rcx    # 0x21af1c
movl %eax, (%rcx)
leaq 0x18c212(%rip), %rax    # 0x21aef4
jmp 0x8eb33
movq %rbx, %rdi
leaq 0x95242(%rip), %rsi     # 0x123f33
callq 0x9090
testq %rax, %rax
je 0x8ed1a
movq %r12, %rdi
callq 0x9470
leaq 0x18c1fe(%rip), %rax    # 0x21af08
movsd %xmm0, (%rax)
leaq 0x18c1e3(%rip), %rax    # 0x21aef8
jmp 0x8eb33
movq %rbx, %rdi
leaq 0x95217(%rip), %rsi     # 0x123f3b
callq 0x9090
testq %rax, %rax
je 0x8ed4d
movq %r12, %rdi
callq 0x9470
leaq 0x18c1d3(%rip), %rax    # 0x21af10
movsd %xmm0, (%rax)
leaq 0x18c1b4(%rip), %rax    # 0x21aefc
jmp 0x8eb33
movq %rbx, %rdi
leaq 0x95318(%rip), %rsi     # 0x12406f
callq 0x9090
testq %rax, %rax
je 0x8ed7e
movq %r12, %rdi
callq 0x95e0
leaq 0x18c1a8(%rip), %rcx    # 0x21af18
movl %eax, (%rcx)
leaq 0x18c187(%rip), %rax    # 0x21af00
jmp 0x8eb33
movq %rbx, %rdi
leaq 0x951bd(%rip), %rsi     # 0x123f45
callq 0x9090
testq %rax, %rax
je 0x8eb39
movq (%r15), %rdi
testq %rdi, %rdi
je 0x8edaa
callq 0x9370
movq $0x0, (%r15)
leaq 0x1aeaaf(%rip), %rdi    # 0x23d860
leaq 0x92523(%rip), %rsi     # 0x1212db
callq 0x9430
movq %rax, (%r15)
leaq 0x18c13d(%rip), %rax    # 0x21af04
cmpl $0x0, (%rax)
je 0x8edda
leaq 0x18c14d(%rip), %rax    # 0x21af20
movl (%rax), %edi
callq 0x6dc2d
leaq 0x18c13f(%rip), %rax    # 0x21af20
movl (%rax), %eax
decl %eax
cmpl $0x2, %eax
jb 0x8ee90
leaq 0x18c0bb(%rip), %rax    # 0x21aeb0
cmpl $0x0, (%rax)
je 0x8ee0a
leaq 0x18c0df(%rip), %rax    # 0x21aee0
movsd (%rax), %xmm0
callq 0x70f27
leaq 0x18c0af(%rip), %rax    # 0x21aec0
cmpl $0x0, (%rax)
je 0x8ee24
leaq 0x18c0bb(%rip), %rax    # 0x21aed8
movl (%rax), %edi
callq 0x8fb74
leaq 0x18c0c9(%rip), %rax    # 0x21aef4
cmpl $0x0, (%rax)
je 0x8ee3e
leaq 0x18c0e5(%rip), %rax    # 0x21af1c
movl (%rax), %edi
callq 0x710f5
leaq 0x18c0b3(%rip), %rax    # 0x21aef8
cmpl $0x0, (%rax)
je 0x8ee5a
leaq 0x18c0b7(%rip), %rax    # 0x21af08
movsd (%rax), %xmm0
callq 0x711d8
leaq 0x18c09b(%rip), %rax    # 0x21aefc
cmpl $0x0, (%rax)
je 0x8ee76
leaq 0x18c0a3(%rip), %rax    # 0x21af10
movsd (%rax), %xmm0
callq 0x7106a
leaq 0x18c083(%rip), %rax    # 0x21af00
cmpl $0x0, (%rax)
je 0x8ee90
leaq 0x18c08f(%rip), %rax    # 0x21af18
movl (%rax), %edi
callq 0x71276
leaq 0x18c021(%rip), %rax    # 0x21aeb8
cmpl $0x0, (%rax)
je 0x8eeaa
leaq 0x18c029(%rip), %rax    # 0x21aecc
movl (%rax), %edi
callq 0x8fc0d
leaq 0x18c013(%rip), %rax    # 0x21aec4
cmpl $0x0, (%rax)
je 0x8eec4
leaq 0x18c013(%rip), %rax    # 0x21aed0
movl (%rax), %edi
callq 0x8fb15
leaq 0x18bffd(%rip), %rax    # 0x21aec8
cmpl $0x0, (%rax)
je 0x8eede
leaq 0x18c005(%rip), %rax    # 0x21aedc
movl (%rax), %edi
callq 0x8fc69
leaq 0x18bfd7(%rip), %rax    # 0x21aebc
cmpl $0x0, (%rax)
je 0x8ef33
leaq 0x18bfe3(%rip), %rax    # 0x21aed4
movzbl (%rax), %edi
leaq 0x27ee26(%rip), %rax    # 0x30dd21
movb %dil, (%rax)
leaq 0x27ee13(%rip), %rax    # 0x30dd18
movzbl (%rax), %esi
leaq 0x27eeb1(%rip), %rax    # 0x30ddc0
movsd (%rax), %xmm0
leaq 0x27eebe(%rip), %rax    # 0x30ddd8
movsd (%rax), %xmm1
leaq 0x27edf7(%rip), %rax    # 0x30dd1c
movss (%rax), %xmm2
movl $0x4, %edx
callq 0x6ad1d
movl $0xa, 0x2ddc97(%rip)    # 0x36cbd4
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq