#include #include #define MAXLINELENGTH 1000 /* maximum size of machine language instr */ #define NUMMEMORY 10000 /* maximum number of data words in memory */ #define NUMREGS 32 /* number of machine registers */ #define regRegALU 0 /* opcode for register-to-register ALU operations */ #define LW 35 #define SW 43 #define ADDI 8 #define ANDI 12 #define BEQZ 4 #define J 2 #define HALT 1 #define NOOP 3 #define addFunc 32 /* function codes for ALU operations */ #define subFunc 34 #define andFunc 36 #define NOOPINSTRUCTION 0x0c000000; /* * Data Structures for Pipeline Registers */ typedef struct IFIDStruct { int instr; int pcPlus1; } IFIDType; typedef struct IDEXStruct { int instr; int pcPlus1; int readRegA; int readRegB; int immediate; } IDEXType; typedef struct EXMEMStruct { int instr; int branchTarget; int branchCond; int aluOutput; int readRegB; } EXMEMType; typedef struct MEMWBStruct { int instr; int loadMemData; int aluOutput; } MEMWBType; typedef struct WBENDStruct { int instr; int writeRegData; } WBENDType; /* * Data structures for machine state */ typedef struct stateStruct { int pc; int instrMem[NUMMEMORY]; int dataMem[NUMMEMORY]; int reg[NUMREGS]; int numMemory; IFIDType IFID; IDEXType IDEX; EXMEMType EXMEM; MEMWBType MEMWB; WBENDType WBEND; int cycles; /* number of cycles run so far */ } stateType; /* * Functions for extracting individual fields from instructions */ int field0(int instruction) { return( (instruction>>21) & 0x1f); } int field1(int instruction) { return( (instruction>>16) & 0x1f); } int field2(int instruction) { return((instruction >> 11) & 0x1f); } int immediate(int instruction) { return(instruction & 0xffff); } int jumpAddr(int instruction) { return(instruction & 0x3ffffff); } int opcode(int instruction) { return((instruction>>26) & 0x3f); } int func( int instruction) { return(instruction & 0x7ff); } void printInstruction(int instr) { char opcodeString[10]; char funcString[11]; int funcCode; int op; if (opcode(instr) == regRegALU) { funcCode = func(instr); if (funcCode == addFunc){ strcpy(opcodeString, "add"); } else if (funcCode == subFunc){ strcpy(opcodeString, "sub"); } else if (funcCode == andFunc){ strcpy(opcodeString, "and"); } else { strcpy(opcodeString, "alu"); } printf("%s %d %d %d \n", opcodeString, field0(instr), field1(instr), field2(instr)); } else if (opcode(instr) == LW) { strcpy(opcodeString, "lw"); printf("%s %d %d %d\n", opcodeString, field0(instr), field1(instr), immediate(instr)); } else if (opcode(instr) == SW) { strcpy(opcodeString, "sw"); printf("%s %d %d %d\n", opcodeString, field0(instr), field1(instr), immediate(instr)); } else if (opcode(instr) == ADDI) { strcpy(opcodeString, "addi"); printf("%s %d %d %d\n", opcodeString, field0(instr), field1(instr), immediate(instr)); } else if (opcode(instr) == ANDI) { strcpy(opcodeString, "andi"); printf("%s %d %d %d\n", opcodeString, field0(instr), field1(instr), immediate(instr)); } else if (opcode(instr) == BEQZ) { strcpy(opcodeString, "beqz"); printf("%s %d %d %d\n", opcodeString, field0(instr), field1(instr), immediate(instr)); } else if (opcode(instr) == J) { strcpy(opcodeString, "j"); printf("%s %d\n", opcodeString, jumpAddr(instr)); #ifdef DEBUG printf("jumpAddress offset = 0x%x\n", jumpAddr(instr)); #endif } else if (opcode(instr) == HALT) { strcpy(opcodeString, "halt"); printf("%s\n", opcodeString); } else if (opcode(instr) == NOOP) { strcpy(opcodeString, "noop"); printf("%s\n", opcodeString); } else { strcpy(opcodeString, "data"); printf("%s %d\n", opcodeString, instr); } } /* * Print current state of simulated machine */ void printState(stateType state) { int i; printf("@@@\nstate before cycle %d starts\n", state.cycles); printf("\tpc %d\n", state.pc); printf("\tdata memory:\n"); for (i=0; i\n", argv[0]); exit(1); } filePtr = fopen(argv[1], "r"); if (filePtr == NULL) { printf("error: can't open file %s", argv[1]); perror("fopen"); exit(1); } /* * Read the entire machine-code file into memory * Note that we read instructions and data values * into both instruction and data memories. We * will only update data memory on store operations. */ for (i=0; i