Solved

A program to simulate the instruction execution in a datapath

Posted on 2007-11-25
3
376 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 276
SNMP error No Such Object available on this agent at this OID 3 360
Indy 10 not Receiving UDP broadcast 3 43
find Speed using GPS latitude & longitude 4 78
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

679 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