Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <map>
  6. #include <vector>
  7. #include "SPRContainer.h"
  8. using namespace std;
  9. int b2d(string b);
  10. int findval(int n, vector<int> temp);
  11. int main()
  12. {
  13. bool fileopen = false;
  14. string opcode;
  15. string val;
  16. int ac = 0;
  17. int pc = 0;
  18. int i = 0;
  19. string reg;
  20. string sim;
  21. vector<int> looper;
  22. string ifile;
  23. ifstream fin;
  24. SPRContainer spr;
  25. vector<int> temp;
  26.  
  27. map<string, int> Ans;
  28.  
  29. Ans["00000000"] = 0; //Didn't quite understand how substr() worked, I thought it was
  30. Ans["00000001"] = 0; //string.substr(begin, end), instead of (begin,length), so I padded all
  31. Ans["00000010"] = 0; //of these in an older version of the code to get them to work.
  32. Ans["00000011"] = 0; //I could totally un-pad them, but there's no reason to do so as far as I can tell
  33. Ans["00000100"] = 0; //program runs fine with them as is, and rewriting this section seems like more work than it's worth.
  34. Ans["00000101"] = 0;
  35. Ans["00000110"] = 0;
  36. Ans["00000111"] = 0;
  37.  
  38. while(true)
  39. {
  40. while(!fileopen)
  41. {
  42. cout << "Please enter input file name:\t";
  43. getline(cin, ifile);
  44. fin.open(ifile);
  45. if(fin.fail()) // always make sure the file was opened properly
  46. {
  47. cout << "File open failed\n";
  48. fin.clear();
  49. fin.ignore();
  50. continue;
  51. }
  52. fileopen = true;
  53. break;
  54. }
  55. while(!fin.eof())
  56. {
  57. fin >> sim;
  58. //cout << pc << ":\t";
  59. opcode = sim.substr(0,4);
  60. //cout << opcode << "\t";
  61. if(opcode == "0000") //STO
  62. {
  63. //cout << "STO";
  64. reg = sim.substr(4,8);
  65. Ans[reg] = ac;
  66. //cout << Ans[reg];
  67. }
  68. else if(opcode == "0001")//PUT
  69. {
  70. //cout << "PUT: ";
  71. val = sim.substr(4,8);
  72. reg = sim.substr(12,8);
  73. //cout << reg << "\t";
  74. Ans[reg] = b2d(val);
  75. //cout << Ans[reg];
  76. }
  77. else if(opcode == "0010")//OUT
  78. {
  79. //cout << "OUT: ";
  80. if(sim.substr(12,8) == "11111111")
  81. {
  82. cout << Ans[sim.substr(4,8)];
  83. }
  84. else if(sim.substr(12,8) == "00000000")
  85. {
  86. val = sim.substr(4,8);
  87. cout << char(b2d(val));
  88. }
  89. else
  90. {
  91. cout << ac;
  92. }
  93. }
  94. else if(opcode == "0011")//ADD
  95. {
  96. //cout << "ADD: ";
  97. reg = sim.substr(4,8);
  98. //cout << reg << "\t";
  99. ac += Ans[reg];
  100. //cout << "aaa" << endl;
  101. }
  102. else if(opcode == "0100")//SUB
  103. {
  104. reg = sim.substr(4,8);
  105. val = Ans[reg];
  106. ac -= b2d(val);
  107. }
  108.  
  109. else if(opcode == "0110")//LOAD
  110. {
  111. reg = sim.substr(4,8);
  112. ac = Ans[reg];
  113. }
  114.  
  115. else if(opcode == "0111") //INPUT
  116. {
  117. //cout << "INPUT!" << endl;
  118. cin >> ac;
  119. //cout << "aaa" << endl;
  120. }
  121. else if(opcode == "1000")//CLEAR
  122. {
  123. //cout << "CLEAR";
  124. Ans["00000000"] = 0;
  125. Ans["00000001"] = 0;
  126. Ans["00000010"] = 0;
  127. Ans["00000011"] = 0;
  128. Ans["00000100"] = 0;
  129. Ans["00000101"] = 0;
  130. Ans["00000110"] = 0;
  131. Ans["00000111"] = 0;
  132. spr.clear();
  133. }
  134. else if (opcode =="0101")//Halt
  135. {
  136. break;
  137. }
  138. else if (opcode == "1001")//LOOP
  139. {
  140.  
  141. //cout << "LOOPING!" << endl;
  142. for(int i = 1; i < Ans[sim.substr(4,8)]; i++)
  143. {
  144. //cout << "HERE" << endl;
  145. looper.push_back(fin.tellg());
  146. //cout << "LOOP: " << looper.at(0) << endl;
  147. }
  148. looper.push_back(-1);
  149. }
  150. else if (opcode == "1010") //LOOP_
  151. {
  152. if(looper.size() > 0)
  153. {
  154. if(looper.at(0) != -1)
  155. {
  156. fin.seekg(looper.at(0)-16, fin.beg);//For some reason it was skipping a command. Going back 16 bits fixes the issue.
  157. }
  158. looper.erase(looper.begin());
  159. }
  160. }
  161. else if (opcode == "1011")//ARRY
  162. {
  163. spr.add(sim.substr(4,8));
  164. }
  165. else if(opcode == "1101")//ADDARRY
  166. {
  167. reg = sim.substr(4,8);
  168. temp = spr.getlistitem(sim.substr(12,8)).Getvals();
  169. temp.push_back(Ans[reg]);
  170. spr.overwritelist(sim.substr(12,8), temp);
  171.  
  172. }
  173. else if(opcode == "1100")//LOOK
  174. {
  175. temp = spr.getlistitem(sim.substr(4,8)).Getvals();
  176. if(sim.substr(12,8) == "11111111")
  177. {
  178. cout << "Result:\t" << findval(ac, temp) << endl;
  179. }
  180. else
  181. {
  182. cout << "Result:\t" << findval(Ans[sim.substr(12,8)], temp) << endl;
  183. }
  184. }
  185. else
  186. {
  187. cout << "An exception occurred. Check the Assembler. Maybe the function isn't matched to an opcode? \nOPCODE:\t" << opcode << endl << "PC:\t" << pc << endl;
  188. throw std::exception();
  189. }
  190. pc++;
  191. //cout << endl;
  192. }
  193. cout << endl << endl << "Program \"" << ifile << "\" ran successfully." << endl << endl;
  194. fileopen = false;
  195. fin.clear();
  196. fin.ignore();
  197. fin.close();
  198. cin.get();
  199. }
  200. return 0;
  201. }
  202. int b2d(string b)
  203. {
  204. int d = 0;
  205. for(int i = b.size()- 1; i >= 0; i--)
  206. {
  207. if(b[i] == '1')
  208. {
  209. d += pow(2, 7-i);
  210. }
  211. }
  212. //cout << d << endl;
  213. return d;
  214.  
  215. }
  216. int findval(int n, vector<int> temp)
  217. {
  218. int found = -1;
  219. for(int i = 0; i < temp.size(); i++)
  220. {
  221. if(temp.at(i) == n)
  222. {
  223. found = i;
  224. break;
  225. }
  226. }
  227. return found;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement