Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //On my honor, I have neither given nor received unauthorized aid on this assignment
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <cmath>
- #include <math.h>
- #include <stdlib.h>
- using namespace std;
- string convertBinaryToDec(string binNum){ //2's complement to decimal
- unsigned int sum = 0;
- for(int i = 0; i < binNum.length(); i++){
- if(binNum.at(i) == '1'){
- sum += pow(2, binNum.length() - 1 - i);
- }
- }
- if (binNum.at(0) == '1' && binNum.length() != 5){ //if its 16 bits, add 1's to the front
- for(int k = binNum.length(); k < 32; k++){
- sum += pow(2,k); //add 1s to the front
- }
- }
- int val = (int) sum;
- stringstream sumString;
- sumString << val;
- string final = sumString.str();
- return final;
- //return (to_string(val)); //-std=c++11 is giving me problems.... sigh...
- }
- string NOP(){ //CATEGORY 1
- return "NOP";
- }
- string J(string s){
- string targetAddress = convertBinaryToDec(s.substr(6,26));
- int targetAdd;
- istringstream (targetAddress) >> targetAdd;
- targetAdd *= 4;
- stringstream temp;
- temp << targetAdd;
- string jNum= temp.str();
- string ins = "J #" + jNum;
- return ins;
- }
- string BEQ(string s){
- string s1 = s.substr(6,5);
- string s2 = s.substr(11,5);
- string offset = s.substr(16,16);
- string ins = "BEQ R";
- ins += convertBinaryToDec(s1) + ", R" + convertBinaryToDec(s2) + ", #" + convertBinaryToDec(offset);
- return ins;
- }
- string BNE(string s){
- string s1 = s.substr(6,5);
- string s2 = s.substr(11,5);
- string offset = s.substr(16,16);
- string ins = "BNE R";
- ins += convertBinaryToDec(s1) + ", R" + convertBinaryToDec(s2) + ", #" + convertBinaryToDec(offset);
- return ins;
- }
- string BGTZ(string s){
- string s1 = s.substr(6,5);
- string offset = s.substr(16,16);
- string ins = "BGTZ R";
- ins += convertBinaryToDec(s1) + ", #" + convertBinaryToDec(offset);
- return ins;
- }
- string SW(string s){
- string base = s.substr(6,5);
- string rt = s.substr(11,5);
- string offset = s.substr(16,16);
- string ins = "SW R";
- ins += convertBinaryToDec(rt) + ", " + convertBinaryToDec(offset) + "(R" +convertBinaryToDec(base) + ")";
- return ins;
- }
- string LW(string s){
- string base = s.substr(6,5);
- string rt = s.substr(11,5);
- string offset = s.substr(16,16);
- string ins = "LW R";
- ins += convertBinaryToDec(rt) + ", " + convertBinaryToDec(offset) + "(R" +convertBinaryToDec(base) + ")";
- return ins;
- }
- string BREAK(){
- return "BREAK";
- }
- string category2(string s){ //CATEGORY 2
- string d = s.substr(6,5);
- string s1 = s.substr(11,5);
- string s2 = s.substr(16,5);
- string ins = "";
- ins += convertBinaryToDec(d) + ", R" + convertBinaryToDec(s1) + ", R" + convertBinaryToDec(s2);
- return ins;
- }
- string category3(string s){ //CATEGORY 3
- string opcode = s.substr(3,3);
- string d = s.substr(6,5);
- string s1 = s.substr(11,5);
- string imm = s.substr(16,16);
- string shiftAmt = s.substr(27,5);
- string ins = "";
- ins += convertBinaryToDec(d) + ", R" + convertBinaryToDec(s1) + ", #";
- if (opcode == "101" || opcode == "110" || opcode == "111"){ //if it's a shift (SRL, SRA, or SLL) use shift amount
- ins += convertBinaryToDec(shiftAmt);
- }
- else if(opcode == "000" || opcode == "001" || opcode == "100"){
- string temp = convertBinaryToDec(imm);
- int val = atoi(temp.c_str());
- if (val < 0){
- val += pow(2,16);
- }
- stringstream s;
- s << val;
- ins += s.str();
- }
- else{ //if it's not a shift, use imm
- ins += convertBinaryToDec(imm);
- }
- return ins;
- }
- string data(string s){ //Data
- return convertBinaryToDec(s);
- }
- //Simulator
- void Simulator(int registers[], string inst[], int data[], int PCOne, int numOfData, int numOfInst){
- ofstream sim ("simulation.txt"); //output to file
- string ins = ""; //initializes vars
- int programCntr = 64;
- int counter2 = 0;
- int cycle = 1;
- bool isJumping = false;
- int firstLocation = PCOne;
- //goes through all of the instructions
- int iWhile = 0;
- while (iWhile < numOfInst){
- ins = inst[iWhile]; //stores the current instruction
- for(int j = 0; j < ins.length(); j++){
- if(!isalpha(ins[j])){ //if it doesn't have letters, it's not an instruction
- break;
- }
- string insWords = ""; //gets the type of instruction
- while(isalpha(ins[j])){
- insWords += ins[j];
- j++;
- }
- //instructions
- if (insWords == "J"){ //CHECK IF THIS IS CORRECT!!! - shifting left 2 is equivalent to multiplying by 4
- j += 2;
- string nums ="";
- while(isdigit(ins[j])){ //gets the jump location
- nums += ins[j];
- j++;
- }
- int nums2 = atoi(nums.c_str());
- iWhile = ((nums2 - 64) / 4) - 1; //make i equal to the new location
- counter2 = nums2 - 4;
- isJumping = true;
- }else if (insWords == "BEQ"){
- j += 2;
- string reg1 = "";
- while(isdigit(ins[j])){ //get register 1
- reg1 += ins[j];
- j++;
- }
- int nums2 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while(isdigit(ins[j])){ //get register 2
- reg2 += ins[j];
- j++;
- }
- int nums3 = atoi(reg2.c_str());
- j += 3;
- string jumpLocation = "";
- while(isdigit(ins[j])){ //gets jump location
- jumpLocation += ins[j];
- j++;
- }
- int nums4 = atoi(jumpLocation.c_str());
- if(registers[nums2] == registers[nums3]){ //if reg1 and reg2 are the same, then jump to the specified location
- iWhile += nums4;
- counter2 = 64 + iWhile * 4;
- isJumping = true;
- }
- }else if (insWords == "BNE"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets register 1
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string jumpLocation = "";
- bool neg = false;
- while(isdigit(ins[j]) || ins[j]=='-'){ //gets thte jump location
- if (ins[j] == '-'){
- neg = true;
- } else {
- jumpLocation += ins[j];
- }
- j++;
- }
- int nums3 = atoi(jumpLocation.c_str());
- if(neg){
- nums3 *= -1;
- }
- if(registers[nums1] != registers[nums2]){ //if the registers are NOT equal to each other, then jump
- iWhile += nums3; //to the specified location
- counter2 = iWhile * 4 + 64;
- isJumping = true;
- }
- //cout <<nums3 << endl;
- }else if (insWords == "BGTZ"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets register 1
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string jumpLocation = "";
- bool neg = false;
- while (isdigit(ins[j]) || ins[j] == '-'){ //gets the jump location
- if ( ins[j] == '-'){
- neg = true;
- } else {
- jumpLocation += ins[j];
- }
- j++;
- }
- int nums3 = atoi(jumpLocation.c_str());
- if(neg) {
- nums3 *= -1;
- }
- //---------RECHECK THIS, IM SLEEPY AND CAN'T THINK RIGHT NOW--------
- int valueA = registers[nums1];
- if(valueA > 0){ //if the value is greater than 0, then jump
- iWhile += nums3; //to the specified location
- counter2 = iWhile * 4 + 64;
- isJumping = true;
- }
- }else if (insWords == "SW"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets register 1
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 2;
- string o = "";
- bool neg = false;
- while(isdigit(ins[j]) || ins[j] == '-'){ //gets the offset
- if (ins[j] == '-'){
- neg = true;
- } else {
- o += ins[j];
- }
- j++;
- }
- int nums4 = atoi(o.c_str());
- if (neg){
- nums4 *= -1;
- }
- j += 2;
- string reg2 = "";
- while(isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- //---------RECHECK THIS, IM SLEEPY AND CAN'T THINK RIGHT NOW--------
- data[((nums4 + registers[nums2]) - PCOne) / 4] = registers[nums1]; //store the val from register 1 to data array
- }else if (insWords == "LW"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets register 1
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 2;
- string o = "";
- bool neg = false;
- while(isdigit(ins[j]) || ins[j] == '-'){ //gets the offset
- if (ins[j] == '-'){
- neg = true;
- } else {
- o += ins[j];
- }
- j++;
- }
- int nums4 = atoi(o.c_str());
- if (neg){
- nums4 *= -1;
- }
- j += 2;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- registers[nums1] = data[((nums4 + registers[nums2]) - PCOne) / 4]; //loads the value from data array to register 1
- }else if (insWords == "BREAK"){ //just causes a breakpoint
- break;
- }else if (insWords == "XOR"){
- j += 2;
- string reg1="";
- while (isdigit(ins[j])){ //gets register 1 - aka destination register
- reg1+=ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets register 2 - register to be XORed
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string reg3 = "";
- while (isdigit(ins[j])){ //gets register 3 - register to be XORed
- reg3 += ins[j];
- j++;
- }
- int nums5 = atoi(reg3.c_str());
- registers[nums1] = registers[nums2] ^ registers[nums5]; //XORs the values in the 2 registers and puts it
- }else if (insWords == "MUL"){
- j += 2;
- string reg1 = "";
- while(isdigit(ins[j])){ //gets register 1
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while(isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string reg3 = "";
- while(isdigit(ins[j])){ //gets register 3
- reg3 += ins[j];
- j++;
- }
- int nums5 = atoi(reg3.c_str());
- registers[nums1] = registers[nums2] * registers[nums5]; //multiplies the 2 values in the registers and puts it
- }else if (insWords == "ADD"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets the register number of reg1 - aka the destination register
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets the register number of reg2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string reg3 = "";
- while (isdigit(ins[j])){ //gets the register number of reg3
- reg3 += ins[j];
- j++;
- }
- int nums5 = atoi(reg3.c_str());
- registers[nums1] = registers[nums2] + registers[nums5];
- }else if (insWords == "SUB"){
- j += 2;
- string reg1="";
- while (isdigit(ins[j])){ //gets register 1 - aka the destination register
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2="";
- while (isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string reg3="";
- while (isdigit(ins[j])){ //gets register 3
- reg3 += ins[j];
- j++;
- }
- int nums5 = atoi(reg3.c_str());
- registers[nums1] = registers[nums2] - registers[nums5]; //subtreacts the value from the 2 registers and puts it
- }else if (insWords == "AND"){
- j += 2;
- string reg1="";
- while(isdigit(ins[j])){ //gets register 1, the destination register
- reg1 += ins[j];
- j++;
- }
- int nums1= atoi(reg1.c_str());
- j += 3;
- string reg2="";
- while(isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string reg3="";
- while(isdigit(ins[j])){ //gets register 3
- reg3 += ins[j];
- j++;
- }
- int nums5 = atoi(reg3.c_str()); //uses the AND (&) operation on the values of the 2 register and
- registers[nums1] = registers[nums2] & registers[nums5]; //puts it in the destination register
- }else if (insWords == "OR"){
- j += 2;
- string reg1="";
- while(isdigit(ins[j])){ //gets register 1 - the destination register
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2="";
- while(isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string reg3="";
- while(isdigit(ins[j])){ //gets register 3
- reg3 += ins[j];
- j++;
- }
- int nums5 = atoi(reg3.c_str());
- registers[nums1] = registers[nums2] | registers[nums5]; //gets the 2 values from the registers, ORs it (|)
- }else if (insWords == "ADDU"){
- j += 2;
- string reg1 = "";
- while(isdigit(ins[j])){ //gets register 1, which is the destination register
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while(isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string reg3 = "";
- while(isdigit(ins[j])){ //gets register 3
- reg3 += ins[j];
- j++;
- }
- int nums5 = atoi(reg3.c_str());
- unsigned int regist2 = registers[nums2]; //converts the value in the 2 register to unsigned values
- unsigned int regist3 = registers[nums5];
- registers[nums1] = regist2 + regist3; //addds those two unsigned values and stores the resul into the
- }else if (insWords == "SUBU"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets register 1 - the destination register
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string reg3 = "";
- while (isdigit(ins[j])){ //gets register 3
- reg3 += ins[j];
- j++;
- }
- int nums5 = atoi(reg3.c_str());
- unsigned int regist2 = registers[nums2]; //convert the values in the two register to unsigned ints
- unsigned int regist3 = registers[nums5];
- registers[nums1] = regist2 - regist3; //subtract those two values and plave it into the destination
- }else if (insWords == "ORI"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets the register 1
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets the register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string imm = "";
- //bool neg = false;
- while (isdigit(ins[j]) /*|| ins[j] == '-'*/){ //gets the immediate value
- /*if (ins[j] == '-'){
- neg = true;
- } else {
- imm += ins[j];
- }*/
- imm += ins[j];
- j++;
- }
- int nums4 = atoi(imm.c_str());
- /* if (neg){
- nums4 *= -1;
- nums4 += pow(2,16);
- }*/
- registers[nums1] = registers[nums2] | nums4; //ORs the value in register 2 and the immediate value, and puts that
- }else if (insWords == "XORI"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets register 1
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string imm = "";
- //bool neg = false;
- while (isdigit(ins[j]) /* || ins[j] == '-'*/){ //gets the immediate value
- /* if (ins[j] == '-'){
- neg = true;
- } else {
- imm += ins[j];
- }*/
- imm += ins[j];
- j++;
- }
- int nums4 = atoi(imm.c_str());
- /* if (neg){
- nums4 *= -1;
- nums4 += pow(2,16);
- }*/
- registers[nums1] = registers[nums2] ^ nums4; //XORs the value in register 2 and the innediate value
- }else if (insWords == "ADDI"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets register 1 - the destination register
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string imm = "";
- bool neg = false;
- while (isdigit(ins[j]) || ins[j] == '-'){ //gets the immediate value
- if (ins[j] == '-'){
- neg = true;
- } else {
- imm += ins[j];
- }
- j++;
- }
- int nums4 = atoi(imm.c_str());
- if (neg){
- nums4 *= -1;
- }
- registers[nums1] = registers[nums2] + nums4; //adds the immediate value to register 2, and then stores the
- }else if (insWords == "SUBI"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){
- //gets register 1 - destimation register
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string imm = "";
- bool neg = false;
- while (isdigit(ins[j]) || ins[j] == '-'){ //gets the immediate value
- if (ins[j] == '-'){
- neg = true;
- } else {
- imm += ins[j];
- }
- j++;
- }
- int nums4 = atoi(imm.c_str());
- if (neg){
- nums4 *= -1;
- }
- //subtracts the immediate value from register 2, and store
- registers[nums1] = registers[nums2] - nums4; //that value into the destimation register (reg1)
- }else if (insWords == "ANDI"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets register 1 - destination register
- reg1+=ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets register 2
- reg2+=ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str()); //gets the immediate value
- j += 3;
- string imm = "";
- //bool neg = false;
- while (isdigit(ins[j]) /*|| ins[j] == '-'*/){ //gets the immediate value
- /*if (ins[j] == '-'){
- neg = true;
- } else {
- imm += ins[j];
- }*/
- imm += ins[j];
- j++;
- }
- int nums4 = atoi(imm.c_str());
- /*if (neg){
- nums4 *= -1;
- nums4 += pow(2,16);
- }*/
- registers[nums1] = registers[nums2] & nums4; //ANDs the immediate value and register 2, then
- }else if (insWords == "SRL"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets register 1 - the destination register
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string shiftAmount = "";
- while (isdigit(ins[j]) || ins[j] == '-'){ //gets the shift amount
- shiftAmount += ins[j];
- j++;
- }
- int nums4 = atoi(shiftAmount.c_str());
- unsigned int regist2 = registers[nums2]; //get the value of register 2 and makes it an unsigned int
- registers[nums1] = regist2 >> nums4; //shift that value right by the shiftAmount and put it into
- }else if (insWords == "SRA"){
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets register 1, the destination register
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string shiftAmount = "";
- while (isdigit(ins[j])){ //gets the shift amount
- shiftAmount += ins[j];
- j++;
- }
- int nums4 = atoi(shiftAmount.c_str());
- registers[nums1] = registers[nums2] >> nums4; //shifts the value in register 2 right by the shift amount and
- }else if (insWords == "SLL") {
- j += 2;
- string reg1 = "";
- while (isdigit(ins[j])){ //gets register 1, the destimation register
- reg1 += ins[j];
- j++;
- }
- int nums1 = atoi(reg1.c_str());
- j += 3;
- string reg2 = "";
- while (isdigit(ins[j])){ //gets register 2
- reg2 += ins[j];
- j++;
- }
- int nums2 = atoi(reg2.c_str());
- j += 3;
- string shiftAmount = "";
- while (isdigit(ins[j])){ //gets the shift amount
- shiftAmount += ins[j];
- j++;
- }
- int nums4 = atoi(shiftAmount.c_str());
- unsigned int regist2 = registers[nums2]; //makes the values of register 2 an insugned integer
- registers[nums1] = regist2 << nums4; //shifts that value to the left by the shift amoutn
- }
- }
- //prints to file
- sim << "--------------------" << endl;
- sim << "Cycle " << cycle << ":\t" << programCntr << "\t" << ins << endl;
- sim << endl;
- //prints out the registers
- sim << "Registers" << endl;
- for (int p = 0; p < 32; ++p){
- if (p < 10){
- if (p == 0 || p == 8 || p == 16 || p == 24 ){
- sim << "R0" << p << ":\t" << registers[p] << "\t";
- }
- else if(p==7){
- sim << registers[p];
- }
- else{
- sim << registers[p] << "\t";
- }
- }
- else{
- if (p == 0 || p == 8 || p == 16 || p == 24 ){
- sim << "R" << p << ":\t" << registers[p] << "\t";
- }else if(p==15 || p==23 || p==31){
- sim << registers[p];
- }
- else{
- sim << registers[p] << "\t";
- }
- }
- if ((p+1) % 8 == 0){
- sim << endl;
- }
- }
- //prints out the data
- sim << endl << "Data" << endl;
- int counts = 0;
- int noTab = PCOne+28;
- //int noTab2 = 7;
- while (counts < numOfData){
- if ((counts % 8) == 0){ // there will be 8 pieces of data per line
- if (counts != 0){
- sim << endl;
- }
- sim << PCOne << ":\t"; //print the first PC Data
- PCOne += 32; //the next time it prints, it will be after 8 pieces of data (8*4=32)
- }
- if(counts>6 && (counts-7)%8==0){
- sim << data[counts];
- }
- else{
- sim << data[counts] << "\t"; //prints all of the data that goes in each line
- }
- counts++;
- }
- cycle++;
- if (isJumping){ // if there is a jump, then update the PC to the new PC (from counter2)
- programCntr = counter2; //set isJumping to false so we can continue to check for jumps later
- isJumping = false;
- }
- programCntr += 4; //increment the PC
- PCOne = firstLocation; //reset the firstLocation
- sim << endl;
- sim << endl;
- iWhile++; //increment the while loop
- }
- sim.close();
- }
- int main(int argc, char** argv){
- //reads in the file
- ifstream inFile (argv[1]);
- /*error if file can't be opened
- while( !inFile.is_open() ) {
- cout << inFile << " does not exist. Please try again: ";
- cin >> inFile;
- cout << endl;
- inFile.open(inFile.c_str(), ifstream::in);
- }*/
- ofstream disassembly ("disassembly.txt");
- string currentLine = ""; //reads the current line of the input file
- int numOfData = 0; //# of instructions and data
- int numOfInst = 0;
- int programCounter = 64; //program counter starts at 64
- while ( getline(inFile,currentLine) ){ //while there are still lines in the input file
- string cat = currentLine.substr(0,3); //categorizes the instruction into 1, 2, 3, or data
- stringstream temp; //convert programCounter to a string
- temp << programCounter;
- string programCounterStr= temp.str();
- if (cat == "001"){ //category 1
- string output = currentLine + "\t" + programCounterStr + "\t";
- string opcode = currentLine.substr(3,3);
- int opcodeNum;
- istringstream (opcode) >> opcodeNum;
- switch(opcodeNum){
- case 0: output += NOP();
- break;
- case 1: output += J(currentLine);
- break;
- case 10:output += BEQ(currentLine);
- break;
- case 11: output += BNE(currentLine);
- break;
- case 100: output += BGTZ(currentLine);
- break;
- case 101: output += SW(currentLine);
- break;
- case 110: output += LW(currentLine);
- break;
- case 111: output += BREAK();
- break;
- default: break;
- }
- disassembly << output << endl; //print out to dissembly.txt
- numOfInst++; //increment instruction count
- }
- else if (cat == "010"){ //category 2
- string output = currentLine + "\t" + programCounterStr + "\t";
- string opcode = currentLine.substr(3,3); //determines instruction type from the opcode
- if(opcode == "000"){
- output += "XOR R";
- }
- if(opcode == "001"){
- output += "MUL R";
- }
- if(opcode == "010"){
- output += "ADD R";
- }
- if(opcode == "011"){
- output += "SUB R";
- }
- if(opcode == "100"){
- output += "AND R";
- }
- if(opcode == "101"){
- output += "OR R";
- }
- if(opcode == "110"){
- output += "ADDU R";
- }
- if(opcode == "111"){
- output += "SUBU R";
- }
- output += category2(currentLine);
- disassembly << output << endl; //print out to dissembly.txt
- numOfInst++; //increment instruction count
- }
- else if (cat == "100"){ //category 3
- string output = currentLine + "\t" + programCounterStr + "\t";
- string opcode = currentLine.substr(3,3); //determines instruction type from the opcode
- if(opcode == "000"){
- output += "ORI R";
- }
- if(opcode == "001"){
- output += "XORI R";
- }
- if(opcode == "010"){
- output += "ADDI R";
- }
- if(opcode == "011"){
- output += "SUBI R";
- }
- if(opcode == "100"){
- output += "ANDI R";
- }
- if(opcode == "101"){
- output += "SRL R";
- }
- if(opcode == "110"){
- output += "SRA R";
- }
- if(opcode == "111"){
- output += "SLL R";
- }
- output += category3(currentLine);
- disassembly << output << endl; //print out to dissembly.txt
- numOfInst++; //increment instruction count
- }
- else{ //if it's data, just print
- disassembly << currentLine << "\t" << programCounterStr << "\t" << convertBinaryToDec(currentLine) << endl;//print to dissembly.txt
- numOfData++; //incrememnt data count
- }
- programCounter += 4; //incrememnt PC by 4 after every instruction
- stringstream temp2; //converts programCounter into a string (again)
- temp2 << programCounter;
- programCounterStr= temp2.str();
- }
- disassembly.close();
- /* //array with all of the instructions (binary) ----------------------check if this works - jk, im not doing this-----------------------------
- int binaryInstructions[numOfInst];
- string currentLine2 = "";
- int counterIns = 0;
- while (getline(inFile,currentLine2)){
- if (counterIns < 32){
- binaryInstructions[i] = currentLine2;
- counterIns++;
- }
- }*/
- ifstream fileTwo ("disassembly.txt");
- int registers[32]; //create and initialize 32 registers to 0 [0-31]
- for(int i = 0; i < 32 ; i++){
- registers[i] = 0;
- }
- string line; //line from disassembly file
- int pc = 64; //program counter starts at "64"
- string *inst = new string[numOfInst];
- //array of the listed instructions and data & its indecies
- int data[numOfData];
- int indexOfInstruction = 0;
- int indexOfData = 0;
- bool isInstruction = true; //true if the line is an inst (the first few lines)
- int PCOne = 0; //location of 1st data
- while ( getline(fileTwo,line) ){ //saves inst & data into respective arrays
- if(isInstruction){ //if the line's an instruction
- string temp;
- for (int i = 0; i < line.length(); i++){
- if (isalpha( line[i]) ){ //if there's a letter than it's an inst; save the inst ONLY into array
- temp = line.substr(i,(line.size() - i));
- break;
- }
- }
- inst[indexOfInstruction] = temp; //put into array
- indexOfInstruction++; //increment the index of the instruction
- }
- else{ //the line is data, rather than an instruction
- if (indexOfData == 0){ //first data being inputted
- PCOne = pc;
- }
- string temp2 = line.substr(0,32);
- int value;
- istringstream( convertBinaryToDec(temp2) ) >> value;
- data[indexOfData] = value; //put into array
- indexOfData++; //increment the index of the data
- }
- if (line.substr(0,6) == "001111"){ //if the inst is BREAK
- isInstruction = false;
- }
- pc += 4; //increment the PC by 4
- }
- Simulator(registers, inst, data, PCOne, numOfData, numOfInst);
- fileTwo.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement