cache memory.cpp

来自「direct mapping associative mapping set」· C++ 代码 · 共 339 行

CPP
339
字号
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <limits>
#include <bitset>
using namespace std;
struct blockstruct
{
	int oneblock[8][3];
	bool old;
};

struct setblocks
{
	blockstruct singleblock[2];
};
setblocks setcacheblocks[8000];
blockstruct cacheblocks[16000];
bool found=0;
double percentage =0;
double hit=0;
int miss=0;
int position=0;
int word=0;
int block=0;
int tag=0;
int set = 0;
int line=0;
int cacheaddress=0;
string sword=" ";
string sblock=" ";
string stag=" ";
string sset=" ";
string sline=" ";
string path="example.txt"; 

template<typename T>
string xxx_to_bin(const T& value)
{
const std::bitset<std::numeric_limits<T>::digits + 1> bs(value);	//This function is used to convert from integer to binary string
const std::string s(bs.to_string());
const std::string::size_type pos(s.find_first_not_of('0'));
return pos == std::string::npos ? "0" : s.substr(pos);
}

void initialize()
{
	bool found=0;
    position=0;
    miss=0;
    hit=0;
    word=0;										//This function is used after running the direct mapping,
    block=0;									//associative mapping or set associative mapping to ensure 
    tag=0;										//that the chache is empty.
    line=0;
	set=0;
    cacheaddress=0;
    sword=" ";
    sblock=" ";
    stag=" ";
    sline=" ";
	sset="";

	for(int i =0; i<16000;i++)
	{
		for(int j =0; j<8;j++)
		{
			cacheblocks[i].oneblock[j][0]=0;
			cacheblocks[i].oneblock[j][1]=0;
			cacheblocks[i].oneblock[j][2]=0;
		}
	}
	for(int s=0; s<8000;s++)
	{
		for(int k=0; k<8;k++)
		{
			setcacheblocks[0].singleblock[s].oneblock[k][0]=0;
			setcacheblocks[0].singleblock[s].oneblock[k][1]=0;
			setcacheblocks[0].singleblock[s].oneblock[k][2]=0;
			setcacheblocks[1].singleblock[s].oneblock[k][0]=0;
			setcacheblocks[1].singleblock[s].oneblock[k][1]=0;
			setcacheblocks[1].singleblock[s].oneblock[k][2]=0;
			setcacheblocks[0].singleblock[s].old=1;
			setcacheblocks[1].singleblock[s].old=1;
		}
	}
}

int counter()
{
  float count = 0;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
		getline (myfile,sline);
        count++;
    }
    myfile.close();
	return count;
  }
  else cout << "Unable to open file";
  return 0;
}


void DirectMapping()
{
  cout<<"Direct Mapping:"<<endl;
  ifstream myfile(path.c_str());
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,sline);
	  sline = sline.substr(2);
	  stringstream convert ( sline );
	  convert>> std::hex >> line;
	  sline = xxx_to_bin(line);
	  while(sline.length()<32)
	  {
		  sline = "0"+sline;
	  }
	  stag = sline.substr(0,15);
	  sblock=sline.substr(15,28);
	  sblock=sblock.substr(0,14);
	  sword = sline.substr(29,32);
	  tag=bitset<numeric_limits<unsigned long>::digits>(stag).to_ulong();
	  block=bitset<numeric_limits<unsigned long>::digits>(sblock).to_ulong();
	  word=bitset<numeric_limits<unsigned long>::digits>(sword).to_ulong();
	  cacheaddress = line%16000;

	  if(cacheblocks[cacheaddress].oneblock[0][1]==block &&
		  cacheblocks[cacheaddress].oneblock[0][0]==tag)
	  {
		  hit++;
	  }
	  else
	  {
		for (int i=0; i<8; i++)
		{
			cacheblocks[cacheaddress].oneblock[i][0]=tag;
			cacheblocks[cacheaddress].oneblock[i][1]=block;
			cacheblocks[cacheaddress].oneblock[i][2]=i;

		}
		miss++;
	  }
	 
    }
	percentage = (hit/counter())*100;
	cout<<"Hits = "<<hit<<endl;
	cout<<"Misses = "<<miss<<endl;
	cout<<"Percentage of hits = "<<percentage<<"%"<<endl;
	cout<<"*********************************"<<endl;
	cout<<endl;
	cout<<endl;
    myfile.close();
	initialize();
  }
  else cout << "Unable to open file"; 
}

void AssociativeMapping()
{
  cout<<"Associative Mapping:"<<endl;
  if(position==16000)
  {
	  position=0;
  }
  ifstream myfile(path.c_str());
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,sline);
	  sline = sline.substr(2);
	  stringstream convert ( sline );
	  convert>> std::hex >> line;
	  sline = xxx_to_bin(line);
	  while(sline.length()<32)
	  {
		  sline = "0"+sline;
	  }
	  stag = sline.substr(0,29);
	  sword = sline.substr(29,32);
	  tag=bitset<numeric_limits<unsigned long>::digits>(stag).to_ulong();
	  word=bitset<numeric_limits<unsigned long>::digits>(sword).to_ulong();

	  for(int i=0; i<16000; i++)
	  {
		  if(cacheblocks[i].oneblock[0][0]==tag)
		  {
			found=1;
			break;
		  }
		  else
			  found=0;
	  }
	  if(found==1)
	  {
		  hit++;
	  }
	  else
	  {
		for (int i=0; i<8; i++)
		{
			cacheblocks[position].oneblock[i][0]=tag;
			cacheblocks[position].oneblock[i][1]=0;
			cacheblocks[position].oneblock[i][2]=i;

		}
		position++;
		miss++;
	  }
	 
    }
	percentage = (hit/counter())*100;
	counter();
	cout<<"Hits = "<<hit<<endl;
	cout<<"Misses = "<<miss<<endl;
	cout<<"Percentage of hits = "<<percentage <<"%"<<endl;
	cout<<"*********************************"<<endl;
	cout<<endl;
	cout<<endl;
    myfile.close();
	initialize();
  }
  else cout << "Unable to open file"; 
}

void SetAssociativeMapping()
{
  cout<<"Set Associative Mapping:"<<endl;
  ifstream myfile(path.c_str());
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,sline);
	  sline = sline.substr(2);
	  stringstream convert ( sline );
	  convert>> std::hex >> line;
	  sline = xxx_to_bin(line);
	  while(sline.length()<32)
	  {
		  sline = "0"+sline;
	  }
	  stag = sline.substr(0,21);
	  sset=sline.substr(21,28);
	  sset=sset.substr(0,8);
	  sword = sline.substr(29,32);
	  tag=bitset<numeric_limits<unsigned long>::digits>(stag).to_ulong();
	  set=bitset<numeric_limits<unsigned long>::digits>(sset).to_ulong();
	  word=bitset<numeric_limits<unsigned long>::digits>(sword).to_ulong();
	  if(setcacheblocks[set].singleblock[0].oneblock[0][0]==tag ||setcacheblocks[set].singleblock[1].oneblock[0][0]==tag )
	  {
		  hit++;
	  }
	  else
	  {
		  if(setcacheblocks[set].singleblock[1].old==1)
		  {
			  for (int i=0; i<8; i++)
			  {
				  setcacheblocks[set].singleblock[0].oneblock[i][0]=tag;
				  setcacheblocks[set].singleblock[0].oneblock[i][1]=set;
				  setcacheblocks[set].singleblock[0].oneblock[i][2]=1;
			  }
			  setcacheblocks[set].singleblock[0].old=0;
			  setcacheblocks[set].singleblock[1].old=1;
		  }
		  else
			  {
				 for (int i=0; i<8; i++)
				 {
				  setcacheblocks[set].singleblock[0].oneblock[i][0]=tag;
				  setcacheblocks[set].singleblock[0].oneblock[i][1]=set;
				  setcacheblocks[set].singleblock[0].oneblock[i][2]=1;
				 }
					 setcacheblocks[set].singleblock[1].old=0;
					 setcacheblocks[set].singleblock[0].old=1;
				}
	  
		for (int i=0; i<8; i++)
		{
			cacheblocks[cacheaddress].oneblock[i][0]=tag;
			cacheblocks[cacheaddress].oneblock[i][1]=set;
			cacheblocks[cacheaddress].oneblock[i][2]=i;

		}
		miss++;
	  }
	 
    }
	percentage = (hit/counter())*100;
	cout<<"Hits = "<<hit<<endl;
	cout<<"Misses = "<<miss<<endl;
	cout<<"Percentage of hits = "<<percentage<<"%"<<endl;
	cout<<"*********************************"<<endl;
	cout<<endl;
	cout<<endl;
    myfile.close();
	initialize();
  }
  else cout << "Unable to open file"; 
}



void main()
{	string where="run";
	char decide;
	cout<<" This program is designed to simulate Direct Mapping, Associative Mapping, and \n Set Associative Mapping. "<<
	"If you want to run the program on an existing \n sample of addresses please enter the number 1. "
		<<"otherwise enter the path of \n the text file that cotain the memmory address that you wnat to run \n the program on: "<<endl;
	cin>>where;
	if(where!="run")
	{
		path = where;
	}
	cout<<endl;
	DirectMapping();
	AssociativeMapping();
	SetAssociativeMapping();
	cout<<"Do you want to Exsit? Y/N"<<endl;
	cin>>decide;
	if(decide=='n'||decide=='N')
	{
		main();
	}
	
}



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?