Skip to content

Commit

Permalink
Upgrade mem editor from Geargrafx
Browse files Browse the repository at this point in the history
  • Loading branch information
drhelius committed Dec 14, 2024
1 parent 6ccff17 commit 59429fe
Show file tree
Hide file tree
Showing 3 changed files with 621 additions and 208 deletions.
144 changes: 110 additions & 34 deletions platforms/desktop-shared/gui_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "imgui/imgui.h"
#include "imgui/memory_editor.h"
#include "imgui/colors.h"
#include "nfd/nfd.h"
#include "config.h"
#include "emu.h"
#include "renderer.h"
Expand Down Expand Up @@ -60,9 +61,11 @@ static bool goto_address_requested = false;
static u16 goto_address_target = 0;
static bool goto_back_requested = false;
static int goto_back = 0;
static char set_value_buffer[5] = {0};

static void debug_window_processor(void);
static void debug_window_memory(void);
static void memory_editor_menu(void);
static void debug_window_disassembler(void);
static void debug_window_vram_registers(void);
static void debug_window_vram(void);
Expand Down Expand Up @@ -197,61 +200,132 @@ void gui_debug_go_back(void)

void gui_debug_copy_memory(void)
{
int size = 0;
u8* data = NULL;
mem_edit[current_mem_edit].Copy(&data, &size);
mem_edit[current_mem_edit].Copy();
}

if (IsValidPointer(data))
{
std::string text;
void gui_debug_paste_memory(void)
{
mem_edit[current_mem_edit].Paste();
}

static void memory_editor_menu(void)
{
ImGui::BeginMenuBar();

for (int i = 0; i < size; i++)
if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("Save Memory As..."))
{
char byte[3];
sprintf(byte, "%02X", data[i]);
if (i > 0)
text += " ";
text += byte;
nfdchar_t *outPath;
nfdfilteritem_t filterItem[1] = { { "Memory Dump Files", "txt" } };
nfdresult_t result = NFD_SaveDialog(&outPath, filterItem, 1, NULL, NULL);
if (result == NFD_OKAY)
{
mem_edit[current_mem_edit].SaveToFile(outPath);
NFD_FreePath(outPath);
}
else if (result != NFD_CANCEL)
{
Log("Save Memory Dump Error: %s", NFD_GetError());
}
}

SDL_SetClipboardText(text.c_str());
ImGui::EndMenu();
}
}

void gui_debug_paste_memory(void)
{
char* clipboard = SDL_GetClipboardText();

if (IsValidPointer(clipboard))
if (ImGui::BeginMenu("Edit"))
{
std::string text(clipboard);
if (ImGui::MenuItem("Copy", "Ctrl+C"))
{
gui_debug_copy_memory();
}

text.erase(std::remove(text.begin(), text.end(), ' '), text.end());
if (ImGui::MenuItem("Paste", "Ctrl+V"))
{
gui_debug_paste_memory();
}

size_t buffer_size = text.size() / 2;
u8* data = new u8[buffer_size];
ImGui::EndMenu();
}

for (size_t i = 0; i < buffer_size; i ++)
if (ImGui::BeginMenu("Selection"))
{
if (ImGui::MenuItem("Select All", "Ctrl+A"))
{
mem_edit[current_mem_edit].SelectAll();
}

if (ImGui::MenuItem("Clear Selection"))
{
std::string byte = text.substr(i * 2, 2);
mem_edit[current_mem_edit].ClearSelection();
}

try
if (ImGui::BeginMenu("Set value"))
{
ImGui::SetNextItemWidth(50);
if (ImGui::InputTextWithHint("##set_value", "XXXX", set_value_buffer, IM_ARRAYSIZE(set_value_buffer), ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase))
{
data[i] = (u8)std::stoul(byte, 0, 16);
try
{
mem_edit[current_mem_edit].SetValueToSelection((int)std::stoul(set_value_buffer, 0, 16));
set_value_buffer[0] = 0;
}
catch(const std::invalid_argument&)
{
}
}
catch(const std::invalid_argument&)
ImGui::SameLine();
if (ImGui::Button("Set!", ImVec2(40, 0)))
{
delete[] data;
return;
try
{
mem_edit[current_mem_edit].SetValueToSelection((int)std::stoul(set_value_buffer, 0, 16));
set_value_buffer[0] = 0;
}
catch(const std::invalid_argument&)
{
}
}
ImGui::EndMenu();
}

mem_edit[current_mem_edit].Paste(data, buffer_size);
ImGui::EndMenu();
}

if (ImGui::BeginMenu("Bookmarks"))
{
if (ImGui::MenuItem("Clear All"))
{
mem_edit[current_mem_edit].RemoveBookmarks();
}

delete[] data;
if (ImGui::MenuItem("Add Bookmark"))
{
mem_edit[current_mem_edit].AddBookmark();
}

std::vector<MemEditor::Bookmark>* bookmarks = mem_edit[current_mem_edit].GetBookmarks();

if (bookmarks->size() > 0)
ImGui::Separator();

for (long unsigned int i = 0; i < bookmarks->size(); i++)
{
MemEditor::Bookmark* bookmark = &(*bookmarks)[i];

char label[80];
snprintf(label, 80, "$%04X: %s", bookmark->address, bookmark->name);

if (ImGui::MenuItem(label))
{
mem_edit[current_mem_edit].JumpToAddress(bookmark->address);
}
}

ImGui::EndMenu();
}

SDL_free(clipboard);
ImGui::EndMenuBar();
}

static void debug_window_memory(void)
Expand All @@ -260,7 +334,9 @@ static void debug_window_memory(void)
ImGui::SetNextWindowPos(ImVec2(567, 249), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(324, 308), ImGuiCond_FirstUseEver);

ImGui::Begin("Memory Editor", &config_debug.show_memory);
ImGui::Begin("Memory Editor", &config_debug.show_memory, ImGuiWindowFlags_MenuBar);

memory_editor_menu();

GearcolecoCore* core = emu_get_core();
Memory* memory = core->GetMemory();
Expand Down
Loading

0 comments on commit 59429fe

Please sign in to comment.