// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathgps.cpp
// Function: set_ubx_pps(int, unsigned char, unsigned char, unsigned char, double, double, int)
void set_ubx_pps(int chan, u08 pps_rate, u08 pps_enable,  u08 pps_polarity,  double delay, double pps_delay, int save)
{
   if(read_only) return;

   if(saw_ubx_tp5) {
      if(pps_enable == 0) {
//sprintf(debug_text2, "pps disable:%02X  tp5:%02X", pps_rate, saw_ubx_tp5); 
         set_pps_freq(chan, 0.0, 0.10, pps_polarity, delay, pps_delay, save);
      }
      else {
         if(pps_rate == RATE_100PPS) {  // 100 hz
            set_pps_freq(chan, 100.0, 0.10, pps_polarity, delay, pps_delay, save);
         }
         else if(pps_rate == RATE_1000PPS) { // 1000 Hz
            set_pps_freq(chan, 1000.0, 0.10, pps_polarity, delay, pps_delay, save);
         }
         else {
            set_pps_freq(chan, 1.0, 0.10, pps_polarity, delay, pps_delay, save);
         }
//sprintf(debug_text2, "pps enable:%02X  tp5:%02X", pps_rate, saw_ubx_tp5);
      }
   }
   else {
      send_ubx_start(UBX_PPS, 20);

      if(pps_rate == RATE_100PPS) {
         send_dword(10000);    // 100 pps
         send_dword(100);      // 100 us width
      }
      else if(pps_rate == RATE_1000PPS) {
         send_dword(1000);     // 1000 pps
         send_dword(500);      // 500 us width
      }
      else {
         send_dword(1000000);  // 1 sec
         send_dword(10000);    // 10 msec width
      }

      if(pps_enable == 0)   send_byte(0);    // pulse off
      else if(pps_polarity) send_byte(0xFF); // neg pulse
      else                  send_byte(1);    // pos pulse

      if(time_flags & TFLAGS_UTC) send_byte(0);  // align to UTC
      else                        send_byte(1);  // align to GPS

      send_byte(1);  // always on

      send_byte(0);  // rsvd

      send_word((s16) (delay*1.0E9));
      send_word(0);  // rf delay
      send_dword((S32) pps_delay);

      send_ubx_end();
   }


   if(save) save_segment(6, 28);  // save PPS timing config in EEPROM

   if(saw_ubx_tp5) request_ubx_tp5(chan);
   else if(chan == 0) request_ubx_msg(UBX_PPS);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leaq 0x2cb45a(%rip), %rax    # 0x33701a
cmpb $0x0, (%rax)
je 0x6bbd4
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r8d, %r14d
movaps %xmm1, %xmm3
movapd %xmm0, %xmm2
movl %ecx, %r12d
movl %edx, %r15d
movl %esi, %r13d
movl %edi, %ebx
leaq 0x2a183c(%rip), %rax    # 0x30d42c
cmpl $0x0, (%rax)
je 0x6bc19
movzbl %r12b, %esi
testb %r15b, %r15b
je 0x6bc50
cmpb $-0x7d, %r13b
je 0x6bc5e
movzbl %r13b, %eax
cmpl $0x82, %eax
jne 0x6bc68
movsd 0x9d449(%rip), %xmm0    # 0x109060
jmp 0x6bc70
movsd %xmm2, -0x38(%rbp)
movsd %xmm3, -0x40(%rbp)
movl $0x607, %edi            # imm = 0x607
movl $0x14, %esi
callq 0x5f37d
cmpb $-0x7e, %r13b
je 0x6bc87
movzbl %r13b, %eax
cmpl $0x83, %eax
jne 0x6bc94
movl $0x1f4, %r13d           # imm = 0x1F4
movl $0x3e8, %edi            # imm = 0x3E8
jmp 0x6bc9f
movsd 0x9d4b0(%rip), %xmm1    # 0x109108
xorpd %xmm0, %xmm0
jmp 0x6bc78
movsd 0x9d3d2(%rip), %xmm0    # 0x109038
jmp 0x6bc70
movsd 0x9d400(%rip), %xmm0    # 0x109070
movsd 0x9d490(%rip), %xmm1    # 0x109108
movl %ebx, %edi
movl %r14d, %edx
callq 0x94e4c
jmp 0x6bd6f
movl $0x64, %r13d
movl $0x2710, %edi           # imm = 0x2710
jmp 0x6bc9f
movl $0x2710, %r13d          # imm = 0x2710
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x592eb
movl %r13d, %edi
callq 0x592eb
xorl %eax, %eax
negb %r12b
movl $0x0, %ecx
sbbl %ecx, %ecx
orb $0x1, %cl
testb %r15b, %r15b
movzbl %cl, %ecx
cmovel %eax, %ecx
movzbl %cl, %edi
callq 0x590f7
leaq 0x2a17d1(%rip), %rax    # 0x30d4a4
movb (%rax), %al
notb %al
andb $0x1, %al
movzbl %al, %edi
callq 0x590f7
movl $0x1, %edi
callq 0x590f7
xorl %edi, %edi
callq 0x590f7
movsd -0x38(%rbp), %xmm0
mulsd 0x9d329(%rip), %xmm0    # 0x109028
cvttsd2si %xmm0, %eax
movw %ax, -0x2a(%rbp)
leaq 0x1acac6(%rip), %r15    # 0x2187d4
xorl %eax, %eax
cmpb $0x0, (%r15)
sete %al
movzbl -0x2a(%rbp,%rax), %edi
callq 0x590f7
xorl %eax, %eax
cmpb $0x0, (%r15)
setne %al
movzbl -0x2a(%rbp,%rax), %edi
callq 0x590f7
movw $0x0, -0x2c(%rbp)
xorl %eax, %eax
cmpb $0x0, (%r15)
sete %al
movzbl -0x2c(%rbp,%rax), %edi
callq 0x590f7
xorl %eax, %eax
cmpb $0x0, (%r15)
setne %al
movzbl -0x2c(%rbp,%rax), %edi
callq 0x590f7
cvttsd2si -0x40(%rbp), %edi
callq 0x592eb
callq 0x5f456
testl %r14d, %r14d
je 0x6bd7e
movl $0x6, %edi
callq 0x6a4ce
leaq 0x2a16a7(%rip), %rax    # 0x30d42c
cmpl $0x0, (%rax)
je 0x6bd9f
movl %ebx, %edi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6ada1
testl %ebx, %ebx
jne 0x6bbc5
movl $0x607, %edi            # imm = 0x607
xorl %esi, %esi
callq 0x5f37d
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5f456