Facebook Badge

Saturday, February 18, 2012

Simulate Instruction Pairing in Pentium Processor


import java.util.*;
import java.io.*;

/**********************************
 * @author Rohit E Iyer 09-221
 * @date 18/02/2012
 *
 * AMP Practical No.2
 * TITLE:Instruction Pairing
 *----------------------------------
 * Instructions
 * {"MUL","DIV","JMP","CALL","JCC"}
 * cannot proceed in V-pipeline
 * If they occur in V-pipeline they are
 * shifted to U-pipeline
 *-----------------------------------
 * PipelineExp.java
 * *******************************/


class PipelineExp
{

public static void main(String args[]) throws Exception
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int k=1;
int U[]=new int[5];
int V[]=new int[5];

System.out.println("Enter program  no of lines:");
int n=Integer.parseInt(br.readLine());;

String prog[]=new String[n+1];

System.out.println("Enter program line by line");
for(int i=1;i<=n;i++)
prog[i]=br.readLine();


//--------------STEP1:First 2 instructions are fetched-----------//
if(k<=n) //Handle the condition if there's just a single instruction
U[0]=k++;
else
U[0]=0;
if(k<=n)
V[0]=k++; //Handle the condition if there's just a single instruction
else
V[0]=0;

display(U,V,prog);

//-------------STEP2:First 2 inst. go in Decode1 stage and Instr. 3 & 4 are fetched if n>2------------//
U[1]=U[0];
V[1]=V[0];
if(k<=n)
U[0]=k++;
else
U[0]=0;
if(k<=n)
V[0]=k++;
else
V[0]=0;
display(U,V,prog);

//-----------STEP3:Shift pipeline one stage at a time after checking for Instruction Pairing-------//
int x=0;

while(x==0)

{
if(isAlright(prog,U[1],V[1])==true)
{
U[4]=U[3];
U[3]=U[2];
U[2]=U[1];
U[1]=U[0];
if(k<=n)
U[0]=k++;
else
U[0]=0;

V[4]=V[3];
V[3]=V[2];
V[2]=V[1];
V[1]=V[0];
if(k<=n)
V[0]=k++;
else
V[0]=0;

display(U,V,prog);
if(U[4]==n || V[4]==n)
x=1;
}
else
{
U[4]=U[3];
U[3]=U[2];
U[2]=U[1];
U[1]=V[1];
V[1]=0;
V[4]=V[3];
V[3]=V[2];
V[2]=V[1];
display(U,V,prog);
if(U[4]==n || V[4]==n)
x=1;  //if the last instruction is displayed in writeback stage stop
}

}
System.out.println("Program Execution Completed");
}

public static boolean isAlright(String prog[],int x,int y)
//checks if the instruction in Decode1 stage of V-pipeline can stay in V-pipeline or not
{
String class1[]={"MOV","ADD","SUB","INC","DEC","PUSH","POP","LEA","NOP"}; //really not required :P :P
String class2[]={"MUL","DIV","JMP","CALL","JCC"};

if(y!=0)
{
for(int i=0;i<class2.length;i++)
if(prog[y].equals(class2[i]))
return false;
}
return true;
}
public static void display(int U[],int V[],String prog[]) throws Exception
//U r free to change this method the way u want the pipelines to get displayed
//Display U-array and V-array however u want.
{

System.out.println("*********U-PIPELINE********: ");

if(U[0]!=0)
System.out.println("FETCH STAGE : "+prog[U[0]]);
if(U[1]!=0)
System.out.println("DECODE1 STAGE : "+prog[U[1]]);
if(U[2]!=0)
System.out.println("DECODE2 STAGE : "+prog[U[2]]);
if(U[3]!=0)
System.out.println("EXECUTE STAGE : "+prog[U[3]]);
if(U[4]!=0)
System.out.println("WRITEBACK STAGE : "+prog[U[4]]);

System.out.println("\n\n*********V-PIPELINE********: ");

if(V[0]!=0)
System.out.println("FETCH STAGE : "+prog[V[0]]);
if(V[1]!=0)
System.out.println("DECODE1 STAGE : "+prog[V[1]]);
if(V[2]!=0)
System.out.println("DECODE2 STAGE : "+prog[V[2]]);
if(V[3]!=0)
System.out.println("EXECUTE STAGE : "+prog[V[3]]);
if(V[4]!=0)
System.out.println("WRITEBACK STAGE : "+prog[V[4]]);
System.out.println("\n\n\n");
System.in.read(); //Wait for ENTER press
}


}

No comments:

Post a Comment