Sei sulla pagina 1di 5

#include #include #include #include

<iostream> <fstream> <sstream> <string>

using namespace std; /*Binary, two's complement data representation. Stored program, fixed word length data and instructions. 4K words of word-addressable main memory. 16-bit data words. 16-bit instructions, 4 for the opcode and 12 for the address. A 16-bit arithmetic logic unit (ALU). Seven registers for control and data movement. */ string memory[4096]; //string alu; string MAR; string MBR; string AC; string IR; string inReg; string outReg; string PC = "0"; int ToInt(string convstr) { int number; stringstream tempstream; tempstream << convstr; tempstream >> number; return number; } string ToString(int convstr) { stringstream tempstream; tempstream << convstr; return tempstream.str(); } int HexToInt(string address) // limit 4096 { int result = 0; int temp = 0; int size = address.length()-1; int tmpHex = 2; int i = size,j=0; int NEG = 0; while(i >= 0) { tmpHex = address[j]; if(tmpHex >=65 && tmpHex <=70) temp = (tmpHex-55)*pow(16.0,i); // A - F else if(tmpHex >=97 && tmpHex <=102) temp = (tmpHex-55)*pow(16.0,i); // a - f else if(tmpHex >= 48 && tmpHex <=57 ) temp = (tmpHex-48)*pow(16.0,i); // 0 -

9 result=result+temp; i--; j++; } return result; } string IntToHex(string decstr) { string result; int dec = 0, temp = 2, neg = 0; string mod; int i = decstr.length()-1,j=0; int value,MIN = 0; if(decstr[0] == '-') { MIN = 1; } while(i >= MIN) { value = int(decstr[i]-48) * int(pow(10.0,j)); dec = dec + value; j++; i--; } if(decstr[0] == '-') { dec +=65538; } while(temp > 0) { if(dec%16 >=10) mod = (dec%16)+55; else mod = (dec%16)+48; result.insert(0,mod); temp = dec/16; dec = temp; } //reverse(result); return result; } void ExecuteCommand(string command[], int size) { string opCode="";//OpCode string address="";//IR int tempnum=0; string IR = ""; int IR1110 = 0;//IR[11-10] while(true) { opCode = ""; address= ""; // OpCode and address split //1 hex char for opcode 3 hex char for address opCode.append(1,command[HexToInt(PC)][0]); address.append(1,command[HexToInt(PC)][1]);

address.append(1,command[HexToInt(PC)][2]); address.append(1,command[HexToInt(PC)][3]); /* 0001 LOAD X //Load contents of address X into AC 0010 STORE X // store contents of AC into address x 0011 ADD X // add the contents of address X to AC 0100 SUBT X // subtract the contents of address X from AC 0101 INPUT // input value from keyboard into AC 0110 OUTPUT // output value of AC to display 0111 HALT // terminate program 1000 SKIPCOND // skip next instruction on condition 1001 JUMP // load value of X into PC */ if(opCode == "0"){;}else if(opCode == "1") { //Load(address); MAR=address; MBR=memory[HexToInt(MAR)]; AC=MBR; PC=IntToHex(ToString(HexToInt(PC)+1)); }else if(opCode == "2") {// Store(address); MAR=address; // MEMORY ADDRESS REG MBR=AC; memory[HexToInt(address)]=MBR; PC=IntToHex(ToString(HexToInt(PC)+1)); } else if(opCode == "3") {//Add(address); MAR=address; MBR=memory[HexToInt(MAR)]; tempnum = ToInt(AC)+ToInt(memory[HexToInt(address)]); AC=ToString(tempnum); PC=IntToHex(ToString(HexToInt(PC)+1)); }else if(opCode == "4") {// Subt(address); MAR=address; MBR=memory[HexToInt(MAR)]; tempnum = ToInt(AC) - ToInt(memory[HexToInt(address)]); AC=ToString(tempnum); PC=IntToHex(ToString(HexToInt(PC)+1)); }else if(opCode == "5") {// Input(); cin >> inReg; AC = inReg; PC=IntToHex(ToString(HexToInt(PC)+1)); }else if(opCode == "6") {//Output(); outReg=AC;

cout << outReg; PC=IntToHex(ToString(HexToInt(PC)+1)); }else if(opCode == "7") {//Halt(); return; }else if(opCode == "8") {// SkipCond(); /* SKIPCOND: OpCode:1000 If IR[11 - 10] = 00 then If AC < 0 then PC <- PC + 1 else If IR[11 - 10] = 01 then If AC = 0 then PC <- PC + 1 else If IR[11 - 10] = 10 then If AC > 0 then PC <- PC + 1 */ // MAR = address; //MAR or IR IR1110 = ((ToInt(MAR)/1024)); // captures bits 11 and 10 out of 12 bit code if(IR1110 == 0) { //00 if(ToInt(AC) < 0) PC=IntToHex(ToString(HexToInt(PC)+1)); }else if(IR1110 == 1) {//01 if(ToInt(AC) == 0) PC=IntToHex(ToString(HexToInt(PC)+1)); }else if(IR1110 == 2) {//10 if(ToInt(AC) > 0) PC=IntToHex(ToString(HexToInt(PC)+1)); } PC=IntToHex(ToString(HexToInt(PC)+1)); }else if(opCode == "9") {//Jump(address); MAR = address; PC = MAR; } } } int main(int argc, char *args[]) { int i = 0; //insert commands into memory // or int argc, char *argv[] if(argc <2) { cout << "" << endl;

cout cout cout cout cout cout }

<< << << << << <<

"MARIE Usage:" << endl; "command syntax: MARIEProg <filename>"<< endl << endl; "-----------------------------------------------------" << endl; "* <filename> : file containing HEX instructions " << endl; "File format is: ASCII TXT"<< endl; "Data/Instruction Format is: 0000 or up to FFFF"<<endl<<endl;

return 0; string datafile = args[1]; ifstream input; input.open(datafile.c_str()); if(input.is_open()) cout << "Welcome to the MARIE Simulator\nFILE OPENED AND REA DING\n"; else if(!input.is_open()) { string inputfile; cout << "instruction file: " << args[1] << " not found" << endl; exit(0); } string test; while(input.is_open()) { while(getline(input,test)) { i++; } input.close(); } string *command=new string[i+1]; int line = 0; input.open(datafile.c_str()); while(input.is_open()) { while(getline(input,command[line])) { line++; } input.close(); } ExecuteCommand(command,i); delete [] command; return 0; }

Potrebbero piacerti anche