// File: /workspace/github_cpp_makefile_star2_O2/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 0x2996c8(%rip), %rax    # 0x32101a
cmpb $0x0, (%rax)
je 0x87966
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 0x17af58(%rip), %rax    # 0x2028d0
movq (%rax), %rax
cmpq $0x20000, %rax          # imm = 0x20000
jne 0x879c1
movsd 0x6b6c5(%rip), %xmm1    # 0xf3050
mulsd %xmm2, %xmm1
movapd %xmm2, %xmm0
cmpnlesd 0x80d64(%rip), %xmm0    # 0x108700
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 0x87ddd
movl %edx, %ebx
movl $0x80000000, %ecx       # imm = 0x80000000
cmpq %rcx, %rax
setne %cl
cmpl $0x1, %r14d
setne %dl
orb %cl, %dl
jne 0x87a18
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x879e9
jnp 0x87ac4
ucomisd %xmm0, %xmm1
jne 0x879f5
jnp 0x87ac4
cvttsd2si %xmm2, %edx
mulsd 0x6b65f(%rip), %xmm1    # 0xf3060
cvttsd2si %xmm1, %ecx
leaq 0x29e814(%rip), %rdi    # 0x326220
leaq 0x86cf1(%rip), %rsi     # 0x10e704
jmp 0x87af4
movl %esi, %r15d
cmpq $0x4, %rax
je 0x87c31
cmpq $0x80, %rax
je 0x87b2d
movabsq $0x400000000, %rcx      # imm = 0x400000000
cmpq %rcx, %rax
je 0x87be7
cmpq $0x100000, %rax         # imm = 0x100000
je 0x87bcc
cmpq $0x800, %rax            # imm = 0x800
jne 0x87957
testl %r14d, %r14d
jne 0x87957
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jae 0x87957
leaq 0x2702ee(%rip), %rbx    # 0x2f7d68
mulsd 0x6b5a6(%rip), %xmm1    # 0xf3028
cvttsd2si %xmm1, %rax
movl (%rbx), %r14d
movl %eax, (%rbx)
leaq 0x27028e(%rip), %rax    # 0x2f7d21
movzbl (%rax), %edi
leaq 0x27033b(%rip), %rax    # 0x2f7dd8
movsd (%rax), %xmm1
leaq 0x270274(%rip), %rax    # 0x2f7d1c
movss (%rax), %xmm2
movzbl %r15b, %esi
pushq $0x2d
popq %rdx
movapd %xmm4, %xmm0
callq 0x61e97
movl %r14d, (%rbx)
jmp 0x87957
leaq 0x270275(%rip), %rax    # 0x2f7d40
cvttsd2si (%rax), %edx
leaq 0x270272(%rip), %rax    # 0x2f7d48
movsd (%rax), %xmm0
mulsd 0x6b57e(%rip), %xmm0    # 0xf3060
cvttsd2si %xmm0, %ecx
leaq 0x29e733(%rip), %rdi    # 0x326220
leaq 0x86bf9(%rip), %rsi     # 0x10e6ed
xorl %eax, %eax
callq 0x9060
leaq 0x29e71e(%rip), %rdi    # 0x326220
callq 0x57791
testl %ebx, %ebx
je 0x87b15
movl $0x81, %edi
callq 0x6166c
movl $0x2253, %edi           # imm = 0x2253
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5e18e
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jae 0x87957
pushq $0x65
popq %rdi
pushq $0x7
popq %rsi
movsd %xmm1, -0x30(%rbp)
movapd %xmm2, -0x40(%rbp)
callq 0x582b6
pushq $0x3
popq %rdi
callq 0x523f2
cvttsd2si -0x40(%rbp), %rdi
callq 0x525e2
pushq $0x1
popq %rdi
callq 0x58354
movapd -0x40(%rbp), %xmm2
movsd -0x30(%rbp), %xmm1
ucomisd 0x6b59b(%rip), %xmm1    # 0xf3118
jbe 0x87957
movsd 0x6b4c5(%rip), %xmm0    # 0xf3050
divsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
pushq $0x65
popq %rdi
pushq $0x7
popq %rsi
callq 0x582b6
pushq $0x1
popq %r14
movl %r14d, %edi
callq 0x523f2
movl %ebx, %edi
callq 0x525e2
movl %r14d, %edi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x58354
ucomisd 0x6b524(%rip), %xmm1    # 0xf30f8
jne 0x87d7f
jp 0x87d7f
pushq $-0x1
jmp 0x87d8d
cmpl $0x1, %r14d
ja 0x87957
testl %r14d, %r14d
leaq 0x86b20(%rip), %rax     # 0x10e71b
leaq 0x86b20(%rip), %rsi     # 0x10e722
cmoveq %rax, %rsi
cvttsd2si %xmm2, %edx
leaq 0x29e60f(%rip), %rbx    # 0x326220
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 0x57a21
leaq 0x26f7f4(%rip), %rax    # 0x2f742c
cmpl $0x0, (%rax)
je 0x87957
movsd %xmm3, -0x50(%rbp)
pushq $0x20
popq %rsi
movl $0x631, %edi            # imm = 0x631
movsd %xmm1, -0x30(%rbp)
movapd %xmm2, -0x40(%rbp)
movsd %xmm4, -0x48(%rbp)
callq 0x57bf3
movzbl %r14b, %edi
callq 0x523f2
xorl %edi, %edi
callq 0x523f2
xorl %edi, %edi
callq 0x52599
movsd -0x48(%rbp), %xmm0
mulsd 0x6b3a2(%rip), %xmm0    # 0xf3028
cvttsd2si %xmm0, %eax
movzwl %ax, %edi
callq 0x52599
xorl %edi, %edi
callq 0x52599
xorpd %xmm2, %xmm2
movapd -0x40(%rbp), %xmm1
movapd %xmm1, %xmm0
cmpnlesd %xmm2, %xmm0
andpd %xmm0, %xmm1
movapd %xmm1, %xmm3
movsd 0x6b3b5(%rip), %xmm1    # 0xf3070
andnpd %xmm1, %xmm0
orpd %xmm3, %xmm0
movsd 0x6b39d(%rip), %xmm1    # 0xf3068
ucomisd -0x30(%rbp), %xmm2
movsd -0x30(%rbp), %xmm2
jae 0x87ce1
ucomisd 0x6b381(%rip), %xmm2    # 0xf3060
jb 0x87ce5
movapd %xmm1, %xmm2
mulsd 0x80a0b(%rip), %xmm2    # 0x1086f8
cvttsd2si %xmm2, %r12
cvttsd2si %xmm0, %r13
movl %r13d, %edi
callq 0x525e2
movl %r13d, %edi
callq 0x525e2
movl %r12d, %edi
callq 0x525e2
movl %r12d, %edi
callq 0x525e2
cvttsd2si -0x50(%rbp), %edi
callq 0x525e2
xorl %eax, %eax
testl %r15d, %r15d
sete %al
shll $0x6, %eax
leaq 0x26f771(%rip), %rcx    # 0x2f74a4
movl (%rcx), %ecx
shll $0x7, %ecx
movzbl %cl, %ecx
leal (%rax,%rcx), %edi
addl $0x2f, %edi
xorl $0x80, %edi
callq 0x525e2
callq 0x57cca
testl %ebx, %ebx
je 0x87d5d
pushq $0x6
popq %rdi
callq 0x6166c
leaq 0x26f6c8(%rip), %rax    # 0x2f742c
cmpl $0x0, (%rax)
je 0x87dbf
movl %r14d, %edi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x61e62
ucomisd 0x6dd11(%rip), %xmm1    # 0xf5a98
jne 0x87dd7
jp 0x87dd7
pushq $-0x2
popq %rdx
leaq 0x29e48b(%rip), %rbx    # 0x326220
leaq 0x8698d(%rip), %rsi     # 0x10e729
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 0x579af
movl $0x607, %edi            # imm = 0x607
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x59749
cvttsd2si %xmm1, %edx
jmp 0x87d8e