Skip to content

Commit

Permalink
Add copy/paste to memory editor. Fix #50
Browse files Browse the repository at this point in the history
  • Loading branch information
drhelius committed Jun 10, 2024
1 parent 0d8e1fb commit 47af433
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 16 deletions.
6 changes: 6 additions & 0 deletions platforms/desktop-shared/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,12 @@ static void sdl_shortcuts_gui(const SDL_Event* event)

switch (key)
{
case SDL_SCANCODE_C:
gui_shortcut(gui_ShortcutDebugCopy);
break;
case SDL_SCANCODE_V:
gui_shortcut(gui_ShortcutDebugPaste);
break;
case SDL_SCANCODE_O:
gui_shortcut(gui_ShortcutOpenROM);
break;
Expand Down
6 changes: 6 additions & 0 deletions platforms/desktop-shared/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ void gui_shortcut(gui_ShortCutEvent event)
if (config_debug.debug)
gui_debug_go_back();
break;
case gui_ShortcutDebugCopy:
gui_debug_copy_memory();
break;
case gui_ShortcutDebugPaste:
gui_debug_paste_memory();
break;
case gui_ShortcutShowMainMenu:
config_emulator.show_menu = !config_emulator.show_menu;
break;
Expand Down
2 changes: 2 additions & 0 deletions platforms/desktop-shared/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ enum gui_ShortCutEvent
gui_ShortcutDebugBreakpoint,
gui_ShortcutDebugRuntocursor,
gui_ShortcutDebugGoBack,
gui_ShortcutDebugCopy,
gui_ShortcutDebugPaste,
gui_ShortcutShowMainMenu
};

Expand Down
81 changes: 71 additions & 10 deletions platforms/desktop-shared/gui_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ struct DisassmeblerLine
std::string symbol;
};

static MemEditor mem_edit_bios;
static MemEditor mem_edit_ram;
static MemEditor mem_edit_sgmram;
static MemEditor mem_edit_rom;
static MemEditor mem_edit_vram;
static MemEditor mem_edit[5];
static int current_mem_edit = 0;
static std::vector<DebugSymbol> symbols;
static Memory::stDisassembleRecord* selected_record = NULL;
static char brk_address_cpu[5] = "";
Expand Down Expand Up @@ -197,6 +194,65 @@ void gui_debug_go_back(void)
goto_back_requested = true;
}

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

if (IsValidPointer(data))
{
std::string text;

for (int i = 0; i < size; i++)
{
char byte[3];
sprintf(byte, "%02X", data[i]);
if (i > 0)
text += " ";
text += byte;
}

SDL_SetClipboardText(text.c_str());
}
}

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

if (IsValidPointer(clipboard))
{
std::string text(clipboard);

text.erase(std::remove(text.begin(), text.end(), ' '), text.end());

int buffer_size = text.size() / 2;
u8* data = new u8[buffer_size];

for (size_t i = 0; i < buffer_size; i ++)
{
std::string byte = text.substr(i * 2, 2);

try
{
data[i] = (u8)std::stoul(byte, 0, 16);
}
catch(const std::invalid_argument&)
{
delete[] data;
return;
}
}

mem_edit[current_mem_edit].Paste(data, buffer_size);

delete[] data;
}

SDL_free(clipboard);
}

static void debug_window_memory(void)
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 8.0f);
Expand Down Expand Up @@ -226,39 +282,44 @@ static void debug_window_memory(void)
if (ImGui::BeginTabItem("BIOS"))
{
ImGui::PushFont(gui_default_font);
mem_edit_bios.Draw(memory->GetBios(), 0x2000, 0);
current_mem_edit = 0;
mem_edit[current_mem_edit].Draw(memory->GetBios(), 0x2000, 0);
ImGui::PopFont();
ImGui::EndTabItem();
}

if (ImGui::BeginTabItem("RAM"))
{
ImGui::PushFont(gui_default_font);
mem_edit_ram.Draw(memory->GetRam(), 0x400, 0x7000);
current_mem_edit = 1;
mem_edit[current_mem_edit].Draw(memory->GetRam(), 0x400, 0x7000);
ImGui::PopFont();
ImGui::EndTabItem();
}

if (ImGui::BeginTabItem("SGM RAM"))
{
ImGui::PushFont(gui_default_font);
mem_edit_sgmram.Draw(memory->GetSGMRam(), 0x8000, 0x0000);
current_mem_edit = 2;
mem_edit[current_mem_edit].Draw(memory->GetSGMRam(), 0x8000, 0x0000);
ImGui::PopFont();
ImGui::EndTabItem();
}

if (IsValidPointer(cart->GetROM()) && ImGui::BeginTabItem("ROM"))
{
ImGui::PushFont(gui_default_font);
mem_edit_rom.Draw(cart->GetROM(), cart->GetROMSize(), 0x0000);
current_mem_edit = 3;
mem_edit[current_mem_edit].Draw(cart->GetROM(), cart->GetROMSize(), 0x0000);
ImGui::PopFont();
ImGui::EndTabItem();
}

if (ImGui::BeginTabItem("VRAM"))
{
ImGui::PushFont(gui_default_font);
mem_edit_vram.Draw(video->GetVRAM(), 0x4000, 0);
current_mem_edit = 4;
mem_edit[current_mem_edit].Draw(video->GetVRAM(), 0x4000, 0);
ImGui::PopFont();
ImGui::EndTabItem();
}
Expand Down
2 changes: 2 additions & 0 deletions platforms/desktop-shared/gui_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ EXTERN void gui_debug_reset_breakpoints_cpu(void);
EXTERN void gui_debug_reset_breakpoints_mem(void);
EXTERN void gui_debug_runtocursor(void);
EXTERN void gui_debug_go_back(void);
EXTERN void gui_debug_copy_memory(void);
EXTERN void gui_debug_paste_memory(void);

#undef GUI_DEBUG_IMPORT
#undef EXTERN
Expand Down
33 changes: 27 additions & 6 deletions platforms/desktop-shared/imgui/memory_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ MemEditor::MemEditor()
m_preview_data_type = 0;
m_preview_endianess = 0;
m_jump_to_address = -1;
m_mem_data = NULL;
}

MemEditor::~MemEditor()
Expand All @@ -46,6 +47,8 @@ MemEditor::~MemEditor()

void MemEditor::Draw(uint8_t* mem_data, int mem_size, int base_display_addr)
{
m_mem_data = mem_data;

ImVec4 addr_color = cyan;
ImVec4 ascii_color = magenta;
ImVec4 column_color = yellow;
Expand Down Expand Up @@ -105,8 +108,6 @@ void MemEditor::Draw(uint8_t* mem_data, int mem_size, int base_display_addr)
m_row_scroll_top = ImGui::GetScrollY() / character_size.y;
m_row_scroll_bottom = m_row_scroll_top + (ImGui::GetWindowHeight() / character_size.y);

//ImGui::TableSetupScrollFreeze(0, 1);

ImGui::TableSetupColumn("ADDR");
ImGui::TableSetupColumn("");

Expand Down Expand Up @@ -157,7 +158,7 @@ void MemEditor::Draw(uint8_t* mem_data, int mem_size, int base_display_addr)
}

ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));

if (m_editing_address == byte_address)
{
ImGui::PushItemWidth((character_size).x *2);
Expand Down Expand Up @@ -275,7 +276,7 @@ void MemEditor::Draw(uint8_t* mem_data, int mem_size, int base_display_addr)

}
ImGui::EndChild();

DrawCursors(mem_size, base_display_addr);
DrawDataPreview(m_selection_start, mem_data, mem_size);
DrawOptions();
Expand Down Expand Up @@ -375,7 +376,6 @@ void MemEditor::JumpToAddress(int address)

void MemEditor::DrawCursors(int mem_size, int base_display_addr)
{

ImVec4 color = ImVec4(0.1f,0.9f,0.9f,1.0f);

ImGui::TextColored(color, "REGION:");
Expand All @@ -387,7 +387,7 @@ void MemEditor::DrawCursors(int mem_size, int base_display_addr)
if (m_selection_start == m_selection_end)
ImGui::Text("%04X", m_selection_start);
else
ImGui::Text("%04X-%04X",m_selection_start, m_selection_end);
ImGui::Text("%04X-%04X", m_selection_start, m_selection_end);
ImGui::Separator();
}

Expand Down Expand Up @@ -577,4 +577,25 @@ int MemEditor::DataPreviewSize()
default:
return 1;
}
}

void MemEditor::Copy(uint8_t** data, int* size)
{
int start = m_selection_start;
int end = m_selection_end;

*size = end - start + 1;
*data = m_mem_data + start;
}

void MemEditor::Paste(uint8_t* data, int size)
{
int selection = m_selection_end - m_selection_start + 1;
int start = m_selection_start;
int end = m_selection_start + (size < selection ? size : selection);

for (int i = start; i < end; i++)
{
m_mem_data[i] = data[i - start];
}
}
3 changes: 3 additions & 0 deletions platforms/desktop-shared/imgui/memory_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class MemEditor
~MemEditor();

void Draw(uint8_t* mem_data, int mem_size, int base_display_addr = 0x0000);
void Copy(uint8_t** data, int* size);
void Paste(uint8_t* data, int size);

private:
bool IsColumnSeparator(int current_column, int column_count);
Expand Down Expand Up @@ -60,6 +62,7 @@ class MemEditor
int m_preview_data_type;
int m_preview_endianess;
int m_jump_to_address;
uint8_t* m_mem_data;
};

#endif /* MEM_EDITOR_H */

0 comments on commit 47af433

Please sign in to comment.