Advertisement
maddada

asdasd

May 23rd, 2016
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.45 KB | None | 0 0
  1. //Simulator Phase 1
  2.  
  3. #ifndef SIMULATORLIB_H_INCLUDED
  4. #define SIMULATORLIB_H_INCLUDED
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <iomanip>
  10.  
  11. using namespace std;
  12.  
  13. ///DEFINITIONS:
  14. // Part 1: Define the CPU
  15. // OS Specialized Registers.
  16. int PC, AC, SP, IR, TIR, //IR -> stores the instruction given by PC.
  17. // Constant Value Registers.
  18.     ZERO = 0, PLUSone = 1, MINUSone = -1,
  19. // For masking operations by the OS.
  20.     AMASK, BMASK,
  21. // General Purpose Registers.
  22.     A, B, C, D, E, F;
  23. // CPU Buses
  24. int Abus, Bbus, Cbus, addressOut, data_in, data_out;
  25. int Alatch, Blatch;
  26. int AMUX, ALU, shifter;
  27. // Gateways to/from Memory.
  28. int MAR, MBR;
  29. // Memory of 4K represented as a one-d array.
  30. int RAM[4095];
  31. // Instructions are 4 characters long.
  32. char inst[4];
  33. int NumberOfInstructions = 0; //
  34. int opcode, address, constant, stackop;
  35. int const CODE_START_ADDRESS = 1000;
  36. int const DATA_START_ADDRESS = 2000;
  37. string currentOperation;
  38. int stop = 0;
  39.  
  40. /*
  41. Part 2: Define the Operations of the Assembly Intructions; each instruction
  42. is represented by a function That represents its internal work.
  43. (done in the lib file)
  44. */
  45.  
  46. ///##############################################################################
  47. /// Assembly Functions:
  48. void LODD(int address) //Load Direct
  49. {
  50.     AC = RAM[address];
  51. }
  52. void STOD(int address) //Store Direct
  53. {
  54.     RAM[address] = AC;
  55. }
  56. void ADDD(int address) //Add Direct
  57. {
  58.     AC = AC + RAM[address];
  59. }
  60. void SUBD(int address) //Subtract Direct
  61. {
  62.     AC = AC - RAM[address];
  63. }
  64. void JPOS(int address) //Jump Positive
  65. {
  66.     if(AC >= 0) PC =address;
  67. }
  68.  
  69. void JZER(int address) //Jump Zero
  70. {
  71.     if(AC == 0) PC =address;
  72. }
  73. void JUMP(int address) //Jump to Adress
  74. {
  75.     PC = address;
  76. }
  77. void LOCO(int address) //Load Constant
  78. {
  79.     AC = address;
  80. }
  81. void LODL(int address) //Load Local
  82. {
  83.     AC = RAM[address+SP];
  84. }
  85. void STOL(int address) //Store Local
  86. {
  87.     RAM[address+SP] = AC;
  88. }
  89. void ADDL(int address) //Add Local
  90. {
  91.     AC += RAM[address+SP];
  92. }
  93. void SUBL(int address) //Subtract Local
  94. {
  95.     AC -= RAM[address+SP];
  96. }
  97. void JNEG(int address) //Jump Negative
  98. {
  99.     if(AC < 0) PC=address;
  100. }
  101. void JNZE(int address) //Jump Nonzero
  102. {
  103.     if(AC !=0 ) PC=address;
  104. }
  105. void CALL(int address) //Call Procedure
  106. {
  107.     SP--;
  108.     RAM[SP] = PC;
  109.     PC = address;
  110. }
  111. void PSHI(int address) //Push Indirect
  112. {
  113.     SP--;
  114.     RAM[SP] = RAM[AC];
  115. }
  116. void POPI(int address) //Push Onto Stack
  117. {
  118.     RAM[AC] = RAM[PC];
  119.     SP++;
  120. }
  121. void PUSH(int address) //Push From Stack
  122. {
  123.     SP--;
  124.     RAM[SP] = AC;
  125. }
  126. void POP(int address) //Pop From Stack
  127. {
  128.     AC = RAM[SP];
  129.     SP++;
  130. }
  131. void RETN(int address) //Return
  132. {
  133.     PC = RAM[SP];
  134.     SP++;
  135. }
  136. void SWAP(int address) //Swap Accumulator and Stack Pointer
  137. {
  138.     int tmp = AC;
  139.     AC  = SP;
  140.     SP  = tmp;
  141. }
  142. void INSP(int address) //Increment Stack Pointer
  143. {
  144.     SP += address;
  145. }
  146. void DESP(int address) //Decrement Stack Pointer
  147. {
  148.     SP -= address;
  149. }
  150.  
  151. ///##############################################################################
  152. /// CPU Cycle functions: fetch -> decode -> execute
  153.  
  154. void LoadText() // This simulates compiling and loading a program into RAM.
  155. {
  156.     string currentLine;
  157.     ifstream instructionstxt ("Instructions.txt");
  158.     int i = CODE_START_ADDRESS;
  159.  
  160.     if(instructionstxt.is_open())
  161.     {
  162.         while (getline(instructionstxt,currentLine))
  163.         {
  164.             instructionstxt >> hex >> RAM[i];
  165.             i++;
  166.             NumberOfInstructions++; //counts number of instructions in the program
  167.         } //end while
  168.         instructionstxt.close();
  169.     } //end if
  170.  
  171.     else
  172.         cout << "Error: Instructions.txt not found";
  173. }
  174.  
  175. void fetch()
  176. {
  177.     MAR = PC;       //Gets next instruction
  178.     MBR = RAM[MAR];
  179.     IR  = MBR;
  180.     PC  = PC + 1;
  181. }
  182.  
  183. void decode()
  184. {
  185.     // [var] >> [number of times to bit shift right]
  186.     opcode   = (IR >> 12); //if 0001 xxxxxxxxxxxx then becomes 000000000000 0001
  187.     stackop  = (IR >> 0x8) & 0x0007; //shifting
  188. }
  189.  
  190. // Call the assembly instructions in hex here.
  191. void calculateAddress()
  192. {
  193.     address  = (IR & 0x0FFF); // Mask with 0000111111111111
  194.     constant = (IR & 0X00FF); // Mask with 0000000011111111
  195. }
  196.  
  197. void execute()
  198. {
  199.     switch(opcode)
  200.     {
  201.         case 0: LODD (address); currentOperation = "LODD" ; break;
  202.         case 1: STOD (address); currentOperation = "STOD" ; break;
  203.         case 2: ADDD (address); currentOperation = "ADDD" ; break;
  204.         case 3: SUBD (address); currentOperation = "SUBD" ; break;
  205.         case 4: JPOS (address); currentOperation = "JPOS" ; break;
  206.         case 5: JZER (address); currentOperation = "JZER" ; break;
  207.         case 6: JUMP (address); currentOperation = "JUMP" ; break;
  208.         case 7: LOCO (address); currentOperation = "LOCO" ; break;
  209.         case 8: LODL (address); currentOperation = "LODL" ; break;
  210.         case 9: STOL (address); currentOperation = "STOL" ; break;
  211.         case 10: ADDL (address); currentOperation = "ADDL" ; break;
  212.         case 11: SUBL (address); currentOperation = "SUBL" ; break;
  213.         case 12: JNEG (address); currentOperation = "JNEG" ; break;
  214.         case 13: JNZE (address); currentOperation = "JNZE" ; break;
  215.         case 14: CALL (address); currentOperation = "CALL" ; break;
  216.         case 15:        switch(stackop)
  217.                         {
  218.                       case 0: PSHI (address); currentOperation = "PSHI" ; break;
  219.                       case 1: POPI (address); currentOperation = "POPI" ; break;
  220.                       case 2: PUSH (address); currentOperation = "PUSH" ; break;
  221.                       case 3: POP  (address); currentOperation = "POP"  ; break;
  222.                       case 4: RETN (address); currentOperation = "RETN" ; break;
  223.                       case 5: SWAP (address); currentOperation = "SWAP" ; break;
  224.                       case 6: INSP (address); currentOperation = "INSP" ; break;
  225.                       case 7: DESP (address); currentOperation = "DESP" ; break;
  226.                         } //end inner switch
  227.  
  228.     } //end outer switch
  229.  } //end execute function
  230.  
  231. //void print(int InstructionNumber)
  232. //{
  233. //        cout << endl
  234. //             <<"Instuction Number:"    << InstructionNumber
  235. //             <<" | Current Operation:" << currentOperation
  236. //             <<" | Address:"           << address
  237. //                       << endl    << endl
  238. //             <<"MAR="  << setw(5) << MAR  <<" | "
  239. //             <<"MBR="  << setw(5) << MBR  <<" | "
  240. //             <<"PC="   << setw(5) << PC   <<" | "
  241. //             <<"AC="   << setw(3) << AC   <<" | "
  242. //             <<"SP="   << setw(3) << SP   <<" | "
  243. //             <<"IR="   << setw(5) << IR   <<"\n";
  244. //        cout<<"\n=========================================================\n";
  245. //}
  246.  
  247. void print(int InstructionNumber)
  248. {
  249.     if (InstructionNumber == 1) //print the header if first instruction.
  250.     {
  251.         cout << "  MAR   MBR     PC     AC   SP    IR      \n";
  252.         cout<<"=========================================================\n";
  253.     }
  254.  
  255.     cout <<"\nInstruction number " << InstructionNumber  << "  Current Operation: " << currentOperation << endl << endl;
  256.     cout << " "<< setw(4) << MAR << setw(7) << MBR << setw(7) << PC << setw(5) <<AC<<setw(5)<<SP<<setw(9)<<IR<<setw(5)<<"\n\n";
  257.     cout<<"------------------------------------------------------------------------------\n";
  258. }
  259.  
  260. void status(int i)
  261. {
  262.         if(i == NumberOfInstructions)
  263.         stop = 1;
  264. }
  265.  
  266. #endif // SIMULATORLIB_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement