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

   // this routine buffers up an incoming TSIP message and then parses it
   // when it is complete.

   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 DLE
      if(c == DLE) {
         tsip_sync = 1;
         get_sync_time();
      }
      tsip_wptr = 0;
      tsip_rptr = 0;
      nvs_rx_crc = 0;
      return;
   }
   else if(tsip_sync == 1) {    // DLE had been seen, now checking next byte
      if(c == DLE) {            // DLE DLE is a 0x10 data byte
         goto rst_msg;
      }
      else if(c == ETX) {       // DLE ETX is end-of-message
         goto rst_msg;          // ... should never happen here
      }
      else {                    // DLE xx is message start
         tsip_sync = 2;         // ... so accumulate the message
         if(tsip_wptr < MAX_TSIP) {
            tsip_buf[tsip_wptr++] = c;
            nvs_rx_crc = calc_nvs_crc(nvs_rx_crc, c);
         }
         else {                 // buffer overlow
            tsip_error |= OVFL_ERROR;
            goto rst_msg;
         }
      }
   }
   else if(tsip_sync == 2) {    // buffer up the message
      if(c == DLE) tsip_sync = 3;
      else if(tsip_wptr < MAX_TSIP) {
         nvs_rx_crc = calc_nvs_crc(nvs_rx_crc, c);
         tsip_buf[tsip_wptr++] = c;
      }
      else {
         tsip_error |= OVFL_ERROR;
         goto rst_msg;
      }
   }
   else if(tsip_sync == 3) {   // last char was a DLE
      if(c == ETX) {           // DLE ETX is end-of-message
         decode_nvs_message(); // so process the buffered message
         packet_end = 1;

         rst_msg:
         tsip_wptr = 0;
         tsip_sync = 0;
      }
      else if(c == 0xFF) {   // DLE 0xFF is start of CRC
         tsip_sync = 4;
      }
      else {                   // DLE DLE is a DLE data byte and DLE xx is message ID
         nvs_rx_crc = calc_nvs_crc(nvs_rx_crc, DLE);
         nvs_rx_crc = calc_nvs_crc(nvs_rx_crc, c);
         if(tsip_wptr < MAX_TSIP) {  // so add it to the message buffer
            tsip_buf[tsip_wptr++] = c;
         }
         else {
            tsip_error |= OVFL_ERROR;
            goto rst_msg;
         }
         tsip_sync = 2;
      }
   }
   else if(tsip_sync == 4) {  // first byte of message CRC
      nvs_msg_crc = c;
      ++tsip_sync;
   }
   else if(tsip_sync == 5) {  // second byte of message CRC
      nvs_msg_crc = (c * 256) + nvs_msg_crc;
      if(nvs_msg_crc == nvs_rx_crc) {
         decode_nvs_message();
         packet_end = 1;
      }
      else {
         tsip_error |= CKSUM_ERROR;
      }
      goto rst_msg;
   }
   else {     // should never happen
      goto rst_msg;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
pushq %rax
pushq $0xa
popq %rdi
callq 0xb771
pushq $0xa
popq %rdi
testl %eax, %eax
je 0x999e2
callq 0x1d93f
callq 0x51891
leaq 0x25e964(%rip), %rcx    # 0x2f82e0
cmpb $0x0, 0x17a12(%rcx)
je 0x9998c
movb $0x0, 0x17a12(%rcx)
leaq 0x2888c3(%rip), %rbx    # 0x322256
movzwl (%rbx), %ecx
cmpq $0x5, %rcx
ja 0x99b7f
leaq 0x701a5(%rip), %rdx     # 0x109b4c
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpl $0x10, %eax
jne 0x999bf
movw $0x1, (%rbx)
callq 0x51a2f
leaq 0x28888c(%rip), %rax    # 0x322252
andw $0x0, (%rax)
leaq 0x288883(%rip), %rax    # 0x322254
andw $0x0, (%rax)
andw $0x0, 0x2bd123(%rip)    # 0x356b00
jmp 0x99b8e
addq $0x8, %rsp
popq %rbx
popq %rbp
jmp 0x1d61b
movw %ax, 0x2bd10e(%rip)     # 0x356b02
movw $0x5, (%rbx)
jmp 0x99b8e
cmpl $0x10, %eax
jne 0x99b1e
movw $0x3, (%rbx)
jmp 0x99b8e
cmpb $-0x1, %al
je 0x99b95
movzbl %al, %ecx
cmpl $0x3, %ecx
je 0x99b0d
movzwl 0x2bd0d4(%rip), %edx    # 0x356b00
movl %edx, %ecx
shll $0x8, %ecx
shrl $0x7, %edx
andl $-0x2, %edx
xorl $0x20, %edx
leaq 0xba93f(%rip), %rsi     # 0x154380
movzwl (%rdx,%rsi), %edx
xorl %edx, %ecx
movzbl %ch, %ecx
xorl %eax, %ecx
shll $0x8, %edx
xorw (%rsi,%rcx,2), %dx
movw %dx, 0x2bd0a6(%rip)     # 0x356b00
leaq 0x2887f1(%rip), %rdx    # 0x322252
movzwl (%rdx), %ecx
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0x99b67
leal 0x1(%rcx), %esi
movw %si, (%rdx)
leaq 0x2877d2(%rip), %rdx    # 0x321250
movb %al, (%rdx,%rcx)
movw $0x2, (%rbx)
jmp 0x99b8e
cmpb $0x3, %al
je 0x99b7f
movzbl %al, %ecx
cmpl $0x10, %ecx
je 0x99b7f
movw $0x2, (%rbx)
leaq 0x2887a7(%rip), %rdx    # 0x322252
movzwl (%rdx), %ecx
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0x99b67
leal 0x1(%rcx), %esi
movw %si, (%rdx)
leaq 0x287788(%rip), %rdx    # 0x321250
movb %al, (%rdx,%rcx)
movzwl 0x2bd02e(%rip), %ecx    # 0x356b00
movl %ecx, %edx
shrl $0x8, %edx
xorl %eax, %edx
shll $0x8, %ecx
leaq 0xba89d(%rip), %rax     # 0x154380
xorw (%rax,%rdx,2), %cx
movw %cx, 0x2bd012(%rip)     # 0x356b00
jmp 0x99b8e
shll $0x8, %eax
addw 0x2bd005(%rip), %ax     # 0x356b02
movw %ax, 0x2bcffe(%rip)     # 0x356b02
cmpw 0x2bcff5(%rip), %ax     # 0x356b00
jne 0x99b74
callq 0x9979e
movl $0x1, 0x2bc178(%rip)    # 0x355c94
jmp 0x99b7f
leaq 0x28872d(%rip), %rdx    # 0x322252
movzwl (%rdx), %ecx
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0x99b67
movzwl 0x2bcfc8(%rip), %esi    # 0x356b00
movl %esi, %edi
shrl $0x8, %edi
xorl %eax, %edi
shll $0x8, %esi
leaq 0xba837(%rip), %r8      # 0x154380
xorw (%r8,%rdi,2), %si
movw %si, 0x2bcfab(%rip)     # 0x356b00
leal 0x1(%rcx), %esi
movw %si, (%rdx)
leaq 0x2876ee(%rip), %rdx    # 0x321250
movb %al, (%rdx,%rcx)
jmp 0x99b8e
leaq 0x18f402(%rip), %rax    # 0x228f70
orb $-0x80, 0x1(%rax)
jmp 0x99b7f
leaq 0x18f3f5(%rip), %rax    # 0x228f70
orb $0x40, 0x1(%rax)
leaq 0x2886cc(%rip), %rax    # 0x322252
andw $0x0, (%rax)
andw $0x0, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %rbp
retq
movw $0x4, (%rbx)
jmp 0x99b8e