// File: /workspace/github_cpp_makefile_star2_O1/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 0xb9c3
movl $0xa, %edi
testl %eax, %eax
je 0xa9406
callq 0x1ea2c
callq 0x584d5
leaq 0x264f3f(%rip), %rcx    # 0x30e2e0
cmpb $0x0, 0x17a12(%rcx)
je 0xa93b1
movb $0x0, 0x17a12(%rcx)
leaq 0x28ee9e(%rip), %rbx    # 0x338256
movzwl (%rbx), %ecx
cmpq $0x5, %rcx
ja 0xa959a
leaq 0x76764(%rip), %rdx     # 0x11fb30
movslq (%rdx,%rcx,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
cmpl $0x10, %eax
jne 0xa93e4
movw $0x1, (%rbx)
callq 0x58682
leaq 0x28ee67(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x28ee5d(%rip), %rax    # 0x338254
movw %cx, (%rax)
movw %cx, 0x2c36ff(%rip)     # 0x36cb00
jmp 0xa95a9
addq $0x8, %rsp
popq %rbx
popq %rbp
jmp 0x1e6a0
movw %ax, 0x2c36ea(%rip)     # 0x36cb02
incl %ecx
movw %cx, (%rbx)
jmp 0xa95a9
cmpl $0x10, %eax
jne 0xa9539
movw $0x3, (%rbx)
jmp 0xa95a9
cmpl $0xff, %eax
je 0xa95b0
cmpl $0x3, %eax
je 0xa9528
movzwl 0x2c36b0(%rip), %edx    # 0x36cb00
movl %edx, %ecx
shll $0x8, %ecx
shrl $0x7, %edx
andl $-0x2, %edx
xorl $0x20, %edx
leaq 0xc0f1b(%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, 0x2c3682(%rip)     # 0x36cb00
leaq 0x28edcd(%rip), %rdx    # 0x338252
movzwl (%rdx), %ecx
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0xa9582
leal 0x1(%rcx), %esi
movw %si, (%rdx)
leaq 0x28ddae(%rip), %rdx    # 0x337250
movb %al, (%rdx,%rcx)
movw $0x2, (%rbx)
jmp 0xa95a9
cmpl $0x3, %eax
je 0xa959a
cmpl $0x10, %eax
je 0xa959a
movw $0x2, (%rbx)
leaq 0x28ed85(%rip), %rdx    # 0x338252
movzwl (%rdx), %ecx
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0xa9582
leal 0x1(%rcx), %esi
movw %si, (%rdx)
leaq 0x28dd66(%rip), %rdx    # 0x337250
movb %al, (%rdx,%rcx)
movzwl 0x2c360c(%rip), %ecx    # 0x36cb00
movl %ecx, %edx
shrl $0x8, %edx
xorl %eax, %edx
shll $0x8, %ecx
leaq 0xc0e7b(%rip), %rax     # 0x16a380
xorw (%rax,%rdx,2), %cx
jmp 0xa93fa
shll $0x8, %eax
addw 0x2c35ea(%rip), %ax     # 0x36cb02
movw %ax, 0x2c35e3(%rip)     # 0x36cb02
cmpw 0x2c35da(%rip), %ax     # 0x36cb00
jne 0xa958f
callq 0xa916a
movl $0x1, 0x2c275d(%rip)    # 0x36bc94
jmp 0xa959a
leaq 0x28ed12(%rip), %rdx    # 0x338252
movzwl (%rdx), %ecx
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0xa9582
movzwl 0x2c35ad(%rip), %esi    # 0x36cb00
movl %esi, %edi
shrl $0x8, %edi
xorl %eax, %edi
shll $0x8, %esi
leaq 0xc0e1c(%rip), %r8      # 0x16a380
xorw (%r8,%rdi,2), %si
movw %si, 0x2c3590(%rip)     # 0x36cb00
leal 0x1(%rcx), %esi
movw %si, (%rdx)
leaq 0x28dcd3(%rip), %rdx    # 0x337250
movb %al, (%rdx,%rcx)
jmp 0xa95a9
leaq 0x1959e7(%rip), %rax    # 0x23ef70
orb $-0x80, 0x1(%rax)
jmp 0xa959a
leaq 0x1959da(%rip), %rax    # 0x23ef70
orb $0x40, 0x1(%rax)
leaq 0x28ecb1(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
movw %cx, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %rbp
retq
movw $0x4, (%rbx)
jmp 0xa95a9