Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.21 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  SML
  4. //
  5. //  Created by Howard Miller on 3/14/18.
  6. //  Copyright � 2018 Howard Miller. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <array>
  12. #include "SML Test Cases.h"
  13.  
  14. using namespace std;
  15.  
  16. #define MaxMemory 100
  17. typedef array<int, MaxMemory> myMemory;
  18.  
  19. void mySML(myMemory &mainMemory);  // This is your Function to write!
  20. void disAssemble(myMemory &mainMemory, int from = 0, int to = MaxMemory, bool printAddress = true);
  21.  
  22. int main(int argc, const char * argv[]) {
  23.     cout << "Darrick Oliver \t\t SML Program \t\tCIST 004A\n\n";
  24.    
  25.     myMemory TC01 SML_TEST_CASE_1;
  26.     myMemory TC02 SML_TEST_CASE_2;
  27.     myMemory TC03 SML_TEST_CASE_3;
  28.     myMemory TC04 SML_TEST_CASE_4;
  29.     myMemory TC05 SML_TEST_CASE_5;
  30.     myMemory TC06 SML_TEST_CASE_6;
  31.     myMemory TC07 SML_TEST_CASE_7;
  32.     myMemory TC10 SML_TEST_CASE_10;
  33.     myMemory TC11 SML_TEST_CASE_11;
  34.     myMemory TC12 SML_TEST_CASE_12;
  35.    
  36.     array < myMemory *, 20> master{ nullptr,   &TC01,   &TC02,   &TC03,   &TC04,
  37.         &TC05,   &TC06,   &TC07, nullptr, nullptr,
  38.         &TC10,   &TC11,   &TC12, nullptr, nullptr,
  39.         nullptr, nullptr, nullptr, nullptr, nullptr };
  40.    
  41.     for (int i{ 1 }; i<20; i++) {
  42.         if (master[i]) {
  43.             cout << endl;
  44.             for (int j{ 0 }; j<65; j++)
  45.                 cout << '*';
  46.             cout << "\nNow Disassmebling Test Case: " << i << "\n" << endl;
  47.             // disAssemble (*master[i]);
  48.             mySML(*master[i]);  // A call to your code with a test case pre-loaded into memory
  49.         }
  50.     }
  51.     cin.clear();
  52.     cin.ignore();
  53.     cin.get();
  54.     return 0;
  55. }
  56.  
  57. void mySML(myMemory &mMem) {
  58.     int acc{};                  // Accumulator used in Math operations
  59.     int *pc{ mMem.begin() };        // Program Counter
  60.     int ir{*pc};                        // Instruction Register
  61.     // Hold current Instruction
  62.     int opcode;                 // The instruction to execute
  63.     int operand;                // Data from the instruction
  64.    
  65.     for (bool run = true; run == true;) {
  66.        
  67.         //--Decoding
  68.         ir = *pc;
  69.        
  70.         operand = ir % 1000;
  71.         opcode = ir / 1000;
  72.  
  73.         cout << setfill('0');
  74.        
  75.         //--Executing
  76.         switch (opcode) {       // Get the Opcode using integert math which truncates! Then decode it!
  77.             case 00:                // The Opcode is 00, therefore this is Either a NOP instruction or data sitting in memory.
  78.                 pc++;
  79.                 break;
  80.             case 10:                // READ console into memory
  81.                 cout << "input: ";
  82.                 cin >> mMem[operand];
  83.                 pc++;
  84.                 break;
  85.             case 11:                // WRITE contents of memory to console
  86.                 cout << "output: " << setfill('0') << setw(5) << mMem[operand] << endl;
  87.                 pc++;
  88.                 break;
  89.             case 15:                // WRITE ASCII char from the accumulator to cout
  90.                 cout << (static_cast <char>(acc));
  91.                 pc++;
  92.                 break;
  93.             case 17:                // WRITE one char from the operand to cout
  94.                 cout << (static_cast <char>(operand));
  95.                 pc++;
  96.                 break;
  97.             case 20:                // LOAD memory into the accumulator
  98.                 acc = mMem[operand];
  99.                 pc++;
  100.                 break;
  101.             case 21:                // STORE contents of the accumulator into memory
  102.                 mMem[operand] = acc;
  103.                 pc++;
  104.                 break;
  105.             case 25:                // LOAD specified value into the accumulator
  106.                 acc = operand;
  107.                 pc++;
  108.                 break;
  109.             case 27:                // LOAD memory pointed to by the accumulator into the accumulator
  110.                 acc = mMem[acc];
  111.                 pc++;
  112.                 break;
  113.             case 30:                // ADD to the accumulator the contents of memory
  114.                 acc += mMem[operand];
  115.                 pc++;
  116.                 break;
  117.             case 31:                // SUB from the accumulator the contents of memory
  118.                 acc -= mMem[operand];
  119.                 pc++;
  120.                 break;
  121.             case 32:                // MUL the accumulator by the contents of memory
  122.                 acc *= mMem[operand];
  123.                 pc++;
  124.                 break;
  125.             case 33:                // DIV the accumulator by the contents of memory
  126.                 acc /= mMem[operand];
  127.                 pc++;
  128.                 break;
  129.             case 34:                // MOD the accumulator by the contents of memory
  130.                 acc %= mMem[operand];
  131.                 pc++;
  132.                 break;
  133.             case 35:                // ADD to the accumulator the contents of the operand
  134.                 acc += operand;
  135.                 pc++;
  136.                 break;
  137.             case 40:                // BRNCH to the specified location
  138.                 pc = &mMem[operand];
  139.                 break;
  140.             case 41:                // BRNCH if acc is negative, otherwise move to next address
  141.                 if (acc < 0) {
  142.                     pc = &mMem[operand];
  143.                 }
  144.                 else {
  145.                     pc++;
  146.                 }
  147.                 break;
  148.             case 42:                // BRNCH if the acc is zero, otherwise move to next address
  149.                 if (acc == 0) {
  150.                     pc = &mMem[operand];
  151.                 }
  152.                 else {
  153.                     pc++;
  154.                 }
  155.                 break;
  156.             case 43:                // BRNCH if the acc is not zero, otheriwse move to next address
  157.                 if (acc != 0) {
  158.                     pc = &mMem[operand];
  159.                 }
  160.                 else{
  161.                     pc++;
  162.                 }
  163.                 break;
  164.             case 45:                // HALT
  165.                 run = false;
  166.                 break;
  167.             default:
  168.                 run = false;
  169.                 break;
  170.         }  // End of Switch
  171.         if (pc > mMem.end()) {
  172.             run = false;
  173.             cout << endl;
  174.         }
  175.  
  176.     }  // End of for loop
  177.    
  178.     //--Dump everything
  179.     cout << "Program Halted at instruction address: " << setw(3) << setfill('0') << pc - mMem.begin() << endl;
  180.     cout << endl << "REGISTERS:" << endl;
  181.    
  182.     //--Registers
  183.     cout << "Accumulator: " << setw(5) << setfill('0') << acc << endl;
  184.     cout << "Program Counter: " << setw(3) << setfill('0') << pc - mMem.begin() << endl;
  185.     cout << "Instruction Register: " << setw(5) << setfill('0') << ir << endl;
  186.     cout << "Opcode: " << setw(2) << setfill('0') << opcode << endl;
  187.     cout << "Operand: " << setw(3) << setfill('0') << operand << endl << endl;
  188.    
  189.     //--Memory
  190.     cout << "MEMORY:" << endl;
  191.     cout << setfill(' ') << "\t";
  192.     for (int i = 0; i<10; ++i)
  193.         cout << setw(6) << i;
  194.     cout << endl << " ";
  195.     for (int i = 0; i<100; i += 10) {
  196.         cout << "  " << i << " ";
  197.         for (int j = (0 + i); j<(10 + i); j++)
  198.             cout << setw(5) << setfill('0') << mMem[j] << " ";
  199.         cout << endl;
  200.     }
  201.     cout << "Simulation Completed!" << endl;
  202.    
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement