Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Simpletron Program
- //Simpletron.h code
- #include <iomanip>
- class Simpletron{
- public:
- Simpletron()
- : memory{}, accumulator{0}, instructionCounter{0},
- operationCode{0}, operand{0}, instructionRegister{0}{
- }
- void printRegisters()const{
- std::cout << "\nREGISTERS:\n";
- std::cout << "accumulator\t\t" << " " << std::showpos <<
- std::internal << std::setfill('0') << std::setw(5)<< accumulator << '\n';
- std::cout << "instructionCounter\t " << std::noshowpos << std::setfill('0') <<
- std::setw(2) << instructionCounter << '\n';
- std::cout << "instructionRegister\t " << std::showpos << std::setfill('0') << std::internal
- << std::setfill('0') << std::setw(5)<< instructionRegister << '\n';
- std::cout << "operationCode\t\t " << std::noshowpos << std::setfill('0') <<
- std::setw(2) << operationCode << '\n';
- std::cout << "operand\t\t\t " << std::noshowpos << std::setfill('0') <<
- std::setw(2) << operand << '\n' << std::endl;
- std::cout << std::setfill(' ');
- }
- void printMemDump() const{
- std::cout << "MEMORY:\n";
- for (int i{0}; i < 100; i += 10){
- if(i == 0){std::cout << " ";}
- else std::cout <<std::noshowpos << std::setw(2) << i;
- for(int j{0}; j < 10; j++){
- if (i == 0) {
- std::cout << std::setw(6)<< j;
- }
- else std::cout << " " << std::showpos <<
- std::internal << std::setfill('0') << std::setw(5)<< memory[i - 10 + j];
- }
- std::cout << std::endl;
- }
- std::cout << std::noshowpos << std::setfill(' ');
- }
- //Below is the function that works because it is in the class.
- void enterInstructions(){
- int instruction{0};
- int i{0};
- do {
- std::cout << std::setfill('0') << std::setw(2) << i << " ? ";
- std::cin >> instruction;
- storeInstruction(instruction, i) ;
- i += 1;
- } while (instruction != -99999);
- }
- void storeInstruction(int instruction, int i) {
- if (instruction >= -9999 && instruction <= 9999) memory[i] = instruction;
- }
- void performInstruction(){
- while (operationCode != opCode::HALT){
- instructionRegister = memory[instructionCounter];
- operationCode = memory[instructionCounter] / 100;
- operand = memory[instructionCounter] % 100;
- switch (operationCode) {
- case opCode::READ: {
- std::cout << "? ";
- std::cin >> memory[operand];
- ++instructionCounter;
- break;
- }
- case opCode::WRITE: {
- std::cout << memory[operand];
- ++instructionCounter;
- break;
- }
- case opCode::LOAD: {
- accumulator = memory[operand];
- ++instructionCounter;
- break;
- }
- case opCode::STORE: {
- memory[operand] = accumulator;
- ++instructionCounter;
- break;
- }
- case opCode::ADD: {
- accumulator += memory[operand];
- ++instructionCounter;
- break;
- }
- case opCode::SUBTRACT: {
- accumulator -= memory[operand];
- ++instructionCounter;
- break;
- }
- case opCode::DIVIDE: {
- accumulator /= memory[operand];
- ++instructionCounter;
- break;
- }
- case opCode::MULTIPLY: {
- accumulator *= memory[operand];
- ++instructionCounter;
- break;
- }
- case opCode::BRANCH: {
- instructionCounter = operand;
- break;
- }
- case opCode::BRANCHNEG:{
- instructionCounter = operand;
- break;
- }
- case opCode::BRANCHZERO: {
- if (accumulator == 0) instructionCounter = operand;
- break;
- }
- case opCode::HALT: {
- ++instructionCounter;
- char anykey;
- std::cout << "\n*** Simpletron execution terminated, press any key to show registers and memory***" << std::endl;
- std::cin >> anykey;
- printRegisters();
- printMemDump();
- }
- }
- }
- }
- private:
- int memory[100];
- int accumulator;
- int instructionCounter;
- int operationCode;
- int operand;
- int instructionRegister;
- enum opCode{ //doesn't compile if this is static or const...
- READ = 10,
- WRITE = 11,
- LOAD = 20,
- STORE = 21,
- ADD = 30,
- SUBTRACT = 31,
- DIVIDE = 32,
- MULTIPLY = 33,
- BRANCH = 40,
- BRANCHNEG = 41,
- BRANCHZERO = 42,
- HALT = 43
- };
- };
- //main.cpp code
- #include <iostream>
- #include "Simpletron.h"
- using namespace std;
- void enterInstructions(Simpletron);
- void welcome();
- int main()
- {
- Simpletron simpletron{};
- welcome();
- enterInstructions(simpletron);
- simpletron.printRegisters();
- simpletron.printMemDump();
- simpletron.enterInstructions();
- simpletron.printRegisters();
- simpletron.printMemDump();
- simpletron.performInstruction();
- }
- //the function below is identical to the one in Simpletron.h
- //except for how it calls storeInstruction and whether the Simpletron object
- // is called on the method or passed to it by argument.
- // It still compiles and runs but this version doesn't actually store the instructions
- void enterInstructions(Simpletron myPC){
- int instruction{0};
- int i{0};
- do {
- cout << setfill('0') << setw(2) << i << " ? ";
- cin >> instruction;
- myPC.storeInstruction(instruction, i);
- i += 1;
- } while (instruction != -99999);
- }
- void welcome() {
- cout << "*** Welcome to Simpletron! ***\n\n";
- cout << "*** Please enter your program one instruction ***\n";
- cout << "*** (or data word) at a time. I will type the ***\n";
- cout << "*** location number and a question mark (?). ***\n";
- cout << "*** You then type the word for that location. ***\n";
- cout << "*** Type the sentinel -99999 to stop entering ***\n";
- cout << "*** your program. ***\n\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement