Monday, June 9, 2008

MICROPROCESSOR SIMULATOR

(NALAIK PANDOO UTHA LO YAHA SE PROJECT OR PRINT KARWA KER SIR KO DO, OR MERI JAN CHORO, MEI KOI ROBOT TO NHI HU, MUJHE BHI DIN MEI 24 HRS HI MILTAY HAIN.
CLASS FELLOWS CHOR KER AB JUNIORS B PECHAY PER GAYE HAIN, YAR YE ASSIGNMENT MILI HAI, YAR IS SUBJECT KA KOI PROJECT BANANA HAI.YAR YE ABEEL NISAR JO HAI NA BARA HI ZAHEEN BACHA HAI, IS KO SUB KUCH ATA HAI.
ABEEL NISAR KOI UPPER SE SEIKH KER AYA THA,YA AASMAN SE SUB KUCH UTARTA HAI MERE DAMAGH MEI, WATT LAGAO SAREY MILL KER MERI, JIS DIN SHIRAFAT KA JANAZA NIKAL GAYA NA TO MEI TUM SUB KI WATT LAGA DU GA.I REALLY MEAN IT.

ISS LIYE THORA HOLA HATH RAKHO OR KISI DOSRAY KA BHI KHAYAL KAR LIYA KARO.TUMHARA MATLAB PURA HO GA TO TUM LOGO NE SALAM KA JAWAB BHI NHI DENA.)



#include<conio.h>
#include<iostream.h>
#include<stdio.h>
class microsimulator
{
private:
int ir;float acc;
int mem[2][32];
public:
int pc;

//Constructor

microsimulator()
{
pc=1;
ir=0;
acc=15;
mem[2][32]=0;
}

//Method for user interface

void user()
{ int i;
cout<<"***--------NOTE-----------***"<<endl;
cout<<">>Memory is 32-bit, first 16-bit for addresses and their instructions"<<endl;
cout<<" remaining 16-bit for addresses and their data"<<endl<<endl;
cout<<">>Instructions should be of 3-digit that consist of an opcode and address"<<endl<<endl;
cout<<">>Opcode with their operations"<<endl;
cout<<"1.Add"<<endl;
cout<<"2.Subtract"<<endl;
cout<<"3.Multiply"<<endl;
cout<<"4.Divide"<<endl;
cout<<"5.Move"<<endl;
cout<<"6.Move X"<<endl<<endl;
for(i=0;i<16;i++)
{
cout<<"Enter address"<<endl;
cin>>mem[0][i];
cout<<"enter its 3-digit instruction"<<endl;
cin>>mem[1][i];
}



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

//Method for search

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

//Method for decoding

int dec1()
{ int b=ir/100;
return b;
}

//Method for decoding

int dec2()
{
int c=ir%100;
return c;
}

//Method for search

int find2(int c)
{ int d;
for(int h=16;h<32;h++)
{
if(c==mem[0][h])
{
d=mem[1][h];
}

}
return d;
}

//Method for arithmetic operations of ALU

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

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

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

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

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

//Method for display

void display()
{
cout<<"pc= "<<pc<<endl;
cout<<"ir= "<<ir<<endl;
cout<<"acc= "<<acc<<endl;
for(int j=0;j<32;j++)
{


cout<<"MEMORY location" <<j<<endl;
cout<<"-------"<<endl;
cout<<"ADDRESS "<<mem[0][j]<<endl;

cout<<"DATA "<<mem[1][j]<<endl;
cout<<"-------"<<endl;
}
}
}; //end of class


void main()
{ clrscr();
microsimulator s;
int a,b,d,pc;
s.user();
pc=s.pc;
for(pc=1;pc<17;pc++)
{

s.find1();
a=s.dec1();
b=s.dec2();
d=s.find2(b);
s.alu(a,d);

}
s.display();

getche();
}


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



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