Skip to content

Commit

Permalink
Created the CLI for the program.
Browse files Browse the repository at this point in the history
Moved the code that calls the emulation to the IOHelper.
  • Loading branch information
azhow committed Jan 5, 2020
1 parent 91f6a1e commit 176d018
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 19 deletions.
1 change: 1 addition & 0 deletions Emuander/Emuander.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<ClCompile Include="src\main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\args.hxx" />
<ClInclude Include="include\CComputer.h" />
<ClInclude Include="include\CIOHelper.h" />
<ClInclude Include="include\SRegisters.h" />
Expand Down
6 changes: 6 additions & 0 deletions Emuander/Emuander.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<Filter Include="include">
<UniqueIdentifier>{42152646-e35e-45f4-adf8-025f07116d90}</UniqueIdentifier>
</Filter>
<Filter Include="lib">
<UniqueIdentifier>{833cd24e-5ff8-49e4-a097-94a282c6019c}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\main.cpp">
Expand All @@ -29,5 +32,8 @@
<ClInclude Include="include\CIOHelper.h">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="include\args.hxx">
<Filter>lib</Filter>
</ClInclude>
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions Emuander/include/CIOHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ namespace Neander

// Print the program registers
static void PrintRegisters(const SRegisters& registersToPrint);

// Run Neander
static EIOCode RunNeander(
const std::filesystem::path& inputFile, const std::filesystem::path& outputFile);
};
}

Expand Down
28 changes: 28 additions & 0 deletions Emuander/src/CIOHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,32 @@ namespace Neander
std::cout << "NEG: " << static_cast<int>(registersToPrint.m_negativeCondition) << std::endl;
std::cout << "ZER: " << static_cast<int>(registersToPrint.m_zeroCondition) << std::endl;
}

CIOHelper::EIOCode
CIOHelper::RunNeander(const std::filesystem::path& inputFile, const std::filesystem::path& outputFile)
{
// Return value
EIOCode retVal(EIOCode::UNKNOWN_ERROR);

// Neander computer instance
Neander::CComputer neanderComputer;

// Input memory
std::array<uint8_t, CComputer::ms_cMemorySize> inputMemory;

retVal = CIOHelper::LoadProgramFromFile(inputFile, inputMemory);

if (retVal == Neander::CIOHelper::EIOCode::SUCCESS)
{
neanderComputer.setMemory(inputMemory);

neanderComputer.runProgram();

CIOHelper::PrintRegisters(neanderComputer.getRegisters());

retVal = CIOHelper::SaveProgramToFile(neanderComputer.getMemory(), outputFile);
}

return retVal;
}
}
60 changes: 41 additions & 19 deletions Emuander/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,54 @@
#include<iostream>
#include<fstream>

#include"CComputer.h"
#include"args.hxx"
#include"CIOHelper.h"

int main()
int main(int argc, char **argv)
{
Neander::CComputer neanderComputer;
// Main return value
int retValue(0);

std::array<uint8_t, Neander::CComputer::ms_cMemorySize> exampleProgram;
// CLI arguments
args::ArgumentParser parser("Neander Emulator", "Made by: Arthur Zachow Coelho");
args::HelpFlag help(parser, "help", "Display this help menu", { 'h', "help" });
args::ValueFlag<std::string> inputPath(parser, "input_program_file", "Input program file (.mem)", { 'i' });
args::ValueFlag<std::string> outputPath(parser, "output_program_file", "Output memory file (.mem)", { 'o' });

Neander::CIOHelper::EIOCode loadStatus =
Neander::CIOHelper::LoadProgramFromFile("/home/lain/projects/Emuander/teste.mem", exampleProgram);

if (loadStatus == Neander::CIOHelper::EIOCode::SUCCESS)
try
{
neanderComputer.setMemory(exampleProgram);

neanderComputer.runProgram();

Neander::SRegisters registersRead(neanderComputer.getRegisters());

Neander::CIOHelper::PrintRegisters(registersRead);
parser.ParseCLI(argc, argv);
}
catch (args::Help& e)
{
std::cout << parser;
retValue = 0;
}
catch (args::ParseError& e)
{
std::cerr << e.what() << std::endl;
std::cerr << parser;
retValue = 1;
}
catch (args::ValidationError& e)
{
std::cerr << e.what() << std::endl;
std::cerr << parser;
retValue = 1;
}

Neander::CIOHelper::SaveProgramToFile(
neanderComputer.getMemory(),
"/home/lain/projects/Emuander/testeOut2.mem");
if (inputPath && outputPath)
{
std::cout << "Running Neander..." << std::endl;
Neander::CIOHelper::RunNeander(args::get(inputPath), args::get(outputPath));
retValue = 0;
}
else
{
std::cout << "Neander usage:" << std::endl;
std::cout << parser;
retValue = 0;
}

return 0;
return retValue;
}

0 comments on commit 176d018

Please sign in to comment.