[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

A program to simulate the instruction execution in a datapath

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
Pricky5
Asked:
Pricky5
  • 2
1 Solution
 
Infinity08Commented:
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
 
Pricky5Author Commented:
This guy is Great!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!: )
0
 
Pricky5Author Commented:
Thanks!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now