// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathgps.cpp
// Function: get_rt17_message()
void get_rt17_message()
{
u08 c;
static int status;
static int msg;
static int len, xlen;

   // This routine buffers up an incoming RT17 message.  When the end of the
   // message is seen, the message is parsed and decoded with decode_rt17_msg()
   // Some RT17 messages are spread out over several packets.  decode_rt17_msg()
   // re-assembles them into one large packet for parsing.

   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 '$'
      if(c == 0x02) {
         tsip_sync = 1;
         get_sync_time();
      }
      else if(c == 0x06) {  // ack
if(debug_file) fprintf(debug_file, "ack\n");
      }
      else if(c == 0x15) {  // nak
if(debug_file) fprintf(debug_file, "nak\n");
      }
      tsip_wptr = 0;
      tsip_rptr = 0;
      rt17_vfy_cksum = 0;  // the calculated message checksum
      rt17_msg_cksum = 0;  // the checksum in the message
      return;
   } 
   else if(tsip_sync == 1) { // STX has been seen, get status byte
      status = c;
      rt17_vfy_cksum += c;
      tsip_sync = 2;
   }
   else if(tsip_sync == 2) {  // msg type byte
      msg = c;
      rt17_vfy_cksum += c;
      tsip_sync = 3;
   }
   else if(tsip_sync == 3) {  // msg len
      len = xlen = c;
      rt17_vfy_cksum += c;
      tsip_sync = 4;
      if(len == 0) {      // null message
         tsip_sync = 5;
      }
   }
   else if(tsip_sync == 4) {  // message data
      rt17_vfy_cksum += c;
      rt17_buf[tsip_wptr++] = c;
      --len;
      if(len == 0) tsip_sync = 5;
   }
   else if(tsip_sync == 5) {  // checksum
      rt17_msg_cksum = c;
      tsip_sync = 6;
   }
   else if(tsip_sync == 6) {  // end of message
sprintf(out, "msg:%02X:%02X:%02X  status:%02X  len:%02x  etx:%02X  chk:%02X  vfy:%02X", 
msg,rt17_buf[0],rt17_buf[1],  status, xlen, c, rt17_msg_cksum&0xFF, rt17_vfy_cksum&0xFF);
if(debug_file) fprintf(debug_file, "%s\n", out);
      if(c != 0x03) goto rst_msg;  // ETX was expected

      rt17_msg_cksum &= 0xFF;
      rt17_vfy_cksum &= 0xFF;
      if(rt17_vfy_cksum != rt17_msg_cksum) goto rst_msg;  // checksums dont match

      decode_rt17_msg(msg, status);  // message checksum matches, process the message
      packet_end = 1;

      tsip_wptr = 0;     // prepare for next message
      tsip_sync = 0;
   }
   else {     // should never happen, prepare for next message
      rst_msg:
      tsip_wptr = 0;
      tsip_sync = 0;
      rt17_wptr = 0;
      rt17_vfy_cksum = rt17_msg_cksum = 0; // init received message checksum
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
pushq $0xa
popq %rdi
callq 0xb771
pushq $0xa
popq %rdi
testl %eax, %eax
je 0x754f2
callq 0x1d93f
callq 0x51891
movl %eax, %ebx
leaq 0x282e48(%rip), %rax    # 0x2f82e0
cmpb $0x0, 0x17a12(%rax)
je 0x754a8
movb $0x0, 0x17a12(%rax)
leaq 0x2acda7(%rip), %r14    # 0x322256
movzwl (%r14), %eax
cmpq $0x6, %rax
ja 0x75655
leaq 0x93b9c(%rip), %rcx     # 0x109060
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpb $0x6, %bl
je 0x7567c
movzbl %bl, %eax
cmpl $0x2, %eax
jne 0x75694
movw $0x1, (%r14)
callq 0x51a2f
jmp 0x756c3
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1d61b
addb %bl, 0x2e15b7(%rip)     # 0x356abe
leaq 0x2acd44(%rip), %rax    # 0x322252
movzwl (%rax), %ecx
leal 0x1(%rcx), %edx
movw %dx, (%rax)
leaq 0x2e58e2(%rip), %rax    # 0x35ae00
movb %bl, (%rcx,%rax)
decl 0x2e68f5(%rip)          # 0x35be1c
jne 0x756e7
movw $0x5, (%r14)
jmp 0x756e7
movl %ebx, 0x2e68da(%rip)    # 0x35be18
addb %bl, 0x2e157a(%rip)     # 0x356abe
movw $0x3, (%r14)
jmp 0x756e7
movl %ebx, 0x2e68cb(%rip)    # 0x35be20
movl %ebx, 0x2e68c1(%rip)    # 0x35be1c
addb %bl, 0x2e155d(%rip)     # 0x356abe
xorl %eax, %eax
cmpl $0x1, %ebx
adcw $0x4, %ax
movw %ax, (%r14)
jmp 0x756e7
movl %ebx, 0x2e689b(%rip)    # 0x35be14
addb %bl, 0x2e153f(%rip)     # 0x356abe
movw $0x2, (%r14)
jmp 0x756e7
movb %bl, 0x2e152f(%rip)     # 0x356abf
movw $0x6, (%r14)
jmp 0x756e7
movl 0x2e6877(%rip), %edx    # 0x35be18
movzbl 0x2e5858(%rip), %ecx    # 0x35ae00
movzbl 0x2e5851(%rip), %r8d    # 0x35ae01
movl 0x2e685d(%rip), %r9d    # 0x35be14
movl 0x2e6862(%rip), %r10d   # 0x35be20
movzbl 0x2e14f9(%rip), %r11d   # 0x356abf
movzbl 0x2e14f0(%rip), %r15d   # 0x356abe
leaq 0x2b0c4b(%rip), %rdi    # 0x326220
leaq 0x97794(%rip), %rsi     # 0x10cd70
xorl %eax, %eax
pushq %r15
pushq %r11
pushq %rbx
pushq %r10
callq 0x9060
addq $0x20, %rsp
leaq 0x1b2d9b(%rip), %rax    # 0x228390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x75612
leaq 0x9a41b(%rip), %rsi     # 0x10fa1f
leaq 0x2b0c15(%rip), %rdx    # 0x326220
xorl %eax, %eax
callq 0x9560
cmpl $0x3, %ebx
jne 0x75655
movb 0x2e14a1(%rip), %al     # 0x356abe
cmpb 0x2e149c(%rip), %al     # 0x356abf
jne 0x75655
movl 0x2e67ed(%rip), %edi    # 0x35be18
movl 0x2e67e3(%rip), %esi    # 0x35be14
callq 0x751d9
leaq 0x2acc15(%rip), %rax    # 0x322252
andw $0x0, (%rax)
movl $0x1, 0x2e0649(%rip)    # 0x355c94
andw $0x0, (%r14)
jmp 0x756e7
leaq 0x2acbf6(%rip), %rax    # 0x322252
andw $0x0, (%rax)
andw $0x0, (%r14)
andl $0x0, 0x2e6794(%rip)    # 0x35be00
xorl %eax, %eax
movb %al, 0x2e144b(%rip)     # 0x356abf
movb %al, 0x2e1444(%rip)     # 0x356abe
jmp 0x756e7
leaq 0x1b2d0d(%rip), %rax    # 0x228390
movq (%rax), %rcx
testq %rcx, %rcx
je 0x756c3
leaq 0x976d4(%rip), %rdi     # 0x10cd66
jmp 0x756b8
cmpl $0x15, %ebx
sete %al
leaq 0x1b2cef(%rip), %rcx    # 0x228390
movq (%rcx), %rcx
testq %rcx, %rcx
setne %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x756c3
leaq 0x976b3(%rip), %rdi     # 0x10cd6b
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
callq 0x9750
leaq 0x2acb88(%rip), %rax    # 0x322252
andw $0x0, (%rax)
leaq 0x2acb7f(%rip), %rax    # 0x322254
andw $0x0, (%rax)
xorl %eax, %eax
movb %al, 0x2e13dd(%rip)     # 0x356abe
movb %al, 0x2e13d8(%rip)     # 0x356abf
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq