Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <regex>
  4. #include <fstream>
  5. #include <math.h>
  6.  
  7. // Error flag
  8. bool ERROR = false;
  9.  
  10. class Memory
  11. {
  12. public:
  13. Memory();
  14. void set();
  15. void get();
  16. void print_program();
  17. void print_to_halt();
  18.  
  19. // Registers
  20. int PC;
  21. std::string IR;
  22. int MAR;
  23. std::string MBR;
  24. int AC;
  25. int MQ;
  26. private:
  27. std::string m[1000];
  28. };
  29.  
  30. Memory memory = Memory();
  31.  
  32. bool execute(int code, int address)
  33. {
  34. // Codes:
  35. // 0 load register
  36. // 1 load memory to register
  37. // 2 load memory
  38. // 3 load |memory|
  39. // 4 load -memory
  40. // 5 load -|memory|
  41. // 6 stor
  42. // 7 add
  43. // 8 add ||
  44. // 9 sub
  45. // 10 sub ||
  46. // 11 mul
  47. // 12 div
  48. // 13 jump+
  49. // 14 jump
  50.  
  51. switch(code)
  52. {
  53. case 0:
  54. memory.AC = memory.MQ;
  55. break;
  56. case 1:
  57. memory.MAR = address;
  58. memory.get();
  59. memory.MQ = std::stoi(memory.MBR);
  60. break;
  61. case 2:
  62. memory.MAR = address;
  63. memory.get();
  64. memory.AC = std::stoi(memory.MBR);
  65. break;
  66. case 3:
  67. memory.MAR = address;
  68. memory.get();
  69. memory.AC = abs(std::stoi(memory.MBR));
  70. break;
  71. case 4:
  72. memory.MAR = address;
  73. memory.get();
  74. memory.AC = -std::stoi(memory.MBR);
  75. break;
  76. case 5:
  77. memory.MAR = address;
  78. memory.get();
  79. memory.AC = -std::stoi(memory.MBR);
  80. break;
  81. case 6:
  82. memory.MAR = address;
  83. memory.MBR = std::to_string(memory.AC);
  84. memory.set();
  85. break;
  86. case 7:
  87. memory.MAR = address;
  88. memory.get();
  89. memory.AC = memory.AC + std::stoi(memory.MBR);
  90. break;
  91. case 8:
  92. memory.MAR = address;
  93. memory.get();
  94. memory.AC = memory.AC + abs(std::stoi(memory.MBR));
  95. break;
  96. case 9:
  97. memory.MAR = address;
  98. memory.get();
  99. memory.AC = memory.AC - std::stoi(memory.MBR);
  100. break;
  101. case 10:
  102. memory.MAR = address;
  103. memory.get();
  104. memory.AC = memory.AC - abs(std::stoi(memory.MBR));
  105. break;
  106. case 11:
  107. memory.MAR = address;
  108. memory.get();
  109. memory.AC = memory.MQ * std::stoi(memory.MBR);
  110. break;
  111. case 12:
  112. memory.MAR = address;
  113. memory.get();
  114. memory.MQ = memory.AC / std::stoi(memory.MBR);
  115. memory.AC = memory.AC % std::stoi(memory.MBR);
  116. break;
  117. case 13:
  118. if(memory.AC >= 0)
  119. {
  120. memory.PC = address - 1; // -1 to account for automatic increment
  121. }
  122. break;
  123. case 14:
  124. memory.PC = address - 1; // -1 to account for automatic increment
  125. break;
  126. default:
  127. return false; // invalid code, which should never happen
  128. break;
  129. }
  130.  
  131. return true;
  132. }
  133.  
  134. bool decode()
  135. {
  136. // begin, halt, nop, and comments do NOT call the execute method
  137. if(memory.IR == "halt")
  138. {
  139. return false;
  140. }
  141. else if(memory.IR == "nop" || memory.IR == "begin" || (char) memory.IR[0] == '.')
  142. {
  143. return true;
  144. }
  145.  
  146. int code = -1;
  147. int address = -1;
  148.  
  149. //std::string command;
  150. //std::regex pattern("((load)|(stor)|(add)|(sub)|(mul)|(div)|(jump\\+)|(jump))\\s+((MQ,)|(MQ))*\\s*(-)*\\s*(\\|)*\\s*(?:M\\(\\s*(\\s*[0-9]+\\s*)\\s*\\))*\\s*(\\|)*", std::regex_constants::icase);
  151.  
  152. std::regex pattern("(((load)\\s+(((MQ,)\\s*M\\s*\\(\\s*([0-9]+)\\s*\\))|(MQ))?\\s*(-)?\\s*(\\|\\s*M\\s*\\(\\s*([0-9]+)\\s*\\)\\s*\\||M\\s*\\(\\s*([0-9]+)\\s*\\))?)|((stor)\\s+M\\s*\\(\\s*([0-9]+)\\s*\\))|((add)\\s+(\\|\\s*M\\s*\\(\\s*([0-9]+)\\s*\\)\\s*\\||M\\s*\\(\\s*([0-9]+)\\s*\\)))|((sub)\\s+(\\|\\s*M\\s*\\(\\s*([0-9]+)\\s*\\)\\s*\\||M\\s*\\(\\s*([0-9]+)\\s*\\)))|((mul)\\s+M\\s*\\(\\s*([0-9]+)\\s*\\))|((div)\\s+M\\s*\\(\\s*([0-9]+)\\s*\\))|((jump\\+)\\s+M\\s*\\(\\s*([0-9]+)\\s*\\))|((jump)\\s+M\\s*\\(\\s*([0-9]+)\\s*\\)))", std::regex_constants::icase);
  153.  
  154. std::smatch sm;
  155. std::regex_match(memory.IR, sm, pattern);
  156.  
  157. if(sm.size() > 0)
  158. {
  159. if(!std::string(sm[3]).empty())
  160. {
  161. // load
  162.  
  163. // default
  164. code = 2;
  165.  
  166. if(!std::string(sm[6]).empty())
  167. {
  168. // load sm[7] into MQ
  169. code = 1;
  170. address = std::stoi(sm[7]);
  171. }
  172. else if(!std::string(sm[8]).empty())
  173. {
  174. // load MQ into AC
  175. code = 0;
  176. address = -1;
  177. }
  178.  
  179. else if(!std::string(sm[11]).empty())
  180. {
  181. // absolute value, 11 is the address
  182. code = 3;
  183. address = std::stoi(sm[11]);
  184.  
  185. if(!std::string(sm[9]).empty())
  186. {
  187. // negative!
  188. code = 5;
  189. }
  190. }
  191.  
  192. else if(!std::string(sm[9]).empty())
  193. {
  194. // negative!
  195. code = 4;
  196.  
  197. if(address == -1)
  198. {
  199. // default address for loading memory into AC
  200. address = std::stoi(sm[12]);
  201. }
  202. }
  203.  
  204. if(address == -1 && code == 2)
  205. {
  206. // default address for loading memory into AC
  207. address = std::stoi(sm[12]);
  208. }
  209. }
  210. else if(!std::string(sm[14]).empty())
  211. {
  212. // stor
  213. code = 6;
  214. address = std::stoi(sm[15]);
  215. }
  216. else if(!std::string(sm[17]).empty())
  217. {
  218. // add
  219. code = 7;
  220. address = std::stoi(sm[20]);
  221.  
  222. if(!std::string(sm[19]).empty())
  223. {
  224. // add absolute value
  225. code = 8;
  226. address = std::stoi(sm[19]);
  227. }
  228. }
  229.  
  230. else if(!std::string(sm[22]).empty())
  231. {
  232. // sub
  233. code = 9;
  234. address = std::stoi(sm[25]);
  235.  
  236. if(!std::string(sm[24]).empty())
  237. {
  238. // sub absolute value
  239. code = 10;
  240. address = std::stoi(sm[24]);
  241. }
  242. }
  243.  
  244. else if(!std::string(sm[27]).empty())
  245. {
  246. // mul
  247. code = 11;
  248. address = std::stoi(sm[28]);
  249. }
  250.  
  251. else if(!std::string(sm[30]).empty())
  252. {
  253. // div
  254. code = 12;
  255. address = std::stoi(sm[31]);
  256. }
  257.  
  258. else if(!std::string(sm[33]).empty())
  259. {
  260. // jump+
  261. code = 13;
  262. address = std::stoi(sm[34]);
  263. }
  264.  
  265. else if(!std::string(sm[36]).empty())
  266. {
  267. // jump
  268. code = 14;
  269. address = std::stoi(sm[37]);
  270. }
  271. }
  272. else
  273. {
  274. ERROR = true;
  275. return false;
  276. }
  277.  
  278. return execute(code, address);
  279. }
  280.  
  281. Memory::Memory()
  282. {
  283. MBR = "nop";
  284.  
  285. for (MAR = 0; MAR < 1000; MAR++)
  286. {
  287. this->set();
  288. }
  289. }
  290.  
  291. void Memory::set()
  292. {
  293. m[MAR] = MBR;
  294. }
  295.  
  296. void Memory::get()
  297. {
  298. MBR = m[MAR];
  299. }
  300.  
  301. void Memory::print_program()
  302. {
  303. std::cout << "Program:" << std::endl;
  304. for(int i=0; i < 1000; i++)
  305. {
  306. if(m[i] == "nop")
  307. {
  308. continue;
  309. }
  310.  
  311. std::cout << i << " " << m[i] << std::endl;
  312. }
  313. }
  314.  
  315. void Memory::print_to_halt()
  316. {
  317. std::cout << "Memory:" << std::endl;
  318. for(int i=0; i < 1000; i++)
  319. {
  320. std::cout << i << " " << m[i] << std::endl;
  321.  
  322. if(m[i] == "halt")
  323. {
  324. break;
  325. }
  326. }
  327. }
  328.  
  329. int main()
  330. {
  331. // Initialize Registers
  332. memory.PC = 0;
  333. memory.MAR = 0;
  334. memory.AC = 0;
  335. memory.MQ = 0;
  336.  
  337. // Set the line where "begin" should be to an invalid address
  338. int start_memory_address = -1;
  339.  
  340. bool valid = false;
  341.  
  342. int address;
  343. std::string content;
  344.  
  345. std::string filename;
  346. std::cout << "Please enter a filename: ";
  347. std::cin >> filename;
  348.  
  349. std::ifstream infile(filename);
  350.  
  351. // Setup memory
  352. std::string line;
  353.  
  354. std::regex line_pattern("([0-9]+)\\s+(.+)\\s*");
  355. std::smatch sm;
  356.  
  357. if(infile)
  358. {
  359. while (std::getline(infile, line).good())
  360. {
  361. //std::cout << "Parsing" << std::endl;
  362. std::regex_match(line,sm,line_pattern);
  363.  
  364. if(sm.size())
  365. {
  366. address = std::stoi(sm.str(1));
  367. content = sm.str(2);
  368.  
  369. if(content == "begin")
  370. {
  371. start_memory_address = address;
  372. }
  373.  
  374. if(content == "halt")
  375. {
  376. valid = true;
  377. }
  378.  
  379. //std::cout << MAR << std::endl;
  380. memory.MAR = address;
  381. memory.MBR = content;
  382. memory.set();
  383. }
  384. }
  385. }
  386. else
  387. {
  388. std::cout << "Couldn't read file." << std::endl;
  389. }
  390.  
  391. memory.print_program();
  392. std::cout << std::endl;
  393.  
  394. if(!(start_memory_address == -1)) // don't run if halt was never set
  395. {
  396. memory.PC = start_memory_address;
  397.  
  398. std::cout << "Program Execution:" << std::endl;
  399.  
  400. while(valid)
  401. {
  402. // Fetch
  403. memory.MAR = memory.PC;
  404. memory.get();
  405. memory.IR = memory.MBR;
  406.  
  407. std::cout << "PC = " << memory.PC << " IR = " << memory.IR << std::endl;
  408.  
  409. // Decode
  410. valid = decode();
  411.  
  412. if(ERROR)
  413. {
  414. std::cout << "An error occured at PC = " << memory.PC << std::endl;
  415. break;
  416. }
  417.  
  418. memory.PC++;
  419. std::cout << "PC = " << memory.PC << " AC = " << memory.AC << " MQ = " << memory.MQ << std::endl << std::endl;
  420. }
  421. }
  422.  
  423. std::cout << std::endl;
  424.  
  425. memory.print_to_halt();
  426. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement