From dd951ec62f03ab67c9d51b43627c4e033f54f7f8 Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Sun, 25 Jun 2017 09:37:31 -0400 Subject: [PATCH] Made the debugger 'load' length field be optional, C++-ification --- src/emu/debug/debugcmd.cpp | 60 +++++++++++++++++++++----------------- src/emu/debug/debughlp.cpp | 8 ++--- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/emu/debug/debugcmd.cpp b/src/emu/debug/debugcmd.cpp index 145aaa0ac3f..ecb0c7d1c8a 100644 --- a/src/emu/debug/debugcmd.cpp +++ b/src/emu/debug/debugcmd.cpp @@ -20,6 +20,7 @@ #include "natkeyboard.h" #include "render.h" #include +#include @@ -206,10 +207,10 @@ debugger_commands::debugger_commands(running_machine& machine, debugger_cpu& cpu m_console.register_command("savei", CMDFLAG_NONE, AS_IO, 3, 4, std::bind(&debugger_commands::execute_save, this, _1, _2)); m_console.register_command("saveo", CMDFLAG_NONE, AS_DECRYPTED_OPCODES, 3, 4, std::bind(&debugger_commands::execute_save, this, _1, _2)); - m_console.register_command("load", CMDFLAG_NONE, AS_PROGRAM, 3, 4, std::bind(&debugger_commands::execute_load, this, _1, _2)); - m_console.register_command("loadd", CMDFLAG_NONE, AS_DATA, 3, 4, std::bind(&debugger_commands::execute_load, this, _1, _2)); - m_console.register_command("loadi", CMDFLAG_NONE, AS_IO, 3, 4, std::bind(&debugger_commands::execute_load, this, _1, _2)); - m_console.register_command("loado", CMDFLAG_NONE, AS_DECRYPTED_OPCODES, 3, 4, std::bind(&debugger_commands::execute_load, this, _1, _2)); + m_console.register_command("load", CMDFLAG_NONE, AS_PROGRAM, 2, 4, std::bind(&debugger_commands::execute_load, this, _1, _2)); + m_console.register_command("loadd", CMDFLAG_NONE, AS_DATA, 2, 4, std::bind(&debugger_commands::execute_load, this, _1, _2)); + m_console.register_command("loadi", CMDFLAG_NONE, AS_IO, 2, 4, std::bind(&debugger_commands::execute_load, this, _1, _2)); + m_console.register_command("loado", CMDFLAG_NONE, AS_DECRYPTED_OPCODES, 2, 4, std::bind(&debugger_commands::execute_load, this, _1, _2)); m_console.register_command("dump", CMDFLAG_NONE, AS_PROGRAM, 3, 7, std::bind(&debugger_commands::execute_dump, this, _1, _2)); m_console.register_command("dumpd", CMDFLAG_NONE, AS_DATA, 3, 7, std::bind(&debugger_commands::execute_dump, this, _1, _2)); @@ -1684,44 +1685,51 @@ void debugger_commands::execute_save(int ref, const std::vector &pa void debugger_commands::execute_load(int ref, const std::vector ¶ms) { - u64 offset, endoffset, length; + u64 offset, endoffset, length = 0; address_space *space; - FILE *f; u64 i; - /* validate parameters */ + // validate parameters if (!validate_number_parameter(params[1], offset)) return; - if (!validate_number_parameter(params[2], length)) + if (params.size() > 2 && !validate_number_parameter(params[2], length)) return; if (!validate_cpu_space_parameter((params.size() > 3) ? params[3].c_str() : nullptr, ref, space)) return; - /* determine the addresses to read */ - endoffset = space->address_to_byte(offset + length - 1) & space->bytemask(); - offset = space->address_to_byte(offset) & space->bytemask(); - - /* open the file */ - f = fopen(params[0].c_str(), "rb"); - if (!f) + // open the file + std::ifstream f; + f.open(params[0], std::ifstream::in | std::ifstream::binary); + if (f.fail()) { m_console.printf("Error opening file '%s'\n", params[0].c_str()); return; } - /* now read the data in, ignore endoffset and load entire file if length has been set to zero (offset-1) */ - u8 byte; - for (i = offset; i <= endoffset || endoffset == offset - 1 ; i++) + // determine the file size, if not specified + if (params.size() <= 2) { - fread(&byte, 1, 1, f); - /* check if end of file has been reached and stop loading if it has */ - if (feof(f)) - break; - m_cpu.write_byte(*space, i, byte, true); + f.seekg(0, std::ios::end); + length = f.tellg(); + f.seekg(0); } - /* close the file */ - fclose(f); - if ( i == offset) + + // determine the addresses to read + endoffset = space->address_to_byte(offset + length - 1) & space->bytemask(); + offset = space->address_to_byte(offset) & space->bytemask(); + + // now read the data in, ignore endoffset and load entire file if length has been set to zero (offset-1) + for (i = offset; f.good() && (i <= endoffset || endoffset == offset - 1); i++) + { + char byte; + f.read(&byte, 1); + if (f) + m_cpu.write_byte(*space, i, byte, true); + } + + if (!f.good()) + m_console.printf("I/O error, load failed\n"); + else if (i == offset) m_console.printf("Length specified too large, load failed\n"); else m_console.printf("Data loaded successfully to memory : 0x%X to 0x%X\n", offset, i-1); diff --git a/src/emu/debug/debughlp.cpp b/src/emu/debug/debughlp.cpp index dd46472abb2..2848f970003 100644 --- a/src/emu/debug/debughlp.cpp +++ b/src/emu/debug/debughlp.cpp @@ -116,9 +116,9 @@ static const help_item static_help_list[] = " save ,
,[,] -- save binary program memory to the given file\n" " saved ,
,[,] -- save binary data memory to the given file\n" " savei ,
,[,] -- save binary I/O memory to the given file\n" - " load ,
,[,] -- load binary program memory from the given file\n" - " loadd ,
,[,] -- load binary data memory from the given file\n" - " loadi ,
,[,] -- load binary I/O memory from the given file\n" + " load ,
[,,] -- load binary program memory from the given file\n" + " loadd ,
[,,] -- load binary data memory from the given file\n" + " loadi ,
[,,] -- load binary I/O memory from the given file\n" " map
-- map logical program address to physical address and bank\n" " mapd
-- map logical data address to physical address and bank\n" " mapi
-- map logical I/O address to physical address and bank\n" @@ -627,7 +627,7 @@ static const help_item static_help_list[] = { "load", "\n" - " load[{d|i}] ,
,[,]\n" + " load[{d|i}] ,
[,,]\n" "\n" "The load/loadd/loadi commands load raw memory from the binary file specified in the " "parameter. 'load' will load program space memory, while 'loadd' will load data space memory "