// File: jkingan[P]ladyheather/heathgps.cpp
// Function: get_tsip_message()
void get_tsip_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;
      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;
         }
         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) {
         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_tsip_message(); // so process the buffered message
         packet_end = 1;

         rst_msg:
         tsip_wptr = 0;
         tsip_sync = 0;
      }
      else {                   // DLE DLE is a DLE data byte and DLE xx is message ID
         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 {     // should never happen
      goto rst_msg;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movl $0xa, %edi
callq 0xcad0
cmpl $0x0, %eax
jne 0xaeb76
movl $0xa, %edi
callq 0x2b610
jmp 0xaedc8
movl $0xa, %edi
callq 0x2bd30
callq 0x79fc0
movb %al, -0x1(%rbp)
leaq 0x2be931(%rip), %rax    # 0x36d4c0
cmpb $0x0, 0x17a12(%rax)
je 0xaeba6
leaq 0x2be921(%rip), %rax    # 0x36d4c0
movb $0x0, 0x17a12(%rax)
leaq 0x2e8889(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x0, %eax
jne 0xaebec
movzbl -0x1(%rbp), %eax
cmpl $0x10, %eax
jne 0xaebcf
leaq 0x2e8871(%rip), %rax    # 0x397436
movw $0x1, (%rax)
callq 0x7a1d0
leaq 0x2e885c(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2e8852(%rip), %rax    # 0x397434
movw $0x0, (%rax)
jmp 0xaedc8
leaq 0x2e8843(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x1, %eax
jne 0xaec8d
movzbl -0x1(%rbp), %eax
cmpl $0x10, %eax
jne 0xaec0d
jmp 0xaed3d
movzbl -0x1(%rbp), %eax
cmpl $0x3, %eax
jne 0xaec1b
jmp 0xaed3d
leaq 0x2e8814(%rip), %rax    # 0x397436
movw $0x2, (%rax)
leaq 0x2e8804(%rip), %rax    # 0x397432
movzwl (%rax), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jge 0xaec67
movb -0x1(%rbp), %dl
leaq 0x2e87f0(%rip), %rax    # 0x397432
movw (%rax), %ax
movw %ax, %si
addw $0x1, %si
leaq 0x2e87df(%rip), %rcx    # 0x397432
movw %si, (%rcx)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x2e77ce(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
jmp 0xaec84
leaq 0x1ef4e2(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x8000, %ecx           # imm = 0x8000
leaq 0x1ef4d3(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
jmp 0xaed3d
jmp 0xaec86
jmp 0xaec88
jmp 0xaedc6
leaq 0x2e87a2(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x2, %eax
jne 0xaed12
movzbl -0x1(%rbp), %eax
cmpl $0x10, %eax
jne 0xaecb3
leaq 0x2e878a(%rip), %rax    # 0x397436
movw $0x3, (%rax)
jmp 0xaed0d
leaq 0x2e8778(%rip), %rax    # 0x397432
movzwl (%rax), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jge 0xaecf3
movb -0x1(%rbp), %dl
leaq 0x2e8764(%rip), %rax    # 0x397432
movw (%rax), %ax
movw %ax, %si
addw $0x1, %si
leaq 0x2e8753(%rip), %rcx    # 0x397432
movw %si, (%rcx)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x2e7742(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
jmp 0xaed0b
leaq 0x1ef456(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x8000, %ecx           # imm = 0x8000
leaq 0x1ef447(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
jmp 0xaed0d
jmp 0xaedc4
leaq 0x2e871d(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x3, %eax
jne 0xaedbd
movzbl -0x1(%rbp), %eax
cmpl $0x3, %eax
jne 0xaed57
callq 0xae540
movl $0x1, 0x31c137(%rip)    # 0x3cae74
leaq 0x2e86ee(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2e86e6(%rip), %rax    # 0x397436
movw $0x0, (%rax)
jmp 0xaedbb
leaq 0x2e86d4(%rip), %rax    # 0x397432
movzwl (%rax), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jge 0xaed97
movb -0x1(%rbp), %dl
leaq 0x2e86c0(%rip), %rax    # 0x397432
movw (%rax), %ax
movw %ax, %si
addw $0x1, %si
leaq 0x2e86af(%rip), %rcx    # 0x397432
movw %si, (%rcx)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x2e769e(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
jmp 0xaedaf
leaq 0x1ef3b2(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x8000, %ecx           # imm = 0x8000
leaq 0x1ef3a3(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
leaq 0x2e8680(%rip), %rax    # 0x397436
movw $0x2, (%rax)
jmp 0xaedc2
jmp 0xaed3d
jmp 0xaedc4
jmp 0xaedc6
jmp 0xaedc8
addq $0x10, %rsp
popq %rbp
retq
nop