Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 41.36 KB | None | 0 0
  1. //On my honor, I have neither given nor received unauthorized aid on this assignment
  2. #include <iostream>
  3. #include <vector>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <string>
  7. #include <cmath>
  8. #include <math.h>
  9. #include <stdlib.h>
  10. using namespace std;
  11.  
  12. string convertBinaryToDec(string binNum){                   //2's complement to decimal
  13.     unsigned int sum = 0;
  14.     for(int i = 0; i < binNum.length(); i++){
  15.         if(binNum.at(i) == '1'){
  16.          sum += pow(2, binNum.length() - 1 - i);
  17.         }
  18.     }
  19.     if (binNum.at(0) == '1' && binNum.length() != 5){                                   //if its 16 bits, add 1's to the front
  20.         for(int k = binNum.length(); k < 32; k++){
  21.             sum += pow(2,k);                                                                        //add 1s to the front
  22.         }
  23.     }
  24.     int val = (int) sum;  
  25.     stringstream sumString;
  26.     sumString << val;
  27.     string final = sumString.str();
  28.     return final;
  29.     //return (to_string(val));                                  //-std=c++11 is giving me problems.... sigh...
  30. }
  31.  
  32. string NOP(){                                                           //CATEGORY 1
  33.     return "NOP";
  34. }
  35. string J(string s){
  36.     string targetAddress = convertBinaryToDec(s.substr(6,26));
  37.     int targetAdd;
  38.     istringstream (targetAddress) >> targetAdd;
  39.     targetAdd *= 4;
  40.     stringstream temp;
  41.     temp << targetAdd;
  42.     string jNum= temp.str();
  43.     string ins = "J #" + jNum;
  44.     return ins;
  45. }
  46. string BEQ(string s){
  47.     string s1 = s.substr(6,5);
  48.     string s2 = s.substr(11,5);
  49.     string offset = s.substr(16,16);
  50.     string ins = "BEQ R";
  51.     ins += convertBinaryToDec(s1) + ", R" + convertBinaryToDec(s2) + ", #" + convertBinaryToDec(offset);
  52.     return ins;
  53. }
  54. string BNE(string s){
  55.     string s1 = s.substr(6,5);
  56.     string s2 = s.substr(11,5);
  57.     string offset = s.substr(16,16);
  58.     string ins = "BNE R";
  59.     ins += convertBinaryToDec(s1) + ", R" + convertBinaryToDec(s2) + ", #" + convertBinaryToDec(offset);
  60.     return ins;
  61. }
  62. string BGTZ(string s){
  63.     string s1 = s.substr(6,5);
  64.     string offset = s.substr(16,16);
  65.     string ins = "BGTZ R";
  66.     ins += convertBinaryToDec(s1) + ", #" + convertBinaryToDec(offset);
  67.     return ins;
  68. }
  69. string SW(string s){
  70.     string base = s.substr(6,5);
  71.     string rt = s.substr(11,5);
  72.     string offset = s.substr(16,16);
  73.     string ins = "SW R";
  74.     ins += convertBinaryToDec(rt) + ", " + convertBinaryToDec(offset) + "(R" +convertBinaryToDec(base) + ")";
  75.     return ins;
  76. }
  77. string LW(string s){
  78.     string base = s.substr(6,5);
  79.     string rt = s.substr(11,5);
  80.     string offset = s.substr(16,16);
  81.     string ins = "LW R";
  82.     ins += convertBinaryToDec(rt) + ", " + convertBinaryToDec(offset) + "(R" +convertBinaryToDec(base) + ")";
  83.     return ins;
  84. }
  85. string BREAK(){
  86.     return "BREAK";
  87. }
  88. string category2(string s){                                 //CATEGORY 2
  89.     string d = s.substr(6,5);
  90.     string s1 = s.substr(11,5);
  91.     string s2 = s.substr(16,5);
  92.     string ins = "";
  93.     ins += convertBinaryToDec(d) + ", R" + convertBinaryToDec(s1) + ", R" + convertBinaryToDec(s2);
  94.     return ins;
  95. }
  96. string category3(string s){                               //CATEGORY 3
  97.     string opcode = s.substr(3,3);
  98.     string d = s.substr(6,5);
  99.     string s1 = s.substr(11,5);
  100.     string imm = s.substr(16,16);
  101.     string shiftAmt = s.substr(27,5);
  102.     string ins = "";
  103.     ins += convertBinaryToDec(d) + ", R" + convertBinaryToDec(s1) + ", #";
  104.     if (opcode == "101" || opcode == "110" || opcode == "111"){     //if it's a shift (SRL, SRA, or SLL) use shift amount
  105.         ins += convertBinaryToDec(shiftAmt);
  106.     }
  107.     else if(opcode == "000" || opcode == "001" || opcode == "100"){
  108.         string temp = convertBinaryToDec(imm);
  109.         int val = atoi(temp.c_str());
  110.         if (val < 0){
  111.             val += pow(2,16);
  112.         }
  113.         stringstream s;
  114.         s << val;
  115.         ins += s.str();
  116.        
  117.     }
  118.     else{                                                           //if it's not a shift, use imm
  119.         ins += convertBinaryToDec(imm);
  120.     }
  121.     return ins;
  122. }
  123. string data(string s){                                              //Data
  124.     return convertBinaryToDec(s);
  125. }
  126.  
  127. //Simulator
  128. void Simulator(int registers[], string inst[], int data[], int PCOne, int numOfData, int numOfInst){
  129.     ofstream  sim ("simulation.txt");               //output to file
  130.     string ins = "";                                //initializes vars
  131.     int programCntr = 64;
  132.     int counter2 = 0;
  133.     int cycle = 1;
  134.     bool isJumping = false;
  135.     int firstLocation = PCOne;
  136.  
  137.     //goes through all of the instructions
  138.     int iWhile = 0;
  139.     while (iWhile < numOfInst){
  140.         ins = inst[iWhile];                          //stores the current instruction
  141.         for(int j = 0; j < ins.length(); j++){
  142.             if(!isalpha(ins[j])){               //if it doesn't have letters, it's not an instruction
  143.                 break;
  144.             }  
  145.             string insWords = "";               //gets the type of instruction
  146.             while(isalpha(ins[j])){
  147.                 insWords += ins[j];
  148.                 j++;
  149.             }
  150.             //instructions
  151.             if (insWords == "J"){           //CHECK IF THIS IS CORRECT!!! - shifting left 2 is equivalent to multiplying by 4
  152.                 j += 2;
  153.                 string nums ="";
  154.                 while(isdigit(ins[j])){                         //gets the jump location
  155.                     nums += ins[j];
  156.                     j++;
  157.                 }
  158.                 int nums2 = atoi(nums.c_str());
  159.                 iWhile = ((nums2 - 64) / 4) - 1;                    //make i equal to the new location
  160.                 counter2 = nums2 - 4;
  161.                 isJumping = true;
  162.             }else if (insWords == "BEQ"){
  163.                 j += 2;
  164.                 string reg1 = "";
  165.                 while(isdigit(ins[j])){                             //get register 1
  166.                     reg1 += ins[j];
  167.                     j++;
  168.                 }
  169.                 int nums2 = atoi(reg1.c_str());
  170.                 j += 3;
  171.                 string reg2 = "";
  172.                 while(isdigit(ins[j])){                             //get register 2
  173.                     reg2 += ins[j];
  174.                     j++;
  175.                 }
  176.                 int nums3 = atoi(reg2.c_str());
  177.                 j += 3;
  178.                 string jumpLocation = "";
  179.                 while(isdigit(ins[j])){                             //gets jump location
  180.                     jumpLocation += ins[j];
  181.                     j++;
  182.                 }
  183.                 int nums4 = atoi(jumpLocation.c_str());
  184.                 if(registers[nums2] == registers[nums3]){       //if reg1 and reg2 are the same, then jump to the specified location
  185.                     iWhile += nums4;
  186.                     counter2 = 64 + iWhile * 4;
  187.                     isJumping = true;
  188.                 }
  189.             }else if (insWords == "BNE"){
  190.                 j += 2;
  191.                 string reg1 = "";
  192.                 while (isdigit(ins[j])){                             //gets register 1
  193.                     reg1 += ins[j];
  194.                     j++;
  195.                 }
  196.                 int nums1 = atoi(reg1.c_str());
  197.                 j += 3;
  198.                 string reg2 = "";
  199.                 while (isdigit(ins[j])){                           //gets register 2
  200.                     reg2 += ins[j];
  201.                     j++;
  202.                 }
  203.                 int nums2 = atoi(reg2.c_str());
  204.                 j += 3;
  205.                 string jumpLocation = "";
  206.                 bool neg = false;
  207.                 while(isdigit(ins[j]) ||  ins[j]=='-'){                           //gets thte jump location
  208.                     if (ins[j] == '-'){
  209.                         neg = true;
  210.                     } else {
  211.                         jumpLocation += ins[j];
  212.                     }
  213.                     j++;
  214.                 }
  215.                 int nums3 = atoi(jumpLocation.c_str());
  216.                 if(neg){
  217.                     nums3 *= -1;
  218.                 }
  219.                 if(registers[nums1] != registers[nums2]){           //if the registers are NOT equal to each other, then jump
  220.                     iWhile += nums3;                                   //to the specified location
  221.                     counter2 = iWhile * 4 + 64;
  222.                     isJumping = true;
  223.                 }
  224.                 //cout <<nums3 << endl;
  225.             }else if (insWords == "BGTZ"){
  226.                 j += 2;
  227.                 string reg1 = "";
  228.                 while (isdigit(ins[j])){                            //gets register 1
  229.                     reg1 += ins[j];
  230.                     j++;
  231.                 }
  232.                 int nums1 = atoi(reg1.c_str());
  233.                 j += 3;
  234.                 string jumpLocation = "";
  235.                 bool neg = false;
  236.                 while (isdigit(ins[j]) || ins[j] == '-'){                            //gets the jump location
  237.                     if ( ins[j] == '-'){
  238.                         neg = true;
  239.                     } else {
  240.                         jumpLocation += ins[j];
  241.                     }
  242.                     j++;
  243.                 }
  244.                 int nums3 = atoi(jumpLocation.c_str());
  245.                 if(neg) {
  246.                     nums3 *= -1;
  247.                 }
  248.                 //---------RECHECK THIS, IM SLEEPY AND CAN'T THINK RIGHT NOW--------
  249.                 int valueA = registers[nums1];
  250.                 if(valueA > 0){                                    //if the value is greater than 0, then jump
  251.                     iWhile += nums3;                                          //to the specified location
  252.                     counter2 = iWhile * 4 + 64;
  253.                     isJumping = true;
  254.                 }
  255.             }else if (insWords == "SW"){
  256.                 j += 2;
  257.                 string reg1 = "";
  258.                 while (isdigit(ins[j])){                            //gets register 1
  259.                     reg1 += ins[j];
  260.                     j++;
  261.                 }
  262.                 int nums1 = atoi(reg1.c_str());
  263.                 j += 2;
  264.                 string o = "";
  265.                 bool neg = false;
  266.                 while(isdigit(ins[j]) || ins[j] == '-'){                           //gets the offset
  267.                     if (ins[j] == '-'){
  268.                         neg = true;
  269.                     } else {
  270.                         o += ins[j];
  271.                     }
  272.                     j++;
  273.                 }
  274.                 int nums4 = atoi(o.c_str());
  275.                 if (neg){
  276.                     nums4 *= -1;
  277.                 }
  278.                 j += 2;
  279.                 string reg2 = "";
  280.                 while(isdigit(ins[j])){                           //gets register 2
  281.                     reg2 += ins[j];
  282.                     j++;
  283.                 }
  284.                 int nums2 = atoi(reg2.c_str());
  285.                 //---------RECHECK THIS, IM SLEEPY AND CAN'T THINK RIGHT NOW--------
  286.                 data[((nums4 + registers[nums2]) - PCOne) / 4] = registers[nums1];     //store the val from register 1 to data array
  287.             }else if (insWords == "LW"){
  288.                 j += 2;
  289.                 string reg1 = "";
  290.                 while (isdigit(ins[j])){                    //gets register 1
  291.                     reg1 += ins[j];
  292.                     j++;
  293.                 }
  294.                 int nums1 = atoi(reg1.c_str());
  295.                 j += 2;
  296.                 string o = "";
  297.                 bool neg = false;
  298.                 while(isdigit(ins[j]) || ins[j] == '-'){                           //gets the offset
  299.                     if (ins[j] == '-'){
  300.                         neg = true;
  301.                     } else {
  302.                         o += ins[j];
  303.                     }
  304.                    
  305.                     j++;
  306.                 }
  307.                
  308.                 int nums4 = atoi(o.c_str());
  309.                
  310.                 if (neg){
  311.                     nums4 *= -1;
  312.                 }
  313.                 j += 2;
  314.                 string reg2 = "";
  315.                 while (isdigit(ins[j])){                    //gets register 2
  316.                     reg2 += ins[j];
  317.                     j++;
  318.                 }
  319.                 int nums2 = atoi(reg2.c_str());
  320.                 registers[nums1] = data[((nums4 + registers[nums2]) - PCOne) / 4];   //loads the value from data array to register 1
  321.             }else if (insWords == "BREAK"){                          //just causes a breakpoint
  322.                 break;
  323.             }else if (insWords == "XOR"){
  324.                 j += 2;
  325.                 string reg1="";
  326.                 while (isdigit(ins[j])){                            //gets register 1 - aka destination register
  327.                     reg1+=ins[j];
  328.                     j++;
  329.                 }
  330.                 int nums1 = atoi(reg1.c_str());
  331.                 j += 3;
  332.                 string reg2 = "";
  333.                 while (isdigit(ins[j])){                            //gets register 2 - register to be XORed
  334.                     reg2 += ins[j];
  335.                     j++;
  336.                 }
  337.                 int nums2 = atoi(reg2.c_str());
  338.                 j += 3;
  339.                 string reg3 = "";
  340.                 while (isdigit(ins[j])){                                //gets register 3 - register to be XORed
  341.                     reg3 += ins[j];
  342.                     j++;
  343.                 }
  344.                 int nums5 = atoi(reg3.c_str());
  345.                
  346.                 registers[nums1] = registers[nums2] ^ registers[nums5];       //XORs the values in the 2 registers and puts it
  347.             }else if (insWords == "MUL"){
  348.                 j += 2;
  349.                 string reg1 = "";
  350.                 while(isdigit(ins[j])){                             //gets register 1
  351.                     reg1 += ins[j];
  352.                     j++;
  353.                 }
  354.                 int nums1 = atoi(reg1.c_str());
  355.                 j += 3;
  356.                 string reg2 = "";
  357.                 while(isdigit(ins[j])){                             //gets register 2
  358.                     reg2 += ins[j];
  359.                     j++;
  360.                 }
  361.                 int nums2 = atoi(reg2.c_str());
  362.                 j += 3;
  363.                 string reg3 = "";
  364.                 while(isdigit(ins[j])){                             //gets register 3
  365.                     reg3 += ins[j];
  366.                     j++;
  367.                 }
  368.                 int nums5 = atoi(reg3.c_str());
  369.                 registers[nums1] = registers[nums2] * registers[nums5];     //multiplies the 2 values in the registers and puts it
  370.             }else if (insWords == "ADD"){
  371.                 j += 2;
  372.                 string reg1 = "";
  373.                 while (isdigit(ins[j])){                        //gets the register number of reg1 - aka the destination register
  374.                     reg1 += ins[j];
  375.                     j++;
  376.                 }
  377.                 int nums1 = atoi(reg1.c_str());
  378.                 j += 3;
  379.                 string reg2 = "";
  380.                 while (isdigit(ins[j])){                        //gets the register number of reg2
  381.                     reg2 += ins[j];
  382.                     j++;
  383.                 }
  384.                 int nums2 = atoi(reg2.c_str());
  385.                 j += 3;
  386.                 string reg3 = "";
  387.                 while (isdigit(ins[j])){                        //gets the register number of reg3
  388.                     reg3 += ins[j];
  389.                     j++;
  390.                 }
  391.                 int nums5 = atoi(reg3.c_str());
  392.                 registers[nums1] = registers[nums2] + registers[nums5];
  393.             }else if (insWords == "SUB"){
  394.                 j += 2;
  395.                 string reg1="";
  396.                 while (isdigit(ins[j])){                //gets register 1 - aka the destination register
  397.                     reg1 += ins[j];
  398.                     j++;
  399.                 }
  400.                 int nums1 = atoi(reg1.c_str());
  401.                 j += 3;
  402.                 string reg2="";
  403.                 while (isdigit(ins[j])){                                 //gets register 2
  404.                     reg2 += ins[j];
  405.                     j++;
  406.                 }
  407.                 int nums2 = atoi(reg2.c_str());
  408.                 j += 3;
  409.                 string reg3="";
  410.                 while (isdigit(ins[j])){                                 //gets register 3
  411.                     reg3 += ins[j];
  412.                     j++;
  413.                 }
  414.                 int nums5 = atoi(reg3.c_str());
  415.                 registers[nums1] = registers[nums2] - registers[nums5];  //subtreacts the value from the 2 registers and puts it
  416.             }else if (insWords == "AND"){
  417.                 j += 2;
  418.                 string reg1="";
  419.                 while(isdigit(ins[j])){                     //gets register 1, the destination register
  420.                     reg1 += ins[j];
  421.                     j++;
  422.                 }
  423.                 int nums1= atoi(reg1.c_str());
  424.                 j += 3;
  425.                 string reg2="";
  426.                 while(isdigit(ins[j])){                             //gets register 2
  427.                     reg2 += ins[j];
  428.                     j++;
  429.                 }
  430.                 int nums2 = atoi(reg2.c_str());
  431.                 j += 3;
  432.                 string reg3="";
  433.                 while(isdigit(ins[j])){                             //gets register 3
  434.                     reg3 += ins[j];
  435.                     j++;
  436.                 }
  437.                 int nums5 = atoi(reg3.c_str());           //uses the AND (&) operation on the values of the 2 register and
  438.                 registers[nums1] = registers[nums2] & registers[nums5];         //puts it in the destination register
  439.             }else if (insWords == "OR"){
  440.                 j += 2;
  441.                 string reg1="";
  442.                 while(isdigit(ins[j])){         //gets register 1 - the destination register
  443.                     reg1 += ins[j];
  444.                     j++;
  445.                 }
  446.                 int nums1 = atoi(reg1.c_str());
  447.                 j += 3;
  448.                 string reg2="";
  449.                 while(isdigit(ins[j])){         //gets register 2
  450.                     reg2 += ins[j];
  451.                     j++;
  452.                 }
  453.                 int nums2 = atoi(reg2.c_str());
  454.                 j += 3;
  455.                 string reg3="";
  456.                 while(isdigit(ins[j])){         //gets register 3
  457.                     reg3 += ins[j];
  458.                     j++;
  459.                 }
  460.                 int nums5 = atoi(reg3.c_str());
  461.                 registers[nums1] = registers[nums2] | registers[nums5];     //gets the 2 values from the registers, ORs it (|)
  462.             }else if (insWords == "ADDU"){
  463.                 j += 2;
  464.                 string reg1 = "";
  465.                 while(isdigit(ins[j])){                 //gets register 1, which is the destination register
  466.                     reg1 += ins[j];
  467.                     j++;
  468.                 }
  469.                 int nums1 = atoi(reg1.c_str());
  470.                 j += 3;
  471.                 string reg2 = "";
  472.                 while(isdigit(ins[j])){                 //gets register 2
  473.                     reg2 += ins[j];
  474.                     j++;
  475.                 }
  476.                 int nums2 = atoi(reg2.c_str());
  477.                 j += 3;
  478.                 string reg3 = "";
  479.                 while(isdigit(ins[j])){                 //gets register 3
  480.                     reg3 += ins[j];
  481.                     j++;
  482.                 }
  483.                 int nums5 = atoi(reg3.c_str());
  484.                 unsigned int regist2 = registers[nums2];        //converts the value in the 2 register to unsigned values
  485.                 unsigned int regist3 = registers[nums5];
  486.                 registers[nums1] = regist2 + regist3;        //addds those two unsigned values and stores the resul into the
  487.             }else if (insWords == "SUBU"){
  488.                 j += 2;
  489.                 string reg1 = "";
  490.                 while (isdigit(ins[j])){                                //gets register 1 - the destination register
  491.                     reg1 += ins[j];
  492.                     j++;
  493.                 }
  494.                 int nums1 = atoi(reg1.c_str());
  495.                 j += 3;
  496.                 string reg2 = "";
  497.                 while (isdigit(ins[j])){                                //gets register 2
  498.                     reg2 += ins[j];
  499.                     j++;
  500.                 }
  501.                 int nums2 = atoi(reg2.c_str());
  502.                 j += 3;
  503.                 string reg3 = "";
  504.                 while (isdigit(ins[j])){                                //gets register 3
  505.                     reg3 += ins[j];
  506.                     j++;
  507.                 }
  508.                 int nums5 = atoi(reg3.c_str());
  509.                 unsigned int regist2 = registers[nums2];            //convert the values in the two register to unsigned ints
  510.                 unsigned int regist3 = registers[nums5];
  511.                 registers[nums1] = regist2 - regist3;               //subtract those two values and plave it into the destination
  512.             }else if (insWords == "ORI"){
  513.                 j += 2;
  514.                 string reg1 = "";
  515.                 while (isdigit(ins[j])){                    //gets the register 1
  516.                     reg1 += ins[j];
  517.                     j++;
  518.                 }
  519.                 int nums1 = atoi(reg1.c_str());
  520.                 j += 3;
  521.                 string reg2 = "";
  522.                 while (isdigit(ins[j])){                    //gets the register 2
  523.                     reg2 += ins[j];
  524.                     j++;
  525.                 }
  526.                 int nums2 = atoi(reg2.c_str());
  527.                 j += 3;
  528.                 string imm = "";
  529.                 //bool neg = false;
  530.                 while (isdigit(ins[j]) /*|| ins[j] == '-'*/){                                //gets the immediate value
  531.                     /*if (ins[j] == '-'){
  532.                      neg = true;
  533.                      } else {
  534.                      imm += ins[j];
  535.                      }*/
  536.                     imm += ins[j];
  537.                     j++;
  538.                 }
  539.                 int nums4 = atoi(imm.c_str());
  540.                 /*  if (neg){
  541.                  nums4 *= -1;
  542.                  nums4 += pow(2,16);
  543.                  }*/
  544.                 registers[nums1] = registers[nums2] | nums4;    //ORs the value in register 2 and the immediate value, and puts that
  545.             }else if (insWords == "XORI"){
  546.                 j += 2;
  547.                 string reg1 = "";
  548.                 while (isdigit(ins[j])){                        //gets register 1
  549.                     reg1 += ins[j];
  550.                     j++;
  551.                 }
  552.                 int nums1 = atoi(reg1.c_str());
  553.                 j += 3;
  554.                 string reg2 = "";
  555.                 while (isdigit(ins[j])){                        //gets register 2
  556.                     reg2 += ins[j];
  557.                     j++;
  558.                 }
  559.                 int nums2 = atoi(reg2.c_str());
  560.                 j += 3;
  561.                 string imm = "";
  562.                 //bool neg = false;
  563.                 while (isdigit(ins[j]) /* || ins[j] == '-'*/){                                //gets the immediate value
  564.                     /* if (ins[j] == '-'){
  565.                      neg = true;
  566.                      } else {
  567.                      imm += ins[j];
  568.                      }*/
  569.                     imm += ins[j];
  570.                     j++;
  571.                 }
  572.                 int nums4 = atoi(imm.c_str());
  573.                 /* if (neg){
  574.                  nums4 *= -1;
  575.                  nums4 += pow(2,16);
  576.                  }*/
  577.                 registers[nums1] = registers[nums2] ^ nums4;    //XORs the value in register 2 and the innediate value
  578.             }else if (insWords == "ADDI"){
  579.                 j += 2;
  580.                 string reg1 = "";
  581.                 while (isdigit(ins[j])){               //gets register 1 - the destination register
  582.                     reg1 += ins[j];
  583.                     j++;
  584.                 }
  585.                 int nums1 = atoi(reg1.c_str());
  586.                 j += 3;
  587.                 string reg2 = "";
  588.                 while (isdigit(ins[j])){               //gets register 2
  589.                     reg2 += ins[j];
  590.                     j++;
  591.                 }
  592.                 int nums2 = atoi(reg2.c_str());
  593.                 j += 3;
  594.                 string imm = "";
  595.                 bool neg = false;
  596.                 while (isdigit(ins[j]) || ins[j] == '-'){                                //gets the immediate value
  597.                     if (ins[j] == '-'){
  598.                         neg = true;
  599.                     } else {
  600.                         imm += ins[j];
  601.                     }
  602.                     j++;
  603.                 }
  604.                 int nums4 = atoi(imm.c_str());
  605.                 if (neg){
  606.                     nums4 *= -1;
  607.                 }
  608.                 registers[nums1] = registers[nums2] + nums4;      //adds the immediate value to register 2, and then stores the
  609.             }else if (insWords == "SUBI"){
  610.                 j += 2;
  611.                 string reg1 = "";
  612.                
  613.                 while (isdigit(ins[j])){
  614.                     //gets register 1 - destimation register
  615.                    
  616.                     reg1 += ins[j];
  617.                     j++;
  618.                 }
  619.                
  620.                 int nums1 = atoi(reg1.c_str());
  621.                 j += 3;
  622.                 string reg2 = "";
  623.                 while (isdigit(ins[j])){                                     //gets register 2
  624.                     reg2 += ins[j];
  625.                     j++;
  626.                 }
  627.                 int nums2 = atoi(reg2.c_str());
  628.                 j += 3;
  629.                 string imm = "";
  630.                 bool neg = false;
  631.                 while (isdigit(ins[j]) || ins[j] == '-'){                                //gets the immediate value
  632.                     if (ins[j] == '-'){
  633.                         neg = true;
  634.                     } else {
  635.                         imm += ins[j];
  636.                     }
  637.                     j++;
  638.                 }
  639.                 int nums4 = atoi(imm.c_str());
  640.                 if (neg){
  641.                     nums4 *= -1;
  642.                 }
  643.                 //subtracts the immediate value from register 2, and store
  644.                 registers[nums1] = registers[nums2] - nums4;        //that value into the destimation register (reg1)
  645.             }else if (insWords == "ANDI"){
  646.                 j += 2;
  647.                 string reg1 = "";
  648.                 while (isdigit(ins[j])){                //gets register 1 - destination register
  649.                     reg1+=ins[j];
  650.                     j++;
  651.                    
  652.                 }
  653.                 int nums1 = atoi(reg1.c_str());
  654.                 j += 3;
  655.                 string reg2 = "";
  656.                 while (isdigit(ins[j])){                //gets register 2
  657.                     reg2+=ins[j];
  658.                     j++;
  659.                 }
  660.                 int nums2 = atoi(reg2.c_str());         //gets the immediate value
  661.                 j += 3;
  662.                 string imm = "";
  663.                 //bool neg = false;
  664.                 while (isdigit(ins[j]) /*|| ins[j] == '-'*/){                                //gets the immediate value
  665.                     /*if (ins[j] == '-'){
  666.                      neg = true;
  667.                      } else {
  668.                      imm += ins[j];
  669.                      }*/
  670.                     imm += ins[j];
  671.                     j++;
  672.                 }
  673.                 int nums4 = atoi(imm.c_str());
  674.                 /*if (neg){
  675.                  nums4 *= -1;
  676.                  nums4 += pow(2,16);
  677.                  }*/
  678.  
  679.                 registers[nums1] = registers[nums2] & nums4;             //ANDs the immediate value and register 2, then
  680.             }else if (insWords == "SRL"){
  681.                 j += 2;
  682.                 string reg1 = "";
  683.                 while (isdigit(ins[j])){                        //gets register 1 - the destination register
  684.                     reg1 += ins[j];
  685.                     j++;
  686.                 }
  687.                 int nums1 = atoi(reg1.c_str());
  688.                 j += 3;
  689.                 string reg2 = "";
  690.                 while (isdigit(ins[j])){                        //gets register 2
  691.                     reg2 += ins[j];
  692.                     j++;
  693.                 }
  694.                 int nums2 = atoi(reg2.c_str());
  695.                 j += 3;
  696.                 string shiftAmount = "";
  697.                 while (isdigit(ins[j]) || ins[j] == '-'){                        //gets the shift amount
  698.                     shiftAmount += ins[j];
  699.                     j++;
  700.                 }
  701.                 int nums4 = atoi(shiftAmount.c_str());
  702.                 unsigned int regist2 = registers[nums2];            //get the value of register 2 and makes it an unsigned int
  703.                 registers[nums1] = regist2 >> nums4;                //shift that value right by the shiftAmount and put it into
  704.             }else if (insWords == "SRA"){
  705.                 j += 2;
  706.                 string reg1 = "";
  707.                 while (isdigit(ins[j])){            //gets register 1, the destination register
  708.                     reg1 += ins[j];
  709.                     j++;
  710.                 }
  711.                 int nums1 = atoi(reg1.c_str());
  712.                 j += 3;
  713.                 string reg2 = "";
  714.                 while (isdigit(ins[j])){            //gets register 2
  715.                     reg2 += ins[j];
  716.                     j++;
  717.                 }
  718.                 int nums2 = atoi(reg2.c_str());
  719.                 j += 3;
  720.                 string shiftAmount = "";
  721.                 while (isdigit(ins[j])){           //gets the shift amount
  722.                     shiftAmount += ins[j];
  723.                     j++;
  724.                 }
  725.                 int nums4 = atoi(shiftAmount.c_str());
  726.                 registers[nums1] = registers[nums2] >> nums4;         //shifts the value in register 2 right by the shift amount and
  727.             }else if (insWords == "SLL") {
  728.                 j += 2;
  729.                 string reg1 = "";
  730.                 while (isdigit(ins[j])){                                        //gets register 1, the destimation register
  731.                     reg1 += ins[j];
  732.                     j++;
  733.                 }
  734.                 int nums1 = atoi(reg1.c_str());
  735.                 j += 3;
  736.                 string reg2 = "";
  737.                 while (isdigit(ins[j])){                                        //gets register 2
  738.                     reg2 += ins[j];
  739.                     j++;
  740.                 }
  741.                 int nums2 = atoi(reg2.c_str());
  742.                 j += 3;
  743.                 string shiftAmount = "";
  744.                 while (isdigit(ins[j])){                                        //gets the shift amount
  745.                     shiftAmount += ins[j];
  746.                     j++;
  747.                 }
  748.                 int nums4 = atoi(shiftAmount.c_str());
  749.                 unsigned int regist2 = registers[nums2];                    //makes the values of register 2 an insugned integer
  750.                 registers[nums1] = regist2 << nums4;                        //shifts that value to the left by the shift amoutn
  751.             }
  752.         }
  753.        
  754.         //prints to file
  755.         sim << "--------------------" << endl;
  756.         sim << "Cycle " << cycle << ":\t" << programCntr << "\t" << ins << endl;
  757.         sim << endl;
  758.         //prints out the registers
  759.         sim << "Registers" << endl;
  760.         for (int p = 0; p < 32; ++p){
  761.             if (p < 10){
  762.                 if (p == 0 || p == 8 || p == 16 || p == 24 ){
  763.                     sim << "R0" << p << ":\t" << registers[p] << "\t";
  764.                 }
  765.                 else if(p==7){
  766.                     sim << registers[p];
  767.                 }
  768.                 else{
  769.                     sim << registers[p] << "\t";
  770.                 }
  771.             }
  772.             else{
  773.                 if (p == 0 || p == 8 || p == 16 || p == 24 ){
  774.                     sim << "R" << p << ":\t" << registers[p] << "\t";
  775.                 }else if(p==15 || p==23 || p==31){
  776.                     sim << registers[p];
  777.                 }
  778.                 else{
  779.                     sim << registers[p] << "\t";
  780.                 }
  781.             }
  782.             if ((p+1) % 8 == 0){
  783.                 sim << endl;
  784.             }
  785.         }
  786.        
  787.         //prints out the data
  788.         sim << endl << "Data" << endl;
  789.         int counts = 0;
  790.         int noTab = PCOne+28;
  791.         //int noTab2 = 7;
  792.         while (counts < numOfData){
  793.             if ((counts % 8) == 0){              // there will be 8 pieces of data per line
  794.                 if (counts != 0){
  795.                     sim << endl;
  796.                 }
  797.                 sim << PCOne << ":\t";           //print the first PC Data
  798.                 PCOne += 32;                      //the next time it prints, it will be after 8 pieces of data (8*4=32)
  799.             }
  800.             if(counts>6 && (counts-7)%8==0){
  801.                 sim << data[counts];
  802.             }
  803.             else{
  804.                 sim << data[counts] << "\t";        //prints all of the data that goes in each line
  805.             }
  806.             counts++;
  807.         }
  808.         cycle++;
  809.         if (isJumping){                     // if there is a jump, then update the PC to the new PC (from counter2)
  810.             programCntr = counter2;         //set isJumping to false so we can continue to check for jumps later
  811.             isJumping = false;
  812.         }
  813.         programCntr += 4;                   //increment the PC
  814.         PCOne = firstLocation;              //reset the firstLocation
  815.         sim << endl;
  816.         sim << endl;
  817.         iWhile++;                                //increment the while loop
  818.     }
  819.     sim.close();
  820. }
  821.  
  822. int main(int argc, char** argv){
  823.     //reads in the file
  824.     ifstream inFile (argv[1]);
  825.     /*error if file can't be opened
  826.      while( !inFile.is_open() ) {
  827.      cout << inFile << " does not exist. Please try again: ";
  828.      cin >> inFile;
  829.      cout << endl;
  830.      inFile.open(inFile.c_str(), ifstream::in);
  831.      }*/
  832.     ofstream  disassembly ("disassembly.txt");
  833.     string currentLine = "";                    //reads the current line of the input file
  834.     int numOfData = 0;                          //# of instructions and data
  835.     int numOfInst = 0;
  836.     int programCounter = 64;                    //program counter starts at 64
  837.    
  838.     while ( getline(inFile,currentLine) ){      //while there are still lines in the input file
  839.         string cat = currentLine.substr(0,3);   //categorizes the instruction into 1, 2, 3, or data
  840.         stringstream temp;                      //convert programCounter to a string
  841.         temp << programCounter;
  842.         string programCounterStr= temp.str();
  843.         if (cat == "001"){                              //category 1
  844.             string output = currentLine + "\t" + programCounterStr + "\t";
  845.             string opcode = currentLine.substr(3,3);
  846.             int opcodeNum;
  847.             istringstream (opcode) >> opcodeNum;
  848.             switch(opcodeNum){
  849.                 case 0: output += NOP();
  850.                     break;
  851.                 case 1: output += J(currentLine);
  852.                     break;
  853.                 case 10:output += BEQ(currentLine);
  854.                     break;
  855.                 case 11: output += BNE(currentLine);
  856.                     break;
  857.                 case 100: output += BGTZ(currentLine);
  858.                     break;
  859.                 case 101: output += SW(currentLine);
  860.                     break;
  861.                 case 110: output += LW(currentLine);
  862.                     break;
  863.                 case 111: output += BREAK();
  864.                     break;
  865.                 default:  break;
  866.             }
  867.             disassembly << output << endl;                      //print out to dissembly.txt
  868.             numOfInst++;                                        //increment instruction count
  869.         }
  870.         else if (cat == "010"){                                //category 2
  871.             string output = currentLine + "\t" + programCounterStr + "\t";
  872.             string opcode = currentLine.substr(3,3);           //determines instruction type from the opcode
  873.             if(opcode == "000"){
  874.                 output += "XOR R";
  875.             }
  876.             if(opcode == "001"){
  877.                 output += "MUL R";
  878.             }
  879.             if(opcode == "010"){
  880.                 output += "ADD R";
  881.             }
  882.             if(opcode == "011"){
  883.                 output += "SUB R";
  884.             }
  885.             if(opcode == "100"){
  886.                 output += "AND R";
  887.             }
  888.             if(opcode == "101"){
  889.                 output += "OR R";
  890.             }
  891.             if(opcode == "110"){
  892.                 output += "ADDU R";
  893.             }
  894.             if(opcode == "111"){
  895.                 output += "SUBU R";
  896.             }
  897.             output += category2(currentLine);
  898.             disassembly << output << endl;                              //print out to dissembly.txt
  899.             numOfInst++;                                                //increment instruction count
  900.         }
  901.         else if (cat == "100"){                                         //category 3
  902.             string output = currentLine + "\t" + programCounterStr + "\t";
  903.             string opcode = currentLine.substr(3,3);                    //determines instruction type from the opcode
  904.             if(opcode == "000"){
  905.                 output += "ORI R";
  906.             }
  907.             if(opcode == "001"){
  908.                 output += "XORI R";
  909.             }
  910.             if(opcode == "010"){
  911.                 output += "ADDI R";
  912.             }
  913.             if(opcode == "011"){
  914.                 output += "SUBI R";
  915.             }
  916.             if(opcode == "100"){
  917.                 output += "ANDI R";
  918.             }
  919.             if(opcode == "101"){
  920.                 output += "SRL R";
  921.             }
  922.             if(opcode == "110"){
  923.                 output += "SRA R";
  924.             }
  925.             if(opcode == "111"){
  926.                 output += "SLL R";
  927.             }
  928.             output += category3(currentLine);
  929.             disassembly << output << endl;                      //print out to dissembly.txt
  930.             numOfInst++;                                        //increment instruction count
  931.         }
  932.         else{                                                     //if it's data, just print
  933.             disassembly << currentLine << "\t" << programCounterStr << "\t" << convertBinaryToDec(currentLine) << endl;//print to dissembly.txt
  934.             numOfData++;            //incrememnt data count
  935.         }
  936.        
  937.         programCounter += 4;                                    //incrememnt PC by 4 after every instruction
  938.         stringstream temp2;                                     //converts programCounter into a string (again)
  939.         temp2 << programCounter;
  940.         programCounterStr= temp2.str();
  941.        
  942.     }
  943.     disassembly.close();
  944.     /*    //array with all of the instructions (binary) ----------------------check if this works - jk, im not doing this-----------------------------
  945.      int binaryInstructions[numOfInst];
  946.      string currentLine2 = "";
  947.      int counterIns = 0;
  948.      while (getline(inFile,currentLine2)){
  949.      if (counterIns < 32){
  950.      binaryInstructions[i] = currentLine2;
  951.      counterIns++;
  952.      }
  953.      }*/
  954.     ifstream fileTwo ("disassembly.txt");
  955.     int registers[32];                                          //create and initialize 32 registers to 0 [0-31]
  956.     for(int i = 0; i < 32 ; i++){
  957.         registers[i] = 0;
  958.     }
  959.     string line;                                                 //line from disassembly file
  960.     int pc = 64;                                                 //program counter starts at "64"
  961.     string *inst = new string[numOfInst];
  962.     //array of the listed instructions and data & its indecies
  963.     int data[numOfData];
  964.     int indexOfInstruction = 0;
  965.     int indexOfData = 0;
  966.     bool isInstruction = true;      //true if the line is an inst (the first few lines)
  967.     int PCOne = 0;                  //location of 1st data
  968.    
  969.     while ( getline(fileTwo,line) ){                              //saves inst & data into respective arrays
  970.         if(isInstruction){                                       //if the line's an instruction
  971.             string temp;
  972.             for (int i = 0; i < line.length(); i++){
  973.                 if (isalpha( line[i]) ){                        //if there's a letter than it's an inst; save the inst ONLY into array
  974.                     temp = line.substr(i,(line.size() - i));
  975.                     break;
  976.                 }
  977.             }
  978.             inst[indexOfInstruction] = temp;        //put into array
  979.             indexOfInstruction++;                   //increment the index of the instruction
  980.         }
  981.         else{                                       //the line is data, rather than an instruction
  982.             if (indexOfData == 0){                  //first data being inputted
  983.                 PCOne = pc;
  984.             }
  985.             string temp2 = line.substr(0,32);
  986.             int value;
  987.             istringstream( convertBinaryToDec(temp2) ) >> value;
  988.             data[indexOfData] = value;                      //put into array
  989.             indexOfData++;                                   //increment the index of the data
  990.         }
  991.         if (line.substr(0,6) == "001111"){                        //if the inst is BREAK
  992.             isInstruction = false;
  993.         }
  994.         pc += 4;                                                    //increment the PC by 4
  995.     }
  996.    
  997.     Simulator(registers, inst, data, PCOne, numOfData, numOfInst);
  998.     fileTwo.close();
  999.     return 0;
  1000. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement