Main Program - Adding a Menu

So the following page contains the source code for an updated main program, we've simply added a menu with options to run the program over and over, or clear the memory etc:
#include <iostream>

#include "Memory.h"
#include "CPU.h"

using namespace std;
using namespace CPU_4001;

void DefaultProgram(Memory*);

void ReportMemory(Memory*);

void ClearMemory (Memory*);

void ShowMenu();
const bool ChooseFromMenu(const char&, Memory*);
const bool IsExitOption(const char& selection);

int main ()
{
 cout << "Init Memory...";
 Memory* theMemory = new Memory();
 cout << "Ready" << endl;
 
 // Changes here: We have the CPU ONLY active
 // for the cycle from the menu through, however
 // we don't want to loose the memory, so that is
 // created outside the loop
 bool quitAll = false;
 char selectedOption = ' ';
 do
 {  
  // Show the menu & read in a
  // character (plus enter) for
  // the next option to perform
  ShowMenu();
  cin >> selectedOption;

  // Check if we're exiting
  if ( !IsExitOption(selectedOption) )
  {
   // Check if we're running
   if ( ChooseFromMenu(selectedOption, theMemory) )
   { 
    cout << "************************************" << endl;
    // Create the CPU    
    CPU* theCPU = new CPU(theMemory);    
    // Run the program in memory    
    theCPU->Run();
    // The CPU is done
    delete theCPU;
    cout << "************************************" << endl;
   }
  }
  else
  {
   quitAll = true;
  }  
 }
 while ( !quitAll );

 delete theMemory;
}

void ShowMenu()
{
 cout << endl << "===== Menu =====" << endl;
 cout << "1. Load Default Program" << endl;
 cout << "2. Clear the Memory" << endl;
 cout << "3. Report Memory" << endl;
 cout << "R. Run the current memory state through the CPU" << endl;
 cout << endl;
 cout << "X. Exit Interpter" << endl;
 cout << endl;
 cout << "Selection: ";
}

const bool IsExitOption(const char& selection)
{
 return (selection == 'x' || selection == 'X');
}

const bool ChooseFromMenu(const char& selection, 
 Memory* theMemory)
{
 bool l_RunThroughCPU = false;

 switch (selection)
 {
  case '1': 
   DefaultProgram(theMemory);
   break;

  case '2':
   ClearMemory(theMemory);
   break;

  case '3':
   ReportMemory(theMemory);
   break;

  case 'r':
  case 'R':
   l_RunThroughCPU = true;
   break;
 }

 return l_RunThroughCPU;
}

void ClearMemory(Memory* theMemory)
{
 if ( theMemory != nullptr )
 {
  cout << "Clearing the memory...";
  theMemory->Clear();
  cout << "Complete" << endl;
 }
}

void DefaultProgram(Memory* theMemory)
{
 if ( theMemory != nullptr )
 {
  ClearMemory(theMemory);

  // Add the program
  cout << "Adding our default machine code program..." << endl;
  // Load0 value 1
  theMemory->Write(1, 1);
  theMemory->Write(2, 1);

  // Load1 Value 2
  theMemory->Write(3, 2);
  theMemory->Write(4, 2);

  // Add
  theMemory->Write(5, 3);

  // Store to 12
  theMemory->Write(6, 5);
  theMemory->Write(7, 12);

  // Print from 12
  theMemory->Write(8, 6);
  theMemory->Write(9, 12);

  // Beep
  theMemory->Write(10, 4);

  // HALT
  theMemory->Write(11, 0);
 }
}

void ReportMemory(Memory* theMemory)
{
 // Now, we only need to add "(int)" here, because the cout
 // stream does not know to use our "byte" as a number, the
 // C++ language would just assume that our memory spot is
 // an "unsigned char"... or character, so we'd output garbage.
 // (int) in front simply means "Treat this as a number"...
 cout << "Memory Size: " << (int)theMemory->c_MaxAddress << endl;

 cout << "Do you want to list the memory?";
 char yesNo;
 cin >> yesNo;
 if ( yesNo == 'Y' || yesNo == 'y' )
 {
  for (byte currentAddress = 0; currentAddress < theMemory->c_MaxAddress; ++currentAddress)
  {
   // Again, add "(int)" to force usage as a number
   cout << "Address [" << (int)currentAddress << "] = " << (int)theMemory->Read(currentAddress) << endl;
  }
 }
}

No comments:

Post a Comment