
 int run(int k1, int run_mode)
 {
  int by, byh, byl, ms, md, m1;
  int i, y[21], num_of_ports, port, cro_use_flag = 0; 
  int y2, y3 = 0, pold = XREF, change;
  float amp[MAX_PORTS + 1], set = 0.0, samp[MAX_PORTS + 1], stime, lap = 0;
  int time_scale;
  char yt;
  char print_buff[512];
  rpc = k1 - 1;
  clear_help();
  gotoxy(1, 21);
  if(run_mode == NORMAL_RUN_MODE || run_mode == TRACE_MODE)
  cro_use_flag = cro_use();
  clrscr();
  if(cro_use_flag && run_mode < 2) {
	clear_help();
	gotoxy(1, 21);
	write(1,"ENTER NUMBER OF PORTS TO BE USED.MAX(9): ",42);
	num_of_ports = ascci_to_int(getch());
	if(num_of_ports > 9 || num_of_ports < 1) num_of_ports = 1;
	for(y2 = 1;y2 <= num_of_ports;y2++) 
		samp[y2] = cro_y(y2);  /*in milli volts*/
	stime = cro_x();
	time_scale = stime;
	stime = 16.0 / stime;
  }
  if(run_mode == 2) {
	num_of_ports = 1;
	samp[1] = 1000;
	time_scale = 500;
	stime = .032;
  }
  else if(run_mode == 3) {
	num_of_ports = 2;
	samp[1] = 2000;
	samp[2] = 1000;
	stime = .032;
	time_scale = 500;
  }

  if(run_mode == 1) call_t_help();
  while(1) {
	rpc++;
	if(run_mode == 1) {
		if(show_trace() == 1) return(0);
	}
	if(y3) {
		for(y2 = 1;y2 <= num_of_ports;y2++)
			y[y2] = YREF - (int)amp[y2];
		set += lap * stime;
		if(set < 1) {
			for(y2 = 1;y2 <= num_of_ports;y2++) putpixel(pold, y[y2], y2);
		}
		while(set >= 1) {
			pold++;
			if(pold == XREF + XLIM) {
				gotoxy(5, 20);
				write(1,"PRESS y TO TERMINATE AND c TO CHANGE THE AMPLITUDE SCALE OF C.R.O:",66);
				gotoxy(5, 21);
				sprintf(print_buff,"TIME SCALE:1 UNIT: %d micro Seconds", time_scale);
				write(1,print_buff,strlen(print_buff));
				yt = getch();
				if(yt == 'c') {
					gotoxy(5, 23);
					write(1,"ENTER THE PORT NUMBER:",22);
					change = ascci_to_int(getche());
					samp[change] = cro_y(change);
					gotoxy(5, 23);
					write(1,"ENTER c TO CHANGE TIME SCALE:",29);
					yt = getch();
					if(yt == 'c') {
						stime = cro_x();
						time_scale = stime;
						gotoxy(5, 21);
						sprintf(print_buff,"TIME SCALE:1 UNIT: %d micro Seconds", time_scale);
						write(1,print_buff,strlen(print_buff));
						stime = 16.0 / stime;
					}
				}
				if(yt == 'y') {
					if(run_mode == 2 || run_mode == 3)
					return(1);
					decorate();
					reset();
					return(1);
				}
				else pold = XREF;
			}
			for(i = YREF;i > YREF - YLIM;i--)
				putpixel(pold, i, 40);
			for(y2 = 1;y2 <= num_of_ports;y2++)
				putpixel(pold, y[y2], y2);
			set -= 1;
		}
	}


	by = memr(rpc);
	byh = by / 0x10;
	byl = by % 0x10;
	if(by == 0xd3)  {     /*OUT*/
		port = memr(++rpc);
		if(y3 == 0 && port < 10 && port > 0 && num_of_ports) {
			clrscr();
			if(run_mode == 1) call_t_help();
			draw_sheet();
			y3 = 1;
		}
		lap = 10.0 / 3.0;
		if(y3 && port <= num_of_ports && port > 0) {
			amp[port] = (float)reg[7] * 312.5 / (float)samp[port];
			/*     16 pixels per unit   */
			//if(amp[port] > YLIM)
			//	amp[port] = 0;
			continue;
		}
	}


	else if(by == 0x76 || by == 0xef)  {  /*HLT*/
		return(halt());
	}


	else if(by > 0x3f && by < 0x80)  {           /*MOV*/
		mov(by,&lap);
		continue;
	}


	else if(byh == 8 || by == 0xce || by == 0xc6) {   /*ADD ADC ADI ACI*/
		add_c_i(by, byl, byh, &lap);
		continue;
	}

	else if(byh == 0xa)  {         /*ANA XRA*/
		ana_xra(byl, &lap);
		continue;
	}


	else if(byh == 0xb || by == 0xf6 || by == 0xfe)  {    /*ORA CMP*/
		ora_cmp_i(by, byl, byh, &lap);
		continue;
	}


	else if(byh < 4) {
		byh_lessthan_four(byl, byh, &lap);
		continue;
	}


	else if(byh == 9 || by == 0xd6 || by == 0xde)  {  /*SUB SBB SBI SUI*/
		sub_b_i(by, byl, byh, &lap);
		continue;
	}


	else if((by > 0xc0) && (byl == 7 || byl == 0xf) ) {
		lap = 4.0 / 3.0;
		continue;
	}

		/* JMP Jx JNx */
	else if(((by > 0xc0) && (byl == 2 || byl == 0xa)) || by == 0xc3) {
		jmp_c_n(by, &lap);
		continue;
	}

		/*CALL CX CNX*/
	else if((by > 0xbf) && (by == 0xcd || byl == 4 || byl == 0xc)) {
		call_c_n(by, &lap);
		continue;
	}

		/* RET RX RNX */
	else if((by > 0xbf) && (byl == 0 || byl == 8 || by == 0xc9) ) {
		ret_c_n(by, &lap);
		continue;
	}


	else if(by > 0xc0 && byl == 5)  {            /*PUSH*/
		lap = 12.0 / 3.0;
		m1 = (byh - 0xc) * 2;
		if(m1 == 6) {
			for(m1 = 0, ms = 0;m1 < 8;m1++)
				ms = (ms * 2) + rf[m1];
			md = reg[7];
		}
		else {
			md = reg[m1];
			ms = reg[m1 + 1];
		}
		push_word(md * 0x100 + ms);
		continue;
	}


	else if(by > 0xc0 && byl == 1)  {        /*POP*/
		lap = 10.0 / 3.0;
		m1 = pop_word();
		if(by == 0xf1) {
			reg[7] = m1 / 0x100;
			m1 %= 0x100;
			for(md = 7;md >= 0;md--) {
				rf[md] = m1 % 2;
				m1 /= 2;
			}
		}
		else {
			ms = (byh - 0xc) * 2;
			reg[ms] = m1 / 0x100;
			reg[ms + 1] = m1 % 0x100;
		}
		continue;
	}


	else if(by == 0xe3)  {      /*XTHL*/
		lap = 16.0 / 3.0;
		ms = memr(rsp);
		md = memr(rsp + 1);
		memw(rsp, reg[5]);
		memw(rsp + 1, reg[4]);
		reg[5] = ms;
		reg[4] = md;
		continue;
	}

	else if(by == 0xeb)  {      /*XCHG*/
		lap = 4.0 / 3.0;
		ms = reg[2];
		reg[2] = reg[4];
		reg[4] = ms;
		ms = reg[5];
		reg[5] = reg[3];
		reg[3] = ms;
		continue;
	}

	else if(by == 0xe9)  {      /*PCHL*/
		lap = 6.0 / 3.0;
		rpc = reg[4] * 0x100 + reg[5];
		continue;
	}

	else if(by == 0xf9)  {      /*SPHL*/
		lap = 6.0 / 3.0;
		rsp = reg[4] * 0x100 + reg[5];
		continue;
	}

	else if(by == 0xe6)  {     /*ANI*/
		lap = 7.0 / 3.0;
		rf[7] = 0;
		rf[3] = 1;
		reg[7] &= memr(++rpc);
		setrf(reg[7]);
		continue;
	}

	else if(by == 0xee)  {    /*XRI*/
		lap = 7.0 / 3.0;
		rf[7] = rf[3] = 0;
		reg[7] ^= memr(++rpc);
		setrf(reg[7]);
		continue;
	}
  }/*while close*/
 }
