// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heather.cpp
// Function: do_term(unsigned int)
void do_term(unsigned port)
{
int i;
double old_cto;

   // Interactive video terminal.  (added mainly to play with SCPI receivers)

   if(port >= NUM_COM_PORTS) return;
   if(com[port].process_com == 0) enable_terminal = 0;
   if(com[port].com_running < 0) enable_terminal = 0;
   if(enable_terminal == 0) return;

   old_cto = com[port].com_timeout;
   com[port].com_timeout = 250.0;
   in_terminal = 1;

   if(rcvr_type == CS_RCVR) strcpy(term_kbd, "SYST:PRINT?");
   else if((rcvr_type == SCPI_RCVR) || (rcvr_type == UCCM_RCVR)) {
      if(scpi_echo_mode) strcpy(term_kbd, ":SYST:COMM:SER1:FDUP 0");
      else               strcpy(term_kbd, "SYST:STAT?");
   }
   else term_kbd[tkbd_col] = 0;
   tkbd_col = strlen(term_kbd);
   last_was_keybuf = 0;

   term_echo = 1;   // echo input to the screen (in yellow)
// term_hex = 0;    // ascii mode
   term_pause = 0;  // send all data to the screeb
   term_header_lines = TERM_HEADER_LINES;  // length of the header area
   monitor_hex = 1;

   erase:
   init_term();

   new_header:
   set_term_header(port);

   term_row = term_header_lines; // start at top of screen
// term_row = term_rows-1;       // start at the screen bottom
   term_col = 0;
   show_term_screen(port);  // draw the screen image

   while(1) {    // the main processing loop...  repeat until exit requested
      serve_os_queue();

      if(KBHIT()) {       // key pressed
          i = get_kbd();  // get the keyboard character

          if     (i == END_CHAR)  break;        // exit the terminal
          else if(i == HOME_CHAR) goto erase;   // erase screen
          else if(i == F8_CHAR)   SendBreak(port);  // send a break
          else if(i == F1_CHAR) {               // toggle echo
             term_echo ^= 1;
             goto new_header;
          }
          else if(i == F2_CHAR) {               // toggle log
             if(raw_file) {
                fclose(raw_file);
                raw_file = 0;
                log_stream &= (~LOG_RAW_STREAM);
             }
             else {
                raw_file = open_raw_file(raw_name, "ab");
                log_stream |= LOG_RAW_STREAM;
             }
             goto new_header;
          }
          else if(i == F3_CHAR) {  // toggle hex display mode
             term_hex ^= 1;
             goto erase;
          }
          else if(i == F4_CHAR) {  // pause screen
             term_pause ^= 1;
             goto new_header;
          }
          else if(i == F5_CHAR) {  // crlf mode
             term_crlf ^= 1;
             goto new_header;
          }
          else if(i == UP_CHAR) {  // resend last keyboard command
             send_line:
             for(i=0; i<tkbd_col; i++) {
                sendout(port, term_kbd[i], EOM);
                echo_term(port, term_kbd[i] | 0x80, 0, 0);
             }
             last_was_keybuf = 1;
          }
#define BUFFERED_KBD buffered_term
//#define BUFFERED_KBD (rcvr_type == LPFRS_RCVR)
          else if(BUFFERED_KBD) {   // don't send line to com port until CR
             if(i == 0x0D) {        // since OSA4530 times out after 175 msecs between keystrokes
                save_term_kbd(0x0D);
                save_term_kbd(0x0A);
                goto send_line;
             }
             else goto buffer_kbd;
          }
          else {
             sendout(port, i, EOM);

             buffer_kbd:
             if(i == 0x08) {      // backspace key pressed
                if(tkbd_col) {    // remove keystroke from keyboard buffer
                   --tkbd_col;
                }
                term_kbd[tkbd_col] = 0;
                last_was_keybuf = 0;

                if(term_col) --term_col; // remove from screen image
                term_screen[term_row][term_col] = 0;
                show_term_screen(port);
             }
             else if(i == 0x0D) { // carriage return
                term_col = 0;
                scroll_up(port);
                if(rcvr_type != STAR_RCVR) last_was_keybuf = 1;
                else save_term_kbd(i);
             }
             else {
                save_term_kbd(i);  // save last keystroke in replay buffer
                i |= 0x80;         // echo input in yellow
                if(term_echo) echo_term(port, i, 0, 0);  // echo keystroke if echo enabled
             }
          }
      }
      else if(SERIAL_DATA_AVAILABLE(port)) {  // we have a char from the receiver
         i = get_com_char();       // get the char (and write to raw file if opened)  aaaahhhh - this needs to be passed term_port
         if(term_pause == 0) {     // send it to the screen unless screen is paused
            echo_term(port, i, term_hex, 0);
         }

      }
      else {  // nothing going on, Take a short snooze
         Sleep(10);
      }

// Assembly:
cmpl $0x12, %edi
ja 0x1e6de
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edi, %ebx
movl %edi, %eax
imulq $0x2260, %rax, %rax     # imm = 0x2260
leaq 0x2da08c(%rip), %rcx    # 0x2f82e0
leaq (%rcx,%rax), %r12
cmpl $0x0, 0x2234(%rax,%rcx)
jne 0x1e269
andl $0x0, 0x20acc7(%rip)    # 0x228f30
cmpl $0x0, 0x2230(%r12)
js 0x1e2e4
cmpl $0x0, 0x20acb5(%rip)    # 0x228f30
je 0x1e6d0
movsd 0x2238(%r12), %xmm0
movabsq $0x406f400000000000, %rax # imm = 0x406F400000000000
movq %rax, 0x2238(%r12)
movl $0x1, 0x30c379(%rip)    # 0x32a620
movq 0x1e4622(%rip), %rax    # 0x2028d0
cmpq $0x8, %rax
movsd %xmm0, -0x30(%rbp)
je 0x1e2f0
cmpq $0x10, %rax
je 0x1e2f0
cmpq $0x20000, %rax          # imm = 0x20000
jne 0x1e31a
movabsq $0x4952503a54535953, %rax # imm = 0x4952503A54535953
movq %rax, 0x30e4b8(%rip)    # 0x32c790
movl $0x3f544e, 0x30e4b6(%rip) # imm = 0x3F544E
jmp 0x1e349
andl $0x0, 0x20ac45(%rip)    # 0x228f30
jmp 0x1e6d0
cmpl $0x0, 0x1e45ed(%rip)    # 0x2028e4
je 0x1e32e
movups 0xd715a(%rip), %xmm0    # 0xf545a
movaps %xmm0, 0x30e489(%rip)   # 0x32c790
movabsq $0x3020505544463a, %rax # imm = 0x3020505544463A
movq %rax, 0x30e487(%rip)    # 0x32c79f
jmp 0x1e349
movslq 0x30e4f7(%rip), %rax    # 0x32c818
leaq 0x30e468(%rip), %rcx    # 0x32c790
movb $0x0, (%rax,%rcx)
jmp 0x1e349
movabsq $0x4154533a54535953, %rax # imm = 0x4154533A54535953
movq %rax, 0x30e451(%rip)    # 0x32c790
movl $0x3f5441, 0x30e44e(%rip) # imm = 0x3F5441
leaq 0x30e440(%rip), %r14    # 0x32c790
movq %r14, %rdi
callq 0x9120
movl %eax, 0x30e4ba(%rip)    # 0x32c818
andl $0x0, 0x30e4b7(%rip)    # 0x32c81c
pushq $0x1
popq %r15
movl %r15d, 0x30e410(%rip)   # 0x32c780
andl $0x0, 0x30e40d(%rip)    # 0x32c784
movl $0x5, 0x30e3fb(%rip)    # 0x32c77c
movl %r15d, 0x20a0bc(%rip)   # 0x228444
callq 0x1ddd0
movl %ebx, %edi
callq 0x1de83
movl 0x30e3e2(%rip), %eax    # 0x32c77c
movl %eax, 0x20a090(%rip)    # 0x228430
andl $0x0, 0x20a08d(%rip)    # 0x228434
movl %ebx, %edi
callq 0x1dbfe
callq 0x1bca4
callq 0xd1b9
testl %eax, %eax
je 0x1e3ec
callq 0xd1d5
movl %eax, %r13d
leal -0x13b(%r13), %eax
cmpl $0x14, %eax
ja 0x1e41c
leaq 0xd51a1(%rip), %rcx     # 0xf3578
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl %ebx, %edi
callq 0xae2d
jmp 0x1e571
movl %ebx, %edi
callq 0xb771
testl %eax, %eax
je 0x1e488
callq 0x51891
cmpl $0x0, 0x30e37d(%rip)    # 0x32c784
jne 0x1e571
movl 0x20a02d(%rip), %edx    # 0x228440
movl %ebx, %edi
movl %eax, %esi
jmp 0x1e4ed
cmpl $0x0, 0x20ab11(%rip)    # 0x228f34
je 0x1e495
cmpl $0x8, %r13d
je 0x1e4b3
cmpl $0xd, %r13d
jne 0x1e4ca
pushq $0xd
popq %rdi
callq 0x1de39
pushq $0xa
popq %rdi
callq 0x1de39
xorl %r13d, %r13d
movslq 0x30e3c5(%rip), %rax    # 0x32c818
cmpq %rax, %r13
jge 0x1e567
movzbl (%r13,%r14), %esi
movl %ebx, %edi
movl %r15d, %edx
callq 0xba3b
movsbl (%r13,%r14), %esi
orl $0x80, %esi
movl %ebx, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xaad9
incq %r13
jmp 0x1e44c
pushq $0xa
popq %rdi
callq 0xae83
jmp 0x1e571
movzbl %r13b, %esi
movl %ebx, %edi
movl %r15d, %edx
callq 0xba3b
cmpl $0xd, %r13d
je 0x1e542
cmpl $0x8, %r13d
jne 0x1e4ca
movslq 0x30e35e(%rip), %rax    # 0x32c818
testq %rax, %rax
je 0x1e4f6
decq %rax
movl %eax, 0x30e350(%rip)    # 0x32c818
jmp 0x1e4f8
movl %r13d, %edi
callq 0x1de39
cmpl $0x0, 0x30e2a7(%rip)    # 0x32c780
je 0x1e571
orl $0x80, %r13d
movl %ebx, %edi
movl %r13d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xaad9
jmp 0x1e571
xorl %eax, %eax
andl $0x0, 0x30e31d(%rip)    # 0x32c81c
movb $0x0, (%rax,%r14)
movslq 0x209f29(%rip), %rax    # 0x228434
testq %rax, %rax
je 0x1e51b
decq %rax
movl %eax, 0x209f1b(%rip)    # 0x228434
jmp 0x1e51d
xorl %eax, %eax
movslq 0x209f0c(%rip), %rcx    # 0x228430
imulq $0x85, %rcx, %rcx
leaq 0x30c0fe(%rip), %rdx    # 0x32a630
addq %rdx, %rcx
movb $0x0, (%rax,%rcx)
movl %ebx, %edi
callq 0x1dbfe
jmp 0x1e571
andl $0x0, 0x209eeb(%rip)    # 0x228434
movl %ebx, %edi
callq 0x1dd24
cmpq $0x1000, 0x1e4375(%rip) # imm = 0x1000
jne 0x1e567
pushq $0xd
popq %rdi
callq 0x1de39
jmp 0x1e571
movl $0x1, 0x30e2ab(%rip)    # 0x32c81c
movl %ebx, %edi
callq 0x1d61b
cmpb $0x0, 0x2250(%r12)
je 0x1e3ae
xorb $0x1, 0x30e292(%rip)    # 0x32c820
movl %ebx, %edi
callq 0x1dbfe
movl %ebx, %edi
callq 0x1d93f
jmp 0x1e3ae
xorb $0x1, 0x30e1e0(%rip)    # 0x32c788
jmp 0x1e38d
xorb $0x1, 0x30e1d0(%rip)    # 0x32c784
jmp 0x1e38d
xorb $0x1, 0x30e1c0(%rip)    # 0x32c780
jmp 0x1e38d
movq 0x209da4(%rip), %rdi    # 0x228370
testq %rdi, %rdi
je 0x1e5e3
callq 0x9360
movl 0x209e34(%rip), %ecx    # 0x228410
andl $-0x9, %ecx
xorl %eax, %eax
jmp 0x1e5ff
leaq 0x208a76(%rip), %rdi    # 0x227060
leaq 0xe5cd5(%rip), %rsi     # 0x1042c6
callq 0xa50c
movl 0x209e14(%rip), %ecx    # 0x228410
orl $0x8, %ecx
movq %rax, 0x209d6a(%rip)    # 0x228370
movl %ecx, 0x209e04(%rip)    # 0x228410
jmp 0x1e38d
xorb $0x1, 0x209e28(%rip)    # 0x228440
jmp 0x1e388
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x2238(%r12)
movl %ebx, %edi
callq 0x1d93f
andl $0x0, 0x30bfe6(%rip)    # 0x32a620
movq 0x1e428f(%rip), %rax    # 0x2028d0
cmpq $0x20000, %rax          # imm = 0x20000
je 0x1e657
cmpq $0x10000, %rax          # imm = 0x10000
je 0x1e671
cmpq $0x10, %rax
jne 0x1e668
andq $0x0, 0x2248(%r12)
cmpq $0x10000, %rax          # imm = 0x10000
je 0x1e671
callq 0x66b41
testl %eax, %eax
je 0x1e679
pushq $0x7
popq %rdi
callq 0x10a99
callq 0xe7d9
movsd 0x2f1652(%rip), %xmm0   # 0x30fcd8
addsd 0xd4ae2(%rip), %xmm0    # 0xf3170
divsd 0xd49a2(%rip), %xmm0    # 0xf3038
cvttsd2si %xmm0, %edx
leaq 0x307b7f(%rip), %rbx    # 0x326220
leaq 0xd6dc9(%rip), %rsi     # 0xf5471
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
pushq $0xe
popq %rdx
xorl %edi, %edi
xorl %esi, %esi
movq %rbx, %rcx
callq 0xe2d2
callq 0xaa05
movl $0x4d2, 0x1e4174(%rip)  # imm = 0x4D2
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq