Skip to content

Commit

Permalink
Added RetroShell command "mem load bin" and "mem save bin" (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkwhoffmann committed Aug 4, 2024
1 parent 558dde6 commit c3acf94
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 69 deletions.
70 changes: 7 additions & 63 deletions Emulator/Components/Memory/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,67 +655,11 @@ Memory::extFingerprint() const
return util::crc32(ext, config.extSize);
}

/*
const char *
Memory::romTitle()
{
return RomFile::title(romFingerprint());
}
const char *
Memory::romVersion()
{
return RomFile::version(romFingerprint());
}
const char *
Memory::romReleased()
{
return RomFile::released(romFingerprint());
}
const char *
Memory::romModel()
{
return RomFile::model(romFingerprint());
}
const char *
Memory::extTitle()
{
return RomFile::title(extFingerprint());
}
const char *
Memory::extVersion()
{
return RomFile::version(extFingerprint());
}
const char *
Memory::extReleased()
{
return RomFile::released(extFingerprint());
}
const char *
Memory::extModel()
{
return RomFile::model(extFingerprint());
}
bool
Memory::hasArosRom() const
{
return RomFile::isArosRom(romFingerprint());
}
*/

void
Memory::loadRom(MediaFile &file)
{
assert(amiga.isPoweredOff());
// if (amiga.isPoweredOn()) throw Error(ERROR_POWERED_ON);

try {

auto &romFile = dynamic_cast<RomFile &>(file);
Expand Down Expand Up @@ -802,25 +746,25 @@ Memory::loadExt(const u8 *buf, isize len)
void
Memory::saveRom(const std::filesystem::path &path)
{
if (rom == nullptr) return;
if (rom == nullptr) throw Error(ERROR_ROM_MISSING);

RomFile file(rom, config.romSize);
file.writeToFile(path);
}

void
Memory::saveWom(const std::filesystem::path &path)
{
if (wom == nullptr) return;
if (wom == nullptr) throw Error(ERROR_ROM_MISSING);

RomFile file(wom, config.womSize);
file.writeToFile(path);
}

void
Memory::saveExt(const std::filesystem::path &path)
{
if (ext == nullptr) return;
if (ext == nullptr) throw Error(ERROR_ROM_MISSING);

RomFile file(ext, config.extSize);
file.writeToFile(path);
Expand Down
40 changes: 40 additions & 0 deletions Emulator/Components/Memory/MemoryDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,46 @@ MemoryDebugger::write(u32 addr, u32 val, isize sz, isize repeats)
}
}

void
MemoryDebugger::load(std::istream& is, u32 addr)
{
for (;; addr++) {

auto val = is.get();
if (val == EOF) return;

mem.patch(addr, u8(val));
}
}

void
MemoryDebugger::load(fs::path& path, u32 addr)
{
std::ifstream stream(path, std::ifstream::binary);
if (!stream.is_open()) throw Error(ERROR_FILE_NOT_FOUND, path);

load(stream, addr);
}

void
MemoryDebugger::save(std::ostream& os, u32 addr, isize count)
{
for (isize i = 0; i < count; i++) {

auto val = mem.peek8 <ACCESSOR_CPU> (u32(addr + i));
os.put(val);
}
}

void
MemoryDebugger::save(fs::path& path, u32 addr, isize count)
{
std::ofstream stream(path, std::ifstream::binary);
if (!stream.is_open()) throw Error(ERROR_FILE_CANT_CREATE, path);

save(stream, addr, count);
}

bool
MemoryDebugger::isReadable(ChipsetReg reg) const
{
Expand Down
8 changes: 8 additions & 0 deletions Emulator/Components/Memory/MemoryDebugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ class MemoryDebugger final : public SubComponent
// Writes a value into memory (multiple times)
void write(u32 addr, u32 val, isize sz, isize repeats = 1);

// Loads a chunk of memory from a stream or file
void load(std::istream& is, u32 addr);
void load(fs::path& path, u32 addr);

// Saves a chunk of memory to a stream or file
void save(std::ostream& is, u32 addr, isize count);
void save(fs::path& path, u32 addr, isize count);


//
// Handling registers
Expand Down
39 changes: 36 additions & 3 deletions Emulator/Misc/RetroShell/CommandConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ CommandConsole::initCommands(Command &root)
initSetters(root, mem);

root.add({cmd, "load"},
"Installs a Rom image");
"Load memory contents from a file");

root.add({cmd, "load", "rom"}, { Arg::path },
"Installs a Kickstart Rom",
Expand All @@ -328,12 +328,45 @@ CommandConsole::initCommands(Command &root)
amiga.mem.loadRom(argv.front());
});

root.add({cmd, "load", "extrom"}, { Arg::path },
"Installs a Rom extension",
root.add({cmd, "load", "ext"}, { Arg::path },
"Installs an extension Rom",
[this](Arguments& argv, long value) {

amiga.mem.loadExt(argv.front());
});

root.add({cmd, "load", "bin"}, { Arg::path, Arg::address },
"Loads a chunk of memory",
[this](Arguments& argv, long value) {

fs::path path(argv[0]);
amiga.mem.debugger.load(path, parseAddr(argv[1]));
});

root.add({cmd, "save"},
"Save memory contents to a file");

root.add({cmd, "save", "rom"}, { Arg::path },
"Saves the Kickstart Rom",
[this](Arguments& argv, long value) {

amiga.mem.saveRom(argv[0]);
});

root.add({cmd, "save", "ext"}, { Arg::path },
"Saves the extension Rom",
[this](Arguments& argv, long value) {

amiga.mem.saveExt(argv[0]);
});

root.add({cmd, "save", "bin"}, { Arg::path, Arg::address, Arg::count },
"Loads a chunk of memory",
[this](Arguments& argv, long value) {

fs::path path(argv[0]);
amiga.mem.debugger.save(path, parseAddr(argv[1]), parseNum(argv[2]));
});
}

//
Expand Down
10 changes: 10 additions & 0 deletions Emulator/Utilities/BasicTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ using std::string;
#endif


//
// Filesystem
//

#ifdef __cplusplus
#include <filesystem>
namespace vamiga { namespace fs = std::filesystem; }
#endif


//
// Integers
//
Expand Down
3 changes: 0 additions & 3 deletions Emulator/Utilities/IOUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
#include <sys/stat.h>
#include <vector>

// Namespace aliases
namespace vamiga { namespace fs = std::filesystem; }

namespace vamiga::util {

//
Expand Down

0 comments on commit c3acf94

Please sign in to comment.