// File: jkingan[P]ladyheather/heather.cpp
// Function: get_serial_char(unsigned int)
u08 get_serial_char(unsigned port)
{
int flag;
u08 c;

   // return the next byte from a com device
   if(port >= NUM_COM_PORTS) return FALSE;

   if((port == RCVR_PORT) && (rcvr_type == NO_RCVR) && (enable_terminal == 0)) { // alternate ready/not ready each check
      return 0;
   }
if((port == RCVR_PORT) && (rcvr_type == TIDE_RCVR) && (enable_terminal == 0)) {
   return 0;
}


   if((port == RCVR_PORT) && sim_file) {  // get char from a simulation file
      return get_sim_char(port);
   }

   if(com[port].process_com == 0) return 0;  //lfs
   if(com[port].com_fd < 0) return 0;
   if(com[port].com_error & RCV_ERR) return 0;

   if(com[port].next_rcv_byte < com[port].rcv_byte_count) {   // return byte previously fetched by check_incoming_data()
      c = com[port].rcv_buffer[com[port].next_rcv_byte++];
      if((c == 0x0D) || (c == 0x0A)) crlf_seen = 1;

      if((zoom_screen == 'O')  && (port == monitor_port)) {   // send char to "terminal" format monitor screen
         echo_term(monitor_port, c, monitor_hex, 0);
      }
      return c;
   }

   com[port].rcv_byte_count = 0;
   com[port].next_rcv_byte = 0;

   while(com[port].rcv_byte_count == 0) {  // wait until we have a character
      if(1 || (com[port].com_port != 0) || (com[port].usb_port != 0)) {  // zork COM port in use: read a byte from serial port
         flag = read(com[port].com_fd, &com[port].rcv_buffer[0], RCV_BUF_SIZE);
         if(flag < 0) {  // !!!! com error or no serial data available - we should check errno()
            flag = com[port].rcv_byte_count = 0;
         }
         else com[port].rcv_byte_count = flag;
      }

      update_pwm();   // if doing pwm temperature control
   }

   if(flag && com[port].rcv_byte_count) {  // succesful read
      c = com[port].rcv_buffer[com[port].next_rcv_byte++];
      if((c == 0x0D) || (c == 0x0A)) crlf_seen = 1;

      if((zoom_screen == 'O') && (port == monitor_port)) {   // send char to "terminal" format monitor screen
         echo_term(monitor_port, c, monitor_hex, 0);
      }
      return c;
   }

   if(com_error_exit) {
      error_exit(222, "Serial receive error");
   }
   else {
      com[port].com_error |= RCV_ERR;
      ++com_errors;
   }
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movl %edi, -0x8(%rbp)
cmpl $0x13, -0x8(%rbp)
jb 0xcd7a
movb $0x0, -0x1(%rbp)
jmp 0xd152
cmpl $0xa, -0x8(%rbp)
jne 0xcd9f
cmpq $0x400, 0x26ad25(%rip) # imm = 0x400
jne 0xcd9f
cmpl $0x0, 0x29137c(%rip)    # 0x29e110
jne 0xcd9f
movb $0x0, -0x1(%rbp)
jmp 0xd152
cmpl $0xa, -0x8(%rbp)
jne 0xcdc4
cmpq $0x800000, 0x26ad00(%rip) # imm = 0x800000
jne 0xcdc4
cmpl $0x0, 0x291357(%rip)    # 0x29e110
jne 0xcdc4
movb $0x0, -0x1(%rbp)
jmp 0xd152
cmpl $0xa, -0x8(%rbp)
jne 0xcde4
cmpq $0x0, 0x2907ce(%rip)    # 0x29d5a0
je 0xcde4
movl -0x8(%rbp), %edi
callq 0xb000
movb %al, -0x1(%rbp)
jmp 0xd152
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x3606d0(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpl $0x0, 0x2234(%rax)
jne 0xce0c
movb $0x0, -0x1(%rbp)
jmp 0xd152
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x3606a8(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpl $0x0, 0x8(%rax)
jge 0xce31
movb $0x0, -0x1(%rbp)
jmp 0xd152
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x360683(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movzbl 0x2251(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xce5f
movb $0x0, -0x1(%rbp)
jmp 0xd152
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x360655(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x2224(%rax), %eax
movl -0x8(%rbp), %ecx
movl %ecx, %edx
leaq 0x360639(%rip), %rcx    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rcx
cmpl 0x2220(%rcx), %eax
jge 0xcf3a
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x360617(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl -0x8(%rbp), %ecx
leaq 0x360603(%rip), %rdx    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rdx
movl 0x2224(%rdx), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x2224(%rdx)
movslq %ecx, %rcx
movb 0x1220(%rax,%rcx), %al
movb %al, -0xd(%rbp)
movzbl -0xd(%rbp), %eax
cmpl $0xd, %eax
je 0xcef7
movzbl -0xd(%rbp), %eax
cmpl $0xa, %eax
jne 0xcf01
movl $0x1, 0x291273(%rip)    # 0x29e174
movzbl 0x35ea80(%rip), %eax    # 0x36b988
cmpl $0x4f, %eax
jne 0xcf2f
movl -0x8(%rbp), %eax
cmpl 0x36059a(%rip), %eax    # 0x36d4b0
jne 0xcf2f
movl 0x360592(%rip), %edi    # 0x36d4b0
movzbl -0xd(%rbp), %esi
movl 0x2906fc(%rip), %edx    # 0x29d624
xorl %ecx, %ecx
callq 0xb1d0
movb -0xd(%rbp), %al
movb %al, -0x1(%rbp)
jmp 0xd152
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x36057a(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x2220(%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x36055a(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x2224(%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x36053a(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpl $0x0, 0x2220(%rax)
jne 0xd036
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x360517(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x8(%rax), %edi
movl -0x8(%rbp), %eax
leaq 0x360500(%rip), %rsi    # 0x36d4c0
imulq $0x2260, %rax, %rax     # imm = 0x2260
addq %rax, %rsi
addq $0x1220, %rsi           # imm = 0x1220
movl $0x1000, %edx           # imm = 0x1000
callq 0x9560
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jge 0xd00d
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x3604d0(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x2220(%rax)
movl $0x0, -0xc(%rbp)
jmp 0xd02c
movl -0xc(%rbp), %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0x3604a4(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rax
movl %ecx, 0x2220(%rax)
callq 0x11ee60
jmp 0xcf7a
cmpl $0x0, -0xc(%rbp)
je 0xd0fd
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x360474(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpl $0x0, 0x2220(%rax)
je 0xd0fd
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x360451(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl -0x8(%rbp), %ecx
leaq 0x36043d(%rip), %rdx    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rdx
movl 0x2224(%rdx), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x2224(%rdx)
movslq %ecx, %rcx
movb 0x1220(%rax,%rcx), %al
movb %al, -0xd(%rbp)
movzbl -0xd(%rbp), %eax
cmpl $0xd, %eax
je 0xd0bd
movzbl -0xd(%rbp), %eax
cmpl $0xa, %eax
jne 0xd0c7
movl $0x1, 0x2910ad(%rip)    # 0x29e174
movzbl 0x35e8ba(%rip), %eax    # 0x36b988
cmpl $0x4f, %eax
jne 0xd0f5
movl -0x8(%rbp), %eax
cmpl 0x3603d4(%rip), %eax    # 0x36d4b0
jne 0xd0f5
movl 0x3603cc(%rip), %edi    # 0x36d4b0
movzbl -0xd(%rbp), %esi
movl 0x290536(%rip), %edx    # 0x29d624
xorl %ecx, %ecx
callq 0xb1d0
movb -0xd(%rbp), %al
movb %al, -0x1(%rbp)
jmp 0xd152
cmpb $0x0, 0x26d30f(%rip)    # 0x27a413
je 0xd119
movl $0xde, %edi
leaq 0x16280e(%rip), %rsi    # 0x16f920
callq 0xc050
jmp 0xd14e
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x36039b(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movzbl 0x2251(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x2251(%rax)
movl 0x3890af(%rip), %eax    # 0x3961f4
addl $0x1, %eax
movl %eax, 0x3890a6(%rip)    # 0x3961f4
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x10, %rsp
popq %rbp
retq
nopl (%rax,%rax)