









































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










































Hand out date: Nov 4, 2004
Due date: Dec 14-15, 2004
By
Kan Liu, Piyush Ranjan Satapathy & Ricky J Sethi
Project Specification Given By Course Instructor, Dr Jun Yang
In this project, you will need to implement the Tomasulo algorithm for an out-of-order execution pipeline architecture. It is preferred that you write this project in C or C++. You should find one or two partner(s) to form a group of two or three. Please document your source code as you develop it. On the due day, you will demonstrate your project to me on a linux machine such as eon.cs.ucr.edu.
You will implement the following 4 stages: IF, Issue, Execute, and Writeback. The tasks performed in each stage were explained in class. You can refer to the detailed actions taken for bookkeeping from the textbook (Figure 3.5 on page 193). Those will help you greatly in coding the dynamic scheduler. You will implement only the FP pipeline. We will consider the following FUs:
The hardware configuration should be fully parameterizable, i.e., the number of reservation stations for each FU, and the number of execution cycles for each FU should all be the inputs to your simulator. You will need to simulate a "memory" which could be as simple as a data array. I'll leave the implementation details to you as long as you have your own way of initializing the memory and printing it out finally. During the demo, I will let you initialize your own memory at the addresses and values I defined. Your simulator should be able to read and write to your own memory correctly.
Data Transfer Instructions
ld Fa, offset(Ra) load a single precision floating point value to Fa
sd Fa, offset(Ra) Store a single precision floating point value to memory
Control Transfer Instructions
beq Rs, Rt, offset if Rs==Rt then branch to PC+offset
bne Rs, Rt, offset if Rs<>Rt then branch to PC+offset
ALU Instructions
add Rd, Rs, Rt Rd = Rs+Rt
add.d Fd, Fs, Ft Fd = Fs+Ft
addi Rt, Rs, immediate Rt=Rs+immediate
sub Rd, Rs, Rt Rd = Rs-Rt
sub.d Fd, Fs, Ft Fd = Fs-Ft
mult.d Fd, Fs, Ft Fd = Fs*Ft, assuming that Fd is enough to hold the result
div.d Fd, Fs, Ft Fd = Fs/Ft
Source Code: /********************************************************************
#include
#ifndef TOMASULO_H #define TOMASULO_H
#define NUM_IN_GPR 32 //define the number of general-purpose integer registers. #define NUM_FP_GPR 32 //define the number of general-purpose floating-point registers. #define SIZE_MEM 32 //define the size of memory(words). #define NUM_FUS 5 //the number of Function Units. #define CYCS_IN 1 //the number of cycles to finish an integer instruction. #define EMPTY -1 //for convenient,define EMPTY to be - #define ZERO 0.0 // For memory comparison
/Implement 11 instructions where 5 of them are integer instructions, 6 of them are floating-point instructions./ enum Instrs {LD,SD,BEQ,BNE,ADD,ADD_D,ADDI,SUB,SUB_D,MULT_D,DIV_D}; enum FUs {READY=-1,ADDER=0,MULTIPLIER,DIVIDER,LOAD,STORE}; //Implement 5 Function Units.Add "READY" for convenience. enum Res_States {CAL_AD,FIN_AD,NOT_EX,STARTED_EX,COMPLETED_EX}; //To show the current state of a reservation station.
//the data structure to represent the instruction status. struct Instruction_Sta { Instrs Op; int rd; int rs; int rt; int in_fetch; int issue; int exe_start; int exe_compl; int wri_back; //int res_id; //the index of the reservation station for this instruction. };//end Instruction_Sta
//the data structure represent the type and fu index and res index of a reservation station. struct Res_Sta_Id { FUs type; //one of ADDER,MULTIPLIER,DIVIDER,LOAD,STORE int fu_in; int index; //0~ };//end Res_Sta_Id
//the data structure to represent reservation stations. struct Reservation_Sta { bool Busy; Instrs Op; float Vj; float Vk; struct Res_Sta_Id Qj; struct Res_Sta_Id Qk; int A;
int des; Res_States curr_sta; int Exe; struct Instruction_Sta* curr_ins; };//end Reservation_Sta
//the data structure to represent an instruction. struct Instruction { //please refer enum Instrs {LD,SD,BEQ,BNE,ADD,ADD_D,ADDI,SUB,SUB_D,MULT_D,DIV_D}; Instrs Op; int label; int rd; int rs; int rt;
TOMASULO(); //constructor. ~TOMASULO(); //destructor.
int init_in_gpr(); //Initialize general-purpose integer registers. int init_fp_gpr(); //Initialize general-purpose floating-point registers. int init_mem(); //Initialize memory;
bool init_from_file(const char *); // RJS: Initialize integer registers, fp registers, and memory from file
void print_stat(); //RJS: Display res sta stat, inst stat, and reg result stat on every cycle void print_final_stats(); // RJS: Print the summary bool parse_input_assembly(const char*); // RJS: Parse the input file
int simu_run(); //simulate the algorithm and run on input assembly instructions.
void print_stat_local(); void print_mem(); //Display the content of memory. void print_in_gpr(); //Display the content of integer registers. void print_fp_gpr(); //Display the content of floating point registers. void print_ins(ostream & out); //Display the instructions in the queue "inst".
void check_regsiter(struct Instruction i); //make sure that the register no. is in valid range. bool check_Issue(); //check whether an instruction is waiting for issuing,issue an instruction when it is possible void Issue_book(int ty,int fu,int in); //bookkeeping for issue void Issue_arith(FUs ty,int fu,int in); //bookkeeping for FP operation issue bool fp_arith_issue(FUs ty); bool load_issue(); bool store_issue(); bool check_start_exe(int ty,int fu); //check whether the reservation station can start execution. bool check_compl_exe(int ty,int fu,int in); //check whether the reservation station finishs execution. bool write_result(int ty,int fu,int in); //write result bool Instr_Fetch(int p); //instruction fetch. bool cal_address(int ty,int fu,int in); //calculate address for load and store bool memory_Access(); //memory access(load or store),every cycle,at most one ins stared
};//end class TOMASULO
#endif
/****************************************************************
****************************************************************/
#include "TOMASULO.h" #include
// Some Constant and Global Variables //============================================================== ====== const int DEBUG = 0; const int MAX_SIZE = 256; const int MAX_BUFFER = 65536;
const char * DELIM = ", \t()RF"; const char * DELIM2 = ", \t"; const char LABEL_END = ':'; const char PAREN = '('; const char REGF = 'F'; const char REGI = 'R'; const int FP_REG_START = 32;
const int WORDSIZE = 4;
// Functions: //============================================================== ======
/**
*******************************************************************/
void dbg(string msg) { //cout << msg << endl; //msg = ""; return; }
*******************************************************************/
bool TOMASULO::parse_input_assembly(const char * filename) { input_filename = filename;
// Let's open the input file first: ifstream infile; infile.open(filename,ios::in); if (infile.fail()) { cerr << "Couldn't open file: " << filename << endl; exit(-1); }
// Tokenize each line of assembly code into space or tab delimited tokens. // Check for branch/loop labels (starts with L1:), offset in offset(R3), etc. // Dump each instruction structure into the inst deque char buf[MAX_BUFFER]; while (infile.getline(buf, MAX_BUFFER)) { char * word; vector<char *> words; Instrs opcode; string token, label, op, rd, rs, rt;
int i_rs=EMPTY,i_rt=EMPTY,i_rd=EMPTY,i_label=EMPTY;
word = strtok(buf, DELIM); if(word == NULL)//blank line. continue; while (word) { words.push_back(word); word = strtok(NULL, DELIM); }
// Get the first assembly token: token = words[0];
// Is first entry a label? if (token[token.size()-1] == LABEL_END) { // TODO: Should label's value be a word address instead of byte address (divide by 4)? label = token.substr(0,token.size()-1); // Get the label (sans the i_label_END)
else if(op == "add.d") opcode=ADD_D; else if(op == "sub") opcode=SUB; else if(op == "sub.d") opcode=SUB_D; else if(op == "mult.d") opcode=MULT_D; else if(op == "div.d") opcode=DIV_D; else{ cout<<"Not a valid operation code:"<<op<<endl; continue; } } if (DEBUG) {cout<<op<<"\t"<<rd<<"\t"<<rs<<"\t"<<rt<<endl;} // Now, let's hose the register prefixes: i_rs = atoi(rs.c_str()); i_rt = atoi(rt.c_str()); i_rd = atoi(rd.c_str());
if (DEBUG) {cout << "Value of rd is: " << rd << " and value of i_rd is: " << i_rd << endl;}
// Add to the inst deque: struct Instruction i; i.Op=opcode; i.label = i_label; i.rd = i_rd; i.rs = i_rs; i.rt = i_rt; check_regsiter(i); inst.push_back(i);
} // End while Loop infile.close();
return true; }
*******************************************************************/
bool TOMASULO::init_from_file(const char * filename) { // Let's open the input file first: ifstream infile; infile.open(filename,ios::in); if (infile.fail()) { cerr << "Couldn't open file: " << filename << endl; //;exit(-1); return false; }
// Vectors for FUs vector
// Tokenize each line of init file into space or comma delimited tokens. char buf[MAX_BUFFER];
for (int i=1; i<limit; i++) { Exe_Cycs[i-1] = atoi(words[i]); if (DEBUG) {cout << "Exe Cycles #: " << i << "=" << Exe_Cycs[i-1] << endl;} } }*/
// Get the FUs: else if (token.at(0) == 'A') { // Get Adder FU vAddRS.push_back(atoi(words[1])); vAddEC.push_back(atoi(words[2])); } else if (token.at(0) == 'M') { // Get Multiplier FU vMultRS.push_back(atoi(words[1])); vMultEC.push_back(atoi(words[2])); } else if (token.at(0) == 'D') { // Get Divider FU vDivRS.push_back(atoi(words[1])); vDivEC.push_back(atoi(words[2])); } else if (token.at(0) == 'L') { // Get Loader FU vLoadRS.push_back(atoi(words[1])); vLoadEC.push_back(atoi(words[2])); } else if (token.at(0) == 'S') { // Get Storer FU vStoreRS.push_back(atoi(words[1])); vStoreEC.push_back(atoi(words[2])); } else { // Initialize Memory array int limit = (words.size()+1 < SIZE_MEM)? words.size()+1 : SIZE_MEM; for (int i=1; i<limit; i++) { Mem[i-1] = atof(words[i]); } }
} // End while Loop infile.close();
if (DEBUG) { for (int x=0; x<vAddRS.size(); x++) { cout << "Adder" << x+1 << ": " << vAddRS[x] << ", " << vAddEC[x] << endl; }
for (int x=0; x<vMultRS.size(); x++) { cout << "Multiplier" << x+1 << ": " << vMultRS[x] << ", " << vMultEC[x] << endl; } }
// Setup the sizes: vector
int i,j,k;
for(i=0; i<NUM_FP_GPR; i++){//initialize Register Status Register_Stas[i].type=READY; Register_Stas[i].fu_in=EMPTY; Register_Stas[i].index=EMPTY; }
/* i can be adder ~store j vary from 0 ~ num-1 fus k vary from 0~ res_num-1 for each FU*/ for(i=0; i<NUM_FUS; i++){//initialize Reservation Stations. //;Num_FU[i]=2;//initialize number of fus (a,m,d,l,s) Num_FU[i]=vSizes[i];//initialize number of fus (a,m,d,l,s)
Res_Stas[i]=new struct Reservation_Sta*[Num_FU[i]];//[Num_FU[i]];// kind of fus Num_Res_Stas[i]=new int[Num_FU[i]];//5 kind of fus Exe_Cycs[i]=new int[Num_FU[i]];//5 kind of fus
for(j=0; j < Num_FU[i]; j++){ //;Num_Res_Stas[i][j]=2; //;Exe_Cycs[i][j]=4; switch(i) { case 0: Num_Res_Stas[i][j]=vAddRS[j]; Exe_Cycs[i][j]=vAddEC[j]; break; case 1:
//Initialize memory; init_mem(); PC=0; //the PC register. cycle=0;
for(int i=0;i<NUM_FUS;i++){ Num_Res_Stas[i]=NULL; Exe_Cycs[i]=NULL; Res_Stas[i]=NULL; } mem_busy=false;// true means there is a ins using the memory port div_busy=NULL;//true means there is a div ins using the fu
}//end of constructor.
//destructor TOMASULO::~TOMASULO(){ for(int i=0; i<NUM_FUS; i++){ if(NULL != Res_Stas[i]){ for(int j=0; j<Num_FU[i]; j++){ if(NULL !=Res_Stas[i][j]) delete[] Res_Stas[i][j]; } delete Res_Stas[i]; } if(NULL !=Num_Res_Stas[i]) delete[] Num_Res_Stas[i]; if(NULL !=Exe_Cycs[i]) delete[] Exe_Cycs[i]; } if(div_busy != NULL) delete[] div_busy; Intrs_Stas.~deque(); }//end of destructor.
/**
*******************************************************************/
void TOMASULO::print_stat() { // Declare our locals: struct Instruction_Sta is; // Use with Intrs_Stas deque
vector
// Let's open the output file first: ofstream outfile;
// Start output (check for 2 since odd incrementing in simu_run() -- added additional print_stat() to compensate for it): if (cycle == 1) { outfile.open(HTMLFILE.c_str(),ios::out); if (outfile.fail()) { cerr << "Couldn't open file: " << HTMLFILE << endl;