// File: 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
subq $0x20, %rsp
leaq 0x1da3a9(%rip), %rax    # 0x29d598
cmpq $0x0, (%rax)
je 0xc3212
leaq 0x1da39c(%rip), %rax    # 0x29d598
movq (%rax), %rdi
callq 0x9380
leaq 0x1da38d(%rip), %rax    # 0x29d598
movq $0x0, (%rax)
leaq 0x1b6cd7(%rip), %rax    # 0x279ef0
movl $0x1, (%rax)
leaq 0x1d981a(%rip), %rdi    # 0x29ca40
leaq 0xae35d(%rip), %rsi     # 0x17158a
leaq 0x2aa16c(%rip), %rdx    # 0x36d3a0
leaq 0xc4090(%rip), %rcx     # 0x1872cb
movb $0x0, %al
callq 0x9060
leaq 0x1d97f7(%rip), %rdi    # 0x29ca40
leaq 0xc0dbd(%rip), %rsi     # 0x18400d
callq 0x9440
movq %rax, %rcx
leaq 0x1da339(%rip), %rax    # 0x29d598
movq %rcx, (%rax)
leaq 0x1da32f(%rip), %rax    # 0x29d598
cmpq $0x0, (%rax)
jne 0xc3274
jmp 0xc37e9
jmp 0xc3276
leaq 0x1da31b(%rip), %rax    # 0x29d598
movq (%rax), %rdx
leaq 0x2d8179(%rip), %rdi    # 0x39b400
movl $0x201, %esi            # imm = 0x201
callq 0x95e0
cmpq $0x0, %rax
je 0xc37bd
leaq 0x2d815e(%rip), %rax    # 0x39b400
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0xc32ac
jmp 0xc3276
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
movq %rax, -0x18(%rbp)
leaq 0x2d813f(%rip), %rdi    # 0x39b400
callq 0x9120
movq %rax, %rcx
movq -0x18(%rbp), %rax
cmpq %rcx, %rax
jae 0xc330d
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2d8122(%rip), %rax    # 0x39b400
movsbl (%rax,%rcx), %eax
cmpl $0x20, %eax
jne 0xc32e9
jmp 0xc3302
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2d810b(%rip), %rax    # 0x39b400
movsbl (%rax,%rcx), %eax
cmpl $0x9, %eax
jne 0xc3300
jmp 0xc3302
jmp 0xc330d
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0xc32b3
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2d80e7(%rip), %rax    # 0x39b400
movsbl (%rax,%rcx), %eax
cmpl $0x2e, %eax
jne 0xc3324
jmp 0xc3379
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2d80d0(%rip), %rax    # 0x39b400
movsbl (%rax,%rcx), %eax
cmpl $0x2b, %eax
jne 0xc333b
jmp 0xc3377
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2d80b9(%rip), %rax    # 0x39b400
movsbl (%rax,%rcx), %eax
cmpl $0x2d, %eax
jne 0xc3352
jmp 0xc3375
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2d80a2(%rip), %rax    # 0x39b400
movsbl (%rax,%rcx), %edi
callq 0x9330
cmpl $0x0, %eax
je 0xc336e
jmp 0xc3373
jmp 0xc3276
jmp 0xc3375
jmp 0xc3377
jmp 0xc3379
leaq 0x2d8080(%rip), %rdi    # 0x39b400
callq 0x9890
leaq 0x1da1e4(%rip), %rax    # 0x29d570
cmpq $0x0, (%rax)
je 0xc33b1
leaq 0x1da1d7(%rip), %rax    # 0x29d570
movq (%rax), %rdi
leaq 0xc3f33(%rip), %rsi     # 0x1872d6
leaq 0x2d8056(%rip), %rdx    # 0x39b400
movb $0x0, %al
callq 0x9550
leaq 0x2d8048(%rip), %rdi    # 0x39b400
leaq 0xc3f23(%rip), %rsi     # 0x1872e2
callq 0x9090
cmpq $0x0, %rax
je 0xc33f9
movl -0x4(%rbp), %eax
leaq 0x2d802c(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x95c0
movl %eax, %ecx
leaq 0x1b6cc7(%rip), %rax    # 0x27a0ac
movl %ecx, (%rax)
leaq 0x1b6caa(%rip), %rax    # 0x27a098
movl $0x1, (%rax)
jmp 0xc37b8
leaq 0x2d8000(%rip), %rdi    # 0x39b400
leaq 0xbf508(%rip), %rsi     # 0x18290f
callq 0x9090
cmpq $0x0, %rax
je 0xc3441
movl -0x4(%rbp), %eax
leaq 0x2d7fe4(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x9480
leaq 0x1b6c95(%rip), %rax    # 0x27a0c0
movsd %xmm0, (%rax)
leaq 0x1b6c5a(%rip), %rax    # 0x27a090
movl $0x1, (%rax)
jmp 0xc37b6
leaq 0x2d7fb8(%rip), %rdi    # 0x39b400
leaq 0xabeef(%rip), %rsi     # 0x16f33e
callq 0x9090
cmpq $0x0, %rax
je 0xc34b9
movl -0x4(%rbp), %eax
leaq 0x2d7f9c(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x9480
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xc34b4
movl -0x4(%rbp), %eax
leaq 0x2d7f77(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x9480
leaq 0x1b69b0(%rip), %rax    # 0x279e48
movsd %xmm0, (%rax)
leaq 0x1b6c25(%rip), %rax    # 0x27a0c8
movsd %xmm0, (%rax)
leaq 0x1b6be6(%rip), %rax    # 0x27a094
movl $0x1, (%rax)
jmp 0xc37b4
leaq 0x2d7f40(%rip), %rdi    # 0x39b400
leaq 0xbf365(%rip), %rsi     # 0x18282c
callq 0x9090
cmpq $0x0, %rax
je 0xc3501
movl -0x4(%rbp), %eax
leaq 0x2d7f24(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x95c0
movl %eax, %ecx
leaq 0x1b6bc3(%rip), %rax    # 0x27a0b0
movl %ecx, (%rax)
leaq 0x1b6bae(%rip), %rax    # 0x27a0a4
movl $0x1, (%rax)
jmp 0xc37b2
leaq 0x2d7ef8(%rip), %rdi    # 0x39b400
leaq 0xc6203(%rip), %rsi     # 0x189712
callq 0x9090
cmpq $0x0, %rax
je 0xc3549
movl -0x4(%rbp), %eax
leaq 0x2d7edc(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x95c0
movl %eax, %ecx
leaq 0x1b6b7f(%rip), %rax    # 0x27a0b4
movl %ecx, (%rax)
leaq 0x1b6b5e(%rip), %rax    # 0x27a09c
movl $0x1, (%rax)
jmp 0xc37b0
leaq 0x2d7eb0(%rip), %rdi    # 0x39b400
leaq 0xccf15(%rip), %rsi     # 0x19046c
callq 0x9090
cmpq $0x0, %rax
je 0xc3591
movl -0x4(%rbp), %eax
leaq 0x2d7e94(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x95c0
movl %eax, %ecx
leaq 0x1b6b3b(%rip), %rax    # 0x27a0b8
movl %ecx, (%rax)
leaq 0x1b6b1a(%rip), %rax    # 0x27a0a0
movl $0x1, (%rax)
jmp 0xc37ae
leaq 0x2d7e68(%rip), %rdi    # 0x39b400
leaq 0xc3d4b(%rip), %rsi     # 0x1872ea
callq 0x9090
cmpq $0x0, %rax
je 0xc35d9
movl -0x4(%rbp), %eax
leaq 0x2d7e4c(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x95c0
movl %eax, %ecx
leaq 0x1b6af7(%rip), %rax    # 0x27a0bc
movl %ecx, (%rax)
leaq 0x1b6ada(%rip), %rax    # 0x27a0a8
movl $0x1, (%rax)
jmp 0xc37ac
leaq 0x2d7e20(%rip), %rdi    # 0x39b400
leaq 0xc3d08(%rip), %rsi     # 0x1872ef
callq 0x9090
cmpq $0x0, %rax
je 0xc3621
movl -0x4(%rbp), %eax
leaq 0x2d7e04(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x95c0
movl %eax, %ecx
leaq 0x1b6af3(%rip), %rax    # 0x27a100
movl %ecx, (%rax)
leaq 0x1b6ace(%rip), %rax    # 0x27a0e4
movl $0x1, (%rax)
jmp 0xc37aa
leaq 0x2d7dd8(%rip), %rdi    # 0x39b400
leaq 0xc3ccb(%rip), %rsi     # 0x1872fa
callq 0x9090
cmpq $0x0, %rax
je 0xc3669
movl -0x4(%rbp), %eax
leaq 0x2d7dbc(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x95c0
movl %eax, %ecx
leaq 0x1b6aab(%rip), %rax    # 0x27a100
movl %ecx, (%rax)
leaq 0x1b6a86(%rip), %rax    # 0x27a0e4
movl $0x1, (%rax)
jmp 0xc37a8
leaq 0x2d7d90(%rip), %rdi    # 0x39b400
leaq 0xd3165(%rip), %rsi     # 0x1967dc
callq 0x9090
cmpq $0x0, %rax
je 0xc36b1
movl -0x4(%rbp), %eax
leaq 0x2d7d74(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x95c0
movl %eax, %ecx
leaq 0x1b6a5f(%rip), %rax    # 0x27a0fc
movl %ecx, (%rax)
leaq 0x1b6a2e(%rip), %rax    # 0x27a0d4
movl $0x1, (%rax)
jmp 0xc37a6
leaq 0x2d7d48(%rip), %rdi    # 0x39b400
leaq 0xc3c41(%rip), %rsi     # 0x187300
callq 0x9090
cmpq $0x0, %rax
je 0xc36f9
movl -0x4(%rbp), %eax
leaq 0x2d7d2c(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x9480
leaq 0x1b6a05(%rip), %rax    # 0x27a0e8
movsd %xmm0, (%rax)
leaq 0x1b69ea(%rip), %rax    # 0x27a0d8
movl $0x1, (%rax)
jmp 0xc37a4
leaq 0x2d7d00(%rip), %rdi    # 0x39b400
leaq 0xc3c01(%rip), %rsi     # 0x187308
callq 0x9090
cmpq $0x0, %rax
je 0xc373e
movl -0x4(%rbp), %eax
leaq 0x2d7ce4(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x9480
leaq 0x1b69c5(%rip), %rax    # 0x27a0f0
movsd %xmm0, (%rax)
leaq 0x1b69a6(%rip), %rax    # 0x27a0dc
movl $0x1, (%rax)
jmp 0xc37a2
leaq 0x2d7cbb(%rip), %rdi    # 0x39b400
leaq 0xc3cf0(%rip), %rsi     # 0x18743c
callq 0x9090
cmpq $0x0, %rax
je 0xc3783
movl -0x4(%rbp), %eax
leaq 0x2d7c9f(%rip), %rdi    # 0x39b400
addq %rax, %rdi
callq 0x95c0
movl %eax, %ecx
leaq 0x1b6986(%rip), %rax    # 0x27a0f8
movl %ecx, (%rax)
leaq 0x1b6965(%rip), %rax    # 0x27a0e0
movl $0x1, (%rax)
jmp 0xc37a0
leaq 0x2d7c76(%rip), %rdi    # 0x39b400
leaq 0xc3b81(%rip), %rsi     # 0x187312
callq 0x9090
cmpq $0x0, %rax
je 0xc379e
jmp 0xc37bd
jmp 0xc37a0
jmp 0xc37a2
jmp 0xc37a4
jmp 0xc37a6
jmp 0xc37a8
jmp 0xc37aa
jmp 0xc37ac
jmp 0xc37ae
jmp 0xc37b0
jmp 0xc37b2
jmp 0xc37b4
jmp 0xc37b6
jmp 0xc37b8
jmp 0xc3276
leaq 0x1d9dd4(%rip), %rax    # 0x29d598
cmpq $0x0, (%rax)
je 0xc37e7
leaq 0x1d9dc7(%rip), %rax    # 0x29d598
movq (%rax), %rdi
callq 0x9380
leaq 0x1d9db8(%rip), %rax    # 0x29d598
movq $0x0, (%rax)
jmp 0xc37e9
leaq 0x1d9250(%rip), %rdi    # 0x29ca40
leaq 0xc0e54(%rip), %rsi     # 0x18464b
callq 0x9440
movq %rax, %rcx
leaq 0x1d9d92(%rip), %rax    # 0x29d598
movq %rcx, (%rax)
leaq 0x1b68d4(%rip), %rax    # 0x27a0e4
cmpl $0x0, (%rax)
je 0xc3823
leaq 0x1b68e4(%rip), %rax    # 0x27a100
movl (%rax), %edi
callq 0x958c0
leaq 0x1b68d6(%rip), %rax    # 0x27a100
cmpl $0x1, (%rax)
je 0xc383b
leaq 0x1b68ca(%rip), %rax    # 0x27a100
cmpl $0x2, (%rax)
jne 0xc3840
jmp 0xc38e4
leaq 0x1b6849(%rip), %rax    # 0x27a090
cmpl $0x0, (%rax)
je 0xc385c
leaq 0x1b686d(%rip), %rax    # 0x27a0c0
movsd (%rax), %xmm0
callq 0x9a4f0
leaq 0x1b683d(%rip), %rax    # 0x27a0a0
cmpl $0x0, (%rax)
je 0xc3876
leaq 0x1b6849(%rip), %rax    # 0x27a0b8
movl (%rax), %edi
callq 0xc4d00
leaq 0x1b6857(%rip), %rax    # 0x27a0d4
cmpl $0x0, (%rax)
je 0xc3890
leaq 0x1b6873(%rip), %rax    # 0x27a0fc
movl (%rax), %edi
callq 0x9a7a0
leaq 0x1b6841(%rip), %rax    # 0x27a0d8
cmpl $0x0, (%rax)
je 0xc38ac
leaq 0x1b6845(%rip), %rax    # 0x27a0e8
movsd (%rax), %xmm0
callq 0x9a890
leaq 0x1b6829(%rip), %rax    # 0x27a0dc
cmpl $0x0, (%rax)
je 0xc38c8
leaq 0x1b6831(%rip), %rax    # 0x27a0f0
movsd (%rax), %xmm0
callq 0x9a6c0
leaq 0x1b6811(%rip), %rax    # 0x27a0e0
cmpl $0x0, (%rax)
je 0xc38e2
leaq 0x1b681d(%rip), %rax    # 0x27a0f8
movl (%rax), %edi
callq 0x9a950
jmp 0xc38e4
leaq 0x1b67ad(%rip), %rax    # 0x27a098
cmpl $0x0, (%rax)
je 0xc38fe
leaq 0x1b67b5(%rip), %rax    # 0x27a0ac
movl (%rax), %edi
callq 0xc4de0
leaq 0x1b679f(%rip), %rax    # 0x27a0a4
cmpl $0x0, (%rax)
je 0xc3918
leaq 0x1b679f(%rip), %rax    # 0x27a0b0
movl (%rax), %edi
callq 0xc4c90
leaq 0x1b6789(%rip), %rax    # 0x27a0a8
cmpl $0x0, (%rax)
je 0xc3932
leaq 0x1b6791(%rip), %rax    # 0x27a0bc
movl (%rax), %edi
callq 0xc4e50
leaq 0x1b6763(%rip), %rax    # 0x27a09c
cmpl $0x0, (%rax)
je 0xc3995
leaq 0x1b676f(%rip), %rax    # 0x27a0b4
movl (%rax), %eax
movb %al, %cl
leaq 0x2a95b1(%rip), %rax    # 0x36cf01
movb %cl, (%rax)
leaq 0x2a95a8(%rip), %rax    # 0x36cf01
movb (%rax), %cl
leaq 0x2a9596(%rip), %rax    # 0x36cef8
movl (%rax), %eax
leaq 0x2a9635(%rip), %rdx    # 0x36cfa0
movsd (%rdx), %xmm0
leaq 0x2a9642(%rip), %rdx    # 0x36cfb8
movsd (%rdx), %xmm1
leaq 0x2a957b(%rip), %rdx    # 0x36cefc
movss (%rdx), %xmm2
movl $0x4, %edx
movzbl %cl, %edi
movzbl %al, %esi
callq 0x91b20
movl $0xa, 0x308405(%rip)    # 0x3cbda4
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)