// File: 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
subq $0x20, %rsp
movb %cl, %al
movb %dl, %cl
movb %sil, %dl
movl %edi, -0x4(%rbp)
movb %dl, -0x5(%rbp)
movb %cl, -0x6(%rbp)
movb %al, -0x7(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
leaq 0x30366a(%rip), %rax    # 0x3961fa
cmpb $0x0, (%rax)
je 0x92b9a
jmp 0x92db6
leaq 0x2d9a6b(%rip), %rax    # 0x36c60c
cmpl $0x0, (%rax)
je 0x92c7a
movzbl -0x6(%rbp), %eax
cmpl $0x0, %eax
jne 0x92bdc
movl -0x4(%rbp), %edi
movzbl -0x7(%rbp), %esi
movsd -0x10(%rbp), %xmm2
movsd -0x18(%rbp), %xmm3
movl -0x1c(%rbp), %edx
xorps %xmm0, %xmm0
movsd 0xdc556(%rip), %xmm1    # 0x16f128
callq 0xcb480
jmp 0x92c75
movzbl -0x5(%rbp), %eax
cmpl $0x82, %eax
jne 0x92c12
movl -0x4(%rbp), %edi
movzbl -0x7(%rbp), %esi
movsd -0x10(%rbp), %xmm2
movsd -0x18(%rbp), %xmm3
movl -0x1c(%rbp), %edx
movsd 0xdc44d(%rip), %xmm0    # 0x16f050
movsd 0xdc51d(%rip), %xmm1    # 0x16f128
callq 0xcb480
jmp 0x92c73
movzbl -0x5(%rbp), %eax
cmpl $0x83, %eax
jne 0x92c48
movl -0x4(%rbp), %edi
movzbl -0x7(%rbp), %esi
movsd -0x10(%rbp), %xmm2
movsd -0x18(%rbp), %xmm3
movl -0x1c(%rbp), %edx
movsd 0xdc3f7(%rip), %xmm0    # 0x16f030
movsd 0xdc4e7(%rip), %xmm1    # 0x16f128
callq 0xcb480
jmp 0x92c71
movl -0x4(%rbp), %edi
movzbl -0x7(%rbp), %esi
movsd -0x10(%rbp), %xmm2
movsd -0x18(%rbp), %xmm3
movl -0x1c(%rbp), %edx
movsd 0xdc404(%rip), %xmm0    # 0x16f068
movsd 0xdc4bc(%rip), %xmm1    # 0x16f128
callq 0xcb480
jmp 0x92c73
jmp 0x92c75
jmp 0x92d79
movl $0x607, %edi            # imm = 0x607
movl $0x14, %esi
callq 0x83710
movzbl -0x5(%rbp), %eax
cmpl $0x82, %eax
jne 0x92caa
movl $0x2710, %edi           # imm = 0x2710
callq 0x7b520
movl $0x64, %edi
callq 0x7b520
jmp 0x92ce1
movzbl -0x5(%rbp), %eax
cmpl $0x83, %eax
jne 0x92ccb
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x7b520
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x7b520
jmp 0x92cdf
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x7b520
movl $0x2710, %edi           # imm = 0x2710
callq 0x7b520
jmp 0x92ce1
movzbl -0x6(%rbp), %eax
cmpl $0x0, %eax
jne 0x92cf3
xorl %edi, %edi
callq 0x7b210
jmp 0x92d11
cmpb $0x0, -0x7(%rbp)
je 0x92d05
movl $0xff, %edi
callq 0x7b210
jmp 0x92d0f
movl $0x1, %edi
callq 0x7b210
jmp 0x92d11
leaq 0x2d996c(%rip), %rax    # 0x36c684
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x92d2b
xorl %edi, %edi
callq 0x7b210
jmp 0x92d35
movl $0x1, %edi
callq 0x7b210
movl $0x1, %edi
callq 0x7b210
xorl %edi, %edi
callq 0x7b210
movsd -0x10(%rbp), %xmm0
movsd 0xdc2d5(%rip), %xmm1    # 0x16f028
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movzwl %ax, %edi
callq 0x7b4b0
xorl %edi, %edi
callq 0x7b4b0
cvttsd2si -0x18(%rbp), %edi
callq 0x7b520
callq 0x83820
cmpl $0x0, -0x1c(%rbp)
je 0x92d8e
movl $0x6, %edi
movl $0x1c, %esi
callq 0x90d70
leaq 0x2d9877(%rip), %rax    # 0x36c60c
cmpl $0x0, (%rax)
je 0x92da4
movl -0x4(%rbp), %edi
callq 0x91ae0
jmp 0x92db6
cmpl $0x0, -0x4(%rbp)
jne 0x92db4
movl $0x607, %edi            # imm = 0x607
callq 0x85d00
jmp 0x92db6
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)