Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

A program to simulate the instruction execution in a datapath

Posted on 2007-11-25
3
Medium Priority
?
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

609 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