	    /* SIMULATION OF 8085 USING C */
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include"malloc.h"
struct data
{
 int d,a;                /* d-DATA  a-ADDRESS */
 struct data *p,*n;      /* *p-PREVIOUS  *n-NEXT */
};
struct data *page[32];        /* reg[8]_REGISTERS   rf[8]_FLAG_REGISTER */
int reg[8],rf[8],rsp,rpc;     /* rsp_STACK_POINTER  rpc_PROGRAM_COUNTER */
int gdriver=DETECT,gmode;
int main()                        /* START OF FUNCTION "main()" */
{
 int k,s; 
 char ch;
 initgraph(&gdriver,&gmode, "");
 clrscr();
 rf[2]=0;
 rf[4]=0;
 rf[6]=0;
 rsp=0x2700;
 rpc=0x2200;
 create();
 rom_write();
 for(k=0;k<8;k++)
 reg[k]=k;
 decorate();

   while(1)
   {
    clear_help();
    gotoxy(1,1);
    if(ch!='g')
    printf("_ups  85");
    else
    printf("E         ");
    gotoxy(1,21);
    printf("SELECT A MODE");

    ch=getch();

 if(ch=='r'||ch=='R')           /*RESET*/
 continue;

 if(ch=='y'||ch=='Y')         /*EXIT*/
 {
 closegraph();
 return(0);
 }
 if(ch=='v'||ch=='V')         /*DEMO*/
 {
 demo();
 decorate();
 continue;
 }
 if(ch=='s'||ch=='S')         /*SHIFT*/
 {
 clear_help();
 gotoxy(1,21);
 printf("YOU ARE IN SHIFT MODE: ENTER\n");
 printf("LOW LIMIT ADDRESS_n_HIGH LIMIT ADDRESS_n_DESTINATION ADDRESS_x");
 block_move();
 continue;
 }
 if(ch=='w'||ch=='W')         /*EXAM.REG*/
 {
 clear_help();
 gotoxy(1,21);
 printf("YOU ARE IN EXAM.REGISTER MODE: ENTER\n");
 printf("a,b,c,d,e,f,8,9,4,5,6 OR 7 TO EXAM & OPTIONALLY VARY A,B,C,D,E,FLAG,H,L,\n");
 printf("HIGH&LOW BYTES OF STACK,HIGH&LOW BYTES OF PROGRAM COUNTER REGISTERS\n");
 printf("RESPECTIVELY.PRESS n,x TO CONTINUE WITH OR TERMINATE PRESENT MODE");
 modify_reg();
 continue;
 }
 if(ch=='i'||ch=='I')         /*INSERT*/
 {
 clear_help();
 gotoxy(1,21);
 printf("YOU ARE IN INSERT MODE: ENTER\n");
 printf("LOW LIMIT,HIGH LIMIT,LOW INSERT ADDRESSES AND NUMBER OF BYTES");
 inserts();
 continue;
 }

 if(ch=='k'||ch=='K')         /*INSERT*/
 {
 clear_help();
 gotoxy(1,21);
 printf("YOU ARE IN DELETE MODE:ENTER\n");
 printf("LOW LIMIT ADRESS_n,HIGH LIMIT ADRESS_n\n");
 printf("LOW DELETE ADDRESS_n AND HIGH DELETE ADRESS_x");
 deletes();
 clear_help();
 continue;
 }

 if(ch=='m'||ch=='M')    /* EXAM MEMORY*/
{
   gotoxy(1,1);
   printf("   .    ");
   gotoxy(1,21);
   printf("ENTER ADDRESS AND ENTER n TO EXAM DATA AT THAT ADDRESS");
   k=adr(ch);
   clear_help();
   if(k<0)
   continue;
 }
 if(ch=='t'||ch=='g'||ch=='T'||ch=='G')
 {
   gotoxy(1,1);
   printf("-us-    ");
   gotoxy(1,21);
   printf("ENTER ADDRESS AND ENTER x TO RUN .");
   k=adr(ch);
   clear_help();
   if(k<0)
   continue;
 }
 if(ch=='g'||ch=='G')         /*CALLING RUN*/
  {
  run(k,0);
  continue;
  }
  if(ch=='t'||ch=='T')         /*GOING TO TRACE*/
 {
 run(k,1);
 continue;
 }
 if(ch=='m'||ch=='M')
 {
  clear_help();
  gotoxy(1,21);
  printf("YOU ARE IN EXAM.MEMORY MODE: OPTIONALLY VARY DATA OR ENTER\n");
  printf("n OR p TO CONFIRM PRESENT DATA & CONTINUE WITH NEXT OR PREVIOUS LOCATION\n");
  printf("ENTER x TO CONFIRM PRESENT DATA & TERMINATE PRESENT MODE.ENTER z FOR NEXT\n");
  printf("SET OF CODES.ENTER r TO TERMINATE PRESENT MODE WITHOUT SAVING CHANGED DATA");
  memo(k);
  continue;
 }
 reset();
}
}                        /* END OF FUNCTION MAIN */

adr(char ch)                                   /* FUNCTION TO EDIT ADDRESS */
{
 int c1=48,c2=49,c3=50,c4=51,c5=5,c6=0;
 while(1)
{
 if(c6)
{
 gotoxy(1,1);
 printf("%c%c%c%c    ",c1,c2,c3,c4);
 c5=getch();
}
 else
{
  c6=1;
  c5=getch();
  if(c5<48||c5>102||(c5>57&&c5<97))
  {
  reset();
  return(-1);
  }
}
 if((c5>47&&c5<58)||(c5>96&&c5<103))
 {
   c1=c2;
   c2=c3;
   c3=c4;
   c4=c5;
   continue;
 }
 if((c5=='x')&&(ch=='g'||ch=='t'))
 return(((help(c1)*0x10+help(c2))*0x10+help(c3))*0x10+help(c4));
 if((c5=='n'||c5=='p')&&(ch=='m'))
 return(((help(c1)*0x10+help(c2))*0x10+help(c3))*0x10+help(c4));
 if(c5!='r')
  reset();
 gotoxy(1,1);
 printf("_ups  85");
 gotoxy(1,21);
 printf("SELECT A MODE");
 return(-1);
}
}                                       /* END OF FUNCTION "adr()" */



 black()
 {
 int k;
 for(k=1;k<=25;k++)
 {
 gotoxy(1,k);
 printf("                                                                                ");
 }
 return;
 }


block_move()
{
int ad1,ad2,ad3;
gotoxy(1,1);
printf(".       ");
ad1=adr('m'); if(ad1<0) return;
gotoxy(1,1);
printf(".       ");
ad2=adr('m'); if(ad2<0) return;
gotoxy(1,1);
printf(".       ");
ad3=adr('m'); if(ad3<0) return;
if(ad2<ad1)
{
reset();
return;
}
shift(ad1,ad2,ad3);
return;
}



 call_t_help()
 {
 clear_help();
 gotoxy(1,21);
 printf("YOU ARE IN SINGLE STEP MODE:                                                \n");
 printf("ENTER n TO CONTINUE                                                         \n");
 printf("ENTER  r OR x TO TERMINATE THE MODE                                         ");
 return;
 }


clear_help()
{
int k;
for(k=0;k<4;k++)
{
gotoxy(1,21+k);
printf("                                                                              ");
}
return(0);
}



code(int i)
{
gotoxy(1,8);
if(i==0)
{
printf("   CE-ACI     8F-ADC A   88-ADC B   89-ADC C   8A-ADC D   \n");
printf("   8B-ADC E   8C-ADC H   8D-ADC L   8E-ADC M   87-ADD A   \n");
printf("   80-ADD B   81-ADD C   82-ADD D   83-ADD E   84-ADD H   \n");
printf("   85-ADD L   86-ADD M   C6-ADI 8   A7-ANA A   A0-ANA B   \n");
printf("   A1-ANA C   A2-ANA D   A3-ANA E   A4-ANA H   A5-ANA L   \n");
printf("   A6-ANA M   E6-ANI     CD-CALL    DC-CC      FC-CM      \n");
printf("   2F-CMA     3F-CMC     BF-CMP A   B8-CMP B   B9-CMP C   \n");
printf("   BA-CMP D   BB-CMP E   BC-CMP H   BD-CMP L   BE-CMP M   \n");
printf("   D4-CNC     C4-CNZ     F4-CP      EC-CPE     FE-CPI     \n");
printf("   E4-CPO     CC-CZ      27-DAA     09-DAD B   19-DAD D   \n");
printf("   29-DAD H   39-DAD SP  3D-DCR A   05-DCR B   0D-DCR C   \n");
printf("   15-DCR D   1D-DCR E   25-DCR H   2D-DCR L   35-DCR M   \n");
}
if(i==1)
{
printf("0B-DCX B    1B-DCX D    2B-DCX H    3B-DCX SP   F3-DI     \n");
printf("FB-EI       76-HLT      DB-IN       3C-INR A    04-INR B  \n");
printf("0C-INR C    14-INR D    1C-INR E    24-INR H    2C-INR L  \n");
printf("34-INR M    03-INX B    13-INX D    23-INX H    33-INX SP \n");
printf("DA-JC       FA-JM       C3-JMP      D2-JNC      C2-JNZ    \n");
printf("F2-JP       EA-JPE      E2-JPO      CA-JZ       3A-LDA    \n");
printf("0A-LDAX B   1A-LDAX D   2A-LHLD     01-LXI B    11-LXI D  \n");
printf("21-LXI H    31-LXI SP   7F-MOV A,A  78-MOV A,B  79-MOV A,C\n");
printf("7A-MOV A,D  7B-MOV A,E  7C-MOV A,H  7D-MOV A,L  7E-MOV A,M\n");
printf("47-MOV B,A  41-MOV B,C  42-MOV B,D  43-MOV B,E  44-MOV B,H\n");
printf("45-MOV B,L  46-MOV B,M  4F-MOV C,A  48-MOV C,B  49-MOV C,C\n");
printf("4A-MOV C,D  4B-MOV C,E  4C-MOV C,H  4D-MOV C,L  4E-MOV C,M\n");
}
if(i==2)
{
printf("57-MOV D,A  50-MOV D,B  51-MOV D,C  52-MOV D,D  53-MOV D,E\n");
printf("54-MOV D,H  55-MOV D,L  56-MOV D,M  5F-MOV E,A  58-MOV E,B\n");
printf("59-MOV E,C  5A-MOV E,D  5B-MOV E,E  5C-MOV E,H  5D-MOV E,L\n");
printf("5E-MOV E,M; 67-MOV H,A  60-MOV H,B  61-MOV H,C  62-MOV H,D\n");
printf("64-MOV H,H  65-MOV H,L  66-MOV H,M  6F-MOV L,A  68-MOV L,B\n");
printf("69-MOV L,C  6A-MOV L,D  6B-MOV L,E  6C-MOV L,H  6D-MOV L,L\n");
printf("6E-MOV L,M  77-MOV M,A  70-MOV M,B  71-MOV M,C  72-MOV M,D\n");
printf("73-MOV M,E  74-MOV M,H  75-MOV M,L  3E-MVI A    06-MVI B  \n");
printf("0E-MVI C    16-MVI D    1E-MVI E    26-MVI H    2E-MVI L  \n");
printf("36-MVI M    00-NOP      B7-ORA A    B0-ORA B    B1-ORA C  \n");
printf("B2-ORA D    B3-ORA E    B4-ORA H    B5-ORA L    B6-ORA M  \n");
printf("F6-ORI      D3-OUT      E9-PCHL     C1-POP B    D1-POP D  \n");
}
if(i==3)
{
printf("E1-POP H    F1-POP PSW  C5-PUSH B   D5-PUSH D   E5-PUSH H \n");
printf("F5-PUSH PSW 17-RAL      1F-RAR      D8-RC       C9-RET    \n");
printf("07-RLC      F8-RM       D0-RNC      C0-RNZ      F0-RP     \n");
printf("E8-RPE      E0-RPO      0F-RRC      C8-RZ       9F-SBB A  \n");
printf("98-SBB B    99-SBB C    9A-SBB D    9B-SBB E    9C-SBB H  \n");
printf("9D-SBB L    9E-SBB M    DE-SBI      22-SHLD     F9-SPHL   \n");
printf("32-STA      02-STAX B   12-STAX D   37-STC      97-SUB A  \n");
printf("90-SUB B    91-SUB C    92-SUB D    93-SUB E    94-SUB H  \n");
printf("95-SUB L    96-SUB M    D6-SUI      EB-XCHG     AF-XRA A  \n");
printf("A8-XRA B    A9-XRA C    AA-XRA D    AB-XRA E    AC-XRA H  \n");
printf("AD-XRA L    AE-XRA M    EE-XRI      E3-XTHL               \n");
printf("                                                          ");
}
return;
}


create()
{
 struct data *ex,*try;
 int m=0x2000,k=0,dir=1,plea;
 try=malloc(sizeof (struct data));
 page[0]=try;
 ex=try;
 ex->a=0x2000;
 ex->d=k;
 while(ex->a!=0x2d20)
{
 try=malloc(sizeof (struct data));
 ex->n=try;
 m++;
 k++;
 if(k>0xff)   k=0;
 try->d=k;
 try->a=m;
 plea=m%0x100;
 if(plea==0)
 {
  page[dir]=try;
  dir++;
 }
 try->p=ex;
 ex=ex->n;
}
 ex->n=page[0];
 page[0]->p=ex;
return;
}

cro_use()
{
char c1,c2;
while(1)
{
clear_help();
gotoxy(1,21);
printf("PRESS y TO USE C.R.O:");
c1=getche();
printf("\nRETYPE TO CONFIRM:");
c2=getche();
if(c1==c2)
break;
}
clear_help();
if(c1!='y')
return(0);
else
return(1);
}

cro_x()
{
char c1,c2;
while(1)
{
clear_help();
gotoxy(1,21);
printf("YOU ARE SETTING SECONDS ON TIME AXIS                                          \n");
printf("PRESS 1,2,3,4,5,6,7,8,9 OR 0 TO SELECT (MICRO SECONDS) 20K,10K,5K,2K,         \n");
printf("1K,500,200,100,50 OR 20 .ANY OTHER KEY IS TAKEN AS 0.                         \n");
c1=getche();
printf(" RETYPE TO CONFIRM:");
c2=getche();
if(c1==c2)
break;
}
clear_help();
switch(c1)
{
case '1':return(20000);
case '2':return(10000);
case '3':return(5000);
case '4':return(2000);
case '5':return(1000);
case '6':return(500);
case '7':return(200);
case '8':return(100);
case '9':return(50);
case '0':return(20);
}
return(20);
}


cro_y(int k)
{
char c1,c2;
while(1)
{
clear_help();
gotoxy(1,21);
printf("YOU ARE SETTING VOLTS PER ONE UNIT ON Y_AXIS FOR PORT %d:\n",k);
printf("PRESS 1,2,3,4,5,6,7,8 OR 0 TO SELECT 2V,.5V,.2V,.1V,50mV,20mV,10mV,1mV OR 1V  \n");
printf("ANY OTHER KEY IS TAKEN AS 0.DO NOT PRESS ENTER KEY AFTER YOUR ENTRY\n");
c1=getche();
printf(" RETYPE TO CONFIRM: ");
c2=getche();
if(c1==c2)
break;
}
clear_help();
switch(c1)
{
case '1':return(2000);
case '2':return(500);
case '3':return(200);
case '4':return(100);
case '5':return(50);
case '6':return(20);
case '7':return(10);
case '8':return(1);
case '0':return(1000);
}
return(1000);
}


 decorate()
 {
 int k,s=15;
 black();
 gotoxy(1,2);  view();
 gotoxy(25,2);
 printf("  TRACE   BOX  ");
 gotoxy(1,7);  view();
 gotoxy(25,7);
 printf("   CODE  BOX   ");
 gotoxy(1,20); view();
 gotoxy(29,20);
 printf("   HELP  BOX   ");
 gotoxy(1,25); view();
 gotoxy(29,25);
 printf(" SIMULATION OF 8085 ");
 s=15;
 gotoxy(63,2);
 printf(" MESSAGE BOX ");
 gotoxy(60,7);
 for(k=0;k<19;k++)
 printf(" ");
 for(k=0;k<23;k++)
 {
 if(k<18)
 {
 gotoxy(59,k+3);
 printf("%c",s);
 }
 gotoxy(79,k+3);
 printf("%c",s);
 }
 clear_help();
 gotoxy(66,5);  printf("DONE");
 gotoxy(67,7);  printf("BY");
 gotoxy(63,9);  printf("Y.R.K.MAHESH");
 gotoxy(66,18); printf("MOTTO:");
 gotoxy(64,19); printf("NO LIMITS"); sleep(1);
 gotoxy(62,3);  printf("AVAILABLE MODES:");
 gotoxy(62,13); printf("v->DEMO");
 gotoxy(62,4);  printf("r->RESET        ");
 gotoxy(62,5);  printf("m->EXAM.MEMORY  ");
 gotoxy(62,6);  printf("g->GO           ");
 gotoxy(62,7);  printf("t->TRACE        ");
 gotoxy(62,8);  printf("s->SHIFT        ");
 gotoxy(62,9);  printf("w->EXAM.REGISTER");
 gotoxy(62,10); printf("i->INSERT       ");
 gotoxy(62,11); printf("k->DELETE       ");
 gotoxy(62,12); printf("y->EXIT         ");
 gotoxy(61,14);
 printf("MEMORY ALLOTMENT:");
 gotoxy(62,15);
 printf("RAM_0X2000_2BFF");
 gotoxy(62,16);
 printf("ROM_0X2C00_2D20");
 gotoxy(60,7);
 code(0);
 return(0);
 }


deletes()
{
int ad1,ad2,ad3,ad4;
gotoxy(1,1);
printf(".       ");
ad1=adr('m'); if(ad1<0) return;/*low limit*/
gotoxy(1,1);
printf(".       ");
ad2=adr('m'); if(ad2<0) return;/*high limit*/
gotoxy(1,1);
printf(".       ");
ad3=adr('m'); if(ad3<0) return;/*low delete*/
gotoxy(1,1);
printf(".       ");
ad4=adr('g'); if(ad4<0) return;/*high delete*/
if((ad4>ad2)||(ad2<ad1)||(ad3>ad4)||(ad3>ad2))
{
reset();
return;
}
shift(ad4+1,ad2,ad3);
return;
}


 demo()
 {
 char ch;
 while(1)
 {
 black();
 gotoxy(1,16);
 printf("#YOU ARE IN DEMO MODE:\n");
 printf("#ENTER YOUR OPTION TO OBSERVE THE OUTPUT OF DIFFERENT PROGRAMS");
 printf("\n# 1->UP RAMP");
 printf("\n# 2->DOWN RAMP");
 printf("\n# 3->TRIANGULAR");
 printf("\n# 4->SQUARE WAVE");
 printf("\n# 5->TRIANGULAR & SQUARE WAVE MIXED");
 printf("\n# y->TERMINATE DEMO MODE");
 ch=getch();
 if(ch=='y'||ch=='Y')
 return;
 if(ch>'5'||ch<'0')
 ch='1';
 switch(ch)
 {
 case '1': romw(0x2c04,0x3c); run(0x2c00,2); break;
 case '2': romw(0x2c04,0x3d); run(0x2c00,2); break;
 case '3': run(0x2c10,2); break;
 case '4': run(0x2c30,2); break;
 case '5': run(0x2c50,3);
 }
 }
 }/*close of demo*/

inserts()
{
int ad1,ad2,ad3,num;
gotoxy(1,1);
printf(".       ");
ad1=adr('m'); if(ad1<0) return;/*low limit*/
gotoxy(1,1);
printf(".       ");
ad2=adr('m'); if(ad2<0) return;    /*high limit*/
gotoxy(1,1);
printf(".       ");
ad3=adr('g'); if(ad3<0) return;   /*low insert*/
num=vary(0);                             /*no of bytes*/
if(num==0) return;
if(ad2<ad1||ad3>ad2)
{
reset();
return;
}
num&=0xff;
shift(ad3,ad2,ad3+num);
memo(ad3);
return;
}

int help(int c1)     /* FUNCTION TO CONVERT ASCII_CHARACTER TO INTEGER */
{
 if(c1<58)
  c1-=48;
 else
  c1-=87;
 return(c1);
}




memo(int k9)
{
int c1,c2,c3,c4=1,i=1;
while(1)
{
gotoxy(1,1);
if(c4)
{
c3=check(k9);
if(c3==0)
{
reset();
return(1);
}
c3=memr(k9);
c1=c3/0x10;
c2=c3%0x10;
if(c1>9) c1+=87; else c1+=48;
if(c2>9) c2+=87; else c2+=48;
c4=0;
}
printf("%0x  %c%c",k9,c1,c2);
c3=getch();
if(c3==122)
{code(i);i++;i=i%4;continue;}
if((c3>47&&c3<58)||(c3>96&&c3<103))
{
c1=c2;
c2=c3;
continue;
}
if(c3=='p'||c3=='n'||c3=='x')
{
c4=(help(c1))*0x10+help(c2);
memw(k9,c4);
if(c3=='n') k9++;if(c3=='p') k9--; c4=1;if(c3=='x') return(1);
continue;
}
if(c3!='r')
reset();
gotoxy(1,1);
printf("_ups  85");
return(1);
}
}



memr(int k4)                    /* FUNCTION TO READ DATA FROM MEMORY */
{
 int s;struct data *read;
 if(k4<0x2000||k4>0x2d20)
 return(0);
 s=k4/0x100-0x20;
 read=page[s];
 while(read->a!=k4)
 {
 read=read->n;
 }
 return(read->d);
}                              /* END OF FUNCTION "memr()" */



memw(int k6,int s)   /* FUNCTION TO WRITE DATA AT A GIVEN ADDRESS */
{                   /* k=ADDRESS s=DATA */
 int w1;
 struct data *wr;
 if(k6<0x2000||k6>=0x2c00)  return;
 w1=k6/0x100-0x20;
 wr=page[w1];
 while(wr->a!=k6)
 {
 wr=wr->n;
 }
 wr->d=s;
return;
}                   /* END OF FUNCTION "memw(k,s)" */



mhlr()      /* FUNCTION TO READ DATA AT ADDRESS POINTED BY "hl" PAIR */
{
 int hl1;
 hl1=reg[4]*0x100+reg[5];
 hl1=memr(hl1);
 return(hl1);
}          /* END OF FUNCTION "mhlr()" */


mhlw(int k5)  /* FUNCTION TO WRITE DATA AT ADDRESS POINTED BY "hl" PAIR */
{            /* 'k' REPRESENTS DATA */
 int hl1;
 hl1=reg[4]*0x100+reg[5];
 memw(hl1,k5);
return;
}           /* END OF FUNCTION "mhlw(k)" */



modify_reg()
{
int c4,c1,c2,c3;
gotoxy(1,1);
printf(".              ");
c4=getch();
if((c4<'4'||c4>'f')||(c4<'a'&&c4>'9'))
{
gotoxy(1,1);
if(c4!='r')
{
reset();
return;
}
gotoxy(1,1);
printf("_ups  85");
return;
}
switch(c4)
{
case'a':gotoxy(1,1);
	printf("A     %02x",reg[7]);
	c1=vary(reg[7]);
	if(c1==0) return;
	c2=c1&0xff;
	reg[7]=c2;
	c2=c1&0x300;
	if(c2==0x200) return;
case'b':gotoxy(1,1);
	printf("B     %02x",reg[0]);
	c1=vary(reg[0]);
	if(c1==0) return;
	c2=c1&0xff;
	reg[0]=c2;
	c2=c1&0x300;
	if(c2==0x200) return;
case'c':gotoxy(1,1);
	printf("C     %02x",reg[1]);
	c1=vary(reg[1]);
	if(c1==0) return;
	c2=c1&0xff;
	reg[1]=c2;
	c2=c1&0x300;
	if(c2==0x200) return;
case'd':gotoxy(1,1);
	printf("D     %02x",reg[2]);
	c1=vary(reg[2]);
	if(c1==0) return;
	c2=c1&0xff;
	reg[2]=c2;
	c2=c1&0x300;
	if(c2==0x200) return;
case'e':gotoxy(1,1);
	printf("E     %02x",reg[3]);
	c1=vary(reg[3]);
	if(c1==0) return;
	c2=c1&0xff;
	reg[3]=c2;
	c2=c1&0x300;
	if(c2==0x200) return;
case'f':gotoxy(1,1);
	c3=128;
	c2=0;
	for(c1=0;c1<8;c1++)
	{
	c2=c2+rf[c1]*c3;
	c3/=2;
	}
	printf("F     %02x",c2);
	c1=vary(c2);
	if(c1==0) return;
	c2=c1&0xff;
	for(c3=7;c3>=0;c3--)
	{
	rf[c3]=c2%2;
	c2/=2;
	}
	c2=c1&0x300;
	if(c2==0x200) return;
case'8':gotoxy(1,1);
	printf("H     %02x",reg[4]);
	c1=vary(reg[4]);
	if(c1==0) return;
	c2=c1&0xff;
	reg[4]=c2;
	c2=c1&0x300;
	if(c2==0x200) return;
case'9':gotoxy(1,1);
	printf("L     %02x",reg[5]);
	c1=vary(reg[5]);
	if(c1==0) return;
	c2=c1&0xff;
	reg[5]=c2;
	c2=c1&0x300;
	if(c2==0x200) return;
case'4':gotoxy(1,1);
	c1=rsp/0x100;
	printf("SPH   %02X",c1);
	c1=vary(c1);
	if(c1==0) return;
	c2=c1&0xff;
	rsp&=0xff;
	rsp|=(c2*0x100);
	c2=c1&0x300;
	if(c2==0x200) return;
case'5':gotoxy(1,1);
	c1=rsp%0x100;
	printf("SPL   %02x",c1);
	c1=vary(c1);
	if(c1==0) return;
	c2=c1&0xff;
	rsp&=0xff00;
	rsp|=c2;
	c2=c1&0x300;
	if(c2==0x200) return;
case'6':gotoxy(1,1);
	c1=rpc/0x100;
	printf("PCH   %02x",c1);
	return;
case'7':gotoxy(1,1);
	c1=rpc%0x100;
	printf("PCL   %02x",c1); return;

}
return;
}



