Skip to content

Commit 59429fe

Browse files
committed
Upgrade mem editor from Geargrafx
1 parent 6ccff17 commit 59429fe

File tree

3 files changed

+621
-208
lines changed

3 files changed

+621
-208
lines changed

platforms/desktop-shared/gui_debug.cpp

Lines changed: 110 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "imgui/imgui.h"
2222
#include "imgui/memory_editor.h"
2323
#include "imgui/colors.h"
24+
#include "nfd/nfd.h"
2425
#include "config.h"
2526
#include "emu.h"
2627
#include "renderer.h"
@@ -60,9 +61,11 @@ static bool goto_address_requested = false;
6061
static u16 goto_address_target = 0;
6162
static bool goto_back_requested = false;
6263
static int goto_back = 0;
64+
static char set_value_buffer[5] = {0};
6365

6466
static void debug_window_processor(void);
6567
static void debug_window_memory(void);
68+
static void memory_editor_menu(void);
6669
static void debug_window_disassembler(void);
6770
static void debug_window_vram_registers(void);
6871
static void debug_window_vram(void);
@@ -197,61 +200,132 @@ void gui_debug_go_back(void)
197200

198201
void gui_debug_copy_memory(void)
199202
{
200-
int size = 0;
201-
u8* data = NULL;
202-
mem_edit[current_mem_edit].Copy(&data, &size);
203+
mem_edit[current_mem_edit].Copy();
204+
}
203205

204-
if (IsValidPointer(data))
205-
{
206-
std::string text;
206+
void gui_debug_paste_memory(void)
207+
{
208+
mem_edit[current_mem_edit].Paste();
209+
}
210+
211+
static void memory_editor_menu(void)
212+
{
213+
ImGui::BeginMenuBar();
207214

208-
for (int i = 0; i < size; i++)
215+
if (ImGui::BeginMenu("File"))
216+
{
217+
if (ImGui::MenuItem("Save Memory As..."))
209218
{
210-
char byte[3];
211-
sprintf(byte, "%02X", data[i]);
212-
if (i > 0)
213-
text += " ";
214-
text += byte;
219+
nfdchar_t *outPath;
220+
nfdfilteritem_t filterItem[1] = { { "Memory Dump Files", "txt" } };
221+
nfdresult_t result = NFD_SaveDialog(&outPath, filterItem, 1, NULL, NULL);
222+
if (result == NFD_OKAY)
223+
{
224+
mem_edit[current_mem_edit].SaveToFile(outPath);
225+
NFD_FreePath(outPath);
226+
}
227+
else if (result != NFD_CANCEL)
228+
{
229+
Log("Save Memory Dump Error: %s", NFD_GetError());
230+
}
215231
}
216232

217-
SDL_SetClipboardText(text.c_str());
233+
ImGui::EndMenu();
218234
}
219-
}
220235

221-
void gui_debug_paste_memory(void)
222-
{
223-
char* clipboard = SDL_GetClipboardText();
224-
225-
if (IsValidPointer(clipboard))
236+
if (ImGui::BeginMenu("Edit"))
226237
{
227-
std::string text(clipboard);
238+
if (ImGui::MenuItem("Copy", "Ctrl+C"))
239+
{
240+
gui_debug_copy_memory();
241+
}
228242

229-
text.erase(std::remove(text.begin(), text.end(), ' '), text.end());
243+
if (ImGui::MenuItem("Paste", "Ctrl+V"))
244+
{
245+
gui_debug_paste_memory();
246+
}
230247

231-
size_t buffer_size = text.size() / 2;
232-
u8* data = new u8[buffer_size];
248+
ImGui::EndMenu();
249+
}
233250

234-
for (size_t i = 0; i < buffer_size; i ++)
251+
if (ImGui::BeginMenu("Selection"))
252+
{
253+
if (ImGui::MenuItem("Select All", "Ctrl+A"))
254+
{
255+
mem_edit[current_mem_edit].SelectAll();
256+
}
257+
258+
if (ImGui::MenuItem("Clear Selection"))
235259
{
236-
std::string byte = text.substr(i * 2, 2);
260+
mem_edit[current_mem_edit].ClearSelection();
261+
}
237262

238-
try
263+
if (ImGui::BeginMenu("Set value"))
264+
{
265+
ImGui::SetNextItemWidth(50);
266+
if (ImGui::InputTextWithHint("##set_value", "XXXX", set_value_buffer, IM_ARRAYSIZE(set_value_buffer), ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase))
239267
{
240-
data[i] = (u8)std::stoul(byte, 0, 16);
268+
try
269+
{
270+
mem_edit[current_mem_edit].SetValueToSelection((int)std::stoul(set_value_buffer, 0, 16));
271+
set_value_buffer[0] = 0;
272+
}
273+
catch(const std::invalid_argument&)
274+
{
275+
}
241276
}
242-
catch(const std::invalid_argument&)
277+
ImGui::SameLine();
278+
if (ImGui::Button("Set!", ImVec2(40, 0)))
243279
{
244-
delete[] data;
245-
return;
280+
try
281+
{
282+
mem_edit[current_mem_edit].SetValueToSelection((int)std::stoul(set_value_buffer, 0, 16));
283+
set_value_buffer[0] = 0;
284+
}
285+
catch(const std::invalid_argument&)
286+
{
287+
}
246288
}
289+
ImGui::EndMenu();
247290
}
248291

249-
mem_edit[current_mem_edit].Paste(data, buffer_size);
292+
ImGui::EndMenu();
293+
}
294+
295+
if (ImGui::BeginMenu("Bookmarks"))
296+
{
297+
if (ImGui::MenuItem("Clear All"))
298+
{
299+
mem_edit[current_mem_edit].RemoveBookmarks();
300+
}
250301

251-
delete[] data;
302+
if (ImGui::MenuItem("Add Bookmark"))
303+
{
304+
mem_edit[current_mem_edit].AddBookmark();
305+
}
306+
307+
std::vector<MemEditor::Bookmark>* bookmarks = mem_edit[current_mem_edit].GetBookmarks();
308+
309+
if (bookmarks->size() > 0)
310+
ImGui::Separator();
311+
312+
for (long unsigned int i = 0; i < bookmarks->size(); i++)
313+
{
314+
MemEditor::Bookmark* bookmark = &(*bookmarks)[i];
315+
316+
char label[80];
317+
snprintf(label, 80, "$%04X: %s", bookmark->address, bookmark->name);
318+
319+
if (ImGui::MenuItem(label))
320+
{
321+
mem_edit[current_mem_edit].JumpToAddress(bookmark->address);
322+
}
323+
}
324+
325+
ImGui::EndMenu();
252326
}
253327

254-
SDL_free(clipboard);
328+
ImGui::EndMenuBar();
255329
}
256330

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

263-
ImGui::Begin("Memory Editor", &config_debug.show_memory);
337+
ImGui::Begin("Memory Editor", &config_debug.show_memory, ImGuiWindowFlags_MenuBar);
338+
339+
memory_editor_menu();
264340

265341
GearcolecoCore* core = emu_get_core();
266342
Memory* memory = core->GetMemory();

0 commit comments

Comments
 (0)