Solved

A program to simulate the instruction execution in a datapath

Posted on 2007-11-25
3
371 Views
Last Modified: 2010-04-21
A program to simulate the instruction execution in a datapath (the test file will contain the instructions lw, sw, add, beq, bne) I wrote the code below but I was wondering is there a easlier way to write this code (I think I have too many "int" and the instruction can be better coded) any input will be helpful or ex:
#include <iostream>

#include <fstream>

#include <string>

#include <cstring>

#include <cmath>

using namespace std;
 

void IF_reg(int &cycle, int &pc, string &bin);

void ID_reg(int &pc, int &r_d1, int &r_d2,int &sign_ext, int &d_reg);

void EX_reg(int &n_pc, int z, int &ALU_r, int &r_d2, int &d_reg);

void MEM_reg(int &read_d, int &ALU_r, int &d_reg);
 

int Convert2Dec(string binaryStr);

int main()

{

	int cyc = 0;

	int pc = 0;

	int rd1 = 0;

	int rd2 = 0;

	int sgex = 0;

	int drg = 0;

	int x = 0;

	int npc = 0;

	int zero = 0;

	int alu = 0;

	int read = 0;

	int alu_r = 0;

	//int z_o = 0;

	int alr = 0;

	int r_d2= 0; 

	int d_r = 0;

	int pc_n = 0;

	int rd_1 = 0;

	int rd_2 = 0;

	int sg_ex = 0;

	int destrg = 0;

	int rd_s1 = 0;

	int rd_s2 = 0;

	int sg_ex2 = 0;

	int destrg2= 0;

	int pc_nx = 0;

	int xzero = 0;

	int xalr = 0;

	int xr_d2 = 0;

	int xd_r = 0;

	int rd_sy1 = 0;

	int rd_sy2 = 0;

	int sgexy = 0;

	int destrgy = 0;
 

	string binary;

	string numbers;

	string digit;

	string sub;

	string beq;

	string bne;

	ifstream inputdata;

	inputdata.open("test.txt");
 
 

while(x < 1)

{

	

	getline(inputdata,binary);

	x++;

	getline(inputdata,numbers);

	x++;

	getline(inputdata,digit);

	x++;

	getline(inputdata,sub);

	x++;

	getline(inputdata,beq);

	x++;

	getline(inputdata,bne);

	x++;

	

	cout<<"Press enter for next cycle.";

	if(cin.ignore())

	{

		cyc = cyc + 1;

		pc = pc + 4;

		

		IF_reg(cyc,pc,binary);//First introduction from text file is being fetched in cycle 1.

		cout<<endl;

	}
 

	cout<<"Press enter for next cycle.";

	if(cin.ignore())

	{

		cyc = cyc + 1;

		pc = pc + 4;

		IF_reg(cyc,pc,numbers);//Second introduction from text file is being fetched in cycle 2.

		pc = pc + 4;

		rd1 = Convert2Dec(binary.substr(6,5))+ 20;

		rd2 = Convert2Dec(binary.substr(11,5))+ 20;

		sgex = Convert2Dec(binary.substr(16,16));

		drg = Convert2Dec(binary.substr(11,5));

		ID_reg(pc,rd1,rd2,sgex,drg);//Instruction in cycle 1 is being decoded.

		cout<<endl;

	}

	cout<<"Press enter for next cycle.";

	if(cin.ignore())

	{

		cyc = cyc + 1;

		pc = pc + 4;

		IF_reg(cyc,pc,digit);//Third introduction from text file is being fetched in cycle 3.

		pc = pc + 4;

		rd1 = Convert2Dec(numbers.substr(6,5))+ 20;

		rd2 = Convert2Dec(numbers.substr(11,5))+ 20;

		sgex = Convert2Dec(numbers.substr(16,16));

		drg = Convert2Dec(numbers.substr(11,5));

		ID_reg(pc,rd1,rd2,sgex,drg);//Instruction in cycle 2 is being decoded.
 

		npc = Convert2Dec(binary.substr(16,16))*4+4;

		alu = Convert2Dec(binary.substr(11,5))+ 20 + Convert2Dec(binary.substr(16,16));

		rd2 = Convert2Dec(binary.substr(11,5)) + 20;

		drg = Convert2Dec(binary.substr(11,5));

		EX_reg(npc,zero,alu,rd2,drg);

		cout<<endl;

	}

	cout<<"Press enter for next cycle.";

	if(cin.ignore())

	{

	cyc = cyc + 1;

	pc = pc + 4;

	IF_reg(cyc,pc,sub);

	pc = pc + 4;

	rd_1 = Convert2Dec(digit.substr(6,5))+ 20;

	rd_2 = Convert2Dec(digit.substr(11,5))+ 20;

	sg_ex = Convert2Dec(digit.substr(16,16));

	destrg = Convert2Dec(digit.substr(11,5));

	ID_reg(pc,rd_1,rd_2,sg_ex,destrg);

	d_r = Convert2Dec(numbers.substr(11,5));

	r_d2 = Convert2Dec(numbers.substr(11,5))+ 20;

	pc_n = Convert2Dec(numbers.substr(16,16))*4+4;

	alr = Convert2Dec(numbers.substr(11,5))+ 20 + Convert2Dec(numbers.substr(16,16));

	EX_reg(pc_n,zero,alr,r_d2,d_r);

	alu_r = Convert2Dec(binary.substr(11,5))+ 20 + Convert2Dec(binary.substr(16,16));

	read = Convert2Dec(binary.substr(11,5))+ 20 + Convert2Dec(binary.substr(16,16)) + 100;

	MEM_reg(read,alu_r,drg);

	cout<<endl;

	}
 

	cout<<"Press enter for next cycle.";

	if(cin.ignore())

	{

	cyc = cyc + 1;

	pc = pc + 4;

	IF_reg(cyc,pc,beq);

	pc = pc + 4;

	rd_s1 = Convert2Dec(sub.substr(6,5))+ 20;

	rd_s2 = Convert2Dec(sub.substr(11,5))+ 20;

	sg_ex2 = Convert2Dec(sub.substr(16,16));

	destrg2 = Convert2Dec(sub.substr(11,5));

	ID_reg(pc,rd_s1,rd_s2,sg_ex2,destrg2);

	xd_r = Convert2Dec(digit.substr(11,5));

	xr_d2 = Convert2Dec(digit.substr(11,5))+ 20;

	xalr = Convert2Dec(digit.substr(11,5))+ 20 + Convert2Dec(digit.substr(16,16));

	pc_nx = Convert2Dec(digit.substr(16,16))*4+4;

	EX_reg(pc_nx,xzero,xalr,xr_d2,xd_r);

	cout<<endl;

	}

	cout<<"Press enter for next cycle.";

	if(cin.ignore())

	{

		cyc = cyc + 1;

		pc = pc + 4;

		IF_reg(cyc,pc,bne);

		rd_sy1 = Convert2Dec(beq.substr(6,5))+ 20;

		rd_sy2 = Convert2Dec(beq.substr(11,5))+ 20;

		sgexy = Convert2Dec(beq.substr(16,16));

		destrgy = Convert2Dec(beq.substr(11,5));

		ID_reg(pc,rd_sy1,rd_sy2,sgexy,destrgy);

	}

}
 
 

	inputdata.close();

	return 0;

}

void IF_reg(int &cycle, int &pc, string &bin)

{

	//cycle = 0;

	

	cout<<"cycle "<<cycle<<endl;

	cout<<"IF/ID:"<<endl;

	cout<<"\"PC+4\" = "<<pc<<", "<<"Instruction = "<<bin<<endl;
 

}
 

void ID_reg(int &pc, int &r_d1, int &r_d2,int &sgn_ext, int &d_reg)

{

	

	cout<<"ID/EX:"<<endl;

	cout<<"PC+4 = "<<pc<<", "<<"Read Data1 = "<<r_d1<<", "<<"Read Data2 = "<<r_d2<<", ";

	cout<<"Sign_ex = "<<sgn_ext<<", "<<"Dest Register = "<<d_reg<<endl;

}
 

void EX_reg(int &n_pc, int z, int &ALU_r, int &r_d2, int &d_reg)

{

	cout<<"EX/MEM:"<<endl;

    if(ALU_r != 0)

		{

			z = 1;

		}

		else

		{

			z = 0;

		}

	cout<<"New PC = "<<n_pc<<", "<<"Zero = "<<z<<", "<<"ALU Result = "<<ALU_r<<", ";

	cout<<"Read Data2 = "<<r_d2<<", "<<"Dest Register = "<<d_reg<<endl;

}
 

void MEM_reg(int &read_d, int &ALU_r, int &d_reg)

{

	cout<<"MEM/WB:"<<endl;

	cout<<"Read Data = "<<read_d<<", "<<"ALU Result = "<<ALU_r<<", "<<"Dest Register = ";

	cout<<d_reg<<endl;

}

int Convert2Dec(string binaryStr)

{

	int number_len = strlen(binaryStr.c_str());

	int solution = 0;

	int place = number_len - 1;

	for(int x = 0; x < number_len; x++)

	{

		if(binaryStr.substr(x, 1) == "1")

		{

			solution = solution + (int) pow(2.0, place);

		}

		place--;

	}

	return solution;

}

Open in new window

0
Comment
Question by:Pricky5
  • 2
3 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 20349410
You're trying to implement a pipeline ?

Imo it would be easier to have a generic loop like this :

        while (key_pressed) {
            // run IF stage
            // run ID stage
            // run EX stage
            // run MEM stage
            // check for key press
        }

and have 4 separate (global) instruction pointers - one for each stage. When all the stages have been run (at the end of the cycle), the instruction pointers for the stages can be updated.
0
 

Author Closing Comment

by:Pricky5
ID: 31410899
This guy is Great!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!: )
0
 

Author Comment

by:Pricky5
ID: 20376413
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now