// File: /workspace/github_cpp_makefile_star2_O3/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
movl $0xa, %edi
callq 0xb9ad
movl $0xa, %edi
testl %eax, %eax
je 0xa9651
callq 0x1ea0b
callq 0x584bd
leaq 0x264cf4(%rip), %rcx    # 0x30e2e0
cmpb $0x0, 0x17a12(%rcx)
je 0xa95fc
movb $0x0, 0x17a12(%rcx)
leaq 0x28ec53(%rip), %rbx    # 0x338256
movzwl (%rbx), %ecx
cmpq $0x5, %rcx
ja 0xa97e7
leaq 0x765f1(%rip), %rdx     # 0x11fc08
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpl $0x10, %eax
jne 0xa962f
movw $0x1, (%rbx)
callq 0x58672
leaq 0x28ec1c(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x28ec12(%rip), %rax    # 0x338254
movw %cx, (%rax)
movw %cx, 0x2c34b4(%rip)     # 0x36cb00
jmp 0xa97f6
addq $0x8, %rsp
popq %rbx
popq %rbp
jmp 0x1e66a
movw %ax, 0x2c349f(%rip)     # 0x36cb02
movw $0x5, (%rbx)
jmp 0xa97f6
cmpl $0x10, %eax
jne 0xa9786
movw $0x3, (%rbx)
jmp 0xa97f6
cmpb $-0x1, %al
je 0xa97fd
movzbl %al, %ecx
cmpl $0x3, %ecx
je 0xa9775
movzwl 0x2c3465(%rip), %edx    # 0x36cb00
movl %edx, %ecx
shll $0x8, %ecx
shrl $0x7, %edx
andl $-0x2, %edx
xorl $0x20, %edx
leaq 0xc0cd0(%rip), %rsi     # 0x16a380
movzwl (%rdx,%rsi), %edx
xorl %edx, %ecx
movzbl %ch, %ecx
xorl %eax, %ecx
shll $0x8, %edx
xorw (%rsi,%rcx,2), %dx
movw %dx, 0x2c3437(%rip)     # 0x36cb00
leaq 0x28eb82(%rip), %rdx    # 0x338252
movzwl (%rdx), %ecx
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0xa97cf
leal 0x1(%rcx), %esi
movw %si, (%rdx)
leaq 0x28db63(%rip), %rdx    # 0x337250
movb %al, (%rdx,%rcx)
movw $0x2, (%rbx)
jmp 0xa97f6
cmpb $0x3, %al
je 0xa97e7
movzbl %al, %ecx
cmpl $0x10, %ecx
je 0xa97e7
movw $0x2, (%rbx)
leaq 0x28eb38(%rip), %rdx    # 0x338252
movzwl (%rdx), %ecx
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0xa97cf
leal 0x1(%rcx), %esi
movw %si, (%rdx)
leaq 0x28db19(%rip), %rdx    # 0x337250
movb %al, (%rdx,%rcx)
movzwl 0x2c33bf(%rip), %ecx    # 0x36cb00
movl %ecx, %edx
shrl $0x8, %edx
xorl %eax, %edx
shll $0x8, %ecx
leaq 0xc0c2e(%rip), %rax     # 0x16a380
xorw (%rax,%rdx,2), %cx
jmp 0xa9645
shll $0x8, %eax
addw 0x2c339d(%rip), %ax     # 0x36cb02
movw %ax, 0x2c3396(%rip)     # 0x36cb02
cmpw 0x2c338d(%rip), %ax     # 0x36cb00
jne 0xa97dc
callq 0xa93b7
movl $0x1, 0x2c2510(%rip)    # 0x36bc94
jmp 0xa97e7
leaq 0x28eac5(%rip), %rdx    # 0x338252
movzwl (%rdx), %ecx
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0xa97cf
movzwl 0x2c3360(%rip), %esi    # 0x36cb00
movl %esi, %edi
shrl $0x8, %edi
xorl %eax, %edi
shll $0x8, %esi
leaq 0xc0bcf(%rip), %r8      # 0x16a380
xorw (%r8,%rdi,2), %si
movw %si, 0x2c3343(%rip)     # 0x36cb00
leal 0x1(%rcx), %esi
movw %si, (%rdx)
leaq 0x28da86(%rip), %rdx    # 0x337250
movb %al, (%rdx,%rcx)
jmp 0xa97f6
leaq 0x19579a(%rip), %rax    # 0x23ef70
orb $-0x80, 0x1(%rax)
jmp 0xa97e7
leaq 0x19578d(%rip), %rax    # 0x23ef70
orb $0x40, 0x1(%rax)
leaq 0x28ea64(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
movw %cx, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %rbp
retq
movw $0x4, (%rbx)
jmp 0xa97f6