// File: /workspace/github_cpp_makefile_star2_O3/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 0x1f9c2
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 0x2eef17(%rip), %rcx    # 0x30e2e0
leaq (%rcx,%rax), %r15
cmpl $0x0, 0x2234(%rax,%rcx)
jne 0x1f3e1
movl $0x0, 0x21fb4f(%rip)    # 0x23ef30
cmpl $0x0, 0x2230(%r15)
js 0x1f459
cmpl $0x0, 0x21fb3e(%rip)    # 0x23ef30
je 0x1f9b4
movsd 0x2238(%r15), %xmm0
movabsq $0x406f400000000000, %rax # imm = 0x406F400000000000
movq %rax, 0x2238(%r15)
movl $0x1, 0x321204(%rip)    # 0x340620
movq 0x1f94ad(%rip), %rax    # 0x2188d0
cmpq $0x8, %rax
movsd %xmm0, -0x30(%rbp)
je 0x1f468
cmpq $0x10, %rax
je 0x1f468
cmpq $0x20000, %rax          # imm = 0x20000
jne 0x1f494
movabsq $0x4952503a54535953, %rax # imm = 0x4952503A54535953
movq %rax, 0x323343(%rip)    # 0x342790
movl $0x3f544e, 0x323341(%rip) # imm = 0x3F544E
jmp 0x1f4c3
movl $0x0, 0x21facd(%rip)    # 0x23ef30
jmp 0x1f9b4
cmpl $0x0, 0x1f9475(%rip)    # 0x2188e4
je 0x1f4a8
movupd 0xebf66(%rip), %xmm0    # 0x10b3df
movapd %xmm0, 0x32330f(%rip)   # 0x342790
movabsq $0x3020505544463a, %rax # imm = 0x3020505544463A
movq %rax, 0x32330d(%rip)    # 0x34279f
jmp 0x1f4c3
movslq 0x32337d(%rip), %rax    # 0x342818
leaq 0x3232ee(%rip), %rcx    # 0x342790
movb $0x0, (%rax,%rcx)
jmp 0x1f4c3
movabsq $0x4154533a54535953, %rax # imm = 0x4154533A54535953
movq %rax, 0x3232d7(%rip)    # 0x342790
movl $0x3f5441, 0x3232d4(%rip) # imm = 0x3F5441
leaq 0x3232c6(%rip), %r14    # 0x342790
movq %r14, %rdi
callq 0x9120
movl %eax, 0x323340(%rip)    # 0x342818
xorl %ecx, %ecx
movl %ecx, 0x32333c(%rip)    # 0x34281c
movl $0x1, %eax
movl %eax, 0x323295(%rip)    # 0x342780
movl %ecx, 0x323293(%rip)    # 0x342784
movl $0x5, 0x323281(%rip)    # 0x34277c
movl %eax, 0x21ef43(%rip)    # 0x23e444
leaq 0xea070(%rip), %r12     # 0x109578
callq 0x1ef25
movl %ebx, %edi
callq 0x1efde
movl 0x323262(%rip), %eax    # 0x34277c
movl %eax, 0x21ef10(%rip)    # 0x23e430
movl $0x0, 0x21ef0a(%rip)    # 0x23e434
movl %ebx, %edi
callq 0x1ecf5
callq 0xd5aa
movq 0x151983(%rip), %rdi    # 0x170ec0
callq 0x95c0
callq 0xa80b
mulsd 0xe9ae9(%rip), %xmm0    # 0x109038
addsd 0xe9b01(%rip), %xmm0    # 0x109058
movsd %xmm0, 0x2ee039(%rip)   # 0x30d598
movl 0x320ae3(%rip), %eax    # 0x340048
cmpl %eax, 0x320ad9(%rip)    # 0x340044
jne 0x1f599
movl %ebx, %edi
callq 0xb9ad
testl %eax, %eax
je 0x1f5dc
callq 0x584bd
cmpl $0x0, 0x323200(%rip)    # 0x342784
jne 0x1f7af
movl 0x21eeb0(%rip), %edx    # 0x23e440
movl %ebx, %edi
movl %eax, %esi
jmp 0x1f7a8
leal 0x1(%rax), %ecx
cmpl $0xf, %eax
movl $0x0, %eax
cmovgel %eax, %ecx
movl %ecx, 0x320a9b(%rip)    # 0x340048
movslq %ecx, %rax
leaq 0x320a49(%rip), %rcx    # 0x340000
movl (%rcx,%rax,4), %r13d
leal -0x13b(%r13), %eax
cmpl $0x14, %eax
ja 0x1f63f
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movl %ebx, %edi
callq 0xaf80
jmp 0x1f7af
movl $0x2710, %edi           # imm = 0x2710
callq 0x9540
jmp 0x1f7af
movl 0x323227(%rip), %edx    # 0x342818
testl %edx, %edx
jle 0x1f630
xorl %r13d, %r13d
movzbl (%r13,%r14), %esi
movl %ebx, %edi
movl $0x1, %edx
callq 0xbcaa
movsbl (%r13,%r14), %esi
orl $0x80, %esi
movl %ebx, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xac18
incq %r13
movslq 0x3231ed(%rip), %rax    # 0x342818
cmpq %rax, %r13
jl 0x1f5f8
movl $0x1, 0x3231e2(%rip)    # 0x34281c
jmp 0x1f7af
cmpl $0x0, 0x21f8ee(%rip)    # 0x23ef34
je 0x1f679
cmpl $0x8, %r13d
je 0x1f699
cmpl $0xd, %r13d
jne 0x1f6b0
cmpl $0x0, 0x3231c1(%rip)    # 0x34281c
je 0x1f7f5
movb $0x0, 0x323128(%rip)    # 0x342790
movl $0x0, 0x3231aa(%rip)    # 0x34281c
xorl %eax, %eax
jmp 0x1f806
movzbl %r13b, %esi
movl %ebx, %edi
movl $0x1, %edx
callq 0xbcaa
cmpl $0xd, %r13d
je 0x1f727
cmpl $0x8, %r13d
jne 0x1f6b0
movslq 0x323178(%rip), %rax    # 0x342818
testq %rax, %rax
je 0x1f6d5
decq %rax
movl %eax, 0x32316a(%rip)    # 0x342818
jmp 0x1f6d7
cmpl $0x0, 0x323165(%rip)    # 0x34281c
je 0x1f76e
movb $0x0, 0x3230cc(%rip)    # 0x342790
movl $0x0, 0x32314e(%rip)    # 0x34281c
xorl %eax, %eax
jmp 0x1f77b
xorl %eax, %eax
movb $0x0, (%rax,%r14)
movl $0x0, 0x323136(%rip)    # 0x34281c
movslq 0x21ed47(%rip), %rax    # 0x23e434
testq %rax, %rax
je 0x1f6fd
decq %rax
movl %eax, 0x21ed39(%rip)    # 0x23e434
jmp 0x1f6ff
xorl %eax, %eax
movslq 0x21ed2a(%rip), %rcx    # 0x23e430
imulq $0x85, %rcx, %rcx
leaq 0x320f1c(%rip), %rdx    # 0x340630
addq %rdx, %rcx
movb $0x0, (%rax,%rcx)
movl %ebx, %edi
callq 0x1ecf5
jmp 0x1f7af
movl $0x0, 0x21ed03(%rip)    # 0x23e434
movl %ebx, %edi
callq 0x1ee63
cmpq $0x1000, 0x1f918d(%rip) # imm = 0x1000
jne 0x1f630
cmpl $0x0, 0x3230cc(%rip)    # 0x34281c
je 0x1f83e
movb $0x0, 0x323033(%rip)    # 0x342790
movl $0x0, 0x3230b5(%rip)    # 0x34281c
xorl %eax, %eax
jmp 0x1f84f
movl 0x3230a4(%rip), %eax    # 0x342818
cmpl $0x82, %eax
jg 0x1f791
cltq
leaq 0x1(%rax), %rcx
movl %ecx, 0x323091(%rip)    # 0x342818
movb %r13b, (%rax,%r14)
movb $0x0, 0x1(%rax,%r14)
cmpl $0x0, 0x322fe8(%rip)    # 0x342780
je 0x1f7af
orl $0x80, %r13d
movl %ebx, %edi
movl %r13d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xac18
movl %ebx, %edi
callq 0x1e66a
cmpb $0x0, 0x2250(%r15)
je 0x1f531
xorb $0x1, 0x323055(%rip)    # 0x342820
movl %ebx, %edi
callq 0x1ecf5
callq 0xd1efe
movsd 0x2edf89(%rip), %xmm0   # 0x30d768
movsd %xmm0, 0x2248(%r15)
movb $0x0, 0x2250(%r15)
jmp 0x1f531
movl 0x32301d(%rip), %eax    # 0x342818
cmpl $0x82, %eax
jg 0x1f5f5
cltq
leaq 0x1(%rax), %rcx
movl %ecx, 0x323006(%rip)    # 0x342818
movw $0xd, (%rax,%r14)
cmpl $0x81, %eax
jg 0x1f5f5
leaq 0x2(%rax), %rdx
movl %edx, 0x322fea(%rip)    # 0x342818
movb $0xa, (%rcx,%r14)
movb $0x0, 0x2(%rax,%r14)
jmp 0x1f5f1
movl 0x322fd4(%rip), %eax    # 0x342818
cmpl $0x82, %eax
jg 0x1f7af
cltq
leal 0x1(%rax), %ecx
movl %ecx, 0x322fbe(%rip)    # 0x342818
movw $0xd, (%rax,%r14)
jmp 0x1f7af
xorb $0x1, 0x322f1b(%rip)    # 0x342788
jmp 0x1f50d
xorb $0x1, 0x322f07(%rip)    # 0x342780
jmp 0x1f50d
movq 0x21eaeb(%rip), %rdi    # 0x23e370
testq %rdi, %rdi
je 0x1f8a8
callq 0x9370
movl 0x21eb7b(%rip), %ecx    # 0x23e410
andl $-0x9, %ecx
xorl %eax, %eax
jmp 0x1f8c4
xorb $0x1, 0x322ee1(%rip)    # 0x342784
jmp 0x1f50d
leaq 0x21d7b1(%rip), %rdi    # 0x23d060
leaq 0xfa990(%rip), %rsi     # 0x11a246
callq 0xa61d
movl 0x21eb4f(%rip), %ecx    # 0x23e410
orl $0x8, %ecx
movq %rax, 0x21eaa5(%rip)    # 0x23e370
movl %ecx, 0x21eb3f(%rip)    # 0x23e410
jmp 0x1f50d
xorb $0x1, 0x21eb63(%rip)    # 0x23e440
jmp 0x1f508
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x2238(%r15)
callq 0xd1efe
movsd 0x2ede6b(%rip), %xmm0   # 0x30d768
movsd %xmm0, 0x2248(%r15)
movb $0x0, 0x2250(%r15)
movl $0x0, 0x320d08(%rip)    # 0x340620
movq 0x1f8fb1(%rip), %rax    # 0x2188d0
cmpq $0x20000, %rax          # imm = 0x20000
je 0x1f935
cmpq $0x10000, %rax          # imm = 0x10000
je 0x1f951
cmpq $0x10, %rax
jne 0x1f948
movq $0x0, 0x2248(%r15)
cmpq $0x10000, %rax          # imm = 0x10000
je 0x1f951
callq 0x702e7
testl %eax, %eax
je 0x1f95b
movl $0x7, %edi
callq 0x1118c
callq 0xeceb
movsd 0x306370(%rip), %xmm0   # 0x325cd8
addsd 0xe9800(%rip), %xmm0    # 0x109170
divsd 0xe96c0(%rip), %xmm0    # 0x109038
cvttsd2si %xmm0, %edx
leaq 0x31c89d(%rip), %rbx    # 0x33c220
leaq 0xeba6c(%rip), %rsi     # 0x10b3f6
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
xorl %edi, %edi
xorl %esi, %esi
movl $0xe, %edx
movq %rbx, %rcx
callq 0xe787
callq 0xab54
movl $0x4d2, 0x1f8e90(%rip)  # imm = 0x4D2
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq