// File: 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
subq $0x10, %rsp
movl $0xa, %edi
callq 0xcad0
cmpl $0x0, %eax
jne 0xe5786
movl $0xa, %edi
callq 0x2b610
jmp 0xe5b12
movl $0xa, %edi
callq 0x2bd30
callq 0x79fc0
movb %al, -0x1(%rbp)
leaq 0x287d21(%rip), %rax    # 0x36d4c0
cmpb $0x0, 0x17a12(%rax)
je 0xe57b6
leaq 0x287d11(%rip), %rax    # 0x36d4c0
movb $0x0, 0x17a12(%rax)
leaq 0x2b1c79(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x0, %eax
jne 0xe5805
movzbl -0x1(%rbp), %eax
cmpl $0x10, %eax
jne 0xe57df
leaq 0x2b1c61(%rip), %rax    # 0x397436
movw $0x1, (%rax)
callq 0x7a1d0
leaq 0x2b1c4c(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2b1c42(%rip), %rax    # 0x397434
movw $0x0, (%rax)
movw $0x0, 0x2e64e0(%rip)    # 0x3cbce0
jmp 0xe5b12
leaq 0x2b1c2a(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x1, %eax
jne 0xe58c0
movzbl -0x1(%rbp), %eax
cmpl $0x10, %eax
jne 0xe5826
jmp 0xe5990
movzbl -0x1(%rbp), %eax
cmpl $0x3, %eax
jne 0xe5834
jmp 0xe5990
leaq 0x2b1bfb(%rip), %rax    # 0x397436
movw $0x2, (%rax)
leaq 0x2b1beb(%rip), %rax    # 0x397432
movzwl (%rax), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jge 0xe589a
movb -0x1(%rbp), %dl
leaq 0x2b1bd7(%rip), %rax    # 0x397432
movw (%rax), %ax
movw %ax, %si
addw $0x1, %si
leaq 0x2b1bc6(%rip), %rcx    # 0x397432
movw %si, (%rcx)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x2b0bb5(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
movw 0x2e645b(%rip), %ax     # 0x3cbce0
movzwl %ax, %edi
movzbl -0x1(%rbp), %esi
callq 0x7b1c0
movw %ax, 0x2e6448(%rip)     # 0x3cbce0
jmp 0xe58b7
leaq 0x1b88af(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x8000, %ecx           # imm = 0x8000
leaq 0x1b88a0(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
jmp 0xe5990
jmp 0xe58b9
jmp 0xe58bb
jmp 0xe5b10
leaq 0x2b1b6f(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x2, %eax
jne 0xe5965
movzbl -0x1(%rbp), %eax
cmpl $0x10, %eax
jne 0xe58ea
leaq 0x2b1b53(%rip), %rax    # 0x397436
movw $0x3, (%rax)
jmp 0xe5960
leaq 0x2b1b41(%rip), %rax    # 0x397432
movzwl (%rax), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jge 0xe5944
movw 0x2e63de(%rip), %ax     # 0x3cbce0
movzwl %ax, %edi
movzbl -0x1(%rbp), %esi
callq 0x7b1c0
movw %ax, 0x2e63cb(%rip)     # 0x3cbce0
movb -0x1(%rbp), %dl
leaq 0x2b1b13(%rip), %rax    # 0x397432
movw (%rax), %ax
movw %ax, %si
addw $0x1, %si
leaq 0x2b1b02(%rip), %rcx    # 0x397432
movw %si, (%rcx)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x2b0af1(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
jmp 0xe595e
leaq 0x1b8805(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x8000, %ecx           # imm = 0x8000
leaq 0x1b87f6(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
jmp 0xe5990
jmp 0xe5960
jmp 0xe5b0e
leaq 0x2b1aca(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x3, %eax
jne 0xe5a6b
movzbl -0x1(%rbp), %eax
cmpl $0x3, %eax
jne 0xe59ad
callq 0xe5500
movl $0x1, 0x2e54e4(%rip)    # 0x3cae74
leaq 0x2b1a9b(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2b1a93(%rip), %rax    # 0x397436
movw $0x0, (%rax)
jmp 0xe5a66
movzbl -0x1(%rbp), %eax
cmpl $0xff, %eax
jne 0xe59c9
leaq 0x2b1a77(%rip), %rax    # 0x397436
movw $0x4, (%rax)
jmp 0xe5a64
movl $0x10, %esi
movzwl 0x2e630b(%rip), %edi    # 0x3cbce0
callq 0x7b1c0
movw %ax, 0x2e62ff(%rip)     # 0x3cbce0
movw 0x2e62f8(%rip), %ax     # 0x3cbce0
movzwl %ax, %edi
movzbl -0x1(%rbp), %esi
callq 0x7b1c0
movw %ax, 0x2e62e5(%rip)     # 0x3cbce0
leaq 0x2b1a30(%rip), %rax    # 0x397432
movzwl (%rax), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jge 0xe5a3b
movb -0x1(%rbp), %dl
leaq 0x2b1a1c(%rip), %rax    # 0x397432
movw (%rax), %ax
movw %ax, %si
addw $0x1, %si
leaq 0x2b1a0b(%rip), %rcx    # 0x397432
movw %si, (%rcx)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x2b09fa(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
jmp 0xe5a58
leaq 0x1b870e(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x8000, %ecx           # imm = 0x8000
leaq 0x1b86ff(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
jmp 0xe5990
leaq 0x2b19d7(%rip), %rax    # 0x397436
movw $0x2, (%rax)
jmp 0xe5a66
jmp 0xe5b0c
leaq 0x2b19c4(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x4, %eax
jne 0xe5a9f
movzbl -0x1(%rbp), %eax
movw %ax, 0x2e625d(%rip)     # 0x3cbce2
leaq 0x2b19aa(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2b199c(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xe5b0a
leaq 0x2b1990(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x5, %eax
jne 0xe5b05
movzbl -0x1(%rbp), %eax
shll $0x8, %eax
movzwl 0x2e6226(%rip), %ecx    # 0x3cbce2
addl %ecx, %eax
movw %ax, 0x2e621d(%rip)     # 0x3cbce2
movzwl 0x2e6216(%rip), %eax    # 0x3cbce2
movzwl 0x2e620d(%rip), %ecx    # 0x3cbce0
cmpl %ecx, %eax
jne 0xe5ae8
callq 0xe5500
movl $0x1, 0x2e538e(%rip)    # 0x3cae74
jmp 0xe5b00
leaq 0x1b8661(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x4000, %ecx           # imm = 0x4000
leaq 0x1b8652(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
jmp 0xe5990
jmp 0xe5990
jmp 0xe5b0c
jmp 0xe5b0e
jmp 0xe5b10
jmp 0xe5b12
addq $0x10, %rsp
popq %rbp
retq
nopl (%rax,%rax)