By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

Cache Memory Simulation problem

P: 87
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. class Cache
  8. {
  9. private:    
  10.     float hit;
  11.     float miss;
  12. public:
  13.     char tag [8];
  14.     int cache_vals [5];
  15.     int memory [65536];
  16.     Cache();
  17.     ~Cache(){};
  18.     void read();
  19.     void menu();
  20.     void display_cache();
  21.     void display_hm(int hit, int miss);
  22.     void load();
  23.     void store(char val[3], int address);
  24.     void direct_address_access(char val);
  25.     int convert (char c[3], int address);
  26.     void store_access(char val[5], int funk);
  27.     int convertaddy(char c[], int address);
  28.     void display_access();
  29. };
  30. Cache::Cache()
  31. {
  32.     int i;
  33.     hit=1;
  34.     miss=1;
  35.     for (i=0;i<5;++i)
  36.     {
  37.         cache_vals[i]='0';
  38.     }
  39.     for (i=0;i<8;++i)
  40.     {
  41.         tag[i]=0;
  42.  
  43.     }
  44.     for (i=0;i<65536;++i)
  45.     {
  46.         memory[i]=0;
  47.     }
  48. }
  49. int Cache::convertaddy(char c[], int address)
  50. {
  51.     int i=0,val=0,temp=0,val1=0,temp1=0;
  52.     do
  53.     {
  54.         temp=(int (c[i])<65)? (int (c[i])-48)
  55.             :(int (c[i])-55);
  56.         val=(int (c[i+1])<65)? (int (c[i+1])-48)
  57.             :(int (c[i+1])-55);
  58.         temp1=(int (c[i+2])<65)? (int (c[i+2])-48)
  59.             :(int (c[i+2])-55);
  60.         val1=(int (c[i+3])<65)? (int (c[i+3])-48)
  61.             :(int (c[i+3])-55);
  62.         temp=temp*(16*16*16);
  63.         val=val*(16*16);
  64.         temp1=temp1*16;
  65.         val1=val1*1;
  66.         val=val+temp+val1+temp1;
  67.         ++i;
  68.         return (val);            
  69.     }
  70.     while (i<=address);
  71. }    
  72. int Cache::convert(char c[], int address)
  73. {
  74.     int i=0,val=0,temp=0;
  75.     do
  76.     {
  77.         temp=(int (c[i])<65)? (int (c[i])-48)
  78.             :(int (c[i])-55);
  79.         val=(int (c[i+1])<65)? (int (c[i+1])-48)
  80.             :(int (c[i+1])-55);
  81.         temp=temp*16;
  82.         val=val%16;
  83.         val=val+temp;
  84.         ++i;
  85.         return (val);            
  86.     }
  87.     while (i<=address);
  88.  
  89.  
  90. }    
  91. void Cache::store_access(char val[],int funk)
  92. {    
  93.     cache_vals[funk]=convertaddy(val,funk);
  94.     cout<<"STORE_ACCESS "<<funk<<" "<<cache_vals[funk]<<endl;
  95. }
  96. void Cache::store(char val[], int address)
  97. {
  98.     memory[address]=convert(val,address);
  99.     cout<<"STORE_MEM "<<address<<" "<<memory[address]<<endl;
  100. }
  101. void Cache::display_cache()
  102. {
  103.     for (int i=0;i<=10;++i)
  104.     {
  105.         cout<<memory[i]<<endl;
  106.     }
  107. }
  108. void Cache::display_access()
  109. {
  110.     for (int i=0;i<=11;++i)
  111.     {
  112.         cout<<cache_vals[i]<<endl;
  113.     }
  114. }
  115. void Cache::display_hm(int hit, int miss)
  116. {
  117.     float tot=0;    
  118.     tot=(hit+miss);
  119.     tot=(hit/tot);
  120.     cout<<"   Hit= "<<hit<<"     Miss= "<<miss;
  121.     cout<<"   Ratio= "<<tot<<"%"<<endl;
  122. }
  123. void Cache::menu()
  124. {
  125.     int sel=0,sel1=0,i=0;
  126.     char file[10]={0};
  127.     cout<<endl<<"#######################################################"<<endl;
  128.     do
  129.     {
  130.         cout<<" Welcome to the Cache Memory program                   "<<endl;
  131.         cout<<" What would you like to do? Please make a selection from the menu"<<endl;
  132.         cout<<endl<<" 1) Direct mapping                                     "<<endl;
  133.         cout<<endl<<" 2) Associative mapping                                "<<endl;
  134.         cout<<endl<<" 3) Set Associative mapping                            "<<endl;
  135.         cout<<endl<<"#######################################################"<<endl<<endl;
  136.         cin>>sel;
  137.     }
  138.     while (sel<1||sel>3);
  139.     switch(sel)
  140.     {
  141.     case 1:
  142.         {
  143.         cout<<"Direct mapping......loading cache"<<endl;
  144.         do
  145.         {
  146.             cout<<"What would you like to do?"<<endl;
  147.             cout<<"1) Begin displaying contents of cache"<<endl;
  148.             cout<<"2) Change input file."<<endl;
  149.             cout<<"3) Display hit/miss ratio"<<endl;
  150.             cout<<"4) Exit"<<endl;
  151.             cin>>sel1;
  152.         }
  153.         while (sel1<1||sel1>4);
  154.         switch (sel1)
  155.         {
  156.         case 1:
  157.             display_access();
  158.             break;
  159.         case 2:
  160.             cout<<"Enter your file name"<<endl;
  161.             cout<<"Test_one.in used as default"<<endl;
  162.             system("PAUSE");
  163.             cout<<"Please press enter to display"<<endl;
  164.             break;
  165.         case 3:
  166.             display_hm(hit,miss);
  167.             break;
  168.         case 4:
  169.             cout<<"Thanks....exiting program"<<endl;
  170.             break;
  171.         }
  172.         break;
  173.         }
  174.     case 2:
  175.         {
  176.         cout<<"Associative mapping not enabled....redirecting"<<endl;
  177.         menu();
  178.         break;
  179.         }
  180.     case 3:
  181.         {
  182.         cout<<"Set Associative mapping not enabled....redirecting"<<endl;
  183.         menu();
  184.         break;
  185.         }
  186.     }
  187. }
  188. int main()
  189. {
  190.     int i=0,j=0, address=0, address1=0 ;
  191.     Cache a;
  192.     char temp[3]={0}, temp1[8]={0};
  193.  
  194.     fstream ifp;
  195.     fstream ifp2;
  196.     ifp.open("C:\\Documents and Settings\\Jeff Young\\My Documents\\Visual Studio Projects\\cache_mem\\memory2.txt");
  197. //    cout<<a.memory<<" FIRST MAIN"<<endl;
  198.     while (ifp>>temp&&address<65536)
  199.     {
  200.         a.store(temp,address);                        
  201.         ++address;
  202.     }
  203.     ifp.close();
  204.     ifp.clear();
  205.     ifp2.open("C:\\Documents and Settings\\Jeff Young\\My Documents\\Visual Studio Projects\\cache_mem\\access1.txt");
  206.     while (ifp2>>temp1&&address1<1000)
  207.     {
  208.  
  209.         a.store_access(temp1,address1);
  210.         ++address1;
  211.     }
  212.     ifp2.close();
  213.     ifp2.clear();
  214. //    a.menu();
  215.     cout<<"HELLO"<<endl;
  216.     a.display_cache();
  217.     a.display_access();
  218.     return(0);
  219.  
  220. }
  221.  
  222.  
Nov 22 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.