// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathgps.cpp
// Function: get_zod_message()
void get_zod_message()
{
static int hdr_ptr = 0;
u08 c;
static u16 cksum;
static u16 byte_count;
static int row = 3;

   // This routine buffers up an incoming Venus message.  When the end of the
   // message is seen, the message is parsed and decoded with parse_zodiac_msg()

   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;
   }


//if(tsip_sync == 0) fprintf(debug_file, "\n");
//if(debug_file) fprintf(debug_file, "%02X ", c);

   if(tsip_sync == 0) {         // syncing to start of message, search for a 0xFF
      if(c == 0xFF) {
         tsip_sync = 1;
         get_sync_time();
      }

      tsip_wptr = 0;
      tsip_rptr = 0;
      zodiac_rx_len = 0;
      zodiac_rx_cksum = 0;
      cksum = 0;
      byte_count = 0;
      zodiac_vfy_cksum = 0;
      hdr_ptr = 0;
      zodiac_hdr[0] = zodiac_hdr[1] = zodiac_hdr[2] = zodiac_hdr[3] = 0;
      return;
   }
   else if(tsip_sync == 1) { // 0xFF has been seen, now checking next byte
      if(c != 0x81) goto rst_msg;
      ++tsip_sync;           // 0xFF 0x81 seen... so get the message header
   }
   else if(tsip_sync == 2) { // header packet
      if(hdr_ptr >= 8) goto rst_msg;   // should never happen

      if(hdr_ptr & 1) {  // high byte
         zodiac_hdr[hdr_ptr>>1] += (((u16) c) * 256);
         zodiac_hdr[hdr_ptr>>1] &= 0xFFFF;
      }
      else {  // low byte
         zodiac_hdr[hdr_ptr>>1] = ((u16) c) & 0xFF;
      }

      if(++hdr_ptr >= 8) {  // end of header
         cksum = (0x81FF + zodiac_hdr[0] + zodiac_hdr[1] + zodiac_hdr[2]) & 0xFFFF;   
         if(cksum != 0x8000) cksum = (0 - cksum) & 0xFFFF;
         if(cksum != zodiac_hdr[3]) goto rst_msg;  // bad checksum;
         zodiac_rx_len = (zodiac_hdr[1]*2);
         if(zodiac_rx_len == 0) goto got_it;  // message has no payload, go parse it
         else {
            ++tsip_sync;
            cksum = 0;
//if(debug_file) fprintf(debug_file, "  [%d]  ", byte_count);
         }
      }
   }
   else if(tsip_sync == 3) {   // get Zodiac message payload
      if(tsip_wptr < MAX_TSIP) {  // so add it to the message buffer
         tsip_buf[tsip_wptr++] = c;
         if(byte_count & 1) zodiac_rx_cksum += ((((u16) c) * 256) + cksum);
         else               cksum = (u16) c;    // low byte of word
         ++byte_count;
         --zodiac_rx_len;
         if(zodiac_rx_len == 0) ++tsip_sync;  // message payload processed, now get checksums
      }
      else {
         tsip_error |= OVFL_ERROR;
         goto rst_msg;
      }
   }
   else if(tsip_sync == 4) {  // get message checksum low byte
      zodiac_vfy_cksum = (u16) c;   // low byte of message checksum
      ++tsip_sync;
   }
   else if(tsip_sync == 5) {  // message checksum, end of venus message
      zodiac_vfy_cksum += ((u16) c) * 256;

      if(zodiac_rx_cksum != 0x8000) zodiac_rx_cksum = (0 - zodiac_rx_cksum) & 0xFFFF;
//sprintf(out, "id:%-4d  len:%-4d  flags:%04X  vfy:%04X  rx:%04X  bc:%-4d  dif:%04X", 
//zodiac_hdr[0], zodiac_hdr[1], zodiac_hdr[2], zodiac_vfy_cksum, zodiac_rx_cksum, byte_count, (zodiac_rx_cksum-zodiac_vfy_cksum)&0xFFFF);
//vidstr(row,3, YELLOW, out);
//if(row++>30) row = 0;
//refresh_page();
//if(debug_file) fprintf(debug_file, "  (v:%04X calc:%04X)\n", zodiac_vfy_cksum, zodiac_rx_cksum);
      if(zodiac_rx_cksum != zodiac_vfy_cksum) { // checksum error
         tsip_error |= CKSUM_ERROR;
         goto rst_msg;
      }

      got_it:
      tsip_sync = 0;
      decode_zod_message();  // message checksum matches, process the message
      packet_end = 1;
   }
   else {     // should never happen
      rst_msg:
      tsip_wptr = 0;
      tsip_sync = 0;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq $0xa
popq %rdi
callq 0xb771
pushq $0xa
popq %rdi
testl %eax, %eax
je 0x8bbc8
callq 0x1d93f
callq 0x51891
leaq 0x26c7af(%rip), %rcx    # 0x2f82e0
cmpb $0x0, 0x17a12(%rcx)
je 0x8bb41
movb $0x0, 0x17a12(%rcx)
leaq 0x29670e(%rip), %rcx    # 0x322256
movzwl (%rcx), %edx
cmpq $0x5, %rdx
ja 0x8bcba
leaq 0x7dcc4(%rip), %rsi     # 0x109820
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
cmpl $0xff, %eax
jne 0x8bb76
movw $0x1, (%rcx)
callq 0x51a2f
leaq 0x2966d5(%rip), %rax    # 0x322252
andw $0x0, (%rax)
leaq 0x2966cc(%rip), %rax    # 0x322254
andw $0x0, (%rax)
andw $0x0, 0x2caf8c(%rip)    # 0x356b20
andw $0x0, 0x2caf82(%rip)    # 0x356b1e
andw $0x0, 0x2d0318(%rip)    # 0x35bebc
andw $0x0, 0x2d0312(%rip)    # 0x35bebe
andw $0x0, 0x2caf68(%rip)    # 0x356b1c
andl $0x0, 0x2d02fd(%rip)    # 0x35beb8
andq $0x0, 0x2caf65(%rip)    # 0x356b28
jmp 0x8bcc9
popq %rbp
jmp 0x1d61b
movw %ax, 0x2caf47(%rip)     # 0x356b1c
movw $0x5, (%rcx)
jmp 0x8bcc9
movl 0x2d02d3(%rip), %edx    # 0x35beb8
cmpl $0x7, %edx
jg 0x8bcba
movl %edx, %esi
sarl %esi
movslq %esi, %rsi
leaq 0x2caf2c(%rip), %rdi    # 0x356b28
testb $0x1, %dl
jne 0x8bccb
movw %ax, (%rdi,%rsi,2)
jmp 0x8bcd2
leaq 0x29663d(%rip), %rsi    # 0x322252
movzwl (%rsi), %edx
movl $0x8000, %edi           # imm = 0x8000
cmpq $0xfff, %rdx            # imm = 0xFFF
ja 0x8bcb1
leal 0x1(%rdx), %edi
movw %di, (%rsi)
leaq 0x295619(%rip), %rsi    # 0x321250
movb %al, (%rsi,%rdx)
movzwl 0x2d027d(%rip), %edx    # 0x35bebe
testb $0x1, %dl
jne 0x8bd30
movw %ax, 0x2d026b(%rip)     # 0x35bebc
jmp 0x8bd41
cmpl $0x81, %eax
jne 0x8bcba
movw $0x2, (%rcx)
jmp 0x8bcc9
shll $0x8, %eax
addw 0x2caeae(%rip), %ax     # 0x356b1c
movw %ax, 0x2caea7(%rip)     # 0x356b1c
movzwl 0x2caea2(%rip), %edx    # 0x356b1e
movw $0x8000, %si            # imm = 0x8000
cmpw $0x8000, %dx            # imm = 0x8000
je 0x8bc92
negl %edx
movw %dx, 0x2cae8e(%rip)     # 0x356b1e
movl %edx, %esi
movl $0x4000, %edi           # imm = 0x4000
cmpw %ax, %si
jne 0x8bcb1
andw $0x0, (%rcx)
callq 0x8b9fd
movl $0x1, 0x2c9fe5(%rip)    # 0x355c94
jmp 0x8bcc9
leaq 0x19d2b8(%rip), %rax    # 0x228f70
orl %edi, (%rax)
leaq 0x296591(%rip), %rax    # 0x322252
andw $0x0, (%rax)
andw $0x0, (%rcx)
popq %rbp
retq
shll $0x8, %eax
addw %ax, (%rdi,%rsi,2)
leal 0x1(%rdx), %eax
movl %eax, 0x2d01dd(%rip)    # 0x35beb8
cmpl $0x7, %edx
jne 0x8bcc9
movzwl 0x2cae43(%rip), %eax    # 0x356b2a
movzwl 0x2cae3a(%rip), %edx    # 0x356b28
addw %ax, %dx
addw 0x2cae34(%rip), %dx     # 0x356b2c
movl $0x7e01, %esi           # imm = 0x7E01
subl %edx, %esi
movw %si, 0x2d01b6(%rip)     # 0x35bebc
cmpw 0x2cae21(%rip), %si     # 0x356b2e
jne 0x8bcba
addl %eax, %eax
movw %ax, 0x2cae08(%rip)     # 0x356b20
testw %ax, %ax
je 0x8bc9c
movw $0x3, (%rcx)
andw $0x0, 0x2d018e(%rip)    # 0x35bebc
jmp 0x8bcc9
shll $0x8, %eax
addw 0x2d0182(%rip), %ax     # 0x35bebc
addw %ax, 0x2caddd(%rip)     # 0x356b1e
leal 0x1(%rdx), %eax
movw %ax, 0x2d0173(%rip)     # 0x35bebe
decw 0x2cadce(%rip)          # 0x356b20
jne 0x8bcc9
movw $0x4, (%rcx)
jmp 0x8bcc9