// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x1
popq %rdi
callq 0x73dcb
leaq 0x2820b1(%rip), %rax    # 0x2f72a8
movl $0x11, (%rax)
leaq 0x281fcc(%rip), %rax    # 0x2f71d0
movl (%rax), %ecx
andl $-0x5, %ecx
andl $0x2, %r14d
leal (%rcx,%r14,2), %ecx
movl %ecx, (%rax)
leaq 0x2e5be6(%rip), %rax    # 0x35ae00
cmpl $0x40, %ebx
jne 0x75234
movzbl 0x2e5bda(%rip), %r14d   # 0x35ae01
movzbl 0x2e5bd3(%rip), %r15d   # 0x35ae02
movq %rax, %rcx
jmp 0x75257
cmpl $0x57, %ebx
jne 0x752ea
movzbl 0x2e5bbc(%rip), %r14d   # 0x35ae01
movl %r14d, %r15d
andl $0xf, %r15d
shrl $0x4, %r14d
leaq 0x2e5bab(%rip), %rcx    # 0x35ae02
movl 0x2e6bb3(%rip), %edx    # 0x35be10
leal 0x1(%rdx), %esi
cmpl %esi, %r14d
jne 0x752c1
movzbl (%rcx), %ecx
movl %r14d, 0x2e6ba1(%rip)   # 0x35be10
cmpl $0x1, %r14d
jne 0x75327
movl %ecx, 0xdf3a9(%rip)     # 0x154628
leaq 0x2acfcc(%rip), %rcx    # 0x322252
movzwl (%rcx), %ecx
movl 0x2e6b71(%rip), %edi    # 0x35be00
xorl %edx, %edx
leaq 0x2abfb8(%rip), %rsi    # 0x321250
cmpq %rdx, %rcx
je 0x7541d
cmpl $0xfff, %edi            # imm = 0xFFF
jg 0x752bc
movb (%rdx,%rax), %r8b
movslq %edi, %r9
incl %edi
movl %edi, 0x2e6b48(%rip)    # 0x35be00
movb %r8b, (%rsi,%r9)
incq %rdx
jmp 0x75298
leaq 0x1b30c8(%rip), %rax    # 0x228390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x753ae
leaq 0x97a46(%rip), %rsi     # 0x10cd21
movl %r14d, %ecx
xorl %eax, %eax
callq 0x9560
jmp 0x753ae
leaq 0x2acf61(%rip), %rcx    # 0x322252
movzwl (%rcx), %ecx
xorl %edx, %edx
leaq 0x2abf53(%rip), %rsi    # 0x321250
cmpq %rdx, %rcx
je 0x75318
cmpq $0xfff, %rdx            # imm = 0xFFF
ja 0x75313
movb (%rdx,%rax), %dil
movb %dil, (%rsi,%rdx)
incq %rdx
jmp 0x752fd
cmpl $0x55, %ebx
jne 0x75381
pushq $0x55
popq %rdi
callq 0x7508e
jmp 0x753a0
movl 0xdf2fb(%rip), %edx     # 0x154628
cmpl %ecx, %edx
jne 0x753ba
cmpl $0x40, %ebx
jne 0x753d9
leaq 0x2acf0d(%rip), %rcx    # 0x322252
movzwl (%rcx), %ecx
movl 0x2e6ab2(%rip), %edi    # 0x35be00
pushq $0x3
popq %rdx
leaq 0x2abef8(%rip), %rsi    # 0x321250
cmpq %rcx, %rdx
jae 0x7541d
cmpl $0xfff, %edi            # imm = 0xFFF
jg 0x7537c
movb (%rdx,%rax), %r8b
movslq %edi, %r9
incl %edi
movl %edi, 0x2e6a88(%rip)    # 0x35be00
movb %r8b, (%rsi,%r9)
incq %rdx
jmp 0x75358
leaq 0x1b3008(%rip), %rax    # 0x228390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x753a0
leaq 0x9796d(%rip), %rsi     # 0x10cd04
movl %ebx, %edx
xorl %eax, %eax
callq 0x9560
andl $0x0, 0x2e6a59(%rip)    # 0x35be00
orl $-0x1, 0xdf27a(%rip)    # 0x154628
andl $0x0, 0x2e6a5b(%rip)    # 0x35be10
jmp 0x75461
leaq 0x1b2fcf(%rip), %rax    # 0x228390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x7541d
leaq 0x97973(%rip), %rsi     # 0x10cd43
xorl %eax, %eax
callq 0x9560
jmp 0x7541d
cmpl $0x57, %ebx
jne 0x7541d
leaq 0x2ace6d(%rip), %rcx    # 0x322252
movzwl (%rcx), %ecx
movl 0x2e6a12(%rip), %edi    # 0x35be00
pushq $0x4
popq %rdx
leaq 0x2abe58(%rip), %rsi    # 0x321250
cmpq %rcx, %rdx
jae 0x7541d
cmpl $0xfff, %edi            # imm = 0xFFF
jg 0x75418
movb (%rdx,%rax), %r8b
movslq %edi, %r9
incl %edi
movl %edi, 0x2e69ec(%rip)    # 0x35be00
movb %r8b, (%rsi,%r9)
incq %rdx
jmp 0x753f8
cmpl %r15d, %r14d
jne 0x75461
andl $0x0, 0x2e69e7(%rip)    # 0x35be10
movzwl 0x2e69d0(%rip), %eax    # 0x35be00
leaq 0x2ace1b(%rip), %rcx    # 0x322252
movw %ax, (%rcx)
cmpl $0x40, %ebx
jne 0x75446
callq 0x7515d
jmp 0x75453
cmpl $0x57, %ebx
jne 0x75453
pushq $0x57
popq %rdi
callq 0x750c5
andl $0x0, 0x2e69a6(%rip)    # 0x35be00
orl $-0x1, 0xdf1c7(%rip)    # 0x154628
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq