#include #include #define MAXLINELENGTH 1000 /* maximum size of machine language instr */ #define NUMMEMORY 10000 /* maximum number of data words in memory */ #define NUMREGS 8 /* number of machine registers */ #define ADD 0 #define NAND 1 #define LW 2 #define SW 3 #define BEQ 4 /* note that JALR will NOT be implemented for project 3 */ #define HALT 6 #define NOOP 7 #define NOOPINSTRUCTION 0x1c000; /* * 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 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>>11) & 0x7); } int field1(int instruction) { return( (instruction>>8) & 0x7); } int field2(int instruction) { return(instruction & 0xFF); } int opcode(int instruction) { return(instruction>>14); } void printInstruction(int instr) { char opcodeString[10]; if (opcode(instr) == ADD) { strcpy(opcodeString, "add"); } else if (opcode(instr) == NAND) { strcpy(opcodeString, "nand"); } else if (opcode(instr) == LW) { strcpy(opcodeString, "lw"); } else if (opcode(instr) == SW) { strcpy(opcodeString, "sw"); } else if (opcode(instr) == BEQ) { strcpy(opcodeString, "beq"); } else if (opcode(instr) == HALT) { strcpy(opcodeString, "halt"); } else if (opcode(instr) == NOOP) { strcpy(opcodeString, "noop"); } else { strcpy(opcodeString, "data"); } printf("%s %d %d %d\n", opcodeString, field0(instr), field1(instr), field2(instr)); } 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