// File: jkingan[P]ladyheather/heathgps.cpp
// Function: get_sirf_message()
void get_sirf_message()
{
u08 c;

   // This routine buffers up an incoming UBX message.  When the end of the
   // message is seen, the message is parsed and decoded with parse_ubx_msg()

   if(SERIAL_DATA_AVAILABLE(RCVR_PORT) == 0) {
      check_com_timer(RCVR_PORT);
      return;
   }
   else {
      reset_com_timer(RCVR_PORT);
   }

   c = get_com_char();
   if(com[RCVR_PORT].rcv_error) {      // parity/framing/overrun errors
      com[RCVR_PORT].rcv_error = 0;
//!!!!!qqqq    goto rst_msg;
   }


   if(tsip_sync == 0) {      // syncing to start of message, search for a 0xA0
      if(c == 0xA0) {
         tsip_sync = 1;
         get_sync_time();
      }
      tsip_wptr = 0;
      tsip_rptr = 0;
      return;
   }
   else if(tsip_sync == 1) { // 0xA0 has been seen, now checking next byte
      if(c != 0xA2) goto rst_msg;
      ++tsip_sync;           // 0xA2 seen... so accumulate the message
      sirf_rx_cksum = 0;     // init received message checksum
   }
   else if(tsip_sync == 2) { // high byte of payload length
      sirf_rx_len = ((u32) c);
      sirf_msg_len = sirf_rx_len;
      ++tsip_sync;
   }
   else if(tsip_sync == 3) {    // low byte of payload length
      sirf_rx_len *= 256;
      sirf_rx_len += (((u32) c) & 0xFF);
      sirf_msg_len = sirf_rx_len;
      ++tsip_sync;
      if(sirf_rx_len == 0) ++tsip_sync;  // message has no payload
   }
   else if(tsip_sync == 4) {   // get sirf message payload
      if(tsip_wptr < MAX_TSIP) {  // so add it to the message buffer
         sirf_rx_cksum += c;
         tsip_buf[tsip_wptr++] = c;
         --sirf_rx_len;
         if((int) sirf_rx_len <= 0) ++tsip_sync;  // message payload processed, now get checksums
      }
      else {
         tsip_error |= OVFL_ERROR;
         goto rst_msg;
      }
   }
   else if(tsip_sync == 5) {  // first (high) byte of message checksum
      sirf_msg_cksum = c;
      ++tsip_sync;
   }
   else if(tsip_sync == 6) {  // last byte of message checksum, end of sirf message
      sirf_msg_cksum = (sirf_msg_cksum * 256) + (((u16) c) & 0xFF);
      sirf_msg_cksum &= 0x7FFF;
      sirf_rx_cksum &= 0x7FFF;

      if(sirf_msg_cksum != sirf_rx_cksum) { // checksum error
         tsip_error |= CKSUM_ERROR;
         goto rst_msg;
      }
      tsip_sync = 7;
//    tsip_sync = 0;
//    decode_sirf_msg();  // message checksum matches, process the message
//    packet_end = 1;
   }
   else if(tsip_sync == 7) {  // end-of-message bytes
      tsip_sync = 8;
   }
   else if(tsip_sync == 8) {  // end-of-message bytes 
      decode_sirf_msg();  // message checksum matches, process the message
      packet_end = 1;
      tsip_sync = 0;
   }
   else {     // should never happen
      rst_msg:
      tsip_wptr = 0;
      tsip_sync = 0;
      sirf_rx_cksum = 0; // init received message checksum
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movl $0xa, %edi
callq 0xcad0
cmpl $0x0, %eax
jne 0xcd566
movl $0xa, %edi
callq 0x2b610
jmp 0xcd8e1
movl $0xa, %edi
callq 0x2bd30
callq 0x79fc0
movb %al, -0x1(%rbp)
leaq 0x29ff41(%rip), %rax    # 0x36d4c0
cmpb $0x0, 0x17a12(%rax)
je 0xcd596
leaq 0x29ff31(%rip), %rax    # 0x36d4c0
movb $0x0, 0x17a12(%rax)
leaq 0x2c9e99(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x0, %eax
jne 0xcd5de
movzbl -0x1(%rbp), %eax
cmpl $0xa0, %eax
jne 0xcd5c1
leaq 0x2c9e7f(%rip), %rax    # 0x397436
movw $0x1, (%rax)
callq 0x7a1d0
leaq 0x2c9e6a(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2c9e60(%rip), %rax    # 0x397434
movw $0x0, (%rax)
jmp 0xcd8e1
leaq 0x2c9e51(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x1, %eax
jne 0xcd623
movzbl -0x1(%rbp), %eax
cmpl $0xa2, %eax
je 0xcd5fd
jmp 0xcd8b0
leaq 0x2c9e32(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c9e24(%rip), %rax    # 0x397436
movw %cx, (%rax)
movw $0x0, 0x2fe6b2(%rip)    # 0x3cbcd0
jmp 0xcd8df
leaq 0x2c9e0c(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x2, %eax
jne 0xcd665
movzbl -0x1(%rbp), %eax
movl %eax, 0x2fe69c(%rip)    # 0x3cbcd8
movl 0x2fe696(%rip), %eax    # 0x3cbcd8
movl %eax, 0x2fe694(%rip)    # 0x3cbcdc
leaq 0x2c9de7(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c9dd9(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xcd8dd
leaq 0x2c9dca(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x3, %eax
jne 0xcd6e2
movl 0x2fe65e(%rip), %eax    # 0x3cbcd8
shll $0x8, %eax
movl %eax, 0x2fe655(%rip)    # 0x3cbcd8
movzbl -0x1(%rbp), %eax
andl $0xff, %eax
addl 0x2fe646(%rip), %eax    # 0x3cbcd8
movl %eax, 0x2fe640(%rip)    # 0x3cbcd8
movl 0x2fe63a(%rip), %eax    # 0x3cbcd8
movl %eax, 0x2fe638(%rip)    # 0x3cbcdc
leaq 0x2c9d8b(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c9d7d(%rip), %rax    # 0x397436
movw %cx, (%rax)
cmpl $0x0, 0x2fe615(%rip)    # 0x3cbcd8
jne 0xcd6dd
leaq 0x2c9d6a(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c9d5c(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xcd8db
leaq 0x2c9d4d(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x4, %eax
jne 0xcd79b
leaq 0x2c9d36(%rip), %rax    # 0x397432
movzwl (%rax), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jge 0xcd779
movzbl -0x1(%rbp), %ecx
movzwl 0x2fe5bf(%rip), %eax    # 0x3cbcd0
addl %ecx, %eax
movw %ax, 0x2fe5b6(%rip)     # 0x3cbcd0
movb -0x1(%rbp), %dl
leaq 0x2c9d0e(%rip), %rax    # 0x397432
movw (%rax), %ax
movw %ax, %si
addw $0x1, %si
leaq 0x2c9cfd(%rip), %rcx    # 0x397432
movw %si, (%rcx)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x2c8cec(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
movl 0x2fe58b(%rip), %eax    # 0x3cbcd8
addl $-0x1, %eax
movl %eax, 0x2fe582(%rip)    # 0x3cbcd8
cmpl $0x0, 0x2fe57b(%rip)    # 0x3cbcd8
jg 0xcd777
leaq 0x2c9cd0(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c9cc2(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xcd796
leaq 0x1d09d0(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x8000, %ecx           # imm = 0x8000
leaq 0x1d09c1(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
jmp 0xcd8b0
jmp 0xcd8d9
leaq 0x2c9c94(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x5, %eax
jne 0xcd7d2
movzbl -0x1(%rbp), %eax
movw %ax, 0x2fe51d(%rip)     # 0x3cbcd2
leaq 0x2c9c7a(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c9c6c(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xcd8d7
leaq 0x2c9c5d(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x6, %eax
jne 0xcd865
movzwl 0x2fe4e6(%rip), %eax    # 0x3cbcd2
shll $0x8, %eax
movzbl -0x1(%rbp), %ecx
movzwl %cx, %ecx
andl $0xff, %ecx
addl %ecx, %eax
movw %ax, 0x2fe4cd(%rip)     # 0x3cbcd2
movzwl 0x2fe4c6(%rip), %eax    # 0x3cbcd2
andl $0x7fff, %eax           # imm = 0x7FFF
movw %ax, 0x2fe4ba(%rip)     # 0x3cbcd2
movzwl 0x2fe4b1(%rip), %eax    # 0x3cbcd0
andl $0x7fff, %eax           # imm = 0x7FFF
movw %ax, 0x2fe4a5(%rip)     # 0x3cbcd0
movzwl 0x2fe4a0(%rip), %eax    # 0x3cbcd2
movzwl 0x2fe497(%rip), %ecx    # 0x3cbcd0
cmpl %ecx, %eax
je 0xcd857
leaq 0x1d090c(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x4000, %ecx           # imm = 0x4000
leaq 0x1d08fd(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
jmp 0xcd8b0
leaq 0x2c9bd8(%rip), %rax    # 0x397436
movw $0x7, (%rax)
jmp 0xcd8d5
leaq 0x2c9bca(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x7, %eax
jne 0xcd882
leaq 0x2c9bbb(%rip), %rax    # 0x397436
movw $0x8, (%rax)
jmp 0xcd8d3
leaq 0x2c9bad(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x8, %eax
jne 0xcd8ae
callq 0xcd3f0
movl $0x1, 0x2fd5d4(%rip)    # 0x3cae74
leaq 0x2c9b8f(%rip), %rax    # 0x397436
movw $0x0, (%rax)
jmp 0xcd8d1
jmp 0xcd8b0
leaq 0x2c9b7b(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2c9b73(%rip), %rax    # 0x397436
movw $0x0, (%rax)
movw $0x0, 0x2fe3ff(%rip)    # 0x3cbcd0
jmp 0xcd8d3
jmp 0xcd8d5
jmp 0xcd8d7
jmp 0xcd8d9
jmp 0xcd8db
jmp 0xcd8dd
jmp 0xcd8df
jmp 0xcd8e1
addq $0x10, %rsp
popq %rbp
retq
nopw (%rax,%rax)