csansoon

SISA Translator [WIP - Updated]

Nov 14th, 2018
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7.  
  8.  
  9.  
  10.  
  11. int bin_to_dec(int n)
  12. {
  13.     int decimalNumber = 0, i = 0, remainder;
  14.     while (n!=0)
  15.     {
  16.         remainder = n%10;
  17.         n /= 10;
  18.         decimalNumber += remainder*pow(2,i);
  19.         ++i;
  20.     }
  21.     return decimalNumber;
  22. }
  23.  
  24.  
  25.  
  26. void say_hex(const vector <int>& s) {
  27.     cout<<"]: 0x";
  28.     int k=0;
  29.     for (int i=0; i<4; i++) {
  30.         int binnum=0;
  31.         for (int j=0; j<4; j++) { //Creates binnum (4 times)
  32.             binnum=binnum*10;
  33.             binnum=binnum+s[k];
  34.             k++;
  35.         }
  36.         binnum=bin_to_dec(binnum);
  37.         if (binnum==10) cout<<"A";
  38.         else if (binnum==11) cout<<"B";
  39.         else if (binnum==12) cout<<"C";
  40.         else if (binnum==13) cout<<"D";
  41.         else if (binnum==14) cout<<"E";
  42.         else if (binnum==15) cout<<"F";
  43.         else cout<<binnum;
  44.     }
  45. }
  46.  
  47.  
  48.  
  49. int dec_to_bin(int n)
  50. {
  51.     int binaryNumber = 0;
  52.     int remainder, i = 1, step = 1;
  53.  
  54.     while (n!=0)
  55.     {
  56.         remainder = n%2;
  57.         n /= 2;
  58.         binaryNumber += remainder*i;
  59.         i *= 10;
  60.     }
  61.     return binaryNumber;
  62. }
  63.  
  64.  
  65.  
  66. int dec_to_ca2_6bits(int n)
  67. {  
  68.     int n2=n;
  69.     if (n<0) n2=64+n;
  70.     int binaryNumber = 0;
  71.     int remainder, i = 1, step = 1;
  72.  
  73.     while (n2!=0)
  74.     {
  75.         remainder = n2%2;
  76.         n2 /= 2;
  77.         binaryNumber += remainder*i;
  78.         i *= 10;
  79.     }
  80.     return binaryNumber;
  81. }
  82.  
  83.  
  84.  
  85. void save(vector <int>& v, int n) {
  86.     for (int i=((v.size())-1); i>=0; i--) {
  87.                 v[i] = n%10;
  88.                 n /= 10;
  89.             }
  90. }
  91.  
  92. bool write_3(vector <int>& R) {
  93.     int input_int=0;
  94.     cin>>input_int;
  95.     if (input_int<0 || input_int>=8) {cout<<"!X!]: Number must be between 0 and 7"<<endl; return true;}
  96.     cout<<input_int;
  97.     input_int = dec_to_bin(input_int);
  98.     save(R,input_int);
  99.     return false;
  100. }
  101.  
  102.  
  103.  
  104. bool write_6(vector <int>& N) {
  105.     int input_int;
  106.     cin>>input_int; //Input N6
  107.     if (input_int >= 32 || input_int < -32) {cout<<"!X!]: N must be a number between -32 and 31"<<endl; return true;}
  108.     cout<<input_int;
  109.     input_int = dec_to_ca2_6bits(input_int);
  110.     save(N,input_int);
  111.     return false;
  112. }
  113.  
  114.  
  115. bool enter(char c) {
  116.     char input_char;
  117.     cin>>input_char;
  118.             if (input_char!=c) {cout<<"!X!]: ERROR: Expected a '"<<c<<"'"<<endl; return true;}
  119.             cout<<input_char;
  120.             return false;
  121. }
  122.  
  123.  
  124.  
  125. bool introduce_hex(vector <int>& FINAL) {
  126.     for (int i=0;i<16;i=i+4) {
  127.         int input_int=0;
  128.         char input_char;
  129.         cin>>input_char;
  130.         if (input_char=='0') input_int=0;
  131.         else if (input_char=='1') input_int=1;
  132.         else if (input_char=='2') input_int=2;
  133.         else if (input_char=='3') input_int=3;
  134.         else if (input_char=='4') input_int=4;
  135.         else if (input_char=='5') input_int=5;
  136.         else if (input_char=='6') input_int=6;
  137.         else if (input_char=='7') input_int=7;
  138.         else if (input_char=='8') input_int=8;
  139.         else if (input_char=='9') input_int=9;
  140.         else if (input_char=='A') input_int=10;
  141.         else if (input_char=='B') input_int=11;
  142.         else if (input_char=='C') input_int=12;
  143.         else if (input_char=='D') input_int=13;
  144.         else if (input_char=='E') input_int=14;
  145.         else if (input_char=='F') input_int=15;
  146.         else {cout<<"!X!]: ERROR: INVALID INPUT"<<endl; return true;}
  147.         cout<<input_char;
  148.         input_int = dec_to_bin(input_int);
  149.         for (int j=0; j<4; j++) {
  150.             FINAL[i+3-j] = input_int%10;
  151.             input_int = input_int/10;
  152.         }
  153.     }
  154.         return false;
  155. }
  156.  
  157.  
  158.  
  159. void move_to(vector <int>& FINAL, int n, vector <int>& R) {
  160.     for (int i=0; i<R.size(); i++) {
  161.         R[i] = FINAL[i+n];
  162.     }
  163. }
  164.  
  165.  
  166.  
  167. void convert(vector <int>& R, int& input_int) {
  168.     input_int=0;
  169.     for (int i=0; i<R.size(); i++) {
  170.         input_int=input_int*10;
  171.         input_int=input_int+R[i];
  172.     }
  173. }
  174.  
  175.  
  176.  
  177. void convert_ca2(vector <int>& N, int& input_int) {
  178.     input_int=0;
  179.     for (int i=0; i<N.size(); i++) {
  180.         input_int=input_int*10;
  181.         input_int=input_int+N[i];
  182.     }
  183.     if (N[0]==1) {
  184.         input_int = -32 + bin_to_dec(input_int%100000);
  185.     }
  186.     else input_int=bin_to_dec(input_int);
  187. }
  188.    
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. void mnemo_to_hex() {
  199.     string mnemo;
  200.     char input_char;
  201.     int input_int;
  202.     vector <int> Ra(3,0);
  203.     vector <int> Rb(3,0);
  204.     vector <int> Rd(3,0);
  205.     vector <int> F(3,0);
  206.     vector <int> N(6,0);
  207.     vector <int> FINAL(16,0);
  208.     cout << "[ENTER MNEMONIC]: ";
  209.     cin>>mnemo;
  210.     if (mnemo=="LD") {
  211.             cout<<"["<<mnemo<<" ";
  212.             if (enter('R')) return;
  213.             if (write_3(Rd)) return;
  214.             if (enter(',')) return;
  215.             if (write_6(N)) return;
  216.             if (enter('(')) return;
  217.             if (enter('R')) return;
  218.             if (write_3(Ra)) return;
  219.             if (enter(')')) return;
  220.             for (int i=0; i<6; i++) FINAL[10+i] = N[i];
  221.             for (int i=0; i<3; i++) FINAL[7+i] = Rd[i];
  222.             for (int i=0; i<3; i++) FINAL[4+i] = Ra[i];
  223.             FINAL[0]=0;
  224.             FINAL[1]=0;
  225.             FINAL[2]=1;
  226.             FINAL[3]=1;
  227.             say_hex(FINAL);
  228.             cout<<endl;
  229.         }
  230.        
  231.         else if (mnemo=="ST") {
  232.             cout<<"["<<mnemo<<" ";
  233.             if (write_6(N)) return;
  234.             if (enter('(')) return;
  235.             if (enter('R')) return;
  236.             if (write_3(Ra)) return;
  237.             if (enter(')')) return;
  238.             if (enter(',')) return;
  239.             if (enter('R')) return;
  240.             if (write_3(Rb)) return;
  241.             for (int i=0; i<6; i++) FINAL[10+i] = N[i];
  242.             for (int i=0; i<3; i++) FINAL[7+i] = Rb[i];
  243.             for (int i=0; i<3; i++) FINAL[4+i] = Ra[i];
  244.             FINAL[0]=0;
  245.             FINAL[1]=1;
  246.             FINAL[2]=0;
  247.             FINAL[3]=0;
  248.             say_hex(FINAL);
  249.             cout<<endl;
  250.         }
  251.        
  252.         else if (mnemo=="LDB") {
  253.             cout<<"["<<mnemo<<" ";
  254.             if (enter('R')) return;
  255.             if (write_3(Rd)) return;
  256.             if (enter(',')) return;
  257.             if (write_6(N)) return;
  258.             if (enter('(')) return;
  259.             if (enter('R')) return;
  260.             if (write_3(Ra)) return;
  261.             if (enter(')')) return;
  262.             for (int i=0; i<6; i++) FINAL[10+i] = N[i];
  263.             for (int i=0; i<3; i++) FINAL[7+i] = Rd[i];
  264.             for (int i=0; i<3; i++) FINAL[4+i] = Ra[i];
  265.             FINAL[0]=0;
  266.             FINAL[1]=1;
  267.             FINAL[2]=0;
  268.             FINAL[3]=1;
  269.             say_hex(FINAL);
  270.             cout<<endl;
  271.         }
  272.        
  273.         else if (mnemo=="STB") {
  274.             cout<<"["<<mnemo<<" ";
  275.             if (write_6(N)) return;
  276.             if (enter('(')) return;
  277.             if (enter('R')) return;
  278.             if (write_3(Ra)) return;
  279.             if (enter(')')) return;
  280.             if (enter(',')) return;
  281.             if (enter('R')) return;
  282.             if (write_3(Rb)) return;
  283.             for (int i=0; i<6; i++) FINAL[10+i] = N[i];
  284.             for (int i=0; i<3; i++) FINAL[7+i] = Rb[i];
  285.             for (int i=0; i<3; i++) FINAL[4+i] = Ra[i];
  286.             FINAL[0]=0;
  287.             FINAL[1]=1;
  288.             FINAL[2]=1;
  289.             FINAL[3]=0;
  290.             say_hex(FINAL);
  291.             cout<<endl;
  292.         }
  293.        
  294.         else if (mnemo=="AND" || mnemo=="OR" || mnemo=="XOR" || mnemo=="NOT" || mnemo=="ADD" || mnemo=="SUB" || mnemo=="SHA" || mnemo=="SHL") {
  295.             cout<<"["<<mnemo<<" ";
  296.             if (enter('R')) return;
  297.             if (write_3(Rd)) return;
  298.             if (enter(',')) return;
  299.             if (enter('R')) return;
  300.             if (write_3(Ra)) return;
  301.             if (enter(',')) return;
  302.             if (enter('R')) return;
  303.             if (write_3(Rb)) return;
  304.             for (int i=0; i<3; i++) FINAL[4+i] = Ra[i];
  305.             for (int i=0; i<3; i++) FINAL[7+i] = Rb[i];
  306.             for (int i=0; i<3; i++) FINAL[10+i] = Rd[i];
  307.             if (mnemo=="AND") {FINAL[13]=0;FINAL[14]=0;FINAL[15]=0;}
  308.             else if (mnemo=="OR") {FINAL[13]=0;FINAL[14]=0;FINAL[15]=1;}
  309.             else if (mnemo=="XOR") {FINAL[13]=0;FINAL[14]=1;FINAL[15]=0;}
  310.             else if (mnemo=="NOT") {FINAL[13]=0;FINAL[14]=1;FINAL[15]=1;}
  311.             else if (mnemo=="ADD") {FINAL[13]=1;FINAL[14]=0;FINAL[15]=0;}
  312.             else if (mnemo=="SUB") {FINAL[13]=1;FINAL[14]=0;FINAL[15]=1;}
  313.             else if (mnemo=="SHA") {FINAL[13]=1;FINAL[14]=1;FINAL[15]=0;}
  314.             else if (mnemo=="SHL") {FINAL[13]=1;FINAL[14]=1;FINAL[15]=1;}
  315.             FINAL[0]=0;
  316.             FINAL[1]=0;
  317.             FINAL[2]=0;
  318.             FINAL[3]=0;
  319.             say_hex(FINAL);
  320.             cout<<endl;
  321.         }
  322.            
  323.            
  324.            
  325.         else {
  326.             cout<<"INVALID MNEMONIC";
  327.             return;
  328.     }
  329. }
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340. void hex_to_mnemo() {
  341.     string mnemo;
  342.     char input_char;
  343.     int input_int=0;
  344.     vector <int> Ra(3,0);
  345.     vector <int> Rb(3,0);
  346.     vector <int> Rd(3,0);
  347.     vector <int> F(3,0);
  348.     vector <int> N(6,0);
  349.     vector <int> FINAL(16,0);
  350.     cout << "[ENTER HEXADECIMAL]: ";
  351.     cin>>input_char;
  352.     if (input_char!='0') {cout<<"!X!]: ERROR: Expected a '0'"<<endl; return;}
  353.     cout<<"["<<input_char;
  354.     if (enter('x')) return;
  355.     if (introduce_hex(FINAL)) return;
  356.     cout<<"]: ";
  357.     for (int i=0; i<4; i++) {
  358.         input_int=input_int*10;
  359.         input_int=input_int+FINAL[i];
  360.     }
  361.     input_int=bin_to_dec(input_int);
  362.     if (input_int==0) {
  363.         move_to(FINAL,4,Ra);
  364.         move_to(FINAL,7,Rb);
  365.         move_to(FINAL,10,Rd);
  366.         move_to(FINAL,13,F);
  367.         convert(F,input_int);
  368.         input_int=bin_to_dec(input_int);
  369.         switch (input_int) {
  370.             case 0: cout<<"AND "; break;
  371.             case 1: cout<<"OR "; break;
  372.             case 2: cout<<"XOR "; break;
  373.             case 3: cout<<"NOT "; break;
  374.             case 4: cout<<"ADD "; break;
  375.             case 5: cout<<"SUB "; break;
  376.             case 6: cout<<"SHA "; break;
  377.             case 7: cout<<"SHL "; break;
  378.         }
  379.         convert(Rd,input_int);
  380.         cout<<"R"<<bin_to_dec(input_int)<<",";
  381.         convert(Ra,input_int);
  382.         cout<<"R"<<bin_to_dec(input_int)<<",";
  383.         convert(Rb,input_int);
  384.         cout<<"R"<<bin_to_dec(input_int)<<endl;
  385.         return;
  386.     }
  387.     else if (input_int==1) {}
  388.     else if (input_int==2) {}
  389.     else if (input_int==3) {
  390.         cout<<"LD ";
  391.         move_to(FINAL,4,Ra);
  392.         move_to(FINAL,7,Rd);
  393.         move_to(FINAL,10,N);
  394.         convert(Rd,input_int);
  395.         cout<<"R"<<bin_to_dec(input_int)<<",";
  396.         convert_ca2(N,input_int);
  397.         cout<<input_int;
  398.         convert(Ra,input_int);
  399.         cout<<"(R"<<bin_to_dec(input_int)<<")"<<endl;
  400.         return;
  401.         }
  402.     else if (input_int==4) {
  403.         cout<<"ST ";
  404.         move_to(FINAL,4,Ra);
  405.         move_to(FINAL,7,Rb);
  406.         move_to(FINAL,10,N);
  407.         convert_ca2(N,input_int);
  408.         cout<<input_int;
  409.         convert(Ra,input_int);
  410.         cout<<"(R"<<bin_to_dec(input_int)<<")";
  411.         convert(Rb,input_int);
  412.         cout<<",R"<<bin_to_dec(input_int)<<endl;
  413.         return;
  414.         }
  415.     else if (input_int==5) {
  416.         cout<<"LDB ";
  417.         move_to(FINAL,4,Ra);
  418.         move_to(FINAL,7,Rd);
  419.         move_to(FINAL,10,N);
  420.         convert(Rd,input_int);
  421.         cout<<"R"<<bin_to_dec(input_int)<<",";
  422.         convert_ca2(N,input_int);
  423.         cout<<input_int;
  424.         convert(Ra,input_int);
  425.         cout<<"(R"<<bin_to_dec(input_int)<<")"<<endl;
  426.         return;
  427.         }
  428.     else if (input_int==6) {
  429.         cout<<"STB ";
  430.         move_to(FINAL,4,Ra);
  431.         move_to(FINAL,7,Rb);
  432.         move_to(FINAL,10,N);
  433.         convert_ca2(N,input_int);
  434.         cout<<input_int;
  435.         convert(Ra,input_int);
  436.         cout<<"(R"<<bin_to_dec(input_int)<<")";
  437.         convert(Rb,input_int);
  438.         cout<<",R"<<bin_to_dec(input_int)<<endl;
  439.         return;
  440.         }
  441.     else if (input_int==7) {}
  442.     else if (input_int==8) {}
  443.     else if (input_int==9) {}
  444.     else if (input_int==10) {}
  445.     else if (input_int==11) {}
  446.     else if (input_int==12) {}
  447.     else if (input_int==13) {}
  448.     else if (input_int==14) {}
  449.     else if (input_int==15) {}
  450.     else {
  451.         cout<<"!X!]: ERROR: INVALID INPUT"<<endl;
  452.         return;
  453.     }
  454. }
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465. void help() {
  466.     string mnemo;
  467.     cout<<"['MNEMONIC' / ALL]: ";
  468.     cin>>mnemo;
  469.     if (mnemo=="ALL") {
  470.     cout<<"AND Rd,Ra,Rb"<<endl;
  471.     cout<<"OR Rd,Ra,Rb"<<endl;
  472.     cout<<"XOR Rd,Ra,Rb"<<endl;
  473.     cout<<"NOT Rd,Ra,Rb"<<endl;
  474.     cout<<"ADD Rd,Ra,Rb"<<endl;
  475.     cout<<"SUB Rd,Ra,Rb"<<endl;
  476.     cout<<"SHA Rd,Ra,Rb"<<endl;
  477.     cout<<"SHL Rd,Ra,Rb"<<endl;
  478.     cout<<"CMPLT Rd,Ra,Rb"<<endl;
  479.     cout<<"CMPLE Rd,Ra,Rb"<<endl;
  480.     cout<<"CMPEQ Rd,Ra,Rb"<<endl;
  481.     cout<<"CMPLTU Rd,Ra,Rb"<<endl;
  482.     cout<<"CMPLEU Rd,Ra,Rb"<<endl;
  483.     cout<<"LD Rd,6N(Ra)"<<endl;
  484.     cout<<"ST 6N(Ra),Rd"<<endl;
  485.     cout<<"LDB Rd,6N(Ra)"<<endl;
  486.     cout<<"STB 6N(Ra),Rd"<<endl;
  487.     }
  488.     else if (mnemo=="AND")      cout<<"AND Rd,Ra,Rb"<<endl;
  489.     else if (mnemo=="OR")       cout<<"OR Rd,Ra,Rb"<<endl;
  490.     else if (mnemo=="XOR")      cout<<"XOR Rd,Ra,Rb"<<endl;
  491.     else if (mnemo=="NOT")      cout<<"NOT Rd,Ra,Rb"<<endl;
  492.     else if (mnemo=="ADD")      cout<<"ADD Rd,Ra,Rb"<<endl;
  493.     else if (mnemo=="SUB")      cout<<"SUB Rd,Ra,Rb"<<endl;
  494.     else if (mnemo=="SHA")      cout<<"SHA Rd,Ra,Rb"<<endl;
  495.     else if (mnemo=="SHL")      cout<<"SHL Rd,Ra,Rb"<<endl;
  496.     else if (mnemo=="CMPLT")    cout<<"CMPLT Rd,Ra,Rb"<<endl;
  497.     else if (mnemo=="CMPLE")    cout<<"CMPLE Rd,Ra,Rb"<<endl;
  498.     else if (mnemo=="CMPEQ")    cout<<"CMPEQ Rd,Ra,Rb"<<endl;
  499.     else if (mnemo=="CMPLTU")   cout<<"CMPLTU Rd,Ra,Rb"<<endl;
  500.     else if (mnemo=="CMPLEU")   cout<<"CMPLEU Rd,Ra,Rb"<<endl;
  501.     else if (mnemo=="LD")       cout<<"LD Rd,6N(Ra)"<<endl;
  502.     else if (mnemo=="ST")       cout<<"ST 6N(Ra),Rd"<<endl;
  503.     else if (mnemo=="LDB")      cout<<"LDB Rd,6N(Ra)"<<endl;
  504.     else if (mnemo=="STB")      cout<<"STB 6N(Ra),Rd"<<endl;
  505.     return;
  506. }
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517. int main() {
  518.     cout<<endl;
  519.     cout << "+=============================================+"<<endl;
  520.     cout << " Type IN to translate from MNEMONIC to HEX" << endl;
  521.     cout << " Type OUT to translate from HEX to MNEMONIC" << endl;
  522.     cout << " Type HELP to see how every MNEMONIC is written" << endl;
  523.     cout << "+=============================================+"<<endl<<endl;
  524.     cout << "[IN/OUT/HELP]: ";
  525.     string type;
  526.     while (cin >> type) {
  527.         if (type=="IN") mnemo_to_hex();
  528.         else if (type=="OUT") hex_to_mnemo();
  529.         else if (type=="HELP") help();
  530.         cout << endl << endl << "[IN/OUT/HELP]: ";
  531. }
  532. }
  533.  
  534.  
  535. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment