// File: /workspace/github_cpp_makefile_star2_O1/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 0x1f9f2
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 0x2eef0b(%rip), %rcx    # 0x30e2e0
leaq (%rcx,%rax), %r15
cmpl $0x0, 0x2234(%rax,%rcx)
jne 0x1f3ed
movl $0x0, 0x21fb43(%rip)    # 0x23ef30
cmpl $0x0, 0x2230(%r15)
jns 0x1f401
movl $0x0, 0x21fb2f(%rip)    # 0x23ef30
cmpl $0x0, 0x21fb28(%rip)    # 0x23ef30
je 0x1f9e4
movsd 0x2238(%r15), %xmm0
movabsq $0x406f400000000000, %rax # imm = 0x406F400000000000
movq %rax, 0x2238(%r15)
movl $0x1, 0x3211ee(%rip)    # 0x340620
movq 0x1f9497(%rip), %rax    # 0x2188d0
cmpq $0x8, %rax
movsd %xmm0, -0x30(%rbp)
je 0x1f46f
cmpq $0x10, %rax
je 0x1f46f
cmpq $0x20000, %rax          # imm = 0x20000
jne 0x1f49b
movabsq $0x4952503a54535953, %rax # imm = 0x4952503A54535953
movq %rax, 0x32332d(%rip)    # 0x342790
movl $0x3f544e, 0x32332b(%rip) # imm = 0x3F544E
jmp 0x1f4ca
cmpl $0x0, 0x1f946e(%rip)    # 0x2188e4
je 0x1f4af
movupd 0xebf4f(%rip), %xmm0    # 0x10b3cf
movapd %xmm0, 0x323308(%rip)   # 0x342790
movabsq $0x3020505544463a, %rax # imm = 0x3020505544463A
movq %rax, 0x323306(%rip)    # 0x34279f
jmp 0x1f4ca
movslq 0x323376(%rip), %rax    # 0x342818
leaq 0x3232e7(%rip), %rcx    # 0x342790
movb $0x0, (%rax,%rcx)
jmp 0x1f4ca
movabsq $0x4154533a54535953, %rax # imm = 0x4154533A54535953
movq %rax, 0x3232d0(%rip)    # 0x342790
movl $0x3f5441, 0x3232cd(%rip) # imm = 0x3F5441
leaq 0x3232bf(%rip), %r14    # 0x342790
movq %r14, %rdi
callq 0x9120
movl %eax, 0x323339(%rip)    # 0x342818
xorl %ecx, %ecx
movl %ecx, 0x323335(%rip)    # 0x34281c
movl $0x1, %eax
movl %eax, 0x32328e(%rip)    # 0x342780
xorl %r12d, %r12d
movl %ecx, 0x323289(%rip)    # 0x342784
movl $0x5, 0x323277(%rip)    # 0x34277c
movl %eax, 0x21ef39(%rip)    # 0x23e444
callq 0x1ef31
movl %ebx, %edi
callq 0x1efea
movl 0x32325f(%rip), %eax    # 0x34277c
movl %eax, 0x21ef0d(%rip)    # 0x23e430
movl $0x0, 0x21ef07(%rip)    # 0x23e434
movl %ebx, %edi
callq 0x1ed16
callq 0xd5c9
movq 0x151980(%rip), %rdi    # 0x170ec0
callq 0x95c0
callq 0xa826
mulsd 0xe9ae6(%rip), %xmm0    # 0x109038
addsd 0xe9afe(%rip), %xmm0    # 0x109058
movsd %xmm0, 0x2ee036(%rip)   # 0x30d598
movl 0x320ae0(%rip), %eax    # 0x340048
cmpl %eax, 0x320ad6(%rip)    # 0x340044
jne 0x1f59c
movl %ebx, %edi
callq 0xb9c3
testl %eax, %eax
je 0x1f5e2
callq 0x584d5
cmpl $0x0, 0x3231fd(%rip)    # 0x342784
jne 0x1f7d7
movl 0x21eead(%rip), %edx    # 0x23e440
movl %ebx, %edi
movl %eax, %esi
jmp 0x1f7d0
leal 0x1(%rax), %ecx
cmpl $0xf, %eax
cmovgel %r12d, %ecx
movl %ecx, 0x320a9c(%rip)    # 0x340048
movslq %ecx, %rax
leaq 0x320a4a(%rip), %rcx    # 0x340000
movl (%rcx,%rax,4), %r13d
leal -0x13b(%r13), %eax
cmpl $0x14, %eax
ja 0x1f5f1
leaq 0xe9f9b(%rip), %rcx     # 0x109568
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl %ebx, %edi
callq 0xaf8f
jmp 0x1f7d7
movl $0x2710, %edi           # imm = 0x2710
callq 0x9540
jmp 0x1f7d7
cmpl $0x0, 0x21f93c(%rip)    # 0x23ef34
je 0x1f6ef
cmpl $0x8, %r13d
je 0x1f70f
cmpl $0xd, %r13d
jne 0x1f775
cmpl $0x0, 0x323203(%rip)    # 0x34281c
je 0x1f633
xorl %eax, %eax
movl %eax, 0x3231f5(%rip)    # 0x342818
movb $0x0, 0x323166(%rip)    # 0x342790
xorl %r12d, %r12d
movl %eax, 0x3231e9(%rip)    # 0x34281c
movslq 0x3231de(%rip), %rax    # 0x342818
cmpq $0x82, %rax
jg 0x1f657
leaq 0x1(%rax), %rcx
movl %ecx, 0x3231cc(%rip)    # 0x342818
movb $0xd, (%rax,%r14)
movb $0x0, 0x1(%rax,%r14)
cmpl $0x0, 0x3231be(%rip)    # 0x34281c
je 0x1f678
xorl %eax, %eax
movl %eax, 0x3231b0(%rip)    # 0x342818
movb $0x0, 0x323121(%rip)    # 0x342790
xorl %r12d, %r12d
movl %eax, 0x3231a4(%rip)    # 0x34281c
movslq 0x323199(%rip), %rax    # 0x342818
cmpq $0x82, %rax
jg 0x1f69c
leaq 0x1(%rax), %rcx
movl %ecx, 0x323187(%rip)    # 0x342818
movb $0xa, (%rax,%r14)
movb $0x0, 0x1(%rax,%r14)
cmpl $0x0, 0x323175(%rip)    # 0x342818
jle 0x1f6e0
xorl %r13d, %r13d
movzbl (%r13,%r14), %esi
movl %ebx, %edi
movl $0x1, %edx
callq 0xbcc0
movsbl (%r13,%r14), %esi
orl $0x80, %esi
movl %ebx, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xac26
incq %r13
movslq 0x32313d(%rip), %rax    # 0x342818
cmpq %rax, %r13
jl 0x1f6a8
movl $0x1, 0x323132(%rip)    # 0x34281c
jmp 0x1f7d7
movzbl %r13b, %esi
movl %ebx, %edi
movl $0x1, %edx
callq 0xbcc0
cmpl $0xd, %r13d
je 0x1f81d
cmpl $0x8, %r13d
jne 0x1f775
movl 0x323103(%rip), %eax    # 0x342818
testl %eax, %eax
je 0x1f721
decl %eax
movl %eax, 0x3230f7(%rip)    # 0x342818
movslq 0x3230f0(%rip), %rax    # 0x342818
movb $0x0, (%rax,%r14)
movl $0x0, 0x3230e5(%rip)    # 0x34281c
movl 0x21ecf7(%rip), %eax    # 0x23e434
testl %eax, %eax
je 0x1f749
decl %eax
movl %eax, 0x21eceb(%rip)    # 0x23e434
movslq 0x21ece0(%rip), %rax    # 0x23e430
movslq 0x21ecdd(%rip), %rcx    # 0x23e434
imulq $0x85, %rax, %rax
leaq 0x320ecb(%rip), %rdx    # 0x340630
addq %rdx, %rax
movb $0x0, (%rcx,%rax)
movl %ebx, %edi
callq 0x1ed16
jmp 0x1f7d7
cmpl $0x0, 0x3230a0(%rip)    # 0x34281c
je 0x1f796
xorl %eax, %eax
movl %eax, 0x323092(%rip)    # 0x342818
movb $0x0, 0x323003(%rip)    # 0x342790
xorl %r12d, %r12d
movl %eax, 0x323086(%rip)    # 0x34281c
movslq 0x32307b(%rip), %rax    # 0x342818
cmpq $0x82, %rax
jg 0x1f7b9
leaq 0x1(%rax), %rcx
movl %ecx, 0x323069(%rip)    # 0x342818
movb %r13b, (%rax,%r14)
movb $0x0, 0x1(%rax,%r14)
cmpl $0x0, 0x322fc0(%rip)    # 0x342780
je 0x1f7d7
orl $0x80, %r13d
movl %ebx, %edi
movl %r13d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xac26
movl %ebx, %edi
callq 0x1e6a0
cmpb $0x0, 0x2250(%r15)
je 0x1f534
xorb $0x1, 0x32302d(%rip)    # 0x342820
movl %ebx, %edi
callq 0x1ed16
callq 0xd1865
movsd 0x2edf61(%rip), %xmm0   # 0x30d768
movsd %xmm0, 0x2248(%r15)
movb $0x0, 0x2250(%r15)
jmp 0x1f534
movl $0x0, 0x21ec0d(%rip)    # 0x23e434
movl %ebx, %edi
callq 0x1ee81
cmpq $0x1000, 0x1f9097(%rip) # imm = 0x1000
jne 0x1f6e0
cmpl $0x0, 0x322fd6(%rip)    # 0x34281c
je 0x1f860
xorl %eax, %eax
movl %eax, 0x322fc8(%rip)    # 0x342818
movb $0x0, 0x322f39(%rip)    # 0x342790
xorl %r12d, %r12d
movl %eax, 0x322fbc(%rip)    # 0x34281c
movslq 0x322fb1(%rip), %rax    # 0x342818
cmpq $0x82, %rax
jg 0x1f7d7
leaq 0x1(%rax), %rcx
movl %ecx, 0x322f9b(%rip)    # 0x342818
movb $0xd, (%rax,%r14)
movb $0x0, 0x1(%rax,%r14)
jmp 0x1f7d7
xorb $0x1, 0x322ef4(%rip)    # 0x342788
jmp 0x1f510
xorb $0x1, 0x322ee4(%rip)    # 0x342784
jmp 0x1f510
xorb $0x1, 0x322ed4(%rip)    # 0x342780
jmp 0x1f510
movq 0x21eab8(%rip), %rdi    # 0x23e370
testq %rdi, %rdi
je 0x1f8d8
callq 0x9370
movq $0x0, 0x21eaa3(%rip) # 0x23e370
movl 0x21eb3d(%rip), %eax    # 0x23e410
andl $-0x9, %eax
jmp 0x1f8fb
leaq 0x21d781(%rip), %rdi    # 0x23d060
leaq 0xfa8cc(%rip), %rsi     # 0x11a1b2
callq 0xa639
movq %rax, 0x21ea7e(%rip)    # 0x23e370
movl 0x21eb18(%rip), %eax    # 0x23e410
orl $0x8, %eax
movl %eax, 0x21eb0f(%rip)    # 0x23e410
jmp 0x1f510
xorb $0x1, 0x21eb33(%rip)    # 0x23e440
jmp 0x1f50b
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x2238(%r15)
callq 0xd1865
movsd 0x2ede3b(%rip), %xmm0   # 0x30d768
movsd %xmm0, 0x2248(%r15)
movb $0x0, 0x2250(%r15)
movl $0x0, 0x320cd8(%rip)    # 0x340620
movq 0x1f8f81(%rip), %rax    # 0x2188d0
cmpq $0x20000, %rax          # imm = 0x20000
je 0x1f965
cmpq $0x10000, %rax          # imm = 0x10000
je 0x1f981
cmpq $0x10, %rax
jne 0x1f978
movq $0x0, 0x2248(%r15)
cmpq $0x10000, %rax          # imm = 0x10000
je 0x1f981
callq 0x701f2
testl %eax, %eax
je 0x1f98b
movl $0x7, %edi
callq 0x1120f
callq 0xed5e
movsd 0x306340(%rip), %xmm0   # 0x325cd8
addsd 0xe97c8(%rip), %xmm0    # 0x109168
divsd 0xe9690(%rip), %xmm0    # 0x109038
cvttsd2si %xmm0, %edx
leaq 0x31c86d(%rip), %rbx    # 0x33c220
leaq 0xeba2c(%rip), %rsi     # 0x10b3e6
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
xorl %edi, %edi
xorl %esi, %esi
movl $0xe, %edx
movq %rbx, %rcx
callq 0xe7d0
callq 0xab5f
movl $0x4d2, 0x1f8e60(%rip)  # imm = 0x4D2
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq