From 176d018da1147c207d140604ebe00c9c9d6af350 Mon Sep 17 00:00:00 2001 From: Arthur Zachow Date: Sun, 5 Jan 2020 20:41:42 -0300 Subject: [PATCH] Created the CLI for the program. Moved the code that calls the emulation to the IOHelper. --- Emuander/Emuander.vcxproj | 1 + Emuander/Emuander.vcxproj.filters | 6 ++++ Emuander/include/CIOHelper.h | 4 +++ Emuander/src/CIOHelper.cpp | 28 +++++++++++++++ Emuander/src/main.cpp | 60 +++++++++++++++++++++---------- 5 files changed, 80 insertions(+), 19 deletions(-) diff --git a/Emuander/Emuander.vcxproj b/Emuander/Emuander.vcxproj index f40a7a2..83b007a 100644 --- a/Emuander/Emuander.vcxproj +++ b/Emuander/Emuander.vcxproj @@ -144,6 +144,7 @@ + diff --git a/Emuander/Emuander.vcxproj.filters b/Emuander/Emuander.vcxproj.filters index 8ca4634..a04fe95 100644 --- a/Emuander/Emuander.vcxproj.filters +++ b/Emuander/Emuander.vcxproj.filters @@ -7,6 +7,9 @@ {42152646-e35e-45f4-adf8-025f07116d90} + + {833cd24e-5ff8-49e4-a097-94a282c6019c} + @@ -29,5 +32,8 @@ include + + lib + \ No newline at end of file diff --git a/Emuander/include/CIOHelper.h b/Emuander/include/CIOHelper.h index 2100242..e3af361 100644 --- a/Emuander/include/CIOHelper.h +++ b/Emuander/include/CIOHelper.h @@ -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); }; } diff --git a/Emuander/src/CIOHelper.cpp b/Emuander/src/CIOHelper.cpp index cd01120..9dbf94d 100644 --- a/Emuander/src/CIOHelper.cpp +++ b/Emuander/src/CIOHelper.cpp @@ -116,4 +116,32 @@ namespace Neander std::cout << "NEG: " << static_cast(registersToPrint.m_negativeCondition) << std::endl; std::cout << "ZER: " << static_cast(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 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; + } } \ No newline at end of file diff --git a/Emuander/src/main.cpp b/Emuander/src/main.cpp index f180045..455dc75 100644 --- a/Emuander/src/main.cpp +++ b/Emuander/src/main.cpp @@ -1,32 +1,54 @@ #include #include -#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 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 inputPath(parser, "input_program_file", "Input program file (.mem)", { 'i' }); + args::ValueFlag 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; } \ No newline at end of file