// File: /workspace/github_cpp_makefile_star2_O1/jkingan[P]ladyheather/heathgps.cpp
// Function: set_pps_freq(int, double, double, int, double, double, int)
void set_pps_freq(int chan, double freq, double duty, int polarity, double cable_delay, double pps_delay, int save)
{
u32 flags;
u32 val;
int i;

   if(read_only) return;

   if(rcvr_type == CS_RCVR) {
      if(freq <= 11.0) freq *= 1.0E6;
      set_cs_freq(chan+1, freq);
   }
   else if((rcvr_type == ESIP_RCVR) && (chan == 1)) {  // GCLK
      if((freq == 0.0) || (duty == 0.0)) {
         sprintf(out, "PERDAPI,FREQ,0,%d,%d,0", (int) pps2_freq, (int) (pps2_duty*100.0));
      }
      else {
         sprintf(out, "PERDAPI,FREQ,1,%d,%d,0", (int) freq, (int) (duty*100.0));
      }
      send_esip_cmd(out);
      if(save) save_segment(129, 87);
      request_rcvr_config(8787);
   }
   else if(rcvr_type == NVS_RCVR) {
      if(chan != 0) return;
      if(freq <= 0.0) return;
      freq = 1.0;  // can only do 1pps

      val = nvs_pps_width;
      nvs_pps_width = (u32) ((1.0E9 / freq) * duty);
//sprintf(debug_text, "nvs set width: %d ns", nvs_pps_width);
      set_pps(user_pps_enable, polarity,  cable_delay, pps1_delay,  pps_threshold, 45);
      nvs_pps_width = val;
   }
   else if(rcvr_type == TM4_RCVR) {
      if     (chan == 0) sprintf(out, "#09,%d", (int) freq);
      else if(chan == 1) sprintf(out, "#14,%d", (int) freq); 
      else return;
      send_tm4_cmd(out);
   }
   else if(rcvr_type == UBX_RCVR) {
      if(saw_ubx_tp5 == 0) return;

      send_ubx_start(UBX_TP5, 32);

      send_byte(chan); // output number (0 / 1)
      send_byte(0);    // rsvd
      send_word(0);    // rsvd
      send_word((s16) (cable_delay*1.0E9));
      send_word(0);    // rf delay

      flags = 0x0001;     // enable PPS
      if(freq <= 0.0) {
         flags = 0x0000;  // disable PPS
         freq = 1.0;
      }

      if(duty <= 0.0) duty = 0.500;
      else if(duty >= 100.0) duty = 0.500;
      val = (u32) (duty * (double) 0xFFFFFFFFU);

      send_dword((u32) freq); // freq / period
      send_dword((u32) freq); // gps locked freq/period

      send_dword(val); // ratio / duty cycle
      send_dword(val); // gps locked ratio / duty cycle

      send_dword((S32) pps_delay);

      flags |= 0x0001;  // !!!! pulse active
      flags |= 0x0002;  // sync to GPS
      flags |= 0x0004;  // !!!! enable alt values if not locked to GPS

      flags |= 0x0008;  // 1=freq mode         0=rate mode
//    flags |= 0x0010;  // 1=pulse width mode  0=duty cycle mode
      flags |= 0x0020;  // !!!! align pulse to TOW
      if(polarity == 0) flags |= 0x0040;   // flag bit: 1=rising edge  0=falling edge
      if((time_flags & TFLAGS_UTC) == 0) flags |= 0x0080;  // sync to GPS time

// !!!!! bits 8/9/10/11 are what GNSS time system to use !!!!!
//       0=UTC 1=GPS 2=GLONASS 3=BEIDOU 4=GALILEO
// !!!!! bits 12/13/14 are how to sync time

      send_dword(flags);    // flags

      send_ubx_end();

// sprintf(plot_title, "SETPPS %d: %lf %lf flags:%08X", chan, freq, duty, flags); 

      if(save) save_segment(6, 29);

      if(saw_ubx_tp5) request_ubx_tp5(chan);
      else            request_ubx_msg(UBX_PPS);
   }
   else if(rcvr_type == VENUS_RCVR) {
//    if(chan != 0) return;
      if(freq <= 0.0) return;

      send_venus_start(SET_VENUS_FREQ);
      send_byte(3);
      send_dword((u32) freq);
      send_venus_save(1);

      if(duty > 0.0) {
         val = (u32) ((1.0E6 / freq) * duty);
         send_venus_start(SET_VENUS_WIDTH);
         send_byte(1);
         send_dword(val);
         send_venus_save(1);
      }
   }
   else if(rcvr_type == ZYFER_RCVR) {
      if(duty == (-1.0)) i = (-1);
      else if(duty == (-2.0)) i = (-2);
      else i = (int) duty;
      sprintf(out, "$PULW,%d", i);
      send_zyfer_cmd(out);
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
leaq 0x2a223a(%rip), %rax    # 0x33701a
cmpb $0x0, (%rax)
je 0x94df4
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movapd %xmm2, %xmm4
movapd %xmm0, %xmm2
movl %edi, %r14d
leaq 0x183aca(%rip), %rax    # 0x2188d0
movq (%rax), %rax
cmpq $0x20000, %rax          # imm = 0x20000
jne 0x94e4f
movsd 0x74237(%rip), %xmm1    # 0x109050
mulsd %xmm2, %xmm1
movapd %xmm2, %xmm0
cmpnlesd 0x898ae(%rip), %xmm0    # 0x11e6d8
andpd %xmm0, %xmm2
andnpd %xmm1, %xmm0
orpd %xmm2, %xmm0
incl %r14d
movl %r14d, %edi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x95286
movl %edx, %ebx
movl $0x80000000, %ecx       # imm = 0x80000000
cmpq %rcx, %rax
setne %cl
cmpl $0x1, %r14d
setne %dl
orb %cl, %dl
jne 0x94ea6
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x94e77
jnp 0x94f4a
ucomisd %xmm0, %xmm1
jne 0x94e83
jnp 0x94f4a
cvttsd2si %xmm2, %edx
mulsd 0x741d1(%rip), %xmm1    # 0x109060
cvttsd2si %xmm1, %ecx
leaq 0x2a7386(%rip), %rdi    # 0x33c220
leaq 0x8f83b(%rip), %rsi     # 0x1246dc
jmp 0x94f7a
movl %esi, %r15d
cmpq $0x7ff, %rax            # imm = 0x7FF
jle 0x94fb3
movabsq $0x400000000, %rcx      # imm = 0x400000000
cmpq %rcx, %rax
je 0x95074
cmpq $0x100000, %rax         # imm = 0x100000
je 0x950be
cmpq $0x800, %rax            # imm = 0x800
jne 0x94de5
testl %r14d, %r14d
jne 0x94de5
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jae 0x94de5
leaq 0x278e6a(%rip), %rbx    # 0x30dd68
mulsd 0x74122(%rip), %xmm1    # 0x109028
cvttsd2si %xmm1, %rax
movl (%rbx), %r14d
movl %eax, (%rbx)
leaq 0x278e0a(%rip), %rax    # 0x30dd21
movzbl (%rax), %edi
leaq 0x278eb7(%rip), %rax    # 0x30ddd8
movsd (%rax), %xmm1
leaq 0x278df0(%rip), %rax    # 0x30dd1c
movss (%rax), %xmm2
movzbl %r15b, %esi
movapd %xmm4, %xmm0
movl $0x2d, %edx
callq 0x6ad1d
movl %r14d, (%rbx)
jmp 0x94de5
leaq 0x278def(%rip), %rax    # 0x30dd40
cvttsd2si (%rax), %edx
leaq 0x278dec(%rip), %rax    # 0x30dd48
movsd (%rax), %xmm0
mulsd 0x740f8(%rip), %xmm0    # 0x109060
cvttsd2si %xmm0, %ecx
leaq 0x2a72ad(%rip), %rdi    # 0x33c220
leaq 0x8f74b(%rip), %rsi     # 0x1246c5
xorl %eax, %eax
callq 0x9060
leaq 0x2a7298(%rip), %rdi    # 0x33c220
callq 0x5ef13
testl %ebx, %ebx
je 0x94f9b
movl $0x81, %edi
callq 0x6a306
movl $0x2253, %edi           # imm = 0x2253
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x66617
cmpq $0x4, %rax
je 0x95115
cmpq $0x80, %rax
jne 0x94de5
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jae 0x94de5
movl $0x65, %edi
movl $0x7, %esi
movsd %xmm1, -0x30(%rbp)
movapd %xmm2, -0x40(%rbp)
callq 0x5fbb4
movl $0x3, %edi
callq 0x591af
cvttsd2si -0x40(%rbp), %rdi
callq 0x593b5
movl $0x1, %edi
callq 0x5fc43
movapd -0x40(%rbp), %xmm2
movsd -0x30(%rbp), %xmm1
ucomisd 0x740f7(%rip), %xmm1    # 0x109118
jbe 0x94de5
movsd 0x74021(%rip), %xmm0    # 0x109050
divsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
movl $0x65, %edi
movl $0x7, %esi
callq 0x5fbb4
movl $0x1, %edi
callq 0x591af
movl %ebx, %edi
callq 0x593b5
movl $0x1, %edi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5fc43
cmpl $0x1, %r14d
ja 0x94de5
testl %r14d, %r14d
leaq 0x8f66b(%rip), %rax     # 0x1246f3
leaq 0x8f66b(%rip), %rsi     # 0x1246fa
cmoveq %rax, %rsi
cvttsd2si %xmm2, %edx
leaq 0x2a7182(%rip), %rbx    # 0x33c220
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5f1e8
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
ucomisd 0x7402d(%rip), %xmm1    # 0x1090f8
jne 0x950cf
jnp 0x950e4
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
ucomisd 0x76944(%rip), %xmm1    # 0x10ba20
jne 0x950e0
jnp 0x950e4
cvttsd2si %xmm1, %edx
leaq 0x2a7135(%rip), %rbx    # 0x33c220
leaq 0x8f60f(%rip), %rsi     # 0x124701
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
incq %rbx
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5f15d
leaq 0x278310(%rip), %rax    # 0x30d42c
cmpl $0x0, (%rax)
je 0x94de5
movsd %xmm3, -0x50(%rbp)
movl $0x631, %edi            # imm = 0x631
movl $0x20, %esi
movsd %xmm1, -0x30(%rbp)
movapd %xmm2, -0x40(%rbp)
movsd %xmm4, -0x48(%rbp)
callq 0x5f3f5
movzbl %r14b, %edi
callq 0x591af
xorl %edi, %edi
callq 0x591af
xorl %edi, %edi
callq 0x5936c
movsd -0x48(%rbp), %xmm0
mulsd 0x73ebc(%rip), %xmm0    # 0x109028
cvttsd2si %xmm0, %eax
movzwl %ax, %edi
callq 0x5936c
xorl %edi, %edi
callq 0x5936c
xorpd %xmm2, %xmm2
movapd -0x40(%rbp), %xmm1
movapd %xmm1, %xmm0
cmpnlesd %xmm2, %xmm0
andpd %xmm0, %xmm1
movapd %xmm1, %xmm3
movsd 0x73ecf(%rip), %xmm1    # 0x109070
andnpd %xmm1, %xmm0
orpd %xmm3, %xmm0
movsd 0x73eb7(%rip), %xmm1    # 0x109068
ucomisd -0x30(%rbp), %xmm2
movsd -0x30(%rbp), %xmm2
jae 0x951c7
ucomisd 0x73e9b(%rip), %xmm2    # 0x109060
jb 0x951cb
movapd %xmm1, %xmm2
mulsd 0x894fd(%rip), %xmm2    # 0x11e6d0
cvttsd2si %xmm2, %r12
cvttsd2si %xmm0, %r13
movl %r13d, %edi
callq 0x593b5
movl %r13d, %edi
callq 0x593b5
movl %r12d, %edi
callq 0x593b5
movl %r12d, %edi
callq 0x593b5
cvttsd2si -0x50(%rbp), %edi
callq 0x593b5
xorl %eax, %eax
testl %r15d, %r15d
sete %al
shll $0x6, %eax
leaq 0x27828b(%rip), %rcx    # 0x30d4a4
movl (%rcx), %ecx
shll $0x7, %ecx
movzbl %cl, %ecx
leal (%rax,%rcx), %edi
addl $0x2f, %edi
xorl $0x80, %edi
callq 0x593b5
callq 0x5f4ce
testl %ebx, %ebx
je 0x95245
movl $0x6, %edi
callq 0x6a306
leaq 0x2781e0(%rip), %rax    # 0x30d42c
cmpl $0x0, (%rax)
je 0x95267
movl %r14d, %edi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6ace6
movl $0x607, %edi            # imm = 0x607
xorl %esi, %esi
callq 0x5f3f5
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5f4ce