// File: /workspace/github_cpp_makefile_star2_O1/jkingan[P]ladyheather/heathgps.cpp
// Function: decode_rt17_msg(int, int)
void decode_rt17_msg(int msg, int status)
{
int page;
int pages;
int reply;
int i;
static int last_reply = (-2);
static int last_page = 0;

   // parse and decode the RT17 message

   start_msg_decode(1);

   have_critical_alarms = 17;
   if(status & 0x02) critical_alarms |= CRIT_PWR;
   else              critical_alarms &= (~CRIT_PWR);

   if(msg == 0x40) {  // GSOF data
      reply = rt17_buf[0];
      page = rt17_buf[1];
      pages = rt17_buf[2];
   }
   else if(msg == 0x57) {  // RAWDATA
      // rt17_buf[0] = message type subcode
      page = rt17_buf[1];
      reply = rt17_buf[2];

      pages = page & 0x0F;
      page  = (page >> 4) & 0x0F;
   }
   else {  // other messages
      for(i=0; i<tsip_wptr; i++) {  // copy RT17 buffer to tsip buffer
         if(i < MAX_TSIP) tsip_buf[i] = rt17_buf[i];
      }

      // !!!!parse message here
      if(msg == 0x55) parse_rt17_svdata(msg);
else if(debug_file) fprintf(debug_file, "Misc msg type %02X skipped.\n", msg);

      rt17_wptr = 0;
      last_reply = (-1);
      last_page = 0;
      return;
   }

if(0 && debug_file) {
   fprintf(debug_file, "copy page %d of %d   reply:%d  last_reply:%d  len:%d\n", page, pages, reply,last_reply, rt17_wptr);
}
   // merge multi-page packet data into one buffer (tsip_buf)
   if(page != (last_page + 1)) {  // page sequence error
if(debug_file) fprintf(debug_file, "page seq error: last:%d  this:%d\n", last_page, page);
      last_page = 0;
      return;
   }

   last_page = page;
   if(page == 1) {  // first page of data, copy all data
      last_reply = reply;
      for(i=0; i<tsip_wptr; i++) {
         if(rt17_wptr < MAX_TSIP) tsip_buf[rt17_wptr++] = rt17_buf[i];
      }
   }
   else if(reply != last_reply) {  // message sequence error
if(debug_file) fprintf(debug_file, "reply seq error: last:%d  this:%d\n", last_reply, reply);
   }
   else if(msg == 0x40) {  // copy only message data from following pages to tsip_buf
      for(i=3; i<tsip_wptr; i++) {  // skip subcode, reply, page, pages
         if(rt17_wptr < MAX_TSIP) tsip_buf[rt17_wptr++] = rt17_buf[i];
      }
   }
   else if(msg == 0x57) {  // copy only message data from following pages to tsip_buf
      for(i=4; i<tsip_wptr; i++) {  // skip subcode, page, reply, flags
         if(rt17_wptr < MAX_TSIP) tsip_buf[rt17_wptr++] = rt17_buf[i];
      }
   }
if(0 && debug_file) fprintf(debug_file, "Page:%02X  tsip_wptr:%d  rt17_wptr:%d\n", page, tsip_wptr, rt17_wptr);

   if(page != pages) {  // more pages follow for this message
      return;
   }
   last_page = 0;

   tsip_wptr = rt17_wptr;  // parse merged page buffer

   if(msg == 0x40) {  // GSOF
      parse_rt17_gsof(msg);
   }
   else if(msg == 0x57) {  // RAWDATA
      parse_rt17_rawdata(msg);
   }

   rt17_wptr = 0;
   last_reply = (-1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %esi, %r14d
movl %edi, %ebx
movl $0x1, %edi
callq 0x7f47d
leaq 0x28c6b5(%rip), %rax    # 0x30d2a8
movl $0x11, (%rax)
leaq 0x28c5d0(%rip), %rax    # 0x30d1d0
movl (%rax), %ecx
andl $-0x5, %ecx
andl $0x2, %r14d
leal (%rcx,%r14,2), %ecx
movl %ecx, (%rax)
cmpl $0x40, %ebx
jne 0x80c2d
movzbl 0x2f01e5(%rip), %r14d   # 0x370e01
leaq 0x2f01dd(%rip), %rax    # 0x370e00
movzbl 0x2f01d7(%rip), %r15d   # 0x370e02
jmp 0x80c50
cmpl $0x57, %ebx
jne 0x80cf8
movzbl 0x2f01c3(%rip), %r14d   # 0x370e01
movl %r14d, %r15d
andl $0xf, %r15d
shrl $0x4, %r14d
leaq 0x2f01b2(%rip), %rax    # 0x370e02
movl 0x2f11ba(%rip), %edx    # 0x371e10
leal 0x1(%rdx), %ecx
cmpl %ecx, %r14d
jne 0x80cc9
movzbl (%rax), %ecx
movl %r14d, 0x2f11a8(%rip)   # 0x371e10
cmpl $0x1, %r14d
jne 0x80d44
movl %ecx, 0xe99b0(%rip)     # 0x16a628
leaq 0x2b75d3(%rip), %rax    # 0x338252
movzwl (%rax), %eax
testq %rax, %rax
je 0x80e5c
movl 0x2f116f(%rip), %esi    # 0x371e00
xorl %ecx, %ecx
leaq 0x2f0166(%rip), %rdx    # 0x370e00
leaq 0x2b65af(%rip), %rdi    # 0x337250
cmpl $0xfff, %esi            # imm = 0xFFF
jg 0x80cbc
movb (%rcx,%rdx), %r8b
movslq %esi, %r9
incl %esi
movl %esi, 0x2f1148(%rip)    # 0x371e00
movb %r8b, (%rdi,%r9)
incq %rcx
cmpq %rcx, %rax
jne 0x80ca1
jmp 0x80e5c
leaq 0x1bd6c0(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x80ce9
leaq 0xa201a(%rip), %rsi     # 0x122cf9
movl %r14d, %ecx
xorl %eax, %eax
callq 0x9560
movl $0x0, 0x2f111d(%rip)    # 0x371e10
jmp 0x80eab
leaq 0x2b7553(%rip), %rax    # 0x338252
movzwl (%rax), %eax
testq %rax, %rax
je 0x80d30
xorl %ecx, %ecx
leaq 0x2f00f0(%rip), %rdx    # 0x370e00
leaq 0x2b6539(%rip), %rsi    # 0x337250
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0x80d28
movb (%rcx,%rdx), %dil
movb %dil, (%rsi,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x80d17
cmpl $0x55, %ebx
jne 0x80db0
movl $0x55, %edi
callq 0x809be
jmp 0x80dcf
movl 0xe98de(%rip), %edx     # 0x16a628
cmpl %ecx, %edx
jne 0x80dec
cmpl $0x40, %ebx
jne 0x80e0b
leaq 0x2b74f0(%rip), %rax    # 0x338252
movzwl (%rax), %eax
cmpq $0x4, %rax
jb 0x80e5c
movl $0x3, %ecx
movl 0x2f1086(%rip), %edi    # 0x371e00
leaq 0x2f007f(%rip), %rdx    # 0x370e00
leaq 0x2b64c8(%rip), %rsi    # 0x337250
cmpl $0xfff, %edi            # imm = 0xFFF
jg 0x80da3
movb (%rcx,%rdx), %r8b
movslq %edi, %r9
incl %edi
movl %edi, 0x2f1061(%rip)    # 0x371e00
movb %r8b, (%rsi,%r9)
incq %rcx
cmpq %rcx, %rax
jne 0x80d88
jmp 0x80e5c
leaq 0x1bd5d9(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x80dcf
leaq 0xa1f16(%rip), %rsi     # 0x122cdc
movl %ebx, %edx
xorl %eax, %eax
callq 0x9560
xorl %eax, %eax
movl %eax, 0x2f1029(%rip)    # 0x371e00
movl $0xffffffff, 0xe9847(%rip) # imm = 0xFFFFFFFF
movl %eax, 0x2f1029(%rip)    # 0x371e10
jmp 0x80eab
leaq 0x1bd59d(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x80e5c
leaq 0xa1f19(%rip), %rsi     # 0x122d1b
xorl %eax, %eax
callq 0x9560
jmp 0x80e5c
cmpl $0x57, %ebx
jne 0x80e5c
leaq 0x2b743b(%rip), %rax    # 0x338252
movzwl (%rax), %eax
cmpq $0x5, %rax
jb 0x80e5c
movl $0x4, %ecx
movl 0x2f0fd5(%rip), %edi    # 0x371e00
leaq 0x2effce(%rip), %rdx    # 0x370e00
leaq 0x2b6417(%rip), %rsi    # 0x337250
cmpl $0xfff, %edi            # imm = 0xFFF
jg 0x80e54
movb (%rcx,%rdx), %r8b
movslq %edi, %r9
incl %edi
movl %edi, 0x2f0fb0(%rip)    # 0x371e00
movb %r8b, (%rsi,%r9)
incq %rcx
cmpq %rcx, %rax
jne 0x80e39
cmpl %r15d, %r14d
jne 0x80eab
movl $0x0, 0x2f0fa5(%rip)    # 0x371e10
movzwl 0x2f0f8e(%rip), %eax    # 0x371e00
leaq 0x2b73d9(%rip), %rcx    # 0x338252
movw %ax, (%rcx)
cmpl $0x40, %ebx
jne 0x80e88
callq 0x80ae2
jmp 0x80e97
cmpl $0x57, %ebx
jne 0x80e97
movl $0x57, %edi
callq 0x80a06
movl $0x0, 0x2f0f5f(%rip)    # 0x371e00
movl $0xffffffff, 0xe977d(%rip) # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq