Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // SML
- //
- // Created by Howard Miller on 3/14/18.
- // Copyright � 2018 Howard Miller. All rights reserved.
- //
- #include <iostream>
- #include <iomanip>
- #include <array>
- #include "SML Test Cases.h"
- using namespace std;
- #define MaxMemory 100
- typedef array<int, MaxMemory> myMemory;
- void mySML(myMemory &mainMemory); // This is your Function to write!
- void disAssemble(myMemory &mainMemory, int from = 0, int to = MaxMemory, bool printAddress = true);
- int main(int argc, const char * argv[]) {
- cout << "Darrick Oliver \t\t SML Program \t\tCIST 004A\n\n";
- myMemory TC01 SML_TEST_CASE_1;
- myMemory TC02 SML_TEST_CASE_2;
- myMemory TC03 SML_TEST_CASE_3;
- myMemory TC04 SML_TEST_CASE_4;
- myMemory TC05 SML_TEST_CASE_5;
- myMemory TC06 SML_TEST_CASE_6;
- myMemory TC07 SML_TEST_CASE_7;
- myMemory TC10 SML_TEST_CASE_10;
- myMemory TC11 SML_TEST_CASE_11;
- myMemory TC12 SML_TEST_CASE_12;
- array < myMemory *, 20> master{ nullptr, &TC01, &TC02, &TC03, &TC04,
- &TC05, &TC06, &TC07, nullptr, nullptr,
- &TC10, &TC11, &TC12, nullptr, nullptr,
- nullptr, nullptr, nullptr, nullptr, nullptr };
- for (int i{ 1 }; i<20; i++) {
- if (master[i]) {
- cout << endl;
- for (int j{ 0 }; j<65; j++)
- cout << '*';
- cout << "\nNow Disassmebling Test Case: " << i << "\n" << endl;
- // disAssemble (*master[i]);
- mySML(*master[i]); // A call to your code with a test case pre-loaded into memory
- }
- }
- cin.clear();
- cin.ignore();
- cin.get();
- return 0;
- }
- void mySML(myMemory &mMem) {
- int acc{}; // Accumulator used in Math operations
- int *pc{ mMem.begin() }; // Program Counter
- int ir{*pc}; // Instruction Register
- // Hold current Instruction
- int opcode; // The instruction to execute
- int operand; // Data from the instruction
- for (bool run = true; run == true;) {
- //--Decoding
- ir = *pc;
- operand = ir % 1000;
- opcode = ir / 1000;
- cout << setfill('0');
- //--Executing
- switch (opcode) { // Get the Opcode using integert math which truncates! Then decode it!
- case 00: // The Opcode is 00, therefore this is Either a NOP instruction or data sitting in memory.
- pc++;
- break;
- case 10: // READ console into memory
- cout << "input: ";
- cin >> mMem[operand];
- pc++;
- break;
- case 11: // WRITE contents of memory to console
- cout << "output: " << setfill('0') << setw(5) << mMem[operand] << endl;
- pc++;
- break;
- case 15: // WRITE ASCII char from the accumulator to cout
- cout << (static_cast <char>(acc));
- pc++;
- break;
- case 17: // WRITE one char from the operand to cout
- cout << (static_cast <char>(operand));
- pc++;
- break;
- case 20: // LOAD memory into the accumulator
- acc = mMem[operand];
- pc++;
- break;
- case 21: // STORE contents of the accumulator into memory
- mMem[operand] = acc;
- pc++;
- break;
- case 25: // LOAD specified value into the accumulator
- acc = operand;
- pc++;
- break;
- case 27: // LOAD memory pointed to by the accumulator into the accumulator
- acc = mMem[acc];
- pc++;
- break;
- case 30: // ADD to the accumulator the contents of memory
- acc += mMem[operand];
- pc++;
- break;
- case 31: // SUB from the accumulator the contents of memory
- acc -= mMem[operand];
- pc++;
- break;
- case 32: // MUL the accumulator by the contents of memory
- acc *= mMem[operand];
- pc++;
- break;
- case 33: // DIV the accumulator by the contents of memory
- acc /= mMem[operand];
- pc++;
- break;
- case 34: // MOD the accumulator by the contents of memory
- acc %= mMem[operand];
- pc++;
- break;
- case 35: // ADD to the accumulator the contents of the operand
- acc += operand;
- pc++;
- break;
- case 40: // BRNCH to the specified location
- pc = &mMem[operand];
- break;
- case 41: // BRNCH if acc is negative, otherwise move to next address
- if (acc < 0) {
- pc = &mMem[operand];
- }
- else {
- pc++;
- }
- break;
- case 42: // BRNCH if the acc is zero, otherwise move to next address
- if (acc == 0) {
- pc = &mMem[operand];
- }
- else {
- pc++;
- }
- break;
- case 43: // BRNCH if the acc is not zero, otheriwse move to next address
- if (acc != 0) {
- pc = &mMem[operand];
- }
- else{
- pc++;
- }
- break;
- case 45: // HALT
- run = false;
- break;
- default:
- run = false;
- break;
- } // End of Switch
- if (pc > mMem.end()) {
- run = false;
- cout << endl;
- }
- } // End of for loop
- //--Dump everything
- cout << "Program Halted at instruction address: " << setw(3) << setfill('0') << pc - mMem.begin() << endl;
- cout << endl << "REGISTERS:" << endl;
- //--Registers
- cout << "Accumulator: " << setw(5) << setfill('0') << acc << endl;
- cout << "Program Counter: " << setw(3) << setfill('0') << pc - mMem.begin() << endl;
- cout << "Instruction Register: " << setw(5) << setfill('0') << ir << endl;
- cout << "Opcode: " << setw(2) << setfill('0') << opcode << endl;
- cout << "Operand: " << setw(3) << setfill('0') << operand << endl << endl;
- //--Memory
- cout << "MEMORY:" << endl;
- cout << setfill(' ') << "\t";
- for (int i = 0; i<10; ++i)
- cout << setw(6) << i;
- cout << endl << " ";
- for (int i = 0; i<100; i += 10) {
- cout << " " << i << " ";
- for (int j = (0 + i); j<(10 + i); j++)
- cout << setw(5) << setfill('0') << mMem[j] << " ";
- cout << endl;
- }
- cout << "Simulation Completed!" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement