Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* For 2-bit Counter */
- #define k 5
- #define entry pow(2,k) // entry = 2^k
- /* Parameters */
- #define Taken 1
- #define NonTaken 0
- /* Declare Variables */
- int Branch_Prediction_Buffer[(int)entry];
- int Hit = 0 , Total = 0; // Record for success of conditional branch
- int mapping = 0;
- /* Functions */
- unsigned long Hex2Dec(char *ch)
- {
- int i,j=0, temp = 0;
- unsigned long sum = 0 ;
- if( ch[1] == 10 ) return 0;
- for(i=7 ; i>=0 ; i--)
- {
- temp = ( ch[i] >= '0' && ch[i] <= '9' )? ch[i] - '0' : ( ch[i] >= 'a' && ch[i] <= 'e')? (ch[i] - 'a') +10 : 0;
- sum += temp << (j++*4);
- }
- return sum;
- }
- int main(int argc, char *argv[])
- {
- char now_buf[20] = {0} , next_buf[20] = {0} , type_buf[10] = {0} ;
- int type ;
- int i =1, PCnow = 0, PCnext = 1;
- int Evaluation = 0 , Predict ;
- float Prediction_Rate;
- /* Initialize Buffer */
- int j;
- for(j=0;j<entry;j++) Branch_Prediction_Buffer[j] = NonTaken;
- /* Open files */
- FILE *Now_ins, *Next_ins, *Ins_type;
- Now_ins = fopen("Path_pc_now.txt","r");
- Next_ins = fopen("Path_pc_next.txt","r");
- Ins_type = fopen("Path_type.txt","r"); // 1 is for conditional branch , SKIP if type is not 1
- /* Main loop */
- while(1)
- {
- /* Read data */
- fgets(now_buf , 20 , Now_ins);
- fgets(type_buf , 10 , Ins_type);
- fgets(next_buf , 20 , Next_ins);
- /* Convert to dec */
- type = atoi(type_buf);
- PCnow = Hex2Dec(now_buf);
- PCnext = Hex2Dec(next_buf);
- /* Last data */
- if(PCnext == 0) break;
- /* Prediction */
- if(type != 1) ;
- else
- {
- Evaluation = (PCnext - PCnow == 4)? NonTaken : Taken;
- mapping = (PCnow>>2) % (1<<k);
- Total++;
- Predict = Branch_Prediction_Buffer[mapping] ;
- printf("i=%3d state=%d mapping=%2d pc:%d p:%d e:%d\n",i,Branch_Prediction_Buffer[mapping],mapping,PCnow,Predict,Evaluation);
- //printf("pc:%d p:%d e:%d\n",PCnow,Predict,Evaluation);
- if( Predict == Evaluation )
- {
- Hit++;
- if( Predict == Taken ) Branch_Prediction_Buffer[mapping]++;
- else Branch_Prediction_Buffer[mapping]--;
- }
- else
- {
- if( Predict == Taken ) Branch_Prediction_Buffer[mapping]--;
- else Branch_Prediction_Buffer[mapping]++;
- }
- if( Branch_Prediction_Buffer[mapping] > Taken ) Branch_Prediction_Buffer[mapping] = Taken;
- else if( Branch_Prediction_Buffer[mapping] < NonTaken ) Branch_Prediction_Buffer[mapping] = NonTaken;
- }
- i++;
- }
- printf("Hit:%d Missed:%d Prediction Rate:%.2f%%\n",Hit,Total-Hit,(float)Hit/Total*100);
- /* Close files */
- fclose(Now_ins);
- fclose(Next_ins);
- fclose(Ins_type);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment