[cpp] MIPS SIMULATION BY GAL and ITAMAR
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #define _CRT_SECURE_NO_WARNINGS
- ////////////////libraries///////////////////
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- ////////////////defines////////////////////
- #define FILE_ERROR "Unable to open file"
- #define MAX 50
- #define DONE "end of simulation"
- #define J 'J'
- #define I 'I'
- #define R 'R'
- #define l 'l'
- #define CHECK_HEAD(head)\
- if(head == NULL)\
- {\
- printf(" function got invalid pointer, exit ");\
- exit(1);\
- }
- ///////////////////////structs///////////////////////
- typedef struct {
- char* instruction_address;
- char* opcode;
- char* operand1; //rd
- char* operand2; //rs
- char* operand3; //rt
- char type;
- }Ins_sets;
- typedef struct {
- Ins_sets i_f;
- }I_F;
- typedef struct {
- Ins_sets id;
- }ID;
- typedef struct {
- Ins_sets ex;
- }EX;
- typedef struct {
- Ins_sets mem;
- }MEM;
- typedef struct {
- Ins_sets wb;
- }WB;
- typedef struct {
- //statges
- I_F i_f;
- ID id;
- EX ex;
- MEM mem;
- WB wb;
- //REGISTERS
- }PipeLine;
- ///////////////////function declaration///////////////////////
- Ins_sets* CreateInput(char* argv[]);
- void Simulation(char* argv[], Ins_sets*);
- char check_type(char*);
- bool is_WriteReg(char*);
- bool is_Jtype(char*);
- void inset_nop(Ins_sets*);
- bool jump(char, char*, char*);
- //////////////////////////main//////////////////////////
- int CYCLE = 0;
- int num_of_ins = 0;
- void main(int argc, char* argv[]) //notice argv[0] stand for the executable file's path
- {
- if (argc < 3) //trace(i) & Forwarding & Branch resolution
- {
- printf("Apparently not enough arguments were sent to main");
- exit(1);
- }
- Ins_sets* ins_sets = CreateInput(argv);
- Simulation(argv, ins_sets);
- system("pause");
- free(ins_sets);
- return 0;
- }
- ///////////////////function definitionn///////////////////////
- Ins_sets* CreateInput(char* argv[])
- {
- FILE* trace = fopen(argv[1], "r+");
- if (trace == NULL) {
- printf(FILE_ERROR);
- exit(1);
- }
- char c;
- int cycle = 0;
- int size_of_txt;
- int index_new_line = 0;
- char instruction_address[MAX];
- char opcode[MAX];
- char operand1[MAX];
- char operand2[MAX];
- char operand3[MAX];
- fseek(trace, 0, SEEK_END);
- size_of_txt = ftell(trace);
- // Extract characters from file and store in character c
- fseek(trace, 0, SEEK_SET);
- while (!feof(trace))
- {
- c = fgetc(trace);
- if (c == '\n')
- num_of_ins++;
- }
- //////////////////CREATE SETS INSRTUCTIONS////////////////// // doto:free
- fseek(trace, 0, SEEK_SET);
- int i = 0;
- Ins_sets* ins_sets = (Ins_sets*)calloc(num_of_ins, sizeof(Ins_sets));
- while (ftell(trace) < size_of_txt)
- {
- fseek(trace, index_new_line, SEEK_SET);
- fscanf(trace, "%s", instruction_address);//instruction_address
- ins_sets[i].instruction_address = (char*)malloc(strlen(instruction_address) + 1, sizeof(char));
- strcpy(ins_sets[i].instruction_address, instruction_address);
- fscanf(trace, "%s", opcode);//opcode
- ins_sets[i].type = check_type(opcode);
- ins_sets[i].opcode = (char*)malloc(strlen(opcode) + 1, sizeof(char));
- strcpy(ins_sets[i].opcode, opcode);
- fscanf(trace, "%s", operand1);//operand1
- ins_sets[i].operand1 = (char*)malloc(strlen(operand1) + 1, sizeof(char));
- strcpy(ins_sets[i].operand1, operand1);
- fscanf(trace, "%s", operand2);//operand2
- ins_sets[i].operand2 = (char*)malloc(strlen(operand2) + 1, sizeof(char));
- strcpy(ins_sets[i].operand2, operand2);
- fscanf(trace, "%s", operand3);//operand3
- ins_sets[i].operand3 = (char*)malloc(strlen(operand3) + 1, sizeof(char));
- strcpy(ins_sets[i].operand3, operand3);
- index_new_line = ftell(trace);
- i++;
- }
- if (fclose(trace) != 0)
- printf("error closing file");
- return ins_sets;
- }
- void Simulation(char* argv[], Ins_sets* ins_sets)
- {
- bool flag = false;
- int flag_b = 0;
- int not_taken = 0;
- bool Branch_resolution = atoi(argv[2]); // Branch resolution
- bool Forwarding = atoi(argv[3]); //Forwarding
- PipeLine* pipeline = (PipeLine*)calloc(1, sizeof(PipeLine));
- if (!Branch_resolution && !Forwarding) { //00
- for (int i = 0; i < num_of_ins + 5; i++)
- {
- int counter = 0;
- if (i <= num_of_ins + 4) {
- pipeline->wb.wb = pipeline->mem.mem;
- }
- if (i <= num_of_ins + 3) {
- pipeline->mem.mem = pipeline->ex.ex; //BRUNCH RESOLUTION IN MEM
- if (i < num_of_ins) {
- // forward_detect stall the pipe for 1 cyclse and hold the pc
- if ((ins_sets[i].type == J) && is_WriteReg(pipeline->id.id.opcode) && (!strcmp(pipeline->id.id.operand1, ins_sets[i].operand1) || !strcmp(pipeline->id.id.operand1, ins_sets[i].operand2)))
- {
- counter = 2;
- }
- if (is_WriteReg(pipeline->mem.mem.opcode) && pipeline->mem.mem.type != J && (!strcmp(pipeline->mem.mem.operand1, ins_sets[i].operand2) || !strcmp(pipeline->mem.mem.operand1, ins_sets[i].operand3)) && atoi(pipeline->mem.mem.operand1) != 0)
- {
- counter = 2;
- }
- }
- }
- if (i <= num_of_ins + 2) {
- if (pipeline->mem.mem.type == J && !not_taken)
- inset_nop(&pipeline->ex.ex);
- else pipeline->ex.ex = pipeline->id.id;
- if (i < num_of_ins) {
- // forward_detect condition - stall the pipe for 2 cyclse and hold the pc
- if ((ins_sets[i].type == J) && is_WriteReg(pipeline->id.id.opcode) && (!strcmp(pipeline->id.id.operand1, ins_sets[i].operand1) || !strcmp(pipeline->id.id.operand1, ins_sets[i].operand2)))
- {
- counter = 3;
- }
- if (is_WriteReg(pipeline->ex.ex.opcode) && pipeline->ex.ex.type != J && (!strcmp(pipeline->ex.ex.operand1, ins_sets[i].operand2) || !strcmp(pipeline->ex.ex.operand1, ins_sets[i].operand3))) // forward_detect stall the pipe for 2 cyclse and hold the pc
- {
- counter = 3;
- }
- }
- }
- if (i <= num_of_ins + 1) {
- if (pipeline->mem.mem.type == J && !not_taken) {
- inset_nop(&pipeline->id.id);
- }
- else pipeline->id.id = pipeline->i_f.i_f;
- if (i < num_of_ins) {
- // forward_detect condition - stall the pipe for 3 cyclse and hold the pc
- if ((ins_sets[i].type == J) && is_WriteReg(pipeline->id.id.opcode) && (!strcmp(pipeline->id.id.operand1, ins_sets[i].operand1) || !strcmp(pipeline->id.id.operand1, ins_sets[i].operand2)))
- {
- counter = 4;
- }
- if (is_WriteReg(pipeline->id.id.opcode) && pipeline->id.id.type != J && (!strcmp(pipeline->id.id.operand1, ins_sets[i].operand2) || !strcmp(pipeline->id.id.operand1, ins_sets[i].operand3))) // forward_detect condition - stall the pipe for 3 cyclse and hold the pc
- {
- counter = 4;
- }
- }
- }
- if (i <= num_of_ins) { //INSRUCTION FETCH
- if (pipeline->mem.mem.type == J && !not_taken) {
- inset_nop(&pipeline->i_f.i_f);
- flag_b = 3;
- }
- else if (i < num_of_ins && flag_b)
- {
- i -= 3;
- pipeline->i_f.i_f = ins_sets[i]; flag_b = 0;
- }
- else if (i < num_of_ins && flag_b == 0)
- pipeline->i_f.i_f = ins_sets[i];
- }
- //PRINTING STATE OF THE PIPE ACCORDING TO STALLS
- if (counter != 0) {
- for (int j = 0; j < counter; j++)
- {
- if (i <= num_of_ins + 3) printf("Cycle %d\n", ++CYCLE);
- if (i <= num_of_ins - 1)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Fetch instruction: %s %s %s %s %s\n", pipeline->i_f.i_f.instruction_address, pipeline->i_f.i_f.opcode, pipeline->i_f.i_f.operand1, pipeline->i_f.i_f.operand2, pipeline->i_f.i_f.operand3);
- if (i <= num_of_ins)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Decode instruction: %s %s %s %s %s\n", pipeline->id.id.instruction_address, pipeline->id.id.opcode, pipeline->id.id.operand1, pipeline->id.id.operand2, pipeline->id.id.operand3);
- if (i <= num_of_ins + 1)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Execute instruction: %s %s %s %s %s\n", pipeline->ex.ex.instruction_address, pipeline->ex.ex.opcode, pipeline->ex.ex.operand1, pipeline->ex.ex.operand2, pipeline->ex.ex.operand3);
- if (i <= num_of_ins + 2)
- printf("Memory instruction: %s %s %s %s %s\n", pipeline->mem.mem.instruction_address, pipeline->mem.mem.opcode, pipeline->mem.mem.operand1, pipeline->mem.mem.operand2, pipeline->mem.mem.operand3);
- if (i <= num_of_ins + 3)
- printf("Writeback instruction: %s %s %s %s %s\n", pipeline->wb.wb.instruction_address, pipeline->wb.wb.opcode, pipeline->wb.wb.operand1, pipeline->wb.wb.operand2, pipeline->wb.wb.operand3);
- flag = jump(pipeline->i_f.i_f.type, pipeline->i_f.i_f.instruction_address, ins_sets[i + 1].instruction_address);
- if (flag) not_taken++;
- printf("\n");
- pipeline->wb.wb = pipeline->mem.mem;
- pipeline->mem.mem = pipeline->ex.ex;
- pipeline->ex.ex = pipeline->id.id;
- inset_nop(&pipeline->id.id);
- }
- }
- else
- {
- if (i <= num_of_ins + 3) printf("Cycle %d\n", ++CYCLE);
- if (i <= num_of_ins - 1)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Fetch instruction: %s %s %s %s %s\n", pipeline->i_f.i_f.instruction_address, pipeline->i_f.i_f.opcode, pipeline->i_f.i_f.operand1, pipeline->i_f.i_f.operand2, pipeline->i_f.i_f.operand3);
- if (i <= num_of_ins)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Decode instruction: %s %s %s %s %s\n", pipeline->id.id.instruction_address, pipeline->id.id.opcode, pipeline->id.id.operand1, pipeline->id.id.operand2, pipeline->id.id.operand3);
- if (i <= num_of_ins + 1)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Execute instruction: %s %s %s %s %s\n", pipeline->ex.ex.instruction_address, pipeline->ex.ex.opcode, pipeline->ex.ex.operand1, pipeline->ex.ex.operand2, pipeline->ex.ex.operand3);
- if (i <= num_of_ins + 2)
- printf("Memory instruction: %s %s %s %s %s\n", pipeline->mem.mem.instruction_address, pipeline->mem.mem.opcode, pipeline->mem.mem.operand1, pipeline->mem.mem.operand2, pipeline->mem.mem.operand3);
- if (i <= num_of_ins + 3)
- printf("Writeback instruction: %s %s %s %s %s\n", pipeline->wb.wb.instruction_address, pipeline->wb.wb.opcode, pipeline->wb.wb.operand1, pipeline->wb.wb.operand2, pipeline->wb.wb.operand3);
- flag = jump(pipeline->i_f.i_f.type, pipeline->i_f.i_f.instruction_address, ins_sets[i + 1].instruction_address);
- if (flag) not_taken++;
- printf("\n");
- }
- }
- printf("\n CPI = %f\n", (float)CYCLE / num_of_ins);
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- else if (Branch_resolution && !Forwarding) //10
- {
- for (int i = 0; i < num_of_ins + 5; i++)
- {
- int counter = 0;
- if (i <= num_of_ins + 4) {
- pipeline->wb.wb = pipeline->mem.mem;
- }
- if (i <= num_of_ins + 3) {
- pipeline->mem.mem = pipeline->ex.ex;
- if (i < num_of_ins) {
- // forward_detect stall the pipe for 1 cyclse and hold the pc
- if ((ins_sets[i].type == J) && is_WriteReg(pipeline->id.id.opcode) && (!strcmp(pipeline->id.id.operand1, ins_sets[i].operand1) || !strcmp(pipeline->id.id.operand1, ins_sets[i].operand2)))
- {
- counter = 2;
- }
- if (is_WriteReg(pipeline->mem.mem.opcode) && pipeline->mem.mem.type != J && (!strcmp(pipeline->mem.mem.operand1, ins_sets[i].operand2) || !strcmp(pipeline->mem.mem.operand1, ins_sets[i].operand3)) && atoi(pipeline->mem.mem.operand1) !=0)
- {
- counter = 2;
- }
- }
- }
- if (i <= num_of_ins + 2) {
- pipeline->ex.ex = pipeline->id.id;
- if (i < num_of_ins) {
- // forward_detect condition - stall the pipe for 2 cyclse and hold the pc
- if ((ins_sets[i].type == J) && is_WriteReg(pipeline->id.id.opcode) && (!strcmp(pipeline->id.id.operand1, ins_sets[i].operand1) || !strcmp(pipeline->id.id.operand1, ins_sets[i].operand2)))
- {
- counter = 3;
- }
- if (is_WriteReg(pipeline->ex.ex.opcode) && pipeline->ex.ex.type != J && (!strcmp(pipeline->ex.ex.operand1, ins_sets[i].operand2) || !strcmp(pipeline->ex.ex.operand1, ins_sets[i].operand3)) && atoi(pipeline->ex.ex.operand1)!=0) // forward_detect stall the pipe for 2 cyclse and hold the pc
- {
- counter = 3;
- }
- }
- }
- if (i <= num_of_ins + 1) {
- pipeline->id.id = pipeline->i_f.i_f; //BRUNCH RESOLUTION IN ID
- if (i < num_of_ins) {
- // forward_detect condition - stall the pipe for 3 cyclse and hold the pc
- if ((ins_sets[i].type == J) && is_WriteReg(pipeline->id.id.opcode) && (!strcmp(pipeline->id.id.operand1, ins_sets[i].operand1) || !strcmp(pipeline->id.id.operand1, ins_sets[i].operand2)))
- {
- counter = 4;
- }
- if (is_WriteReg(pipeline->id.id.opcode) && pipeline->id.id.type != J && (!strcmp(pipeline->id.id.operand1, ins_sets[i].operand2) || !strcmp(pipeline->id.id.operand1, ins_sets[i].operand3)) && atoi(pipeline->id.id.operand1)!=0)
- {
- counter = 4;
- }
- }
- }
- if (i <= num_of_ins) { //INSRUCTION FETCH
- if (pipeline->id.id.type == J && !not_taken) {
- inset_nop(&pipeline->i_f.i_f);
- flag_b = 1;
- }
- else if (i < num_of_ins && flag_b == 1)
- {
- i--;
- pipeline->i_f.i_f = ins_sets[i]; flag_b = 0;
- }
- else if (i < num_of_ins && flag_b == 0)
- pipeline->i_f.i_f = ins_sets[i];
- }
- //PRINTING STATE OF THE PIPE ACCORDING TO STALLS
- if (counter != 0) {
- for (int j = 0; j < counter; j++)
- {
- if (i <= num_of_ins + 3) printf("Cycle %d\n", ++CYCLE);
- if (i <= num_of_ins - 1)
- pipeline->id.id.type == J && !not_taken ? printf("FLUSH\n") : printf("Fetch instruction: %s %s %s %s %s\n", pipeline->i_f.i_f.instruction_address, pipeline->i_f.i_f.opcode, pipeline->i_f.i_f.operand1, pipeline->i_f.i_f.operand2, pipeline->i_f.i_f.operand3);
- if (i <= num_of_ins)
- printf("Decode instruction: %s %s %s %s %s\n", pipeline->id.id.instruction_address, pipeline->id.id.opcode, pipeline->id.id.operand1, pipeline->id.id.operand2, pipeline->id.id.operand3);
- if (i <= num_of_ins + 1)
- printf("Execute instruction: %s %s %s %s %s\n", pipeline->ex.ex.instruction_address, pipeline->ex.ex.opcode, pipeline->ex.ex.operand1, pipeline->ex.ex.operand2, pipeline->ex.ex.operand3);
- if (i <= num_of_ins + 2)
- printf("Memory instruction: %s %s %s %s %s\n", pipeline->mem.mem.instruction_address, pipeline->mem.mem.opcode, pipeline->mem.mem.operand1, pipeline->mem.mem.operand2, pipeline->mem.mem.operand3);
- if (i <= num_of_ins + 3)
- printf("Writeback instruction: %s %s %s %s %s\n", pipeline->wb.wb.instruction_address, pipeline->wb.wb.opcode, pipeline->wb.wb.operand1, pipeline->wb.wb.operand2, pipeline->wb.wb.operand3);
- flag = jump(pipeline->i_f.i_f.type, pipeline->i_f.i_f.instruction_address, ins_sets[i + 1].instruction_address);
- if (flag) not_taken++;
- printf("\n");
- pipeline->wb.wb = pipeline->mem.mem;
- pipeline->mem.mem = pipeline->ex.ex;
- pipeline->ex.ex = pipeline->id.id;
- inset_nop(&pipeline->id.id);
- }
- }
- else
- {
- if (i <= num_of_ins + 3) printf("Cycle %d\n", ++CYCLE);
- if (i <= num_of_ins - 1)
- pipeline->id.id.type == J && !not_taken ? printf("FLUSH\n") : printf("Fetch instruction: %s %s %s %s %s\n", pipeline->i_f.i_f.instruction_address, pipeline->i_f.i_f.opcode, pipeline->i_f.i_f.operand1, pipeline->i_f.i_f.operand2, pipeline->i_f.i_f.operand3);
- if (i <= num_of_ins)
- printf("Decode instruction: %s %s %s %s %s\n", pipeline->id.id.instruction_address, pipeline->id.id.opcode, pipeline->id.id.operand1, pipeline->id.id.operand2, pipeline->id.id.operand3);
- if (i <= num_of_ins + 1)
- printf("Execute instruction: %s %s %s %s %s\n", pipeline->ex.ex.instruction_address, pipeline->ex.ex.opcode, pipeline->ex.ex.operand1, pipeline->ex.ex.operand2, pipeline->ex.ex.operand3);
- if (i <= num_of_ins + 2)
- printf("Memory instruction: %s %s %s %s %s\n", pipeline->mem.mem.instruction_address, pipeline->mem.mem.opcode, pipeline->mem.mem.operand1, pipeline->mem.mem.operand2, pipeline->mem.mem.operand3);
- if (i <= num_of_ins + 3)
- printf("Writeback instruction: %s %s %s %s %s\n", pipeline->wb.wb.instruction_address, pipeline->wb.wb.opcode, pipeline->wb.wb.operand1, pipeline->wb.wb.operand2, pipeline->wb.wb.operand3);
- flag = jump(pipeline->i_f.i_f.type, pipeline->i_f.i_f.instruction_address, ins_sets[i + 1].instruction_address);
- if (flag) not_taken++;
- printf("\n");
- }
- }
- printf("\n CPI = %f\n", (float)CYCLE / num_of_ins);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- else if (!Branch_resolution && Forwarding) // 01
- {
- for (int i = 0; i < num_of_ins + 5; i++)
- {
- int counter = 0;
- if (i <= num_of_ins + 4) {
- pipeline->wb.wb = pipeline->mem.mem;
- }
- if (i <= num_of_ins + 3) {
- pipeline->mem.mem = pipeline->ex.ex; //BRUNCH RESOLUTION IN MEM
- }
- if (i <= num_of_ins + 2) {
- if (pipeline->mem.mem.type == J && !not_taken)
- inset_nop(&pipeline->ex.ex);
- else pipeline->ex.ex = pipeline->id.id;
- }
- if (i <= num_of_ins + 1) {
- if (pipeline->mem.mem.type == J && !not_taken) {
- inset_nop(&pipeline->id.id);
- }
- else pipeline->id.id = pipeline->i_f.i_f;
- if (i < num_of_ins) {
- //ASSUMIMG UNCASUAL FORWARDING NEED
- if (is_WriteReg(ins_sets[i].opcode) && pipeline->id.id.type == l && (!strcmp(pipeline->id.id.operand1, ins_sets[i].operand2) || !strcmp(pipeline->id.id.operand1, ins_sets[i].operand3) && atoi(pipeline->id.id.operand1)!=0)) // forward_detect condition - stall the pipe for 3 cyclse and hold the pc
- {
- counter = 2;
- }
- }
- }
- if (i <= num_of_ins) { //INSRUCTION FETCH
- if (pipeline->mem.mem.type == J && !not_taken) {
- inset_nop(&pipeline->i_f.i_f);
- flag_b = 3;
- }
- else if (i < num_of_ins && flag_b)
- {
- i -= 3;
- pipeline->i_f.i_f = ins_sets[i]; flag_b = 0;
- }
- else if (i < num_of_ins && flag_b == 0)
- pipeline->i_f.i_f = ins_sets[i];
- }
- //PRINTING STATE OF THE PIPE ACCORDING TO STALLS
- if (counter != 0) {
- for (int j = 0; j < counter; j++)
- {
- if (i <= num_of_ins + 3) printf("Cycle %d\n", ++CYCLE);
- if (i <= num_of_ins - 1)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Fetch instruction: %s %s %s %s %s\n", pipeline->i_f.i_f.instruction_address, pipeline->i_f.i_f.opcode, pipeline->i_f.i_f.operand1, pipeline->i_f.i_f.operand2, pipeline->i_f.i_f.operand3);
- if (i <= num_of_ins)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Decode instruction: %s %s %s %s %s\n", pipeline->id.id.instruction_address, pipeline->id.id.opcode, pipeline->id.id.operand1, pipeline->id.id.operand2, pipeline->id.id.operand3);
- if (i <= num_of_ins + 1)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Execute instruction: %s %s %s %s %s\n", pipeline->ex.ex.instruction_address, pipeline->ex.ex.opcode, pipeline->ex.ex.operand1, pipeline->ex.ex.operand2, pipeline->ex.ex.operand3);
- if (i <= num_of_ins + 2)
- printf("Memory instruction: %s %s %s %s %s\n", pipeline->mem.mem.instruction_address, pipeline->mem.mem.opcode, pipeline->mem.mem.operand1, pipeline->mem.mem.operand2, pipeline->mem.mem.operand3);
- if (i <= num_of_ins + 3)
- printf("Writeback instruction: %s %s %s %s %s\n", pipeline->wb.wb.instruction_address, pipeline->wb.wb.opcode, pipeline->wb.wb.operand1, pipeline->wb.wb.operand2, pipeline->wb.wb.operand3);
- flag = jump(pipeline->i_f.i_f.type, pipeline->i_f.i_f.instruction_address, ins_sets[i + 1].instruction_address);
- if (flag) not_taken++;
- printf("\n");
- if (j == 0) {
- pipeline->wb.wb = pipeline->mem.mem;
- pipeline->mem.mem = pipeline->ex.ex;
- pipeline->ex.ex = pipeline->id.id;
- inset_nop(&pipeline->id.id);
- }
- }
- }
- else
- {
- if (i <= num_of_ins + 3) printf("Cycle %d\n", ++CYCLE);
- if (i <= num_of_ins - 1)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Fetch instruction: %s %s %s %s %s\n", pipeline->i_f.i_f.instruction_address, pipeline->i_f.i_f.opcode, pipeline->i_f.i_f.operand1, pipeline->i_f.i_f.operand2, pipeline->i_f.i_f.operand3);
- if (i <= num_of_ins)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Decode instruction: %s %s %s %s %s\n", pipeline->id.id.instruction_address, pipeline->id.id.opcode, pipeline->id.id.operand1, pipeline->id.id.operand2, pipeline->id.id.operand3);
- if (i <= num_of_ins + 1)
- pipeline->mem.mem.type == J && !not_taken ? printf("FLUSH\n") : printf("Execute instruction: %s %s %s %s %s\n", pipeline->ex.ex.instruction_address, pipeline->ex.ex.opcode, pipeline->ex.ex.operand1, pipeline->ex.ex.operand2, pipeline->ex.ex.operand3);
- if (i <= num_of_ins + 2)
- printf("Memory instruction: %s %s %s %s %s\n", pipeline->mem.mem.instruction_address, pipeline->mem.mem.opcode, pipeline->mem.mem.operand1, pipeline->mem.mem.operand2, pipeline->mem.mem.operand3);
- if (i <= num_of_ins + 3)
- printf("Writeback instruction: %s %s %s %s %s\n", pipeline->wb.wb.instruction_address, pipeline->wb.wb.opcode, pipeline->wb.wb.operand1, pipeline->wb.wb.operand2, pipeline->wb.wb.operand3);
- flag = jump(pipeline->i_f.i_f.type, pipeline->i_f.i_f.instruction_address, ins_sets[i + 1].instruction_address);
- if (flag) not_taken++;
- printf("\n");
- }
- }
- printf("\n CPI = %f\n", (float)CYCLE / num_of_ins);
- }
- else if (Branch_resolution && Forwarding) // 11
- {
- for (int i = 0; i < num_of_ins + 5; i++)
- {
- int counter = 0;
- if (i <= num_of_ins + 4) {
- pipeline->wb.wb = pipeline->mem.mem;
- }
- if (i <= num_of_ins + 3) {
- pipeline->mem.mem = pipeline->ex.ex; //BRUNCH RESOLUTION IN MEM
- }
- if (i <= num_of_ins + 2) {
- pipeline->ex.ex = pipeline->id.id;
- }
- if (i <= num_of_ins + 1) {
- pipeline->id.id = pipeline->i_f.i_f;
- if (i < num_of_ins) {
- if (is_WriteReg(ins_sets[i].opcode) && pipeline->id.id.type == l && (!strcmp(pipeline->id.id.operand1, ins_sets[i].operand2) || !strcmp(pipeline->id.id.operand1, ins_sets[i].operand3) && (atoi(pipeline->id.id.operand1) != 0))) // forward_detect condition - stall the pipe for 3 cyclse and hold the pc
- {
- counter = 2;
- }
- }
- }
- if (i <= num_of_ins) { //INSRUCTION FETCH
- if (pipeline->id.id.type == J && !not_taken) {
- inset_nop(&pipeline->i_f.i_f);
- flag_b = 1;
- }
- else if (i < num_of_ins && flag_b)
- {
- i--;
- pipeline->i_f.i_f = ins_sets[i]; flag_b = 0;
- }
- else if (i < num_of_ins && flag_b == 0)
- pipeline->i_f.i_f = ins_sets[i];
- }
- //PRINTING STATE OF THE PIPE ACCORDING TO STALLS
- if (counter != 0) {
- for (int j = 0; j < counter; j++)
- {
- if (i <= num_of_ins + 3) printf("Cycle %d\n", ++CYCLE);
- if (i <= num_of_ins - 1)
- pipeline->id.id.type == J && !not_taken ? printf("FLUSH\n") : printf("Fetch instruction: %s %s %s %s %s\n", pipeline->i_f.i_f.instruction_address, pipeline->i_f.i_f.opcode, pipeline->i_f.i_f.operand1, pipeline->i_f.i_f.operand2, pipeline->i_f.i_f.operand3);
- if (i <= num_of_ins)
- printf("Decode instruction: %s %s %s %s %s\n", pipeline->id.id.instruction_address, pipeline->id.id.opcode, pipeline->id.id.operand1, pipeline->id.id.operand2, pipeline->id.id.operand3);
- if (i <= num_of_ins + 1)
- printf("Execute instruction: %s %s %s %s %s\n", pipeline->ex.ex.instruction_address, pipeline->ex.ex.opcode, pipeline->ex.ex.operand1, pipeline->ex.ex.operand2, pipeline->ex.ex.operand3);
- if (i <= num_of_ins + 2)
- printf("Memory instruction: %s %s %s %s %s\n", pipeline->mem.mem.instruction_address, pipeline->mem.mem.opcode, pipeline->mem.mem.operand1, pipeline->mem.mem.operand2, pipeline->mem.mem.operand3);
- if (i <= num_of_ins + 3)
- printf("Writeback instruction: %s %s %s %s %s\n", pipeline->wb.wb.instruction_address, pipeline->wb.wb.opcode, pipeline->wb.wb.operand1, pipeline->wb.wb.operand2, pipeline->wb.wb.operand3);
- flag = jump(pipeline->i_f.i_f.type, pipeline->i_f.i_f.instruction_address, ins_sets[i + 1].instruction_address);
- if (flag) not_taken++;
- printf("\n");
- if (j == 0) {
- pipeline->wb.wb = pipeline->mem.mem;
- pipeline->mem.mem = pipeline->ex.ex;
- pipeline->ex.ex = pipeline->id.id;
- inset_nop(&pipeline->id.id);
- }
- }
- }
- else
- {
- if (i <= num_of_ins + 3) printf("Cycle %d\n", ++CYCLE);
- if (i <= num_of_ins - 1)
- pipeline->id.id.type == J && !not_taken ? printf("FLUSH\n") : printf("Fetch instruction: %s %s %s %s %s\n", pipeline->i_f.i_f.instruction_address, pipeline->i_f.i_f.opcode, pipeline->i_f.i_f.operand1, pipeline->i_f.i_f.operand2, pipeline->i_f.i_f.operand3);
- if (i <= num_of_ins)
- printf("Decode instruction: %s %s %s %s %s\n", pipeline->id.id.instruction_address, pipeline->id.id.opcode, pipeline->id.id.operand1, pipeline->id.id.operand2, pipeline->id.id.operand3);
- if (i <= num_of_ins + 1)
- printf("Execute instruction: %s %s %s %s %s\n", pipeline->ex.ex.instruction_address, pipeline->ex.ex.opcode, pipeline->ex.ex.operand1, pipeline->ex.ex.operand2, pipeline->ex.ex.operand3);
- if (i <= num_of_ins + 2)
- printf("Memory instruction: %s %s %s %s %s\n", pipeline->mem.mem.instruction_address, pipeline->mem.mem.opcode, pipeline->mem.mem.operand1, pipeline->mem.mem.operand2, pipeline->mem.mem.operand3);
- if (i <= num_of_ins + 3)
- printf("Writeback instruction: %s %s %s %s %s\n", pipeline->wb.wb.instruction_address, pipeline->wb.wb.opcode, pipeline->wb.wb.operand1, pipeline->wb.wb.operand2, pipeline->wb.wb.operand3);
- flag = jump(pipeline->i_f.i_f.type, pipeline->i_f.i_f.instruction_address, ins_sets[i + 1].instruction_address);
- if (flag) not_taken++;
- printf("\n");
- }
- }
- printf("\n CPI = %f\n", (float)CYCLE / num_of_ins);
- }
- free(pipeline);
- }
- char check_type(char* opcode)
- {
- //add, sub, or , and, addi, subi, ori, andi, lw, sw, j, beq, bneq
- if (!strcmp(opcode, "j") || !strcmp(opcode, "beq") || !strcmp(opcode, "bneq"))
- return J;
- if (!strcmp(opcode, "addi") || !strcmp(opcode, "subi") || !strcmp(opcode, "ori") || !strcmp(opcode, "andi") || !strcmp(opcode, "sw"))
- return I;
- if (!strcmp(opcode, "add") || !strcmp(opcode, "sub") || !strcmp(opcode, "or") || !strcmp(opcode, "and"))
- return R;
- if (!strcmp(opcode, "lw"))
- return l;
- }
- void inset_nop(Ins_sets* ins)
- {
- ins->instruction_address = NULL;
- ins->opcode = NULL;
- ins->operand1 = NULL;
- ins->operand2 = NULL;
- ins->operand3 = NULL;
- ins->type = 'k';
- }
- bool is_WriteReg(char* opcode)
- {
- if (opcode) {
- if (!strcmp(opcode, "addi") || !strcmp(opcode, "subi") || !strcmp(opcode, "ori") || !strcmp(opcode, "andi"))
- return true;
- if (!strcmp(opcode, "add") || !strcmp(opcode, "sub") || !strcmp(opcode, "or") || !strcmp(opcode, "and") || !strcmp(opcode, "lw"))
- return true;
- if (!strcmp(opcode, "bneq") || !strcmp(opcode, "beq") || !strcmp(opcode, "j"))
- return true;
- }
- return false;
- }
- bool is_Jtype(char* opcode)
- {
- return (!strcmp(opcode, "bneq") || !strcmp(opcode, "beq") || !strcmp(opcode, "j"));
- }
- bool jump(char type, char* fetch_instruction_address, char* PC_instruction_address) {
- return (type == J && atoi(fetch_instruction_address) + 4 == atoi(PC_instruction_address)); // IF TRUE DO NOT JUMP
- }
Editor
You can edit this paste and save as new: