Sunday, June 8, 2008

Mini Project: Simulation of Microprocessor

Mini Project:
Simulation of Microprocessor using Object Oriented Programming in C++



I have recently done this mini project.
Here`s the code
------------------------------



#include<conio.h>
#include<iostream.h>
#include<stdio.h>
class micro
{
private:
int ir;float acc;
int mem[2][6];
public:
   int pc;
    micro()
{
pc=1;
ir=0;
acc=15;
mem[2][6]=0;
}
void fillmem()
{ int i;
for(i=0;i<3;i++)
{
cout<<"enter address"<<endl;
cin>>mem[0][i];
cout<<"enter its 3-digit instruction"<<endl;
cin>>mem[1][i];
}



for(int j=3;j<6;j++)
{
cout<<"enter address"<<endl;
cin>>mem[0][j];
cout<<"enter its corresponding data"<<endl;
cin>>mem[1][j];
}
}

void search1()
{
for(int h=0;h<3;h++)
{
if(pc==mem[0][h])
{
ir=mem[1][h];
}
}
}

int decoder1()
{ int b=ir/100;
return b;
}
int decoder2()
{
int c=ir%100;
return c;
}


int search2(int c)
{ int d;
for(int h=3;h<6;h++)
{
if(c==mem[0][h])
{
d=mem[1][h];
}
}
return d;
}

void alu(int op,int m)
{
if(op==1)
{ acc=acc+m;
pc++;
}

else if(op==2)
{ acc=acc-m;
pc++;
}

else if(op==3)
{ acc=acc*m;
pc++;
}

else if(op==4)
{ acc=acc/m;
pc++;
}

else if(op==5)
{ m=acc;
pc++;
}
else if(op==6)
{ acc=m;
}
else
cout<<"Invalid opcode"<<endl;
}






void display()
{
cout<<"pc= "<<pc<<endl;
cout<<"ir= "<<ir<<endl;
cout<<"acc= "<<acc<<endl;
for(int j=0;j<6;j++)
{
cout<<"MEMORY location" <<j<<endl;
cout<<"-------"<<endl;
cout<<"ADDRESS "<<mem[0][j]<<endl;

cout<<"DATA "<<mem[1][j]<<endl;
cout<<"-------"<<endl;
}
}
};
void main()
{ clrscr();
micro z;
int d1,d2,data,pc;
z.fillmem();
pc=z.pc;
for(pc=1;pc<4;pc++)
{

z.search1();
d1=z.decoder1();
d2=z.decoder2();
data=z.search2(d2);
z.alu(d1,data);

}
z.display();

getche();
}



-----------------------------

No comments:

Post a Comment

/* google analytics ------------------------------------------------------------- */