// File: 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:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movl %edi, -0x4(%rbp)
cmpl $0x13, -0x4(%rbp)
jb 0x2c846
jmp 0x2ce37
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x340c6e(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpl $0x0, 0x2234(%rax)
jne 0x2c86f
movl $0x0, 0x2718a1(%rip)    # 0x29e110
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x340c45(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpl $0x0, 0x2230(%rax)
jge 0x2c898
movl $0x0, 0x271878(%rip)    # 0x29e110
cmpl $0x0, 0x271871(%rip)    # 0x29e110
jne 0x2c8a6
jmp 0x2ce37
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x340c0e(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movsd 0x2238(%rax), %xmm0
movsd %xmm0, -0x10(%rbp)
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x340beb(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movsd 0x142761(%rip), %xmm0   # 0x16f048
movsd %xmm0, 0x2238(%rax)
movl $0x1, 0x372f07(%rip)    # 0x39f800
cmpq $0x20000, 0x24b1ac(%rip) # imm = 0x20000
jne 0x2c91b
leaq 0x375063(%rip), %rdi    # 0x3a1970
leaq 0x144aac(%rip), %rsi    # 0x1713c0
callq 0x9350
jmp 0x2c976
cmpq $0x10, 0x24b18d(%rip)   # 0x277ab0
je 0x2c92f
cmpq $0x8, 0x24b183(%rip)    # 0x277ab0
jne 0x2c962
cmpl $0x0, 0x24b18e(%rip)    # 0x277ac4
je 0x2c94d
leaq 0x375031(%rip), %rdi    # 0x3a1970
leaq 0x144a86(%rip), %rsi    # 0x1713cc
callq 0x9350
jmp 0x2c960
leaq 0x37501c(%rip), %rdi    # 0x3a1970
leaq 0x15ab20(%rip), %rsi    # 0x18747b
callq 0x9350
jmp 0x2c974
movslq 0x37508f(%rip), %rcx    # 0x3a19f8
leaq 0x375000(%rip), %rax    # 0x3a1970
movb $0x0, (%rax,%rcx)
jmp 0x2c976
leaq 0x374ff3(%rip), %rdi    # 0x3a1970
callq 0x9120
movl %eax, 0x375070(%rip)    # 0x3a19f8
movl $0x0, 0x37506a(%rip)    # 0x3a19fc
movl $0x1, 0x374fc4(%rip)    # 0x3a1960
movl $0x0, 0x374fbe(%rip)    # 0x3a1964
movl $0x5, 0x374fac(%rip)    # 0x3a195c
movl $0x1, 0x270c6a(%rip)    # 0x29d624
callq 0x2c3d0
movl -0x4(%rbp), %edi
callq 0x2c4e0
movl 0x374f8f(%rip), %eax    # 0x3a195c
movl %eax, 0x270c3d(%rip)    # 0x29d610
movl $0x0, 0x270c37(%rip)    # 0x29d614
movl -0x4(%rbp), %edi
callq 0x2c120
callq 0x290e0
callq 0xf3d0
cmpl $0x0, %eax
je 0x2ccbc
callq 0xf3f0
movl %eax, -0x8(%rbp)
cmpl $0x14f, -0x8(%rbp)      # imm = 0x14F
jne 0x2ca0e
jmp 0x2cd46
cmpl $0x147, -0x8(%rbp)      # imm = 0x147
jne 0x2ca19
jmp 0x2c9ba
cmpl $0x142, -0x8(%rbp)      # imm = 0x142
jne 0x2ca2f
movl -0x4(%rbp), %edi
callq 0xb840
jmp 0x2ccb6
cmpl $0x13b, -0x8(%rbp)      # imm = 0x13B
jne 0x2ca4c
movl 0x374f22(%rip), %eax    # 0x3a1960
xorl $0x1, %eax
movl %eax, 0x374f19(%rip)    # 0x3a1960
jmp 0x2c9bf
cmpl $0x13c, -0x8(%rbp)      # imm = 0x13C
jne 0x2cab5
cmpq $0x0, 0x270af3(%rip)    # 0x29d550
je 0x2ca87
movq 0x270aea(%rip), %rdi    # 0x29d550
callq 0x9380
movq $0x0, 0x270ada(%rip) # 0x29d550
movl 0x270b74(%rip), %eax    # 0x29d5f0
andl $-0x9, %eax
movl %eax, 0x270b6b(%rip)    # 0x29d5f0
jmp 0x2cab0
leaq 0x26f7b2(%rip), %rdi    # 0x29c240
leaq 0x1521b3(%rip), %rsi    # 0x17ec48
callq 0xa960
movq %rax, 0x270aaf(%rip)    # 0x29d550
movl 0x270b49(%rip), %eax    # 0x29d5f0
orl $0x8, %eax
movl %eax, 0x270b40(%rip)    # 0x29d5f0
jmp 0x2c9bf
cmpl $0x13d, -0x8(%rbp)      # imm = 0x13D
jne 0x2cad2
movl 0x270b5c(%rip), %eax    # 0x29d620
xorl $0x1, %eax
movl %eax, 0x270b53(%rip)    # 0x29d620
jmp 0x2c9ba
cmpl $0x13e, -0x8(%rbp)      # imm = 0x13E
jne 0x2caef
movl 0x374e83(%rip), %eax    # 0x3a1964
xorl $0x1, %eax
movl %eax, 0x374e7a(%rip)    # 0x3a1964
jmp 0x2c9bf
cmpl $0x13f, -0x8(%rbp)      # imm = 0x13F
jne 0x2cb0c
movl 0x374e6a(%rip), %eax    # 0x3a1968
xorl $0x1, %eax
movl %eax, 0x374e61(%rip)    # 0x3a1968
jmp 0x2c9bf
cmpl $0x148, -0x8(%rbp)      # imm = 0x148
jne 0x2cb80
jmp 0x2cb17
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl 0x374ed1(%rip), %eax    # 0x3a19f8
jge 0x2cb71
movl -0x4(%rbp), %edi
movslq -0x8(%rbp), %rcx
movl $0x1, %edx
leaq 0x374e34(%rip), %rax    # 0x3a1970
movzbl (%rax,%rcx), %esi
callq 0xd160
movl -0x4(%rbp), %edi
movslq -0x8(%rbp), %rcx
leaq 0x374e1d(%rip), %rax    # 0x3a1970
movsbl (%rax,%rcx), %esi
orl $0x80, %esi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0xb1d0
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x2cb1e
movl $0x1, 0x374e81(%rip)    # 0x3a19fc
jmp 0x2ccaa
cmpl $0x0, 0x27158d(%rip)    # 0x29e114
je 0x2cbaa
cmpl $0xd, -0x8(%rbp)
jne 0x2cba8
movl $0xd, %edi
callq 0x2c460
movl $0xa, %edi
callq 0x2c460
jmp 0x2cb17
jmp 0x2cbbd
movl -0x4(%rbp), %edi
movl -0x8(%rbp), %eax
movl $0x1, %edx
movzbl %al, %esi
callq 0xd160
cmpl $0x8, -0x8(%rbp)
jne 0x2cc3c
cmpl $0x0, 0x374e2e(%rip)    # 0x3a19f8
je 0x2cbdb
movl 0x374e26(%rip), %eax    # 0x3a19f8
addl $-0x1, %eax
movl %eax, 0x374e1d(%rip)    # 0x3a19f8
movslq 0x374e16(%rip), %rcx    # 0x3a19f8
leaq 0x374d87(%rip), %rax    # 0x3a1970
movb $0x0, (%rax,%rcx)
movl $0x0, 0x374e05(%rip)    # 0x3a19fc
cmpl $0x0, 0x270a16(%rip)    # 0x29d614
je 0x2cc0f
movl 0x270a0e(%rip), %eax    # 0x29d614
addl $-0x1, %eax
movl %eax, 0x270a05(%rip)    # 0x29d614
movslq 0x2709fa(%rip), %rcx    # 0x29d610
leaq 0x372bf3(%rip), %rax    # 0x39f810
imulq $0x85, %rcx, %rcx
addq %rcx, %rax
movslq 0x2709e6(%rip), %rcx    # 0x29d614
movb $0x0, (%rax,%rcx)
movl -0x4(%rbp), %edi
callq 0x2c120
jmp 0x2cca6
cmpl $0xd, -0x8(%rbp)
jne 0x2cc77
movl $0x0, 0x2709c8(%rip)    # 0x29d614
movl -0x4(%rbp), %edi
callq 0x2c2e0
cmpq $0x1000, 0x24ae51(%rip) # imm = 0x1000
je 0x2cc6d
movl $0x1, 0x374d91(%rip)    # 0x3a19fc
jmp 0x2cc75
movl -0x8(%rbp), %edi
callq 0x2c460
jmp 0x2cca4
movl -0x8(%rbp), %edi
callq 0x2c460
movl -0x8(%rbp), %eax
orl $0x80, %eax
movl %eax, -0x8(%rbp)
cmpl $0x0, 0x374ccf(%rip)    # 0x3a1960
je 0x2cca2
movl -0x4(%rbp), %edi
movl -0x8(%rbp), %esi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0xb1d0
jmp 0x2cca4
jmp 0x2cca6
jmp 0x2cca8
jmp 0x2ccaa
jmp 0x2ccac
jmp 0x2ccae
jmp 0x2ccb0
jmp 0x2ccb2
jmp 0x2ccb4
jmp 0x2ccb6
jmp 0x2ccb8
jmp 0x2ccba
jmp 0x2ccfb
movl -0x4(%rbp), %edi
callq 0xcad0
cmpl $0x0, %eax
je 0x2ccef
callq 0x79fc0
movl %eax, -0x8(%rbp)
cmpl $0x0, 0x374c8c(%rip)    # 0x3a1964
jne 0x2cced
movl -0x4(%rbp), %edi
movl -0x8(%rbp), %esi
movl 0x27093a(%rip), %edx    # 0x29d620
xorl %ecx, %ecx
callq 0xb1d0
jmp 0x2ccf9
movl $0xa, %edi
callq 0xb8b0
jmp 0x2ccfb
movl -0x4(%rbp), %edi
callq 0x2b610
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x3407b1(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpb $0x0, 0x2250(%rax)
je 0x2cd41
movl 0x374cd8(%rip), %eax    # 0x3a1a00
xorl $0x1, %eax
movl %eax, 0x374ccf(%rip)    # 0x3a1a00
movl -0x4(%rbp), %edi
callq 0x2c120
movl -0x4(%rbp), %edi
callq 0x2bd30
jmp 0x2c9e5
movsd -0x10(%rbp), %xmm0
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x340769(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movsd %xmm0, 0x2238(%rax)
movl -0x4(%rbp), %edi
callq 0x2bd30
movl $0x0, 0x372a85(%rip)    # 0x39f800
cmpq $0x10, 0x24ad2d(%rip)   # 0x277ab0
je 0x2cd92
cmpq $0x20000, 0x24ad20(%rip) # imm = 0x20000
jne 0x2cdb3
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x340722(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x2248(%rax)
cmpq $0x10000, 0x24acf2(%rip) # imm = 0x10000
je 0x2cdca
callq 0x992e0
cmpl $0x0, %eax
je 0x2cdd9
movl $0x7, %edi
movl $0x1, %esi
callq 0x14df0
callq 0x117e0
movsd 0x14248a(%rip), %xmm0   # 0x16f270
addsd 0x3580ca(%rip), %xmm0   # 0x384eb8
movsd 0x14223a(%rip), %xmm1   # 0x16f030
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
leaq 0x36e5fb(%rip), %rdi    # 0x39b400
leaq 0x1445d7(%rip), %rsi    # 0x1713e3
movb $0x0, %al
callq 0x9060
xorl %esi, %esi
movl $0xe, %edx
leaq 0x36e5df(%rip), %rcx    # 0x39b400
movl %esi, %edi
callq 0x10ee0
callq 0xb0d0
movl $0x4d2, 0x24abed(%rip)  # imm = 0x4D2
addq $0x10, %rsp
popq %rbp
retq
nopl (%rax)