Virtual 4001 CPU - main.cpp


IF YOU HAVE JUST ARRIVED HERE, PLEASE SEE: http://megalomaniacbore.blogspot.co.uk/2014/04/virtual-cpu-in-c-4001-cpu.html


//------------- main.cpp --------------------

#include <iostream>

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

using namespace std;
using namespace CPU_4001;

int main ()
{
cout << "Init Memory...";

Memory* theMemory = new Memory();

cout << "Ready" << endl;


// 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;


// 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);


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;
}
}


cout << "Creating the CPU instance...";
CPU* theCPU = new CPU(theMemory);
cout << "Ready" << endl;

cout << "Starting..." << endl;
theCPU->Run();
cout << "Complete" << endl;

// We must delete our CPU before the memory, because
// the CPU uses the memory, and the CPU constructor
// breaks the link to the memory when its "~CPU" function
// sets "m-TheMemory = nullptr;"
delete theCPU;

delete theMemory;
}

No comments:

Post a Comment