Docsity

TOMASULO Processor Architecture: Instruction Execution and Reservation Stations, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
CS203A Course Project: A Tomasulo
Algorithm Simulation Sketch
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.
•
Pipeline Configuration:
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:
1. FP adder which can perform both FP add and FP sub; pipelined
2. FP multiplier which performs FP multiplication; pipelined
3. FP divider which performs FP division; not pipelined
4. load unit; pipelined
5. store unit; pipelined
6. 32 general-purpose integer registers and 32 general-purpose floating-point
registers; infinite number of ports for each register file.
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31

Partial preview of the text

Download TOMASULO Processor Architecture: Instruction Execution and Reservation Stations and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CS203A Course Project: A Tomasulo

Algorithm Simulation Sketch

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.

  • Pipeline Configuration:

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:

  1. FP adder which can perform both FP add and FP sub; pipelined
  2. FP multiplier which performs FP multiplication; pipelined
  3. FP divider which performs FP division; not pipelined
  4. load unit; pipelined
  5. store unit; pipelined
  6. 32 general-purpose integer registers and 32 general-purpose floating-point registers; infinite number of ports for each register file.

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.

  • Instructions

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

  1. Divide the work among the group members. When you can distribute the load in a team efficiently, you can improve your time management and the quality of your project.
  2. Make plans ahead of time. Conquer the project step by step. For example, instruction parsing, input interface, and output display can all be done first and separately. Leave at least one week for final testing and debugging. Don't wait till last minute.

Source Code: /********************************************************************

  • File name: TOMASULO.h
  • This is a class declaration to simulate the Tomasulo algorithm
  • for an out-of-order execution pipeline architecture.
  • Authors:
  • Kan Liu
  • Piyush Ranjan Satapathy
  • Ricky J. Sethi
  • CS203A The University of California, Riverside
  • 11/15/ ****************************************************************/

#include #include using namespace std;

#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

/****************************************************************

  • File name: TOMASULO.cpp, v0.2.
  • This is a class to simulate the Tomasulo algorithm for an
  • out-of-order execution pipeline architecture.
  • Authors:
  • Kan Liu
  • Piyush Ranjan Satapathy
  • Ricky J. Sethi
  • CS203A The University of California, Riverside
  • 11/15/
  • TODO:
    • Add # Cycles in Memory, # of FUs to init

****************************************************************/

#include "TOMASULO.h" #include // Need to do tons of I/O #include // Make sure we use strings #include // Get the filestream stuff (in and out) #include // For stderr, etc. #include // For the maps #include // For the vectors using namespace std;

// 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: //============================================================== ======

/**

  • FUNCTION: dbg(msg)
  • DESCRIPTION: Local debug fxn
  • RECEIVE: Uses the following EXPLICIT parameters:
    • msg: string, message to print out
  • Uses the following IMPLICIT parameters:
    • ostream & cout
  • RETURN: EXPLICITLY returns:
    • nothing
  • IMPLICITLY returns:
    • none
  • OUTPUT: Prints out the msg (if active only)
  • PRECONDITION: msg is a valid string
  • POSTCONDITION: msg was printed

*******************************************************************/

void dbg(string msg) { //cout << msg << endl; //msg = ""; return; }

  • FUNCTION: parse_input_assembly()
  • DESCRIPTION: Parses the input assembly file and stores instructions in the deque, inst
  • RECEIVE: Uses the following EXPLICIT parameters:
    • filename: const char *, input assembly filename
  • Uses the following IMPLICIT parameters:
    • none
  • RETURN: EXPLICITLY returns:
    • bool, status
  • IMPLICITLY returns:
    • nothing
  • OUTPUT: Debugging statements
  • PRECONDITION: filename is a valid string and file exists and contains assembly instructions with format like "sd Fa, offset(Ra)"
  • POSTCONDITION: instructions from file are in the inst deque

*******************************************************************/

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; }

  • FUNCTION: init_from_file(const char *)
  • DESCRIPTION: Parses the initialization input file
  • RECEIVE: Uses the following EXPLICIT parameters:
    • const char * filename
  • Uses the following IMPLICIT parameters:
    • INITFILE
    • In_Gpr[NUM_IN_GPR]
    • Fp_Gpr[NUM_FP_GPR]
    • Mem[SIZE_MEM]
  • RETURN: EXPLICITLY returns:
    • bool, status
  • IMPLICITLY returns:
    • nothing
  • OUTPUT: Debugging statements
  • PRECONDITION: INITFILE is valid and is accessible and contains correct initialization strings (comma separated)
  • POSTCONDITION: The int reg, fp reg, and memory is initialized to specified values

*******************************************************************/

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 vAddRS; vector vAddEC; vector vMultRS; vector vMultEC; vector vDivRS; vector vDivEC; vector vLoadRS; vector vLoadEC; vector vStoreRS; vector vStoreEC;

// 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();

// DEBUG:

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 vSizes; vSizes.push_back(vAddRS.size()); vSizes.push_back(vMultRS.size()); vSizes.push_back(vDivRS.size()); vSizes.push_back(vLoadRS.size()); vSizes.push_back(vStoreRS.size());

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.

/**

  • FUNCTION: print_stat()
  • DESCRIPTION: Displays the reservation station status, instruction status, and the register result status for ONE cycle
  • RECEIVE: Uses the following EXPLICIT parameters:
    • none
  • Uses the following IMPLICIT parameters:
    • Intrs_Stas deque (Instruction Status)
    • Res_Stas array (Reservation Stations)
    • Register_Stas array (Register Status)
    • cycle integer
  • RETURN: EXPLICITLY returns:
    • nothing
  • IMPLICITLY returns:
    • nothing
  • OUTPUT: HTML file containing the output
  • PRECONDITION: The deques exist and we can write a file here
  • POSTCONDITION: The HTML file is created with the tables

*******************************************************************/

void TOMASULO::print_stat() { // Declare our locals: struct Instruction_Sta is; // Use with Intrs_Stas deque i = Intrs_Stas; // Copy Intrs_Stas deque vector vResStations; // Map integers to reservations tations vResStations.push_back("ADDER"); vResStations.push_back("MULTIPLIER"); vResStations.push_back("DIVIDER"); vResStations.push_back("LOAD"); vResStations.push_back("STORE");

vector vInstructions; // Map integers to reservations tations vInstructions.push_back("LD"); vInstructions.push_back("SD"); vInstructions.push_back("BEQ"); vInstructions.push_back("BNE"); vInstructions.push_back("ADD"); vInstructions.push_back("ADD_D"); vInstructions.push_back("ADDI"); vInstructions.push_back("SUB"); vInstructions.push_back("SUB_D"); vInstructions.push_back("MULT_D"); vInstructions.push_back("DIV_D");

// 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;