Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

btb.cpp

Go to the documentation of this file.
00001 /*
00002         Copyright (C) 1997 CFU Corporation
00003 
00004         File: btb.cpp
00005         Description: branch target buffer
00006         Author: Chao-yinig Fu
00007         Date: Oct. 27, 2000
00008 
00009         ^..^
00010         (00)
00011 
00012          $__$
00013         <~00~>
00014          (oo)
00015           ||
00016 
00017         ```````
00018         C ^|^ D
00019          \ O /
00020 */
00021 
00022 #include "btb.h"
00023 
00024 //
00025 // Input: 
00026 //      ip:     the ip of this mop
00027 //      slot:   the predicted slot
00028 // Return
00029 //      find_btb: true (yes), false (no)
00030 //      target: the predicted address
00031 //
00032 void BTB::predict(int ip,int slot,bool &find_btb,int &target)
00033 {
00034         //if(ip!=5692)return;
00035 
00036         int index=ip>>_b;
00037         index=index&_index_mask;
00038 
00039         int start_index=index*_num_of_entry_per_set;
00040 
00041         //fprintf(stderr,"ip=%d index=%d\n",ip,index);
00042 
00043         int this_index;
00044         int i;
00045         for(i=0;i<_num_of_entry_per_set;i++)
00046         {
00047                 this_index=start_index+i;
00048 
00049                 // Check Tag
00050                 if(_tag[this_index]==ip && _slot[this_index]==slot)
00051                 {
00052                         break;
00053                 }
00054         }
00055 
00056         if(i==_num_of_entry_per_set)    // We didn't find the tag match
00057         {
00058                 find_btb=false;
00059 
00060                 //fprintf(stderr,"We didn't find_btb for ip=%d slot=%d\n",ip,slot);
00061                 return; 
00062         }
00063         else
00064         {
00065                 find_btb=true;
00066                 target=_target[this_index];
00067 
00068                 //fprintf(stderr,"We find_btb for ip=%d slot=%d target=%d\n",ip,slot,target);
00069                 return;
00070         }
00071 }
00072 
00073 //
00074 // Parameter
00075 //      ip: ip of this mop
00076 //      slot: the actual taken slot
00077 //      target: the taken address
00078 //      global_cycle: the current global cycle
00079 //
00080 void BTB::update(int ip,int slot,int target,int global_cycle)
00081 {
00082         //if(ip!=5692)return;
00083 
00084         int index=ip>>_b;
00085         index=index&_index_mask;
00086 
00087         int start_index=index*_num_of_entry_per_set;
00088 
00089         //HZ: add initialization of this_index, not sure it is the right thing
00090         // to do
00091         //int this_index;
00092         int this_index = 0;
00093         int i;
00094         int oldest_time=_time[start_index]; //_time[this_index];
00095         int oldest_entry=start_index;
00096         int empty_entry=-1;
00097 
00098         for(i=0;i<_num_of_entry_per_set;i++)
00099         {
00100                 this_index=start_index+i;
00101 
00102                 int this_tag=_tag[this_index];
00103                 int this_slot=_slot[this_index];
00104                 // Check Tag
00105                 if(this_tag==ip && this_slot==slot)
00106                 {
00107                         break;
00108                 }
00109 
00110                 // find the first empty entry
00111                 if(empty_entry==-1 && this_tag==-1)
00112                 {
00113                         empty_entry=this_index;
00114                 }
00115 
00116                 // find LRU entry
00117                 int this_time=_time[this_index];
00118                 if(this_time<oldest_time)
00119                 {
00120                         oldest_time=this_time;
00121                         oldest_entry=this_index;
00122                 }
00123         }
00124 
00125         if(i==_num_of_entry_per_set)    // We didn't find the tag match
00126         {
00127                 // Replace oldest_entry
00128                 // update its time
00129                 if(empty_entry==-1)     // put to oldest entry
00130                 {
00131                         _time[oldest_entry]=global_cycle;
00132                         _tag[oldest_entry]=ip;
00133                         _slot[oldest_entry]=slot;
00134                         _target[oldest_entry]=target;
00135 
00136                         //fprintf(stderr,"BTB MISS UPDATE ip=%d slot=%d target=%d saveto (index=%d oldest_entry=%d)\n",ip,slot,target,index,oldest_entry%start_index);
00137                 }
00138                 else    // put to empty_entry
00139                 {
00140                         _time[empty_entry]=global_cycle;
00141                         _tag[empty_entry]=ip;
00142                         _slot[empty_entry]=slot;
00143                         _target[empty_entry]=target;
00144 
00145                         //fprintf(stderr,"BTB MISS UPDATE ip=%d slot=%d target=%d saveto (index=%d empty_entry=%d)\n",ip,slot,target,index,empty_entry%start_index);
00146                 }
00147         }
00148         else    // we find the tag match
00149         {
00150                 // update its time
00151                 _time[this_index]=global_cycle;
00152 
00153                 if(_target[this_index]!=target)
00154                 {
00155                         //fprintf(stderr,"BTB HIT UPDATE ip=%d slot=%d old_target=%d new_target=%d\n",ip,slot,_target[this_index],target);
00156                         _target[this_index]=target;
00157                 }
00158         }
00159 }

Generated on Mon Jul 21 20:24:02 2003 for TINKER LEGO DOC by doxygen 1.3.2