diff --git a/CMakeLists.txt b/CMakeLists.txt index 46e93ae..546848e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,6 +94,8 @@ list(APPEND TOOLS_SOURCE_FILES ${CMAKE_CURRENT_LIST_DIR}/src/tools/Tools.cpp ${CMAKE_CURRENT_LIST_DIR}/src/hardware/VirtualHardDrive.cpp + + ${CMAKE_CURRENT_LIST_DIR}/src/debugger/DisassemblyLoader.cpp ) #----------------------------------------------------------------------------------------- diff --git a/other/build.nr b/other/build.nr index d65a7d9..9c54547 100644 --- a/other/build.nr +++ b/other/build.nr @@ -1 +1 @@ -1638 +1639 diff --git a/src/tools/Tools.cpp b/src/tools/Tools.cpp index b0bba67..fa81a3b 100644 --- a/src/tools/Tools.cpp +++ b/src/tools/Tools.cpp @@ -1,9 +1,12 @@ #include "Tools.hpp" +#include +#include #include #include #include "../hardware/VirtualHardDrive.hpp" #include "GlobalData.hpp" +#include "debugger/DisassemblyLoader.hpp" #include namespace dragon @@ -51,6 +54,12 @@ namespace dragon if (rValue != ErrorNoError) return rValue; } + else if (tool == "print-disassembly") + { + rValue = tool_print_disassembly(argc, argv); + if (rValue != ErrorNoError) + return rValue; + } else if (tool == "--help") { print_application_help(); @@ -251,7 +260,7 @@ namespace dragon } if (flags_str.len() > 0) flags_str.substr(0, flags_str.len() - 1); - + out.fg(ostd::ConsoleColors::Yellow).p(flags_str).fg(ostd::ConsoleColors::Cyan).nl(); } out.fg(ostd::ConsoleColors::BrightGray); @@ -287,7 +296,7 @@ namespace dragon return -1; }; auto make_bytestream = [](uint16_t size, ostd::Byte value = 0xFF) -> ostd::ByteStream { - + ostd::ByteStream stream; for (int16_t i = 0; i < size; i++) stream.push_back(value); @@ -306,7 +315,7 @@ namespace dragon bool has_args = true; bool part_started = false; tPartData _part_data; - uint32_t part_start_addr = data::DPTStructure::DiskStartAddr; + uint32_t part_start_addr = data::DPTStructure::DiskStartAddr; while (has_args) { ostd::String arg = argv[arg_index]; @@ -427,7 +436,7 @@ namespace dragon dpt_block.w_String(addr, part.label, false, true); addr += data::DPTStructure::EntryLabelSizeBytes; } - + int16_t index = 0; for (auto& b : dpt_block.getData()) { @@ -442,6 +451,79 @@ namespace dragon return ErrorNoError; } + int32_t Tools::tool_print_disassembly(int argc, char** argv) + { + using TableList = std::vector; + if (argc < 3) + { + out.fg(ostd::ConsoleColors::Red).p("Error: too few arguments.").nl(); + out.fg(ostd::ConsoleColors::Red).p(" Usage: ./dtools print-disassembly ").reset().nl(); + return ErrorPrintDisassemblyTooFewArgs; + } + ostd::String arg1 = argv[2]; + arg1.trim(); + TableList codeTable; + TableList labelTable; + TableList dataTable; + if (arg1 == "-d") + { + if (argc < 4) + { + out.fg(ostd::ConsoleColors::Red).p("Error: too few arguments.").nl(); + out.fg(ostd::ConsoleColors::Red).p(" Usage: ./dtools print-disassembly -d ").reset().nl(); + return ErrorPrintDisassemblyTooFewArgs; + } + dragon::DisassemblyLoader::loadDirectory(argv[3]); + codeTable = dragon::DisassemblyLoader::getCodeTable(); + labelTable = dragon::DisassemblyLoader::getLabelTable(); + dataTable = dragon::DisassemblyLoader::getDataTable(); + } + else if (arg1 == "-f") + { + const auto& table = dragon::DisassemblyLoader::loadFile(argv[3]); + if (!table.isInitialized()) + { + out.fg(ostd::ConsoleColors::Red).p("Error: Invalid disassembly file.").reset().nl(); + return ErrorPrintDisassemblyInvalidFile; + } + codeTable = table.getCodeTable(); + labelTable = table.getLabelTable(); + dataTable = table.getDataTable(); + } + else + { + out.fg(ostd::ConsoleColors::Red).p("Error: invalid argument.").nl(); + out.fg(ostd::ConsoleColors::Red).p(" Usage: ./dtools print-disassembly -d ").reset().nl(); + return ErrorPrintDisassemblyInvalidArg; + } + out.bg(ostd::ConsoleColors::White).fg(ostd::ConsoleColors::Black).p("DATA:").reset().nl(); + for (const auto& line : dataTable) + { + out.fg(ostd::ConsoleColors::BrightGray).p(ostd::Utils::getHexStr(line.addr, true, 2)).p("\t\t"); + out.fg(ostd::ConsoleColors::Green).p(line.code).p("\t\t"); + out.fg(ostd::ConsoleColors::Blue).p(line.size).p(" bytes\t\t"); + out.reset().nl(); + } + out.nl(); + out.bg(ostd::ConsoleColors::White).fg(ostd::ConsoleColors::Black).p("LABELS:").reset().nl(); + for (const auto& line : labelTable) + { + out.fg(ostd::ConsoleColors::BrightGray).p(ostd::Utils::getHexStr(line.addr, true, 2)).p("\t\t"); + out.fg(ostd::ConsoleColors::Green).p(line.code).p("\t\t"); + out.reset().nl(); + } + out.nl(); + out.bg(ostd::ConsoleColors::White).fg(ostd::ConsoleColors::Black).p("CODE:").reset().nl(); + for (const auto& line : codeTable) + { + out.fg(ostd::ConsoleColors::BrightGray).p(ostd::Utils::getHexStr(line.addr, true, 2)).p("\t\t"); + out.fg(ostd::ConsoleColors::Green).p(line.code).p("\t\t"); + out.reset().nl(); + } + out.nl(); + return ErrorNoError; + } + void Tools::print_application_help(void) { out.nl().fg(ostd::ConsoleColors::Yellow).p("List of available tools:").nl().nl(); @@ -469,6 +551,13 @@ namespace dragon out.p(" -l (optional) Used to specify the partition's label.").nl(); out.p(" -f (optional) Used to specify one single flag for the partition. Use multiple -f parameters for multiple flags.").nl().nl(); + out.fg(ostd::ConsoleColors::Blue).p("print-disassembly [-d ] | [-f ]").nl(); + out.fg(ostd::ConsoleColors::Green).p("The tool is used to parse and print disassembly tables.").nl(); + out.p(" Path to the directory containing the disassembly files to parse. (Used only for -d option)").nl(); + out.p(" Path to the disassembly file to parse. (Used only for -f option)").nl(); + out.p(" -d Used to specify a directory path (mutually exclusive with -f).").nl(); + out.p(" -f Used to specify a file path (mutually exclusive with -d).").nl().nl(); + out.fg(ostd::ConsoleColors::Magenta).p("Usage: ./dtools [...arguments...]").nl().nl().reset(); } @@ -485,4 +574,4 @@ namespace dragon outTool = tool; return ErrorNoError; } -} \ No newline at end of file +} diff --git a/src/tools/Tools.hpp b/src/tools/Tools.hpp index c6e50b6..07a0c13 100644 --- a/src/tools/Tools.hpp +++ b/src/tools/Tools.hpp @@ -18,6 +18,7 @@ namespace dragon static int32_t tool_load_binary(int argc, char** argv); static int32_t tool_read_dpt(int argc, char** argv); static int32_t tool_new_dpt(int argc, char** argv); + static int32_t tool_print_disassembly(int argc, char** argv); static void print_application_help(void); static int32_t get_tool(int argc, char** argv, ostd::String& outTool); @@ -51,6 +52,9 @@ namespace dragon inline static constexpr int32_t ErrorNewDPTDiskOverflow = 19; inline static constexpr int32_t ErrorNewDPTUnknownFlag = 20; inline static constexpr int32_t ErrorNewDPTTooManyPartitions = 21; + inline static constexpr int32_t ErrorPrintDisassemblyTooFewArgs = 22; + inline static constexpr int32_t ErrorPrintDisassemblyInvalidFile = 23; + inline static constexpr int32_t ErrorPrintDisassemblyInvalidArg = 24; }; }