push_word(word) /* FUNCTION TO PUSH A GIVEN WORD ON_TO THE STACK */
{
 int ms,md;
 ms=word/0x100;
 md=word%0x100;
 memw(rsp-1,ms);
 memw(rsp-2,md);
 rsp-=2;
return;
}                 /* END OF FUNCTION "push_word(word)" */

pop_word()   /* FUNCTION TO RETURN A WORD FROM THE TOP OF THE STACK */
{
 int ms,md,m1;
 ms=memr(rsp);rsp++; m1=rsp;
 md=memr(m1);
 rsp++;
 ms=md*0x100+ms;
 return(ms);
}

		/* END OF FUNCTION "pop_word()" */
int check(int k8)
{
if(k8>=0x2d00||k8<0x2000)
return(0);
else
return(1);
}



reset()
{
char ch='j';
clear_help();
gotoxy(1,21);
printf("PRESS r TO RESET");
gotoxy(1,1);
printf("err     ");
while(ch!='r')
ch=getch();
clear_help();
gotoxy(1,21);
printf("SELECT A MODE");
gotoxy(1,1);
printf("_ups  85                                  ");
return;
}



romw(int k6,int s)
{
int w1;
struct data *wr;
w1=k6/0x100-0x20;
wr=page[w1];
while(wr->a!=k6)
{
wr=wr->n;
}
wr->d=s;
return;
}


 rom_write()
 {
 romw(0x2c00,0x3e);  /*ramp*/
 romw(0x2c01,0x00);
 romw(0x2c02,0xd3);
 romw(0x2c03,0x01);
 romw(0x2c04,0x3c);
 romw(0x2c05,0xc3);
 romw(0x2c06,0x02);
 romw(0x2c07,0x2c);
 romw(0x2c10,0x3e);    /*triangular*/
 romw(0x2c11,0x00);
 romw(0x2c12,0xd3);
 romw(0x2c13,0x01);
 romw(0x2c14,0x3c);
 romw(0x2c15,0xc2);
 romw(0x2c16,0x12);
 romw(0x2c17,0x2c);
 romw(0x2c18,0x3d);
 romw(0x2c19,0xd3);
 romw(0x2c1a,0x01);
 romw(0x2c1b,0xc2);
 romw(0x2c1c,0x18);
 romw(0x2c1d,0x2c);
 romw(0x2c1e,0xc3);
 romw(0x2c1f,0x12);
 romw(0x2c20,0x2c);
 romw(0x2c30,0x3e);      /*square*/
 romw(0x2c31,0xff);
 romw(0x2c32,0x06);
 romw(0x2c33,0xff);
 romw(0x2c34,0xd3);
 romw(0x2c35,0x01);
 romw(0x2c36,0x05);
 romw(0x2c37,0xc2);
 romw(0x2c38,0x36);
 romw(0x2c39,0x2c);
 romw(0x2c3a,0x2f);
 romw(0x2c3b,0xc3);
 romw(0x2c3c,0x34);
 romw(0x2c3d,0x2c);
 romw(0x2c50,0x3e);        /*mix*/
 romw(0x2c51,0xa0);
 romw(0x2c52,0xd3);
 romw(0x2c53,0x01);
 romw(0x2c54,0x3e);
 romw(0x2c55,0x00);
 romw(0x2c56,0xd3);
 romw(0x2c57,0x02);
 romw(0x2c58,0x3c);
 romw(0x2c59,0xc2);
 romw(0x2c5a,0x56);
 romw(0x2c5b,0x2c);
 romw(0x2c5c,0xd3);
 romw(0x2c5d,0x01);
 romw(0x2c5e,0x3d);
 romw(0x2c5f,0xd3);
 romw(0x2c60,0x02);
 romw(0x2c61,0xc2);
 romw(0x2c62,0x5e);
 romw(0x2c63,0x2c);
 romw(0x2c64,0xc3);
 romw(0x2c65,0x50);
 romw(0x2c66,0x2c);
 return;
 }/*close of rom_write*/



run(int k1,int k2)
{
 int by,byh,byl,ms,md,m1,m2,m3,m4,m7;
 int xref=10,yref=300,xlim=320,ylim=160;
 int i,y[21],port,y1=0,y2,y3=0,pold=xref,change;
 int col=3;
 float amp[21],set=0.0,samp[21],stime,lap=0;
 int time_scale;
 long int m5,m6;
 char tur='y',s,yt;
 rpc=k1-1;
 clear_help();
 gotoxy(1,21);
 if(k2==1||k2==0)
 y1=cro_use();
 clrscr();
 if(y1&&k2<2)
 {
 clear_help();
 gotoxy(1,21);
 printf("ENTER NUMBER OF PORTS TO BE USED.MAX(20): ");
 scanf("%d",&y1);
 if(y1>20||y1<1)
 y1=1;
 for(y2=1;y2<=y1;y2++)
 samp[y2]=cro_y(y2);          /*in milli volts*/
 stime=cro_x();
 time_scale=stime;
 stime=16.0/stime;
 }
 if(k2==2)
 {
 y1=1;
 samp[1]=1000;
 time_scale=500;
 stime=.032;
 }
 if(k2==3)
 {
 y1=2;
 samp[1]=2000;
 samp[2]=1000;
 stime=.032;
 time_scale=500;
 }

 if(k2==1)
 call_t_help();
 while(1)
{
 rpc++;
 if(k2==1)
 {
 gotoxy(1,3);
 printf("B  C  D  E  H  L  M  A  RPC->code  RSP ->DATA \n");
 for(m7=0;m7<8;m7++)
 printf("%02x ",reg[m7]);
 printf("%04x %02x    %04x  %02x %02x\n",rpc,memr(rpc),rsp,memr(rsp+1),memr(rsp));
 printf("S Z X A X P X C \n");
 for(m7=0;m7<8;m7++)
 printf("%d ",rf[m7]);
 tur=getch();
 if(tur=='r'||tur=='x')
 {
 decorate();
 return(1);
 }
 if(tur!='n')
 {
 clrscr();
 decorate();
 reset();
 return(1);
 }
 }
 if(y3)
 {

 for(y2=1;y2<=y1;y2++)
 y[y2]=yref-(int)amp[y2];
 set+=lap*stime;
 col++;
 if(col==7) col=3;
 if(set<1)
 {
 for(y2=1;y2<=y1;y2++)
 putpixel(pold,y[y2],col);
 }
 while(set>=1)
 {
 pold++;
 if(pold==xref+xlim)
 {
 gotoxy(5,23);
 printf("PRESS y TO TERMINATE AND c TO CHANGE THE AMPLITUDE SCALE OF C.R.O:");
 gotoxy(5,25);
 printf("TIME SCALE:1 UNIT: %d micro Seconds",time_scale);
 yt=getch();
 if(yt=='c')
 {
 gotoxy(5,24);
 printf("ENTER THE PORT NUMBER AND PRESS ENTER KEY:");
 scanf("%d",&change);
 samp[change]=cro_y(change);
 gotoxy(5,24);
 printf("ENTER c TO CHANGE TIME SCALE:");
 yt=getch();
 if(yt=='c')
 {
 stime=cro_x();
 time_scale=stime;
 gotoxy(5,25);
 printf("TIME SCALE:1 UNIT: %d micro Seconds",time_scale);
 stime=16.0/stime;
 }
 }
 if(yt=='y')
 {
 if(k2==2||k2==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<=y1;y2++)
 putpixel(pold,y[y2],col);
 set-=1;
 }
 }


 by=memr(rpc);
 byh=by/0x10;
 byl=by%0x10;
 if(by==0xd3)      /*OUT*/
 {
 port=memr(++rpc);
 if(y3==0&&port<21&&port>0&&y1)
 {
 clrscr();
 if(k2==1)
 call_t_help();
 for(i=xref;i<=xref+xlim;i++)
 {
 putpixel(i,yref+1,3);
 if(i<=ylim)
 putpixel(xref,yref-i+xref,3);

 if(!((i-xref)%0x10))
 {
 for(y2=yref+1;y2<yref+10;y2++)
  {
  if(i<160)
  putpixel(xref-y2+yref,yref-i+xref,3);
  putpixel(i,y2,3);
  }
 }
 }
 y3=1;
 }
 lap=10.0/3.0;
 if(y3&&port<=y1&&port>0)
 {
 amp[port]=(float)reg[7]*312.5/(float)samp[port];//16 pixels per unit
 if(amp[port]>ylim)
 amp[port]=0;
 continue;
 }
 }


 if(by==0x76||by==0xef)   /*HLT*/
 {
   decorate();
   gotoxy(1,21);
   printf("SELECT A MODE");
   gotoxy(1,1);
   printf("E       ");
   return(1);
 }


 if(by>0x3f&&by<0x80)            /*MOV*/
{
 lap=4.0/3.0;
 md=by/0x10;
 md=2*md-8;
 ms=by&0xf;
 if(ms>7)
 {
  ms-=8;
  md++;
 }
 if(ms==6)     reg[ms]=mhlr();
 if(md==6)     mhlw(reg[ms]);
 reg[md]=reg[ms];
 if(md==6||ms==6)
 lap=7.0/3.0;
 continue;
}


if(byh==8||by==0xce||by==0xc6)          /*ADD ADC ADI ACI*/
{
 m1=0;
 if(byl==6||byl==0xe)
 lap=7.0/3.0;
 else
 lap=4.0/3.0;
 if(byh!=0xc)
 {
  ms=byl;
  if(ms==6||ms==0xe)    reg[6]=mhlr();
  if(ms>7)
  {
   m1=rf[7];
   ms-=8;
  }
  m2=reg[ms];
 }
 else
 {
  lap=7.0/3.0;
  if(by==0xce)
  m1=rf[7];
  m2=memr(++rpc);
 }
 m3=m2&0xf;
 m4=reg[7]&0xf;
 m3=m3+m4+m1;
 if(m3>15) rf[3]=1;
 else rf[3]=0;
 reg[7]+=m2+m1;
 if(reg[7]>0xff) rf[7]=1;
 else rf[7]=0;
 reg[7]&=0xff;
 setrf(reg[7]);
 continue;
}

 if(byh==0xa)          /*ANA XRA*/
{
 if(byl==6||byl==0xe)
 lap=7.0/3.0;
 else
 lap=4.0/3.0;
 rf[7]=0;
 rf[3]=0;
 ms=byl;
 if(ms==6||ms==0xe)  reg[6]=mhlr();
 if(ms<8)
 {
   reg[7]&=reg[ms];
   rf[3]++;
 }
 else reg[7]^=reg[ms-8];
 setrf(reg[7]);
 continue;
}


 if(byh==0xb||by==0xf6||by==0xfe)      /*ORA CMP*/
{
 ms=9;
 if(byl==6||byl==0xe)
 lap=7.0/3.0;
 else
 lap=4.0/3.0;
 if(byh!=15)
 {
  ms=byl;
  if(ms==6||ms==0xe) reg[6]=mhlr();
  if(ms<8) m1=reg[ms];
  else m1=reg[ms-8];
 }
 else  m1=memr(++rpc);
 if(ms<8||by==0xf6)
 {
   reg[7]|=m1;
   rf[7]=0;
   rf[3]=0;
   setrf(reg[7]);
 }
 else
 {
  if(reg[7]<m1)   rf[7]=1;
  else   rf[7]=0;
  m1=m1|0xff00;
  m1=~m1;
  m1++;
  m2=m1&0xf;
  m3=reg[7]&0xf;
  if(m2+m3>15)   rf[3]=1;
  else   rf[3]=0;
  m1+=reg[7];
  m1=m1&0xff;
  setrf(m1);
 }
 continue;
}


 if(byh<4)
{
md=byh*2;
ms=byl;
switch(ms)
{
case 6:  reg[md]=memr(++rpc); lap=7.0/3.0;
	 if(md==6) {lap=10.0/3.0; mhlw(reg[6]); }  break; /*MVI*/
case 0xe:md++; lap=7.0/3.0;
	 reg[md]=memr(++rpc);       break;
case 4:  if(md==6) {lap=10.0/3.0; reg[6]=mhlr();}        /*INR*/
	 else
	 lap=4.0/3.0;
	 m1=reg[md]&0xf;
	 if(m1==0xf)	  rf[3]=1;
	 else	  rf[3]=0;
	 reg[md]=(reg[md]+1)&0xff;
	 if(md==6)  mhlw(reg[md]);
	 setrf(reg[md]);            break;
case 0xc:lap=4.0/3.0;
	 md++;
	 m1=reg[md]&0xf;
	 if(m1==0xf)	  rf[3]=1;
	 else	  rf[3]=0;
	 reg[md]=(reg[md]+1)&0xff;
	 setrf(reg[md]);            break;
case 3:  lap=6.0/3.0;                             /*INX*/
	 ms=(reg[md]*0x100)+reg[md+1];
	 if(md==6)   ms=rsp;
	 m1=ms/0x100;
	 m2=ms%0x100;
	 if(m1==0xff&&m2==0xff)  ms=0;
	 else   ms++;
	 if(md==6) rsp=ms;
	 else
	 {
	   reg[md]=ms/0x100;
	   reg[md+1]=ms%0x100;
	 }                          break;
case 0xb:lap=6.0/3.0;                                 /*DCX*/
	 ms=(reg[md]*0x100)+reg[md+1];
	 if(md==6)  ms=rsp;
	 if(ms==0)  ms=0xffff;
	 else       ms--;
	 if(md==6)  rsp=ms;
	 else
	 {
	   reg[md]=ms/0x100;
	   reg[md+1]=ms%0x100;
	 }                          break;
case 5:  if(md==6) {lap=10.0/3.0; reg[6]=mhlr();}     /*DCR*/
	 else
	 lap=4.0/3.0;
	 reg[md]=reg[md]+0xff;
	 reg[md]&=0xff;
	 if(md==6)  mhlw(reg[6]);
	 setrf(reg[md]);            break;
case 0xd:lap=4.0/3.0;
	 md++;
	 reg[md]=(reg[md]+0xff);
	 reg[md]&=0xff;
	 setrf(reg[md]);            break;
case 8:  lap=4.0/3.0;               break;
case 1:  lap=10.0/3.0;                                /*LXI*/
	 ms=memr(++rpc);
	 m1=memr(++rpc);
	 if(md==6)   rsp=(m1*0x100)+ms;
	 else
	 {
	   reg[md]=m1;
	   reg[md+1]=ms;
	 }                          break;
case 9:  lap=10.0/3.0;                                /*DAD*/
	 if(md==6)	   m1=rsp;
	 else	   m1=(reg[md]*0x100)+reg[md+1];
	 m5=m1+(reg[4]*0x100)+reg[5];
	 m6=m5&0x10000;
	 if(m6)
	 {
	  rf[7]=1;
	  m5=m5&0xffff;
	 }
	 else rf[7]=0;
	 reg[4]=m5/0x100;
	 reg[5]=m5%0x100;           break;
case 2:  switch(md)
	 {
	 case 0:lap=7.0/3.0;                                 /*STAX B,D*/
		m1=(reg[md]*0x100)+reg[md+1];
		memw(m1,reg[7]);               break;
	 case 2:lap=7.0/3.0;
		m1=(reg[md]*0x100)+reg[md+1];
		memw(m1,reg[7]);               break;
	 case 4:lap=16.0/3.0;                         /*SHLD*/
		m1=memr(++rpc);
		ms=memr(++rpc);
		m1=(ms*0x100)+m1;
		memw(m1,reg[5]);
		memw((m1+1),reg[4]);           break;
	 case 6:lap=13.0/3.0;                         /*STA*/
		m1=memr(++rpc);
		ms=memr(++rpc);
		m1+=ms*0x100;
		memw(m1,reg[7]);
	 }
	 break;
case 0xa:lap=7.0/3.0;
	 switch(md)
	 {
	 case 0:m1=(reg[0]*0x100)+reg[1];             /*LDAX*/
		reg[7]=memr(m1);               break;
	 case 2:m1=(reg[2]*0x100)+reg[3];
		reg[7]=memr(m1);               break;
	 case 4:lap=16.0/3.0;                         /*LHLD*/
		m1=memr(++rpc);
		ms=memr(++rpc);
		m1+=ms*0x100;
		reg[5]=memr(m1);
		reg[4]=memr((m1+1));           break;
	 case 6:lap=13.0/3.0;                         /*LDA*/
		m1=memr(++rpc);
		ms=memr(++rpc);
		m1+=ms*0x100;
		reg[7]=memr(m1);
	 }
	 break;
case 0xf:lap=4.0/3.0;
	 switch(md)
	 {
	 case 0:m1=reg[7]&1;                          /*RRC*/
		if(m1)  rf[7]=1;
		else    rf[7]=0;
		reg[7]/=2;
		if(rf[7])  reg[7]=reg[7]|0x80; break;
	 case 2:m1=0;                                 /*RAR*/
		m2=reg[7]&1;
		if(m2)  m1=1;
		reg[7]/=2;
		if(rf[7])   reg[7]|=0x80;
		rf[7]=m1;                      break;
	 case 4:reg[7]|=0xff00;                       /*CMA*/
		reg[7]=~reg[7];
		break;
	 case 6:if(rf[7])   rf[7]=0;                  /*CMC*/
		else        rf[7]=1;
	 }
	 break;
case 7:  lap=4.0/3.0;
	 switch(md)
	 {
	 case 0:m1=reg[7];                            /*RLC*/
		m2=m1&0x80;
		if(m2)  rf[7]=1;
		else    rf[7]=0;
		m1*=2;
		m1=m1&0xff;
		m1|=rf[7];
		reg[7]=m1;                     break;
	 case 2:reg[7]*=2;                            /*RAL*/
		reg[7]|=rf[7];
		m2=reg[7]&0x100;
		if(m2)  rf[7]=1;
		else    rf[7]=0;
		reg[7]&=0xff;                  break;
	 case 4:m1=reg[7];                            /*DAA*/
		md=m1&0xf;
		m2=m1&0xf;
		if(m2>9||rf[3])
		{
		  m1+=6;
		  if((md+6)>0xf)   rf[3]=1;
		  else   rf[3]=0;
		}
		ms=m1&0xf0;
		if((ms>0x90)||rf[7])   m1+=0x60;
		if(m1>0xff)
		{
		  rf[7]=1;
		  reg[7]=m1&0xff;
		}
		else
		{
		reg[7]=m1;
		rf[7]=0;
		}
		m1=reg[7];
		setrf(m1);                     break;
	 case 6:rf[7]=1;                              /*STC*/
		}
}  continue;
}


if(byh==9||by==0xd6||by==0xde)                        /*SUB SBB SBI SUI*/
{
 if(byl==6||byl==0xe)
 lap=7.0/3.0;
 else
 lap=4.0/3.0;
 m1=0;
 if(byh!=0xd)
 {
   ms=byl;
   if(ms>7)
   {
     ms-=8;
     if(rf[7]) m1=0xff;
   }
   if(ms==6)   reg[6]=mhlr();
 }
 else
 {
   reg[6]=memr(++rpc);
   ms=6;
   if(by==0xde)   m1=rf[7]*0xff;
 }
 md=reg[ms]|0xff00;
 md=~md;
 md++;
 md&=0xff;
 m2=reg[7]&0xf;
 m3=m1&0xf;
 m4=md&0xf;
 m4=m2+m3+m4;
 if(m4>0xf)  rf[3]=1;
 else   rf[3]=0;
 reg[7]=reg[7]+m1+md;
 if(reg[7]>0xff)  rf[7]=1;
 else    rf[7]=0;
 reg[7]&=0xff;
 setrf(reg[7]);
continue;
}


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


if(((by>0xc0)&&(byl==2||byl==0xa))||by==0xc3)         /*JMP Jx JNx*/
{
  lap=7.0/3.0;
  if(by!=0xc3)   m1=trans(by);
  else   m1=1;
  if(m1)
  { lap=10.0/3.0;
    ms=memr(++rpc);
    md=memr(++rpc);
    rpc=(md*0x100)+ms-1;
  }
  else rpc+=2;
continue;
}


if((by>0xbf)&&(by==0xcd||byl==4||byl==0xc))           /*CALL CX CNX*/
{
  lap=9.0/3.0;
  if(by!=0xcd)  m1=trans(by);
  else  m1=1;
  if(m1)
  {
    lap=18.0/3.0;
    push_word(rpc+3);
    ms=memr(++rpc);
    md=memr(++rpc);
    rpc=(md*0x100)+ms-1;
  }
  else rpc+=2;
continue;
}


if((by>0xbf)&&byl==0||byl==8||by==0xc9)               /*RET RX RNX*/
{
  lap=6.0/3.0;
  if(by!=0xc9)
  {
  m1=trans(by);
  if(m1)  lap=12.0/3.0;
  }
  else
  {
  lap=10.0/3.0;
  m1=1;
  }
  if(m1)
  rpc=pop_word()-1;
continue;
}


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;
}


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;
}


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;
}

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;
}

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

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

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

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



shift(int ad1,int ad2,int ad3)
{
int m1,m2;
if(ad3>=ad2||ad3<=ad1)
{
while(ad1<=ad2)
{
m1=memr(ad1);
memw(ad3,m1);
ad1++;
ad3++;
}
}
else
{
m2=ad2-ad1+ad3;
while(ad2>=ad1)
{
m1=memr(ad2);
memw(m2,m1);
m2--;
ad2--;
}
}
return;
}


vary(int val)
{
int c1,c2,c3,c4;
c1=val/0x10;
c2=val%0x10;
if(c1>9) c1+=87;
else c1+=48;
if(c2>9) c2+=87;
else c2+=48;
while(1)
{
c3=getch();
if((c3>='0'&&c3<='9')||(c3>='a'&&c3<='f'))
{
c1=c2;
c2=c3;
gotoxy(7,1);
printf("%c%c",c1,c2);
continue;
}
c4=help(c1)*0x10+help(c2);
if(c3=='n')
{
c4=c4|0x100;
return(c4);
}
if(c3=='x')
{
c4|=0x200;
gotoxy(1,1);
printf("_              ");
return(c4);
}
reset();
return(0);
}
}



setrf(k7)        /* FUNCTION TO UPDATE SIGN_ZERO_PARITY FLAGS */
{
 int count=0;
 if(k7==0)   rf[1]=1;
 else   rf[1]=0;
 if(k7>0x7f)  rf[0]=1;
 else  rf[0]=0;
 k7&=0xff;
 while(k7)
{
 if(k7%2)
 count++;
 k7/=2;
}
 if(count%2)
  rf[5]=0;
 else
  rf[5]=1;
return;
}                       /* END OF FUNCTION "setrf(k)" */


trans(by)
{
  int m2,md,ms=0,m1=0;
  md=by/0x10;
  m2=by%0x10;
  if(m2==0xa||m2==0xc||m2==8)   ms++;
  switch(ms)
  {
   case 0: switch(md)
	  {
	   case 12:if(!rf[1]) m1++;break;
	   case 13:if(!rf[7]) m1++;break;
	   case 14:if(!rf[5]) m1++;break;
	   case 15:if(!rf[0]) m1++;
	  }     break;
   case 1:switch(md)
	  {
	   case 12:if(rf[1]) m1++;break;
	   case 13:if(rf[7]) m1++;break;
	   case 14:if(rf[5]) m1++;break;
	   case 15:if(rf[0]) m1++;
	  }
  }
  return(m1);
}                /* END OF FUNCTION "trans(by)" */


view()
{
int y,z=15;
for(y=0;y<=78;y++)
printf("%c",z);
return;
}
