Advertisement
Nofxthepirate

Simpletron Program

Jun 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.06 KB | None | 0 0
  1. //Simpletron Program
  2.  
  3. //Simpletron.h code
  4.  
  5. #include <iomanip>
  6.  
  7.  
  8. class Simpletron{
  9. public:
  10.  
  11.  
  12.     Simpletron()
  13.     : memory{}, accumulator{0}, instructionCounter{0},
  14.      operationCode{0}, operand{0}, instructionRegister{0}{
  15.     }
  16. void printRegisters()const{
  17.     std::cout << "\nREGISTERS:\n";
  18.  
  19.     std::cout << "accumulator\t\t" << " " << std::showpos <<
  20.     std::internal << std::setfill('0') << std::setw(5)<< accumulator << '\n';
  21.  
  22.     std::cout << "instructionCounter\t    " << std::noshowpos << std::setfill('0') <<
  23.     std::setw(2) << instructionCounter << '\n';
  24.  
  25.     std::cout << "instructionRegister\t " << std::showpos << std::setfill('0') << std::internal
  26.      << std::setfill('0') << std::setw(5)<< instructionRegister << '\n';
  27.  
  28.     std::cout << "operationCode\t\t    " << std::noshowpos << std::setfill('0') <<
  29.     std::setw(2) << operationCode << '\n';
  30.  
  31.     std::cout << "operand\t\t\t    " << std::noshowpos << std::setfill('0') <<
  32.     std::setw(2) << operand << '\n' << std::endl;
  33.  
  34.     std::cout << std::setfill(' ');
  35.  
  36. }
  37. void printMemDump() const{
  38.  
  39.     std::cout << "MEMORY:\n";
  40.     for (int i{0}; i < 100; i += 10){
  41.         if(i == 0){std::cout << "  ";}
  42.         else std::cout <<std::noshowpos << std::setw(2) << i;
  43.  
  44.         for(int j{0}; j < 10; j++){
  45.                 if (i == 0) {
  46.                     std::cout << std::setw(6)<< j;
  47.                 }
  48.         else std::cout << " " << std::showpos <<
  49.              std::internal << std::setfill('0') << std::setw(5)<< memory[i - 10 + j];
  50.         }
  51.     std::cout << std::endl;
  52.     }
  53.     std::cout << std::noshowpos << std::setfill(' ');
  54. }
  55. //Below is the function that works because it is in the class.
  56. void enterInstructions(){
  57.     int instruction{0};
  58.     int i{0};
  59.  
  60.     do {
  61.         std::cout << std::setfill('0') << std::setw(2) << i << " ? ";
  62.         std::cin >> instruction;
  63.         storeInstruction(instruction, i) ;
  64.         i += 1;
  65.  
  66.     } while (instruction != -99999);
  67. }
  68.  
  69. void storeInstruction(int instruction, int i) {
  70.     if (instruction >= -9999 && instruction <= 9999) memory[i] = instruction;
  71.  
  72. }
  73.  
  74. void performInstruction(){
  75.  
  76.     while (operationCode != opCode::HALT){
  77.         instructionRegister = memory[instructionCounter];
  78.         operationCode = memory[instructionCounter] / 100;
  79.         operand = memory[instructionCounter] % 100;
  80.  
  81.         switch (operationCode) {
  82.         case opCode::READ: {
  83.             std::cout << "? ";
  84.             std::cin >> memory[operand];
  85.             ++instructionCounter;
  86.             break;
  87.         }
  88.         case opCode::WRITE: {
  89.             std::cout << memory[operand];
  90.             ++instructionCounter;
  91.             break;
  92.         }
  93.         case opCode::LOAD: {
  94.             accumulator = memory[operand];
  95.             ++instructionCounter;
  96.             break;
  97.         }
  98.         case opCode::STORE: {
  99.             memory[operand] = accumulator;
  100.             ++instructionCounter;
  101.             break;
  102.         }
  103.         case opCode::ADD: {
  104.             accumulator += memory[operand];
  105.             ++instructionCounter;
  106.             break;
  107.         }
  108.         case opCode::SUBTRACT: {
  109.             accumulator -= memory[operand];
  110.             ++instructionCounter;
  111.             break;
  112.         }
  113.         case opCode::DIVIDE: {
  114.             accumulator /= memory[operand];
  115.             ++instructionCounter;
  116.             break;
  117.         }
  118.         case opCode::MULTIPLY: {
  119.             accumulator *= memory[operand];
  120.             ++instructionCounter;
  121.             break;
  122.         }
  123.         case opCode::BRANCH: {
  124.             instructionCounter = operand;
  125.             break;
  126.         }
  127.         case opCode::BRANCHNEG:{
  128.             instructionCounter = operand;
  129.             break;
  130.         }
  131.         case opCode::BRANCHZERO: {
  132.             if (accumulator == 0) instructionCounter = operand;
  133.             break;
  134.         }
  135.         case opCode::HALT: {
  136.             ++instructionCounter;
  137.             char anykey;
  138.             std::cout << "\n*** Simpletron execution terminated, press any key to show registers and memory***" << std::endl;
  139.             std::cin >> anykey;
  140.             printRegisters();
  141.             printMemDump();
  142.  
  143.         }
  144.         }
  145.     }
  146. }
  147. private:
  148.     int memory[100];
  149.     int accumulator;
  150.     int instructionCounter;
  151.     int operationCode;
  152.     int operand;
  153.     int instructionRegister;
  154.      enum opCode{ //doesn't compile if this is static or const...
  155.      READ = 10,
  156.      WRITE = 11,
  157.      LOAD = 20,
  158.      STORE = 21,
  159.      ADD = 30,
  160.      SUBTRACT = 31,
  161.      DIVIDE = 32,
  162.      MULTIPLY = 33,
  163.      BRANCH = 40,
  164.      BRANCHNEG = 41,
  165.      BRANCHZERO = 42,
  166.      HALT = 43
  167.     };
  168. };
  169.  
  170. //main.cpp code
  171.  
  172. #include <iostream>
  173. #include "Simpletron.h"
  174.  
  175. using namespace std;
  176.  
  177.  
  178.  
  179. void enterInstructions(Simpletron);
  180. void welcome();
  181.  
  182. int main()
  183. {
  184.  
  185. Simpletron simpletron{};
  186.  
  187. welcome();
  188.  
  189. enterInstructions(simpletron);
  190. simpletron.printRegisters();
  191. simpletron.printMemDump();
  192.  
  193. simpletron.enterInstructions();
  194. simpletron.printRegisters();
  195. simpletron.printMemDump();
  196. simpletron.performInstruction();
  197.  
  198. }
  199. //the function below is identical to the one in Simpletron.h
  200. //except for how it calls storeInstruction and whether the Simpletron object
  201. // is called on the method or passed to it by argument.
  202. // It still compiles and runs but this version doesn't actually store the instructions
  203.  
  204. void enterInstructions(Simpletron myPC){
  205.     int instruction{0};
  206.     int i{0};
  207.     do {
  208.         cout << setfill('0') << setw(2) << i << " ? ";
  209.         cin >> instruction;
  210.         myPC.storeInstruction(instruction, i);
  211.         i += 1;
  212.  
  213.     } while (instruction != -99999);
  214. }
  215. void welcome() {
  216.     cout << "*** Welcome to Simpletron! ***\n\n";
  217.     cout << "*** Please enter your program one instruction ***\n";
  218.     cout << "*** (or data word) at a time. I will type the ***\n";
  219.     cout << "*** location number and a question mark (?).  ***\n";
  220.     cout << "*** You then type the word for that location. ***\n";
  221.     cout << "*** Type the sentinel -99999 to stop entering ***\n";
  222.     cout << "*** your program. ***\n\n";
  223.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement