My program is to simulate cache memory. I read in the info from 2 external files, 1) access 2) data in memory. When I read the information in I display the info...and it is all correct. However when I attempt to display the info in main anywhere the info is incorrect. I thought it might be a problem while I was reading the data in, but it wouldn't be correct the first time if that were the case. Below is the code. At the end of main where the a.display functions are is where the info is not being displayed properly.
Thanks,
J
Thanks,
J
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class Cache
{
private:
float hit;
float miss;
public:
char tag [8];
int cache_vals [5];
int memory [65536];
Cache();
~Cache(){};
void read();
void menu();
void display_cache();
void display_hm(int hit, int miss);
void load();
void store(char val[3], int address);
void direct_address_access(char val);
int convert (char c[3], int address);
void store_access(char val[5], int funk);
int convertaddy(char c[], int address);
void display_access();
};
Cache::Cache()
{
int i;
hit=1;
miss=1;
for (i=0;i<5;++i)
{
cache_vals[i]='0';
}
for (i=0;i<8;++i)
{
tag[i]=0;
}
for (i=0;i<65536;++i)
{
memory[i]=0;
}
}
int Cache::convertaddy(char c[], int address)
{
int i=0,val=0,temp=0,val1=0,temp1=0;
do
{
temp=(int (c[i])<65)? (int (c[i])-48)
:(int (c[i])-55);
val=(int (c[i+1])<65)? (int (c[i+1])-48)
:(int (c[i+1])-55);
temp1=(int (c[i+2])<65)? (int (c[i+2])-48)
:(int (c[i+2])-55);
val1=(int (c[i+3])<65)? (int (c[i+3])-48)
:(int (c[i+3])-55);
temp=temp*(16*16*16);
val=val*(16*16);
temp1=temp1*16;
val1=val1*1;
val=val+temp+val1+temp1;
++i;
return (val);
}
while (i<=address);
}
int Cache::convert(char c[], int address)
{
int i=0,val=0,temp=0;
do
{
temp=(int (c[i])<65)? (int (c[i])-48)
:(int (c[i])-55);
val=(int (c[i+1])<65)? (int (c[i+1])-48)
:(int (c[i+1])-55);
temp=temp*16;
val=val%16;
val=val+temp;
++i;
return (val);
}
while (i<=address);
}
void Cache::store_access(char val[],int funk)
{
cache_vals[funk]=convertaddy(val,funk);
cout<<"STORE_ACCESS "<<funk<<" "<<cache_vals[funk]<<endl;
}
void Cache::store(char val[], int address)
{
memory[address]=convert(val,address);
cout<<"STORE_MEM "<<address<<" "<<memory[address]<<endl;
}
void Cache::display_cache()
{
for (int i=0;i<=10;++i)
{
cout<<memory[i]<<endl;
}
}
void Cache::display_access()
{
for (int i=0;i<=11;++i)
{
cout<<cache_vals[i]<<endl;
}
}
void Cache::display_hm(int hit, int miss)
{
float tot=0;
tot=(hit+miss);
tot=(hit/tot);
cout<<" Hit= "<<hit<<" Miss= "<<miss;
cout<<" Ratio= "<<tot<<"%"<<endl;
}
void Cache::menu()
{
int sel=0,sel1=0,i=0;
char file[10]={0};
cout<<endl<<"#######################################################"<<endl;
do
{
cout<<" Welcome to the Cache Memory program "<<endl;
cout<<" What would you like to do? Please make a selection from the menu"<<endl;
cout<<endl<<" 1) Direct mapping "<<endl;
cout<<endl<<" 2) Associative mapping "<<endl;
cout<<endl<<" 3) Set Associative mapping "<<endl;
cout<<endl<<"#######################################################"<<endl<<endl;
cin>>sel;
}
while (sel<1||sel>3);
switch(sel)
{
case 1:
{
cout<<"Direct mapping......loading cache"<<endl;
do
{
cout<<"What would you like to do?"<<endl;
cout<<"1) Begin displaying contents of cache"<<endl;
cout<<"2) Change input file."<<endl;
cout<<"3) Display hit/miss ratio"<<endl;
cout<<"4) Exit"<<endl;
cin>>sel1;
}
while (sel1<1||sel1>4);
switch (sel1)
{
case 1:
display_access();
break;
case 2:
cout<<"Enter your file name"<<endl;
cout<<"Test_one.in used as default"<<endl;
system("PAUSE");
cout<<"Please press enter to display"<<endl;
break;
case 3:
display_hm(hit,miss);
break;
case 4:
cout<<"Thanks....exiting program"<<endl;
break;
}
break;
}
case 2:
{
cout<<"Associative mapping not enabled....redirecting"<<endl;
menu();
break;
}
case 3:
{
cout<<"Set Associative mapping not enabled....redirecting"<<endl;
menu();
break;
}
}
}
int main()
{
int i=0,j=0, address=0, address1=0 ;
Cache a;
char temp[3]={0}, temp1[8]={0};
fstream ifp;
fstream ifp2;
ifp.open("C:\\Documents and Settings\\Jeff Young\\My Documents\\Visual Studio Projects\\cache_mem\\memory2.txt");
// cout<<a.memory<<" FIRST MAIN"<<endl;
while (ifp>>temp&&address<65536)
{
a.store(temp,address);
++address;
}
ifp.close();
ifp.clear();
ifp2.open("C:\\Documents and Settings\\Jeff Young\\My Documents\\Visual Studio Projects\\cache_mem\\access1.txt");
while (ifp2>>temp1&&address1<1000)
{
a.store_access(temp1,address1);
++address1;
}
ifp2.close();
ifp2.clear();
// a.menu();
cout<<"HELLO"<<endl;
a.display_cache();
a.display_access();
return(0);
}